summaryrefslogtreecommitdiff
path: root/chapter05/src
diff options
context:
space:
mode:
authorXmiliaH <45106915+XmiliaH@users.noreply.github.com>2023-11-01 10:25:56 +0100
committerDibyendu Majumdar <dibyendumajumdar@users.noreply.github.com>2023-11-01 18:56:50 +0000
commit43f2a09f106072727eaceb6134be02c03429b425 (patch)
tree9d60a5573c4ac861699a487033a2341960a7fd1a /chapter05/src
parent6181af431c47199a00c48af5350e362e6c423faa (diff)
downloadSimple-43f2a09f106072727eaceb6134be02c03429b425.tar.gz
Simple-43f2a09f106072727eaceb6134be02c03429b425.zip
Report unclosed block in parsing (#28)
Diffstat (limited to 'chapter05/src')
-rw-r--r--chapter05/src/main/java/com/seaofnodes/simple/Parser.java4
-rw-r--r--chapter05/src/test/java/com/seaofnodes/simple/Chapter05Test.java10
2 files changed, 13 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 79b50672..a287f997 100644
--- a/chapter05/src/main/java/com/seaofnodes/simple/Parser.java
+++ b/chapter05/src/main/java/com/seaofnodes/simple/Parser.java
@@ -115,7 +115,9 @@ public class Parser {
// Enter a new scope
_scope.push();
Node n = null;
- while (!match("}") && !_lexer.isEOF()) {
+ while (!match("}")) {
+ if (_lexer.isEOF()) throw errorSyntax("}");
+
Node n0 = parseStatement();
if (n0 != null) n = n0; // Allow null returns from eg showGraph
}
diff --git a/chapter05/src/test/java/com/seaofnodes/simple/Chapter05Test.java b/chapter05/src/test/java/com/seaofnodes/simple/Chapter05Test.java
index ab956e9f..254f4530 100644
--- a/chapter05/src/test/java/com/seaofnodes/simple/Chapter05Test.java
+++ b/chapter05/src/test/java/com/seaofnodes/simple/Chapter05Test.java
@@ -429,4 +429,14 @@ return c;
// this test used to fail in chapter 1
assertEquals("return -100;", new Parser("return -100;").parse().print());
}
+
+ @Test
+ public void testBad6() {
+ try {
+ new Parser("int a=1; int b=2; int c=0; { int b=3; c=a+b;").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Syntax error, expected }: ",e.getMessage());
+ }
+ }
}