r.js 5.3 KB

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