From ccd83187b172f8ba3cd7c49b59a080fb027a184b Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Sun, 29 Oct 2023 09:32:35 -0700 Subject: Fix bugs using keywords as ids Add an exact 'matchx' for keywords. Add a list of keywords. int true=0; return true; // Expected id, found 'true' int else=arg; if(else) else=2; else else=1; return else; // Expected id, found 'else' inta=1; return a; // undefined inta --- .../java/com/seaofnodes/simple/Chapter05Test.java | 31 +++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'chapter05/src/test/java') diff --git a/chapter05/src/test/java/com/seaofnodes/simple/Chapter05Test.java b/chapter05/src/test/java/com/seaofnodes/simple/Chapter05Test.java index 8f09620e..32ac5aac 100644 --- a/chapter05/src/test/java/com/seaofnodes/simple/Chapter05Test.java +++ b/chapter05/src/test/java/com/seaofnodes/simple/Chapter05Test.java @@ -95,6 +95,26 @@ return c; StopNode stop = new Parser("return true;").parse(); assertEquals("return 1;",stop.toString()); } + + @Test + public void testChapter5Keyword1() { + try { + new Parser("int true=0; return true;").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Expected an identifier, found 'true'",e.getMessage()); + } + } + + @Test + public void testChapter5Keyword2() { + try { + new Parser("int else=arg; if(else) else=2; else else=1; return else;").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Expected an identifier, found 'else'",e.getMessage()); + } + } @Test public void testChapter4Peephole() { @@ -196,6 +216,16 @@ return c; assertEquals("return (arg+1);", ret.print()); } + @Test + public void testChapter4Bug3() { + try { + new Parser("inta=1; return a;").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Undefined name 'inta'",e.getMessage()); + } + } + @Test public void testVarDecl() { Parser parser = new Parser("int a=1; return a;"); @@ -242,7 +272,6 @@ return c; } } - @Test public void testChapter2ParseGrammar() { Node._disablePeephole = true; // disable peephole so we can observe full graph -- cgit v1.2.3