summaryrefslogtreecommitdiff
path: root/chapter32/src/test/java/com/seaofnodes/simple/ParserTest.java
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-11-15 16:11:39 -0800
committerCliff Click <cliffc@acm.org>2026-11-15 16:11:39 -0800
commit9fe7c6f9697b5bbf3041261fdc60040e75cc674f (patch)
treed92a8ff20ed984938d90ad8917b4d67fba9fb745 /chapter32/src/test/java/com/seaofnodes/simple/ParserTest.java
parentf0a6c6757a795b8e186de0076836a87acef59fa7 (diff)
downloadSimple-ch32.tar.gz
Simple-ch32.zip
Split off chapter 31ch32
Ch 32 will be focused on compiler performance issues.
Diffstat (limited to 'chapter32/src/test/java/com/seaofnodes/simple/ParserTest.java')
-rw-r--r--chapter32/src/test/java/com/seaofnodes/simple/ParserTest.java33
1 files changed, 33 insertions, 0 deletions
diff --git a/chapter32/src/test/java/com/seaofnodes/simple/ParserTest.java b/chapter32/src/test/java/com/seaofnodes/simple/ParserTest.java
new file mode 100644
index 00000000..364979f2
--- /dev/null
+++ b/chapter32/src/test/java/com/seaofnodes/simple/ParserTest.java
@@ -0,0 +1,33 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.codegen.CodeGen;
+import com.seaofnodes.simple.parser.Parser;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class ParserTest {
+ @Test public void testDeferredFragments() {
+ // The for update is scanned before the body, then parsed after it.
+ // Delimiters in strings, characters and comments must not end the scan.
+ String[] updates = {"n=n+\") (\"#", "n=n+')'", "n=n+/* ) */2", "n=n+{ -> 2; }()"};
+ String[] results = {"6", "82", "4", "4"};
+ for( int i = 0; i<updates.length; i++ ) {
+ CodeGen code = new CodeGen("int n=0; for(int i=0; i<2; "+updates[i]+") i++; return n;")
+ .driver(CodeGen.Phase.TypeCheck);
+ assertEquals(updates[i],results[i],Eval2.eval(code,0));
+ }
+ // Declared constructor bodies use the same scanner with brace nesting.
+ CodeGen code = new CodeGen("struct Box { int x; new Box={ -> x=\"}\"#; }; }; return new Box().x;")
+ .driver(CodeGen.Phase.TypeCheck);
+ assertEquals("1",Eval2.eval(code,0));
+ }
+
+ @Test public void testUnclosedDeferredFragment() {
+ try {
+ new CodeGen("for(int i=0; i<2; i++").driver(CodeGen.Phase.TypeCheck);
+ fail("Accepted an unterminated for update");
+ } catch( Parser.ParseException e ) {
+ assertEquals("Unclosed for update",e.getMessage());
+ }
+ }
+}