Skip to content

Commit 350f0bf

Browse files
committed
python3 support
1 parent 8ee00f3 commit 350f0bf

File tree

4 files changed

+175
-153
lines changed

4 files changed

+175
-153
lines changed

__init__.py

+18-13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
from ply import lex, yacc
55
from collections import namedtuple, deque
66

7+
try:
8+
unicode
9+
except NameError: # python 3
10+
unicode = str
11+
712

813
def oprex(source_code):
914
source_lines = sanitize(source_code)
@@ -93,7 +98,7 @@ def is_blank_or_comments_only(line):
9398
'STRING',
9499
'VARNAME',
95100
'WHITESPACE',
96-
] + reserved.values()
101+
] + list(reserved.values())
97102

98103
GLOBALMARK = '*)'
99104
t_AT = r'\@'
@@ -250,7 +255,7 @@ def check_unused_vars(self, useds):
250255

251256
class Scope(dict):
252257
types = ('ROOTSCOPE', 'BLOCKSCOPE', 'FLAGSCOPE')
253-
ROOTSCOPE, BLOCKSCOPE, FLAGSCOPE = range(3)
258+
ROOTSCOPE, BLOCKSCOPE, FLAGSCOPE = tuple(range(3))
254259
__slots__ = ('starting_lineno', 'type')
255260
def __init__(self, type, starting_lineno, parent_scope):
256261
self.starting_lineno = starting_lineno
@@ -400,7 +405,7 @@ def negated(self):
400405
class CCItem(namedtuple('CCItem', 'source type value')):
401406
__slots__ = ()
402407
op_types = ('unary', 'binary')
403-
UNARY_OP, BINARY_OP = range(2)
408+
UNARY_OP, BINARY_OP = tuple(range(2))
404409

405410
@staticmethod
406411
def token(t, type, value):
@@ -410,7 +415,7 @@ def token(t, type, value):
410415
regexlib.compile('[' + value + ']')
411416
except regexlib.error as e:
412417
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)))
414419
t.type = 'CHAR'
415420
t.value = CCItem(source, type, value)
416421
return t
@@ -525,7 +530,7 @@ def t_CHARCLASS_prop(t):
525530

526531
def t_CHARCLASS_name(t):
527532
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('_', ' '))
529534

530535

531536
def t_CHARCLASS_escape(t):
@@ -554,7 +559,7 @@ def t_FLAGSET(t):
554559
r'\([- \t\w]+\)'
555560
flags = t.value[1:-1] # exclude the surrounding ( )
556561
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
558563
turn_ons = ''
559564
turn_offs = ''
560565
for flag in flags:
@@ -575,7 +580,7 @@ def t_FLAGSET(t):
575580
regexlib.compile('(?V1)' + test)
576581
except Exception as e:
577582
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)))
579584
else:
580585
t.type = 'LPAREN'
581586
t.extra_tokens = [ExtraToken(t, 'FLAGSET', value=flags), ExtraToken(t, 'RPAREN')]
@@ -624,7 +629,7 @@ def t_STRING(t):
624629
try:
625630
t.value = OVERESCAPED_RE.sub(restore_overescaped, value)
626631
except KeyError as e:
627-
raise OprexSyntaxError(t.lineno, e.message)
632+
raise OprexSyntaxError(t.lineno, str(e)[1:-1])
628633
else:
629634
return t
630635

@@ -1267,7 +1272,7 @@ def p_flagged_expr(t):
12671272
def p_scoped_flags(t):
12681273
'''scoped_flags : LPAREN FLAGSET RPAREN WHITESPACE'''
12691274
flags = t[2]
1270-
for flag_name, global_flag in Flagset.globals.iteritems():
1275+
for flag_name, global_flag in Flagset.globals.items():
12711276
if global_flag in flags.turn_ons:
12721277
raise OprexSyntaxError(t.lineno(2), "'%s' is a global flag and must be set using global flag syntax, not scoped." % flag_name)
12731278
t[0] = flags
@@ -1612,7 +1617,7 @@ def lookup(varname):
16121617
try:
16131618
var = scope[varname]
16141619
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])
16161621
if not isinstance(var.value, CharClass):
16171622
raise OprexSyntaxError(self.lineno, "Cannot include '%s': not a character class" % varname)
16181623
else:
@@ -1682,7 +1687,7 @@ def p_ranged_char(t):
16821687
regexlib.compile('[%s]' % value)
16831688
except regexlib.error as e:
16841689
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)))
16861691

16871692
t[0] = CCItem(source, 'range', value)
16881693

@@ -1782,7 +1787,7 @@ def put_in_scope(var):
17821787
raise OprexSyntaxError(t.lineno(1), errmsg)
17831788
return var
17841789

1785-
list_of_variables = map(variable_from, assignment.declarations)
1790+
list_of_variables = list(map(variable_from, assignment.declarations))
17861791
t[0] = list_of_variables
17871792

17881793

@@ -1969,4 +1974,4 @@ def check_unclosed_scope():
19691974
with codecs.open(source_file, 'r') as f:
19701975
source_code = f.read()
19711976

1972-
print oprex(source_code)
1977+
print(oprex(source_code))

0 commit comments

Comments
 (0)