5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror"], mod);
11
})(function(CodeMirror) {
14
CodeMirror.defineMode("haxe", function(config, parserConfig) {
15
var indentUnit = config.indentUnit;
19
function kw(type) {return {type: type, style: "keyword"};}
20
var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c");
21
var operator = kw("operator"), atom = {type: "atom", style: "atom"}, attribute = {type:"attribute", style: "attribute"};
22
var type = kw("typedef");
24
"if": A, "while": A, "else": B, "do": B, "try": B,
25
"return": C, "break": C, "continue": C, "new": C, "throw": C,
26
"var": kw("var"), "inline":attribute, "static": attribute, "using":kw("import"),
27
"public": attribute, "private": attribute, "cast": kw("cast"), "import": kw("import"), "macro": kw("macro"),
28
"function": kw("function"), "catch": kw("catch"), "untyped": kw("untyped"), "callback": kw("cb"),
29
"for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"),
30
"in": operator, "never": kw("property_access"), "trace":kw("trace"),
31
"class": type, "abstract":type, "enum":type, "interface":type, "typedef":type, "extends":type, "implements":type, "dynamic":type,
32
"true": atom, "false": atom, "null": atom
35
var isOperatorChar = /[+\-*&%=<>!?|]/;
37
function chain(stream, state, f) {
39
return f(stream, state);
42
function toUnescaped(stream, end) {
43
var escaped = false, next;
44
while ((next = stream.next()) != null) {
45
if (next == end && !escaped)
47
escaped = !escaped && next == "\\";
54
function ret(tp, style, cont) {
55
type = tp; content = cont;
59
function haxeTokenBase(stream, state) {
60
var ch = stream.next();
61
if (ch == '"' || ch == "'") {
62
return chain(stream, state, haxeTokenString(ch));
63
} else if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
65
} else if (ch == "0" && stream.eat(/x/i)) {
66
stream.eatWhile(/[\da-f]/i);
67
return ret("number", "number");
68
} else if (/\d/.test(ch) || ch == "-" && stream.eat(/\d/)) {
69
stream.match(/^\d*(?:\.\d*(?!\.))?(?:[eE][+\-]?\d+)?/);
70
return ret("number", "number");
71
} else if (state.reAllowed && (ch == "~" && stream.eat(/\//))) {
72
toUnescaped(stream, "/");
73
stream.eatWhile(/[gimsu]/);
74
return ret("regexp", "string-2");
75
} else if (ch == "/") {
76
if (stream.eat("*")) {
77
return chain(stream, state, haxeTokenComment);
78
} else if (stream.eat("/")) {
80
return ret("comment", "comment");
82
stream.eatWhile(isOperatorChar);
83
return ret("operator", null, stream.current());
85
} else if (ch == "#") {
87
return ret("conditional", "meta");
88
} else if (ch == "@") {
90
stream.eatWhile(/[\w_]/);
91
return ret ("metadata", "meta");
92
} else if (isOperatorChar.test(ch)) {
93
stream.eatWhile(isOperatorChar);
94
return ret("operator", null, stream.current());
97
if(/[A-Z]/.test(ch)) {
98
stream.eatWhile(/[\w_<>]/);
99
word = stream.current();
100
return ret("type", "variable-3", word);
102
stream.eatWhile(/[\w_]/);
103
var word = stream.current(), known = keywords.propertyIsEnumerable(word) && keywords[word];
104
return (known && state.kwAllowed) ? ret(known.type, known.style, word) :
105
ret("variable", "variable", word);
110
function haxeTokenString(quote) {
111
return function(stream, state) {
112
if (toUnescaped(stream, quote))
113
state.tokenize = haxeTokenBase;
114
return ret("string", "string");
118
function haxeTokenComment(stream, state) {
119
var maybeEnd = false, ch;
120
while (ch = stream.next()) {
121
if (ch == "/" && maybeEnd) {
122
state.tokenize = haxeTokenBase;
125
maybeEnd = (ch == "*");
127
return ret("comment", "comment");
132
var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true};
134
function HaxeLexical(indented, column, type, align, prev, info) {
135
this.indented = indented;
136
this.column = column;
140
if (align != null) this.align = align;
143
function inScope(state, varname) {
144
for (var v = state.localVars; v; v = v.next)
145
if (v.name == varname) return true;
148
function parseHaxe(state, style, type, content, stream) {
152
cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc;
154
if (!state.lexical.hasOwnProperty("align"))
155
state.lexical.align = true;
158
var combinator = cc.length ? cc.pop() : statement;
159
if (combinator(type, content)) {
160
while(cc.length && cc[cc.length - 1].lex)
162
if (cx.marked) return cx.marked;
163
if (type == "variable" && inScope(state, content)) return "variable-2";
164
if (type == "variable" && imported(state, content)) return "variable-3";
170
function imported(state, typename) {
171
if (/[a-z]/.test(typename.charAt(0)))
173
var len = state.importedtypes.length;
174
for (var i = 0; i<len; i++)
175
if(state.importedtypes[i]==typename) return true;
178
function registerimport(importname) {
179
var state = cx.state;
180
for (var t = state.importedtypes; t; t = t.next)
181
if(t.name == importname) return;
182
state.importedtypes = { name: importname, next: state.importedtypes };
186
var cx = {state: null, column: null, marked: null, cc: null};
188
for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]);
191
pass.apply(null, arguments);
194
function inList(name, list) {
195
for (var v = list; v; v = v.next)
196
if (v.name == name) return true;
199
function register(varname) {
200
var state = cx.state;
203
if (inList(varname, state.localVars)) return;
204
state.localVars = {name: varname, next: state.localVars};
205
} else if (state.globalVars) {
206
if (inList(varname, state.globalVars)) return;
207
state.globalVars = {name: varname, next: state.globalVars};
213
var defaultVars = {name: "this", next: null};
214
function pushcontext() {
215
if (!cx.state.context) cx.state.localVars = defaultVars;
216
cx.state.context = {prev: cx.state.context, vars: cx.state.localVars};
218
function popcontext() {
219
cx.state.localVars = cx.state.context.vars;
220
cx.state.context = cx.state.context.prev;
222
popcontext.lex = true;
223
function pushlex(type, info) {
224
var result = function() {
225
var state = cx.state;
226
state.lexical = new HaxeLexical(state.indented, cx.stream.column(), type, null, state.lexical, info);
232
var state = cx.state;
233
if (state.lexical.prev) {
234
if (state.lexical.type == ")")
235
state.indented = state.lexical.indented;
236
state.lexical = state.lexical.prev;
241
function expect(wanted) {
243
if (type == wanted) return cont();
244
else if (wanted == ";") return pass();
250
function statement(type) {
251
if (type == "@") return cont(metadef);
252
if (type == "var") return cont(pushlex("vardef"), vardef1, expect(";"), poplex);
253
if (type == "keyword a") return cont(pushlex("form"), expression, statement, poplex);
254
if (type == "keyword b") return cont(pushlex("form"), statement, poplex);
255
if (type == "{") return cont(pushlex("}"), pushcontext, block, poplex, popcontext);
256
if (type == ";") return cont();
257
if (type == "attribute") return cont(maybeattribute);
258
if (type == "function") return cont(functiondef);
259
if (type == "for") return cont(pushlex("form"), expect("("), pushlex(")"), forspec1, expect(")"),
260
poplex, statement, poplex);
261
if (type == "variable") return cont(pushlex("stat"), maybelabel);
262
if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"),
263
block, poplex, poplex);
264
if (type == "case") return cont(expression, expect(":"));
265
if (type == "default") return cont(expect(":"));
266
if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),
267
statement, poplex, popcontext);
268
if (type == "import") return cont(importdef, expect(";"));
269
if (type == "typedef") return cont(typedef);
270
return pass(pushlex("stat"), expression, expect(";"), poplex);
272
function expression(type) {
273
if (atomicTypes.hasOwnProperty(type)) return cont(maybeoperator);
274
if (type == "type" ) return cont(maybeoperator);
275
if (type == "function") return cont(functiondef);
276
if (type == "keyword c") return cont(maybeexpression);
277
if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeoperator);
278
if (type == "operator") return cont(expression);
279
if (type == "[") return cont(pushlex("]"), commasep(maybeexpression, "]"), poplex, maybeoperator);
280
if (type == "{") return cont(pushlex("}"), commasep(objprop, "}"), poplex, maybeoperator);
283
function maybeexpression(type) {
284
if (type.match(/[;\}\)\],]/)) return pass();
285
return pass(expression);
288
function maybeoperator(type, value) {
289
if (type == "operator" && /\+\+|--/.test(value)) return cont(maybeoperator);
290
if (type == "operator" || type == ":") return cont(expression);
291
if (type == ";") return;
292
if (type == "(") return cont(pushlex(")"), commasep(expression, ")"), poplex, maybeoperator);
293
if (type == ".") return cont(property, maybeoperator);
294
if (type == "[") return cont(pushlex("]"), expression, expect("]"), poplex, maybeoperator);
297
function maybeattribute(type) {
298
if (type == "attribute") return cont(maybeattribute);
299
if (type == "function") return cont(functiondef);
300
if (type == "var") return cont(vardef1);
303
function metadef(type) {
304
if(type == ":") return cont(metadef);
305
if(type == "variable") return cont(metadef);
306
if(type == "(") return cont(pushlex(")"), commasep(metaargs, ")"), poplex, statement);
308
function metaargs(type) {
309
if(type == "variable") return cont();
312
function importdef (type, value) {
313
if(type == "variable" && /[A-Z]/.test(value.charAt(0))) { registerimport(value); return cont(); }
314
else if(type == "variable" || type == "property" || type == "." || value == "*") return cont(importdef);
317
function typedef (type, value)
319
if(type == "variable" && /[A-Z]/.test(value.charAt(0))) { registerimport(value); return cont(); }
320
else if (type == "type" && /[A-Z]/.test(value.charAt(0))) { return cont(); }
323
function maybelabel(type) {
324
if (type == ":") return cont(poplex, statement);
325
return pass(maybeoperator, expect(";"), poplex);
327
function property(type) {
328
if (type == "variable") {cx.marked = "property"; return cont();}
330
function objprop(type) {
331
if (type == "variable") cx.marked = "property";
332
if (atomicTypes.hasOwnProperty(type)) return cont(expect(":"), expression);
334
function commasep(what, end) {
335
function proceed(type) {
336
if (type == ",") return cont(what, proceed);
337
if (type == end) return cont();
338
return cont(expect(end));
340
return function(type) {
341
if (type == end) return cont();
342
else return pass(what, proceed);
345
function block(type) {
346
if (type == "}") return cont();
347
return pass(statement, block);
349
function vardef1(type, value) {
350
if (type == "variable"){register(value); return cont(typeuse, vardef2);}
353
function vardef2(type, value) {
354
if (value == "=") return cont(expression, vardef2);
355
if (type == ",") return cont(vardef1);
357
function forspec1(type, value) {
358
if (type == "variable") {
360
return cont(forin, expression)
365
function forin(_type, value) {
366
if (value == "in") return cont();
368
function functiondef(type, value) {
370
if (type == "variable" || type == "type") {register(value); return cont(functiondef);}
371
if (value == "new") return cont(functiondef);
372
if (type == "(") return cont(pushlex(")"), pushcontext, commasep(funarg, ")"), poplex, typeuse, statement, popcontext);
374
function typeuse(type) {
375
if(type == ":") return cont(typestring);
377
function typestring(type) {
378
if(type == "type") return cont();
379
if(type == "variable") return cont();
380
if(type == "{") return cont(pushlex("}"), commasep(typeprop, "}"), poplex);
382
function typeprop(type) {
383
if(type == "variable") return cont(typeuse);
385
function funarg(type, value) {
386
if (type == "variable") {register(value); return cont(typeuse);}
391
startState: function(basecolumn) {
392
var defaulttypes = ["Int", "Float", "String", "Void", "Std", "Bool", "Dynamic", "Array"];
394
tokenize: haxeTokenBase,
398
lexical: new HaxeLexical((basecolumn || 0) - indentUnit, 0, "block", false),
399
localVars: parserConfig.localVars,
400
importedtypes: defaulttypes,
401
context: parserConfig.localVars && {vars: parserConfig.localVars},
404
if (parserConfig.globalVars && typeof parserConfig.globalVars == "object")
405
state.globalVars = parserConfig.globalVars;
409
token: function(stream, state) {
411
if (!state.lexical.hasOwnProperty("align"))
412
state.lexical.align = false;
413
state.indented = stream.indentation();
415
if (stream.eatSpace()) return null;
416
var style = state.tokenize(stream, state);
417
if (type == "comment") return style;
418
state.reAllowed = !!(type == "operator" || type == "keyword c" || type.match(/^[\[{}\(,;:]$/));
419
state.kwAllowed = type != '.';
420
return parseHaxe(state, style, type, content, stream);
423
indent: function(state, textAfter) {
424
if (state.tokenize != haxeTokenBase) return 0;
425
var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical;
426
if (lexical.type == "stat" && firstChar == "}") lexical = lexical.prev;
427
var type = lexical.type, closing = firstChar == type;
428
if (type == "vardef") return lexical.indented + 4;
429
else if (type == "form" && firstChar == "{") return lexical.indented;
430
else if (type == "stat" || type == "form") return lexical.indented + indentUnit;
431
else if (lexical.info == "switch" && !closing)
432
return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);
433
else if (lexical.align) return lexical.column + (closing ? 0 : 1);
434
else return lexical.indented + (closing ? 0 : indentUnit);
438
blockCommentStart: "/*",
439
blockCommentEnd: "*/",
444
CodeMirror.defineMIME("text/x-haxe", "haxe");
446
CodeMirror.defineMode("hxml", function () {
449
startState: function () {
455
token: function (stream, state) {
456
var ch = stream.peek();
457
var sol = stream.sol();
464
if (sol && ch == "-") {
465
var style = "variable-2";
469
if (stream.peek() == "-") {
474
if (stream.peek() == "D") {
480
stream.eatWhile(/[A-Z]/i);
484
var ch = stream.peek();
486
if (state.inString == false && ch == "'") {
487
state.inString = true;
491
if (state.inString == true) {
492
if (stream.skipTo("'")) {
498
if (stream.peek() == "'") {
500
state.inString = false;
513
CodeMirror.defineMIME("text/x-hxml", "hxml");