1 CodeMirror.defineMode("vb", function(conf, parserConf) {
2 var ERRORCLASS = 'error';
4 function wordRegexp(words) {
5 return new RegExp("^((" + words.join(")|(") + "))\\b", "i");
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]*");
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'];
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'];
25 var keywords = wordRegexp(commonkeywords);
26 var types = wordRegexp(commontypes);
27 var stringPrefixes = '"';
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']);
35 var indentInfo = null;
40 function indent(_stream, state) {
41 state.currentIndent++;
44 function dedent(_stream, state) {
45 state.currentIndent--;
48 function tokenBase(stream, state) {
49 if (stream.eatSpace()) {
53 var ch = stream.peek();
62 // Handle Number Literals
63 if (stream.match(/^((&H)|(&O))?[0-9\.a-f]/i, false)) {
64 var floatLiteral = false;
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; }
71 // Float literals may be "imaginary"
76 var intLiteral = false;
78 if (stream.match(/^&H[0-9a-f]+/i)) { intLiteral = true; }
80 else if (stream.match(/^&O[0-7]+/i)) { intLiteral = true; }
82 else if (stream.match(/^[1-9]\d*F?/)) {
83 // Decimal literals may be "imaginary"
85 // TODO - Can you have imaginary longs?
88 // Zero by itself with no other piece of number.
89 else if (stream.match(/^0(?![\dx])/i)) { intLiteral = true; }
91 // Integer literals may be "long"
98 if (stream.match(stringPrefixes)) {
99 state.tokenize = tokenStringFactory(stream.current());
100 return state.tokenize(stream, state);
103 // Handle operators and Delimiters
104 if (stream.match(tripleDelimiters) || stream.match(doubleDelimiters)) {
107 if (stream.match(doubleOperators)
108 || stream.match(singleOperators)
109 || stream.match(wordOperators)) {
112 if (stream.match(singleDelimiters)) {
115 if (stream.match(doOpening)) {
116 indent(stream,state);
117 state.doInCurrentLine = true;
120 if (stream.match(opening)) {
121 if (! state.doInCurrentLine)
122 indent(stream,state);
124 state.doInCurrentLine = false;
127 if (stream.match(middle)) {
131 if (stream.match(doubleClosing)) {
132 dedent(stream,state);
133 dedent(stream,state);
136 if (stream.match(closing)) {
137 dedent(stream,state);
141 if (stream.match(types)) {
145 if (stream.match(keywords)) {
149 if (stream.match(identifiers)) {
153 // Handle non-detected items
158 function tokenStringFactory(delimiter) {
159 var singleline = delimiter.length == 1;
160 var OUTCLASS = 'string';
162 return function(stream, state) {
163 while (!stream.eol()) {
164 stream.eatWhile(/[^'"]/);
165 if (stream.match(delimiter)) {
166 state.tokenize = tokenBase;
173 if (parserConf.singleLineStringErrors) {
176 state.tokenize = tokenBase;
184 function tokenLexer(stream, state) {
185 var style = state.tokenize(stream, state);
186 var current = stream.current();
188 // Handle '.' connected identifiers
189 if (current === '.') {
190 style = state.tokenize(stream, state);
191 current = stream.current();
192 if (style === 'variable') {
200 var delimiter_index = '[({'.indexOf(current);
201 if (delimiter_index !== -1) {
202 indent(stream, state );
204 if (indentInfo === 'dedent') {
205 if (dedent(stream, state)) {
209 delimiter_index = '])}'.indexOf(current);
210 if (delimiter_index !== -1) {
211 if (dedent(stream, state)) {
220 electricChars:"dDpPtTfFeE ",
221 startState: function() {
227 doInCurrentLine: false
233 token: function(stream, state) {
235 state.currentIndent += state.nextLineIndent;
236 state.nextLineIndent = 0;
237 state.doInCurrentLine = 0;
239 var style = tokenLexer(stream, state);
241 state.lastToken = {style:style, content: stream.current()};
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;
259 CodeMirror.defineMIME("text/x-vb", "vb");