simple.js 7.9 KB

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