r.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://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.registerHelper("wordChars", "r", /[\w.]/);
  13. CodeMirror.defineMode("r", function(config) {
  14. function wordObj(words) {
  15. var res = {};
  16. for (var i = 0; i < words.length; ++i) res[words[i]] = true;
  17. return res;
  18. }
  19. var commonAtoms = ["NULL", "NA", "Inf", "NaN", "NA_integer_", "NA_real_", "NA_complex_", "NA_character_", "TRUE", "FALSE"];
  20. var commonBuiltins = ["list", "quote", "bquote", "eval", "return", "call", "parse", "deparse"];
  21. var commonKeywords = ["if", "else", "repeat", "while", "function", "for", "in", "next", "break"];
  22. var commonBlockKeywords = ["if", "else", "repeat", "while", "function", "for"];
  23. CodeMirror.registerHelper("hintWords", "r", commonAtoms.concat(commonBuiltins, commonKeywords));
  24. var atoms = wordObj(commonAtoms);
  25. var builtins = wordObj(commonBuiltins);
  26. var keywords = wordObj(commonKeywords);
  27. var blockkeywords = wordObj(commonBlockKeywords);
  28. var opChars = /[+\-*\/^<>=!&|~$:]/;
  29. var curPunc;
  30. function tokenBase(stream, state) {
  31. curPunc = null;
  32. var ch = stream.next();
  33. if (ch == "#") {
  34. stream.skipToEnd();
  35. return "comment";
  36. } else if (ch == "0" && stream.eat("x")) {
  37. stream.eatWhile(/[\da-f]/i);
  38. return "number";
  39. } else if (ch == "." && stream.eat(/\d/)) {
  40. stream.match(/\d*(?:e[+\-]?\d+)?/);
  41. return "number";
  42. } else if (/\d/.test(ch)) {
  43. stream.match(/\d*(?:\.\d+)?(?:e[+\-]\d+)?L?/);
  44. return "number";
  45. } else if (ch == "'" || ch == '"') {
  46. state.tokenize = tokenString(ch);
  47. return "string";
  48. } else if (ch == "`") {
  49. stream.match(/[^`]+`/);
  50. return "variable-3";
  51. } else if (ch == "." && stream.match(/.(?:[.]|\d+)/)) {
  52. return "keyword";
  53. } else if (/[a-zA-Z\.]/.test(ch)) {
  54. stream.eatWhile(/[\w\.]/);
  55. var word = stream.current();
  56. if (atoms.propertyIsEnumerable(word)) return "atom";
  57. if (keywords.propertyIsEnumerable(word)) {
  58. // Block keywords start new blocks, except 'else if', which only starts
  59. // one new block for the 'if', no block for the 'else'.
  60. if (blockkeywords.propertyIsEnumerable(word) &&
  61. !stream.match(/\s*if(\s+|$)/, false))
  62. curPunc = "block";
  63. return "keyword";
  64. }
  65. if (builtins.propertyIsEnumerable(word)) return "builtin";
  66. return "variable";
  67. } else if (ch == "%") {
  68. if (stream.skipTo("%")) stream.next();
  69. return "operator variable-2";
  70. } else if (
  71. (ch == "<" && stream.eat("-")) ||
  72. (ch == "<" && stream.match("<-")) ||
  73. (ch == "-" && stream.match(/>>?/))
  74. ) {
  75. return "operator arrow";
  76. } else if (ch == "=" && state.ctx.argList) {
  77. return "arg-is";
  78. } else if (opChars.test(ch)) {
  79. if (ch == "$") return "operator dollar";
  80. stream.eatWhile(opChars);
  81. return "operator";
  82. } else if (/[\(\){}\[\];]/.test(ch)) {
  83. curPunc = ch;
  84. if (ch == ";") return "semi";
  85. return null;
  86. } else {
  87. return null;
  88. }
  89. }
  90. function tokenString(quote) {
  91. return function(stream, state) {
  92. if (stream.eat("\\")) {
  93. var ch = stream.next();
  94. if (ch == "x") stream.match(/^[a-f0-9]{2}/i);
  95. else if ((ch == "u" || ch == "U") && stream.eat("{") && stream.skipTo("}")) stream.next();
  96. else if (ch == "u") stream.match(/^[a-f0-9]{4}/i);
  97. else if (ch == "U") stream.match(/^[a-f0-9]{8}/i);
  98. else if (/[0-7]/.test(ch)) stream.match(/^[0-7]{1,2}/);
  99. return "string-2";
  100. } else {
  101. var next;
  102. while ((next = stream.next()) != null) {
  103. if (next == quote) { state.tokenize = tokenBase; break; }
  104. if (next == "\\") { stream.backUp(1); break; }
  105. }
  106. return "string";
  107. }
  108. };
  109. }
  110. var ALIGN_YES = 1, ALIGN_NO = 2, BRACELESS = 4
  111. function push(state, type, stream) {
  112. state.ctx = {type: type,
  113. indent: state.indent,
  114. flags: 0,
  115. column: stream.column(),
  116. prev: state.ctx};
  117. }
  118. function setFlag(state, flag) {
  119. var ctx = state.ctx
  120. state.ctx = {type: ctx.type,
  121. indent: ctx.indent,
  122. flags: ctx.flags | flag,
  123. column: ctx.column,
  124. prev: ctx.prev}
  125. }
  126. function pop(state) {
  127. state.indent = state.ctx.indent;
  128. state.ctx = state.ctx.prev;
  129. }
  130. return {
  131. startState: function() {
  132. return {tokenize: tokenBase,
  133. ctx: {type: "top",
  134. indent: -config.indentUnit,
  135. flags: ALIGN_NO},
  136. indent: 0,
  137. afterIdent: false};
  138. },
  139. token: function(stream, state) {
  140. if (stream.sol()) {
  141. if ((state.ctx.flags & 3) == 0) state.ctx.flags |= ALIGN_NO
  142. if (state.ctx.flags & BRACELESS) pop(state)
  143. state.indent = stream.indentation();
  144. }
  145. if (stream.eatSpace()) return null;
  146. var style = state.tokenize(stream, state);
  147. if (style != "comment" && (state.ctx.flags & ALIGN_NO) == 0) setFlag(state, ALIGN_YES)
  148. if ((curPunc == ";" || curPunc == "{" || curPunc == "}") && state.ctx.type == "block") pop(state);
  149. if (curPunc == "{") push(state, "}", stream);
  150. else if (curPunc == "(") {
  151. push(state, ")", stream);
  152. if (state.afterIdent) state.ctx.argList = true;
  153. }
  154. else if (curPunc == "[") push(state, "]", stream);
  155. else if (curPunc == "block") push(state, "block", stream);
  156. else if (curPunc == state.ctx.type) pop(state);
  157. else if (state.ctx.type == "block" && style != "comment") setFlag(state, BRACELESS)
  158. state.afterIdent = style == "variable" || style == "keyword";
  159. return style;
  160. },
  161. indent: function(state, textAfter) {
  162. if (state.tokenize != tokenBase) return 0;
  163. var firstChar = textAfter && textAfter.charAt(0), ctx = state.ctx,
  164. closing = firstChar == ctx.type;
  165. if (ctx.flags & BRACELESS) ctx = ctx.prev
  166. if (ctx.type == "block") return ctx.indent + (firstChar == "{" ? 0 : config.indentUnit);
  167. else if (ctx.flags & ALIGN_YES) return ctx.column + (closing ? 0 : 1);
  168. else return ctx.indent + (closing ? 0 : config.indentUnit);
  169. },
  170. lineComment: "#"
  171. };
  172. });
  173. CodeMirror.defineMIME("text/x-rsrc", "r");
  174. });