rust.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. (function(mod) {
  2. if (typeof exports == "object" && typeof module == "object") // CommonJS
  3. mod(require("../../lib/codemirror"));
  4. else if (typeof define == "function" && define.amd) // AMD
  5. define(["../../lib/codemirror"], mod);
  6. else // Plain browser env
  7. mod(CodeMirror);
  8. })(function(CodeMirror) {
  9. "use strict";
  10. CodeMirror.defineMode("rust", function() {
  11. var indentUnit = 4, altIndentUnit = 2;
  12. var valKeywords = {
  13. "if": "if-style", "while": "if-style", "else": "else-style",
  14. "do": "else-style", "ret": "else-style", "fail": "else-style",
  15. "break": "atom", "cont": "atom", "const": "let", "resource": "fn",
  16. "let": "let", "fn": "fn", "for": "for", "alt": "alt", "iface": "iface",
  17. "impl": "impl", "type": "type", "enum": "enum", "mod": "mod",
  18. "as": "op", "true": "atom", "false": "atom", "assert": "op", "check": "op",
  19. "claim": "op", "native": "ignore", "unsafe": "ignore", "import": "else-style",
  20. "export": "else-style", "copy": "op", "log": "op", "log_err": "op",
  21. "use": "op", "bind": "op", "self": "atom"
  22. };
  23. var typeKeywords = function() {
  24. var keywords = {"fn": "fn", "block": "fn", "obj": "obj"};
  25. var atoms = "bool uint int i8 i16 i32 i64 u8 u16 u32 u64 float f32 f64 str char".split(" ");
  26. for (var i = 0, e = atoms.length; i < e; ++i) keywords[atoms[i]] = "atom";
  27. return keywords;
  28. }();
  29. var operatorChar = /[+\-*&%=<>!?|\.@]/;
  30. // Tokenizer
  31. // Used as scratch variable to communicate multiple values without
  32. // consing up tons of objects.
  33. var tcat, content;
  34. function r(tc, style) {
  35. tcat = tc;
  36. return style;
  37. }
  38. function tokenBase(stream, state) {
  39. var ch = stream.next();
  40. if (ch == '"') {
  41. state.tokenize = tokenString;
  42. return state.tokenize(stream, state);
  43. }
  44. if (ch == "'") {
  45. tcat = "atom";
  46. if (stream.eat("\\")) {
  47. if (stream.skipTo("'")) { stream.next(); return "string"; }
  48. else { return "error"; }
  49. } else {
  50. stream.next();
  51. return stream.eat("'") ? "string" : "error";
  52. }
  53. }
  54. if (ch == "/") {
  55. if (stream.eat("/")) { stream.skipToEnd(); return "comment"; }
  56. if (stream.eat("*")) {
  57. state.tokenize = tokenComment(1);
  58. return state.tokenize(stream, state);
  59. }
  60. }
  61. if (ch == "#") {
  62. if (stream.eat("[")) { tcat = "open-attr"; return null; }
  63. stream.eatWhile(/\w/);
  64. return r("macro", "meta");
  65. }
  66. if (ch == ":" && stream.match(":<")) {
  67. return r("op", null);
  68. }
  69. if (ch.match(/\d/) || (ch == "." && stream.eat(/\d/))) {
  70. var flp = false;
  71. if (!stream.match(/^x[\da-f]+/i) && !stream.match(/^b[01]+/)) {
  72. stream.eatWhile(/\d/);
  73. if (stream.eat(".")) { flp = true; stream.eatWhile(/\d/); }
  74. if (stream.match(/^e[+\-]?\d+/i)) { flp = true; }
  75. }
  76. if (flp) stream.match(/^f(?:32|64)/);
  77. else stream.match(/^[ui](?:8|16|32|64)/);
  78. return r("atom", "number");
  79. }
  80. if (ch.match(/[()\[\]{}:;,]/)) return r(ch, null);
  81. if (ch == "-" && stream.eat(">")) return r("->", null);
  82. if (ch.match(operatorChar)) {
  83. stream.eatWhile(operatorChar);
  84. return r("op", null);
  85. }
  86. stream.eatWhile(/\w/);
  87. content = stream.current();
  88. if (stream.match(/^::\w/)) {
  89. stream.backUp(1);
  90. return r("prefix", "variable-2");
  91. }
  92. if (state.keywords.propertyIsEnumerable(content))
  93. return r(state.keywords[content], content.match(/true|false/) ? "atom" : "keyword");
  94. return r("name", "variable");
  95. }
  96. function tokenString(stream, state) {
  97. var ch, escaped = false;
  98. while (ch = stream.next()) {
  99. if (ch == '"' && !escaped) {
  100. state.tokenize = tokenBase;
  101. return r("atom", "string");
  102. }
  103. escaped = !escaped && ch == "\\";
  104. }
  105. // Hack to not confuse the parser when a string is split in
  106. // pieces.
  107. return r("op", "string");
  108. }
  109. function tokenComment(depth) {
  110. return function(stream, state) {
  111. var lastCh = null, ch;
  112. while (ch = stream.next()) {
  113. if (ch == "/" && lastCh == "*") {
  114. if (depth == 1) {
  115. state.tokenize = tokenBase;
  116. break;
  117. } else {
  118. state.tokenize = tokenComment(depth - 1);
  119. return state.tokenize(stream, state);
  120. }
  121. }
  122. if (ch == "*" && lastCh == "/") {
  123. state.tokenize = tokenComment(depth + 1);
  124. return state.tokenize(stream, state);
  125. }
  126. lastCh = ch;
  127. }
  128. return "comment";
  129. };
  130. }
  131. // Parser
  132. var cx = {state: null, stream: null, marked: null, cc: null};
  133. function pass() {
  134. for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]);
  135. }
  136. function cont() {
  137. pass.apply(null, arguments);
  138. return true;
  139. }
  140. function pushlex(type, info) {
  141. var result = function() {
  142. var state = cx.state;
  143. state.lexical = {indented: state.indented, column: cx.stream.column(),
  144. type: type, prev: state.lexical, info: info};
  145. };
  146. result.lex = true;
  147. return result;
  148. }
  149. function poplex() {
  150. var state = cx.state;
  151. if (state.lexical.prev) {
  152. if (state.lexical.type == ")")
  153. state.indented = state.lexical.indented;
  154. state.lexical = state.lexical.prev;
  155. }
  156. }
  157. function typecx() { cx.state.keywords = typeKeywords; }
  158. function valcx() { cx.state.keywords = valKeywords; }
  159. poplex.lex = typecx.lex = valcx.lex = true;
  160. function commasep(comb, end) {
  161. function more(type) {
  162. if (type == ",") return cont(comb, more);
  163. if (type == end) return cont();
  164. return cont(more);
  165. }
  166. return function(type) {
  167. if (type == end) return cont();
  168. return pass(comb, more);
  169. };
  170. }
  171. function stat_of(comb, tag) {
  172. return cont(pushlex("stat", tag), comb, poplex, block);
  173. }
  174. function block(type) {
  175. if (type == "}") return cont();
  176. if (type == "let") return stat_of(letdef1, "let");
  177. if (type == "fn") return stat_of(fndef);
  178. if (type == "type") return cont(pushlex("stat"), tydef, endstatement, poplex, block);
  179. if (type == "enum") return stat_of(enumdef);
  180. if (type == "mod") return stat_of(mod);
  181. if (type == "iface") return stat_of(iface);
  182. if (type == "impl") return stat_of(impl);
  183. if (type == "open-attr") return cont(pushlex("]"), commasep(expression, "]"), poplex);
  184. if (type == "ignore" || type.match(/[\]\);,]/)) return cont(block);
  185. return pass(pushlex("stat"), expression, poplex, endstatement, block);
  186. }
  187. function endstatement(type) {
  188. if (type == ";") return cont();
  189. return pass();
  190. }
  191. function expression(type) {
  192. if (type == "atom" || type == "name") return cont(maybeop);
  193. if (type == "{") return cont(pushlex("}"), exprbrace, poplex);
  194. if (type.match(/[\[\(]/)) return matchBrackets(type, expression);
  195. if (type.match(/[\]\)\};,]/)) return pass();
  196. if (type == "if-style") return cont(expression, expression);
  197. if (type == "else-style" || type == "op") return cont(expression);
  198. if (type == "for") return cont(pattern, maybetype, inop, expression, expression);
  199. if (type == "alt") return cont(expression, altbody);
  200. if (type == "fn") return cont(fndef);
  201. if (type == "macro") return cont(macro);
  202. return cont();
  203. }
  204. function maybeop(type) {
  205. if (content == ".") return cont(maybeprop);
  206. if (content == "::<"){return cont(typarams, maybeop);}
  207. if (type == "op" || content == ":") return cont(expression);
  208. if (type == "(" || type == "[") return matchBrackets(type, expression);
  209. return pass();
  210. }
  211. function maybeprop() {
  212. if (content.match(/^\w+$/)) {cx.marked = "variable"; return cont(maybeop);}
  213. return pass(expression);
  214. }
  215. function exprbrace(type) {
  216. if (type == "op") {
  217. if (content == "|") return cont(blockvars, poplex, pushlex("}", "block"), block);
  218. if (content == "||") return cont(poplex, pushlex("}", "block"), block);
  219. }
  220. if (content == "mutable" || (content.match(/^\w+$/) && cx.stream.peek() == ":"
  221. && !cx.stream.match("::", false)))
  222. return pass(record_of(expression));
  223. return pass(block);
  224. }
  225. function record_of(comb) {
  226. function ro(type) {
  227. if (content == "mutable" || content == "with") {cx.marked = "keyword"; return cont(ro);}
  228. if (content.match(/^\w*$/)) {cx.marked = "variable"; return cont(ro);}
  229. if (type == ":") return cont(comb, ro);
  230. if (type == "}") return cont();
  231. return cont(ro);
  232. }
  233. return ro;
  234. }
  235. function blockvars(type) {
  236. if (type == "name") {cx.marked = "def"; return cont(blockvars);}
  237. if (type == "op" && content == "|") return cont();
  238. return cont(blockvars);
  239. }
  240. function letdef1(type) {
  241. if (type.match(/[\]\)\};]/)) return cont();
  242. if (content == "=") return cont(expression, letdef2);
  243. if (type == ",") return cont(letdef1);
  244. return pass(pattern, maybetype, letdef1);
  245. }
  246. function letdef2(type) {
  247. if (type.match(/[\]\)\};,]/)) return pass(letdef1);
  248. else return pass(expression, letdef2);
  249. }
  250. function maybetype(type) {
  251. if (type == ":") return cont(typecx, rtype, valcx);
  252. return pass();
  253. }
  254. function inop(type) {
  255. if (type == "name" && content == "in") {cx.marked = "keyword"; return cont();}
  256. return pass();
  257. }
  258. function fndef(type) {
  259. if (content == "@" || content == "~") {cx.marked = "keyword"; return cont(fndef);}
  260. if (type == "name") {cx.marked = "def"; return cont(fndef);}
  261. if (content == "<") return cont(typarams, fndef);
  262. if (type == "{") return pass(expression);
  263. if (type == "(") return cont(pushlex(")"), commasep(argdef, ")"), poplex, fndef);
  264. if (type == "->") return cont(typecx, rtype, valcx, fndef);
  265. if (type == ";") return cont();
  266. return cont(fndef);
  267. }
  268. function tydef(type) {
  269. if (type == "name") {cx.marked = "def"; return cont(tydef);}
  270. if (content == "<") return cont(typarams, tydef);
  271. if (content == "=") return cont(typecx, rtype, valcx);
  272. return cont(tydef);
  273. }
  274. function enumdef(type) {
  275. if (type == "name") {cx.marked = "def"; return cont(enumdef);}
  276. if (content == "<") return cont(typarams, enumdef);
  277. if (content == "=") return cont(typecx, rtype, valcx, endstatement);
  278. if (type == "{") return cont(pushlex("}"), typecx, enumblock, valcx, poplex);
  279. return cont(enumdef);
  280. }
  281. function enumblock(type) {
  282. if (type == "}") return cont();
  283. if (type == "(") return cont(pushlex(")"), commasep(rtype, ")"), poplex, enumblock);
  284. if (content.match(/^\w+$/)) cx.marked = "def";
  285. return cont(enumblock);
  286. }
  287. function mod(type) {
  288. if (type == "name") {cx.marked = "def"; return cont(mod);}
  289. if (type == "{") return cont(pushlex("}"), block, poplex);
  290. return pass();
  291. }
  292. function iface(type) {
  293. if (type == "name") {cx.marked = "def"; return cont(iface);}
  294. if (content == "<") return cont(typarams, iface);
  295. if (type == "{") return cont(pushlex("}"), block, poplex);
  296. return pass();
  297. }
  298. function impl(type) {
  299. if (content == "<") return cont(typarams, impl);
  300. if (content == "of" || content == "for") {cx.marked = "keyword"; return cont(rtype, impl);}
  301. if (type == "name") {cx.marked = "def"; return cont(impl);}
  302. if (type == "{") return cont(pushlex("}"), block, poplex);
  303. return pass();
  304. }
  305. function typarams() {
  306. if (content == ">") return cont();
  307. if (content == ",") return cont(typarams);
  308. if (content == ":") return cont(rtype, typarams);
  309. return pass(rtype, typarams);
  310. }
  311. function argdef(type) {
  312. if (type == "name") {cx.marked = "def"; return cont(argdef);}
  313. if (type == ":") return cont(typecx, rtype, valcx);
  314. return pass();
  315. }
  316. function rtype(type) {
  317. if (type == "name") {cx.marked = "variable-3"; return cont(rtypemaybeparam); }
  318. if (content == "mutable") {cx.marked = "keyword"; return cont(rtype);}
  319. if (type == "atom") return cont(rtypemaybeparam);
  320. if (type == "op" || type == "obj") return cont(rtype);
  321. if (type == "fn") return cont(fntype);
  322. if (type == "{") return cont(pushlex("{"), record_of(rtype), poplex);
  323. return matchBrackets(type, rtype);
  324. }
  325. function rtypemaybeparam() {
  326. if (content == "<") return cont(typarams);
  327. return pass();
  328. }
  329. function fntype(type) {
  330. if (type == "(") return cont(pushlex("("), commasep(rtype, ")"), poplex, fntype);
  331. if (type == "->") return cont(rtype);
  332. return pass();
  333. }
  334. function pattern(type) {
  335. if (type == "name") {cx.marked = "def"; return cont(patternmaybeop);}
  336. if (type == "atom") return cont(patternmaybeop);
  337. if (type == "op") return cont(pattern);
  338. if (type.match(/[\]\)\};,]/)) return pass();
  339. return matchBrackets(type, pattern);
  340. }
  341. function patternmaybeop(type) {
  342. if (type == "op" && content == ".") return cont();
  343. if (content == "to") {cx.marked = "keyword"; return cont(pattern);}
  344. else return pass();
  345. }
  346. function altbody(type) {
  347. if (type == "{") return cont(pushlex("}", "alt"), altblock1, poplex);
  348. return pass();
  349. }
  350. function altblock1(type) {
  351. if (type == "}") return cont();
  352. if (type == "|") return cont(altblock1);
  353. if (content == "when") {cx.marked = "keyword"; return cont(expression, altblock2);}
  354. if (type.match(/[\]\);,]/)) return cont(altblock1);
  355. return pass(pattern, altblock2);
  356. }
  357. function altblock2(type) {
  358. if (type == "{") return cont(pushlex("}", "alt"), block, poplex, altblock1);
  359. else return pass(altblock1);
  360. }
  361. function macro(type) {
  362. if (type.match(/[\[\(\{]/)) return matchBrackets(type, expression);
  363. return pass();
  364. }
  365. function matchBrackets(type, comb) {
  366. if (type == "[") return cont(pushlex("]"), commasep(comb, "]"), poplex);
  367. if (type == "(") return cont(pushlex(")"), commasep(comb, ")"), poplex);
  368. if (type == "{") return cont(pushlex("}"), commasep(comb, "}"), poplex);
  369. return cont();
  370. }
  371. function parse(state, stream, style) {
  372. var cc = state.cc;
  373. // Communicate our context to the combinators.
  374. // (Less wasteful than consing up a hundred closures on every call.)
  375. cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc;
  376. while (true) {
  377. var combinator = cc.length ? cc.pop() : block;
  378. if (combinator(tcat)) {
  379. while(cc.length && cc[cc.length - 1].lex)
  380. cc.pop()();
  381. return cx.marked || style;
  382. }
  383. }
  384. }
  385. return {
  386. startState: function() {
  387. return {
  388. tokenize: tokenBase,
  389. cc: [],
  390. lexical: {indented: -indentUnit, column: 0, type: "top", align: false},
  391. keywords: valKeywords,
  392. indented: 0
  393. };
  394. },
  395. token: function(stream, state) {
  396. if (stream.sol()) {
  397. if (!state.lexical.hasOwnProperty("align"))
  398. state.lexical.align = false;
  399. state.indented = stream.indentation();
  400. }
  401. if (stream.eatSpace()) return null;
  402. tcat = content = null;
  403. var style = state.tokenize(stream, state);
  404. if (style == "comment") return style;
  405. if (!state.lexical.hasOwnProperty("align"))
  406. state.lexical.align = true;
  407. if (tcat == "prefix") return style;
  408. if (!content) content = stream.current();
  409. return parse(state, stream, style);
  410. },
  411. indent: function(state, textAfter) {
  412. if (state.tokenize != tokenBase) return 0;
  413. var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical,
  414. type = lexical.type, closing = firstChar == type;
  415. if (type == "stat") return lexical.indented + indentUnit;
  416. if (lexical.align) return lexical.column + (closing ? 0 : 1);
  417. return lexical.indented + (closing ? 0 : (lexical.info == "alt" ? altIndentUnit : indentUnit));
  418. },
  419. electricChars: "{}",
  420. blockCommentStart: "/*",
  421. blockCommentEnd: "*/",
  422. lineComment: "//",
  423. fold: "brace"
  424. };
  425. });
  426. CodeMirror.defineMIME("text/x-rustsrc", "rust");
  427. });