1 | package de.ugoe.cs.swe.bnftools.ui.syntaxcoloring;
|
---|
2 |
|
---|
3 | import org.eclipse.xtext.CrossReference;
|
---|
4 | import org.eclipse.xtext.parsetree.AbstractNode;
|
---|
5 | import org.eclipse.xtext.parsetree.LeafNode;
|
---|
6 | import org.eclipse.xtext.parsetree.NodeUtil;
|
---|
7 | import org.eclipse.xtext.resource.XtextResource;
|
---|
8 | import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightedPositionAcceptor;
|
---|
9 | import org.eclipse.xtext.ui.editor.syntaxcoloring.ISemanticHighlightingCalculator;
|
---|
10 |
|
---|
11 | import de.ugoe.cs.swe.bnftools.analysis.EbnfAnalysisUtils;
|
---|
12 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
13 |
|
---|
14 | public class EtsiBnfSemanticHighlightingCalculator implements
|
---|
15 | ISemanticHighlightingCalculator {
|
---|
16 |
|
---|
17 | public void provideHighlightingFor(XtextResource resource,
|
---|
18 | IHighlightedPositionAcceptor acceptor) {
|
---|
19 |
|
---|
20 | if (resource == null)
|
---|
21 | return;
|
---|
22 |
|
---|
23 | Iterable<AbstractNode> allNodes = NodeUtil.getAllContents(resource
|
---|
24 | .getParseResult().getRootNode());
|
---|
25 |
|
---|
26 | for (AbstractNode node : allNodes) {
|
---|
27 | if (node.getGrammarElement() instanceof CrossReference) {
|
---|
28 | acceptor.addPosition(node.getOffset(), node.getLength(),
|
---|
29 | EtsiBnfHighlightingConfiguration.CROSSREF_ID);
|
---|
30 | } else if (node.getElement() instanceof Rule) {
|
---|
31 | Rule rule = (Rule) node.getElement();
|
---|
32 |
|
---|
33 | int dividerPosition = 0;
|
---|
34 | for (int i = 0; i < node.getLeafNodes().size(); i++) {
|
---|
35 | LeafNode currentNode = node.getLeafNodes().get(i);
|
---|
36 | if (currentNode.getText().equals(rule.getName())) {
|
---|
37 | dividerPosition = currentNode.getOffset()
|
---|
38 | + currentNode.getLength();
|
---|
39 | break;
|
---|
40 | }
|
---|
41 | }
|
---|
42 | int length = dividerPosition - node.getOffset();
|
---|
43 | if (length > 0) {
|
---|
44 | if (EbnfAnalysisUtils.isTokenRule(rule)) {
|
---|
45 | acceptor.addPosition(node.getOffset(), length,
|
---|
46 | EtsiBnfHighlightingConfiguration.TOKENRULE_ID);
|
---|
47 | } else {
|
---|
48 | acceptor.addPosition(node.getOffset(), length,
|
---|
49 | EtsiBnfHighlightingConfiguration.RULENAME_ID);
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | }
|
---|
56 |
|
---|
57 | }
|
---|