|
| 1 | +fromlib2to3.pgen2importgrammar |
| 2 | + |
| 3 | +from .importtoken |
| 4 | + |
| 5 | + |
| 6 | +classGrammar(grammar.Grammar): |
| 7 | + |
| 8 | +defproduce_graminit_h(self,writer): |
| 9 | +writer("/* Generated by Parser/pgen2 */\n\n") |
| 10 | +fornumber,symbolinself.number2symbol.items(): |
| 11 | +writer("#define {} {}\n".format(symbol,number)) |
| 12 | + |
| 13 | +defproduce_graminit_c(self,writer): |
| 14 | +writer("/* Generated by Parser/pgen2 */\n\n") |
| 15 | + |
| 16 | +writer('#include "pgenheaders.h"\n') |
| 17 | +writer('#include "grammar.h"\n') |
| 18 | +writer("grammar _PyParser_Grammar;\n") |
| 19 | + |
| 20 | +self.print_dfas(writer) |
| 21 | +self.print_labels(writer) |
| 22 | + |
| 23 | +writer("grammar _PyParser_Grammar = {\n") |
| 24 | +writer(" {n_dfas},\n".format(n_dfas=len(self.dfas))) |
| 25 | +writer(" dfas,\n") |
| 26 | +writer(" {{{n_labels}, labels}},\n".format(n_labels=len(self.labels))) |
| 27 | +writer(" {start_number}\n".format(start_number=self.start)) |
| 28 | +writer("};\n") |
| 29 | + |
| 30 | +defprint_labels(self,writer): |
| 31 | +writer( |
| 32 | +"static label labels[{n_labels}] = {{\n".format(n_labels=len(self.labels)) |
| 33 | + ) |
| 34 | +forlabel,nameinself.labels: |
| 35 | +ifnameisNone: |
| 36 | +writer(" {{{label}, 0}},\n".format(label=label)) |
| 37 | +else: |
| 38 | +writer( |
| 39 | +' {{{label}, "{label_name}"}},\n'.format( |
| 40 | +label=label,label_name=name |
| 41 | + ) |
| 42 | + ) |
| 43 | +writer("};\n") |
| 44 | + |
| 45 | +defprint_dfas(self,writer): |
| 46 | +self.print_states(writer) |
| 47 | +writer("static dfa dfas[{}] = {{\n".format(len(self.dfas))) |
| 48 | +fordfaindex,dfa_eleminenumerate(self.dfas.items()): |
| 49 | +symbol, (dfa,first_sets)=dfa_elem |
| 50 | +writer( |
| 51 | +' {{{dfa_symbol}, "{symbol_name}", '.format( |
| 52 | +dfa_symbol=symbol,symbol_name=self.number2symbol[symbol] |
| 53 | + ) |
| 54 | ++"0, {n_states}, states_{dfa_index},\n".format( |
| 55 | +n_states=len(dfa),dfa_index=dfaindex |
| 56 | + ) |
| 57 | + ) |
| 58 | +writer(' "') |
| 59 | + |
| 60 | +k= [nameforlabel,nameinself.labelsiflabelinfirst_sets] |
| 61 | +bitset=bytearray((len(self.labels)>>3)+1) |
| 62 | +fortokeninfirst_sets: |
| 63 | +bitset[token>>3]|=1<< (token&7) |
| 64 | +forbyteinbitset: |
| 65 | +writer("\\%03o"% (byte&0xFF)) |
| 66 | +writer('"},\n') |
| 67 | +writer("};\n") |
| 68 | + |
| 69 | +defprint_states(self,write): |
| 70 | +fordfaindex,dfainenumerate(self.states): |
| 71 | +self.print_arcs(write,dfaindex,dfa) |
| 72 | +write( |
| 73 | +"static state states_{dfa_index}[{n_states}] = {{\n".format( |
| 74 | +dfa_index=dfaindex,n_states=len(dfa) |
| 75 | + ) |
| 76 | + ) |
| 77 | +forstateindex,stateinenumerate(dfa): |
| 78 | +narcs=len(state) |
| 79 | +write( |
| 80 | +" {{{n_arcs}, arcs_{dfa_index}_{state_index}}},\n".format( |
| 81 | +n_arcs=narcs,dfa_index=dfaindex,state_index=stateindex |
| 82 | + ) |
| 83 | + ) |
| 84 | +write("};\n") |
| 85 | + |
| 86 | +defprint_arcs(self,write,dfaindex,states): |
| 87 | +forstateindex,stateinenumerate(states): |
| 88 | +narcs=len(state) |
| 89 | +write( |
| 90 | +"static arc arcs_{dfa_index}_{state_index}[{n_arcs}] = {{\n".format( |
| 91 | +dfa_index=dfaindex,state_index=stateindex,n_arcs=narcs |
| 92 | + ) |
| 93 | + ) |
| 94 | +fora,binstate: |
| 95 | +write( |
| 96 | +" {{{from_label}, {to_state}}},\n".format( |
| 97 | +from_label=a,to_state=b |
| 98 | + ) |
| 99 | + ) |
| 100 | +write("};\n") |