Skip to content
Snippets Groups Projects
Commit 6a407f80 authored by Johannes Mey's avatar Johannes Mey
Browse files

fix genericity problem

parent 8b3024a1
No related branches found
No related tags found
No related merge requests found
Pipeline #13251 passed
...@@ -36,7 +36,7 @@ public class Main { ...@@ -36,7 +36,7 @@ public class Main {
e.printStackTrace(); e.printStackTrace();
} }
try { try {
parser.parseT(); parser.parse();
} catch (WorldParser.ParseException e) { } catch (WorldParser.ParseException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -20,27 +20,9 @@ public final class RobotParser extends MotionGrammarParser<Tidy> { ...@@ -20,27 +20,9 @@ public final class RobotParser extends MotionGrammarParser<Tidy> {
return (World) context_.get(WORLD); return (World) context_.get(WORLD);
} }
/**
* Parse a motion grammar with root T
*
* @return the parsed AST of type T
*/
public Tidy parseTidy() throws ParseException {
// don't try this at home
rootContainer_ = new ASTNode<>();
if (rootContainer_.getNumChild() == 0) {
rootContainer_.addChild(null);
}
printAST("initial", null);
parseTidy(rootContainer_, 0);
Tidy result = rootContainer_.getChild(0);
printAST("complete", null);
result.setParent(null);
rootContainer_.setChild(null, 0);
return result;
}
private void parseTidy(ASTNode<?> parent, int index) throws ParseException { @Override
protected void parse(ASTNode<?> parent, int index) throws ParseException {
Tidy result; Tidy result;
if (true) { if (true) {
...@@ -49,6 +31,6 @@ public final class RobotParser extends MotionGrammarParser<Tidy> { ...@@ -49,6 +31,6 @@ public final class RobotParser extends MotionGrammarParser<Tidy> {
// semantic action for T // semantic action for T
result.action(getWorld()); result.action(getWorld());
printAST("parseT", result); printAST("parseTidy", result);
} }
} }
...@@ -21,27 +21,8 @@ public final class WorldParser extends MotionGrammarParser<T> { ...@@ -21,27 +21,8 @@ public final class WorldParser extends MotionGrammarParser<T> {
return (World) context_.get(WORLD); return (World) context_.get(WORLD);
} }
/** @Override
* Parse a motion grammar with root T protected void parse(ASTNode<?> parent, int index) throws ParseException {
*
* @return the parsed AST of type T
*/
public T parseT() throws ParseException {
// don't try this at home
rootContainer_ = new ASTNode<>();
if (rootContainer_.getNumChild() == 0) {
rootContainer_.addChild(null);
}
printAST("initial", null);
parseT(rootContainer_, 0);
T result = rootContainer_.getChild(0);
printAST("complete", null);
result.setParent(null);
rootContainer_.setChild(null, 0);
return result;
}
private void parseT(ASTNode<?> parent, int index) throws ParseException {
T result; T result;
// try to parse a T // try to parse a T
...@@ -66,7 +47,7 @@ public final class WorldParser extends MotionGrammarParser<T> { ...@@ -66,7 +47,7 @@ public final class WorldParser extends MotionGrammarParser<T> {
T1 result = (T1) parent.getChild(index); T1 result = (T1) parent.getChild(index);
parseLoad(result, 0); parseLoad(result, 0);
parseT(result, 1); parse(result, 1);
parseUnload(result, 2); parseUnload(result, 2);
// semantic action for T1 // semantic action for T1
......
...@@ -70,4 +70,26 @@ public abstract class MotionGrammarParser<T extends MotionGrammarElement> { ...@@ -70,4 +70,26 @@ public abstract class MotionGrammarParser<T extends MotionGrammarElement> {
super("Unable to parse token " + expectedToken.name() + "."); super("Unable to parse token " + expectedToken.name() + ".");
} }
} }
/**
* Parse a motion grammar with root T
*
* @return the parsed AST of type T
*/
public T parse() throws ParseException {
// don't try this at home
rootContainer_ = new ASTNode<>();
if (rootContainer_.getNumChild() == 0) {
rootContainer_.addChild(null);
}
printAST("initial", null);
parse(rootContainer_, 0);
T result = rootContainer_.getChild(0);
printAST("complete", null);
result.setParent(null);
rootContainer_.setChild(null, 0);
return result;
}
protected abstract void parse(ASTNode<?> parent, int index) throws ParseException;
} }
...@@ -41,7 +41,7 @@ public class ParserTest { ...@@ -41,7 +41,7 @@ public class ParserTest {
WorldParser parser = new WorldParser(world); WorldParser parser = new WorldParser(world);
parser.setDebugDiagramDir(AST_DIAGRAM_DIR); parser.setDebugDiagramDir(AST_DIAGRAM_DIR);
var result = parser.parseT(); var result = parser.parse();
assertThat(result).isNotNull().isInstanceOf(T1.class); assertThat(result).isNotNull().isInstanceOf(T1.class);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment