LaravelTest
243 строки · 5.5 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: http://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("elm", function() {15
16function switchState(source, setState, f)17{18setState(f);19return f(source, setState);20}21
22var lowerRE = /[a-z]/;23var upperRE = /[A-Z]/;24var innerRE = /[a-zA-Z0-9_]/;25
26var digitRE = /[0-9]/;27var hexRE = /[0-9A-Fa-f]/;28var symbolRE = /[-&*+.\\/<>=?^|:]/;29var specialRE = /[(),[\]{}]/;30var spacesRE = /[ \v\f]/; // newlines are handled in tokenizer31
32function normal()33{34return function(source, setState)35{36if (source.eatWhile(spacesRE))37{38return null;39}40
41var char = source.next();42
43if (specialRE.test(char))44{45return (char === '{' && source.eat('-'))46? switchState(source, setState, chompMultiComment(1))47: (char === '[' && source.match('glsl|'))48? switchState(source, setState, chompGlsl)49: 'builtin';50}51
52if (char === '\'')53{54return switchState(source, setState, chompChar);55}56
57if (char === '"')58{59return source.eat('"')60? source.eat('"')61? switchState(source, setState, chompMultiString)62: 'string'63: switchState(source, setState, chompSingleString);64}65
66if (upperRE.test(char))67{68source.eatWhile(innerRE);69return 'variable-2';70}71
72if (lowerRE.test(char))73{74var isDef = source.pos === 1;75source.eatWhile(innerRE);76return isDef ? "def" : "variable";77}78
79if (digitRE.test(char))80{81if (char === '0')82{83if (source.eat(/[xX]/))84{85source.eatWhile(hexRE); // should require at least 186return "number";87}88}89else90{91source.eatWhile(digitRE);92}93if (source.eat('.'))94{95source.eatWhile(digitRE); // should require at least 196}97if (source.eat(/[eE]/))98{99source.eat(/[-+]/);100source.eatWhile(digitRE); // should require at least 1101}102return "number";103}104
105if (symbolRE.test(char))106{107if (char === '-' && source.eat('-'))108{109source.skipToEnd();110return "comment";111}112source.eatWhile(symbolRE);113return "keyword";114}115
116if (char === '_')117{118return "keyword";119}120
121return "error";122}123}124
125function chompMultiComment(nest)126{127if (nest == 0)128{129return normal();130}131return function(source, setState)132{133while (!source.eol())134{135var char = source.next();136if (char == '{' && source.eat('-'))137{138++nest;139}140else if (char == '-' && source.eat('}'))141{142--nest;143if (nest === 0)144{145setState(normal());146return 'comment';147}148}149}150setState(chompMultiComment(nest));151return 'comment';152}153}154
155function chompMultiString(source, setState)156{157while (!source.eol())158{159var char = source.next();160if (char === '"' && source.eat('"') && source.eat('"'))161{162setState(normal());163return 'string';164}165}166return 'string';167}168
169function chompSingleString(source, setState)170{171while (source.skipTo('\\"')) { source.next(); source.next(); }172if (source.skipTo('"'))173{174source.next();175setState(normal());176return 'string';177}178source.skipToEnd();179setState(normal());180return 'error';181}182
183function chompChar(source, setState)184{185while (source.skipTo("\\'")) { source.next(); source.next(); }186if (source.skipTo("'"))187{188source.next();189setState(normal());190return 'string';191}192source.skipToEnd();193setState(normal());194return 'error';195}196
197function chompGlsl(source, setState)198{199while (!source.eol())200{201var char = source.next();202if (char === '|' && source.eat(']'))203{204setState(normal());205return 'string';206}207}208return 'string';209}210
211var wellKnownWords = {212case: 1,213of: 1,214as: 1,215if: 1,216then: 1,217else: 1,218let: 1,219in: 1,220type: 1,221alias: 1,222module: 1,223where: 1,224import: 1,225exposing: 1,226port: 1227};228
229return {230startState: function () { return { f: normal() }; },231copyState: function (s) { return { f: s.f }; },232
233token: function(stream, state) {234var type = state.f(stream, function(s) { state.f = s; });235var word = stream.current();236return (wellKnownWords.hasOwnProperty(word)) ? 'keyword' : type;237}238};239
240});241
242CodeMirror.defineMIME("text/x-elm", "elm");243});244