LaravelTest
216 строк · 7.9 Кб
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.defineSimpleMode = function(name, states) {15CodeMirror.defineMode(name, function(config) {16return CodeMirror.simpleMode(config, states);17});18};19
20CodeMirror.simpleMode = function(config, states) {21ensureState(states, "start");22var states_ = {}, meta = states.meta || {}, hasIndentation = false;23for (var state in states) if (state != meta && states.hasOwnProperty(state)) {24var list = states_[state] = [], orig = states[state];25for (var i = 0; i < orig.length; i++) {26var data = orig[i];27list.push(new Rule(data, states));28if (data.indent || data.dedent) hasIndentation = true;29}30}31var mode = {32startState: function() {33return {state: "start", pending: null,34local: null, localState: null,35indent: hasIndentation ? [] : null};36},37copyState: function(state) {38var s = {state: state.state, pending: state.pending,39local: state.local, localState: null,40indent: state.indent && state.indent.slice(0)};41if (state.localState)42s.localState = CodeMirror.copyState(state.local.mode, state.localState);43if (state.stack)44s.stack = state.stack.slice(0);45for (var pers = state.persistentStates; pers; pers = pers.next)46s.persistentStates = {mode: pers.mode,47spec: pers.spec,48state: pers.state == state.localState ? s.localState : CodeMirror.copyState(pers.mode, pers.state),49next: s.persistentStates};50return s;51},52token: tokenFunction(states_, config),53innerMode: function(state) { return state.local && {mode: state.local.mode, state: state.localState}; },54indent: indentFunction(states_, meta)55};56if (meta) for (var prop in meta) if (meta.hasOwnProperty(prop))57mode[prop] = meta[prop];58return mode;59};60
61function ensureState(states, name) {62if (!states.hasOwnProperty(name))63throw new Error("Undefined state " + name + " in simple mode");64}65
66function toRegex(val, caret) {67if (!val) return /(?:)/;68var flags = "";69if (val instanceof RegExp) {70if (val.ignoreCase) flags = "i";71if (val.unicode) flags += "u"72val = val.source;73} else {74val = String(val);75}76return new RegExp((caret === false ? "" : "^") + "(?:" + val + ")", flags);77}78
79function asToken(val) {80if (!val) return null;81if (val.apply) return val82if (typeof val == "string") return val.replace(/\./g, " ");83var result = [];84for (var i = 0; i < val.length; i++)85result.push(val[i] && val[i].replace(/\./g, " "));86return result;87}88
89function Rule(data, states) {90if (data.next || data.push) ensureState(states, data.next || data.push);91this.regex = toRegex(data.regex);92this.token = asToken(data.token);93this.data = data;94}95
96function tokenFunction(states, config) {97return function(stream, state) {98if (state.pending) {99var pend = state.pending.shift();100if (state.pending.length == 0) state.pending = null;101stream.pos += pend.text.length;102return pend.token;103}104
105if (state.local) {106if (state.local.end && stream.match(state.local.end)) {107var tok = state.local.endToken || null;108state.local = state.localState = null;109return tok;110} else {111var tok = state.local.mode.token(stream, state.localState), m;112if (state.local.endScan && (m = state.local.endScan.exec(stream.current())))113stream.pos = stream.start + m.index;114return tok;115}116}117
118var curState = states[state.state];119for (var i = 0; i < curState.length; i++) {120var rule = curState[i];121var matches = (!rule.data.sol || stream.sol()) && stream.match(rule.regex);122if (matches) {123if (rule.data.next) {124state.state = rule.data.next;125} else if (rule.data.push) {126(state.stack || (state.stack = [])).push(state.state);127state.state = rule.data.push;128} else if (rule.data.pop && state.stack && state.stack.length) {129state.state = state.stack.pop();130}131
132if (rule.data.mode)133enterLocalMode(config, state, rule.data.mode, rule.token);134if (rule.data.indent)135state.indent.push(stream.indentation() + config.indentUnit);136if (rule.data.dedent)137state.indent.pop();138var token = rule.token139if (token && token.apply) token = token(matches)140if (matches.length > 2 && rule.token && typeof rule.token != "string") {141for (var j = 2; j < matches.length; j++)142if (matches[j])143(state.pending || (state.pending = [])).push({text: matches[j], token: rule.token[j - 1]});144stream.backUp(matches[0].length - (matches[1] ? matches[1].length : 0));145return token[0];146} else if (token && token.join) {147return token[0];148} else {149return token;150}151}152}153stream.next();154return null;155};156}157
158function cmp(a, b) {159if (a === b) return true;160if (!a || typeof a != "object" || !b || typeof b != "object") return false;161var props = 0;162for (var prop in a) if (a.hasOwnProperty(prop)) {163if (!b.hasOwnProperty(prop) || !cmp(a[prop], b[prop])) return false;164props++;165}166for (var prop in b) if (b.hasOwnProperty(prop)) props--;167return props == 0;168}169
170function enterLocalMode(config, state, spec, token) {171var pers;172if (spec.persistent) for (var p = state.persistentStates; p && !pers; p = p.next)173if (spec.spec ? cmp(spec.spec, p.spec) : spec.mode == p.mode) pers = p;174var mode = pers ? pers.mode : spec.mode || CodeMirror.getMode(config, spec.spec);175var lState = pers ? pers.state : CodeMirror.startState(mode);176if (spec.persistent && !pers)177state.persistentStates = {mode: mode, spec: spec.spec, state: lState, next: state.persistentStates};178
179state.localState = lState;180state.local = {mode: mode,181end: spec.end && toRegex(spec.end),182endScan: spec.end && spec.forceEnd !== false && toRegex(spec.end, false),183endToken: token && token.join ? token[token.length - 1] : token};184}185
186function indexOf(val, arr) {187for (var i = 0; i < arr.length; i++) if (arr[i] === val) return true;188}189
190function indentFunction(states, meta) {191return function(state, textAfter, line) {192if (state.local && state.local.mode.indent)193return state.local.mode.indent(state.localState, textAfter, line);194if (state.indent == null || state.local || meta.dontIndentStates && indexOf(state.state, meta.dontIndentStates) > -1)195return CodeMirror.Pass;196
197var pos = state.indent.length - 1, rules = states[state.state];198scan: for (;;) {199for (var i = 0; i < rules.length; i++) {200var rule = rules[i];201if (rule.data.dedent && rule.data.dedentIfLineStart !== false) {202var m = rule.regex.exec(textAfter);203if (m && m[0]) {204pos--;205if (rule.next || rule.push) rules = states[rule.next || rule.push];206textAfter = textAfter.slice(m[0].length);207continue scan;208}209}210}211break;212}213return pos < 0 ? 0 : state.indent[pos];214};215}216});217