r.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 == "." && stream.match(/.[.\d]+/)) {
  44. return "keyword";
  45. } else if (/[\w\.]/.test(ch) && ch != "_") {
  46. stream.eatWhile(/[\w\.]/);
  47. var word = stream.current();
  48. if (atoms.propertyIsEnumerable(word)) return "atom";
  49. if (keywords.propertyIsEnumerable(word)) {
  50. // Block keywords start new blocks, except 'else if', which only starts
  51. // one new block for the 'if', no block for the 'else'.
  52. if (blockkeywords.propertyIsEnumerable(word) &&
  53. !stream.match(/\s*if(\s+|$)/, false))
  54. curPunc = "block";
  55. return "keyword";
  56. }
  57. if (builtins.propertyIsEnumerable(word)) return "builtin";
  58. return "variable";
  59. } else if (ch == "%") {
  60. if (stream.skipTo("%")) stream.next();
  61. return "variable-2";
  62. } else if (ch == "<" && stream.eat("-")) {
  63. return "arrow";
  64. } else if (ch == "=" && state.ctx.argList) {
  65. return "arg-is";
  66. } else if (opChars.test(ch)) {
  67. if (ch == "$") return "dollar";
  68. stream.eatWhile(opChars);
  69. return "operator";
  70. } else if (/[\(\){}\[\];]/.test(ch)) {
  71. curPunc = ch;
  72. if (ch == ";") return "semi";
  73. return null;
  74. } else {
  75. return null;
  76. }
  77. }
  78. function tokenString(quote) {
  79. return function(stream, state) {
  80. if (stream.eat("\\")) {
  81. var ch = stream.next();
  82. if (ch == "x") stream.match(/^[a-f0-9]{2}/i);
  83. else if ((ch == "u" || ch == "U") && stream.eat("{") && stream.skipTo("}")) stream.next();
  84. else if (ch == "u") stream.match(/^[a-f0-9]{4}/i);
  85. else if (ch == "U") stream.match(/^[a-f0-9]{8}/i);
  86. else if (/[0-7]/.test(ch)) stream.match(/^[0-7]{1,2}/);
  87. return "string-2";
  88. } else {
  89. var next;
  90. while ((next = stream.next()) != null) {
  91. if (next == quote) { state.tokenize = tokenBase; break; }
  92. if (next == "\\") { stream.backUp(1); break; }
  93. }
  94. return "string";
  95. }
  96. };
  97. }
  98. function push(state, type, stream) {
  99. state.ctx = {type: type,
  100. indent: state.indent,
  101. align: null,
  102. column: stream.column(),
  103. prev: state.ctx};
  104. }
  105. function pop(state) {
  106. state.indent = state.ctx.indent;
  107. state.ctx = state.ctx.prev;
  108. }
  109. return {
  110. startState: function() {
  111. return {tokenize: tokenBase,
  112. ctx: {type: "top",
  113. indent: -config.indentUnit,
  114. align: false},
  115. indent: 0,
  116. afterIdent: false};
  117. },
  118. token: function(stream, state) {
  119. if (stream.sol()) {
  120. if (state.ctx.align == null) state.ctx.align = false;
  121. state.indent = stream.indentation();
  122. }
  123. if (stream.eatSpace()) return null;
  124. var style = state.tokenize(stream, state);
  125. if (style != "comment" && state.ctx.align == null) state.ctx.align = true;
  126. var ctype = state.ctx.type;
  127. if ((curPunc == ";" || curPunc == "{" || curPunc == "}") && ctype == "block") pop(state);
  128. if (curPunc == "{") push(state, "}", stream);
  129. else if (curPunc == "(") {
  130. push(state, ")", stream);
  131. if (state.afterIdent) state.ctx.argList = true;
  132. }
  133. else if (curPunc == "[") push(state, "]", stream);
  134. else if (curPunc == "block") push(state, "block", stream);
  135. else if (curPunc == ctype) pop(state);
  136. state.afterIdent = style == "variable" || style == "keyword";
  137. return style;
  138. },
  139. indent: function(state, textAfter) {
  140. if (state.tokenize != tokenBase) return 0;
  141. var firstChar = textAfter && textAfter.charAt(0), ctx = state.ctx,
  142. closing = firstChar == ctx.type;
  143. if (ctx.type == "block") return ctx.indent + (firstChar == "{" ? 0 : config.indentUnit);
  144. else if (ctx.align) return ctx.column + (closing ? 0 : 1);
  145. else return ctx.indent + (closing ? 0 : config.indentUnit);
  146. },
  147. lineComment: "#"
  148. };
  149. });
  150. CodeMirror.defineMIME("text/x-rsrc", "r");
  151. });