Keep track of sub-function identifier tokens for error reporting
This commit is contained in:
parent
bdb1fc738e
commit
3a5ccdfc13
@ -30,6 +30,10 @@ public class Parser {
|
|||||||
private final Consumer<? super DslException> errorReporter;
|
private final Consumer<? super DslException> errorReporter;
|
||||||
private int current = 0;
|
private int current = 0;
|
||||||
|
|
||||||
|
// For error reporting
|
||||||
|
private String currentRuleName;
|
||||||
|
private final Map<String, Map<SubFunctionPattern, List<Token>>> subFunctionIdentifiers = new HashMap<>();
|
||||||
|
|
||||||
public Parser(final List<Token> tokens, final Consumer<? super DslException> errorReporter) {
|
public Parser(final List<Token> tokens, final Consumer<? super DslException> errorReporter) {
|
||||||
this.tokens = tokens;
|
this.tokens = tokens;
|
||||||
this.errorReporter = errorReporter;
|
this.errorReporter = errorReporter;
|
||||||
@ -39,6 +43,10 @@ public class Parser {
|
|||||||
return rules();
|
return rules();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Token> getSubFunctionIdentifiers(final String ruleName, final SubFunctionPattern subFunction) {
|
||||||
|
return subFunctionIdentifiers.get(ruleName).get(subFunction);
|
||||||
|
}
|
||||||
|
|
||||||
// rules = { rule } , EOF ;
|
// rules = { rule } , EOF ;
|
||||||
private List<PatternRule> rules() {
|
private List<PatternRule> rules() {
|
||||||
final List<PatternRule> rules = new ArrayList<>();
|
final List<PatternRule> rules = new ArrayList<>();
|
||||||
@ -59,6 +67,7 @@ public class Parser {
|
|||||||
private PatternRule rule() throws UnexpectedTokenException {
|
private PatternRule rule() throws UnexpectedTokenException {
|
||||||
final RuleType type = ruleType();
|
final RuleType type = ruleType();
|
||||||
final String name = matchOrFail(IDENTIFIER).lexeme;
|
final String name = matchOrFail(IDENTIFIER).lexeme;
|
||||||
|
currentRuleName = name; // This field must be set before calling pattern() and replacements()
|
||||||
matchOrFail(COLON);
|
matchOrFail(COLON);
|
||||||
final Pattern target = pattern();
|
final Pattern target = pattern();
|
||||||
matchOrFail(ARROW);
|
matchOrFail(ARROW);
|
||||||
@ -212,7 +221,9 @@ public class Parser {
|
|||||||
case NUMBER:
|
case NUMBER:
|
||||||
return new NumberPattern(new BigDecimal(curToken.lexeme));
|
return new NumberPattern(new BigDecimal(curToken.lexeme));
|
||||||
case IDENTIFIER:
|
case IDENTIFIER:
|
||||||
return new SubFunctionPattern(curToken.lexeme);
|
final SubFunctionPattern subFunction = new SubFunctionPattern(curToken.lexeme);
|
||||||
|
saveSubFunctionIdentifier(subFunction, curToken);
|
||||||
|
return subFunction;
|
||||||
case LEFT_PAREN:
|
case LEFT_PAREN:
|
||||||
final Pattern grouped = sum();
|
final Pattern grouped = sum();
|
||||||
matchOrFail(RIGHT_PAREN);
|
matchOrFail(RIGHT_PAREN);
|
||||||
@ -221,6 +232,18 @@ public class Parser {
|
|||||||
throw new UnexpectedTokenException(curToken);
|
throw new UnexpectedTokenException(curToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void saveSubFunctionIdentifier(final SubFunctionPattern subFunction, final Token curToken) {
|
||||||
|
final Map<SubFunctionPattern, List<Token>> ruleMap = subFunctionIdentifiers.computeIfAbsent(
|
||||||
|
currentRuleName,
|
||||||
|
key -> new HashMap<>()
|
||||||
|
);
|
||||||
|
final List<Token> subFunctionList = ruleMap.computeIfAbsent(
|
||||||
|
subFunction,
|
||||||
|
key -> new ArrayList<>()
|
||||||
|
);
|
||||||
|
subFunctionList.add(curToken);
|
||||||
|
}
|
||||||
|
|
||||||
private Pattern matchLeftAssoc(
|
private Pattern matchLeftAssoc(
|
||||||
final PatternParser operandParser,
|
final PatternParser operandParser,
|
||||||
final Map<TokenType, BiFunction<Pattern, Pattern, Pattern>> operators
|
final Map<TokenType, BiFunction<Pattern, Pattern, Pattern>> operators
|
||||||
|
Loading…
x
Reference in New Issue
Block a user