diff options
author | David Beazley <dave@dabeaz.com> | 2012-04-26 14:42:55 -0500 |
---|---|---|
committer | David Beazley <dave@dabeaz.com> | 2012-04-26 14:42:55 -0500 |
commit | b766b21116b0b4fa14a9b836e1abd805a4bf6064 (patch) | |
tree | e24ebf0009dbabccd176320e11685c7a049536da /test | |
parent | 708ff46356912aa05d8e30a5b84d837d7a65d3b2 (diff) | |
download | ply-b766b21116b0b4fa14a9b836e1abd805a4bf6064.tar.gz |
Changes to panic mode error recovery
Diffstat (limited to 'test')
-rw-r--r-- | test/calclex.py | 2 | ||||
-rw-r--r-- | test/testyacc.py | 18 | ||||
-rw-r--r-- | test/yacc_error5.py | 2 | ||||
-rw-r--r-- | test/yacc_error6.py | 80 | ||||
-rw-r--r-- | test/yacc_error7.py | 80 |
5 files changed, 181 insertions, 1 deletions
diff --git a/test/calclex.py b/test/calclex.py index 302f0b0..030a986 100644 --- a/test/calclex.py +++ b/test/calclex.py @@ -43,7 +43,7 @@ def t_error(t): t.lexer.skip(1) # Build the lexer -lex.lex() +lexer = lex.lex() diff --git a/test/testyacc.py b/test/testyacc.py index cd2ab03..1a98b4f 100644 --- a/test/testyacc.py +++ b/test/testyacc.py @@ -184,6 +184,24 @@ class YaccErrorWarningTests(unittest.TestCase): "Assignment Error at 2:5 to 5:33\n" )) + def test_yacc_error6(self): + run_import("yacc_error6") + result = sys.stdout.getvalue() + self.assert_(check_expected(result, + "a=7\n" + "Line 3: Syntax error at '*'\n" + "c=21\n" + )) + + def test_yacc_error7(self): + run_import("yacc_error7") + result = sys.stdout.getvalue() + self.assert_(check_expected(result, + "a=7\n" + "Line 3: Syntax error at '*'\n" + "c=21\n" + )) + def test_yacc_inf(self): self.assertRaises(ply.yacc.YaccError,run_import,"yacc_inf") result = sys.stderr.getvalue() diff --git a/test/yacc_error5.py b/test/yacc_error5.py index 7cb538e..9eb0f85 100644 --- a/test/yacc_error5.py +++ b/test/yacc_error5.py @@ -78,6 +78,8 @@ def p_error(t): print("Syntax error at '%s'" % t.value) parser = yacc.yacc() +import calclex +calclex.lexer.lineno=1 parser.parse(""" a = 3 + (4*5) + diff --git a/test/yacc_error6.py b/test/yacc_error6.py new file mode 100644 index 0000000..20e8b24 --- /dev/null +++ b/test/yacc_error6.py @@ -0,0 +1,80 @@ +# ----------------------------------------------------------------------------- +# yacc_error6.py +# +# Panic mode recovery test +# ----------------------------------------------------------------------------- +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'), + ) + +def p_statements(t): + 'statements : statements statement' + pass + +def p_statements_1(t): + 'statements : statement' + pass + +def p_statement_assign(p): + 'statement : LPAREN NAME EQUALS expression RPAREN' + print("%s=%s" % (p[2],p[4])) + +def p_statement_expr(t): + 'statement : LPAREN expression RPAREN' + 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_number(t): + 'expression : NUMBER' + t[0] = t[1] + +def p_error(p, parser): + if p: + print("Line %d: Syntax error at '%s'" % (p.lineno, p.value)) + # Scan ahead looking for a name token + while True: + tok = parser.token() + if not tok or tok.type == 'RPAREN': + break + if tok: + parser.restart() + return None + +parser = yacc.yacc() +import calclex +calclex.lexer.lineno=1 + +parser.parse(""" +(a = 3 + 4) +(b = 4 + * 5 - 6 + *) +(c = 10 + 11) +""") + + + + + + diff --git a/test/yacc_error7.py b/test/yacc_error7.py new file mode 100644 index 0000000..0e7c0a7 --- /dev/null +++ b/test/yacc_error7.py @@ -0,0 +1,80 @@ +# ----------------------------------------------------------------------------- +# yacc_error7.py +# +# Panic mode recovery test using deprecated functionality +# ----------------------------------------------------------------------------- +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'), + ) + +def p_statements(t): + 'statements : statements statement' + pass + +def p_statements_1(t): + 'statements : statement' + pass + +def p_statement_assign(p): + 'statement : LPAREN NAME EQUALS expression RPAREN' + print("%s=%s" % (p[2],p[4])) + +def p_statement_expr(t): + 'statement : LPAREN expression RPAREN' + 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_number(t): + 'expression : NUMBER' + t[0] = t[1] + +def p_error(p, parser): + if p: + print("Line %d: Syntax error at '%s'" % (p.lineno, p.value)) + # Scan ahead looking for a name token + while True: + tok = yacc.token() + if not tok or tok.type == 'RPAREN': + break + if tok: + yacc.restart() + return None + +parser = yacc.yacc() +import calclex +calclex.lexer.lineno=1 + +parser.parse(""" +(a = 3 + 4) +(b = 4 + * 5 - 6 + *) +(c = 10 + 11) +""") + + + + + + |