@@ -27,23 +27,32 @@ def asdict(self, with_message=True):
27
27
else "unexpected_character" ,
28
28
"lineno" : self .exception .line ,
29
29
"column" : self .exception .column ,
30
- "found_type" : self .exception .token .type .lower (),
31
- "found_value" : self .exception .token .value ,
32
- "expected" : sorted (x for x in self .exception .accepts if "__ANON" not in x ),
33
30
"line" : self .filecontent .split ("\n " )[self .exception .line - 1 ],
34
31
** ({"message" : str (self )} if with_message else {}),
32
+ ** ({
33
+ "found_type" : self .exception .token .type .lower (),
34
+ "found_value" : self .exception .token .value ,
35
+ "expected" : sorted (x for x in self .exception .accepts if "__ANON" not in x ),
36
+ } if hasattr (self .exception , 'token' ) and self .exception .token else {})
35
37
}
36
38
37
39
def __str__ (self ):
38
40
d = self .asdict (with_message = False )
39
- if len (d ["expected" ]) == 1 :
41
+ if not d .get ('expected' ):
42
+ exp = None
43
+ elif len (d ["expected" ]) == 1 :
40
44
exp = d ["expected" ][0 ]
41
45
else :
42
46
exp = f"one of { ' ' .join (d ['expected' ])} "
43
47
44
48
sth = "character" if d ["type" ] == "unexpected_character" else ""
45
49
46
- return f"On line { d ['lineno' ]} column { d ['column' ]} :\n Unexpected { sth } { d ['found_type' ]} ('{ d ['found_value' ]} ')\n Expecting { exp } \n { d ['lineno' ]:05d} | { d ['line' ]} \n { ' ' * (self .exception .column - 1 )} ^"
50
+ if d .get ('found_type' ) and d .get ('found_value' ):
51
+ sth += f"{ d ['found_type' ]} ('{ d ['found_value' ]} ')"
52
+
53
+ formatted_exp = f'Expecting { exp } \n ' if exp else ''
54
+
55
+ return f"On line { d ['lineno' ]} column { d ['column' ]} :\n Unexpected { sth } \n { formatted_exp } { d ['lineno' ]:05d} | { d ['line' ]} \n { ' ' * (self .exception .column - 1 )} ^"
47
56
48
57
49
58
class DuplicateNameError (ValidationError ):
@@ -292,7 +301,7 @@ def process_tree(filecontent, file_tree, with_progress):
292
301
def parse (* , filename = None , filecontent = None , with_progress = False , with_tree = True ):
293
302
if filename :
294
303
assert not filecontent
295
- filecontent = open (filename , encoding = None ).read ()
304
+ filecontent = open (filename , encoding = 'latin-1' ).read ()
296
305
297
306
# Match and remove the comments
298
307
p = r"/\*[\s\S]*?\*/"
0 commit comments