Package: VdmCodeScanner

VdmCodeScanner

nameinstructionbranchcomplexitylinemethod
VdmCodeScanner(VdmColorProvider)
M: 255 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 33 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.ui
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.ui.editor.syntax;
23:
24: import java.util.ArrayList;
25: import java.util.List;
26:
27: import org.eclipse.jface.text.TextAttribute;
28: import org.eclipse.jface.text.rules.IRule;
29: import org.eclipse.jface.text.rules.IToken;
30: import org.eclipse.jface.text.rules.MultiLineRule;
31: import org.eclipse.jface.text.rules.RuleBasedScanner;
32: import org.eclipse.jface.text.rules.SingleLineRule;
33: import org.eclipse.jface.text.rules.Token;
34: import org.eclipse.jface.text.rules.WhitespaceRule;
35: import org.eclipse.jface.text.rules.WordRule;
36: import org.eclipse.swt.SWT;
37:
38: public abstract class VdmCodeScanner extends RuleBasedScanner
39: {
40:
41:         private IVdmKeywords fgKeywords = getKeywords();
42:
43:         public static final String[] latexOperators = { "\\begin{vdm_al}",
44:                         "\\end{vdm_al}" };
45:
46:         public VdmCodeScanner(VdmColorProvider provider)
47:         {
48:
49:                 IToken keyword = new Token(new TextAttribute(provider.getColor(VdmColorProvider.KEYWORD), null, SWT.BOLD));
50:                 IToken type = new Token(new TextAttribute(provider.getColor(VdmColorProvider.TYPE), null, SWT.BOLD));
51:
52:                 final IToken stringBold = new Token(new TextAttribute(provider.getColor(VdmColorProvider.DEFAULT), null, SWT.BOLD
53:                                 | SWT.ITALIC));
54:                 IToken comment = new Token(new TextAttribute(provider.getColor(VdmColorProvider.SINGLE_LINE_COMMENT)));
55:                 IToken latex = new Token(new TextAttribute(provider.getColor(VdmColorProvider.LATEX)));
56:                 final IToken other = new Token(new TextAttribute(provider.getColor(VdmColorProvider.DEFAULT)));
57:
58:                 List<IRule> rules = new ArrayList<IRule>();
59:
60:                 // Add generic whitespace rule.
61:                 rules.add(new WhitespaceRule(new VdmWhitespaceDetector()));
62:
63:                 // TODO: this is a hack to get latex related stuff commented
64:                 //rules.add(new SingleLineRule("\\begin{vdm_al", "}", comment));
65:                 //rules.add(new SingleLineRule("\\end{vdm_al", "}", comment));
66:                 rules.add(new MultiLineRule("\\end{vdm_al}","\\begin{vdm_al}", latex,(char) 0,true));
67:                 rules.add(new MultiLineRule("\\section{","\\begin{vdm_al}", latex,(char) 0,false));
68: //                rules.add(new SingleLineRule("\\end{vdm_al", "}", comment,));
69:
70:•                if (fgKeywords.supportsQuoteTypes())
71:                 {
72:                         rules.add(new QuoteRule(type));
73:                 }
74:
75:•                if (fgKeywords.supportsTypleSelect())
76:                 {
77:                         rules.add(new TupleSelectRule(stringBold));
78:                 }
79:
80:•                for (String prefix : fgKeywords.getUnderscorePrefixKeywords())
81:                 {
82:                         rules.add(new PrefixedUnderscoreRule(prefix, keyword));
83:                 }
84:
85:•                for (String prefix : fgKeywords.getUnderscorePrefixReservedWords())
86:                 {
87:                         rules.add(new PrefixedUnderscoreRule(prefix, stringBold));
88:                 }
89:
90:                 MultipleWordsWordRule multipleWordRule = new MultipleWordsWordRule(new VdmWordDetector(), Token.UNDEFINED, false);
91:•                for (int i = 0; i < fgKeywords.getMultipleKeywords().length; i++)
92:                 {
93:                         multipleWordRule.addWord(fgKeywords.getMultipleKeywords()[i], keyword);
94:                 }
95:                 rules.add(multipleWordRule);
96:
97:                 // Add word rule for keywords.
98:                 WordRule wordRule = new WordRule(new VdmWordDetector(), other);// Not sure why Token.UNDEFINED doesn't
99:                                                                                                                                                                         // work but
100:                 // it makes S'end' colored.
101:
102:•                for (int i = 0; i < fgKeywords.getAllSingleWordKeywords().length; i++)
103:                 {
104:                         wordRule.addWord(fgKeywords.getAllSingleWordKeywords()[i], keyword);
105:                 }
106:                 rules.add(wordRule);
107:
108:                 IRule[] result = new IRule[rules.size()];
109:                 rules.toArray(result);
110:                 setRules(result);
111:                 // sets the default style. If styledText.getStyleRangeAtOffset is called on the editor this default style is
112:                 // returned instead of null
113:                 setDefaultReturnToken(other);
114:         }
115:
116: //        /**
117: //         * overrides super method to debug rule scanners
118: //         */
119: //        public IToken nextToken()
120: //        {
121: //
122: //                fTokenOffset = fOffset;
123: //                fColumn = UNDEFINED;
124: //
125: //                if (fRules != null)
126: //                {
127: //                        for (int i = 0; i < fRules.length; i++)
128: //                        {
129: //                                int o = fOffset;
130: //
131: //                                IToken token = (fRules[i].evaluate(this));
132: //                                if (o != fOffset)
133: //                                {
134: //                                        try
135: //                                        {
136: //
137: //                                                String text = fDocument.get(o, fOffset - o);
138: //                                                System.out.println("Offset changed from: " +o+" to: "+ fOffset
139: //                                                                + ", scanned '" + text + "' with rule "
140: //                                                                + fRules[i].getClass().getSimpleName()
141: //                                                                + " is token undefined: "+( token.isUndefined()?"yes":"no"));
142: //                                        } catch (BadLocationException e)
143: //                                        {
144: //                                                e.printStackTrace();
145: //                                        }
146: //                                }
147: //
148: //                                if (!token.isUndefined())
149: //                                        return token;
150: //                        }
151: //                }
152: //
153: //                if (read() == EOF)
154: //                        return Token.EOF;
155: //                return fDefaultReturnToken;
156: //        }
157:
158:         protected abstract IVdmKeywords getKeywords();
159: }