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.defineSimpleMode = function(name, states) {
15
CodeMirror.defineMode(name, function(config) {
16
return CodeMirror.simpleMode(config, states);
20
CodeMirror.simpleMode = function(config, states) {
21
ensureState(states, "start");
22
var states_ = {}, meta = states.meta || {}, hasIndentation = false;
23
for (var state in states) if (state != meta && states.hasOwnProperty(state)) {
24
var list = states_[state] = [], orig = states[state];
25
for (var i = 0; i < orig.length; i++) {
27
list.push(new Rule(data, states));
28
if (data.indent || data.dedent) hasIndentation = true;
32
startState: function() {
33
return {state: "start", pending: null,
34
local: null, localState: null,
35
indent: hasIndentation ? [] : null};
37
copyState: function(state) {
38
var s = {state: state.state, pending: state.pending,
39
local: state.local, localState: null,
40
indent: state.indent && state.indent.slice(0)};
42
s.localState = CodeMirror.copyState(state.local.mode, state.localState);
44
s.stack = state.stack.slice(0);
45
for (var pers = state.persistentStates; pers; pers = pers.next)
46
s.persistentStates = {mode: pers.mode,
48
state: pers.state == state.localState ? s.localState : CodeMirror.copyState(pers.mode, pers.state),
49
next: s.persistentStates};
52
token: tokenFunction(states_, config),
53
innerMode: function(state) { return state.local && {mode: state.local.mode, state: state.localState}; },
54
indent: indentFunction(states_, meta)
56
if (meta) for (var prop in meta) if (meta.hasOwnProperty(prop))
57
mode[prop] = meta[prop];
61
function ensureState(states, name) {
62
if (!states.hasOwnProperty(name))
63
throw new Error("Undefined state " + name + " in simple mode");
66
function toRegex(val, caret) {
67
if (!val) return /(?:)/;
69
if (val instanceof RegExp) {
70
if (val.ignoreCase) flags = "i";
71
if (val.unicode) flags += "u"
76
return new RegExp((caret === false ? "" : "^") + "(?:" + val + ")", flags);
79
function asToken(val) {
80
if (!val) return null;
81
if (val.apply) return val
82
if (typeof val == "string") return val.replace(/\./g, " ");
84
for (var i = 0; i < val.length; i++)
85
result.push(val[i] && val[i].replace(/\./g, " "));
89
function Rule(data, states) {
90
if (data.next || data.push) ensureState(states, data.next || data.push);
91
this.regex = toRegex(data.regex);
92
this.token = asToken(data.token);
96
function tokenFunction(states, config) {
97
return function(stream, state) {
99
var pend = state.pending.shift();
100
if (state.pending.length == 0) state.pending = null;
101
stream.pos += pend.text.length;
106
if (state.local.end && stream.match(state.local.end)) {
107
var tok = state.local.endToken || null;
108
state.local = state.localState = null;
111
var tok = state.local.mode.token(stream, state.localState), m;
112
if (state.local.endScan && (m = state.local.endScan.exec(stream.current())))
113
stream.pos = stream.start + m.index;
118
var curState = states[state.state];
119
for (var i = 0; i < curState.length; i++) {
120
var rule = curState[i];
121
var matches = (!rule.data.sol || stream.sol()) && stream.match(rule.regex);
123
if (rule.data.next) {
124
state.state = rule.data.next;
125
} else if (rule.data.push) {
126
(state.stack || (state.stack = [])).push(state.state);
127
state.state = rule.data.push;
128
} else if (rule.data.pop && state.stack && state.stack.length) {
129
state.state = state.stack.pop();
133
enterLocalMode(config, state, rule.data.mode, rule.token);
134
if (rule.data.indent)
135
state.indent.push(stream.indentation() + config.indentUnit);
136
if (rule.data.dedent)
138
var token = rule.token
139
if (token && token.apply) token = token(matches)
140
if (matches.length > 2 && rule.token && typeof rule.token != "string") {
141
for (var j = 2; j < matches.length; j++)
143
(state.pending || (state.pending = [])).push({text: matches[j], token: rule.token[j - 1]});
144
stream.backUp(matches[0].length - (matches[1] ? matches[1].length : 0));
146
} else if (token && token.join) {
159
if (a === b) return true;
160
if (!a || typeof a != "object" || !b || typeof b != "object") return false;
162
for (var prop in a) if (a.hasOwnProperty(prop)) {
163
if (!b.hasOwnProperty(prop) || !cmp(a[prop], b[prop])) return false;
166
for (var prop in b) if (b.hasOwnProperty(prop)) props--;
170
function enterLocalMode(config, state, spec, token) {
172
if (spec.persistent) for (var p = state.persistentStates; p && !pers; p = p.next)
173
if (spec.spec ? cmp(spec.spec, p.spec) : spec.mode == p.mode) pers = p;
174
var mode = pers ? pers.mode : spec.mode || CodeMirror.getMode(config, spec.spec);
175
var lState = pers ? pers.state : CodeMirror.startState(mode);
176
if (spec.persistent && !pers)
177
state.persistentStates = {mode: mode, spec: spec.spec, state: lState, next: state.persistentStates};
179
state.localState = lState;
180
state.local = {mode: mode,
181
end: spec.end && toRegex(spec.end),
182
endScan: spec.end && spec.forceEnd !== false && toRegex(spec.end, false),
183
endToken: token && token.join ? token[token.length - 1] : token};
186
function indexOf(val, arr) {
187
for (var i = 0; i < arr.length; i++) if (arr[i] === val) return true;
190
function indentFunction(states, meta) {
191
return function(state, textAfter, line) {
192
if (state.local && state.local.mode.indent)
193
return state.local.mode.indent(state.localState, textAfter, line);
194
if (state.indent == null || state.local || meta.dontIndentStates && indexOf(state.state, meta.dontIndentStates) > -1)
195
return CodeMirror.Pass;
197
var pos = state.indent.length - 1, rules = states[state.state];
199
for (var i = 0; i < rules.length; i++) {
201
if (rule.data.dedent && rule.data.dedentIfLineStart !== false) {
202
var m = rule.regex.exec(textAfter);
205
if (rule.next || rule.push) rules = states[rule.next || rule.push];
206
textAfter = textAfter.slice(m[0].length);
213
return pos < 0 ? 0 : state.indent[pos];