| 1 |
function mkStex(mathMode) { |
| 2 |
function pushCommand(state, command) { |
| 3 |
state.cmdState.push(command); |
| 4 |
} |
| 5 |
|
| 6 |
function peekCommand(state) { |
| 7 |
if (state.cmdState.length > 0) { |
| 8 |
return state.cmdState[state.cmdState.length - 1]; |
| 9 |
} else { |
| 10 |
return null; |
| 11 |
} |
| 12 |
} |
| 13 |
|
| 14 |
function popCommand(state) { |
| 15 |
var plug = state.cmdState.pop(); |
| 16 |
if (plug) { |
| 17 |
plug.closeBracket(); |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
// returns the non-default plugin closest to the end of the list |
| 22 |
function getMostPowerful(state) { |
| 23 |
var context = state.cmdState; |
| 24 |
for (var i = context.length - 1; i >= 0; i--) { |
| 25 |
var plug = context[i]; |
| 26 |
if (plug.name == "DEFAULT") { |
| 27 |
continue; |
| 28 |
} |
| 29 |
return plug; |
| 30 |
} |
| 31 |
return { styleIdentifier: function() { return null; } }; |
| 32 |
} |
| 33 |
|
| 34 |
function addPluginPattern(pluginName, cmdStyle, styles) { |
| 35 |
return function () { |
| 36 |
this.name = pluginName; |
| 37 |
this.bracketNo = 0; |
| 38 |
this.style = cmdStyle; |
| 39 |
this.styles = styles; |
| 40 |
this.argument = null; // \begin and \end have arguments that follow. These are stored in the plugin |
| 41 |
|
| 42 |
this.styleIdentifier = function() { |
| 43 |
return this.styles[this.bracketNo - 1] || null; |
| 44 |
}; |
| 45 |
this.openBracket = function() { |
| 46 |
this.bracketNo++; |
| 47 |
return "bracket"; |
| 48 |
}; |
| 49 |
this.closeBracket = function() {}; |
| 50 |
}; |
| 51 |
} |
| 52 |
|
| 53 |
var plugins = {}; |
| 54 |
|
| 55 |
plugins["importmodule"] = addPluginPattern("importmodule", "tag", ["string", "builtin"]); |
| 56 |
plugins["documentclass"] = addPluginPattern("documentclass", "tag", ["", "atom"]); |
| 57 |
plugins["usepackage"] = addPluginPattern("usepackage", "tag", ["atom"]); |
| 58 |
plugins["begin"] = addPluginPattern("begin", "tag", ["atom"]); |
| 59 |
plugins["end"] = addPluginPattern("end", "tag", ["atom"]); |
| 60 |
|
| 61 |
plugins["label" ] = addPluginPattern("label" , "tag", ["atom"]); |
| 62 |
plugins["ref" ] = addPluginPattern("ref" , "tag", ["atom"]); |
| 63 |
plugins["eqref" ] = addPluginPattern("eqref" , "tag", ["atom"]); |
| 64 |
plugins["cite" ] = addPluginPattern("cite" , "tag", ["atom"]); |
| 65 |
plugins["bibitem" ] = addPluginPattern("bibitem" , "tag", ["atom"]); |
| 66 |
plugins["Bibitem" ] = addPluginPattern("Bibitem" , "tag", ["atom"]); |
| 67 |
plugins["RBibitem" ] = addPluginPattern("RBibitem" , "tag", ["atom"]); |
| 68 |
|
| 69 |
plugins["DEFAULT"] = function () { |
| 70 |
this.name = "DEFAULT"; |
| 71 |
this.style = "tag"; |
| 72 |
|
| 73 |
this.styleIdentifier = this.openBracket = this.closeBracket = function() {}; |
| 74 |
}; |
| 75 |
|
| 76 |
function setState(state, f) { |
| 77 |
state.f = f; |
| 78 |
} |
| 79 |
|
| 80 |
// called when in a normal (no environment) context |
| 81 |
function normal(source, state) { |
| 82 |
var plug; |
| 83 |
// Do we look like '\command' ? If so, attempt to apply the plugin 'command' |
| 84 |
if (source.match(/^\\[a-zA-Z@\xc0-\u1fff\u2060-\uffff]+/)) { |
| 85 |
var cmdName = source.current().slice(1); |
| 86 |
plug = plugins.hasOwnProperty(cmdName) ? plugins[cmdName] : plugins["DEFAULT"]; |
| 87 |
plug = new plug(); |
| 88 |
pushCommand(state, plug); |
| 89 |
setState(state, beginParams); |
| 90 |
return plug.style; |
| 91 |
} |
| 92 |
|
| 93 |
// escape characters |
| 94 |
if (source.match(/^\\[$&%#{}_]/)) { |
| 95 |
return "tag"; |
| 96 |
} |
| 97 |
|
| 98 |
// white space control characters |
| 99 |
if (source.match(/^\\[,;!\/\\]/)) { |
| 100 |
return "tag"; |
| 101 |
} |
| 102 |
|
| 103 |
// find if we're starting various math modes |
| 104 |
if (source.match("\\[")) { |
| 105 |
setState(state, function(source, state){ return inMathMode(source, state, "\\]"); }); |
| 106 |
return "keyword"; |
| 107 |
} |
| 108 |
if (source.match("\\(")) { |
| 109 |
setState(state, function(source, state){ return inMathMode(source, state, "\\)"); }); |
| 110 |
return "keyword"; |
| 111 |
} |
| 112 |
if (source.match("$$")) { |
| 113 |
setState(state, function(source, state){ return inMathMode(source, state, "$$"); }); |
| 114 |
return "keyword"; |
| 115 |
} |
| 116 |
if (source.match("$")) { |
| 117 |
setState(state, function(source, state){ return inMathMode(source, state, "$"); }); |
| 118 |
return "keyword"; |
| 119 |
} |
| 120 |
|
| 121 |
var ch = source.next(); |
| 122 |
if (ch == "%") { |
| 123 |
source.skipToEnd(); |
| 124 |
return "comment"; |
| 125 |
} else if (ch == '}' || ch == ']') { |
| 126 |
plug = peekCommand(state); |
| 127 |
if (plug) { |
| 128 |
plug.closeBracket(ch); |
| 129 |
setState(state, beginParams); |
| 130 |
} else { |
| 131 |
return "error"; |
| 132 |
} |
| 133 |
return "bracket"; |
| 134 |
} else if (ch == '{' || ch == '[') { |
| 135 |
plug = plugins["DEFAULT"]; |
| 136 |
plug = new plug(); |
| 137 |
pushCommand(state, plug); |
| 138 |
return "bracket"; |
| 139 |
} else if (/\d/.test(ch)) { |
| 140 |
source.eatWhile(/[\w.%]/); |
| 141 |
return "atom"; |
| 142 |
} else { |
| 143 |
source.eatWhile(/[\w\-_]/); |
| 144 |
plug = getMostPowerful(state); |
| 145 |
if (plug.name == 'begin') { |
| 146 |
plug.argument = source.current(); |
| 147 |
} |
| 148 |
return plug.styleIdentifier(); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
function inMathMode(source, state, endModeSeq) { |
| 153 |
if (source.eatSpace()) { |
| 154 |
return null; |
| 155 |
} |
| 156 |
if (endModeSeq && source.match(endModeSeq)) { |
| 157 |
setState(state, normal); |
| 158 |
return "keyword"; |
| 159 |
} |
| 160 |
if (source.match(/^\\[a-zA-Z@]+/)) { |
| 161 |
return "tag"; |
| 162 |
} |
| 163 |
if (source.match(/^[a-zA-Z]+/)) { |
| 164 |
return "variableName.special"; |
| 165 |
} |
| 166 |
// escape characters |
| 167 |
if (source.match(/^\\[$&%#{}_]/)) { |
| 168 |
return "tag"; |
| 169 |
} |
| 170 |
// white space control characters |
| 171 |
if (source.match(/^\\[,;!\/]/)) { |
| 172 |
return "tag"; |
| 173 |
} |
| 174 |
// special math-mode characters |
| 175 |
if (source.match(/^[\^_&]/)) { |
| 176 |
return "tag"; |
| 177 |
} |
| 178 |
// non-special characters |
| 179 |
if (source.match(/^[+\-<>|=,\/@!*:;'"`~#?]/)) { |
| 180 |
return null; |
| 181 |
} |
| 182 |
if (source.match(/^(\d+\.\d*|\d*\.\d+|\d+)/)) { |
| 183 |
return "number"; |
| 184 |
} |
| 185 |
var ch = source.next(); |
| 186 |
if (ch == "{" || ch == "}" || ch == "[" || ch == "]" || ch == "(" || ch == ")") { |
| 187 |
return "bracket"; |
| 188 |
} |
| 189 |
|
| 190 |
if (ch == "%") { |
| 191 |
source.skipToEnd(); |
| 192 |
return "comment"; |
| 193 |
} |
| 194 |
return "error"; |
| 195 |
} |
| 196 |
|
| 197 |
function beginParams(source, state) { |
| 198 |
var ch = source.peek(), lastPlug; |
| 199 |
if (ch == '{' || ch == '[') { |
| 200 |
lastPlug = peekCommand(state); |
| 201 |
lastPlug.openBracket(ch); |
| 202 |
source.eat(ch); |
| 203 |
setState(state, normal); |
| 204 |
return "bracket"; |
| 205 |
} |
| 206 |
if (/[ \t\r]/.test(ch)) { |
| 207 |
source.eat(ch); |
| 208 |
return null; |
| 209 |
} |
| 210 |
setState(state, normal); |
| 211 |
popCommand(state); |
| 212 |
|
| 213 |
return normal(source, state); |
| 214 |
} |
| 215 |
|
| 216 |
return { |
| 217 |
name: "stex", |
| 218 |
startState: function() { |
| 219 |
var f = mathMode ? function(source, state){ return inMathMode(source, state); } : normal; |
| 220 |
return { |
| 221 |
cmdState: [], |
| 222 |
f: f |
| 223 |
}; |
| 224 |
}, |
| 225 |
copyState: function(s) { |
| 226 |
return { |
| 227 |
cmdState: s.cmdState.slice(), |
| 228 |
f: s.f |
| 229 |
}; |
| 230 |
}, |
| 231 |
token: function(stream, state) { |
| 232 |
return state.f(stream, state); |
| 233 |
}, |
| 234 |
blankLine: function(state) { |
| 235 |
state.f = normal; |
| 236 |
state.cmdState.length = 0; |
| 237 |
}, |
| 238 |
languageData: { |
| 239 |
commentTokens: {line: "%"} |
| 240 |
} |
| 241 |
}; |
| 242 |
}; |
| 243 |
|
| 244 |
export const stex = mkStex(false) |
| 245 |
export const stexMath = mkStex(true) |