From 3a5ccdfc13436e823976fb30e1a73c6581aeb199 Mon Sep 17 00:00:00 2001 From: Riccardo Azzolini Date: Wed, 30 Jan 2019 19:58:47 +0100 Subject: [PATCH] Keep track of sub-function identifier tokens for error reporting --- .../math/rules/dsl/frontend/Parser.java | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/it/cavallium/warppi/math/rules/dsl/frontend/Parser.java b/core/src/main/java/it/cavallium/warppi/math/rules/dsl/frontend/Parser.java index c7f50e73..8349f453 100644 --- a/core/src/main/java/it/cavallium/warppi/math/rules/dsl/frontend/Parser.java +++ b/core/src/main/java/it/cavallium/warppi/math/rules/dsl/frontend/Parser.java @@ -30,6 +30,10 @@ public class Parser { private final Consumer errorReporter; private int current = 0; + // For error reporting + private String currentRuleName; + private final Map>> subFunctionIdentifiers = new HashMap<>(); + public Parser(final List tokens, final Consumer errorReporter) { this.tokens = tokens; this.errorReporter = errorReporter; @@ -39,6 +43,10 @@ public class Parser { return rules(); } + public List getSubFunctionIdentifiers(final String ruleName, final SubFunctionPattern subFunction) { + return subFunctionIdentifiers.get(ruleName).get(subFunction); + } + // rules = { rule } , EOF ; private List rules() { final List rules = new ArrayList<>(); @@ -59,6 +67,7 @@ public class Parser { private PatternRule rule() throws UnexpectedTokenException { final RuleType type = ruleType(); final String name = matchOrFail(IDENTIFIER).lexeme; + currentRuleName = name; // This field must be set before calling pattern() and replacements() matchOrFail(COLON); final Pattern target = pattern(); matchOrFail(ARROW); @@ -212,7 +221,9 @@ public class Parser { case NUMBER: return new NumberPattern(new BigDecimal(curToken.lexeme)); case IDENTIFIER: - return new SubFunctionPattern(curToken.lexeme); + final SubFunctionPattern subFunction = new SubFunctionPattern(curToken.lexeme); + saveSubFunctionIdentifier(subFunction, curToken); + return subFunction; case LEFT_PAREN: final Pattern grouped = sum(); matchOrFail(RIGHT_PAREN); @@ -221,6 +232,18 @@ public class Parser { throw new UnexpectedTokenException(curToken); } + private void saveSubFunctionIdentifier(final SubFunctionPattern subFunction, final Token curToken) { + final Map> ruleMap = subFunctionIdentifiers.computeIfAbsent( + currentRuleName, + key -> new HashMap<>() + ); + final List subFunctionList = ruleMap.computeIfAbsent( + subFunction, + key -> new ArrayList<>() + ); + subFunctionList.add(curToken); + } + private Pattern matchLeftAssoc( final PatternParser operandParser, final Map> operators