]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
84c0055fc5699b8f726404792a5a25eba586b723
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.modelica;\r
13 \r
14 import java.util.HashSet;\r
15 import java.util.Set;\r
16 \r
17 import org.eclipse.jface.text.IDocument;\r
18 import org.eclipse.jface.text.TextAttribute;\r
19 import org.eclipse.jface.text.presentation.IPresentationReconciler;\r
20 import org.eclipse.jface.text.presentation.PresentationReconciler;\r
21 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;\r
22 import org.eclipse.jface.text.rules.ICharacterScanner;\r
23 import org.eclipse.jface.text.rules.IRule;\r
24 import org.eclipse.jface.text.rules.IToken;\r
25 import org.eclipse.jface.text.rules.RuleBasedScanner;\r
26 import org.eclipse.jface.text.rules.Token;\r
27 import org.eclipse.jface.text.source.ISourceViewer;\r
28 import org.eclipse.jface.text.source.SourceViewerConfiguration;\r
29 import org.eclipse.swt.SWT;\r
30 import org.eclipse.swt.graphics.Color;\r
31 \r
32 public class ModelicaSourceViewerConfiguration extends SourceViewerConfiguration {\r
33 \r
34         @Override\r
35     public IPresentationReconciler getPresentationReconciler(\r
36                         ISourceViewer sourceViewer) {\r
37                 PresentationReconciler pr = new PresentationReconciler();\r
38                 DefaultDamagerRepairer ddr = new DefaultDamagerRepairer(\r
39                                 new RuleBasedScanner() {                                                                                                                \r
40                                         {\r
41                                                 setRules(new IRule[] {new IRule() {\r
42 \r
43                                                         @Override\r
44                                                         public IToken evaluate(ICharacterScanner scanner) {\r
45                                                                 int ch;\r
46                                                                 try {\r
47                                                                         scanner.unread();\r
48                                                                         ch = scanner.read();\r
49                                                                 } catch (Throwable t) {\r
50                                                                         ch = -1;\r
51                                                                 }\r
52                                                                 if (ch <= 0 || !isIdentifierChar((char) ch)) {\r
53                                                                         ch = scanner.read();\r
54                                                                         if(isIdentifierChar((char)ch)) {\r
55                                                                                 StringBuilder b = new StringBuilder();\r
56                                                                                 do {\r
57                                                                                         b.append((char)ch);\r
58                                                                                         ch = scanner.read();\r
59                                                                                 } while(isIdentifierChar((char) ch));\r
60                                                                                 String str = b.toString();\r
61                                                                                 if(keywords.contains(str))\r
62                                                                                         return getModelicaKeywordToken();\r
63                                                                         }\r
64                                                                         scanner.unread();\r
65                                                                 }\r
66                                                                 return Token.UNDEFINED;\r
67                                                         }                                                                       \r
68                                                         \r
69                                                 }});\r
70                                         }                                                       \r
71                                 }\r
72                         );\r
73                 pr.setRepairer(ddr, IDocument.DEFAULT_CONTENT_TYPE);\r
74                 pr.setDamager(ddr, IDocument.DEFAULT_CONTENT_TYPE);\r
75                 return pr;\r
76         }\r
77         \r
78         static boolean isIdentifierChar(char c) {\r
79                 return \r
80                         (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') \r
81                  || (c >= '0' && c <= '9') || c == '_';\r
82         }\r
83         \r
84         // Includes also tokens that are strictly speaking not keywords\r
85         // but should be handled in (almost) a similar fashion.\r
86         public static final Set<String> keywords = new HashSet<String>();\r
87 \r
88     static {\r
89         keywords.add("algorithm");\r
90         keywords.add("discrete");\r
91         keywords.add("false");\r
92         keywords.add("model");\r
93         keywords.add("redeclare");\r
94         keywords.add("and");\r
95         keywords.add("each");\r
96         keywords.add("final");\r
97         keywords.add("not");\r
98         keywords.add("replaceable");\r
99         keywords.add("annotation");\r
100         keywords.add("else");\r
101         keywords.add("flow");\r
102         keywords.add("operator");\r
103         keywords.add("return");\r
104         keywords.add("assert");\r
105         keywords.add("elseif");\r
106         keywords.add("for");\r
107         keywords.add("or");\r
108         keywords.add("stream");\r
109         keywords.add("block");\r
110         keywords.add("elsewhen");\r
111         keywords.add("function");\r
112         keywords.add("outer");\r
113         keywords.add("then");\r
114         keywords.add("break");\r
115         keywords.add("encapsulated");\r
116         keywords.add("if");\r
117         keywords.add("output");\r
118         keywords.add("true");\r
119         keywords.add("class");\r
120         keywords.add("end");\r
121         keywords.add("import");\r
122         keywords.add("package");\r
123         keywords.add("type");\r
124         keywords.add("connect");\r
125         keywords.add("enumeration");\r
126         keywords.add("in");\r
127         keywords.add("parameter");\r
128         keywords.add("when");\r
129         keywords.add("connector");\r
130         keywords.add("equation");\r
131         keywords.add("initial");\r
132         keywords.add("partial");\r
133         keywords.add("while");\r
134         keywords.add("constant");\r
135         keywords.add("expandable");\r
136         keywords.add("inner");\r
137         keywords.add("protected");\r
138         keywords.add("within");\r
139         keywords.add("constrainedby");\r
140         keywords.add("extends");\r
141         keywords.add("input");\r
142         keywords.add("public");\r
143         keywords.add("der");\r
144         keywords.add("external");\r
145         keywords.add("loop");\r
146         keywords.add("record");\r
147         keywords.add("time");\r
148         keywords.add("pure");\r
149         keywords.add("impure");\r
150         keywords.add("Real");\r
151         keywords.add("Boolean");\r
152         keywords.add("Integer");\r
153         keywords.add("String");\r
154     }\r
155 \r
156         static IToken modelicaKeywordToken = null;\r
157         public IToken getModelicaKeywordToken() {\r
158                 if(modelicaKeywordToken == null) {\r
159                         modelicaKeywordToken = \r
160                                 new Token(\r
161                                                 new TextAttribute(\r
162                                                                 new Color(null, 127, 0, 85),\r
163                                                                 new Color(null, 255, 255, 255),\r
164                                                                 SWT.BOLD\r
165                                                                 ));\r
166                 }\r
167                 return modelicaKeywordToken;\r
168         }\r
169         \r
170 }\r