From 4aab5c5feeb87d52cc4bc2aa9753c6140cb725ae Mon Sep 17 00:00:00 2001 From: hneemann Date: Mon, 12 Apr 2021 16:25:04 +0200 Subject: [PATCH] more cleanup of test case parser, see #708 --- .../testing/parser/OperatorPrecedence.java | 20 ++++++++- .../digital/testing/parser/Parser.java | 44 +++++-------------- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/src/main/java/de/neemann/digital/testing/parser/OperatorPrecedence.java b/src/main/java/de/neemann/digital/testing/parser/OperatorPrecedence.java index faed779b7..d6ac9039f 100644 --- a/src/main/java/de/neemann/digital/testing/parser/OperatorPrecedence.java +++ b/src/main/java/de/neemann/digital/testing/parser/OperatorPrecedence.java @@ -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]; + } + } diff --git a/src/main/java/de/neemann/digital/testing/parser/Parser.java b/src/main/java/de/neemann/digital/testing/parser/Parser.java index 1431ffeb1..0511206f8 100644 --- a/src/main/java/de/neemann/digital/testing/parser/Parser.java +++ b/src/main/java/de/neemann/digital/testing/parser/Parser.java @@ -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) {