[08 - AST] Add ASTParser, ASTParserTreeTest abstract and Parser abstract
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package de.dhbw.compiler.xparser;
|
||||
|
||||
import de.dhbw.compiler.xparser.api.Parser;
|
||||
|
||||
/**
|
||||
* <h1>ASTParser</h1>
|
||||
* <p>Parses programming language X into abstract syntax trees.</p>
|
||||
*/
|
||||
public class ASTParser extends Parser {
|
||||
public ASTParser(TokenReader in) {
|
||||
super(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tree parseProgram() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user