more cleanup of test case parser, see #708

This commit is contained in:
hneemann 2021-04-12 16:25:04 +02:00
parent 4300a32904
commit 4aab5c5fee
2 changed files with 31 additions and 33 deletions

View File

@ -12,5 +12,23 @@ public enum OperatorPrecedence {
/**
* the operator precedences
*/
MUL, ADD, SHIFT, COMPARE, EQUAL, AND, XOR, OR
MUL, ADD, SHIFT, COMPARE, EQUAL, AND, XOR, OR;
/**
* @return the OperatorPrecedence to start with
*/
public static OperatorPrecedence first() {
return OR;
}
/**
* @return the predecessor or null if there is none
*/
public OperatorPrecedence getPredecessor() {
if (ordinal() == 0)
return null;
else
return values()[ordinal() - 1];
}
}

View File

@ -343,6 +343,18 @@ public class Parser {
return value;
}
private Expression parseExpression() throws IOException, ParserException {
return parseExpression(OperatorPrecedence.first());
}
private Expression parseExpression(OperatorPrecedence op) throws IOException, ParserException {
OperatorPrecedence pr = op.getPredecessor();
if (pr == null)
return parseExpression(op, this::parseIdent);
else
return parseExpression(op, () -> parseExpression(pr));
}
private Expression parseExpression(OperatorPrecedence oppr, Next next) throws IOException, ParserException {
Expression ac = next.next();
while (tok.peek().getPrecedence() == oppr) {
@ -354,38 +366,6 @@ public class Parser {
return ac;
}
private Expression parseExpression() throws IOException, ParserException {
return parseExpression(OperatorPrecedence.OR, this::parseXOR);
}
private Expression parseXOR() throws IOException, ParserException {
return parseExpression(OperatorPrecedence.XOR, this::parseAND);
}
private Expression parseAND() throws IOException, ParserException {
return parseExpression(OperatorPrecedence.AND, this::parseEquals);
}
private Expression parseEquals() throws IOException, ParserException {
return parseExpression(OperatorPrecedence.EQUAL, this::parseCompare);
}
private Expression parseCompare() throws IOException, ParserException {
return parseExpression(OperatorPrecedence.COMPARE, this::parseShift);
}
private Expression parseShift() throws IOException, ParserException {
return parseExpression(OperatorPrecedence.SHIFT, this::parseAdd);
}
private Expression parseAdd() throws IOException, ParserException {
return parseExpression(OperatorPrecedence.ADD, this::parseMul);
}
private Expression parseMul() throws IOException, ParserException {
return parseExpression(OperatorPrecedence.MUL, this::parseIdent);
}
private Expression parseIdent() throws IOException, ParserException {
Tokenizer.Token t = tok.next();
switch (t) {