summaryrefslogtreecommitdiffstats
path: root/rba.tool.editor/src/rba/tool/editor/scoping/internal/RBAModelMemberOperationRegistry.xtend
blob: b64136210c3c1d5fac41034736422154e59c5253 (plain)
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
package rba.tool.editor.scoping.internal

import com.google.inject.Inject
import com.google.inject.Singleton
import java.util.Collections
import java.util.Map
import java.util.Set
import java.util.regex.Matcher
import java.util.regex.Pattern
import org.eclipse.emf.ecore.EObject
import org.eclipse.xtext.GrammarUtil
import org.eclipse.xtext.IGrammarAccess
import org.eclipse.xtext.Keyword
import org.eclipse.xtext.generator.parser.antlr.FirstSetComputer
import org.eclipse.xtext.naming.QualifiedName
import org.eclipse.xtext.resource.IEObjectDescription
import rba.core.ExpressionType
import rba.core.Operator
import rba.core.RuleObject
import rba.tool.editor.resource.RBAModelEObjectDescription
import rba.tool.editor.scoping.IExpressionScope

@Singleton
class RBAModelMemberOperationRegistry {

	private static Map<String, String> EMPTY_USERDATA = Collections.<String, String>emptyMap();
	private static Pattern pattern = Pattern.compile("^ConfigurationParserRule_([A-Za-z]+)OperatorFor([A-Za-z]+)$");
	private static String DESCRIPTION_KEY = "%s_%s";
	private static String QUALIFIED_NAME = "%s()";

	private final Map<String, Set<IEObjectDescription>> operations = newLinkedHashMap();

	@Inject
	public new(IGrammarAccess grammarAccess) {
		registerOperations(grammarAccess);
	}

	def protected void registerOperations(IGrammarAccess grammarAccess) {
		val multipleOperandParserRule = GrammarUtil.findRuleForName(grammarAccess.getGrammar(), "ConfigurationParserRule_OperatorWithMultipleOperand");
		val multipleOperandConfiguration = FirstSetComputer.getFirstSet(multipleOperandParserRule.alternatives).filter(Keyword).toList;
		val allConfigurations = GrammarUtil.allRules(grammarAccess.getGrammar()).filter(r|pattern.matcher(r.name).find);
		for (configuration : allConfigurations) {
			val Matcher matcher = pattern.matcher(configuration.name);
			if (matcher.find) {
				val descriptionKey = String.format(DESCRIPTION_KEY, matcher.group(1), matcher.group(2)).toUpperCase;
				val descriptionEntries = newHashSet();
				val keywords = FirstSetComputer.getFirstSet(configuration.alternatives).filter(Keyword);
				for (keyword : keywords) {
					descriptionEntries.add(
						new RBAModelEObjectDescription(QualifiedName.create(String.format(QUALIFIED_NAME, keyword.value)), null, EMPTY_USERDATA,
							#[if (multipleOperandConfiguration.contains(keyword)) -1 else 0, -50]));
				}
				operations.put(descriptionKey, descriptionEntries);
			}
		}
	}

	def public Set<IEObjectDescription> getOperations(EObject model, IExpressionScope.Anchor anchor) {
		var ExpressionType expressionType = ExpressionType.BOOLEAN;
		if (model instanceof RuleObject) {
			expressionType = (model as RuleObject).expressionType;
		} else if (model instanceof Operator) {
			expressionType = (model as Operator).underlyingType;
		}
		return operations.get(String.format(DESCRIPTION_KEY, expressionType.getName(), anchor.name));
	}

}