import "../../../../../../@chevrotain_regexp-to-ast@11.0.3/node_modules/@chevrotain/regexp-to-ast/lib/src/character-classes.js";
import { BaseRegExpVisitor as j } from "../../../../../../@chevrotain_regexp-to-ast@11.0.3/node_modules/@chevrotain/regexp-to-ast/lib/src/base-regexp-visitor.js";
import { LexerDefinitionErrorType as d, Lexer as R } from "./lexer_public.js";
import { PRINT_ERROR as H } from "../../../../../../@chevrotain_utils@11.0.3/node_modules/@chevrotain/utils/lib/src/print.js";
import { canMatchCharCode as $, failedOptimizationPrefixMsg as K, getOptimizedStartCodesIndices as se } from "./reg_exp.js";
import { getRegExpAst as Z } from "./reg_exp_parser.js";
import p from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/has.js";
import g from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/forEach.js";
import q from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/compact.js";
import ie from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/flatten.js";
import ae from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/values.js";
import J from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/reject.js";
import ce from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/keys.js";
import u from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/map.js";
import w from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/isUndefined.js";
import L from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/isArray.js";
import v from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/includes.js";
import _ from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/isRegExp.js";
import A from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/isString.js";
import U from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/isFunction.js";
import ue from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/defaults.js";
import W from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/indexOf.js";
import y from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/reduce.js";
import Q from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/isEmpty.js";
import E from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/filter.js";
import ee from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/difference.js";
import le from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/head.js";
import fe from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/find.js";
const P = "PATTERN", C = "defaultMode", S = "modes";
let de = typeof new RegExp("(?:)").sticky == "boolean";
function it(e, t) {
  t = ue(t, {
    useSticky: de,
    debug: !1,
    safeMode: !1,
    positionTracking: "full",
    lineTerminatorCharacters: ["\r", `
`],
    tracer: (c, s) => s()
  });
  const n = t.tracer;
  n("initCharCodeToOptimizedIndexMap", () => {
    ye();
  });
  let r;
  n("Reject Lexer.NA", () => {
    r = J(e, (c) => c[P] === R.NA);
  });
  let o = !1, i;
  n("Transform Patterns", () => {
    o = !1, i = u(r, (c) => {
      const s = c[P];
      if (_(s)) {
        const f = s.source;
        return f.length === 1 && // only these regExp meta characters which can appear in a length one regExp
        f !== "^" && f !== "$" && f !== "." && !s.ignoreCase ? f : f.length === 2 && f[0] === "\\" && // not a meta character
        !v([
          "d",
          "D",
          "s",
          "S",
          "t",
          "r",
          "n",
          "t",
          "0",
          "c",
          "b",
          "B",
          "f",
          "v",
          "w",
          "W"
        ], f[1]) ? f[1] : t.useSticky ? X(s) : V(s);
      } else {
        if (U(s))
          return o = !0, { exec: s };
        if (typeof s == "object")
          return o = !0, s;
        if (typeof s == "string") {
          if (s.length === 1)
            return s;
          {
            const f = s.replace(/[\\^$.*+?()[\]{}|]/g, "\\$&"), T = new RegExp(f);
            return t.useSticky ? X(T) : V(T);
          }
        } else
          throw Error("non exhaustive match");
      }
    });
  });
  let a, l, m, h, D;
  n("misc mapping", () => {
    a = u(r, (c) => c.tokenTypeIdx), l = u(r, (c) => {
      const s = c.GROUP;
      if (s !== R.SKIPPED) {
        if (A(s))
          return s;
        if (w(s))
          return !1;
        throw Error("non exhaustive match");
      }
    }), m = u(r, (c) => {
      const s = c.LONGER_ALT;
      if (s)
        return L(s) ? u(s, (T) => W(r, T)) : [W(r, s)];
    }), h = u(r, (c) => c.PUSH_MODE), D = u(r, (c) => p(c, "POP_MODE"));
  });
  let I;
  n("Line Terminator Handling", () => {
    const c = re(t.lineTerminatorCharacters);
    I = u(r, (s) => !1), t.positionTracking !== "onlyOffset" && (I = u(r, (s) => p(s, "LINE_BREAKS") ? !!s.LINE_BREAKS : ne(s, c) === !1 && $(c, s.PATTERN)));
  });
  let z, B, k, O;
  n("Misc Mapping #2", () => {
    z = u(r, te), B = u(i, Se), k = y(r, (c, s) => {
      const f = s.GROUP;
      return A(f) && f !== R.SKIPPED && (c[f] = []), c;
    }, {}), O = u(i, (c, s) => ({
      pattern: i[s],
      longerAlt: m[s],
      canLineTerminator: I[s],
      isCustom: z[s],
      short: B[s],
      group: l[s],
      push: h[s],
      pop: D[s],
      tokenTypeIdx: a[s],
      tokenType: r[s]
    }));
  });
  let x = !0, G = [];
  return t.safeMode || n("First Char Optimization", () => {
    G = y(r, (c, s, f) => {
      if (typeof s.PATTERN == "string") {
        const T = s.PATTERN.charCodeAt(0), N = Y(T);
        b(c, N, O[f]);
      } else if (L(s.START_CHARS_HINT)) {
        let T;
        g(s.START_CHARS_HINT, (N) => {
          const oe = typeof N == "string" ? N.charCodeAt(0) : N, F = Y(oe);
          T !== F && (T = F, b(c, F, O[f]));
        });
      } else if (_(s.PATTERN))
        if (s.PATTERN.unicode)
          x = !1, t.ensureOptimizations && H(`${K}	Unable to analyze < ${s.PATTERN.toString()} > pattern.
	The regexp unicode flag is not currently supported by the regexp-to-ast library.
	This will disable the lexer's first char optimizations.
	For details See: https://chevrotain.io/docs/guide/resolving_lexer_errors.html#UNICODE_OPTIMIZE`);
        else {
          const T = se(s.PATTERN, t.ensureOptimizations);
          Q(T) && (x = !1), g(T, (N) => {
            b(c, N, O[f]);
          });
        }
      else
        t.ensureOptimizations && H(`${K}	TokenType: <${s.name}> is using a custom token pattern without providing <start_chars_hint> parameter.
	This will disable the lexer's first char optimizations.
	For details See: https://chevrotain.io/docs/guide/resolving_lexer_errors.html#CUSTOM_OPTIMIZE`), x = !1;
      return c;
    }, []);
  }), {
    emptyGroups: k,
    patternIdxToConfig: O,
    charCodeToPatternIdxToConfig: G,
    hasCustom: o,
    canBeOptimized: x
  };
}
function at(e, t) {
  let n = [];
  const r = pe(e);
  n = n.concat(r.errors);
  const o = Te(r.valid), i = o.valid;
  return n = n.concat(o.errors), n = n.concat(me(i)), n = n.concat(Pe(i)), n = n.concat(Ie(i, t)), n = n.concat(Oe(i)), n;
}
function me(e) {
  let t = [];
  const n = E(e, (r) => _(r[P]));
  return t = t.concat(Ee(n)), t = t.concat(Re(n)), t = t.concat(_e(n)), t = t.concat(Ne(n)), t = t.concat(ge(n)), t;
}
function pe(e) {
  const t = E(e, (o) => !p(o, P)), n = u(t, (o) => ({
    message: "Token Type: ->" + o.name + "<- missing static 'PATTERN' property",
    type: d.MISSING_PATTERN,
    tokenTypes: [o]
  })), r = ee(e, t);
  return { errors: n, valid: r };
}
function Te(e) {
  const t = E(e, (o) => {
    const i = o[P];
    return !_(i) && !U(i) && !p(i, "exec") && !A(i);
  }), n = u(t, (o) => ({
    message: "Token Type: ->" + o.name + "<- static 'PATTERN' can only be a RegExp, a Function matching the {CustomPatternMatcherFunc} type or an Object matching the {ICustomPattern} interface.",
    type: d.INVALID_PATTERN,
    tokenTypes: [o]
  })), r = ee(e, t);
  return { errors: n, valid: r };
}
const he = /[^\\][$]/;
function Ee(e) {
  class t extends j {
    constructor() {
      super(...arguments), this.found = !1;
    }
    visitEndAnchor(i) {
      this.found = !0;
    }
  }
  const n = E(e, (o) => {
    const i = o.PATTERN;
    try {
      const a = Z(i), l = new t();
      return l.visit(a), l.found;
    } catch {
      return he.test(i.source);
    }
  });
  return u(n, (o) => ({
    message: `Unexpected RegExp Anchor Error:
	Token Type: ->` + o.name + `<- static 'PATTERN' cannot contain end of input anchor '$'
	See chevrotain.io/docs/guide/resolving_lexer_errors.html#ANCHORS	for details.`,
    type: d.EOI_ANCHOR_FOUND,
    tokenTypes: [o]
  }));
}
function ge(e) {
  const t = E(e, (r) => r.PATTERN.test(""));
  return u(t, (r) => ({
    message: "Token Type: ->" + r.name + "<- static 'PATTERN' must not match an empty string",
    type: d.EMPTY_MATCH_PATTERN,
    tokenTypes: [r]
  }));
}
const Ae = /[^\\[][\^]|^\^/;
function Re(e) {
  class t extends j {
    constructor() {
      super(...arguments), this.found = !1;
    }
    visitStartAnchor(i) {
      this.found = !0;
    }
  }
  const n = E(e, (o) => {
    const i = o.PATTERN;
    try {
      const a = Z(i), l = new t();
      return l.visit(a), l.found;
    } catch {
      return Ae.test(i.source);
    }
  });
  return u(n, (o) => ({
    message: `Unexpected RegExp Anchor Error:
	Token Type: ->` + o.name + `<- static 'PATTERN' cannot contain start of input anchor '^'
	See https://chevrotain.io/docs/guide/resolving_lexer_errors.html#ANCHORS	for details.`,
    type: d.SOI_ANCHOR_FOUND,
    tokenTypes: [o]
  }));
}
function _e(e) {
  const t = E(e, (r) => {
    const o = r[P];
    return o instanceof RegExp && (o.multiline || o.global);
  });
  return u(t, (r) => ({
    message: "Token Type: ->" + r.name + "<- static 'PATTERN' may NOT contain global('g') or multiline('m')",
    type: d.UNSUPPORTED_FLAGS_FOUND,
    tokenTypes: [r]
  }));
}
function Ne(e) {
  const t = [];
  let n = u(e, (i) => y(e, (a, l) => (i.PATTERN.source === l.PATTERN.source && !v(t, l) && l.PATTERN !== R.NA && (t.push(l), a.push(l)), a), []));
  n = q(n);
  const r = E(n, (i) => i.length > 1);
  return u(r, (i) => {
    const a = u(i, (m) => m.name);
    return {
      message: `The same RegExp pattern ->${le(i).PATTERN}<-has been used in all of the following Token Types: ${a.join(", ")} <-`,
      type: d.DUPLICATE_PATTERNS_FOUND,
      tokenTypes: i
    };
  });
}
function Pe(e) {
  const t = E(e, (r) => {
    if (!p(r, "GROUP"))
      return !1;
    const o = r.GROUP;
    return o !== R.SKIPPED && o !== R.NA && !A(o);
  });
  return u(t, (r) => ({
    message: "Token Type: ->" + r.name + "<- static 'GROUP' can only be Lexer.SKIPPED/Lexer.NA/A String",
    type: d.INVALID_GROUP_TYPE_FOUND,
    tokenTypes: [r]
  }));
}
function Ie(e, t) {
  const n = E(e, (o) => o.PUSH_MODE !== void 0 && !v(t, o.PUSH_MODE));
  return u(n, (o) => ({
    message: `Token Type: ->${o.name}<- static 'PUSH_MODE' value cannot refer to a Lexer Mode ->${o.PUSH_MODE}<-which does not exist`,
    type: d.PUSH_MODE_DOES_NOT_EXIST,
    tokenTypes: [o]
  }));
}
function Oe(e) {
  const t = [], n = y(e, (r, o, i) => {
    const a = o.PATTERN;
    return a === R.NA || (A(a) ? r.push({ str: a, idx: i, tokenType: o }) : _(a) && Ce(a) && r.push({ str: a.source, idx: i, tokenType: o })), r;
  }, []);
  return g(e, (r, o) => {
    g(n, ({ str: i, idx: a, tokenType: l }) => {
      if (o < a && xe(i, r.PATTERN)) {
        const m = `Token: ->${l.name}<- can never be matched.
Because it appears AFTER the Token Type ->${r.name}<-in the lexer's definition.
See https://chevrotain.io/docs/guide/resolving_lexer_errors.html#UNREACHABLE`;
        t.push({
          message: m,
          type: d.UNREACHABLE_PATTERN,
          tokenTypes: [r, l]
        });
      }
    });
  }), t;
}
function xe(e, t) {
  if (_(t)) {
    const n = t.exec(e);
    return n !== null && n.index === 0;
  } else {
    if (U(t))
      return t(e, 0, [], {});
    if (p(t, "exec"))
      return t.exec(e, 0, [], {});
    if (typeof t == "string")
      return t === e;
    throw Error("non exhaustive match");
  }
}
function Ce(e) {
  return fe([
    ".",
    "\\",
    "[",
    "]",
    "|",
    "^",
    "$",
    "(",
    ")",
    "?",
    "*",
    "+",
    "{"
  ], (n) => e.source.indexOf(n) !== -1) === void 0;
}
function V(e) {
  const t = e.ignoreCase ? "i" : "";
  return new RegExp(`^(?:${e.source})`, t);
}
function X(e) {
  const t = e.ignoreCase ? "iy" : "y";
  return new RegExp(`${e.source}`, t);
}
function ct(e, t, n) {
  const r = [];
  return p(e, C) || r.push({
    message: "A MultiMode Lexer cannot be initialized without a <" + C + `> property in its definition
`,
    type: d.MULTI_MODE_LEXER_WITHOUT_DEFAULT_MODE
  }), p(e, S) || r.push({
    message: "A MultiMode Lexer cannot be initialized without a <" + S + `> property in its definition
`,
    type: d.MULTI_MODE_LEXER_WITHOUT_MODES_PROPERTY
  }), p(e, S) && p(e, C) && !p(e.modes, e.defaultMode) && r.push({
    message: `A MultiMode Lexer cannot be initialized with a ${C}: <${e.defaultMode}>which does not exist
`,
    type: d.MULTI_MODE_LEXER_DEFAULT_MODE_VALUE_DOES_NOT_EXIST
  }), p(e, S) && g(e.modes, (o, i) => {
    g(o, (a, l) => {
      if (w(a))
        r.push({
          message: `A Lexer cannot be initialized using an undefined Token Type. Mode:<${i}> at index: <${l}>
`,
          type: d.LEXER_DEFINITION_CANNOT_CONTAIN_UNDEFINED
        });
      else if (p(a, "LONGER_ALT")) {
        const m = L(a.LONGER_ALT) ? a.LONGER_ALT : [a.LONGER_ALT];
        g(m, (h) => {
          !w(h) && !v(o, h) && r.push({
            message: `A MultiMode Lexer cannot be initialized with a longer_alt <${h.name}> on token <${a.name}> outside of mode <${i}>
`,
            type: d.MULTI_MODE_LEXER_LONGER_ALT_NOT_IN_CURRENT_MODE
          });
        });
      }
    });
  }), r;
}
function ut(e, t, n) {
  const r = [];
  let o = !1;
  const i = q(ie(ae(e.modes))), a = J(i, (m) => m[P] === R.NA), l = re(n);
  return t && g(a, (m) => {
    const h = ne(m, l);
    if (h !== !1) {
      const I = {
        message: Me(m, h),
        type: h.issue,
        tokenType: m
      };
      r.push(I);
    } else
      p(m, "LINE_BREAKS") ? m.LINE_BREAKS === !0 && (o = !0) : $(l, m.PATTERN) && (o = !0);
  }), t && !o && r.push({
    message: `Warning: No LINE_BREAKS Found.
	This Lexer has been defined to track line and column information,
	But none of the Token Types can be identified as matching a line terminator.
	See https://chevrotain.io/docs/guide/resolving_lexer_errors.html#LINE_BREAKS 
	for details.`,
    type: d.NO_LINE_BREAKS_FLAGS
  }), r;
}
function lt(e) {
  const t = {}, n = ce(e);
  return g(n, (r) => {
    const o = e[r];
    if (L(o))
      t[r] = [];
    else
      throw Error("non exhaustive match");
  }), t;
}
function te(e) {
  const t = e.PATTERN;
  if (_(t))
    return !1;
  if (U(t))
    return !0;
  if (p(t, "exec"))
    return !0;
  if (A(t))
    return !1;
  throw Error("non exhaustive match");
}
function Se(e) {
  return A(e) && e.length === 1 ? e.charCodeAt(0) : !1;
}
const ft = {
  // implements /\n|\r\n?/g.test
  test: function(e) {
    const t = e.length;
    for (let n = this.lastIndex; n < t; n++) {
      const r = e.charCodeAt(n);
      if (r === 10)
        return this.lastIndex = n + 1, !0;
      if (r === 13)
        return e.charCodeAt(n + 1) === 10 ? this.lastIndex = n + 2 : this.lastIndex = n + 1, !0;
    }
    return !1;
  },
  lastIndex: 0
};
function ne(e, t) {
  if (p(e, "LINE_BREAKS"))
    return !1;
  if (_(e.PATTERN)) {
    try {
      $(t, e.PATTERN);
    } catch (n) {
      return {
        issue: d.IDENTIFY_TERMINATOR,
        errMsg: n.message
      };
    }
    return !1;
  } else {
    if (A(e.PATTERN))
      return !1;
    if (te(e))
      return { issue: d.CUSTOM_LINE_BREAK };
    throw Error("non exhaustive match");
  }
}
function Me(e, t) {
  if (t.issue === d.IDENTIFY_TERMINATOR)
    return `Warning: unable to identify line terminator usage in pattern.
	The problem is in the <${e.name}> Token Type
	 Root cause: ${t.errMsg}.
	For details See: https://chevrotain.io/docs/guide/resolving_lexer_errors.html#IDENTIFY_TERMINATOR`;
  if (t.issue === d.CUSTOM_LINE_BREAK)
    return `Warning: A Custom Token Pattern should specify the <line_breaks> option.
	The problem is in the <${e.name}> Token Type
	For details See: https://chevrotain.io/docs/guide/resolving_lexer_errors.html#CUSTOM_LINE_BREAK`;
  throw Error("non exhaustive match");
}
function re(e) {
  return u(e, (n) => A(n) ? n.charCodeAt(0) : n);
}
function b(e, t, n) {
  e[t] === void 0 ? e[t] = [n] : e[t].push(n);
}
const Le = 256;
let M = [];
function Y(e) {
  return e < Le ? e : M[e];
}
function ye() {
  if (Q(M)) {
    M = new Array(65536);
    for (let e = 0; e < 65536; e++)
      M[e] = e > 255 ? 255 + ~~(e / 255) : e;
  }
}
export {
  C as DEFAULT_MODE,
  ft as LineTerminatorOptimizedTester,
  S as MODES,
  de as SUPPORT_STICKY,
  V as addStartOfInput,
  X as addStickyFlag,
  it as analyzeTokenTypes,
  Me as buildLineBreakIssueMessage,
  Y as charCodeToOptimizedIndex,
  lt as cloneEmptyGroups,
  Ne as findDuplicatePatterns,
  ge as findEmptyMatchRegExps,
  Ee as findEndOfInputAnchor,
  Pe as findInvalidGroupType,
  Te as findInvalidPatterns,
  pe as findMissingPatterns,
  Ie as findModesThatDoNotExist,
  Re as findStartOfInputAnchor,
  Oe as findUnreachablePatterns,
  _e as findUnsupportedFlags,
  te as isCustomPattern,
  Se as isShortPattern,
  Le as minOptimizationVal,
  ct as performRuntimeChecks,
  ut as performWarningRuntimeChecks,
  at as validatePatterns
};
