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