broken parser

This commit is contained in:
nora 2021-01-16 21:11:45 +01:00
parent 313a584631
commit d7c9aeb127
4 changed files with 13 additions and 12 deletions

2
.idea/.gitignore generated vendored
View file

@ -1,5 +1,5 @@
# Default ignored files
/shelf/
/workspace.xml
out\
target/
*.class

View file

@ -166,8 +166,11 @@ public class Lexer {
Leorio does say "small killua"
}
""";
String hierarchy = """
killua0 hunts 3 > 3
""";
ArrayList<LexToken> tokens = l.lex(ifs);
ArrayList<LexToken> tokens = l.lex(hierarchy);
Parser p = new Parser();
try {

View file

@ -27,13 +27,13 @@ public enum WordType {
private int postArgAmount;
private int preArgAmount;
WordType(int postArgAmount, int preArgAmount) {
WordType(int preArgAmount, int postArgAmount) {
this.postArgAmount = postArgAmount;
this.preArgAmount = preArgAmount;
}
WordType(int postArgAmount) {
this(postArgAmount, 0);
this(0, postArgAmount);
}
WordType() {

View file

@ -45,7 +45,7 @@ public class Parser {
}
//calculate the arguments of the start node, parsing the whole statement
getArguments(startNode, true);
doStatement(startNode, true);
System.out.println(prevNode);
statements.add(prevNode);
} while (iterator.hasNext());
@ -54,7 +54,7 @@ public class Parser {
return statements;
}
private void getArguments(ParseTreeNode node, boolean isParent) throws ParseException {
private void doStatement(ParseTreeNode node, boolean isParent) throws ParseException {
if (node.getKey() == WordType.MINUS && prevNode.getKey() != WordType.NUMBER) {
node.setKey(WordType.NEGATIVE);
}
@ -77,19 +77,17 @@ public class Parser {
if (isParent) {
prevNode = node;
}
if (expectedArg == 0) {
nextToken();
} else {
nextToken();
if (expectedArg != 0) {
for (int i = 0; i < expectedArg; i++) {
ParseTreeNode child = currentToken.toNode();
getArguments(child, false);
doStatement(child, false);
node.addChild(child);
}
}
if (currentToken != null && currentToken.expectsPreArg()) {
getArguments(currentToken.toNode(), true);
doStatement(currentToken.toNode(), true);
}
}