r.js 5.8 KB

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