27b22719519fd3bd92c0136aa1ded3be4bf7c86d
[myslice.git] / third-party / codemirror-3.15 / mode / vb / vb.js
1 CodeMirror.defineMode("vb", function(conf, parserConf) {
2     var ERRORCLASS = 'error';
3
4     function wordRegexp(words) {
5         return new RegExp("^((" + words.join(")|(") + "))\\b", "i");
6     }
7
8     var singleOperators = new RegExp("^[\\+\\-\\*/%&\\\\|\\^~<>!]");
9     var singleDelimiters = new RegExp('^[\\(\\)\\[\\]\\{\\}@,:`=;\\.]');
10     var doubleOperators = new RegExp("^((==)|(<>)|(<=)|(>=)|(<>)|(<<)|(>>)|(//)|(\\*\\*))");
11     var doubleDelimiters = new RegExp("^((\\+=)|(\\-=)|(\\*=)|(%=)|(/=)|(&=)|(\\|=)|(\\^=))");
12     var tripleDelimiters = new RegExp("^((//=)|(>>=)|(<<=)|(\\*\\*=))");
13     var identifiers = new RegExp("^[_A-Za-z][_A-Za-z0-9]*");
14
15     var openingKeywords = ['class','module', 'sub','enum','select','while','if','function',  'get','set','property', 'try'];
16     var middleKeywords = ['else','elseif','case', 'catch'];
17     var endKeywords = ['next','loop'];
18
19     var wordOperators = wordRegexp(['and', 'or', 'not', 'xor', 'in']);
20     var commonkeywords = ['as', 'dim', 'break',  'continue','optional', 'then',  'until',
21                           'goto', 'byval','byref','new','handles','property', 'return',
22                           'const','private', 'protected', 'friend', 'public', 'shared', 'static', 'true','false'];
23     var commontypes = ['integer','string','double','decimal','boolean','short','char', 'float','single'];
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
38
39
40     function indent(_stream, state) {
41       state.currentIndent++;
42     }
43
44     function dedent(_stream, state) {
45       state.currentIndent--;
46     }
47     // tokenizers
48     function tokenBase(stream, state) {
49         if (stream.eatSpace()) {
50             return null;
51         }
52
53         var ch = stream.peek();
54
55         // Handle Comments
56         if (ch === "'") {
57             stream.skipToEnd();
58             return 'comment';
59         }
60
61
62         // Handle Number Literals
63         if (stream.match(/^((&H)|(&O))?[0-9\.a-f]/i, false)) {
64             var floatLiteral = false;
65             // Floats
66             if (stream.match(/^\d*\.\d+F?/i)) { floatLiteral = true; }
67             else if (stream.match(/^\d+\.\d*F?/)) { floatLiteral = true; }
68             else if (stream.match(/^\.\d+F?/)) { floatLiteral = true; }
69
70             if (floatLiteral) {
71                 // Float literals may be "imaginary"
72                 stream.eat(/J/i);
73                 return 'number';
74             }
75             // Integers
76             var intLiteral = false;
77             // Hex
78             if (stream.match(/^&H[0-9a-f]+/i)) { intLiteral = true; }
79             // Octal
80             else if (stream.match(/^&O[0-7]+/i)) { intLiteral = true; }
81             // Decimal
82             else if (stream.match(/^[1-9]\d*F?/)) {
83                 // Decimal literals may be "imaginary"
84                 stream.eat(/J/i);
85                 // TODO - Can you have imaginary longs?
86                 intLiteral = true;
87             }
88             // Zero by itself with no other piece of number.
89             else if (stream.match(/^0(?![\dx])/i)) { intLiteral = true; }
90             if (intLiteral) {
91                 // Integer literals may be "long"
92                 stream.eat(/L/i);
93                 return 'number';
94             }
95         }
96
97         // Handle Strings
98         if (stream.match(stringPrefixes)) {
99             state.tokenize = tokenStringFactory(stream.current());
100             return state.tokenize(stream, state);
101         }
102
103         // Handle operators and Delimiters
104         if (stream.match(tripleDelimiters) || stream.match(doubleDelimiters)) {
105             return null;
106         }
107         if (stream.match(doubleOperators)
108             || stream.match(singleOperators)
109             || stream.match(wordOperators)) {
110             return 'operator';
111         }
112         if (stream.match(singleDelimiters)) {
113             return null;
114         }
115         if (stream.match(doOpening)) {
116             indent(stream,state);
117             state.doInCurrentLine = true;
118             return 'keyword';
119         }
120         if (stream.match(opening)) {
121             if (! state.doInCurrentLine)
122               indent(stream,state);
123             else
124               state.doInCurrentLine = false;
125             return 'keyword';
126         }
127         if (stream.match(middle)) {
128             return 'keyword';
129         }
130
131         if (stream.match(doubleClosing)) {
132             dedent(stream,state);
133             dedent(stream,state);
134             return 'keyword';
135         }
136         if (stream.match(closing)) {
137             dedent(stream,state);
138             return 'keyword';
139         }
140
141         if (stream.match(types)) {
142             return 'keyword';
143         }
144
145         if (stream.match(keywords)) {
146             return 'keyword';
147         }
148
149         if (stream.match(identifiers)) {
150             return 'variable';
151         }
152
153         // Handle non-detected items
154         stream.next();
155         return ERRORCLASS;
156     }
157
158     function tokenStringFactory(delimiter) {
159         var singleline = delimiter.length == 1;
160         var OUTCLASS = 'string';
161
162         return function(stream, state) {
163             while (!stream.eol()) {
164                 stream.eatWhile(/[^'"]/);
165                 if (stream.match(delimiter)) {
166                     state.tokenize = tokenBase;
167                     return OUTCLASS;
168                 } else {
169                     stream.eat(/['"]/);
170                 }
171             }
172             if (singleline) {
173                 if (parserConf.singleLineStringErrors) {
174                     return ERRORCLASS;
175                 } else {
176                     state.tokenize = tokenBase;
177                 }
178             }
179             return OUTCLASS;
180         };
181     }
182
183
184     function tokenLexer(stream, state) {
185         var style = state.tokenize(stream, state);
186         var current = stream.current();
187
188         // Handle '.' connected identifiers
189         if (current === '.') {
190             style = state.tokenize(stream, state);
191             current = stream.current();
192             if (style === 'variable') {
193                 return 'variable';
194             } else {
195                 return ERRORCLASS;
196             }
197         }
198
199
200         var delimiter_index = '[({'.indexOf(current);
201         if (delimiter_index !== -1) {
202             indent(stream, state );
203         }
204         if (indentInfo === 'dedent') {
205             if (dedent(stream, state)) {
206                 return ERRORCLASS;
207             }
208         }
209         delimiter_index = '])}'.indexOf(current);
210         if (delimiter_index !== -1) {
211             if (dedent(stream, state)) {
212                 return ERRORCLASS;
213             }
214         }
215
216         return style;
217     }
218
219     var external = {
220         electricChars:"dDpPtTfFeE ",
221         startState: function() {
222             return {
223               tokenize: tokenBase,
224               lastToken: null,
225               currentIndent: 0,
226               nextLineIndent: 0,
227               doInCurrentLine: false
228
229
230           };
231         },
232
233         token: function(stream, state) {
234             if (stream.sol()) {
235               state.currentIndent += state.nextLineIndent;
236               state.nextLineIndent = 0;
237               state.doInCurrentLine = 0;
238             }
239             var style = tokenLexer(stream, state);
240
241             state.lastToken = {style:style, content: stream.current()};
242
243
244
245             return style;
246         },
247
248         indent: function(state, textAfter) {
249             var trueText = textAfter.replace(/^\s+|\s+$/g, '') ;
250             if (trueText.match(closing) || trueText.match(doubleClosing) || trueText.match(middle)) return conf.indentUnit*(state.currentIndent-1);
251             if(state.currentIndent < 0) return 0;
252             return state.currentIndent * conf.indentUnit;
253         }
254
255     };
256     return external;
257 });
258
259 CodeMirror.defineMIME("text/x-vb", "vb");