rust.js 16 KB

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