4
4
from ply import lex , yacc
5
5
from collections import namedtuple , deque
6
6
7
+ try :
8
+ unicode
9
+ except NameError : # python 3
10
+ unicode = str
11
+
7
12
8
13
def oprex (source_code ):
9
14
source_lines = sanitize (source_code )
@@ -93,7 +98,7 @@ def is_blank_or_comments_only(line):
93
98
'STRING' ,
94
99
'VARNAME' ,
95
100
'WHITESPACE' ,
96
- ] + reserved .values ()
101
+ ] + list ( reserved .values () )
97
102
98
103
GLOBALMARK = '*)'
99
104
t_AT = r'\@'
@@ -250,7 +255,7 @@ def check_unused_vars(self, useds):
250
255
251
256
class Scope (dict ):
252
257
types = ('ROOTSCOPE' , 'BLOCKSCOPE' , 'FLAGSCOPE' )
253
- ROOTSCOPE , BLOCKSCOPE , FLAGSCOPE = range (3 )
258
+ ROOTSCOPE , BLOCKSCOPE , FLAGSCOPE = tuple ( range (3 ) )
254
259
__slots__ = ('starting_lineno' , 'type' )
255
260
def __init__ (self , type , starting_lineno , parent_scope ):
256
261
self .starting_lineno = starting_lineno
@@ -400,7 +405,7 @@ def negated(self):
400
405
class CCItem (namedtuple ('CCItem' , 'source type value' )):
401
406
__slots__ = ()
402
407
op_types = ('unary' , 'binary' )
403
- UNARY_OP , BINARY_OP = range (2 )
408
+ UNARY_OP , BINARY_OP = tuple ( range (2 ) )
404
409
405
410
@staticmethod
406
411
def token (t , type , value ):
@@ -410,7 +415,7 @@ def token(t, type, value):
410
415
regexlib .compile ('[' + value + ']' )
411
416
except regexlib .error as e :
412
417
raise OprexSyntaxError (t .lineno ,
413
- '%s compiles to %s which is rejected by the regex engine with error message: %s' % (source , value , e . message ))
418
+ '%s compiles to %s which is rejected by the regex engine with error message: %s' % (source , value , str ( e ) ))
414
419
t .type = 'CHAR'
415
420
t .value = CCItem (source , type , value )
416
421
return t
@@ -525,7 +530,7 @@ def t_CHARCLASS_prop(t):
525
530
526
531
def t_CHARCLASS_name (t ):
527
532
r''':[\w-]+'''
528
- return CCItem .token (t , 'name' , '\N{%s} ' % t .value [1 :].replace ('_' , ' ' ))
533
+ return CCItem .token (t , 'name' , r '\N{%s}' % t .value [1 :].replace ('_' , ' ' ))
529
534
530
535
531
536
def t_CHARCLASS_escape (t ):
@@ -554,7 +559,7 @@ def t_FLAGSET(t):
554
559
r'\([- \t\w]+\)'
555
560
flags = t .value [1 :- 1 ] # exclude the surrounding ( )
556
561
flags = flags .split (' ' ) # will contain empty strings in case of consecutive spaces, so...
557
- flags = filter ( lambda flag : flag , flags ) # ...exclude empty strings
562
+ flags = [ flag for flag in flags if flag ] # ...exclude empty strings
558
563
turn_ons = ''
559
564
turn_offs = ''
560
565
for flag in flags :
@@ -575,7 +580,7 @@ def t_FLAGSET(t):
575
580
regexlib .compile ('(?V1)' + test )
576
581
except Exception as e :
577
582
raise OprexSyntaxError (t .lineno , '%s compiles to %s which is rejected by the regex engine with error message: %s' %
578
- (t .value , test , str (e . message )))
583
+ (t .value , test , str (e )))
579
584
else :
580
585
t .type = 'LPAREN'
581
586
t .extra_tokens = [ExtraToken (t , 'FLAGSET' , value = flags ), ExtraToken (t , 'RPAREN' )]
@@ -624,7 +629,7 @@ def t_STRING(t):
624
629
try :
625
630
t .value = OVERESCAPED_RE .sub (restore_overescaped , value )
626
631
except KeyError as e :
627
- raise OprexSyntaxError (t .lineno , e . message )
632
+ raise OprexSyntaxError (t .lineno , str ( e )[ 1 : - 1 ] )
628
633
else :
629
634
return t
630
635
@@ -1267,7 +1272,7 @@ def p_flagged_expr(t):
1267
1272
def p_scoped_flags (t ):
1268
1273
'''scoped_flags : LPAREN FLAGSET RPAREN WHITESPACE'''
1269
1274
flags = t [2 ]
1270
- for flag_name , global_flag in Flagset .globals .iteritems ():
1275
+ for flag_name , global_flag in Flagset .globals .items ():
1271
1276
if global_flag in flags .turn_ons :
1272
1277
raise OprexSyntaxError (t .lineno (2 ), "'%s' is a global flag and must be set using global flag syntax, not scoped." % flag_name )
1273
1278
t [0 ] = flags
@@ -1612,7 +1617,7 @@ def lookup(varname):
1612
1617
try :
1613
1618
var = scope [varname ]
1614
1619
except KeyError as e :
1615
- raise OprexSyntaxError (self .lineno , "Cannot include '%s': not defined" % e . message )
1620
+ raise OprexSyntaxError (self .lineno , "Cannot include '%s': not defined" % str ( e )[ 1 : - 1 ] )
1616
1621
if not isinstance (var .value , CharClass ):
1617
1622
raise OprexSyntaxError (self .lineno , "Cannot include '%s': not a character class" % varname )
1618
1623
else :
@@ -1682,7 +1687,7 @@ def p_ranged_char(t):
1682
1687
regexlib .compile ('[%s]' % value )
1683
1688
except regexlib .error as e :
1684
1689
raise OprexSyntaxError (t .lineno (0 ),
1685
- '%s compiles to [%s] which is rejected by the regex engine with error message: %s' % (source , value , e . message ))
1690
+ '%s compiles to [%s] which is rejected by the regex engine with error message: %s' % (source , value , str ( e ) ))
1686
1691
1687
1692
t [0 ] = CCItem (source , 'range' , value )
1688
1693
@@ -1782,7 +1787,7 @@ def put_in_scope(var):
1782
1787
raise OprexSyntaxError (t .lineno (1 ), errmsg )
1783
1788
return var
1784
1789
1785
- list_of_variables = map (variable_from , assignment .declarations )
1790
+ list_of_variables = list ( map (variable_from , assignment .declarations ) )
1786
1791
t [0 ] = list_of_variables
1787
1792
1788
1793
@@ -1969,4 +1974,4 @@ def check_unclosed_scope():
1969
1974
with codecs .open (source_file , 'r' ) as f :
1970
1975
source_code = f .read ()
1971
1976
1972
- print oprex (source_code )
1977
+ print ( oprex (source_code ) )
0 commit comments