mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-24 04:42:51 -04:00
more cleanup of test case parser, see #708
This commit is contained in:
parent
4300a32904
commit
4aab5c5fee
@ -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];
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user