r.js 5.5 KB

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