r.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. var ALIGN_YES = 1, ALIGN_NO = 2, BRACELESS = 4
  106. function push(state, type, stream) {
  107. state.ctx = {type: type,
  108. indent: state.indent,
  109. flags: 0,
  110. column: stream.column(),
  111. prev: state.ctx};
  112. }
  113. function setFlag(state, flag) {
  114. var ctx = state.ctx
  115. state.ctx = {type: ctx.type,
  116. indent: ctx.indent,
  117. flags: ctx.flags | flag,
  118. column: ctx.column,
  119. prev: ctx.prev}
  120. }
  121. function pop(state) {
  122. state.indent = state.ctx.indent;
  123. state.ctx = state.ctx.prev;
  124. }
  125. return {
  126. startState: function() {
  127. return {tokenize: tokenBase,
  128. ctx: {type: "top",
  129. indent: -config.indentUnit,
  130. flags: ALIGN_NO},
  131. indent: 0,
  132. afterIdent: false};
  133. },
  134. token: function(stream, state) {
  135. if (stream.sol()) {
  136. if ((state.ctx.flags & 3) == 0) state.ctx.flags |= ALIGN_NO
  137. if (state.ctx.flags & BRACELESS) pop(state)
  138. state.indent = stream.indentation();
  139. }
  140. if (stream.eatSpace()) return null;
  141. var style = state.tokenize(stream, state);
  142. if (style != "comment" && (state.ctx.flags & ALIGN_NO) == 0) setFlag(state, ALIGN_YES)
  143. if ((curPunc == ";" || curPunc == "{" || curPunc == "}") && state.ctx.type == "block") pop(state);
  144. if (curPunc == "{") push(state, "}", stream);
  145. else if (curPunc == "(") {
  146. push(state, ")", stream);
  147. if (state.afterIdent) state.ctx.argList = true;
  148. }
  149. else if (curPunc == "[") push(state, "]", stream);
  150. else if (curPunc == "block") push(state, "block", stream);
  151. else if (curPunc == state.ctx.type) pop(state);
  152. else if (state.ctx.type == "block" && style != "comment") setFlag(state, BRACELESS)
  153. state.afterIdent = style == "variable" || style == "keyword";
  154. return style;
  155. },
  156. indent: function(state, textAfter) {
  157. if (state.tokenize != tokenBase) return 0;
  158. var firstChar = textAfter && textAfter.charAt(0), ctx = state.ctx,
  159. closing = firstChar == ctx.type;
  160. if (ctx.flags & BRACELESS) ctx = ctx.prev
  161. if (ctx.type == "block") return ctx.indent + (firstChar == "{" ? 0 : config.indentUnit);
  162. else if (ctx.flags & ALIGN_YES) return ctx.column + (closing ? 0 : 1);
  163. else return ctx.indent + (closing ? 0 : config.indentUnit);
  164. },
  165. lineComment: "#"
  166. };
  167. });
  168. CodeMirror.defineMIME("text/x-rsrc", "r");
  169. });