You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

330 lines
11KB

  1. 'use strict';
  2. function copyObj(obj, target, overwrite) {
  3. if (!target) { target = {}; }
  4. for (var prop in obj)
  5. { if (obj.hasOwnProperty(prop) && (overwrite !== false || !target.hasOwnProperty(prop)))
  6. { target[prop] = obj[prop]; } }
  7. return target
  8. }
  9. // Counts the column offset in a string, taking tabs into account.
  10. // Used mostly to find indentation.
  11. function countColumn(string, end, tabSize, startIndex, startValue) {
  12. if (end == null) {
  13. end = string.search(/[^\s\u00a0]/);
  14. if (end == -1) { end = string.length; }
  15. }
  16. for (var i = startIndex || 0, n = startValue || 0;;) {
  17. var nextTab = string.indexOf("\t", i);
  18. if (nextTab < 0 || nextTab >= end)
  19. { return n + (end - i) }
  20. n += nextTab - i;
  21. n += tabSize - (n % tabSize);
  22. i = nextTab + 1;
  23. }
  24. }
  25. function nothing() {}
  26. function createObj(base, props) {
  27. var inst;
  28. if (Object.create) {
  29. inst = Object.create(base);
  30. } else {
  31. nothing.prototype = base;
  32. inst = new nothing();
  33. }
  34. if (props) { copyObj(props, inst); }
  35. return inst
  36. }
  37. // STRING STREAM
  38. // Fed to the mode parsers, provides helper functions to make
  39. // parsers more succinct.
  40. var StringStream = function(string, tabSize, lineOracle) {
  41. this.pos = this.start = 0;
  42. this.string = string;
  43. this.tabSize = tabSize || 8;
  44. this.lastColumnPos = this.lastColumnValue = 0;
  45. this.lineStart = 0;
  46. this.lineOracle = lineOracle;
  47. };
  48. StringStream.prototype.eol = function () {return this.pos >= this.string.length};
  49. StringStream.prototype.sol = function () {return this.pos == this.lineStart};
  50. StringStream.prototype.peek = function () {return this.string.charAt(this.pos) || undefined};
  51. StringStream.prototype.next = function () {
  52. if (this.pos < this.string.length)
  53. { return this.string.charAt(this.pos++) }
  54. };
  55. StringStream.prototype.eat = function (match) {
  56. var ch = this.string.charAt(this.pos);
  57. var ok;
  58. if (typeof match == "string") { ok = ch == match; }
  59. else { ok = ch && (match.test ? match.test(ch) : match(ch)); }
  60. if (ok) {++this.pos; return ch}
  61. };
  62. StringStream.prototype.eatWhile = function (match) {
  63. var start = this.pos;
  64. while (this.eat(match)){}
  65. return this.pos > start
  66. };
  67. StringStream.prototype.eatSpace = function () {
  68. var start = this.pos;
  69. while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) { ++this.pos; }
  70. return this.pos > start
  71. };
  72. StringStream.prototype.skipToEnd = function () {this.pos = this.string.length;};
  73. StringStream.prototype.skipTo = function (ch) {
  74. var found = this.string.indexOf(ch, this.pos);
  75. if (found > -1) {this.pos = found; return true}
  76. };
  77. StringStream.prototype.backUp = function (n) {this.pos -= n;};
  78. StringStream.prototype.column = function () {
  79. if (this.lastColumnPos < this.start) {
  80. this.lastColumnValue = countColumn(this.string, this.start, this.tabSize, this.lastColumnPos, this.lastColumnValue);
  81. this.lastColumnPos = this.start;
  82. }
  83. return this.lastColumnValue - (this.lineStart ? countColumn(this.string, this.lineStart, this.tabSize) : 0)
  84. };
  85. StringStream.prototype.indentation = function () {
  86. return countColumn(this.string, null, this.tabSize) -
  87. (this.lineStart ? countColumn(this.string, this.lineStart, this.tabSize) : 0)
  88. };
  89. StringStream.prototype.match = function (pattern, consume, caseInsensitive) {
  90. if (typeof pattern == "string") {
  91. var cased = function (str) { return caseInsensitive ? str.toLowerCase() : str; };
  92. var substr = this.string.substr(this.pos, pattern.length);
  93. if (cased(substr) == cased(pattern)) {
  94. if (consume !== false) { this.pos += pattern.length; }
  95. return true
  96. }
  97. } else {
  98. var match = this.string.slice(this.pos).match(pattern);
  99. if (match && match.index > 0) { return null }
  100. if (match && consume !== false) { this.pos += match[0].length; }
  101. return match
  102. }
  103. };
  104. StringStream.prototype.current = function (){return this.string.slice(this.start, this.pos)};
  105. StringStream.prototype.hideFirstChars = function (n, inner) {
  106. this.lineStart += n;
  107. try { return inner() }
  108. finally { this.lineStart -= n; }
  109. };
  110. StringStream.prototype.lookAhead = function (n) {
  111. var oracle = this.lineOracle;
  112. return oracle && oracle.lookAhead(n)
  113. };
  114. StringStream.prototype.baseToken = function () {
  115. var oracle = this.lineOracle;
  116. return oracle && oracle.baseToken(this.pos)
  117. };
  118. // Known modes, by name and by MIME
  119. var modes = {}, mimeModes = {};
  120. // Extra arguments are stored as the mode's dependencies, which is
  121. // used by (legacy) mechanisms like loadmode.js to automatically
  122. // load a mode. (Preferred mechanism is the require/define calls.)
  123. function defineMode(name, mode) {
  124. if (arguments.length > 2)
  125. { mode.dependencies = Array.prototype.slice.call(arguments, 2); }
  126. modes[name] = mode;
  127. }
  128. function defineMIME(mime, spec) {
  129. mimeModes[mime] = spec;
  130. }
  131. // Given a MIME type, a {name, ...options} config object, or a name
  132. // string, return a mode config object.
  133. function resolveMode(spec) {
  134. if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) {
  135. spec = mimeModes[spec];
  136. } else if (spec && typeof spec.name == "string" && mimeModes.hasOwnProperty(spec.name)) {
  137. var found = mimeModes[spec.name];
  138. if (typeof found == "string") { found = {name: found}; }
  139. spec = createObj(found, spec);
  140. spec.name = found.name;
  141. } else if (typeof spec == "string" && /^[\w\-]+\/[\w\-]+\+xml$/.test(spec)) {
  142. return resolveMode("application/xml")
  143. } else if (typeof spec == "string" && /^[\w\-]+\/[\w\-]+\+json$/.test(spec)) {
  144. return resolveMode("application/json")
  145. }
  146. if (typeof spec == "string") { return {name: spec} }
  147. else { return spec || {name: "null"} }
  148. }
  149. // Given a mode spec (anything that resolveMode accepts), find and
  150. // initialize an actual mode object.
  151. function getMode(options, spec) {
  152. spec = resolveMode(spec);
  153. var mfactory = modes[spec.name];
  154. if (!mfactory) { return getMode(options, "text/plain") }
  155. var modeObj = mfactory(options, spec);
  156. if (modeExtensions.hasOwnProperty(spec.name)) {
  157. var exts = modeExtensions[spec.name];
  158. for (var prop in exts) {
  159. if (!exts.hasOwnProperty(prop)) { continue }
  160. if (modeObj.hasOwnProperty(prop)) { modeObj["_" + prop] = modeObj[prop]; }
  161. modeObj[prop] = exts[prop];
  162. }
  163. }
  164. modeObj.name = spec.name;
  165. if (spec.helperType) { modeObj.helperType = spec.helperType; }
  166. if (spec.modeProps) { for (var prop$1 in spec.modeProps)
  167. { modeObj[prop$1] = spec.modeProps[prop$1]; } }
  168. return modeObj
  169. }
  170. // This can be used to attach properties to mode objects from
  171. // outside the actual mode definition.
  172. var modeExtensions = {};
  173. function extendMode(mode, properties) {
  174. var exts = modeExtensions.hasOwnProperty(mode) ? modeExtensions[mode] : (modeExtensions[mode] = {});
  175. copyObj(properties, exts);
  176. }
  177. function copyState(mode, state) {
  178. if (state === true) { return state }
  179. if (mode.copyState) { return mode.copyState(state) }
  180. var nstate = {};
  181. for (var n in state) {
  182. var val = state[n];
  183. if (val instanceof Array) { val = val.concat([]); }
  184. nstate[n] = val;
  185. }
  186. return nstate
  187. }
  188. // Given a mode and a state (for that mode), find the inner mode and
  189. // state at the position that the state refers to.
  190. function innerMode(mode, state) {
  191. var info;
  192. while (mode.innerMode) {
  193. info = mode.innerMode(state);
  194. if (!info || info.mode == mode) { break }
  195. state = info.state;
  196. mode = info.mode;
  197. }
  198. return info || {mode: mode, state: state}
  199. }
  200. function startState(mode, a1, a2) {
  201. return mode.startState ? mode.startState(a1, a2) : true
  202. }
  203. var modeMethods = {
  204. __proto__: null,
  205. modes: modes,
  206. mimeModes: mimeModes,
  207. defineMode: defineMode,
  208. defineMIME: defineMIME,
  209. resolveMode: resolveMode,
  210. getMode: getMode,
  211. modeExtensions: modeExtensions,
  212. extendMode: extendMode,
  213. copyState: copyState,
  214. innerMode: innerMode,
  215. startState: startState
  216. };
  217. // Copy StringStream and mode methods into exports (CodeMirror) object.
  218. exports.StringStream = StringStream;
  219. exports.countColumn = countColumn;
  220. for (var exported in modeMethods) { exports[exported] = modeMethods[exported]; }
  221. // Shim library CodeMirror with the minimal CodeMirror defined above.
  222. require.cache[require.resolve("../../lib/codemirror")] = require.cache[require.resolve("./runmode.node")];
  223. require.cache[require.resolve("../../addon/runmode/runmode")] = require.cache[require.resolve("./runmode.node")];
  224. // Minimal default mode.
  225. exports.defineMode("null", function () { return ({token: function (stream) { return stream.skipToEnd(); }}); });
  226. exports.defineMIME("text/plain", "null");
  227. exports.registerHelper = exports.registerGlobalHelper = Math.min;
  228. exports.splitLines = function(string) { return string.split(/\r?\n|\r/) };
  229. exports.defaults = { indentUnit: 2 };
  230. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  231. // Distributed under an MIT license: https://codemirror.net/LICENSE
  232. (function(mod) {
  233. if (typeof exports == "object" && typeof module == "object") // CommonJS
  234. { mod(require("../../lib/codemirror")); }
  235. else if (typeof define == "function" && define.amd) // AMD
  236. { define(["../../lib/codemirror"], mod); }
  237. else // Plain browser env
  238. { mod(CodeMirror); }
  239. })(function(CodeMirror) {
  240. CodeMirror.runMode = function(string, modespec, callback, options) {
  241. var mode = CodeMirror.getMode(CodeMirror.defaults, modespec);
  242. var tabSize = (options && options.tabSize) || CodeMirror.defaults.tabSize;
  243. // Create a tokenizing callback function if passed-in callback is a DOM element.
  244. if (callback.appendChild) {
  245. var ie = /MSIE \d/.test(navigator.userAgent);
  246. var ie_lt9 = ie && (document.documentMode == null || document.documentMode < 9);
  247. var node = callback, col = 0;
  248. node.innerHTML = "";
  249. callback = function(text, style) {
  250. if (text == "\n") {
  251. // Emitting LF or CRLF on IE8 or earlier results in an incorrect display.
  252. // Emitting a carriage return makes everything ok.
  253. node.appendChild(document.createTextNode(ie_lt9 ? '\r' : text));
  254. col = 0;
  255. return;
  256. }
  257. var content = "";
  258. // replace tabs
  259. for (var pos = 0;;) {
  260. var idx = text.indexOf("\t", pos);
  261. if (idx == -1) {
  262. content += text.slice(pos);
  263. col += text.length - pos;
  264. break;
  265. } else {
  266. col += idx - pos;
  267. content += text.slice(pos, idx);
  268. var size = tabSize - col % tabSize;
  269. col += size;
  270. for (var i = 0; i < size; ++i) { content += " "; }
  271. pos = idx + 1;
  272. }
  273. }
  274. // Create a node with token style and append it to the callback DOM element.
  275. if (style) {
  276. var sp = node.appendChild(document.createElement("span"));
  277. sp.className = "cm-" + style.replace(/ +/g, " cm-");
  278. sp.appendChild(document.createTextNode(content));
  279. } else {
  280. node.appendChild(document.createTextNode(content));
  281. }
  282. };
  283. }
  284. var lines = CodeMirror.splitLines(string), state = (options && options.state) || CodeMirror.startState(mode);
  285. for (var i = 0, e = lines.length; i < e; ++i) {
  286. if (i) { callback("\n"); }
  287. var stream = new CodeMirror.StringStream(lines[i], null, {
  288. lookAhead: function(n) { return lines[i + n] },
  289. baseToken: function() {}
  290. });
  291. if (!stream.string && mode.blankLine) { mode.blankLine(state); }
  292. while (!stream.eol()) {
  293. var style = mode.token(stream, state);
  294. callback(stream.current(), style, i, stream.start, state, mode);
  295. stream.start = stream.pos;
  296. }
  297. }
  298. };
  299. });