{"version":3,"sources":["../src/combinators/attempt.ts","../src/combinators/many.ts","../src/combinators/map.ts","../src/combinators/sequence.ts","../src/combinators/chain.ts","../src/combinators/choice.ts","../src/combinators/error.ts","../src/combinators/lookahead.ts","../src/core/run.ts","../src/core/tryRun.ts","../src/core/grammar.ts","../src/parsers/any.ts","../src/parsers/defer.ts","../src/parsers/eof.ts","../src/utils/unicode.ts","../src/parsers/string.ts","../src/parsers/eol.ts","../src/parsers/regexp.ts","../src/parsers/letter.ts","../src/parsers/noneOf.ts","../src/parsers/nothing.ts","../src/parsers/numbers.ts","../src/parsers/oneOf.ts","../src/parsers/rest.ts","../src/parsers/whitespace.ts","../src/combinators/optional.ts","../src/combinators/sepBy.ts","../src/combinators/take.ts","../src/combinators/until.ts","../src/combinators/when.ts"],"names":["attempt","parser","input","pos","result","many","values","nextPos","many1","resultP","resultR","map","fn","span","mapTo","value","sequence","ps","toLR","left","right","op","chainl","choice","first","rest","nextResult","error","expected","lookahead","run","ParserError","res","tryRun","grammar","init","key","any","defer","deferred","eof","size","string","bytes","index","high","low","match","slice","ustring","EOL_UNIX","EOL_NON_UNIX","eol","regexp","rs","re","LETTER_RE","LETTERS_RE","letter","letters","noneOf","chars","charset","char","nothing","_","HEXADECIMAL_RE","BINARY_RE","OCTAL_RE","WHOLE_RE","INTEGER_RE","FLOAT_RE","hex","binary","octal","whole","integer","float","oneOf","WHITESPACE_REQUIRED_RE","whitespace","optional","sepBy","sep","resultS","sepBy1","toLeft","toMiddle","middle","toRight","toSides","takeLeft","p1","p2","takeMid","p3","takeRight","takeSides","takeUntil","terminator","resultT","skipUntil","when","context"],"mappings":"AAUO,SAASA,EAAWC,EAA8B,CACvD,MAAO,CACL,MAAMC,EAAOC,EAAK,CAChB,IAAMC,EAASH,EAAO,MAAMC,EAAOC,CAAG,EAEtC,OAAQC,EAAO,KAAM,CAEnB,IAAK,GACH,MAAO,CACL,KAAM,GACN,KAAM,CAACD,EAAKA,CAAG,EACf,IAAAA,EACA,MAAO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CAChBF,EAAUE,EAAQ,IAClB,QACF,CAEA,OAAOA,CACT,CACF,CACF,CACF,CACF,CACF,CCxEO,SAAS6F,GACdC,EACAtG,EACa,CACb,MAAO,CACL,MAAMC,EAAOC,EAAK,CAChB,IAAMC,EAASmG,EAAQ,MAAMrG,EAAOC,CAAG,EAEvC,OAAQC,EAAO,KAAM,CACnB,IAAK,GACH,OAAOH,EAAO,CAAE,MAAOG,EAAO,MAAO,IAAKA,EAAO,IAAK,MAAAF,CAAM,CAAC,EAAE,MAAMA,EAAOE,EAAO,GAAG,EAGxF,IAAK,GACH,OAAOA,CAEX,CACF,CACF,CACF","sourcesContent":["import type { Parser } from '@types'\n\n/**\n * Applies `parser` without consuming any input. It doesn't care if `parser` succeeds or fails, it\n * won't consume any input.\n *\n * @param parser - Parser to apply\n *\n * @returns Result of `parser`\n */\nexport function attempt<T>(parser: Parser<T>): Parser<T> {\n  return {\n    parse(input, pos) {\n      const result = parser.parse(input, pos)\n\n      switch (result.isOk) {\n        // If parser succeeded, keep the position untouched.\n        case true: {\n          return {\n            isOk: true,\n            span: [pos, pos],\n            pos,\n            value: result.value\n          }\n        }\n\n        // If parser failed, keep the position untouched as well.\n        case false: {\n          return {\n            isOk: false,\n            span: [pos, pos],\n            pos,\n            expected: result.expected\n          }\n        }\n      }\n    }\n  }\n}\n","import type { Parser, SucceedingParser } from '@types'\n\n/**\n * Applies `parser` *zero* or more times, collecting its results. Never fails.\n *\n * @param parser - Parser to apply\n *\n * @returns Array of the returned values of `parser`\n */\nexport function many<T>(parser: Parser<T>): SucceedingParser<Array<T>> {\n  return {\n    parse(input, pos) {\n      const values: Array<T> = []\n      let nextPos = pos\n\n      while (nextPos < input.length) {\n        const result = parser.parse(input, nextPos)\n\n        if (result.isOk) {\n          values.push(result.value)\n          nextPos = result.pos\n        } else {\n          break\n        }\n      }\n\n      return {\n        isOk: true,\n        span: [pos, nextPos],\n        pos: nextPos,\n        value: values\n      }\n    }\n  }\n}\n\n/**\n * Applies `parser` *one* or more times, collecting its results.\n *\n * @param parser - Parser to apply\n *\n * @returns Array of the returned values of `parser`\n */\nexport function many1<T>(parser: Parser<T>): Parser<Array<T>> {\n  return {\n    parse(input, pos) {\n      const resultP = parser.parse(input, pos)\n\n      if (resultP.isOk) {\n        const values: Array<T> = []\n        let nextPos = resultP.pos\n\n        values.push(resultP.value)\n\n        while (nextPos < input.length) {\n          const resultR = parser.parse(input, nextPos)\n\n          if (resultR.isOk) {\n            values.push(resultR.value)\n            nextPos = resultR.pos\n            continue\n          }\n\n          break\n        }\n\n        return {\n          isOk: true,\n          span: [pos, nextPos],\n          pos: nextPos,\n          value: values\n        }\n      }\n\n      return resultP\n    }\n  }\n}\n","import type { Parser, Span } from '@types'\n\n/**\n * Applies `fn` to the `parser`'s result.\n *\n * @param parser - Parser to apply\n * @param fn - Function to apply to `parser`'s result\n *\n * @returns Result of `fn`\n */\nexport function map<T, R>(parser: Parser<T>, fn: (value: T, span: Span) => R): Parser<R> {\n  return {\n    parse(input, pos) {\n      const result = parser.parse(input, pos)\n\n      switch (result.isOk) {\n        case true: {\n          const span: Span = [pos, result.pos]\n\n          return {\n            isOk: true,\n            span,\n            pos: result.pos,\n            value: fn(result.value, span)\n          }\n        }\n\n        case false: {\n          return result\n        }\n      }\n    }\n  }\n}\n\n/**\n * Maps the `parser`'s result to a constant `value`.\n *\n * @param parser - Parser to apply\n * @param value - Value to map `parser`'s result to\n *\n * @returns `value`\n */\nexport function mapTo<T, R>(parser: Parser<T>, value: R): Parser<R> {\n  return map(parser, () => value)\n}\n","import type { Parser, ToTuple, ToTupleOrArray } from '@types'\n\n/**\n * Applies `ps` parsers in order, until *all* of them succeed.\n *\n * @param ps - Parsers to apply\n *\n * @returns Tuple of values returned by `ps` parsers\n */\nexport function sequence<T extends Array<Parser<unknown>>>(...ps: T): Parser<ToTuple<T>>\nexport function sequence<T extends Array<Parser<unknown>>>(...ps: T): Parser<ToTupleOrArray<T>>\nexport function sequence<T>(...ps: Array<Parser<T>>): Parser<Array<T>> {\n  return {\n    parse(input, pos) {\n      const values: Array<T> = []\n      let nextPos = pos\n\n      for (const parser of ps) {\n        const result = parser.parse(input, nextPos)\n\n        switch (result.isOk) {\n          case true: {\n            values.push(result.value)\n            nextPos = result.pos\n            break\n          }\n\n          case false: {\n            return result\n          }\n        }\n      }\n\n      return {\n        isOk: true,\n        span: [pos, nextPos],\n        pos: nextPos,\n        value: values\n      }\n    }\n  }\n}\n","import { many } from './many'\nimport { map } from './map'\nimport { sequence } from './sequence'\n\nimport type { Parser } from '@types'\n\n/** @internal */\ntype Fn<L, R> = (left: L, right: R) => L\n\n/** @internal */\nfunction toLR<T, L extends T, R>(fn: Fn<T, R>) {\n  return ([left, right]: [L, Array<R>]): T => {\n    return right.reduce<T>((result, op) => fn(result, op), left)\n  }\n}\n\n/**\n * Parses *zero* or more occurrences of `parser`, separated by `op` (in [EBNF] notation:\n * `parser (op parser)*`). Returns a value obtained by a recursive left-associative application of\n * `fn` to the values returned by `op` and `parser`.\n *\n * This combinator is particularly useful for eliminating left recursion, which typically occurs in\n * expression grammars.\n *\n * [EBNF]: https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form\n *\n * @param parser - Parser to apply\n * @param op - Separating parser\n * @param fn - Left-associative function to apply to the values returned by `op` and `parser`\n *\n * @returns Value from `fn`\n */\nexport function chainl<T, L extends T, R>(\n  parser: Parser<L>,\n  op: Parser<R>,\n  fn: Fn<T, R>\n): Parser<T> {\n  return map(sequence(parser, many(op)), toLR(fn))\n}\n","import type { Parser, ToUnion } from '@types'\n\n/**\n * Applies `ps` parsers in order until one of them succeeds.\n *\n * @param ps - Parsers to apply\n *\n * @returns Value of the succeeding parser\n */\nexport function choice<T extends Array<Parser<unknown>>>(...ps: T): Parser<ToUnion<T>>\nexport function choice<T>(...ps: Array<Parser<T>>): Parser<T> {\n  return {\n    parse(input, pos) {\n      // It's \"guaranteed\" by type system that there will be at least two parsers, so I'm not gonna\n      // bother checking for `ps` length and asserting it, because it would hit performance.\n      const [first, ...rest] = ps\n\n      // Run the first parser to infer the type.\n      let nextResult = first.parse(input, pos)\n\n      // Test other alternatives if the first one fails.\n      if (!nextResult.isOk) {\n        for (const parser of rest) {\n          const result = parser.parse(input, pos)\n\n          switch (result.isOk) {\n            case true: {\n              return result\n            }\n\n            case false: {\n              if (!nextResult || nextResult.pos < result.pos) {\n                nextResult = result\n              }\n            }\n          }\n        }\n      }\n\n      return nextResult\n    }\n  }\n}\n","import type { Parser } from '@types'\n\n/**\n * Replaces `parser`'s error message with `expected`.\n *\n * @param parser - Parser of which error message should be replaced\n * @param expected - New error message\n *\n * @returns Unchanged `parser`'s result or failure with new error message\n */\nexport function error<T>(parser: Parser<T>, expected: string): Parser<T> {\n  return {\n    parse(input, pos) {\n      const result = parser.parse(input, pos)\n\n      switch (result.isOk) {\n        case true: {\n          return result\n        }\n\n        case false: {\n          return {\n            isOk: false,\n            span: [pos, result.pos],\n            pos: result.pos,\n            expected\n          }\n        }\n      }\n    }\n  }\n}\n","import type { Parser } from '@types'\n\n/**\n * Applies `parser` without consuming any input. If `parser` fails and consumes some input, so does\n * `lookahead`.\n *\n * @param parser - Parser to apply\n *\n * @returns Result of `parser`\n */\nexport function lookahead<T>(parser: Parser<T>): Parser<T> {\n  return {\n    parse(input, pos) {\n      const result = parser.parse(input, pos)\n\n      switch (result.isOk) {\n        // If parser succeeded, keep the position untouched.\n        case true: {\n          return {\n            isOk: true,\n            span: result.span,\n            pos,\n            value: result.value\n          }\n        }\n\n        // If the parser failed, then still advance the pos cursor.\n        case false: {\n          return result\n        }\n      }\n    }\n  }\n}\n","import type { Parser, Result } from '@types'\n\n/** @internal */\ninterface Runnable<T> {\n  with(input: string): Result<T>\n}\n\n/**\n * Runs a parser with provided input.\n *\n * @param parser - Parser to run\n *\n * @returns Parser result\n */\nexport function run<T>(parser: Parser<T>): Runnable<T> {\n  return {\n    with(input) {\n      return parser.parse(input, 0)\n    }\n  }\n}\n","import type { Failure, Parser, Span, Success } from '@types'\n\n/** @internal */\ninterface Runnable<T> {\n  with(input: string): Success<T>\n}\n\n/** @internal */\ntype ErrorResult = Omit<Failure, 'isOk'>\n\nexport class ParserError extends Error {\n  readonly name = 'ParserError'\n\n  readonly span: Span\n  readonly pos: number\n\n  constructor(res: ErrorResult) {\n    super(res.expected)\n\n    this.span = res.span\n    this.pos = res.pos\n  }\n}\n\n/**\n * Runs a parser with provided input, throwing on failure.\n *\n * @param parser - Parser to run\n * @throws {@link ParserError} Parser error with `message` (`expected`) `span`, and `pos`\n *\n * @returns Parser result\n */\nexport function tryRun<T>(parser: Parser<T>): Runnable<T> {\n  return {\n    with(input) {\n      const result = parser.parse(input, 0)\n\n      switch (result.isOk) {\n        case true: {\n          return result\n        }\n\n        case false: {\n          throw new ParserError(result)\n        }\n      }\n    }\n  }\n}\n","import type { Parser } from '@lib/types'\n\n/**\n * This type extracts the return-types from the parser initializers.\n */\nexport type Grammar<T> = {\n  [P in keyof T]: T[P] extends () => unknown ? ReturnType<T[P]> : never\n}\n\n/**\n * This type injects the initialized parser types into `this`, allowing for\n * type-safe self-contained and mutually recursive grammars.\n */\nexport type GrammarInit<T> = T & ThisType<Grammar<T>>\n\n/**\n * This defines the input to the `grammar` function - the parser initializers.\n */\nexport type GrammarType = {\n  [name: string]: () => Parser<unknown>\n}\n\n/**\n * This is a utility function to simplify the creation of a self-contained grammar.\n *\n * Similarly to `defer`, this allows for the creation of mutually recursive parsers,\n * but lets you define all of the component parsers within a single call.\n *\n * The function takes an object with parser initializers, and returns an object with\n * all of those parsers initialized. Within the parser initializers, use `this` to\n * reference other initialized parsers, as in the example below.\n *\n * The properties of the resulting object are just regular parsers - you can freely\n * destructure these, pass them around individually, or compose them with other\n * grammars, parsers or combinators as needed.\n *\n * @example\n *\n * ```typescript\n * interface NumberNode {\n *   type: 'number'\n *   value: number\n * }\n *\n * interface ListNode {\n *   type: 'list'\n *   value: Array<NumberNode | ListNode>\n * }\n *\n * const tupleGrammar = grammar({\n *   tupleNumber(): Parser<NumberNode> {\n *     return map(integer(), (value, span) => ({ type: 'number', span, value }))\n *   },\n *   tupleList(): Parser<ListNode> {\n *     return map(\n *       takeMid(\n *         string('('),\n *         sepBy(choice(this.tupleList, this.tupleNumber), string(',')),\n *         string(')')\n *       ),\n *       (value, span) => ({ type: 'list', span, value })\n *     )\n *   }\n * })\n *\n * const result = run(tupleGrammar.tupleList).with('(1,2,(3,4))')\n * ```\n */\nexport function grammar<T extends GrammarType>(init: GrammarInit<T>): Grammar<T> {\n  const grammar = {} as { [key: string]: Parser<unknown> }\n\n  for (const key in init) {\n    grammar[key] = {\n      // istanbul ignore next\n      parse() {\n        throw new Error(`internal error`) // this line should never execute\n      }\n    }\n  }\n\n  for (const key in init) {\n    grammar[key].parse = init[key].apply(grammar).parse\n  }\n\n  return grammar as Grammar<T>\n}\n","import type { Parser } from '@types'\n\n/**\n * Parses any single character from the input and returns it. Fails at the end of input.\n *\n * @returns A single parsed character.\n */\nexport function any(): Parser<string> {\n  return {\n    parse(input, pos) {\n      if (input.length === pos) {\n        return {\n          isOk: false,\n          span: [pos, pos],\n          pos,\n          expected: 'any @ reached the end of input'\n        }\n      }\n\n      const nextPos = pos + 1\n      const value = input.substring(pos, nextPos)\n\n      return {\n        isOk: true,\n        span: [pos, nextPos],\n        pos: nextPos,\n        value\n      }\n    }\n  }\n}\n","import type { Parser } from '@types'\n\n/**\n * Intersection type to add a method for deferred parser definition.\n *\n * @internal\n */\ntype Deferred<T> = Parser<T> & {\n  with(parser: Parser<T>): void\n}\n\n/**\n * This is a special parser that has an additional `with` method, which should be used to define the\n * parser. This parser is tailored for creating mutually recursive parsers.\n *\n * @example\n *\n * ```typescript\n * interface NumberNode {\n *   type: 'number'\n *   value: number\n * }\n *\n * interface ListNode {\n *   type: 'list'\n *   value: Array<NumberNode | ListNode>\n * }\n *\n * // Here we create 'dummies'\n *\n * const TupleList = defer<ListNode>()\n * const TupleNumber = defer<NumberNode>()\n *\n * // And below we actually define parsers\n *\n * TupleNumber.with(\n *   map(\n *     int(),\n *     (value) => ({ type: 'number', value })\n *   )\n * )\n *\n * TupleList.with(\n *   map(\n *     takeMid(\n *       string('('),\n *       sepBy(choice(TupleList, TupleNumber), string(',')),\n *       string(')')\n *     ),\n *     (value) => ({ type: 'list', value })\n *   )\n * )\n *\n * console.log(\n *   run(TupleList).with('(1,2,(3,(4,5)))')\n * )\n * ```\n */\nexport function defer<T>(): Deferred<T> {\n  let deferred: Parser<T> | null = null\n\n  return {\n    with(parser): void {\n      deferred = parser\n    },\n\n    parse(input, pos) {\n      if (deferred) {\n        return deferred.parse(input, pos)\n      }\n\n      throw new Error('Deferred parser was not initialized')\n    }\n  }\n}\n","import type { Parser } from '@types'\n\n/**\n * Only succeeds at the end of the input.\n *\n * @returns `null`\n */\nexport function eof(): Parser<null> {\n  return {\n    parse(input, pos) {\n      switch (pos === input.length) {\n        case true: {\n          return {\n            isOk: true,\n            span: [pos, pos],\n            pos: input.length,\n            value: null\n          }\n        }\n\n        case false: {\n          return {\n            isOk: false,\n            span: [pos, pos],\n            pos,\n            expected: 'end of input'\n          }\n        }\n      }\n    }\n  }\n}\n","/**\n * Iterates over a given Unicode string and counts its length in bytes.\n *\n * Covers Basic Multilingual Plane with all 163 blocks in range from `0x0000` to `0xFFFF`.\n *\n * @internal\n */\nexport function size(string: string): number {\n  const size = string.length\n  let bytes = 0\n\n  for (let index = 0; index < size; index++) {\n    const high = string.charCodeAt(index)\n\n    switch (true) {\n      // [0x0000, 0x007F] - [Basic Latin]\n      case high < 0x0080: {\n        bytes += 1\n        break\n      }\n\n      // [0x0080, 0x07FF] - [Latin-1 Supplement, NKo]\n      case high < 0x0800: {\n        bytes += 2\n        break\n      }\n\n      // [0x0800, 0xD7FF] - [Samaritan, Hangul Jamo Extended-B]\n      case high < 0xd800: {\n        bytes += 3\n        break\n      }\n\n      // [0xD800, 0xDBFF] - [High Surrogates, High Private Use Surrogates]\n      case high < 0xdc00: {\n        // Stepping into low surrogates territory. Here we should be using **two** code points.\n        const low = string.charCodeAt(++index)\n\n        // Followed by: [0xDC00, 0xDFFF] - [Low Surrogates]\n        if (index < size && low >= 0xdc00 && low <= 0xdfff) {\n          bytes += 4\n          break\n        } else {\n          throw new Error('Malformed Unicode string with missing or invalid low surrogate.')\n        }\n      }\n\n      // [0xDC00, 0xDFFF] - [Low Surrogates], but if we somehow get here, then the string is\n      // malformed, because here we get only **one** code point.\n      case high < 0xe000: {\n        throw new Error('Malformed Unicode string with invalid high surrogate.')\n      }\n\n      // [0xE000, 0xFFFF] - [Private Use Area, Specials]\n      default: {\n        bytes += 3\n        break\n      }\n    }\n  }\n\n  return bytes\n}\n","import type { Parser, Span } from '@types'\nimport { size } from '@utils/unicode'\n\n/**\n * Parses an *ASCII* string. For parsing Unicode strings, consider using `ustring`.\n *\n * @param match - String to parse\n *\n * @returns Parsed string\n */\nexport function string(match: string): Parser<string> {\n  return {\n    parse(input, pos) {\n      const nextPos = Math.min(pos + match.length, input.length)\n      const slice = input.substring(pos, nextPos)\n      const span: Span = [pos, nextPos]\n\n      switch (slice === match) {\n        case true: {\n          return {\n            isOk: true,\n            span,\n            pos: nextPos,\n            value: match\n          }\n        }\n\n        case false: {\n          return {\n            isOk: false,\n            span,\n            pos: nextPos,\n            expected: match\n          }\n        }\n      }\n    }\n  }\n}\n\n/**\n * Parses a Unicode string. For parsing ASCII-only strings, consider using `string`.\n *\n * @param match - String to parse\n *\n * @returns Parsed string\n */\nexport function ustring(match: string): Parser<string> {\n  return {\n    parse(input, pos) {\n      const nextPos = Math.min(pos + size(match), input.length)\n      const slice = input.substring(pos, nextPos)\n      const span: Span = [pos, nextPos]\n\n      switch (slice === match) {\n        case true: {\n          return {\n            isOk: true,\n            span,\n            pos: nextPos,\n            value: match\n          }\n        }\n\n        case false: {\n          return {\n            isOk: false,\n            span,\n            pos: nextPos,\n            expected: match\n          }\n        }\n      }\n    }\n  }\n}\n","import { string } from './string'\n\nimport { choice, error } from '@combinators'\nimport type { Parser } from '@types'\n\nconst EOL_UNIX = '\\n'\nconst EOL_NON_UNIX = '\\r\\n'\n\n/**\n * Only succeeds at the end of the line, either `\\n` or `\\r\\n`.\n *\n * @returns Matched line break character\n */\nexport function eol(): Parser<string> {\n  return error(choice(string(EOL_UNIX), string(EOL_NON_UNIX)), 'end of line')\n}\n","import type { Parser } from '@types'\n\n/**\n * Parses a string that matches a provided `re` regular expression. Returns the matched string, or\n * fails with an `expected` message.\n *\n * The regular expression must obey two simple rules:\n *\n * - It *does* use `g` flag. Flags like u and i are allowed and can be added if needed.\n * - It *doesn't* use `^` and `$` to match at the beginning or at the end of the text.\n *\n * If `g` flag is missing, it will be automatically injected. It's still better to always provide it\n * to avoid small performance penalty and clearly document the intention.\n *\n * @param rs - Regular expression\n * @param expected - Error message if the regular expression does not match input\n *\n * @returns Matched string\n */\nexport function regexp(rs: RegExp, expected: string): Parser<string> {\n  const re = rs.global ? rs : new RegExp(rs.source, rs.flags + 'g')\n\n  return {\n    parse(input, pos) {\n      // Reset RegExp index, because we abuse the 'g' flag.\n      re.lastIndex = pos\n\n      // `.exec` is actually a little bit faster than `.test`.\n      const result = re.exec(input)\n\n      if (result && result.index === pos) {\n        const [match] = result\n        const index = pos + match.length\n\n        return {\n          isOk: true,\n          span: [pos, index],\n          pos: index,\n          value: match\n        }\n      } else {\n        return {\n          isOk: false,\n          // TODO: Can this be improved? Zero-length span for this parser doesn't look helpful.\n          span: [pos, pos],\n          pos,\n          expected\n        }\n      }\n    }\n  }\n}\n","import { regexp } from './regexp'\n\nimport type { Parser } from '@types'\n\nconst LETTER_RE = /\\p{Letter}/gu\nconst LETTERS_RE = /\\p{Letter}+/gu\n\n/**\n * Parses a single alphabetical character. Unicode friendly.\n *\n * @returns Matched character.\n */\nexport function letter(): Parser<string> {\n  return regexp(LETTER_RE, 'letter')\n}\n\n/**\n * Parses a sequence of alphabetical characters. Unicode friendly.\n *\n * @returns Matched characters as a string.\n */\nexport function letters(): Parser<string> {\n  return regexp(LETTERS_RE, 'letters')\n}\n","import type { Parser } from '@types'\n\n/**\n * Ensures that none of the characters in the given string matches the current character.\n *\n * @param chars - A string of characters that current character should not match\n *\n * @returns Current character\n */\nexport function noneOf(chars: string): Parser<string> {\n  const charset = [...chars]\n\n  return {\n    parse(input, pos) {\n      if (input.length === pos) {\n        return {\n          isOk: false,\n          span: [pos, pos],\n          pos,\n          expected: 'noneOf @ reached the end of input'\n        }\n      }\n\n      const nextPos = pos + 1\n      const char = input.substring(pos, nextPos)\n\n      if (!charset.includes(char)) {\n        return {\n          isOk: true,\n          span: [pos, nextPos],\n          pos: nextPos,\n          value: char\n        }\n      }\n\n      return {\n        isOk: false,\n        span: [pos, pos],\n        pos: nextPos,\n        expected: `none of: ${charset.join(', ')}`\n      }\n    }\n  }\n}\n","import type { Parser } from '@types'\n\n/**\n * Simply resolves to `null`.\n *\n * @returns `null`.\n */\nexport function nothing(): Parser<null> {\n  return {\n    parse(_, pos) {\n      return {\n        isOk: true,\n        span: [pos, pos],\n        pos,\n        value: null\n      }\n    }\n  }\n}\n","import { regexp } from './regexp'\n\nimport type { Parser } from '@types'\n\nconst HEXADECIMAL_RE = /0[xX][0-9a-fA-F]+/g\nconst BINARY_RE = /0[bB][01]+/g\nconst OCTAL_RE = /0[oO][0-8]+/g\nconst WHOLE_RE = /(0|[1-9][0-9]*)/g\nconst INTEGER_RE = /-?(0|[1-9][0-9]*)/g\nconst FLOAT_RE = /-?[0-9]+\\.[0-9]+/g\n\n/**\n * Parses a hexadecimal number prefixed with `0x` or `0X`, e.g. `0xFF`, `0XFF`, `0xff`.\n *\n * @returns Parsed hexadecimal number as a decimal one\n */\nexport function hex(): Parser<number> {\n  return {\n    parse(input, pos) {\n      const result = regexp(HEXADECIMAL_RE, 'hexadecimal number').parse(input, pos)\n\n      switch (result.isOk) {\n        case true: {\n          return {\n            isOk: true,\n            span: [pos, result.pos],\n            pos: result.pos,\n            value: parseInt(result.value.slice(2), 16)\n          }\n        }\n\n        case false: {\n          return result\n        }\n      }\n    }\n  }\n}\n\n/**\n * Parses a binary number prefixed with `0b` or `0B`, e.g. `0b101`, `0B101`.\n *\n * @returns Parsed binary number as a decimal one\n */\nexport function binary(): Parser<number> {\n  return {\n    parse(input, pos) {\n      const result = regexp(BINARY_RE, 'binary number').parse(input, pos)\n\n      switch (result.isOk) {\n        case true: {\n          return {\n            isOk: true,\n            span: [pos, result.pos],\n            pos: result.pos,\n            value: parseInt(result.value.slice(2), 2)\n          }\n        }\n\n        case false: {\n          return result\n        }\n      }\n    }\n  }\n}\n\n/**\n * Parses an octal number prefixed with `0o` or `0O`, e.g. `0o420`, `0O420`.\n *\n * @returns Parsed octal number as a decimal one\n */\nexport function octal(): Parser<number> {\n  return {\n    parse(input, pos) {\n      const result = regexp(OCTAL_RE, 'octal number').parse(input, pos)\n\n      switch (result.isOk) {\n        case true: {\n          return {\n            isOk: true,\n            span: [pos, result.pos],\n            pos: result.pos,\n            value: parseInt(result.value.slice(2), 8)\n          }\n        }\n\n        case false: {\n          return result\n        }\n      }\n    }\n  }\n}\n\n/**\n * Parses a positive whole number without leading zeros, e.g. `0`, `7`, `420`.\n *\n * @returns Parsed whole number\n */\nexport function whole(): Parser<number> {\n  return {\n    parse(input, pos) {\n      const result = regexp(WHOLE_RE, 'whole number').parse(input, pos)\n\n      switch (result.isOk) {\n        case true: {\n          return {\n            isOk: true,\n            span: [pos, result.pos],\n            pos: result.pos,\n            value: parseInt(result.value, 10)\n          }\n        }\n\n        case false: {\n          return result\n        }\n      }\n    }\n  }\n}\n\n/**\n * Parses an integer number with an optional minus sign, e.g. `0`, `-7`, `420`.\n *\n * @returns Parsed integer number\n */\nexport function integer(): Parser<number> {\n  return {\n    parse(input, pos) {\n      const result = regexp(INTEGER_RE, 'integer number').parse(input, pos)\n\n      switch (result.isOk) {\n        case true: {\n          return {\n            isOk: true,\n            span: [pos, result.pos],\n            pos: result.pos,\n            value: parseInt(result.value, 10)\n          }\n        }\n\n        case false: {\n          return result\n        }\n      }\n    }\n  }\n}\n\n/**\n * Parses a float number with an optional minus sign, e.g. `0.25`, `-7.90`, `4.20`.\n *\n * Note: It doesn't handle floats with exponent parts.\n *\n * @returns Parsed float number\n */\nexport function float(): Parser<number> {\n  return {\n    parse(input, pos) {\n      const result = regexp(FLOAT_RE, 'float number').parse(input, pos)\n\n      switch (result.isOk) {\n        case true: {\n          return {\n            isOk: true,\n            span: [pos, result.pos],\n            pos: result.pos,\n            value: parseFloat(result.value)\n          }\n        }\n\n        case false: {\n          return result\n        }\n      }\n    }\n  }\n}\n","import type { Parser } from '@types'\n\n/**\n * Ensures that one of the characters in the given string matches the current character.\n *\n * @param chars - A string of characters that current character should match\n *\n * @returns Current character\n */\nexport function oneOf(chars: string): Parser<string> {\n  const charset = [...chars]\n\n  return {\n    parse(input, pos) {\n      if (input.length === pos) {\n        return {\n          isOk: false,\n          span: [pos, pos],\n          pos,\n          expected: 'oneOf @ reached the end of input'\n        }\n      }\n\n      const nextPos = pos + 1\n      const char = input.substring(pos, nextPos)\n\n      if (charset.includes(char)) {\n        return {\n          isOk: true,\n          span: [pos, nextPos],\n          pos: nextPos,\n          value: char\n        }\n      }\n\n      return {\n        isOk: false,\n        span: [pos, pos],\n        pos,\n        expected: `one of: ${charset.join(', ')}`\n      }\n    }\n  }\n}\n","import type { SucceedingParser } from '@types'\n\n/**\n * Simply returns the unparsed input as a string. Never fails.\n *\n * @returns Rest of the input as a string\n */\nexport function rest(): SucceedingParser<string> {\n  return {\n    parse(input, pos) {\n      return {\n        isOk: true,\n        span: [pos, input.length],\n        pos: input.length,\n        value: input.substring(pos)\n      }\n    }\n  }\n}\n","import { regexp } from './regexp'\n\nimport type { Parser } from '@types'\n\nconst WHITESPACE_REQUIRED_RE = /\\s+/g\n\n/**\n * Parses whitespace, either a single character or consecutive ones.\n *\n * @returns Matched whitespace character(s)\n */\nexport function whitespace(): Parser<string> {\n  return regexp(WHITESPACE_REQUIRED_RE, 'whitespace')\n}\n","import { choice } from './choice'\n\nimport { nothing } from '@parsers'\nimport type { Parser } from '@types'\n\n/**\n * Applies `parser`. Only fails if `parser` fails.\n *\n * @param parser - Parser to apply\n *\n * @returns Result of `parser` or `null`\n */\nexport function optional<T>(parser: Parser<T>): Parser<T | null> {\n  return choice(parser, nothing())\n}\n","import { many } from './many'\nimport { sequence } from './sequence'\n\nimport type { Parser } from '@types'\n\n/**\n * Parses *zero* or more occurrences of `parser`, separated by `sep`. Never fails.\n *\n * @param parser - Parser to apply\n * @param sep - Separating parser\n *\n * @returns List of values (without separator) returned by `parser`\n */\nexport function sepBy<T, S>(parser: Parser<T>, sep: Parser<S>): Parser<Array<T>> {\n  return {\n    parse(input, pos) {\n      // Run the parser once to get the first value.\n      const resultP = parser.parse(input, pos)\n\n      // If the parser succeeds, run the parser and separator parser many times.\n      if (resultP.isOk) {\n        const resultS = many(sequence(sep, parser)).parse(input, resultP.pos)\n        const values = [resultP.value]\n\n        // If the parsers succeed, concatenate the values sans the separator.\n        for (const [, value] of resultS.value) {\n          values.push(value)\n        }\n\n        return {\n          isOk: true,\n          span: [pos, resultS.pos],\n          pos: resultS.pos,\n          value: values\n        }\n      }\n\n      return {\n        isOk: true,\n        span: [pos, pos],\n        pos,\n        value: []\n      }\n    }\n  }\n}\n\n/**\n * Parses *one* or more occurrences of `parser`, separated by `sep`.\n *\n * @param parser - Parser to apply\n * @param sep - Separating parser\n *\n * @returns List of values (without separator) returned by `parser`\n */\nexport function sepBy1<T, S>(parser: Parser<T>, sep: Parser<S>): Parser<Array<T>> {\n  return {\n    parse(input, pos) {\n      // Run the parser once to get the first value.\n      const resultP = parser.parse(input, pos)\n\n      // If the parser succeeds, run the parser and separator parser many times.\n      if (resultP.isOk) {\n        const resultS = many(sequence(sep, parser)).parse(input, resultP.pos)\n        const values = [resultP.value]\n\n        // If the parsers succeed, concatenate the values sans the separator.\n        for (const [, value] of resultS.value) {\n          values.push(value)\n        }\n\n        return {\n          isOk: true,\n          span: [pos, resultS.pos],\n          pos: resultS.pos,\n          value: values\n        }\n      }\n\n      return {\n        isOk: false,\n        span: [pos, resultP.pos],\n        pos: resultP.pos,\n        expected: resultP.expected\n      }\n    }\n  }\n}\n","import { map } from './map'\nimport { sequence } from './sequence'\n\nimport type { Parser } from '@types'\n\nconst toLeft = <T1, T2>([left]: [T1, T2]): T1 => left\nconst toMiddle = <T1, T2, T3>([, middle]: [T1, T2, T3]): T2 => middle\nconst toRight = <T1, T2>([, right]: [T1, T2]): T2 => right\nconst toSides = <T1, T2, T3>([left, _, right]: [T1, T2, T3]): [T1, T3] => [left, right]\n\n/**\n * Takes exactly **two** parsers and applies them in order, returning the result of the leftmost\n * `p1` parser.\n *\n * @param p1 - First parser to apply\n * @param p2 - Second parser to apply\n *\n * @returns Result of the leftmost `p1` parser\n */\nexport function takeLeft<T1, T2>(p1: Parser<T1>, p2: Parser<T2>): Parser<T1> {\n  return map(sequence(p1, p2), toLeft)\n}\n\n/**\n * Takes exactly **three** parsers and applies them in order, returning the result of the `p2`\n * parser in the middle.\n *\n * @param p1 - First parser to apply\n * @param p2 - Second parser to apply\n * @param p3 - Third parser to apply\n *\n * @returns Result of the `p2` parser in the middle\n */\nexport function takeMid<T1, T2, T3>(p1: Parser<T1>, p2: Parser<T2>, p3: Parser<T3>): Parser<T2> {\n  return map(sequence(p1, p2, p3), toMiddle)\n}\n\n/**\n * Takes exactly **two** parsers and applies them in order, returning the result of the rightmost\n * `p2` parser.\n *\n * @param p1 - First parser to apply\n * @param p2 - Second parser to apply\n *\n * @returns Result of the rightmost `p2` parser\n */\nexport function takeRight<T1, T2>(p1: Parser<T1>, p2: Parser<T2>): Parser<T2> {\n  return map(sequence(p1, p2), toRight)\n}\n\n/**\n * Takes exactly **three** parsers and applies them in order, returning a tuple of the results of\n * `p1` and `p3` parsers.\n *\n * @param p1 - First parser to apply\n * @param p2 - Second parser to apply\n * @param p3 - Third parser to apply\n *\n * @returns Results of `p1` and `p3` parsers as a tuple\n */\nexport function takeSides<T1, T2, T3>(\n  p1: Parser<T1>,\n  p2: Parser<T2>,\n  p3: Parser<T3>\n): Parser<[T1, T3]> {\n  return map(sequence(p1, p2, p3), toSides)\n}\n","import type { Parser } from '@types'\n\n/**\n * Applies source `parser`, collects its output, and stops after `terminator` parser succeeds.\n *\n * @param parser - Parser to apply\n * @param terminator - Terminating parser to stop after\n *\n * @returns Tuple of values collected by `parser` and `terminator`\n */\nexport function takeUntil<T, S>(parser: Parser<T>, terminator: Parser<S>): Parser<[Array<T>, S]> {\n  return {\n    parse(input, pos) {\n      const values: Array<T> = []\n      let nextPos = pos\n\n      while (true) {\n        const resultT = terminator.parse(input, nextPos)\n\n        switch (resultT.isOk) {\n          // If ok, then we stumbled upon a terminating parser, so push final matches, and then\n          // return accumulated values.\n          case true: {\n            return {\n              isOk: true,\n              span: [pos, resultT.pos],\n              pos: resultT.pos,\n              value: [values, resultT.value]\n            }\n          }\n\n          // Otherwise try to run source parser and push results into `values`.\n          // If it fails, then return early and stop parsing.\n          case false: {\n            const resultP = parser.parse(input, nextPos)\n\n            if (resultP.isOk) {\n              values.push(resultP.value)\n              nextPos = resultP.pos\n              continue\n            }\n\n            return resultP\n          }\n        }\n      }\n    }\n  }\n}\n\n/**\n * Applies source `parser`, ignores its output, and stops after `terminator` parser succeeds.\n *\n * @param parser - Parser to apply\n * @param terminator - Terminating parser to stop after\n *\n * @returns Value of `terminator` parser\n */\nexport function skipUntil<T, S>(parser: Parser<T>, terminator: Parser<S>): Parser<S> {\n  return {\n    parse(input, pos) {\n      let nextPos = pos\n\n      while (true) {\n        const resultT = terminator.parse(input, nextPos)\n\n        switch (resultT.isOk) {\n          // If ok, then we stumbled upon a terminating parser, so return its value.\n          case true: {\n            return {\n              isOk: true,\n              span: [pos, resultT.pos],\n              pos: resultT.pos,\n              value: resultT.value\n            }\n          }\n\n          // Otherwise try to run source parser *ignoring* its results.\n          // If it fails, then return early and stop parsing.\n          case false: {\n            const resultP = parser.parse(input, nextPos)\n\n            if (resultP.isOk) {\n              nextPos = resultP.pos\n              continue\n            }\n\n            return resultP\n          }\n        }\n      }\n    }\n  }\n}\n","import type { Parser, ToParser } from '@types'\n\n/**\n * Context provided to a callback for producing conditional/chained parser.\n *\n * @internal\n */\ninterface Context<T> {\n  value: T\n  input: string\n  pos: number\n}\n\n/**\n * Creates chained, context-aware `parser`, that may depend on the output of the `context` parser.\n *\n * @param context - Source (context) parser\n * @param parser - Function that returns a new parser\n *\n * @returns New parser\n */\nexport function when<T, R extends Parser<unknown>>(\n  context: Parser<T>,\n  parser: (ctx: Context<T>) => R\n): ToParser<R> {\n  return {\n    parse(input, pos) {\n      const result = context.parse(input, pos)\n\n      switch (result.isOk) {\n        case true: {\n          return parser({ value: result.value, pos: result.pos, input }).parse(input, result.pos)\n        }\n\n        case false: {\n          return result\n        }\n      }\n    }\n  } as ToParser<R>\n}\n"]}