Theme Inspinia
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

erlang.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. /*jshint unused:true, eqnull:true, curly:true, bitwise:true */
  4. /*jshint undef:true, latedef:true, trailing:true */
  5. /*global CodeMirror:true */
  6. // erlang mode.
  7. // tokenizer -> token types -> CodeMirror styles
  8. // tokenizer maintains a parse stack
  9. // indenter uses the parse stack
  10. // TODO indenter:
  11. // bit syntax
  12. // old guard/bif/conversion clashes (e.g. "float/1")
  13. // type/spec/opaque
  14. (function(mod) {
  15. if (typeof exports == "object" && typeof module == "object") // CommonJS
  16. mod(require("../../lib/codemirror"));
  17. else if (typeof define == "function" && define.amd) // AMD
  18. define(["../../lib/codemirror"], mod);
  19. else // Plain browser env
  20. mod(CodeMirror);
  21. })(function(CodeMirror) {
  22. "use strict";
  23. CodeMirror.defineMIME("text/x-erlang", "erlang");
  24. CodeMirror.defineMode("erlang", function(cmCfg) {
  25. "use strict";
  26. /////////////////////////////////////////////////////////////////////////////
  27. // constants
  28. var typeWords = [
  29. "-type", "-spec", "-export_type", "-opaque"];
  30. var keywordWords = [
  31. "after","begin","catch","case","cond","end","fun","if",
  32. "let","of","query","receive","try","when"];
  33. var separatorRE = /[\->,;]/;
  34. var separatorWords = [
  35. "->",";",","];
  36. var operatorAtomWords = [
  37. "and","andalso","band","bnot","bor","bsl","bsr","bxor",
  38. "div","not","or","orelse","rem","xor"];
  39. var operatorSymbolRE = /[\+\-\*\/<>=\|:!]/;
  40. var operatorSymbolWords = [
  41. "=","+","-","*","/",">",">=","<","=<","=:=","==","=/=","/=","||","<-","!"];
  42. var openParenRE = /[<\(\[\{]/;
  43. var openParenWords = [
  44. "<<","(","[","{"];
  45. var closeParenRE = /[>\)\]\}]/;
  46. var closeParenWords = [
  47. "}","]",")",">>"];
  48. var guardWords = [
  49. "is_atom","is_binary","is_bitstring","is_boolean","is_float",
  50. "is_function","is_integer","is_list","is_number","is_pid",
  51. "is_port","is_record","is_reference","is_tuple",
  52. "atom","binary","bitstring","boolean","function","integer","list",
  53. "number","pid","port","record","reference","tuple"];
  54. var bifWords = [
  55. "abs","adler32","adler32_combine","alive","apply","atom_to_binary",
  56. "atom_to_list","binary_to_atom","binary_to_existing_atom",
  57. "binary_to_list","binary_to_term","bit_size","bitstring_to_list",
  58. "byte_size","check_process_code","contact_binary","crc32",
  59. "crc32_combine","date","decode_packet","delete_module",
  60. "disconnect_node","element","erase","exit","float","float_to_list",
  61. "garbage_collect","get","get_keys","group_leader","halt","hd",
  62. "integer_to_list","internal_bif","iolist_size","iolist_to_binary",
  63. "is_alive","is_atom","is_binary","is_bitstring","is_boolean",
  64. "is_float","is_function","is_integer","is_list","is_number","is_pid",
  65. "is_port","is_process_alive","is_record","is_reference","is_tuple",
  66. "length","link","list_to_atom","list_to_binary","list_to_bitstring",
  67. "list_to_existing_atom","list_to_float","list_to_integer",
  68. "list_to_pid","list_to_tuple","load_module","make_ref","module_loaded",
  69. "monitor_node","node","node_link","node_unlink","nodes","notalive",
  70. "now","open_port","pid_to_list","port_close","port_command",
  71. "port_connect","port_control","pre_loaded","process_flag",
  72. "process_info","processes","purge_module","put","register",
  73. "registered","round","self","setelement","size","spawn","spawn_link",
  74. "spawn_monitor","spawn_opt","split_binary","statistics",
  75. "term_to_binary","time","throw","tl","trunc","tuple_size",
  76. "tuple_to_list","unlink","unregister","whereis"];
  77. // upper case: [A-Z] [Ø-Þ] [À-Ö]
  78. // lower case: [a-z] [ß-ö] [ø-ÿ]
  79. var anumRE = /[\w@Ø-ÞÀ-Öß-öø-ÿ]/;
  80. var escapesRE =
  81. /[0-7]{1,3}|[bdefnrstv\\"']|\^[a-zA-Z]|x[0-9a-zA-Z]{2}|x{[0-9a-zA-Z]+}/;
  82. /////////////////////////////////////////////////////////////////////////////
  83. // tokenizer
  84. function tokenizer(stream,state) {
  85. // in multi-line string
  86. if (state.in_string) {
  87. state.in_string = (!doubleQuote(stream));
  88. return rval(state,stream,"string");
  89. }
  90. // in multi-line atom
  91. if (state.in_atom) {
  92. state.in_atom = (!singleQuote(stream));
  93. return rval(state,stream,"atom");
  94. }
  95. // whitespace
  96. if (stream.eatSpace()) {
  97. return rval(state,stream,"whitespace");
  98. }
  99. // attributes and type specs
  100. if (!peekToken(state) &&
  101. stream.match(/-\s*[a-zß-öø-ÿ][\wØ-ÞÀ-Öß-öø-ÿ]*/)) {
  102. if (is_member(stream.current(),typeWords)) {
  103. return rval(state,stream,"type");
  104. }else{
  105. return rval(state,stream,"attribute");
  106. }
  107. }
  108. var ch = stream.next();
  109. // comment
  110. if (ch == '%') {
  111. stream.skipToEnd();
  112. return rval(state,stream,"comment");
  113. }
  114. // colon
  115. if (ch == ":") {
  116. return rval(state,stream,"colon");
  117. }
  118. // macro
  119. if (ch == '?') {
  120. stream.eatSpace();
  121. stream.eatWhile(anumRE);
  122. return rval(state,stream,"macro");
  123. }
  124. // record
  125. if (ch == "#") {
  126. stream.eatSpace();
  127. stream.eatWhile(anumRE);
  128. return rval(state,stream,"record");
  129. }
  130. // dollar escape
  131. if (ch == "$") {
  132. if (stream.next() == "\\" && !stream.match(escapesRE)) {
  133. return rval(state,stream,"error");
  134. }
  135. return rval(state,stream,"number");
  136. }
  137. // dot
  138. if (ch == ".") {
  139. return rval(state,stream,"dot");
  140. }
  141. // quoted atom
  142. if (ch == '\'') {
  143. if (!(state.in_atom = (!singleQuote(stream)))) {
  144. if (stream.match(/\s*\/\s*[0-9]/,false)) {
  145. stream.match(/\s*\/\s*[0-9]/,true);
  146. return rval(state,stream,"fun"); // 'f'/0 style fun
  147. }
  148. if (stream.match(/\s*\(/,false) || stream.match(/\s*:/,false)) {
  149. return rval(state,stream,"function");
  150. }
  151. }
  152. return rval(state,stream,"atom");
  153. }
  154. // string
  155. if (ch == '"') {
  156. state.in_string = (!doubleQuote(stream));
  157. return rval(state,stream,"string");
  158. }
  159. // variable
  160. if (/[A-Z_Ø-ÞÀ-Ö]/.test(ch)) {
  161. stream.eatWhile(anumRE);
  162. return rval(state,stream,"variable");
  163. }
  164. // atom/keyword/BIF/function
  165. if (/[a-z_ß-öø-ÿ]/.test(ch)) {
  166. stream.eatWhile(anumRE);
  167. if (stream.match(/\s*\/\s*[0-9]/,false)) {
  168. stream.match(/\s*\/\s*[0-9]/,true);
  169. return rval(state,stream,"fun"); // f/0 style fun
  170. }
  171. var w = stream.current();
  172. if (is_member(w,keywordWords)) {
  173. return rval(state,stream,"keyword");
  174. }else if (is_member(w,operatorAtomWords)) {
  175. return rval(state,stream,"operator");
  176. }else if (stream.match(/\s*\(/,false)) {
  177. // 'put' and 'erlang:put' are bifs, 'foo:put' is not
  178. if (is_member(w,bifWords) &&
  179. ((peekToken(state).token != ":") ||
  180. (peekToken(state,2).token == "erlang"))) {
  181. return rval(state,stream,"builtin");
  182. }else if (is_member(w,guardWords)) {
  183. return rval(state,stream,"guard");
  184. }else{
  185. return rval(state,stream,"function");
  186. }
  187. }else if (is_member(w,operatorAtomWords)) {
  188. return rval(state,stream,"operator");
  189. }else if (lookahead(stream) == ":") {
  190. if (w == "erlang") {
  191. return rval(state,stream,"builtin");
  192. } else {
  193. return rval(state,stream,"function");
  194. }
  195. }else if (is_member(w,["true","false"])) {
  196. return rval(state,stream,"boolean");
  197. }else if (is_member(w,["true","false"])) {
  198. return rval(state,stream,"boolean");
  199. }else{
  200. return rval(state,stream,"atom");
  201. }
  202. }
  203. // number
  204. var digitRE = /[0-9]/;
  205. var radixRE = /[0-9a-zA-Z]/; // 36#zZ style int
  206. if (digitRE.test(ch)) {
  207. stream.eatWhile(digitRE);
  208. if (stream.eat('#')) { // 36#aZ style integer
  209. if (!stream.eatWhile(radixRE)) {
  210. stream.backUp(1); //"36#" - syntax error
  211. }
  212. } else if (stream.eat('.')) { // float
  213. if (!stream.eatWhile(digitRE)) {
  214. stream.backUp(1); // "3." - probably end of function
  215. } else {
  216. if (stream.eat(/[eE]/)) { // float with exponent
  217. if (stream.eat(/[-+]/)) {
  218. if (!stream.eatWhile(digitRE)) {
  219. stream.backUp(2); // "2e-" - syntax error
  220. }
  221. } else {
  222. if (!stream.eatWhile(digitRE)) {
  223. stream.backUp(1); // "2e" - syntax error
  224. }
  225. }
  226. }
  227. }
  228. }
  229. return rval(state,stream,"number"); // normal integer
  230. }
  231. // open parens
  232. if (nongreedy(stream,openParenRE,openParenWords)) {
  233. return rval(state,stream,"open_paren");
  234. }
  235. // close parens
  236. if (nongreedy(stream,closeParenRE,closeParenWords)) {
  237. return rval(state,stream,"close_paren");
  238. }
  239. // separators
  240. if (greedy(stream,separatorRE,separatorWords)) {
  241. return rval(state,stream,"separator");
  242. }
  243. // operators
  244. if (greedy(stream,operatorSymbolRE,operatorSymbolWords)) {
  245. return rval(state,stream,"operator");
  246. }
  247. return rval(state,stream,null);
  248. }
  249. /////////////////////////////////////////////////////////////////////////////
  250. // utilities
  251. function nongreedy(stream,re,words) {
  252. if (stream.current().length == 1 && re.test(stream.current())) {
  253. stream.backUp(1);
  254. while (re.test(stream.peek())) {
  255. stream.next();
  256. if (is_member(stream.current(),words)) {
  257. return true;
  258. }
  259. }
  260. stream.backUp(stream.current().length-1);
  261. }
  262. return false;
  263. }
  264. function greedy(stream,re,words) {
  265. if (stream.current().length == 1 && re.test(stream.current())) {
  266. while (re.test(stream.peek())) {
  267. stream.next();
  268. }
  269. while (0 < stream.current().length) {
  270. if (is_member(stream.current(),words)) {
  271. return true;
  272. }else{
  273. stream.backUp(1);
  274. }
  275. }
  276. stream.next();
  277. }
  278. return false;
  279. }
  280. function doubleQuote(stream) {
  281. return quote(stream, '"', '\\');
  282. }
  283. function singleQuote(stream) {
  284. return quote(stream,'\'','\\');
  285. }
  286. function quote(stream,quoteChar,escapeChar) {
  287. while (!stream.eol()) {
  288. var ch = stream.next();
  289. if (ch == quoteChar) {
  290. return true;
  291. }else if (ch == escapeChar) {
  292. stream.next();
  293. }
  294. }
  295. return false;
  296. }
  297. function lookahead(stream) {
  298. var m = stream.match(/([\n\s]+|%[^\n]*\n)*(.)/,false);
  299. return m ? m.pop() : "";
  300. }
  301. function is_member(element,list) {
  302. return (-1 < list.indexOf(element));
  303. }
  304. function rval(state,stream,type) {
  305. // parse stack
  306. pushToken(state,realToken(type,stream));
  307. // map erlang token type to CodeMirror style class
  308. // erlang -> CodeMirror tag
  309. switch (type) {
  310. case "atom": return "atom";
  311. case "attribute": return "attribute";
  312. case "boolean": return "atom";
  313. case "builtin": return "builtin";
  314. case "close_paren": return null;
  315. case "colon": return null;
  316. case "comment": return "comment";
  317. case "dot": return null;
  318. case "error": return "error";
  319. case "fun": return "meta";
  320. case "function": return "tag";
  321. case "guard": return "property";
  322. case "keyword": return "keyword";
  323. case "macro": return "variable-2";
  324. case "number": return "number";
  325. case "open_paren": return null;
  326. case "operator": return "operator";
  327. case "record": return "bracket";
  328. case "separator": return null;
  329. case "string": return "string";
  330. case "type": return "def";
  331. case "variable": return "variable";
  332. default: return null;
  333. }
  334. }
  335. function aToken(tok,col,ind,typ) {
  336. return {token: tok,
  337. column: col,
  338. indent: ind,
  339. type: typ};
  340. }
  341. function realToken(type,stream) {
  342. return aToken(stream.current(),
  343. stream.column(),
  344. stream.indentation(),
  345. type);
  346. }
  347. function fakeToken(type) {
  348. return aToken(type,0,0,type);
  349. }
  350. function peekToken(state,depth) {
  351. var len = state.tokenStack.length;
  352. var dep = (depth ? depth : 1);
  353. if (len < dep) {
  354. return false;
  355. }else{
  356. return state.tokenStack[len-dep];
  357. }
  358. }
  359. function pushToken(state,token) {
  360. if (!(token.type == "comment" || token.type == "whitespace")) {
  361. state.tokenStack = maybe_drop_pre(state.tokenStack,token);
  362. state.tokenStack = maybe_drop_post(state.tokenStack);
  363. }
  364. }
  365. function maybe_drop_pre(s,token) {
  366. var last = s.length-1;
  367. if (0 < last && s[last].type === "record" && token.type === "dot") {
  368. s.pop();
  369. }else if (0 < last && s[last].type === "group") {
  370. s.pop();
  371. s.push(token);
  372. }else{
  373. s.push(token);
  374. }
  375. return s;
  376. }
  377. function maybe_drop_post(s) {
  378. var last = s.length-1;
  379. if (s[last].type === "dot") {
  380. return [];
  381. }
  382. if (s[last].type === "fun" && s[last-1].token === "fun") {
  383. return s.slice(0,last-1);
  384. }
  385. switch (s[s.length-1].token) {
  386. case "}": return d(s,{g:["{"]});
  387. case "]": return d(s,{i:["["]});
  388. case ")": return d(s,{i:["("]});
  389. case ">>": return d(s,{i:["<<"]});
  390. case "end": return d(s,{i:["begin","case","fun","if","receive","try"]});
  391. case ",": return d(s,{e:["begin","try","when","->",
  392. ",","(","[","{","<<"]});
  393. case "->": return d(s,{r:["when"],
  394. m:["try","if","case","receive"]});
  395. case ";": return d(s,{E:["case","fun","if","receive","try","when"]});
  396. case "catch":return d(s,{e:["try"]});
  397. case "of": return d(s,{e:["case"]});
  398. case "after":return d(s,{e:["receive","try"]});
  399. default: return s;
  400. }
  401. }
  402. function d(stack,tt) {
  403. // stack is a stack of Token objects.
  404. // tt is an object; {type:tokens}
  405. // type is a char, tokens is a list of token strings.
  406. // The function returns (possibly truncated) stack.
  407. // It will descend the stack, looking for a Token such that Token.token
  408. // is a member of tokens. If it does not find that, it will normally (but
  409. // see "E" below) return stack. If it does find a match, it will remove
  410. // all the Tokens between the top and the matched Token.
  411. // If type is "m", that is all it does.
  412. // If type is "i", it will also remove the matched Token and the top Token.
  413. // If type is "g", like "i", but add a fake "group" token at the top.
  414. // If type is "r", it will remove the matched Token, but not the top Token.
  415. // If type is "e", it will keep the matched Token but not the top Token.
  416. // If type is "E", it behaves as for type "e", except if there is no match,
  417. // in which case it will return an empty stack.
  418. for (var type in tt) {
  419. var len = stack.length-1;
  420. var tokens = tt[type];
  421. for (var i = len-1; -1 < i ; i--) {
  422. if (is_member(stack[i].token,tokens)) {
  423. var ss = stack.slice(0,i);
  424. switch (type) {
  425. case "m": return ss.concat(stack[i]).concat(stack[len]);
  426. case "r": return ss.concat(stack[len]);
  427. case "i": return ss;
  428. case "g": return ss.concat(fakeToken("group"));
  429. case "E": return ss.concat(stack[i]);
  430. case "e": return ss.concat(stack[i]);
  431. }
  432. }
  433. }
  434. }
  435. return (type == "E" ? [] : stack);
  436. }
  437. /////////////////////////////////////////////////////////////////////////////
  438. // indenter
  439. function indenter(state,textAfter) {
  440. var t;
  441. var unit = cmCfg.indentUnit;
  442. var wordAfter = wordafter(textAfter);
  443. var currT = peekToken(state,1);
  444. var prevT = peekToken(state,2);
  445. if (state.in_string || state.in_atom) {
  446. return CodeMirror.Pass;
  447. }else if (!prevT) {
  448. return 0;
  449. }else if (currT.token == "when") {
  450. return currT.column+unit;
  451. }else if (wordAfter === "when" && prevT.type === "function") {
  452. return prevT.indent+unit;
  453. }else if (wordAfter === "(" && currT.token === "fun") {
  454. return currT.column+3;
  455. }else if (wordAfter === "catch" && (t = getToken(state,["try"]))) {
  456. return t.column;
  457. }else if (is_member(wordAfter,["end","after","of"])) {
  458. t = getToken(state,["begin","case","fun","if","receive","try"]);
  459. return t ? t.column : CodeMirror.Pass;
  460. }else if (is_member(wordAfter,closeParenWords)) {
  461. t = getToken(state,openParenWords);
  462. return t ? t.column : CodeMirror.Pass;
  463. }else if (is_member(currT.token,[",","|","||"]) ||
  464. is_member(wordAfter,[",","|","||"])) {
  465. t = postcommaToken(state);
  466. return t ? t.column+t.token.length : unit;
  467. }else if (currT.token == "->") {
  468. if (is_member(prevT.token, ["receive","case","if","try"])) {
  469. return prevT.column+unit+unit;
  470. }else{
  471. return prevT.column+unit;
  472. }
  473. }else if (is_member(currT.token,openParenWords)) {
  474. return currT.column+currT.token.length;
  475. }else{
  476. t = defaultToken(state);
  477. return truthy(t) ? t.column+unit : 0;
  478. }
  479. }
  480. function wordafter(str) {
  481. var m = str.match(/,|[a-z]+|\}|\]|\)|>>|\|+|\(/);
  482. return truthy(m) && (m.index === 0) ? m[0] : "";
  483. }
  484. function postcommaToken(state) {
  485. var objs = state.tokenStack.slice(0,-1);
  486. var i = getTokenIndex(objs,"type",["open_paren"]);
  487. return truthy(objs[i]) ? objs[i] : false;
  488. }
  489. function defaultToken(state) {
  490. var objs = state.tokenStack;
  491. var stop = getTokenIndex(objs,"type",["open_paren","separator","keyword"]);
  492. var oper = getTokenIndex(objs,"type",["operator"]);
  493. if (truthy(stop) && truthy(oper) && stop < oper) {
  494. return objs[stop+1];
  495. } else if (truthy(stop)) {
  496. return objs[stop];
  497. } else {
  498. return false;
  499. }
  500. }
  501. function getToken(state,tokens) {
  502. var objs = state.tokenStack;
  503. var i = getTokenIndex(objs,"token",tokens);
  504. return truthy(objs[i]) ? objs[i] : false;
  505. }
  506. function getTokenIndex(objs,propname,propvals) {
  507. for (var i = objs.length-1; -1 < i ; i--) {
  508. if (is_member(objs[i][propname],propvals)) {
  509. return i;
  510. }
  511. }
  512. return false;
  513. }
  514. function truthy(x) {
  515. return (x !== false) && (x != null);
  516. }
  517. /////////////////////////////////////////////////////////////////////////////
  518. // this object defines the mode
  519. return {
  520. startState:
  521. function() {
  522. return {tokenStack: [],
  523. in_string: false,
  524. in_atom: false};
  525. },
  526. token:
  527. function(stream, state) {
  528. return tokenizer(stream, state);
  529. },
  530. indent:
  531. function(state, textAfter) {
  532. return indenter(state,textAfter);
  533. },
  534. lineComment: "%"
  535. };
  536. });
  537. });