diff options
| author | Cliff Click <cliffc@acm.org> | 2023-10-29 10:04:09 -0700 |
|---|---|---|
| committer | Dibyendu Majumdar <dibyendumajumdar@users.noreply.github.com> | 2023-11-01 18:56:50 +0000 |
| commit | 6cb3c96d60f93fe52ced758b2e268b75a6ce3c48 (patch) | |
| tree | 27f4def14d83a3b030ad92aace3d002d24870c54 /chapter05 | |
| parent | 4a425c35b81b144006907361fe55f2b3390e5d7c (diff) | |
| download | Simple-6cb3c96d60f93fe52ced758b2e268b75a6ce3c48.tar.gz Simple-6cb3c96d60f93fe52ced758b2e268b75a6ce3c48.zip | |
Fix missing trailing expression
Diffstat (limited to 'chapter05')
| -rw-r--r-- | chapter05/src/main/java/com/seaofnodes/simple/Parser.java | 2 | ||||
| -rw-r--r-- | chapter05/src/test/java/com/seaofnodes/simple/Chapter05Test.java | 10 |
2 files changed, 11 insertions, 1 deletions
diff --git a/chapter05/src/main/java/com/seaofnodes/simple/Parser.java b/chapter05/src/main/java/com/seaofnodes/simple/Parser.java index a66ee278..63dd7067 100644 --- a/chapter05/src/main/java/com/seaofnodes/simple/Parser.java +++ b/chapter05/src/main/java/com/seaofnodes/simple/Parser.java @@ -302,7 +302,7 @@ public class Parser { if( matchx("true" ) ) return new ConstantNode(TypeInteger.constant(1)).peephole(); if( matchx("false") ) return new ConstantNode(TypeInteger.constant(0)).peephole(); String name = _lexer.matchId(); - if( name == null) return null; + if( name == null) throw errorSyntax("an identifier or expression"); Node n = _scope.lookup(name); if( n!=null ) return n; throw error("Undefined name '" + name + "'"); diff --git a/chapter05/src/test/java/com/seaofnodes/simple/Chapter05Test.java b/chapter05/src/test/java/com/seaofnodes/simple/Chapter05Test.java index 2ad7e59a..bb95dc18 100644 --- a/chapter05/src/test/java/com/seaofnodes/simple/Chapter05Test.java +++ b/chapter05/src/test/java/com/seaofnodes/simple/Chapter05Test.java @@ -97,6 +97,16 @@ return c; } @Test + public void testChapter5BadNum() { + try { + new Parser("return 1-;").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Syntax error, expected an identifier or expression: ;",e.getMessage()); + } + } + + @Test public void testChapter5Keyword1() { try { new Parser("int true=0; return true;").parse(); |
