home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / lib2to3 / pgen2 / conv.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  8.0 KB  |  322 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """Convert graminit.[ch] spit out by pgen to Python code.
  5.  
  6. Pgen is the Python parser generator.  It is useful to quickly create a
  7. parser from a grammar file in Python's grammar notation.  But I don't
  8. want my parsers to be written in C (yet), so I'm translating the
  9. parsing tables to Python data structures and writing a Python parse
  10. engine.
  11.  
  12. Note that the token numbers are constants determined by the standard
  13. Python tokenizer.  The standard token module defines these numbers and
  14. their names (the names are not used much).  The token numbers are
  15. hardcoded into the Python tokenizer and into pgen.  A Python
  16. implementation of the Python tokenizer is also available, in the
  17. standard tokenize module.
  18.  
  19. On the other hand, symbol numbers (representing the grammar's
  20. non-terminals) are assigned by pgen based on the actual grammar
  21. input.
  22.  
  23. Note: this module is pretty much obsolete; the pgen module generates
  24. equivalent grammar tables directly from the Grammar.txt input file
  25. without having to invoke the Python pgen C program.
  26.  
  27. """
  28. import re
  29. from pgen2 import grammar, token
  30.  
  31. class Converter(grammar.Grammar):
  32.     '''Grammar subclass that reads classic pgen output files.
  33.  
  34.     The run() method reads the tables as produced by the pgen parser
  35.     generator, typically contained in two C files, graminit.h and
  36.     graminit.c.  The other methods are for internal use only.
  37.  
  38.     See the base class for more documentation.
  39.  
  40.     '''
  41.     
  42.     def run(self, graminit_h, graminit_c):
  43.         '''Load the grammar tables from the text files written by pgen.'''
  44.         self.parse_graminit_h(graminit_h)
  45.         self.parse_graminit_c(graminit_c)
  46.         self.finish_off()
  47.  
  48.     
  49.     def parse_graminit_h(self, filename):
  50.         '''Parse the .h file writen by pgen.  (Internal)
  51.  
  52.         This file is a sequence of #define statements defining the
  53.         nonterminals of the grammar as numbers.  We build two tables
  54.         mapping the numbers to names and back.
  55.  
  56.         '''
  57.         
  58.         try:
  59.             f = open(filename)
  60.         except IOError:
  61.             err = None
  62.             print "Can't open %s: %s" % (filename, err)
  63.             return False
  64.  
  65.         self.symbol2number = { }
  66.         self.number2symbol = { }
  67.         lineno = 0
  68.         for line in f:
  69.             lineno += 1
  70.             mo = re.match('^#define\\s+(\\w+)\\s+(\\d+)$', line)
  71.             if not mo and line.strip():
  72.                 print "%s(%s): can't parse %s" % (filename, lineno, line.strip())
  73.                 continue
  74.             (symbol, number) = mo.groups()
  75.             number = int(number)
  76.             if not symbol not in self.symbol2number:
  77.                 raise AssertionError
  78.             if not number not in self.number2symbol:
  79.                 raise AssertionError
  80.             self.symbol2number[symbol] = number
  81.             self.number2symbol[number] = symbol
  82.         
  83.         return True
  84.  
  85.     
  86.     def parse_graminit_c(self, filename):
  87.         '''Parse the .c file writen by pgen.  (Internal)
  88.  
  89.         The file looks as follows.  The first two lines are always this:
  90.  
  91.         #include "pgenheaders.h"
  92.         #include "grammar.h"
  93.  
  94.         After that come four blocks:
  95.  
  96.         1) one or more state definitions
  97.         2) a table defining dfas
  98.         3) a table defining labels
  99.         4) a struct defining the grammar
  100.  
  101.         A state definition has the following form:
  102.         - one or more arc arrays, each of the form:
  103.           static arc arcs_<n>_<m>[<k>] = {
  104.                   {<i>, <j>},
  105.                   ...
  106.           };
  107.         - followed by a state array, of the form:
  108.           static state states_<s>[<t>] = {
  109.                   {<k>, arcs_<n>_<m>},
  110.                   ...
  111.           };
  112.  
  113.         '''
  114.         
  115.         try:
  116.             f = open(filename)
  117.         except IOError:
  118.             err = None
  119.             print "Can't open %s: %s" % (filename, err)
  120.             return False
  121.  
  122.         lineno = 0
  123.         lineno = lineno + 1
  124.         line = f.next()
  125.         if not line == '#include "pgenheaders.h"\n':
  126.             raise AssertionError, (lineno, line)
  127.         lineno = lineno + 1
  128.         line = f.next()
  129.         if not line == '#include "grammar.h"\n':
  130.             raise AssertionError, (lineno, line)
  131.         lineno = lineno + 1
  132.         line = f.next()
  133.         allarcs = { }
  134.         states = []
  135.         while line.startswith('static arc '):
  136.             while line.startswith('static arc '):
  137.                 mo = re.match('static arc arcs_(\\d+)_(\\d+)\\[(\\d+)\\] = {$', line)
  138.                 if not mo:
  139.                     raise AssertionError, (lineno, line)
  140.                 (n, m, k) = map(int, mo.groups())
  141.                 arcs = []
  142.                 for _ in range(k):
  143.                     lineno = lineno + 1
  144.                     line = f.next()
  145.                     mo = re.match('\\s+{(\\d+), (\\d+)},$', line)
  146.                     if not mo:
  147.                         raise AssertionError, (lineno, line)
  148.                     (i, j) = map(int, mo.groups())
  149.                     arcs.append((i, j))
  150.                 
  151.                 lineno = lineno + 1
  152.                 line = f.next()
  153.                 if not line == '};\n':
  154.                     raise AssertionError, (lineno, line)
  155.                 allarcs[(n, m)] = arcs
  156.                 lineno = lineno + 1
  157.                 line = f.next()
  158.                 continue
  159.                 line == '};\n'
  160.             mo = re.match('static state states_(\\d+)\\[(\\d+)\\] = {$', line)
  161.             if not mo:
  162.                 raise AssertionError, (lineno, line)
  163.             (s, t) = map(int, mo.groups())
  164.             if not s == len(states):
  165.                 raise AssertionError, (lineno, line)
  166.             state = []
  167.             for _ in range(t):
  168.                 lineno = lineno + 1
  169.                 line = f.next()
  170.                 mo = re.match('\\s+{(\\d+), arcs_(\\d+)_(\\d+)},$', line)
  171.                 if not mo:
  172.                     raise AssertionError, (lineno, line)
  173.                 (k, n, m) = map(int, mo.groups())
  174.                 arcs = allarcs[(n, m)]
  175.                 if not k == len(arcs):
  176.                     raise AssertionError, (lineno, line)
  177.                 state.append(arcs)
  178.             
  179.             states.append(state)
  180.             lineno = lineno + 1
  181.             line = f.next()
  182.             if not line == '};\n':
  183.                 raise AssertionError, (lineno, line)
  184.             lineno = lineno + 1
  185.             line = f.next()
  186.             continue
  187.             line == '};\n'
  188.         self.states = states
  189.         dfas = { }
  190.         mo = re.match('static dfa dfas\\[(\\d+)\\] = {$', line)
  191.         if not mo:
  192.             raise AssertionError, (lineno, line)
  193.         ndfas = int(mo.group(1))
  194.         for i in range(ndfas):
  195.             lineno = lineno + 1
  196.             line = f.next()
  197.             mo = re.match('\\s+{(\\d+), "(\\w+)", (\\d+), (\\d+), states_(\\d+),$', line)
  198.             if not mo:
  199.                 raise AssertionError, (lineno, line)
  200.             symbol = mo.group(2)
  201.             (number, x, y, z) = map(int, mo.group(1, 3, 4, 5))
  202.             if not self.symbol2number[symbol] == number:
  203.                 raise AssertionError, (lineno, line)
  204.             if not self.number2symbol[number] == symbol:
  205.                 raise AssertionError, (lineno, line)
  206.             if not x == 0:
  207.                 raise AssertionError, (lineno, line)
  208.             state = states[z]
  209.             if not y == len(state):
  210.                 raise AssertionError, (lineno, line)
  211.             lineno = lineno + 1
  212.             line = f.next()
  213.             mo = re.match('\\s+("(?:\\\\\\d\\d\\d)*")},$', line)
  214.             if not mo:
  215.                 raise AssertionError, (lineno, line)
  216.             first = { }
  217.             rawbitset = eval(mo.group(1))
  218.             for i, c in enumerate(rawbitset):
  219.                 byte = ord(c)
  220.                 for j in range(8):
  221.                     if byte & 1 << j:
  222.                         first[i * 8 + j] = 1
  223.                         continue
  224.                     mo
  225.                 
  226.             
  227.             dfas[number] = (state, first)
  228.         
  229.         lineno = lineno + 1
  230.         line = f.next()
  231.         if not line == '};\n':
  232.             raise AssertionError, (lineno, line)
  233.         self.dfas = dfas
  234.         labels = []
  235.         lineno = lineno + 1
  236.         line = f.next()
  237.         mo = re.match('static label labels\\[(\\d+)\\] = {$', line)
  238.         if not mo:
  239.             raise AssertionError, (lineno, line)
  240.         nlabels = int(mo.group(1))
  241.         for i in range(nlabels):
  242.             lineno = lineno + 1
  243.             line = f.next()
  244.             mo = re.match('\\s+{(\\d+), (0|"\\w+")},$', line)
  245.             if not mo:
  246.                 raise AssertionError, (lineno, line)
  247.             (x, y) = mo.groups()
  248.             x = int(x)
  249.             labels.append((x, y))
  250.         
  251.         lineno = lineno + 1
  252.         line = f.next()
  253.         if not line == '};\n':
  254.             raise AssertionError, (lineno, line)
  255.         self.labels = labels
  256.         lineno = lineno + 1
  257.         line = f.next()
  258.         if not line == 'grammar _PyParser_Grammar = {\n':
  259.             raise AssertionError, (lineno, line)
  260.         lineno = lineno + 1
  261.         line = f.next()
  262.         mo = re.match('\\s+(\\d+),$', line)
  263.         if not mo:
  264.             raise AssertionError, (lineno, line)
  265.         ndfas = int(mo.group(1))
  266.         if not ndfas == len(self.dfas):
  267.             raise AssertionError
  268.         lineno = lineno + 1
  269.         line = f.next()
  270.         if not line == '\tdfas,\n':
  271.             raise AssertionError, (lineno, line)
  272.         lineno = lineno + 1
  273.         line = f.next()
  274.         mo = re.match('\\s+{(\\d+), labels},$', line)
  275.         if not mo:
  276.             raise AssertionError, (lineno, line)
  277.         nlabels = int(mo.group(1))
  278.         if not nlabels == len(self.labels):
  279.             raise AssertionError, (lineno, line)
  280.         lineno = lineno + 1
  281.         line = f.next()
  282.         mo = re.match('\\s+(\\d+)$', line)
  283.         if not mo:
  284.             raise AssertionError, (lineno, line)
  285.         start = int(mo.group(1))
  286.         if not start in self.number2symbol:
  287.             raise AssertionError, (lineno, line)
  288.         self.start = start
  289.         lineno = lineno + 1
  290.         line = f.next()
  291.         if not line == '};\n':
  292.             raise AssertionError, (lineno, line)
  293.         
  294.         try:
  295.             lineno = lineno + 1
  296.             line = f.next()
  297.         except StopIteration:
  298.             line == '};\n'
  299.             line == '};\n'
  300.             start in self.number2symbol
  301.         except:
  302.             mo
  303.  
  304.         if not 0:
  305.             raise AssertionError, (lineno, line)
  306.  
  307.     
  308.     def finish_off(self):
  309.         '''Create additional useful structures.  (Internal).'''
  310.         self.keywords = { }
  311.         self.tokens = { }
  312.         for type, value in enumerate(self.labels):
  313.             if type == token.NAME and value is not None:
  314.                 self.keywords[value] = ilabel
  315.                 continue
  316.             if value is None:
  317.                 self.tokens[type] = ilabel
  318.                 continue
  319.         
  320.  
  321.  
  322.