Debian Codegraph @

codemirror-legacy-modes/mode/vb.js

raw
1 var ERRORCLASS = 'error';
2
3 function wordRegexp(words) {
4 return new RegExp("^((" + words.join(")|(") + "))\\b", "i");
5 }
6
7 var singleOperators = new RegExp("^[\\+\\-\\*/%&\\\\|\\^~<>!]");
8 var singleDelimiters = new RegExp('^[\\(\\)\\[\\]\\{\\}@,:`=;\\.]');
9 var doubleOperators = new RegExp("^((==)|(<>)|(<=)|(>=)|(<>)|(<<)|(>>)|(//)|(\\*\\*))");
10 var doubleDelimiters = new RegExp("^((\\+=)|(\\-=)|(\\*=)|(%=)|(/=)|(&=)|(\\|=)|(\\^=))");
11 var tripleDelimiters = new RegExp("^((//=)|(>>=)|(<<=)|(\\*\\*=))");
12 var identifiers = new RegExp("^[_A-Za-z][_A-Za-z0-9]*");
13
14 var openingKeywords = ['class','module', 'sub','enum','select','while','if','function', 'get','set','property', 'try', 'structure', 'synclock', 'using', 'with'];
15 var middleKeywords = ['else','elseif','case', 'catch', 'finally'];
16 var endKeywords = ['next','loop'];
17
18 var operatorKeywords = ['and', "andalso", 'or', 'orelse', 'xor', 'in', 'not', 'is', 'isnot', 'like'];
19 var wordOperators = wordRegexp(operatorKeywords);
20
21 var commonKeywords = ["#const", "#else", "#elseif", "#end", "#if", "#region", "addhandler", "addressof", "alias", "as", "byref", "byval", "cbool", "cbyte", "cchar", "cdate", "cdbl", "cdec", "cint", "clng", "cobj", "compare", "const", "continue", "csbyte", "cshort", "csng", "cstr", "cuint", "culng", "cushort", "declare", "default", "delegate", "dim", "directcast", "each", "erase", "error", "event", "exit", "explicit", "false", "for", "friend", "gettype", "goto", "handles", "implements", "imports", "infer", "inherits", "interface", "isfalse", "istrue", "lib", "me", "mod", "mustinherit", "mustoverride", "my", "mybase", "myclass", "namespace", "narrowing", "new", "nothing", "notinheritable", "notoverridable", "of", "off", "on", "operator", "option", "optional", "out", "overloads", "overridable", "overrides", "paramarray", "partial", "private", "protected", "public", "raiseevent", "readonly", "redim", "removehandler", "resume", "return", "shadows", "shared", "static", "step", "stop", "strict", "then", "throw", "to", "true", "trycast", "typeof", "until", "until", "when", "widening", "withevents", "writeonly"];
22
23 var commontypes = ['object', 'boolean', 'char', 'string', 'byte', 'sbyte', 'short', 'ushort', 'int16', 'uint16', 'integer', 'uinteger', 'int32', 'uint32', 'long', 'ulong', 'int64', 'uint64', 'decimal', 'single', 'double', 'float', 'date', 'datetime', 'intptr', 'uintptr'];
24
25 var keywords = wordRegexp(commonKeywords);
26 var types = wordRegexp(commontypes);
27 var stringPrefixes = '"';
28
29 var opening = wordRegexp(openingKeywords);
30 var middle = wordRegexp(middleKeywords);
31 var closing = wordRegexp(endKeywords);
32 var doubleClosing = wordRegexp(['end']);
33 var doOpening = wordRegexp(['do']);
34
35 var indentInfo = null;
36
37 function indent(_stream, state) {
38 state.currentIndent++;
39 }
40
41 function dedent(_stream, state) {
42 state.currentIndent--;
43 }
44 // tokenizers
45 function tokenBase(stream, state) {
46 if (stream.eatSpace()) {
47 return null;
48 }
49
50 var ch = stream.peek();
51
52 // Handle Comments
53 if (ch === "'") {
54 stream.skipToEnd();
55 return 'comment';
56 }
57
58
59 // Handle Number Literals
60 if (stream.match(/^((&H)|(&O))?[0-9\.a-f]/i, false)) {
61 var floatLiteral = false;
62 // Floats
63 if (stream.match(/^\d*\.\d+F?/i)) { floatLiteral = true; }
64 else if (stream.match(/^\d+\.\d*F?/)) { floatLiteral = true; }
65 else if (stream.match(/^\.\d+F?/)) { floatLiteral = true; }
66
67 if (floatLiteral) {
68 // Float literals may be "imaginary"
69 stream.eat(/J/i);
70 return 'number';
71 }
72 // Integers
73 var intLiteral = false;
74 // Hex
75 if (stream.match(/^&H[0-9a-f]+/i)) { intLiteral = true; }
76 // Octal
77 else if (stream.match(/^&O[0-7]+/i)) { intLiteral = true; }
78 // Decimal
79 else if (stream.match(/^[1-9]\d*F?/)) {
80 // Decimal literals may be "imaginary"
81 stream.eat(/J/i);
82 // TODO - Can you have imaginary longs?
83 intLiteral = true;
84 }
85 // Zero by itself with no other piece of number.
86 else if (stream.match(/^0(?![\dx])/i)) { intLiteral = true; }
87 if (intLiteral) {
88 // Integer literals may be "long"
89 stream.eat(/L/i);
90 return 'number';
91 }
92 }
93
94 // Handle Strings
95 if (stream.match(stringPrefixes)) {
96 state.tokenize = tokenStringFactory(stream.current());
97 return state.tokenize(stream, state);
98 }
99
100 // Handle operators and Delimiters
101 if (stream.match(tripleDelimiters) || stream.match(doubleDelimiters)) {
102 return null;
103 }
104 if (stream.match(doubleOperators)
105 || stream.match(singleOperators)
106 || stream.match(wordOperators)) {
107 return 'operator';
108 }
109 if (stream.match(singleDelimiters)) {
110 return null;
111 }
112 if (stream.match(doOpening)) {
113 indent(stream,state);
114 state.doInCurrentLine = true;
115 return 'keyword';
116 }
117 if (stream.match(opening)) {
118 if (! state.doInCurrentLine)
119 indent(stream,state);
120 else
121 state.doInCurrentLine = false;
122 return 'keyword';
123 }
124 if (stream.match(middle)) {
125 return 'keyword';
126 }
127
128 if (stream.match(doubleClosing)) {
129 dedent(stream,state);
130 dedent(stream,state);
131 return 'keyword';
132 }
133 if (stream.match(closing)) {
134 dedent(stream,state);
135 return 'keyword';
136 }
137
138 if (stream.match(types)) {
139 return 'keyword';
140 }
141
142 if (stream.match(keywords)) {
143 return 'keyword';
144 }
145
146 if (stream.match(identifiers)) {
147 return 'variable';
148 }
149
150 // Handle non-detected items
151 stream.next();
152 return ERRORCLASS;
153 }
154
155 function tokenStringFactory(delimiter) {
156 var singleline = delimiter.length == 1;
157 var OUTCLASS = 'string';
158
159 return function(stream, state) {
160 while (!stream.eol()) {
161 stream.eatWhile(/[^'"]/);
162 if (stream.match(delimiter)) {
163 state.tokenize = tokenBase;
164 return OUTCLASS;
165 } else {
166 stream.eat(/['"]/);
167 }
168 }
169 if (singleline) {
170 state.tokenize = tokenBase;
171 }
172 return OUTCLASS;
173 };
174 }
175
176
177 function tokenLexer(stream, state) {
178 var style = state.tokenize(stream, state);
179 var current = stream.current();
180
181 // Handle '.' connected identifiers
182 if (current === '.') {
183 style = state.tokenize(stream, state);
184 if (style === 'variable') {
185 return 'variable';
186 } else {
187 return ERRORCLASS;
188 }
189 }
190
191
192 var delimiter_index = '[({'.indexOf(current);
193 if (delimiter_index !== -1) {
194 indent(stream, state );
195 }
196 if (indentInfo === 'dedent') {
197 if (dedent(stream, state)) {
198 return ERRORCLASS;
199 }
200 }
201 delimiter_index = '])}'.indexOf(current);
202 if (delimiter_index !== -1) {
203 if (dedent(stream, state)) {
204 return ERRORCLASS;
205 }
206 }
207
208 return style;
209 }
210
211 export const vb = {
212 name: "vb",
213
214 startState: function() {
215 return {
216 tokenize: tokenBase,
217 lastToken: null,
218 currentIndent: 0,
219 nextLineIndent: 0,
220 doInCurrentLine: false
221
222
223 };
224 },
225
226 token: function(stream, state) {
227 if (stream.sol()) {
228 state.currentIndent += state.nextLineIndent;
229 state.nextLineIndent = 0;
230 state.doInCurrentLine = 0;
231 }
232 var style = tokenLexer(stream, state);
233
234 state.lastToken = {style:style, content: stream.current()};
235
236
237
238 return style;
239 },
240
241 indent: function(state, textAfter, cx) {
242 var trueText = textAfter.replace(/^\s+|\s+$/g, '') ;
243 if (trueText.match(closing) || trueText.match(doubleClosing) || trueText.match(middle)) return cx.unit*(state.currentIndent-1);
244 if(state.currentIndent < 0) return 0;
245 return state.currentIndent * cx.unit;
246 },
247
248 languageData: {
249 closeBrackets: {brackets: ["(", "[", "{", '"']},
250 commentTokens: {line: "'"},
251 autocomplete: openingKeywords.concat(middleKeywords).concat(endKeywords)
252 .concat(operatorKeywords).concat(commonKeywords).concat(commontypes)
253 }
254 };