runmode-standalone.js 12 KB

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