rust.js 15 KB

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