From 26e8a598be2a221f469e3c501b72c5986764fabc Mon Sep 17 00:00:00 2001 From: "Jeremy G. Siek" Date: Sat, 21 Dec 2024 15:06:34 -0500 Subject: [PATCH] improved error for print and parenthesized term --- rec_desc_parser.py | 18 ++++++++++++++---- test/should-error/paren_term.pf | 9 +++++++++ test/should-error/paren_term.pf.err | 8 ++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 test/should-error/paren_term.pf create mode 100644 test/should-error/paren_term.pf.err diff --git a/rec_desc_parser.py b/rec_desc_parser.py index 1b3dc40..39b5588 100644 --- a/rec_desc_parser.py +++ b/rec_desc_parser.py @@ -250,11 +250,14 @@ def parse_term_hi(): elif token.type == 'LPAR': advance() + while_parsing = 'while parsing parenthesized term\n' \ + + '\tterm ::= "(" term ")"\n' + term = parse_term() if current_token().type != 'RPAR': error(meta_from_tokens(current_token(), current_token()), 'expected closing parentheses, not\n\t' \ - + current_token().value) + + current_token().value + '\n' + while_parsing) advance() return term @@ -1307,10 +1310,17 @@ def parse_statement(): name = parse_identifier() return Import(meta_from_tokens(token, previous_token()), name) elif token.type == 'PRINT': + while_parsing = 'while parsing\n' \ + + '\tstatement ::= "print" term\n' advance() - subject = parse_term() - meta = meta_from_tokens(token, previous_token()) - return Print(meta, subject) + try: + subject = parse_term() + meta = meta_from_tokens(token, previous_token()) + return Print(meta, subject) + except Exception as e: + meta = meta_from_tokens(token, previous_token()) + raise Exception(str(e) + '\n' + error_header(meta) + while_parsing) + elif token.type == 'THEOREM' or token.type == 'LEMMA': return parse_theorem() elif token.type == 'UNION': diff --git a/test/should-error/paren_term.pf b/test/should-error/paren_term.pf new file mode 100644 index 0000000..40443d8 --- /dev/null +++ b/test/should-error/paren_term.pf @@ -0,0 +1,9 @@ +import Nat +import List + +function sum(List) -> Nat { + sum([]) = 0 + sum(node(x, xs)) = x + sum(xs) +} + +print(sum, [1,2,3]) diff --git a/test/should-error/paren_term.pf.err b/test/should-error/paren_term.pf.err new file mode 100644 index 0000000..dd2c472 --- /dev/null +++ b/test/should-error/paren_term.pf.err @@ -0,0 +1,8 @@ +./test/should-error/paren_term.pf:9.10-9.11: expected closing parentheses, not + , +while parsing parenthesized term + term ::= "(" term ")" + +./test/should-error/paren_term.pf:9.1-9.10: while parsing + statement ::= "print" term +