60
60
61
61
PARSE_ERRORS = {
62
62
PARSE_UNKNOWN_TOKEN : 'Unknown token' ,
63
- PARSE_UNBALANCED_CLOSING_PARENS : 'Bad closing parenthesis' ,
63
+ PARSE_UNBALANCED_CLOSING_PARENS : 'Unbalanced parenthesis' ,
64
64
PARSE_INVALID_EXPRESSION : 'Invalid expression' ,
65
65
}
66
66
67
67
68
68
class ParseError (Exception ):
69
69
"""
70
- Raised when the parser or tokemizer encounters a syntax error. Instances of
70
+ Raised when the parser or tokenizer encounters a syntax error. Instances of
71
71
this class have attributes token_type, token_string, position, error_code to
72
72
access the details of the error. str() of the exception instance returns a
73
73
formatted message.
@@ -80,21 +80,16 @@ def __init__(self, token_type=None, token_string='', position=-1, error_code=0):
80
80
self .error_code = error_code
81
81
82
82
def __str__ (self , * args , ** kwargs ):
83
- ttype = ''
84
- if self .token_type :
85
- ttype = TOKEN_TYPES .get (self .token_type ) or repr (self .token_type )
86
- ttype = ' for token type: %r' % (ttype )
87
-
88
83
tstr = ''
89
84
if self .token_string :
90
- tstr = ' with value : %r' % (self .token_string )
85
+ tstr = ' for token : %r' % (self .token_string )
91
86
92
87
pos = ''
93
88
if self .position > 0 :
94
89
pos = ' at position: %d' % (self .position )
95
90
emsg = PARSE_ERRORS .get (self .error_code , 'Unknown parsing error' )
96
91
97
- return '{emsg){ttype }{tstr}( pos}.' .format (ttype = ttype , tstr = tstr , pos = pos , emsg = emsg )
92
+ return '{emsg}{tstr}{ pos}.' .format (tstr = tstr , pos = pos , emsg = emsg )
98
93
99
94
100
95
class BooleanAlgebra (object ):
@@ -225,32 +220,36 @@ def parse(self, expr, simplify=False):
225
220
elif token == TOKEN_RPAR :
226
221
while True :
227
222
if ast [0 ] is None :
228
- raise ParseError (token , tokstr , position ,
229
- PARSE_UNBALANCED_CLOSING_PARENS )
223
+ raise ParseError (token , tokstr , position , PARSE_UNBALANCED_CLOSING_PARENS )
230
224
if ast [1 ] is TOKEN_LPAR :
231
225
ast [0 ].append (ast [2 ])
232
226
ast = ast [0 ]
233
227
break
228
+ if isinstance (ast [1 ], int ):
229
+ raise ParseError (token , tokstr , position , PARSE_UNBALANCED_CLOSING_PARENS )
234
230
subex = ast [1 ](* ast [2 :])
235
231
ast [0 ].append (subex )
236
232
ast = ast [0 ]
237
233
else :
238
234
raise ParseError (token , tokstr , position , PARSE_UNKNOWN_TOKEN )
239
235
240
- while True :
241
- if ast [0 ] is None :
242
- if ast [1 ] is None :
243
-
244
- if len (ast ) != 3 :
245
- raise ParseError (error_code = PARSE_INVALID_EXPRESSION )
246
- parsed = ast [2 ]
236
+ try :
237
+ while True :
238
+ if ast [0 ] is None :
239
+ if ast [1 ] is None :
240
+
241
+ if len (ast ) != 3 :
242
+ raise ParseError (error_code = PARSE_INVALID_EXPRESSION )
243
+ parsed = ast [2 ]
244
+ else :
245
+ parsed = ast [1 ](* ast [2 :])
246
+ break
247
247
else :
248
- parsed = ast [1 ](* ast [2 :])
249
- break
250
- else :
251
- subex = ast [1 ](* ast [2 :])
252
- ast [0 ].append (subex )
253
- ast = ast [0 ]
248
+ subex = ast [1 ](* ast [2 :])
249
+ ast [0 ].append (subex )
250
+ ast = ast [0 ]
251
+ except TypeError :
252
+ raise ParseError (error_code = PARSE_INVALID_EXPRESSION )
254
253
255
254
if simplify :
256
255
return parsed .simplify ()
0 commit comments