11import collections
22
3- class Grammar :
4- """Pgen parsing tables conversion class.
5-
6- Once initialized, this class supplies the grammar tables for the
7- parsing engine implemented by parse.py. The parsing engine
8- accesses the instance variables directly. The class here does not
9- provide initialization of the tables; several subclasses exist to
10- do this (see the conv and pgen modules).
113
12- The load() method reads the tables from a pickle file, which is
13- much faster than the other ways offered by subclasses. The pickle
14- file is written by calling dump() (after loading the grammar
15- tables using a subclass). The report() method prints a readable
16- representation of the tables to stdout, for debugging.
4+ class Grammar :
5+ """Pgen parsing tables class.
176
187 The instance variables are as follows:
198
@@ -36,8 +25,7 @@ class Grammar:
3625 dfas -- a dict mapping symbol numbers to (DFA, first)
3726 pairs, where DFA is an item from the states list
3827 above, and first is a set of tokens that can
39- begin this grammar rule (represented by a dict
40- whose values are always 1).
28+ begin this grammar rule.
4129
4230 labels -- a list of (x, y) pairs where x is either a token
4331 number or a symbol number, and y is either None
@@ -92,14 +80,12 @@ def print_labels(self, writer):
9280 "static label labels[{n_labels}] = {{\n " .format (n_labels = len (self .labels ))
9381 )
9482 for label , name in self .labels :
95- if name is None :
96- writer (" {{{label}, 0}},\n " .format (label = label ))
97- else :
98- writer (
99- ' {{{label}, "{label_name}"}},\n ' .format (
100- label = label , label_name = name
101- )
83+ label_name = '"{}"' .format (name ) if name is not None else 0
84+ writer (
85+ ' {{{label}, {label_name}}},\n ' .format (
86+ label = label , label_name = label_name
10287 )
88+ )
10389 writer ("};\n " )
10490
10591 def print_dfas (self , writer ):
@@ -114,10 +100,9 @@ def print_dfas(self, writer):
114100 + "0, {n_states}, states_{dfa_index},\n " .format (
115101 n_states = len (dfa ), dfa_index = dfaindex
116102 )
103+ + ' "'
117104 )
118- writer (' "' )
119105
120- k = [name for label , name in self .labels if label in first_sets ]
121106 bitset = bytearray ((len (self .labels ) >> 3 ) + 1 )
122107 for token in first_sets :
123108 bitset [token >> 3 ] |= 1 << (token & 7 )
0 commit comments