1+ import os
2+ import sys
13import collections
2- from lib2to3 . pgen2 import tokenize
4+ import importlib . machinery
35
4- from .import token ,grammar
6+ # Use Lib/token.py and Lib/tokenize.py to obtain the tokens. To maintain this
7+ # compatible with older versions of Python, we need to make sure that we only
8+ # import these two files (and not any of the dependencies of these files).
9+
10+ CURRENT_FOLDER_LOCATION = os .path .dirname (os .path .realpath (__file__ ))
11+ LIB_LOCATION = os .path .realpath (os .path .join (CURRENT_FOLDER_LOCATION ,'..' ,'..' ,'Lib' ))
12+ TOKEN_LOCATION = os .path .join (LIB_LOCATION ,'token.py' )
13+ TOKENIZE_LOCATION = os .path .join (LIB_LOCATION ,'tokenize.py' )
14+
15+ token = importlib .machinery .SourceFileLoader ('token' ,
16+ TOKEN_LOCATION ).load_module ()
17+ # Add token to the module cache so tokenize.py uses that excact one instead of
18+ # the one in the stdlib of the interpreter executing this file.
19+ sys .modules ['token' ]= token
20+ tokenize = importlib .machinery .SourceFileLoader ('tokenize' ,
21+ TOKENIZE_LOCATION ).load_module ()
22+
23+ from .import grammar
524
625class ParserGenerator (object ):
726
8- def __init__ (self ,filename ,tokens , stream = None ,verbose = False ):
27+ def __init__ (self ,filename ,stream = None ,verbose = False ):
928close_stream = None
1029if stream is None :
1130stream = open (filename )
1231close_stream = stream .close
13- self .tokens = dict (token .generate_tokens (tokens ))
14- self .opmap = dict (token .generate_opmap (tokens ))
32+ self .tokens = token
33+ self .opmap = token .EXACT_TOKEN_TYPES
34+ # Manually add <> so it does not collide with !=
35+ self .opmap ['<>' ]= self .tokens .NOTEQUAL
1536self .verbose = verbose
1637self .filename = filename
1738self .stream = stream
@@ -87,9 +108,9 @@ def make_label(self, c, label):
87108return ilabel
88109else :
89110# A named token (NAME, NUMBER, STRING)
90- itoken = self .tokens . get ( label ,None )
111+ itoken = getattr ( self .tokens , label ,None )
91112assert isinstance (itoken ,int ),label
92- assert itoken in self .tokens .values () ,label
113+ assert itoken in self .tokens .tok_name ,label
93114if itoken in c .tokens :
94115return c .tokens [itoken ]
95116else :
@@ -105,12 +126,12 @@ def make_label(self, c, label):
105126if value in c .keywords :
106127return c .keywords [value ]
107128else :
108- c .labels .append ((self .tokens [ ' NAME' ] ,value ))
129+ c .labels .append ((self .tokens . NAME ,value ))
109130c .keywords [value ]= ilabel
110131return ilabel
111132else :
112133# An operator (any non-numeric token)
113- itoken = self .tokens [ self . opmap [value ] ]# Fails if unknown token
134+ itoken = self .opmap [value ]# Fails if unknown token
114135if itoken in c .tokens :
115136return c .tokens [itoken ]
116137else :
@@ -163,16 +184,16 @@ def parse(self):
163184dfas = collections .OrderedDict ()
164185startsymbol = None
165186# MSTART: (NEWLINE | RULE)* ENDMARKER
166- while self .type != self .tokens [ ' ENDMARKER' ] :
167- while self .type == self .tokens [ ' NEWLINE' ] :
187+ while self .type != self .tokens . ENDMARKER :
188+ while self .type == self .tokens . NEWLINE :
168189self .gettoken ()
169190# RULE: NAME ':' RHS NEWLINE
170- name = self .expect (self .tokens [ ' NAME' ] )
191+ name = self .expect (self .tokens . NAME )
171192if self .verbose :
172193print ("Processing rule {dfa_name}" .format (dfa_name = name ))
173- self .expect (self .tokens [ 'OP' ] ,":" )
194+ self .expect (self .tokens . OP ,":" )
174195a ,z = self .parse_rhs ()
175- self .expect (self .tokens [ ' NEWLINE' ] )
196+ self .expect (self .tokens . NEWLINE )
176197if self .verbose :
177198self .dump_nfa (name ,a ,z )
178199dfa = self .make_dfa (a ,z )
@@ -288,7 +309,7 @@ def parse_alt(self):
288309# ALT: ITEM+
289310a ,b = self .parse_item ()
290311while (self .value in ("(" ,"[" )or
291- self .type in (self .tokens [ ' NAME' ] ,self .tokens [ ' STRING' ] )):
312+ self .type in (self .tokens . NAME ,self .tokens . STRING )):
292313c ,d = self .parse_item ()
293314b .addarc (c )
294315b = d
@@ -299,7 +320,7 @@ def parse_item(self):
299320if self .value == "[" :
300321self .gettoken ()
301322a ,z = self .parse_rhs ()
302- self .expect (self .tokens [ 'OP' ] ,"]" )
323+ self .expect (self .tokens . OP ,"]" )
303324a .addarc (z )
304325return a ,z
305326else :
@@ -319,9 +340,9 @@ def parse_atom(self):
319340if self .value == "(" :
320341self .gettoken ()
321342a ,z = self .parse_rhs ()
322- self .expect (self .tokens [ 'OP' ] ,")" )
343+ self .expect (self .tokens . OP ,")" )
323344return a ,z
324- elif self .type in (self .tokens [ ' NAME' ] ,self .tokens [ ' STRING' ] ):
345+ elif self .type in (self .tokens . NAME ,self .tokens . STRING ):
325346a = NFAState ()
326347z = NFAState ()
327348a .addarc (z ,self .value )