function simpleMode(e) {
  ensureState(e, "start");
  var n = {}, r = e.languageData || {}, s = false;
  for (var c in e) if (c != r && e.hasOwnProperty(c)) for (var l = n[c] = [], u = e[c], d = 0; d < u.length; d++) {
    var f = u[d];
    l.push(new Rule(f, e)), (f.indent || f.dedent) && (s = true);
  }
  return {
    name: r.name,
    startState: function() {
      return {
        state: "start",
        pending: null,
        indent: s ? [] : null
      };
    },
    copyState: function(e2) {
      var t = {
        state: e2.state,
        pending: e2.pending,
        indent: e2.indent && e2.indent.slice(0)
      };
      return e2.stack && (t.stack = e2.stack.slice(0)), t;
    },
    token: tokenFunction(n),
    indent: indentFunction(n, r),
    mergeTokens: r.mergeTokens,
    languageData: r
  };
}
function ensureState(e, t) {
  if (!e.hasOwnProperty(t)) throw Error("Undefined state " + t + " in simple mode");
}
function toRegex(e, t) {
  if (!e) return /(?:)/;
  var n = "";
  return e instanceof RegExp ? (e.ignoreCase && (n = "i"), e.unicode && (n += "u"), e = e.source) : e = String(e), RegExp((t === false ? "" : "^") + "(?:" + e + ")", n);
}
function asToken(e) {
  if (!e) return null;
  if (e.apply) return e;
  if (typeof e == "string") return e.replace(/\./g, " ");
  for (var t = [], n = 0; n < e.length; n++) t.push(e[n] && e[n].replace(/\./g, " "));
  return t;
}
function Rule(e, i) {
  (e.next || e.push) && ensureState(i, e.next || e.push), this.regex = toRegex(e.regex), this.token = asToken(e.token), this.data = e;
}
function tokenFunction(e) {
  return function(t, n) {
    if (n.pending) {
      var r = n.pending.shift();
      return n.pending.length == 0 && (n.pending = null), t.pos += r.text.length, r.token;
    }
    for (var i = e[n.state], a = 0; a < i.length; a++) {
      var o = i[a], s = (!o.data.sol || t.sol()) && t.match(o.regex);
      if (s) {
        o.data.next ? n.state = o.data.next : o.data.push ? ((n.stack || (n.stack = [])).push(n.state), n.state = o.data.push) : o.data.pop && n.stack && n.stack.length && (n.state = n.stack.pop()), o.data.indent && n.indent.push(t.indentation() + t.indentUnit), o.data.dedent && n.indent.pop();
        var c = o.token;
        if (c && c.apply && (c = c(s)), s.length > 2 && o.token && typeof o.token != "string") {
          n.pending = [];
          for (var l = 2; l < s.length; l++) s[l] && n.pending.push({
            text: s[l],
            token: o.token[l - 1]
          });
          return t.backUp(s[0].length - (s[1] ? s[1].length : 0)), c[0];
        } else if (c && c.join) return c[0];
        else return c;
      }
    }
    return t.next(), null;
  };
}
function indentFunction(e, t) {
  return function(n, r) {
    if (n.indent == null || t.dontIndentStates && t.dontIndentStates.indexOf(n.state) > -1) return null;
    var i = n.indent.length - 1, a = e[n.state];
    scan: for (; ; ) {
      for (var o = 0; o < a.length; o++) {
        var s = a[o];
        if (s.data.dedent && s.data.dedentIfLineStart !== false) {
          var c = s.regex.exec(r);
          if (c && c[0]) {
            i--, (s.next || s.push) && (a = e[s.next || s.push]), r = r.slice(c[0].length);
            continue scan;
          }
        }
      }
      break;
    }
    return i < 0 ? 0 : n.indent[i];
  };
}
export {
  simpleMode as t
};
