From b7851e7305ed79ace6d0552e72b01677566107be Mon Sep 17 00:00:00 2001 From: Jesper Gravgaard Date: Wed, 1 Apr 2020 14:51:16 +0200 Subject: [PATCH] Proof of Concept macro expansion working. #169 --- .../camelot64/kickc/parsing/macros/Macros.g4 | 2 +- .../parsing/macros/TestMacrosParser.java | 22 ++++++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/test/java/dk/camelot64/kickc/parsing/macros/Macros.g4 b/src/test/java/dk/camelot64/kickc/parsing/macros/Macros.g4 index 8ac8333b6..5760e678a 100644 --- a/src/test/java/dk/camelot64/kickc/parsing/macros/Macros.g4 +++ b/src/test/java/dk/camelot64/kickc/parsing/macros/Macros.g4 @@ -39,4 +39,4 @@ IDENTIFIER: [a-zA-Z_]+ ; NUMBER: [0-9]+ ; DEFINE: '#define' ; DEFINE_CONTINUE: '\\\n' ; -WHITESPACE: [ \t\r\n]+ -> channel(1) ; // skip; // +WHITESPACE: [ \t\r\n]+ -> channel(1) ; // Send whitespace to the hidden WS channel diff --git a/src/test/java/dk/camelot64/kickc/parsing/macros/TestMacrosParser.java b/src/test/java/dk/camelot64/kickc/parsing/macros/TestMacrosParser.java index fc526fa31..888133749 100644 --- a/src/test/java/dk/camelot64/kickc/parsing/macros/TestMacrosParser.java +++ b/src/test/java/dk/camelot64/kickc/parsing/macros/TestMacrosParser.java @@ -28,6 +28,10 @@ public class TestMacrosParser { assertEquals("+(name:a,*(name:b,name:c));", parse("a+b*c;")); // Addition and a cast assertEquals("+(cast(char,name:b),num:1);", parse("(char)b+1;")); + // Two statements + assertEquals("+(name:a,name:c);*(name:d,num:2);", parse("a+c;d*2;")); + // Some whitespace + assertEquals("+(name:a,name:c);", parse(" \ta + \nc ;")); } /** @@ -40,7 +44,9 @@ public class TestMacrosParser { // A simple used define assertEquals("+(*(name:axe,num:2),name:axe);", parse("#define A axe\nA*2+A;")); // A define using a special token class - assertEquals("+(name:axe,num:1);", parse("#define A axe\n#define B +\nA B 1;")); + assertEquals("+(name:a,num:1);", parse("#define A a\n#define B +\n\n#define C 1\nA B C;")); + // A define with two tokens + assertEquals("+(name:a,num:1);", parse("#define A a+\nA 1;")); } /** @@ -66,14 +72,15 @@ public class TestMacrosParser { } }); - /* Map> macros = new LinkedHashMap<>(); final ArrayList finalTokens = new ArrayList<>(); Token token = lexer.nextToken(); while(token.getType() != Token.EOF) { if(token.getType() == MacrosLexer.DEFINE) { - // Define a new macro - final Token name = lexer.nextToken(); + // Define a new macro - find name + Token name = lexer.nextToken(); + while(name.getType()==MacrosLexer.WHITESPACE) + name = lexer.nextToken(); // Find value by gobbling tokens until the line ends final ArrayList macroValue = new ArrayList<>(); boolean macroRead = true; @@ -102,11 +109,10 @@ public class TestMacrosParser { } token = lexer.nextToken(); } - MacrosParser parser = new MacrosParser(new BufferedTokenStream(new ListTokenSource(finalTokens))); - */ + MacrosParser parser = new MacrosParser(new CommonTokenStream(new ListTokenSource(finalTokens))); - CommonTokenStream tokenStream = new CommonTokenStream(lexer); - MacrosParser parser = new MacrosParser(tokenStream); + //CommonTokenStream tokenStream = new CommonTokenStream(lexer); + //MacrosParser parser = new MacrosParser(tokenStream); parser.setBuildParseTree(true); parser.addErrorListener(new BaseErrorListener() {