From 8cf57691dc158aa4e458b0454f53fd921e35f4ea Mon Sep 17 00:00:00 2001 From: Humenius Date: Mon, 18 May 2020 11:22:16 +0200 Subject: [PATCH] [08 - AST] Add ASTParser, ASTParserTreeTest abstract and Parser abstract --- .../.idea/uiDesigner.xml | 124 ++++++++++++++++++ .../src/de/dhbw/compiler/xparser/ASTParser.java | 18 +++ .../src/de/dhbw/compiler/xparser/XParser.java | 21 +-- .../de/dhbw/compiler/xparser/api/Parser.java | 28 ++++ .../compiler/xparser/test/ASTParseTreeTest.java | 27 ++++ .../compiler/xparser/test/TestXASTParser1.java | 2 +- .../compiler/xparser/test/TestXASTParser2.java | 2 +- 7 files changed, 203 insertions(+), 19 deletions(-) create mode 100644 ÜB-Praxis-Abstiegsparser für X-Leer/.idea/uiDesigner.xml create mode 100644 ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/ASTParser.java create mode 100644 ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/api/Parser.java create mode 100644 ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/test/ASTParseTreeTest.java diff --git a/ÜB-Praxis-Abstiegsparser für X-Leer/.idea/uiDesigner.xml b/ÜB-Praxis-Abstiegsparser für X-Leer/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/ÜB-Praxis-Abstiegsparser für X-Leer/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/ASTParser.java b/ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/ASTParser.java new file mode 100644 index 0000000..b32e208 --- /dev/null +++ b/ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/ASTParser.java @@ -0,0 +1,18 @@ +package de.dhbw.compiler.xparser; + +import de.dhbw.compiler.xparser.api.Parser; + +/** + *

ASTParser

+ *

Parses programming language X into abstract syntax trees.

+ */ +public class ASTParser extends Parser { + public ASTParser(TokenReader in) { + super(in); + } + + @Override + public Tree parseProgram() { + return null; + } +} diff --git a/ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/XParser.java b/ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/XParser.java index 0bb307c..f67e524 100644 --- a/ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/XParser.java +++ b/ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/XParser.java @@ -11,13 +11,14 @@ package de.dhbw.compiler.xparser; -public class XParser { - private final TokenReader in; +import de.dhbw.compiler.xparser.api.Parser; +public class XParser extends Parser { public XParser(TokenReader in) { - this.in = in; + super(in); } + @Override public Tree parseProgram() { int oldPosition = in.getPosition(); Tree program, id, semicolon, block, dot, eof; @@ -277,18 +278,4 @@ public class XParser { in.setPosition(oldPosition); return null; } - - private Tree parseToken(int tokenType) { - int oldPosition = in.getPosition(); - Token nextToken = in.nextToken(); - System.out.println("parseToken> " + nextToken + " = " + Token.getTypeName(tokenType)); - if (nextToken.getType() == tokenType) { - return new Tree(nextToken); - } else { - // Reset to last position as it needs to be re-read again - in.setPosition(oldPosition); - return null; - } - } - } diff --git a/ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/api/Parser.java b/ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/api/Parser.java new file mode 100644 index 0000000..d3787a2 --- /dev/null +++ b/ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/api/Parser.java @@ -0,0 +1,28 @@ +package de.dhbw.compiler.xparser.api; + +import de.dhbw.compiler.xparser.Token; +import de.dhbw.compiler.xparser.TokenReader; +import de.dhbw.compiler.xparser.Tree; + +public abstract class Parser { + protected final TokenReader in; + + protected Parser(TokenReader in) { + this.in = in; + } + + public abstract Tree parseProgram(); + + protected Tree parseToken(int tokenType) { + int oldPosition = in.getPosition(); + Token nextToken = in.nextToken(); + System.out.println("parseToken> " + nextToken + " = " + Token.getTypeName(tokenType)); + if (nextToken.getType() == tokenType) { + return new Tree(nextToken); + } else { + // Reset to last position as it needs to be re-read again + in.setPosition(oldPosition); + return null; + } + } +} diff --git a/ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/test/ASTParseTreeTest.java b/ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/test/ASTParseTreeTest.java new file mode 100644 index 0000000..d57bbb5 --- /dev/null +++ b/ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/test/ASTParseTreeTest.java @@ -0,0 +1,27 @@ +package de.dhbw.compiler.xparser.test; + +import static org.junit.Assert.assertEquals; + +import java.io.StringReader; + +import de.dhbw.compiler.xparser.ASTParser; +import de.dhbw.compiler.xparser.JFlexXScanner; +import de.dhbw.compiler.xparser.TokenReader; +import de.dhbw.compiler.xparser.Tree; + +public abstract class ASTParseTreeTest extends ParseTreeTest { + @Override + protected void testParseTree(String in, String expected) throws Exception { + JFlexXScanner scanner = new JFlexXScanner(new StringReader(in)); + TokenReader reader = new TokenReader(scanner); + ASTParser parser = new ASTParser(reader); + + Tree out = parser.parseProgram(); + + if (out==null) { + assertEquals(expected, out); + } else { + assertEquals(expected, out.toString()); + } + } +} diff --git a/ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/test/TestXASTParser1.java b/ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/test/TestXASTParser1.java index b5af174..6fc7998 100644 --- a/ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/test/TestXASTParser1.java +++ b/ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/test/TestXASTParser1.java @@ -16,7 +16,7 @@ import org.junit.Test; import org.junit.runners.MethodSorters; @FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class TestXASTParser1 extends ParseTreeTest { +public class TestXASTParser1 extends ASTParseTreeTest { @Test public void program00BeginEnd() throws Exception { diff --git a/ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/test/TestXASTParser2.java b/ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/test/TestXASTParser2.java index 5fe7705..ef02351 100644 --- a/ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/test/TestXASTParser2.java +++ b/ÜB-Praxis-Abstiegsparser für X-Leer/src/de/dhbw/compiler/xparser/test/TestXASTParser2.java @@ -16,7 +16,7 @@ import org.junit.Test; import org.junit.runners.MethodSorters; @FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class TestXASTParser2 extends ParseTreeTest { +public class TestXASTParser2 extends ASTParseTreeTest { @Test public void program30Decl() throws Exception {