From 662a8a16942c0736e4e97cca1d504677ffc63f21 Mon Sep 17 00:00:00 2001 From: "Mike D. Lowis" Date: Thu, 19 Jul 2012 16:58:42 -0400 Subject: [PATCH] Added tests for error conditions in parsing rules --- source/parser.scm | 3 +-- tests/test_parser.scm | 51 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/source/parser.scm b/source/parser.scm index 2973488..6c62147 100644 --- a/source/parser.scm +++ b/source/parser.scm @@ -106,8 +106,7 @@ node) (define (dlang/basic-expr in) - (define tok (buf-lookahead! in 1)) - (if (equal? 'lpar (token-type tok)) + (if (token-matches? in 'lpar) (dlang/operator-app in) (dlang/literal in))) diff --git a/tests/test_parser.scm b/tests/test_parser.scm index 96eb831..da99c1f 100644 --- a/tests/test_parser.scm +++ b/tests/test_parser.scm @@ -290,6 +290,28 @@ (syntree 'number "1.0" '()) (syntree 'number "2.0" '()))))))) +(def-test "dlang/operator-app should error when first paren missing" + (call-with-input-string "1.0 * 2.0)" + (lambda (input) + (define lxr (make-lexer input)) + (check-exception + "Expected a token of type 'lpar, received 'number instead" + (dlang/operator-app lxr))))) + +(def-test "dlang/operator-app should error when second paren missing" + (call-with-input-string "(1.0 * 2.0" + (lambda (input) + (define lxr (make-lexer input)) + (check-exception "Expected a token of type 'rpar, received EOF instead" + (dlang/operator-app lxr))))) + +(def-test "dlang/operator-app should error operator is not an id" + (call-with-input-string "(1.0 2.0 3.0)" + (lambda (input) + (define lxr (make-lexer input)) + (check-exception "Expected a token of type 'id, received 'number instead" + (dlang/operator-app lxr))))) + ; dlang/operator ;------------------------------------------------------------------------------ (def-test "dlang/operator should parse an Id" @@ -405,6 +427,27 @@ (syntree 'number "1.0" '()) (syntree 'symbol "$c" '()))))))) +(def-test "dlang/arg-list should error when first paren missing" + (call-with-input-string ")" + (lambda (input) + (define lxr (make-lexer input)) + (check-exception "Expected a token of type 'lpar, received 'rpar instead" + (dlang/arg-list lxr))))) + +(def-test "dlang/arg-list should error when second paren missing" + (call-with-input-string "(" + (lambda (input) + (define lxr (make-lexer input)) + (check-exception "Expected a literal" + (dlang/arg-list lxr))))) + +(def-test "dlang/arg-list should error when comma missing between args" + (call-with-input-string "(a b)" + (lambda (input) + (define lxr (make-lexer input)) + (check-exception "Expected a token of type 'comma, received 'id instead" + (dlang/arg-list lxr))))) + ; dlang/arg-list? ;------------------------------------------------------------------------------ (def-test "dlang/arg-list? should return true if input contains an arg list" @@ -517,3 +560,11 @@ (list (syntree 'number "1.0" '()) (syntree 'number "2.0" '()))))))) + +(def-test "dlang/expr-block should error when no terminator found" + (call-with-input-string "1.0 2.0" + (lambda (input) + (define lxr (make-lexer input)) + (check-exception "Expected a literal" + (dlang/expr-block lxr 'term))))) + -- 2.52.0