var curPunc;
function wordRegexp(e) {
  return RegExp("^(?:" + e.join("|") + ")$", "i");
}
wordRegexp([]);
var keywords = wordRegexp([
  "@prefix",
  "@base",
  "a"
]), operatorChars = /[*+\-<>=&|]/;
function tokenBase(n, a) {
  var s = n.next();
  if (curPunc = null, s == "<" && !n.match(/^[\s\u00a0=]/, false)) return n.match(/^[^\s\u00a0>]*>?/), "atom";
  if (s == '"' || s == "'") return a.tokenize = tokenLiteral(s), a.tokenize(n, a);
  if (/[{}\(\),\.;\[\]]/.test(s)) return curPunc = s, null;
  if (s == "#") return n.skipToEnd(), "comment";
  if (operatorChars.test(s)) return n.eatWhile(operatorChars), null;
  if (s == ":") return "operator";
  if (n.eatWhile(/[_\w\d]/), n.peek() == ":") return "variableName.special";
  var c = n.current();
  return keywords.test(c) ? "meta" : s >= "A" && s <= "Z" ? "comment" : "keyword";
  var c;
}
function tokenLiteral(e) {
  return function(n, r) {
    for (var i = false, o; (o = n.next()) != null; ) {
      if (o == e && !i) {
        r.tokenize = tokenBase;
        break;
      }
      i = !i && o == "\\";
    }
    return "string";
  };
}
function pushContext(e, n, r) {
  e.context = {
    prev: e.context,
    indent: e.indent,
    col: r,
    type: n
  };
}
function popContext(e) {
  e.indent = e.context.indent, e.context = e.context.prev;
}
const turtle = {
  name: "turtle",
  startState: function() {
    return {
      tokenize: tokenBase,
      context: null,
      indent: 0,
      col: 0
    };
  },
  token: function(n, r) {
    if (n.sol() && (r.context && r.context.align == null && (r.context.align = false), r.indent = n.indentation()), n.eatSpace()) return null;
    var i = r.tokenize(n, r);
    if (i != "comment" && r.context && r.context.align == null && r.context.type != "pattern" && (r.context.align = true), curPunc == "(") pushContext(r, ")", n.column());
    else if (curPunc == "[") pushContext(r, "]", n.column());
    else if (curPunc == "{") pushContext(r, "}", n.column());
    else if (/[\]\}\)]/.test(curPunc)) {
      for (; r.context && r.context.type == "pattern"; ) popContext(r);
      r.context && curPunc == r.context.type && popContext(r);
    } else curPunc == "." && r.context && r.context.type == "pattern" ? popContext(r) : /atom|string|variable/.test(i) && r.context && (/[\}\]]/.test(r.context.type) ? pushContext(r, "pattern", n.column()) : r.context.type == "pattern" && !r.context.align && (r.context.align = true, r.context.col = n.column()));
    return i;
  },
  indent: function(e, n, r) {
    var i = n && n.charAt(0), a = e.context;
    if (/[\]\}]/.test(i)) for (; a && a.type == "pattern"; ) a = a.prev;
    var o = a && i == a.type;
    return a ? a.type == "pattern" ? a.col : a.align ? a.col + (o ? 0 : 1) : a.indent + (o ? 0 : r.unit) : 0;
  },
  languageData: { commentTokens: { line: "#" } }
};
export {
  turtle as t
};
