aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/language/src/main/java/tools/refinery/language/parser/antlr/IdentifierTokenProvider.java
blob: 306a86fc0a6104753839e8b8aa86cccca7d72a34 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.language.parser.antlr;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.eclipse.xtext.*;
import org.eclipse.xtext.parser.antlr.ITokenDefProvider;
import tools.refinery.language.services.ProblemGrammarAccess;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

@Singleton
public class IdentifierTokenProvider {
	private final int[] identifierTokensArray;

	@Inject
	private IdentifierTokenProvider(Initializer initializer) {
		this.identifierTokensArray = initializer.getIdentifierTokesArray();
	}

	public boolean isIdentifierToken(int tokenId) {
		for (int identifierTokenId : identifierTokensArray) {
			if (identifierTokenId == tokenId) {
				return true;
			}
		}
		return false;
	}

	private static class Initializer {
		@Inject
		private ITokenDefProvider tokenDefProvider;

		@Inject
		private ProblemGrammarAccess problemGrammarAccess;

		private HashMap<String, Integer> valueToTokenIdMap;

		private Set<Integer> identifierTokens;

		public int[] getIdentifierTokesArray() {
			createValueToTokenIdMap();
			identifierTokens = new HashSet<>();
			collectIdentifierTokensFromRule(problemGrammarAccess.getIdentifierRule());
			var identifierTokensArray = new int[identifierTokens.size()];
			int i = 0;
			for (var tokenId : identifierTokens) {
				identifierTokensArray[i] = tokenId;
				i++;
			}
			return identifierTokensArray;
		}

		private void createValueToTokenIdMap() {
			var tokenIdToValueMap = tokenDefProvider.getTokenDefMap();
			valueToTokenIdMap = new HashMap<>(tokenIdToValueMap.size());
			for (var entry : tokenIdToValueMap.entrySet()) {
				valueToTokenIdMap.put(entry.getValue(), entry.getKey());
			}
		}

		private void collectIdentifierTokensFromRule(AbstractRule rule) {
			if (rule instanceof TerminalRule) {
				collectToken("RULE_" + rule.getName());
				return;
			}
			collectIdentifierTokensFromElement(rule.getAlternatives());
		}

		private void collectIdentifierTokensFromElement(AbstractElement element) {
			if (element instanceof Alternatives alternatives) {
				for (var alternative : alternatives.getElements()) {
					collectIdentifierTokensFromElement(alternative);
				}
			} else if (element instanceof RuleCall ruleCall) {
				collectIdentifierTokensFromRule(ruleCall.getRule());
			} else if (element instanceof Keyword keyword) {
				collectToken("'" + keyword.getValue() + "'");
			} else {
				throw new IllegalArgumentException("Unknown Xtext grammar element: " + element);
			}
		}

		private void collectToken(String value) {
			identifierTokens.add(valueToTokenIdMap.get(value));
		}
	}
}