1+ import _ast as ast
2+
13
2- import iast as ast
34class ParserError (BaseException ):
4- def __init__ (self ,name ,help , line ,errcode = 0 ):
5+ def __init__ (self , name , _help , line , errcode = 0 ):
56 self .name = name
6- self .help = help
7- self .line = line + 1
7+ self .help = _help
8+ self .line = line + 1
89 self .errcode = errcode
910
1011 def __str__ (self ):
11- return "Parser error: " + self .name .upper () + "(errno " + str (self .errcode ) + ") - (line " + str (self .line ) + "):\n " + self .help
12-
12+ return (
13+ "Parser error: "
14+ + self .name .upper ()
15+ + "(errno "
16+ + str (self .errcode )
17+ + ") - (line "
18+ + str (self .line )
19+ + "):\n "
20+ + self .help
21+ )
1322
1423
1524class Parser :
16- def __init__ (self ,tokens ):
25+ def __init__ (self , tokens ):
1726 self .tokens = tokens
1827
19- def tokenstolist (self ,tokenlist ):
28+ def tokens_to_list (self , tokens ):
2029 l = []
21- for token in tokenlist :
30+ for token in tokens :
2231 l .append (token .type )
2332 return l
2433
25- def parseoneof (self ,tokens ,line ,local ,funcs ):
34+ def parse_one_of (self , tokens , line , local , funcs ):
2635 for f in funcs :
27- retval = f (tokens ,line ,local )
36+ retval = f (tokens , line , local )
2837 if retval is not None :
2938 return retval
30- def splittokens (self ,tokens ,splitter ):
39+
40+ def split_tokens (self , tokens , splitter ):
3141 tl = [[]]
3242 i = 0
3343 for t in tokens :
3444 if t .type == splitter :
3545 i += 1
3646 tl .append ([])
37- else : tl [i ].append (t )
47+ else :
48+ tl [i ].append (t )
3849 return tl
3950
40- def parse_function_definition (self ,tokens ,line ,local ):
51+ def parse_function_definition (self , tokens , line , local ):
4152 if tokens [0 ].type == "BASETYPE" :
4253 if tokens [1 ].type == "NAME" :
43- split = self .splittokens (tokens ,"CLAMP_CLOSE" )
44- if len (split )== 2 :
54+ split = self .split_tokens (tokens , "CLAMP_CLOSE" )
55+ if len (split ) == 2 :
4556 split [0 ] = split [0 ][1 :]
4657 if tokens [3 ].type == "CLAMP_OPEN" :
4758 pass
4859
49-
50-
51- def parse_value (self ,tokens ,line ,local ,list = 0 ):
60+ def parse_value (self , tokens , line , local , list = 0 ):
5261 if len (tokens ) == 1 :
53- if tokens [0 ].type in ["BOOL" ,"STRING" ,"FLOAT" ,"INT" ,"HEX" ]:
54- return ast .StaticValue (tokens [0 ].type .lower (),tokens [0 ].value )
62+ if tokens [0 ].type in ["BOOL" , "STRING" , "FLOAT" , "INT" , "HEX" ]:
63+ return ast .StaticValue (tokens [0 ].type .lower (), tokens [0 ].value )
5564 elif tokens [0 ].value == "null" :
56- return ast .StaticValue ("null" ,"null" )
65+ return ast .StaticValue ("null" , "null" )
5766 print (tokens )
5867 if tokens [0 ].type == "INDEX_OPEN" and tokens [- 1 ].type == "INDEX_CLOSE" :
5968 typ = None
60- lis = ast .StaticList (None ,[],list + 1 )
69+ lis = ast .StaticList (None , [], list + 1 )
6170 if tokens [1 :- 1 ] == []:
6271 typ = "emptylist"
6372 lis .type = typ
6473 return lis
65- for buf in self .splittokens (tokens [1 :- 1 ],"SEPERATOR" ):
66- tree = self .parseoneof (buf ,line ,local ,[self .parse_value ])
67- if typ is None : typ = tree .type
68- if tree .type != typ : typ = "dynamic"
74+ for buf in self .split_tokens (tokens [1 :- 1 ], "SEPERATOR" ):
75+ tree = self .parse_one_of (buf , line , local , [self .parse_value ])
76+ if typ is None :
77+ typ = tree .type
78+ if tree .type != typ :
79+ typ = "dynamic"
6980 lis .values .append (tree )
7081 if tokens [1 :- 1 ] == []:
7182 typ = "emptylist"
7283 lis .type = typ
7384 return lis
7485
75-
76-
77- def parse (self ,tokens = None ,start :ast .AST = ast .Main (),local = 0 ,startline = 0 ):
78- line = startline
86+ def parse (self , tokens = None , start : ast .AST = ast .Main (), local = 0 , start_line = 0 ):
87+ line = start_line
7988 if tokens is None :
8089 tokens = self .tokens
8190 print ("parsing..." )
8291 index = 0
8392 buffer = []
8493 block = 0
8594 while index < len (tokens ):
86- #print(buffer)
95+ # print(buffer)
8796 if tokens [index ].type == "NEWLINE" :
8897 line += 1
89- else : buffer .append (tokens [index ])
98+ else :
99+ buffer .append (tokens [index ])
90100 if tokens [index ].type == "BLOCK_OPEN" :
91101 block += 1
92102 elif tokens [index ].type == "BLOCK_CLOSE" :
93103 block -= 1
94- ast .delete_locals (block + 1 )
104+ ast .delete_locals (block + 1 )
95105 if block == 0 and tokens [index ].type == "END_CMD" :
96- tree = self .parseoneof (buffer ,line ,local + block ,[self .parse_define_variable , self .parse_import ])
97- #if tree is None: pass
106+ tree = self .parse_one_of (
107+ buffer ,
108+ line ,
109+ local + block ,
110+ [self .parse_define_variable , self .parse_import ],
111+ )
112+ # if tree is None: pass
98113 start .last ().nexttask = tree
99114 buffer = []
100115 index += 1
101116 return start
102117
103- def parse_import (self ,tokens ,line ,local ):
118+ def parse_import (self , tokens , line , local ):
104119 if tokens [0 ].type == "IMPORT" :
105120 if tokens [1 ].type == "NAME" :
106- #if len(tokens) == 3 and tokens[2].type == "END_CMD":
121+ # if len(tokens) == 3 and tokens[2].type == "END_CMD":
107122 return ast .Import (tokens [1 ].value )
108123
109- else : raise ParserError ("notaname" ,
110- "Expected a module name after 'import'" ,
111- line )
124+ else :
125+ raise ParserError (
126+ "notaname" , "Expected a module name after 'import'" , line
127+ )
112128
113-
114- def parse_define_variable (self ,tokens ,line ,local ):
115- tl : list = self .tokenstolist (tokens )
116- #valid = True
129+ def parse_define_variable (self , tokens , line , local ):
130+ tl : list = self .tokens_to_list (tokens )
131+ # valid = True
117132 indef = False
118133 listdimension = 0
119134 if tl [0 ] == "INDEFINITE" :
@@ -126,50 +141,112 @@ def parse_define_variable(self,tokens,line,local):
126141 while tl [i ] == "INDEX_OPEN" and tl [i + 1 ] == "INDEX_CLOSE" :
127142 listdimension += 1
128143 i += 2
129- if tl [i - 1 ] != "INDEX_CLOSE" :
130- raise ParserError ("unclosedindex" , """In this line there is an unclosed '['. This is needed to have a working list""" , line )
144+ if tl [i - 1 ] != "INDEX_CLOSE" :
145+ raise ParserError (
146+ "unclosedindex" ,
147+ """In this line there is an unclosed '['. This is needed to have a working list""" ,
148+ line ,
149+ )
131150
132151 tl = [tl [0 ]] + tl [i :]
133- #print(tl)
152+ # print(tl)
134153 tokens = [tokens [0 ]] + tokens [i :]
135154
136155 if tl [1 ] == "NAME" :
137- if tl [2 ] == "END_CMD" : # e.g. ?int my_int;
156+ if tl [2 ] == "END_CMD" : # e.g. ?int my_int;
138157 if not tokens [1 ].value in ast .known_vars :
139158 print ("Variable found" )
140- ast .known_vars [tokens [1 ].value ] = ast .Variable (tokens [1 ].value ,tokens [0 ].value ,local ,listdimension ,line ,indef )
141- return ast .DefineVariableNovalue (tokens [1 ].value ,tokens [0 ].value ,listdimension ,indef )
159+ ast .known_vars [tokens [1 ].value ] = ast .Variable (
160+ tokens [1 ].value ,
161+ tokens [0 ].value ,
162+ local ,
163+ listdimension ,
164+ line ,
165+ indef ,
166+ )
167+ return ast .DefineVariableNovalue (
168+ tokens [1 ].value , tokens [0 ].value , listdimension , indef
169+ )
142170 else :
143- raise ParserError ("varoverlap" ,
144- "This variable seems overlapping with an already existing one ('" + str (ast .known_vars [tokens [1 ].value ].name ) + "', line " + str (ast .known_vars [tokens [1 ].value ].line ) + ")" ,
145- line )
171+ raise ParserError (
172+ "varoverlap" ,
173+ "This variable seems overlapping with an already existing one ('"
174+ + str (ast .known_vars [tokens [1 ].value ].name )
175+ + "', line "
176+ + str (ast .known_vars [tokens [1 ].value ].line )
177+ + ")" ,
178+ line ,
179+ )
146180 elif tl [2 ] == "SET" :
147- if tl [3 ] == "END_CMD" : raise ParserError ("nosetvalue" ,
148- "It looks like you forgot to set a value here or you put in a '=' where you didn't want it." ,
149- line )
181+ if tl [3 ] == "END_CMD" :
182+ raise ParserError (
183+ "nosetvalue" ,
184+ "It looks like you forgot to set a value here or you put in a '=' where you didn't want it." ,
185+ line ,
186+ )
150187 elif tl [- 1 ] == "END_CMD" :
151- #print(tokens[3:-1])
152- tree = self .parseoneof (tokens [3 :- 1 ],line ,local ,[self .parse_value ])
153- if tree is not None and (tree .type == tokens [0 ].value or tokens [0 ].value == "dynamic" or (indef and tree .type == "null" ) or tree .type == "emptylist" ):
188+ # print(tokens[3:-1])
189+ tree = self .parse_one_of (
190+ tokens [3 :- 1 ], line , local , [self .parse_value ]
191+ )
192+ if tree is not None and (
193+ tree .type == tokens [0 ].value
194+ or tokens [0 ].value == "dynamic"
195+ or (indef and tree .type == "null" )
196+ or tree .type == "emptylist"
197+ ):
154198 if not tokens [1 ].value in ast .known_vars :
155- ast .known_vars [tokens [1 ].value ] = ast .Variable (tokens [1 ].value , tokens [0 ].value , local ,
156- listdimension , line , indef )
157- return ast .DefineVariable (tokens [1 ].value , tokens [0 ].value , listdimension , indef ,tree )
199+ ast .known_vars [tokens [1 ].value ] = ast .Variable (
200+ tokens [1 ].value ,
201+ tokens [0 ].value ,
202+ local ,
203+ listdimension ,
204+ line ,
205+ indef ,
206+ )
207+ return ast .DefineVariable (
208+ tokens [1 ].value ,
209+ tokens [0 ].value ,
210+ listdimension ,
211+ indef ,
212+ tree ,
213+ )
158214 else :
159- raise ParserError ("varoverlap" ,
160- "This variable seems overlapping with an already existing one ('" + str (
161- ast .known_vars [tokens [1 ].value ].name ) + "', line " + str (
162- ast .known_vars [tokens [1 ].value ].line ) + ")" ,
163- line )
164- raise ParserError ("unmatchingtype" ,
165- "The return type of this variable (" + str (tree ) + ") does not match the vars expected (" + int (indef )* "?" + str (tokens [0 ].value ) + listdimension * "[]" + ")" ,
166- line )
215+ raise ParserError (
216+ "varoverlap" ,
217+ "This variable seems overlapping with an already existing one ('"
218+ + str (ast .known_vars [tokens [1 ].value ].name )
219+ + "', line "
220+ + str (ast .known_vars [tokens [1 ].value ].line )
221+ + ")" ,
222+ line ,
223+ )
224+ raise ParserError (
225+ "unmatchingtype" ,
226+ "The return type of this variable ("
227+ + str (tree )
228+ + ") does not match the vars expected ("
229+ + int (indef ) * "?"
230+ + str (tokens [0 ].value )
231+ + listdimension * "[]"
232+ + ")" ,
233+ line ,
234+ )
167235 else :
168- raise ParserError ("noendcmd" ,
169- "This Command seems to have no ';', which is required at the end of every Command" ,
170- line )
236+ raise ParserError (
237+ "noendcmd" ,
238+ "This Command seems to have no ';', which is required at the end of every Command" ,
239+ line ,
240+ )
171241 else :
172- raise ParserError ("noendcmd" , """This Command seems to have no ';', which is required at the end of every Command""" , line )
242+ raise ParserError (
243+ "noendcmd" ,
244+ """This Command seems to have no ';', which is required at the end of every Command""" ,
245+ line ,
246+ )
173247 elif indef :
174- raise ParserError ("unusedindef" ,"""The '?' in this line could not be used, this could be because the rest of the declaration is wrong.""" ,line )
175-
248+ raise ParserError (
249+ "unusedindef" ,
250+ """The '?' in this line could not be used, this could be because the rest of the declaration is wrong.""" ,
251+ line ,
252+ )
0 commit comments