1
0

javascript.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. CodeMirror.defineMode("javascript", function(config, parserConfig) {
  2. var indentUnit = config.indentUnit;
  3. var jsonMode = parserConfig.json;
  4. // Tokenizer
  5. var keywords = function(){
  6. function kw(type) {return {type: type, style: "keyword"};}
  7. var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c");
  8. var operator = kw("operator"), atom = {type: "atom", style: "atom"};
  9. return {
  10. "if": A, "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B,
  11. "return": C, "break": C, "continue": C, "new": C, "delete": C, "throw": C,
  12. "var": kw("var"), "function": kw("function"), "catch": kw("catch"),
  13. "for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"),
  14. "in": operator, "typeof": operator, "instanceof": operator,
  15. "true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom
  16. };
  17. }();
  18. var isOperatorChar = /[+\-*&%=<>!?|]/;
  19. function chain(stream, state, f) {
  20. state.tokenize = f;
  21. return f(stream, state);
  22. }
  23. function nextUntilUnescaped(stream, end) {
  24. var escaped = false, next;
  25. while ((next = stream.next()) != null) {
  26. if (next == end && !escaped)
  27. return false;
  28. escaped = !escaped && next == "\\";
  29. }
  30. return escaped;
  31. }
  32. // Used as scratch variables to communicate multiple values without
  33. // consing up tons of objects.
  34. var type, content;
  35. function ret(tp, style, cont) {
  36. type = tp; content = cont;
  37. return style;
  38. }
  39. function jsTokenBase(stream, state) {
  40. var ch = stream.next();
  41. if (ch == '"' || ch == "'")
  42. return chain(stream, state, jsTokenString(ch));
  43. else if (/[\[\]{}\(\),;\:\.]/.test(ch))
  44. return ret(ch);
  45. else if (ch == "0" && stream.eat(/x/i)) {
  46. stream.eatWhile(/[\da-f]/i);
  47. return ret("number", "number");
  48. }
  49. else if (/\d/.test(ch)) {
  50. stream.match(/^\d*(?:\.\d*)?(?:e[+\-]?\d+)?/);
  51. return ret("number", "number");
  52. }
  53. else if (ch == "/") {
  54. if (stream.eat("*")) {
  55. return chain(stream, state, jsTokenComment);
  56. }
  57. else if (stream.eat("/")) {
  58. stream.skipToEnd();
  59. return ret("comment", "comment");
  60. }
  61. else if (state.reAllowed) {
  62. nextUntilUnescaped(stream, "/");
  63. stream.eatWhile(/[gimy]/); // 'y' is "sticky" option in Mozilla
  64. return ret("regexp", "string");
  65. }
  66. else {
  67. stream.eatWhile(isOperatorChar);
  68. return ret("operator", null, stream.current());
  69. }
  70. }
  71. else if (isOperatorChar.test(ch)) {
  72. stream.eatWhile(isOperatorChar);
  73. return ret("operator", null, stream.current());
  74. }
  75. else {
  76. stream.eatWhile(/[\w\$_]/);
  77. var word = stream.current(), known = keywords.propertyIsEnumerable(word) && keywords[word];
  78. return known ? ret(known.type, known.style, word) :
  79. ret("variable", "variable", word);
  80. }
  81. }
  82. function jsTokenString(quote) {
  83. return function(stream, state) {
  84. if (!nextUntilUnescaped(stream, quote))
  85. state.tokenize = jsTokenBase;
  86. return ret("string", "string");
  87. };
  88. }
  89. function jsTokenComment(stream, state) {
  90. var maybeEnd = false, ch;
  91. while (ch = stream.next()) {
  92. if (ch == "/" && maybeEnd) {
  93. state.tokenize = jsTokenBase;
  94. break;
  95. }
  96. maybeEnd = (ch == "*");
  97. }
  98. return ret("comment", "comment");
  99. }
  100. // Parser
  101. var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true};
  102. function JSLexical(indented, column, type, align, prev, info) {
  103. this.indented = indented;
  104. this.column = column;
  105. this.type = type;
  106. this.prev = prev;
  107. this.info = info;
  108. if (align != null) this.align = align;
  109. }
  110. function inScope(state, varname) {
  111. for (var v = state.localVars; v; v = v.next)
  112. if (v.name == varname) return true;
  113. }
  114. function parseJS(state, style, type, content, stream) {
  115. var cc = state.cc;
  116. // Communicate our context to the combinators.
  117. // (Less wasteful than consing up a hundred closures on every call.)
  118. cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc;
  119. if (!state.lexical.hasOwnProperty("align"))
  120. state.lexical.align = true;
  121. while(true) {
  122. var combinator = cc.length ? cc.pop() : jsonMode ? expression : statement;
  123. if (combinator(type, content)) {
  124. while(cc.length && cc[cc.length - 1].lex)
  125. cc.pop()();
  126. if (cx.marked) return cx.marked;
  127. if (type == "variable" && inScope(state, content)) return "variable-2";
  128. return style;
  129. }
  130. }
  131. }
  132. // Combinator utils
  133. var cx = {state: null, column: null, marked: null, cc: null};
  134. function pass() {
  135. for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]);
  136. }
  137. function cont() {
  138. pass.apply(null, arguments);
  139. return true;
  140. }
  141. function register(varname) {
  142. var state = cx.state;
  143. if (state.context) {
  144. cx.marked = "def";
  145. for (var v = state.localVars; v; v = v.next)
  146. if (v.name == varname) return;
  147. state.localVars = {name: varname, next: state.localVars};
  148. }
  149. }
  150. // Combinators
  151. var defaultVars = {name: "this", next: {name: "arguments"}};
  152. function pushcontext() {
  153. if (!cx.state.context) cx.state.localVars = defaultVars;
  154. cx.state.context = {prev: cx.state.context, vars: cx.state.localVars};
  155. }
  156. function popcontext() {
  157. cx.state.localVars = cx.state.context.vars;
  158. cx.state.context = cx.state.context.prev;
  159. }
  160. function pushlex(type, info) {
  161. var result = function() {
  162. var state = cx.state;
  163. state.lexical = new JSLexical(state.indented, cx.stream.column(), type, null, state.lexical, info)
  164. };
  165. result.lex = true;
  166. return result;
  167. }
  168. function poplex() {
  169. var state = cx.state;
  170. if (state.lexical.prev) {
  171. if (state.lexical.type == ")")
  172. state.indented = state.lexical.indented;
  173. state.lexical = state.lexical.prev;
  174. }
  175. }
  176. poplex.lex = true;
  177. function expect(wanted) {
  178. return function expecting(type) {
  179. if (type == wanted) return cont();
  180. else if (wanted == ";") return pass();
  181. else return cont(arguments.callee);
  182. };
  183. }
  184. function statement(type) {
  185. if (type == "var") return cont(pushlex("vardef"), vardef1, expect(";"), poplex);
  186. if (type == "keyword a") return cont(pushlex("form"), expression, statement, poplex);
  187. if (type == "keyword b") return cont(pushlex("form"), statement, poplex);
  188. if (type == "{") return cont(pushlex("}"), block, poplex);
  189. if (type == ";") return cont();
  190. if (type == "function") return cont(functiondef);
  191. if (type == "for") return cont(pushlex("form"), expect("("), pushlex(")"), forspec1, expect(")"),
  192. poplex, statement, poplex);
  193. if (type == "variable") return cont(pushlex("stat"), maybelabel);
  194. if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"),
  195. block, poplex, poplex);
  196. if (type == "case") return cont(expression, expect(":"));
  197. if (type == "default") return cont(expect(":"));
  198. if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),
  199. statement, poplex, popcontext);
  200. return pass(pushlex("stat"), expression, expect(";"), poplex);
  201. }
  202. function expression(type) {
  203. if (atomicTypes.hasOwnProperty(type)) return cont(maybeoperator);
  204. if (type == "function") return cont(functiondef);
  205. if (type == "keyword c") return cont(expression);
  206. if (type == "(") return cont(pushlex(")"), expression, expect(")"), poplex, maybeoperator);
  207. if (type == "operator") return cont(expression);
  208. if (type == "[") return cont(pushlex("]"), commasep(expression, "]"), poplex, maybeoperator);
  209. if (type == "{") return cont(pushlex("}"), commasep(objprop, "}"), poplex, maybeoperator);
  210. return cont();
  211. }
  212. function maybeoperator(type, value) {
  213. if (type == "operator" && /\+\+|--/.test(value)) return cont(maybeoperator);
  214. if (type == "operator") return cont(expression);
  215. if (type == ";") return;
  216. if (type == "(") return cont(pushlex(")"), commasep(expression, ")"), poplex, maybeoperator);
  217. if (type == ".") return cont(property, maybeoperator);
  218. if (type == "[") return cont(pushlex("]"), expression, expect("]"), poplex, maybeoperator);
  219. }
  220. function maybelabel(type) {
  221. if (type == ":") return cont(poplex, statement);
  222. return pass(maybeoperator, expect(";"), poplex);
  223. }
  224. function property(type) {
  225. if (type == "variable") {cx.marked = "property"; return cont();}
  226. }
  227. function objprop(type) {
  228. if (type == "variable") cx.marked = "property";
  229. if (atomicTypes.hasOwnProperty(type)) return cont(expect(":"), expression);
  230. }
  231. function commasep(what, end) {
  232. function proceed(type) {
  233. if (type == ",") return cont(what, proceed);
  234. if (type == end) return cont();
  235. return cont(expect(end));
  236. }
  237. return function commaSeparated(type) {
  238. if (type == end) return cont();
  239. else return pass(what, proceed);
  240. };
  241. }
  242. function block(type) {
  243. if (type == "}") return cont();
  244. return pass(statement, block);
  245. }
  246. function vardef1(type, value) {
  247. if (type == "variable"){register(value); return cont(vardef2);}
  248. return cont();
  249. }
  250. function vardef2(type, value) {
  251. if (value == "=") return cont(expression, vardef2);
  252. if (type == ",") return cont(vardef1);
  253. }
  254. function forspec1(type) {
  255. if (type == "var") return cont(vardef1, forspec2);
  256. if (type == ";") return pass(forspec2);
  257. if (type == "variable") return cont(formaybein);
  258. return pass(forspec2);
  259. }
  260. function formaybein(type, value) {
  261. if (value == "in") return cont(expression);
  262. return cont(maybeoperator, forspec2);
  263. }
  264. function forspec2(type, value) {
  265. if (type == ";") return cont(forspec3);
  266. if (value == "in") return cont(expression);
  267. return cont(expression, expect(";"), forspec3);
  268. }
  269. function forspec3(type) {
  270. if (type != ")") cont(expression);
  271. }
  272. function functiondef(type, value) {
  273. if (type == "variable") {register(value); return cont(functiondef);}
  274. if (type == "(") return cont(pushlex(")"), pushcontext, commasep(funarg, ")"), poplex, statement, popcontext);
  275. }
  276. function funarg(type, value) {
  277. if (type == "variable") {register(value); return cont();}
  278. }
  279. // Interface
  280. return {
  281. startState: function(basecolumn) {
  282. return {
  283. tokenize: jsTokenBase,
  284. reAllowed: true,
  285. cc: [],
  286. lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
  287. localVars: null,
  288. context: null,
  289. indented: 0
  290. };
  291. },
  292. token: function(stream, state) {
  293. if (stream.sol()) {
  294. if (!state.lexical.hasOwnProperty("align"))
  295. state.lexical.align = false;
  296. state.indented = stream.indentation();
  297. }
  298. if (stream.eatSpace()) return null;
  299. var style = state.tokenize(stream, state);
  300. if (type == "comment") return style;
  301. state.reAllowed = type == "operator" || type == "keyword c" || type.match(/^[\[{}\(,;:]$/);
  302. return parseJS(state, style, type, content, stream);
  303. },
  304. indent: function(state, textAfter) {
  305. if (state.tokenize != jsTokenBase) return 0;
  306. var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical,
  307. type = lexical.type, closing = firstChar == type;
  308. if (type == "vardef") return lexical.indented + 4;
  309. else if (type == "form" && firstChar == "{") return lexical.indented;
  310. else if (type == "stat" || type == "form") return lexical.indented + indentUnit;
  311. else if (lexical.info == "switch" && !closing)
  312. return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);
  313. else if (lexical.align) return lexical.column + (closing ? 0 : 1);
  314. else return lexical.indented + (closing ? 0 : indentUnit);
  315. },
  316. electricChars: ":{}"
  317. };
  318. });
  319. CodeMirror.defineMIME("text/javascript", "javascript");
  320. CodeMirror.defineMIME("application/json", {name: "javascript", json: true});