Debian Codegraph @

codemirror-legacy-modes/mode/modelica.js

raw
1 function words(str) {
2 var obj = {}, words = str.split(" ");
3 for (var i=0; i<words.length; ++i)
4 obj[words[i]] = true;
5 return obj;
6 }
7
8 var keywords = words("algorithm and annotation assert block break class connect connector constant constrainedby der discrete each else elseif elsewhen encapsulated end enumeration equation expandable extends external false final flow for function if import impure in initial inner input loop model not operator or outer output package parameter partial protected public pure record redeclare replaceable return stream then true type when while within")
9 var builtin = words("abs acos actualStream asin atan atan2 cardinality ceil cos cosh delay div edge exp floor getInstanceName homotopy inStream integer log log10 mod pre reinit rem semiLinear sign sin sinh spatialDistribution sqrt tan tanh")
10 var atoms = words("Real Boolean Integer String")
11
12 var completions = [].concat(Object.keys(keywords), Object.keys(builtin), Object.keys(atoms))
13
14 var isSingleOperatorChar = /[;=\(:\),{}.*<>+\-\/^\[\]]/;
15 var isDoubleOperatorChar = /(:=|<=|>=|==|<>|\.\+|\.\-|\.\*|\.\/|\.\^)/;
16 var isDigit = /[0-9]/;
17 var isNonDigit = /[_a-zA-Z]/;
18
19 function tokenLineComment(stream, state) {
20 stream.skipToEnd();
21 state.tokenize = null;
22 return "comment";
23 }
24
25 function tokenBlockComment(stream, state) {
26 var maybeEnd = false, ch;
27 while (ch = stream.next()) {
28 if (maybeEnd && ch == "/") {
29 state.tokenize = null;
30 break;
31 }
32 maybeEnd = (ch == "*");
33 }
34 return "comment";
35 }
36
37 function tokenString(stream, state) {
38 var escaped = false, ch;
39 while ((ch = stream.next()) != null) {
40 if (ch == '"' && !escaped) {
41 state.tokenize = null;
42 state.sol = false;
43 break;
44 }
45 escaped = !escaped && ch == "\\";
46 }
47
48 return "string";
49 }
50
51 function tokenIdent(stream, state) {
52 stream.eatWhile(isDigit);
53 while (stream.eat(isDigit) || stream.eat(isNonDigit)) { }
54
55
56 var cur = stream.current();
57
58 if(state.sol && (cur == "package" || cur == "model" || cur == "when" || cur == "connector")) state.level++;
59 else if(state.sol && cur == "end" && state.level > 0) state.level--;
60
61 state.tokenize = null;
62 state.sol = false;
63
64 if (keywords.propertyIsEnumerable(cur)) return "keyword";
65 else if (builtin.propertyIsEnumerable(cur)) return "builtin";
66 else if (atoms.propertyIsEnumerable(cur)) return "atom";
67 else return "variable";
68 }
69
70 function tokenQIdent(stream, state) {
71 while (stream.eat(/[^']/)) { }
72
73 state.tokenize = null;
74 state.sol = false;
75
76 if(stream.eat("'"))
77 return "variable";
78 else
79 return "error";
80 }
81
82 function tokenUnsignedNumber(stream, state) {
83 stream.eatWhile(isDigit);
84 if (stream.eat('.')) {
85 stream.eatWhile(isDigit);
86 }
87 if (stream.eat('e') || stream.eat('E')) {
88 if (!stream.eat('-'))
89 stream.eat('+');
90 stream.eatWhile(isDigit);
91 }
92
93 state.tokenize = null;
94 state.sol = false;
95 return "number";
96 }
97
98 // Interface
99 export const modelica = {
100 name: "modelica",
101 startState: function() {
102 return {
103 tokenize: null,
104 level: 0,
105 sol: true
106 };
107 },
108
109 token: function(stream, state) {
110 if(state.tokenize != null) {
111 return state.tokenize(stream, state);
112 }
113
114 if(stream.sol()) {
115 state.sol = true;
116 }
117
118 // WHITESPACE
119 if(stream.eatSpace()) {
120 state.tokenize = null;
121 return null;
122 }
123
124 var ch = stream.next();
125
126 // LINECOMMENT
127 if(ch == '/' && stream.eat('/')) {
128 state.tokenize = tokenLineComment;
129 }
130 // BLOCKCOMMENT
131 else if(ch == '/' && stream.eat('*')) {
132 state.tokenize = tokenBlockComment;
133 }
134 // TWO SYMBOL TOKENS
135 else if(isDoubleOperatorChar.test(ch+stream.peek())) {
136 stream.next();
137 state.tokenize = null;
138 return "operator";
139 }
140 // SINGLE SYMBOL TOKENS
141 else if(isSingleOperatorChar.test(ch)) {
142 state.tokenize = null;
143 return "operator";
144 }
145 // IDENT
146 else if(isNonDigit.test(ch)) {
147 state.tokenize = tokenIdent;
148 }
149 // Q-IDENT
150 else if(ch == "'" && stream.peek() && stream.peek() != "'") {
151 state.tokenize = tokenQIdent;
152 }
153 // STRING
154 else if(ch == '"') {
155 state.tokenize = tokenString;
156 }
157 // UNSIGNED_NUMBER
158 else if(isDigit.test(ch)) {
159 state.tokenize = tokenUnsignedNumber;
160 }
161 // ERROR
162 else {
163 state.tokenize = null;
164 return "error";
165 }
166
167 return state.tokenize(stream, state);
168 },
169
170 indent: function(state, textAfter, cx) {
171 if (state.tokenize != null) return null;
172
173 var level = state.level;
174 if(/(algorithm)/.test(textAfter)) level--;
175 if(/(equation)/.test(textAfter)) level--;
176 if(/(initial algorithm)/.test(textAfter)) level--;
177 if(/(initial equation)/.test(textAfter)) level--;
178 if(/(end)/.test(textAfter)) level--;
179
180 if(level > 0)
181 return cx.unit*level;
182 else
183 return 0;
184 },
185
186 languageData: {
187 commentTokens: {line: "//", block: {open: "/*", close: "*/"}},
188 autocomplete: completions
189 }
190 };