summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES4
-rw-r--r--ply/yacc.py14
-rw-r--r--test/testyacc.py8
-rw-r--r--test/yacc_unicode_literals.py70
4 files changed, 92 insertions, 4 deletions
diff --git a/CHANGES b/CHANGES
index 0ecff7f..0111acd 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,10 @@
Version 3.5
---------------------
05/29/13: beazley
+ Fixed yacc validation bugs when from __future__ import unicode_literals
+ is being used. Reported by Kenn Knowles.
+
+05/29/13: beazley
Added support for Travis-CI. Contributed by Kenn Knowles.
05/29/13: beazley
diff --git a/ply/yacc.py b/ply/yacc.py
index 67eff06..7cd0b07 100644
--- a/ply/yacc.py
+++ b/ply/yacc.py
@@ -94,6 +94,12 @@ else:
def func_code(f):
return f.__code__
+# String type-checking compatibility
+if sys.version_info[0] < 3:
+ string_types = basestring
+else:
+ string_types = str
+
# Compatibility
try:
MAXINT = sys.maxint
@@ -2904,7 +2910,7 @@ class ParserReflect(object):
# Validate the start symbol
def validate_start(self):
if self.start is not None:
- if not isinstance(self.start,str):
+ if not isinstance(self.start, string_types):
self.log.error("'start' must be a string")
# Look for error handler
@@ -2990,16 +2996,16 @@ class ParserReflect(object):
self.error = 1
return
assoc = p[0]
- if not isinstance(assoc,str):
+ if not isinstance(assoc, string_types):
self.log.error("precedence associativity must be a string")
self.error = 1
return
for term in p[1:]:
- if not isinstance(term,str):
+ if not isinstance(term, string_types):
self.log.error("precedence items must be strings")
self.error = 1
return
- preclist.append((term,assoc,level+1))
+ preclist.append((term, assoc, level+1))
self.preclist = preclist
# Get all p_functions from the grammar
diff --git a/test/testyacc.py b/test/testyacc.py
index c284444..aa87779 100644
--- a/test/testyacc.py
+++ b/test/testyacc.py
@@ -339,6 +339,7 @@ class YaccErrorWarningTests(unittest.TestCase):
self.assert_(check_expected(result,
"Generating LALR tables\n"
))
+
def test_yacc_sr(self):
run_import("yacc_sr")
result = sys.stderr.getvalue()
@@ -354,6 +355,13 @@ class YaccErrorWarningTests(unittest.TestCase):
"yacc_term1.py:24: Illegal rule name 'NUMBER'. Already defined as a token\n"
))
+ def test_yacc_unicode_literals(self):
+ run_import("yacc_unicode_literals")
+ result = sys.stderr.getvalue()
+ self.assert_(check_expected(result,
+ "Generating LALR tables\n"
+ ))
+
def test_yacc_unused(self):
self.assertRaises(ply.yacc.YaccError,run_import,"yacc_unused")
result = sys.stderr.getvalue()
diff --git a/test/yacc_unicode_literals.py b/test/yacc_unicode_literals.py
new file mode 100644
index 0000000..5ae4f5b
--- /dev/null
+++ b/test/yacc_unicode_literals.py
@@ -0,0 +1,70 @@
+# -----------------------------------------------------------------------------
+# yacc_unicode_literals
+#
+# Test for unicode literals on Python 2.x
+# -----------------------------------------------------------------------------
+from __future__ import unicode_literals
+
+import sys
+
+if ".." not in sys.path: sys.path.insert(0,"..")
+import ply.yacc as yacc
+
+from calclex import tokens
+
+# Parsing rules
+precedence = (
+ ('left','PLUS','MINUS'),
+ ('left','TIMES','DIVIDE'),
+ ('right','UMINUS'),
+ )
+
+# dictionary of names
+names = { }
+
+def p_statement_assign(t):
+ 'statement : NAME EQUALS expression'
+ names[t[1]] = t[3]
+
+def p_statement_expr(t):
+ 'statement : expression'
+ print(t[1])
+
+def p_expression_binop(t):
+ '''expression : expression PLUS expression
+ | expression MINUS expression
+ | expression TIMES expression
+ | expression DIVIDE expression'''
+ if t[2] == '+' : t[0] = t[1] + t[3]
+ elif t[2] == '-': t[0] = t[1] - t[3]
+ elif t[2] == '*': t[0] = t[1] * t[3]
+ elif t[2] == '/': t[0] = t[1] / t[3]
+
+def p_expression_uminus(t):
+ 'expression : MINUS expression %prec UMINUS'
+ t[0] = -t[2]
+
+def p_expression_group(t):
+ 'expression : LPAREN expression RPAREN'
+ t[0] = t[2]
+
+def p_expression_number(t):
+ 'expression : NUMBER'
+ t[0] = t[1]
+
+def p_expression_name(t):
+ 'expression : NAME'
+ try:
+ t[0] = names[t[1]]
+ except LookupError:
+ print("Undefined name '%s'" % t[1])
+ t[0] = 0
+
+def p_error(t):
+ print("Syntax error at '%s'" % t.value)
+
+yacc.yacc()
+
+
+
+