function wordObj(e) {
  for (var g = {}, _ = 0; _ < e.length; ++_) g[e[_]] = true;
  return g;
}
var commonAtoms = [
  "NULL",
  "NA",
  "Inf",
  "NaN",
  "NA_integer_",
  "NA_real_",
  "NA_complex_",
  "NA_character_",
  "TRUE",
  "FALSE"
], commonBuiltins = [
  "list",
  "quote",
  "bquote",
  "eval",
  "return",
  "call",
  "parse",
  "deparse"
], commonKeywords = [
  "if",
  "else",
  "repeat",
  "while",
  "function",
  "for",
  "in",
  "next",
  "break"
], commonBlockKeywords = [
  "if",
  "else",
  "repeat",
  "while",
  "function",
  "for"
], atoms = wordObj(commonAtoms), builtins = wordObj(commonBuiltins), keywords = wordObj(commonKeywords), blockkeywords = wordObj(commonBlockKeywords), opChars = /[+\-*\/^<>=!&|~$:]/, curPunc;
function tokenBase(e, g) {
  curPunc = null;
  var _ = e.next();
  if (_ == "#") return e.skipToEnd(), "comment";
  if (_ == "0" && e.eat("x")) return e.eatWhile(/[\da-f]/i), "number";
  if (_ == "." && e.eat(/\d/)) return e.match(/\d*(?:e[+\-]?\d+)?/), "number";
  if (/\d/.test(_)) return e.match(/\d*(?:\.\d+)?(?:e[+\-]\d+)?L?/), "number";
  if (_ == "'" || _ == '"') return g.tokenize = tokenString(_), "string";
  if (_ == "`") return e.match(/[^`]+`/), "string.special";
  if (_ == "." && e.match(/.(?:[.]|\d+)/)) return "keyword";
  if (/[a-zA-Z\.]/.test(_)) {
    e.eatWhile(/[\w\.]/);
    var v = e.current();
    return atoms.propertyIsEnumerable(v) ? "atom" : keywords.propertyIsEnumerable(v) ? (blockkeywords.propertyIsEnumerable(v) && !e.match(/\s*if(\s+|$)/, false) && (curPunc = "block"), "keyword") : builtins.propertyIsEnumerable(v) ? "builtin" : "variable";
  } else if (_ == "%") return e.skipTo("%") && e.next(), "variableName.special";
  else if (_ == "<" && e.eat("-") || _ == "<" && e.match("<-") || _ == "-" && e.match(/>>?/)) return "operator";
  else if (_ == "=" && g.ctx.argList) return "operator";
  else if (opChars.test(_)) return _ == "$" || e.eatWhile(opChars), "operator";
  else if (/[\(\){}\[\];]/.test(_)) return curPunc = _, _ == ";" ? "punctuation" : null;
  else return null;
}
function tokenString(e) {
  return function(g, _) {
    if (g.eat("\\")) {
      var v = g.next();
      return v == "x" ? g.match(/^[a-f0-9]{2}/i) : (v == "u" || v == "U") && g.eat("{") && g.skipTo("}") ? g.next() : v == "u" ? g.match(/^[a-f0-9]{4}/i) : v == "U" ? g.match(/^[a-f0-9]{8}/i) : /[0-7]/.test(v) && g.match(/^[0-7]{1,2}/), "string.special";
    } else {
      for (var y; (y = g.next()) != null; ) {
        if (y == e) {
          _.tokenize = tokenBase;
          break;
        }
        if (y == "\\") {
          g.backUp(1);
          break;
        }
      }
      return "string";
    }
  };
}
var ALIGN_YES = 1, ALIGN_NO = 2, BRACELESS = 4;
function push(e, g, _) {
  e.ctx = {
    type: g,
    indent: e.indent,
    flags: 0,
    column: _.column(),
    prev: e.ctx
  };
}
function setFlag(e, g) {
  var _ = e.ctx;
  e.ctx = {
    type: _.type,
    indent: _.indent,
    flags: _.flags | g,
    column: _.column,
    prev: _.prev
  };
}
function pop(e) {
  e.indent = e.ctx.indent, e.ctx = e.ctx.prev;
}
const r = {
  name: "r",
  startState: function(e) {
    return {
      tokenize: tokenBase,
      ctx: {
        type: "top",
        indent: -e,
        flags: ALIGN_NO
      },
      indent: 0,
      afterIdent: false
    };
  },
  token: function(e, g) {
    if (e.sol() && (g.ctx.flags & 3 || (g.ctx.flags |= ALIGN_NO), g.ctx.flags & BRACELESS && pop(g), g.indent = e.indentation()), e.eatSpace()) return null;
    var _ = g.tokenize(e, g);
    return _ != "comment" && (g.ctx.flags & ALIGN_NO) == 0 && setFlag(g, ALIGN_YES), (curPunc == ";" || curPunc == "{" || curPunc == "}") && g.ctx.type == "block" && pop(g), curPunc == "{" ? push(g, "}", e) : curPunc == "(" ? (push(g, ")", e), g.afterIdent && (g.ctx.argList = true)) : curPunc == "[" ? push(g, "]", e) : curPunc == "block" ? push(g, "block", e) : curPunc == g.ctx.type ? pop(g) : g.ctx.type == "block" && _ != "comment" && setFlag(g, BRACELESS), g.afterIdent = _ == "variable" || _ == "keyword", _;
  },
  indent: function(e, g, _) {
    if (e.tokenize != tokenBase) return 0;
    var v = g && g.charAt(0), y = e.ctx, b = v == y.type;
    return y.flags & BRACELESS && (y = y.prev), y.type == "block" ? y.indent + (v == "{" ? 0 : _.unit) : y.flags & ALIGN_YES ? y.column + (b ? 0 : 1) : y.indent + (b ? 0 : _.unit);
  },
  languageData: {
    wordChars: ".",
    commentTokens: { line: "#" },
    autocomplete: commonAtoms.concat(commonBuiltins, commonKeywords)
  }
};
export {
  r as t
};
