var curPunc;
function wordRegexp(e) {
  return RegExp("^(?:" + e.join("|") + ")$", "i");
}
var ops = wordRegexp(/* @__PURE__ */ "str.lang.langmatches.datatype.bound.sameterm.isiri.isuri.iri.uri.bnode.count.sum.min.max.avg.sample.group_concat.rand.abs.ceil.floor.round.concat.substr.strlen.replace.ucase.lcase.encode_for_uri.contains.strstarts.strends.strbefore.strafter.year.month.day.hours.minutes.seconds.timezone.tz.now.uuid.struuid.md5.sha1.sha256.sha384.sha512.coalesce.if.strlang.strdt.isnumeric.regex.exists.isblank.isliteral.a.bind".split(".")), keywords = wordRegexp(/* @__PURE__ */ "base.prefix.select.distinct.reduced.construct.describe.ask.from.named.where.order.limit.offset.filter.optional.graph.by.asc.desc.as.having.undef.values.group.minus.in.not.service.silent.using.insert.delete.union.true.false.with.data.copy.to.move.add.create.drop.clear.load.into".split(".")), operatorChars = /[*+\-<>=&|\^\/!\?]/, PN_CHARS = "[A-Za-z_\\-0-9]", PREFIX_START = /* @__PURE__ */ RegExp("[A-Za-z]"), PREFIX_REMAINDER = /* @__PURE__ */ RegExp("((" + PN_CHARS + "|\\.)*(" + PN_CHARS + "))?:");
function tokenBase(s, d) {
  var f = s.next();
  if (curPunc = null, f == "$" || f == "?") return f == "?" && s.match(/\s/, false) ? "operator" : (s.match(/^[A-Za-z0-9_\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD][A-Za-z0-9_\u00B7\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u037D\u037F-\u1FFF\u200C-\u200D\u203F-\u2040\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]*/), "variableName.local");
  if (f == "<" && !s.match(/^[\s\u00a0=]/, false)) return s.match(/^[^\s\u00a0>]*>?/), "atom";
  if (f == '"' || f == "'") return d.tokenize = tokenLiteral(f), d.tokenize(s, d);
  if (/[{}\(\),\.;\[\]]/.test(f)) return curPunc = f, "bracket";
  if (f == "#") return s.skipToEnd(), "comment";
  if (operatorChars.test(f)) return "operator";
  if (f == ":") return eatPnLocal(s), "atom";
  if (f == "@") return s.eatWhile(/[a-z\d\-]/i), "meta";
  if (PREFIX_START.test(f) && s.match(PREFIX_REMAINDER)) return eatPnLocal(s), "atom";
  s.eatWhile(/[_\w\d]/);
  var p = s.current();
  return ops.test(p) ? "builtin" : keywords.test(p) ? "keyword" : "variable";
}
function eatPnLocal(e) {
  e.match(/(\.(?=[\w_\-\\%])|[:\w_-]|\\[-\\_~.!$&'()*+,;=/?#@%]|%[a-f\d][a-f\d])+/i);
}
function tokenLiteral(e) {
  return function(s, c) {
    for (var l = false, u; (u = s.next()) != null; ) {
      if (u == e && !l) {
        c.tokenize = tokenBase;
        break;
      }
      l = !l && u == "\\";
    }
    return "string";
  };
}
function pushContext(e, s, c) {
  e.context = {
    prev: e.context,
    indent: e.indent,
    col: c,
    type: s
  };
}
function popContext(e) {
  e.indent = e.context.indent, e.context = e.context.prev;
}
const sparql = {
  name: "sparql",
  startState: function() {
    return {
      tokenize: tokenBase,
      context: null,
      indent: 0,
      col: 0
    };
  },
  token: function(s, c) {
    if (s.sol() && (c.context && c.context.align == null && (c.context.align = false), c.indent = s.indentation()), s.eatSpace()) return null;
    var l = c.tokenize(s, c);
    if (l != "comment" && c.context && c.context.align == null && c.context.type != "pattern" && (c.context.align = true), curPunc == "(") pushContext(c, ")", s.column());
    else if (curPunc == "[") pushContext(c, "]", s.column());
    else if (curPunc == "{") pushContext(c, "}", s.column());
    else if (/[\]\}\)]/.test(curPunc)) {
      for (; c.context && c.context.type == "pattern"; ) popContext(c);
      c.context && curPunc == c.context.type && (popContext(c), curPunc == "}" && c.context && c.context.type == "pattern" && popContext(c));
    } else curPunc == "." && c.context && c.context.type == "pattern" ? popContext(c) : /atom|string|variable/.test(l) && c.context && (/[\}\]]/.test(c.context.type) ? pushContext(c, "pattern", s.column()) : c.context.type == "pattern" && !c.context.align && (c.context.align = true, c.context.col = s.column()));
    return l;
  },
  indent: function(e, s, c) {
    var l = s && s.charAt(0), u = e.context;
    if (/[\]\}]/.test(l)) for (; u && u.type == "pattern"; ) u = u.prev;
    var d = u && l == u.type;
    return u ? u.type == "pattern" ? u.col : u.align ? u.col + (d ? 0 : 1) : u.indent + (d ? 0 : c.unit) : 0;
  },
  languageData: { commentTokens: { line: "#" } }
};
export {
  sparql as t
};
