source: default/trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/syntaxcoloring/EtsiBnfSemanticHighlightingCalculator.java @ 5

Last change on this file since 5 was 5, checked in by zeiss, 14 years ago
  • Property svn:mime-type set to text/plain
File size: 1.9 KB
Line 
1package de.ugoe.cs.swe.bnftools.ui.syntaxcoloring;
2
3import org.eclipse.xtext.CrossReference;
4import org.eclipse.xtext.parsetree.AbstractNode;
5import org.eclipse.xtext.parsetree.LeafNode;
6import org.eclipse.xtext.parsetree.NodeUtil;
7import org.eclipse.xtext.resource.XtextResource;
8import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightedPositionAcceptor;
9import org.eclipse.xtext.ui.editor.syntaxcoloring.ISemanticHighlightingCalculator;
10
11import de.ugoe.cs.swe.bnftools.analysis.EbnfAnalysisUtils;
12import de.ugoe.cs.swe.bnftools.ebnf.Rule;
13
14public 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}
Note: See TracBrowser for help on using the repository browser.