all repos — NoPaste @ 5c12c8a8f515faa64a6cd1d3f04d9aa60561322d

Resurrected - The PussTheCat.org fork of NoPaste

scripts/CodeMirror/mode/r/r.js (view raw)

  1// CodeMirror, copyright (c) by Marijn Haverbeke and others
  2// Distributed under an MIT license: https://codemirror.net/LICENSE
  3
  4(function(mod) {
  5  if (typeof exports == "object" && typeof module == "object") // CommonJS
  6    mod(require("../../lib/codemirror"));
  7  else if (typeof define == "function" && define.amd) // AMD
  8    define(["../../lib/codemirror"], mod);
  9  else // Plain browser env
 10    mod(CodeMirror);
 11})(function(CodeMirror) {
 12"use strict";
 13
 14CodeMirror.registerHelper("wordChars", "r", /[\w.]/);
 15
 16CodeMirror.defineMode("r", function(config) {
 17  function wordObj(words) {
 18    var res = {};
 19    for (var i = 0; i < words.length; ++i) res[words[i]] = true;
 20    return res;
 21  }
 22  var commonAtoms = ["NULL", "NA", "Inf", "NaN", "NA_integer_", "NA_real_", "NA_complex_", "NA_character_", "TRUE", "FALSE"];
 23  var commonBuiltins = ["list", "quote", "bquote", "eval", "return", "call", "parse", "deparse"];
 24  var commonKeywords = ["if", "else", "repeat", "while", "function", "for", "in", "next", "break"];
 25  var commonBlockKeywords = ["if", "else", "repeat", "while", "function", "for"];
 26
 27  CodeMirror.registerHelper("hintWords", "r", commonAtoms.concat(commonBuiltins, commonKeywords));
 28
 29  var atoms = wordObj(commonAtoms);
 30  var builtins = wordObj(commonBuiltins);
 31  var keywords = wordObj(commonKeywords);
 32  var blockkeywords = wordObj(commonBlockKeywords);
 33  var opChars = /[+\-*\/^<>=!&|~$:]/;
 34  var curPunc;
 35
 36  function tokenBase(stream, state) {
 37    curPunc = null;
 38    var ch = stream.next();
 39    if (ch == "#") {
 40      stream.skipToEnd();
 41      return "comment";
 42    } else if (ch == "0" && stream.eat("x")) {
 43      stream.eatWhile(/[\da-f]/i);
 44      return "number";
 45    } else if (ch == "." && stream.eat(/\d/)) {
 46      stream.match(/\d*(?:e[+\-]?\d+)?/);
 47      return "number";
 48    } else if (/\d/.test(ch)) {
 49      stream.match(/\d*(?:\.\d+)?(?:e[+\-]\d+)?L?/);
 50      return "number";
 51    } else if (ch == "'" || ch == '"') {
 52      state.tokenize = tokenString(ch);
 53      return "string";
 54    } else if (ch == "`") {
 55      stream.match(/[^`]+`/);
 56      return "variable-3";
 57    } else if (ch == "." && stream.match(/.[.\d]+/)) {
 58      return "keyword";
 59    } else if (/[\w\.]/.test(ch) && ch != "_") {
 60      stream.eatWhile(/[\w\.]/);
 61      var word = stream.current();
 62      if (atoms.propertyIsEnumerable(word)) return "atom";
 63      if (keywords.propertyIsEnumerable(word)) {
 64        // Block keywords start new blocks, except 'else if', which only starts
 65        // one new block for the 'if', no block for the 'else'.
 66        if (blockkeywords.propertyIsEnumerable(word) &&
 67            !stream.match(/\s*if(\s+|$)/, false))
 68          curPunc = "block";
 69        return "keyword";
 70      }
 71      if (builtins.propertyIsEnumerable(word)) return "builtin";
 72      return "variable";
 73    } else if (ch == "%") {
 74      if (stream.skipTo("%")) stream.next();
 75      return "operator variable-2";
 76    } else if (
 77        (ch == "<" && stream.eat("-")) ||
 78        (ch == "<" && stream.match("<-")) ||
 79        (ch == "-" && stream.match(/>>?/))
 80      ) {
 81      return "operator arrow";
 82    } else if (ch == "=" && state.ctx.argList) {
 83      return "arg-is";
 84    } else if (opChars.test(ch)) {
 85      if (ch == "$") return "operator dollar";
 86      stream.eatWhile(opChars);
 87      return "operator";
 88    } else if (/[\(\){}\[\];]/.test(ch)) {
 89      curPunc = ch;
 90      if (ch == ";") return "semi";
 91      return null;
 92    } else {
 93      return null;
 94    }
 95  }
 96
 97  function tokenString(quote) {
 98    return function(stream, state) {
 99      if (stream.eat("\\")) {
100        var ch = stream.next();
101        if (ch == "x") stream.match(/^[a-f0-9]{2}/i);
102        else if ((ch == "u" || ch == "U") && stream.eat("{") && stream.skipTo("}")) stream.next();
103        else if (ch == "u") stream.match(/^[a-f0-9]{4}/i);
104        else if (ch == "U") stream.match(/^[a-f0-9]{8}/i);
105        else if (/[0-7]/.test(ch)) stream.match(/^[0-7]{1,2}/);
106        return "string-2";
107      } else {
108        var next;
109        while ((next = stream.next()) != null) {
110          if (next == quote) { state.tokenize = tokenBase; break; }
111          if (next == "\\") { stream.backUp(1); break; }
112        }
113        return "string";
114      }
115    };
116  }
117
118  var ALIGN_YES = 1, ALIGN_NO = 2, BRACELESS = 4
119
120  function push(state, type, stream) {
121    state.ctx = {type: type,
122                 indent: state.indent,
123                 flags: 0,
124                 column: stream.column(),
125                 prev: state.ctx};
126  }
127  function setFlag(state, flag) {
128    var ctx = state.ctx
129    state.ctx = {type: ctx.type,
130                 indent: ctx.indent,
131                 flags: ctx.flags | flag,
132                 column: ctx.column,
133                 prev: ctx.prev}
134  }
135  function pop(state) {
136    state.indent = state.ctx.indent;
137    state.ctx = state.ctx.prev;
138  }
139
140  return {
141    startState: function() {
142      return {tokenize: tokenBase,
143              ctx: {type: "top",
144                    indent: -config.indentUnit,
145                    flags: ALIGN_NO},
146              indent: 0,
147              afterIdent: false};
148    },
149
150    token: function(stream, state) {
151      if (stream.sol()) {
152        if ((state.ctx.flags & 3) == 0) state.ctx.flags |= ALIGN_NO
153        if (state.ctx.flags & BRACELESS) pop(state)
154        state.indent = stream.indentation();
155      }
156      if (stream.eatSpace()) return null;
157      var style = state.tokenize(stream, state);
158      if (style != "comment" && (state.ctx.flags & ALIGN_NO) == 0) setFlag(state, ALIGN_YES)
159
160      if ((curPunc == ";" || curPunc == "{" || curPunc == "}") && state.ctx.type == "block") pop(state);
161      if (curPunc == "{") push(state, "}", stream);
162      else if (curPunc == "(") {
163        push(state, ")", stream);
164        if (state.afterIdent) state.ctx.argList = true;
165      }
166      else if (curPunc == "[") push(state, "]", stream);
167      else if (curPunc == "block") push(state, "block", stream);
168      else if (curPunc == state.ctx.type) pop(state);
169      else if (state.ctx.type == "block" && style != "comment") setFlag(state, BRACELESS)
170      state.afterIdent = style == "variable" || style == "keyword";
171      return style;
172    },
173
174    indent: function(state, textAfter) {
175      if (state.tokenize != tokenBase) return 0;
176      var firstChar = textAfter && textAfter.charAt(0), ctx = state.ctx,
177          closing = firstChar == ctx.type;
178      if (ctx.flags & BRACELESS) ctx = ctx.prev
179      if (ctx.type == "block") return ctx.indent + (firstChar == "{" ? 0 : config.indentUnit);
180      else if (ctx.flags & ALIGN_YES) return ctx.column + (closing ? 0 : 1);
181      else return ctx.indent + (closing ? 0 : config.indentUnit);
182    },
183
184    lineComment: "#"
185  };
186});
187
188CodeMirror.defineMIME("text/x-rsrc", "r");
189
190});