1
0

r.js 4.8 KB

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