simple.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://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. val = val.source;
  67. } else {
  68. val = String(val);
  69. }
  70. return new RegExp((caret === false ? "" : "^") + "(?:" + val + ")", flags);
  71. }
  72. function asToken(val) {
  73. if (!val) return null;
  74. if (typeof val == "string") return val.replace(/\./g, " ");
  75. var result = [];
  76. for (var i = 0; i < val.length; i++)
  77. result.push(val[i] && val[i].replace(/\./g, " "));
  78. return result;
  79. }
  80. function Rule(data, states) {
  81. if (data.next || data.push) ensureState(states, data.next || data.push);
  82. this.regex = toRegex(data.regex);
  83. this.token = asToken(data.token);
  84. this.data = data;
  85. }
  86. function tokenFunction(states, config) {
  87. return function(stream, state) {
  88. if (state.pending) {
  89. var pend = state.pending.shift();
  90. if (state.pending.length == 0) state.pending = null;
  91. stream.pos += pend.text.length;
  92. return pend.token;
  93. }
  94. if (state.local) {
  95. if (state.local.end && stream.match(state.local.end)) {
  96. var tok = state.local.endToken || null;
  97. state.local = state.localState = null;
  98. return tok;
  99. } else {
  100. var tok = state.local.mode.token(stream, state.localState), m;
  101. if (state.local.endScan && (m = state.local.endScan.exec(stream.current())))
  102. stream.pos = stream.start + m.index;
  103. return tok;
  104. }
  105. }
  106. var curState = states[state.state];
  107. for (var i = 0; i < curState.length; i++) {
  108. var rule = curState[i];
  109. var matches = (!rule.data.sol || stream.sol()) && stream.match(rule.regex);
  110. if (matches) {
  111. if (rule.data.next) {
  112. state.state = rule.data.next;
  113. } else if (rule.data.push) {
  114. (state.stack || (state.stack = [])).push(state.state);
  115. state.state = rule.data.push;
  116. } else if (rule.data.pop && state.stack && state.stack.length) {
  117. state.state = state.stack.pop();
  118. }
  119. if (rule.data.mode)
  120. enterLocalMode(config, state, rule.data.mode, rule.token);
  121. if (rule.data.indent)
  122. state.indent.push(stream.indentation() + config.indentUnit);
  123. if (rule.data.dedent)
  124. state.indent.pop();
  125. if (matches.length > 2) {
  126. state.pending = [];
  127. for (var j = 2; j < matches.length; j++)
  128. if (matches[j])
  129. state.pending.push({text: matches[j], token: rule.token[j - 1]});
  130. stream.backUp(matches[0].length - (matches[1] ? matches[1].length : 0));
  131. return rule.token[0];
  132. } else if (rule.token && rule.token.join) {
  133. return rule.token[0];
  134. } else {
  135. return rule.token;
  136. }
  137. }
  138. }
  139. stream.next();
  140. return null;
  141. };
  142. }
  143. function cmp(a, b) {
  144. if (a === b) return true;
  145. if (!a || typeof a != "object" || !b || typeof b != "object") return false;
  146. var props = 0;
  147. for (var prop in a) if (a.hasOwnProperty(prop)) {
  148. if (!b.hasOwnProperty(prop) || !cmp(a[prop], b[prop])) return false;
  149. props++;
  150. }
  151. for (var prop in b) if (b.hasOwnProperty(prop)) props--;
  152. return props == 0;
  153. }
  154. function enterLocalMode(config, state, spec, token) {
  155. var pers;
  156. if (spec.persistent) for (var p = state.persistentStates; p && !pers; p = p.next)
  157. if (spec.spec ? cmp(spec.spec, p.spec) : spec.mode == p.mode) pers = p;
  158. var mode = pers ? pers.mode : spec.mode || CodeMirror.getMode(config, spec.spec);
  159. var lState = pers ? pers.state : CodeMirror.startState(mode);
  160. if (spec.persistent && !pers)
  161. state.persistentStates = {mode: mode, spec: spec.spec, state: lState, next: state.persistentStates};
  162. state.localState = lState;
  163. state.local = {mode: mode,
  164. end: spec.end && toRegex(spec.end),
  165. endScan: spec.end && spec.forceEnd !== false && toRegex(spec.end, false),
  166. endToken: token && token.join ? token[token.length - 1] : token};
  167. }
  168. function indexOf(val, arr) {
  169. for (var i = 0; i < arr.length; i++) if (arr[i] === val) return true;
  170. }
  171. function indentFunction(states, meta) {
  172. return function(state, textAfter, line) {
  173. if (state.local && state.local.mode.indent)
  174. return state.local.mode.indent(state.localState, textAfter, line);
  175. if (state.indent == null || state.local || meta.dontIndentStates && indexOf(state.state, meta.dontIndentStates) > -1)
  176. return CodeMirror.Pass;
  177. var pos = state.indent.length - 1, rules = states[state.state];
  178. scan: for (;;) {
  179. for (var i = 0; i < rules.length; i++) {
  180. var rule = rules[i];
  181. if (rule.data.dedent && rule.data.dedentIfLineStart !== false) {
  182. var m = rule.regex.exec(textAfter);
  183. if (m && m[0]) {
  184. pos--;
  185. if (rule.next || rule.push) rules = states[rule.next || rule.push];
  186. textAfter = textAfter.slice(m[0].length);
  187. continue scan;
  188. }
  189. }
  190. }
  191. break;
  192. }
  193. return pos < 0 ? 0 : state.indent[pos];
  194. };
  195. }
  196. });