LaravelTest
515 строк · 17.2 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function(mod) {5if (typeof exports == "object" && typeof module == "object") // CommonJS6mod(require("../../lib/codemirror"));7else if (typeof define == "function" && define.amd) // AMD8define(["../../lib/codemirror"], mod);9else // Plain browser env10mod(CodeMirror);11})(function(CodeMirror) {12"use strict";13
14CodeMirror.defineMode("haxe", function(config, parserConfig) {15var indentUnit = config.indentUnit;16
17// Tokenizer18
19function kw(type) {return {type: type, style: "keyword"};}20var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c");21var operator = kw("operator"), atom = {type: "atom", style: "atom"}, attribute = {type:"attribute", style: "attribute"};22var type = kw("typedef");23var keywords = {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": atom33};34
35var isOperatorChar = /[+\-*&%=<>!?|]/;36
37function chain(stream, state, f) {38state.tokenize = f;39return f(stream, state);40}41
42function toUnescaped(stream, end) {43var escaped = false, next;44while ((next = stream.next()) != null) {45if (next == end && !escaped)46return true;47escaped = !escaped && next == "\\";48}49}50
51// Used as scratch variables to communicate multiple values without52// consing up tons of objects.53var type, content;54function ret(tp, style, cont) {55type = tp; content = cont;56return style;57}58
59function haxeTokenBase(stream, state) {60var ch = stream.next();61if (ch == '"' || ch == "'") {62return chain(stream, state, haxeTokenString(ch));63} else if (/[\[\]{}\(\),;\:\.]/.test(ch)) {64return ret(ch);65} else if (ch == "0" && stream.eat(/x/i)) {66stream.eatWhile(/[\da-f]/i);67return ret("number", "number");68} else if (/\d/.test(ch) || ch == "-" && stream.eat(/\d/)) {69stream.match(/^\d*(?:\.\d*(?!\.))?(?:[eE][+\-]?\d+)?/);70return ret("number", "number");71} else if (state.reAllowed && (ch == "~" && stream.eat(/\//))) {72toUnescaped(stream, "/");73stream.eatWhile(/[gimsu]/);74return ret("regexp", "string-2");75} else if (ch == "/") {76if (stream.eat("*")) {77return chain(stream, state, haxeTokenComment);78} else if (stream.eat("/")) {79stream.skipToEnd();80return ret("comment", "comment");81} else {82stream.eatWhile(isOperatorChar);83return ret("operator", null, stream.current());84}85} else if (ch == "#") {86stream.skipToEnd();87return ret("conditional", "meta");88} else if (ch == "@") {89stream.eat(/:/);90stream.eatWhile(/[\w_]/);91return ret ("metadata", "meta");92} else if (isOperatorChar.test(ch)) {93stream.eatWhile(isOperatorChar);94return ret("operator", null, stream.current());95} else {96var word;97if(/[A-Z]/.test(ch)) {98stream.eatWhile(/[\w_<>]/);99word = stream.current();100return ret("type", "variable-3", word);101} else {102stream.eatWhile(/[\w_]/);103var word = stream.current(), known = keywords.propertyIsEnumerable(word) && keywords[word];104return (known && state.kwAllowed) ? ret(known.type, known.style, word) :105ret("variable", "variable", word);106}107}108}109
110function haxeTokenString(quote) {111return function(stream, state) {112if (toUnescaped(stream, quote))113state.tokenize = haxeTokenBase;114return ret("string", "string");115};116}117
118function haxeTokenComment(stream, state) {119var maybeEnd = false, ch;120while (ch = stream.next()) {121if (ch == "/" && maybeEnd) {122state.tokenize = haxeTokenBase;123break;124}125maybeEnd = (ch == "*");126}127return ret("comment", "comment");128}129
130// Parser131
132var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true};133
134function HaxeLexical(indented, column, type, align, prev, info) {135this.indented = indented;136this.column = column;137this.type = type;138this.prev = prev;139this.info = info;140if (align != null) this.align = align;141}142
143function inScope(state, varname) {144for (var v = state.localVars; v; v = v.next)145if (v.name == varname) return true;146}147
148function parseHaxe(state, style, type, content, stream) {149var cc = state.cc;150// Communicate our context to the combinators.151// (Less wasteful than consing up a hundred closures on every call.)152cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc;153
154if (!state.lexical.hasOwnProperty("align"))155state.lexical.align = true;156
157while(true) {158var combinator = cc.length ? cc.pop() : statement;159if (combinator(type, content)) {160while(cc.length && cc[cc.length - 1].lex)161cc.pop()();162if (cx.marked) return cx.marked;163if (type == "variable" && inScope(state, content)) return "variable-2";164if (type == "variable" && imported(state, content)) return "variable-3";165return style;166}167}168}169
170function imported(state, typename) {171if (/[a-z]/.test(typename.charAt(0)))172return false;173var len = state.importedtypes.length;174for (var i = 0; i<len; i++)175if(state.importedtypes[i]==typename) return true;176}177
178function registerimport(importname) {179var state = cx.state;180for (var t = state.importedtypes; t; t = t.next)181if(t.name == importname) return;182state.importedtypes = { name: importname, next: state.importedtypes };183}184// Combinator utils185
186var cx = {state: null, column: null, marked: null, cc: null};187function pass() {188for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]);189}190function cont() {191pass.apply(null, arguments);192return true;193}194function inList(name, list) {195for (var v = list; v; v = v.next)196if (v.name == name) return true;197return false;198}199function register(varname) {200var state = cx.state;201if (state.context) {202cx.marked = "def";203if (inList(varname, state.localVars)) return;204state.localVars = {name: varname, next: state.localVars};205} else if (state.globalVars) {206if (inList(varname, state.globalVars)) return;207state.globalVars = {name: varname, next: state.globalVars};208}209}210
211// Combinators212
213var defaultVars = {name: "this", next: null};214function pushcontext() {215if (!cx.state.context) cx.state.localVars = defaultVars;216cx.state.context = {prev: cx.state.context, vars: cx.state.localVars};217}218function popcontext() {219cx.state.localVars = cx.state.context.vars;220cx.state.context = cx.state.context.prev;221}222popcontext.lex = true;223function pushlex(type, info) {224var result = function() {225var state = cx.state;226state.lexical = new HaxeLexical(state.indented, cx.stream.column(), type, null, state.lexical, info);227};228result.lex = true;229return result;230}231function poplex() {232var state = cx.state;233if (state.lexical.prev) {234if (state.lexical.type == ")")235state.indented = state.lexical.indented;236state.lexical = state.lexical.prev;237}238}239poplex.lex = true;240
241function expect(wanted) {242function f(type) {243if (type == wanted) return cont();244else if (wanted == ";") return pass();245else return cont(f);246}247return f;248}249
250function statement(type) {251if (type == "@") return cont(metadef);252if (type == "var") return cont(pushlex("vardef"), vardef1, expect(";"), poplex);253if (type == "keyword a") return cont(pushlex("form"), expression, statement, poplex);254if (type == "keyword b") return cont(pushlex("form"), statement, poplex);255if (type == "{") return cont(pushlex("}"), pushcontext, block, poplex, popcontext);256if (type == ";") return cont();257if (type == "attribute") return cont(maybeattribute);258if (type == "function") return cont(functiondef);259if (type == "for") return cont(pushlex("form"), expect("("), pushlex(")"), forspec1, expect(")"),260poplex, statement, poplex);261if (type == "variable") return cont(pushlex("stat"), maybelabel);262if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"),263block, poplex, poplex);264if (type == "case") return cont(expression, expect(":"));265if (type == "default") return cont(expect(":"));266if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),267statement, poplex, popcontext);268if (type == "import") return cont(importdef, expect(";"));269if (type == "typedef") return cont(typedef);270return pass(pushlex("stat"), expression, expect(";"), poplex);271}272function expression(type) {273if (atomicTypes.hasOwnProperty(type)) return cont(maybeoperator);274if (type == "type" ) return cont(maybeoperator);275if (type == "function") return cont(functiondef);276if (type == "keyword c") return cont(maybeexpression);277if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeoperator);278if (type == "operator") return cont(expression);279if (type == "[") return cont(pushlex("]"), commasep(maybeexpression, "]"), poplex, maybeoperator);280if (type == "{") return cont(pushlex("}"), commasep(objprop, "}"), poplex, maybeoperator);281return cont();282}283function maybeexpression(type) {284if (type.match(/[;\}\)\],]/)) return pass();285return pass(expression);286}287
288function maybeoperator(type, value) {289if (type == "operator" && /\+\+|--/.test(value)) return cont(maybeoperator);290if (type == "operator" || type == ":") return cont(expression);291if (type == ";") return;292if (type == "(") return cont(pushlex(")"), commasep(expression, ")"), poplex, maybeoperator);293if (type == ".") return cont(property, maybeoperator);294if (type == "[") return cont(pushlex("]"), expression, expect("]"), poplex, maybeoperator);295}296
297function maybeattribute(type) {298if (type == "attribute") return cont(maybeattribute);299if (type == "function") return cont(functiondef);300if (type == "var") return cont(vardef1);301}302
303function metadef(type) {304if(type == ":") return cont(metadef);305if(type == "variable") return cont(metadef);306if(type == "(") return cont(pushlex(")"), commasep(metaargs, ")"), poplex, statement);307}308function metaargs(type) {309if(type == "variable") return cont();310}311
312function importdef (type, value) {313if(type == "variable" && /[A-Z]/.test(value.charAt(0))) { registerimport(value); return cont(); }314else if(type == "variable" || type == "property" || type == "." || value == "*") return cont(importdef);315}316
317function typedef (type, value)318{319if(type == "variable" && /[A-Z]/.test(value.charAt(0))) { registerimport(value); return cont(); }320else if (type == "type" && /[A-Z]/.test(value.charAt(0))) { return cont(); }321}322
323function maybelabel(type) {324if (type == ":") return cont(poplex, statement);325return pass(maybeoperator, expect(";"), poplex);326}327function property(type) {328if (type == "variable") {cx.marked = "property"; return cont();}329}330function objprop(type) {331if (type == "variable") cx.marked = "property";332if (atomicTypes.hasOwnProperty(type)) return cont(expect(":"), expression);333}334function commasep(what, end) {335function proceed(type) {336if (type == ",") return cont(what, proceed);337if (type == end) return cont();338return cont(expect(end));339}340return function(type) {341if (type == end) return cont();342else return pass(what, proceed);343};344}345function block(type) {346if (type == "}") return cont();347return pass(statement, block);348}349function vardef1(type, value) {350if (type == "variable"){register(value); return cont(typeuse, vardef2);}351return cont();352}353function vardef2(type, value) {354if (value == "=") return cont(expression, vardef2);355if (type == ",") return cont(vardef1);356}357function forspec1(type, value) {358if (type == "variable") {359register(value);360return cont(forin, expression)361} else {362return pass()363}364}365function forin(_type, value) {366if (value == "in") return cont();367}368function functiondef(type, value) {369//function names starting with upper-case letters are recognised as types, so cludging them together here.370if (type == "variable" || type == "type") {register(value); return cont(functiondef);}371if (value == "new") return cont(functiondef);372if (type == "(") return cont(pushlex(")"), pushcontext, commasep(funarg, ")"), poplex, typeuse, statement, popcontext);373}374function typeuse(type) {375if(type == ":") return cont(typestring);376}377function typestring(type) {378if(type == "type") return cont();379if(type == "variable") return cont();380if(type == "{") return cont(pushlex("}"), commasep(typeprop, "}"), poplex);381}382function typeprop(type) {383if(type == "variable") return cont(typeuse);384}385function funarg(type, value) {386if (type == "variable") {register(value); return cont(typeuse);}387}388
389// Interface390return {391startState: function(basecolumn) {392var defaulttypes = ["Int", "Float", "String", "Void", "Std", "Bool", "Dynamic", "Array"];393var state = {394tokenize: haxeTokenBase,395reAllowed: true,396kwAllowed: true,397cc: [],398lexical: new HaxeLexical((basecolumn || 0) - indentUnit, 0, "block", false),399localVars: parserConfig.localVars,400importedtypes: defaulttypes,401context: parserConfig.localVars && {vars: parserConfig.localVars},402indented: 0403};404if (parserConfig.globalVars && typeof parserConfig.globalVars == "object")405state.globalVars = parserConfig.globalVars;406return state;407},408
409token: function(stream, state) {410if (stream.sol()) {411if (!state.lexical.hasOwnProperty("align"))412state.lexical.align = false;413state.indented = stream.indentation();414}415if (stream.eatSpace()) return null;416var style = state.tokenize(stream, state);417if (type == "comment") return style;418state.reAllowed = !!(type == "operator" || type == "keyword c" || type.match(/^[\[{}\(,;:]$/));419state.kwAllowed = type != '.';420return parseHaxe(state, style, type, content, stream);421},422
423indent: function(state, textAfter) {424if (state.tokenize != haxeTokenBase) return 0;425var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical;426if (lexical.type == "stat" && firstChar == "}") lexical = lexical.prev;427var type = lexical.type, closing = firstChar == type;428if (type == "vardef") return lexical.indented + 4;429else if (type == "form" && firstChar == "{") return lexical.indented;430else if (type == "stat" || type == "form") return lexical.indented + indentUnit;431else if (lexical.info == "switch" && !closing)432return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);433else if (lexical.align) return lexical.column + (closing ? 0 : 1);434else return lexical.indented + (closing ? 0 : indentUnit);435},436
437electricChars: "{}",438blockCommentStart: "/*",439blockCommentEnd: "*/",440lineComment: "//"441};442});443
444CodeMirror.defineMIME("text/x-haxe", "haxe");445
446CodeMirror.defineMode("hxml", function () {447
448return {449startState: function () {450return {451define: false,452inString: false453};454},455token: function (stream, state) {456var ch = stream.peek();457var sol = stream.sol();458
459///* comments */460if (ch == "#") {461stream.skipToEnd();462return "comment";463}464if (sol && ch == "-") {465var style = "variable-2";466
467stream.eat(/-/);468
469if (stream.peek() == "-") {470stream.eat(/-/);471style = "keyword a";472}473
474if (stream.peek() == "D") {475stream.eat(/[D]/);476style = "keyword c";477state.define = true;478}479
480stream.eatWhile(/[A-Z]/i);481return style;482}483
484var ch = stream.peek();485
486if (state.inString == false && ch == "'") {487state.inString = true;488stream.next();489}490
491if (state.inString == true) {492if (stream.skipTo("'")) {493
494} else {495stream.skipToEnd();496}497
498if (stream.peek() == "'") {499stream.next();500state.inString = false;501}502
503return "string";504}505
506stream.next();507return null;508},509lineComment: "#"510};511});512
513CodeMirror.defineMIME("text/x-hxml", "hxml");514
515});516