A parsing buffer class, note for convenience and brevity, a lot of the functions here are pre-bound using public fields, this means they can be used like C# style delegates, which is handy for constructive parsing, as well as providing easy access to nested parsing/parsing combinator style constructs.

Constructors

  • Construct this with a buffer, initial offset into the buffer and an end offset.

    Parameters

    • buffer: Uint8Array

      Input datasource.

    • OptionalinitialOffset: number

      The initial offset in the buffer to start parsing from (will default to 0)

    • OptionalendOffset: number

      The (exclusive) offset to stop parsing/treat as the end of the buffer (will default to the buffer's length)

    Returns default

Properties

buffer: Uint8Array

Input datasource.

end: number

Accessors

  • get address(): number
  • Get the current cursor address of the stream, relative the initial offset.

    Returns number

    The cursor relative the initial offset, instead of absolute the start of the buffer.

  • get cursor(): number
  • Get the current position in the buffer this parser is at.

    Returns number

    The cursor position in bytes.

  • get finished(): boolean
  • Has this parsing buffer hit or parsed its end mark?

    Returns boolean

    True if the parsing buffer has passed its end mark.

  • get unfinished(): boolean
  • Has this parsing buffer hit or parsed its end mark?

    Returns boolean

    True if the parsing buffer is not passed its end mark.

Methods

  • Begin a parsing cursor transaction, that allows us to rollback to a parse point.

    Returns void

  • Match against a single char and move the cursor forwards if so.

    Parameters

    • value: number

      A char in the ascii range that's been encoded into a number.

    Returns boolean

    True if the match was successful.

  • In order of specification, try each operation in turn until one succeeds, rewinding the cursor on each failure.

    Parameters

    • ...choices: () => boolean[]

      The sequence to run.

    Returns boolean

    True if any operations in the sequence return true. False otherwise.

  • Commit a parsing cursor transaction, ending the transaction at the current cursor and accepting it.

    Returns void

  • Looks at the current cursor value and advance.

    Returns undefined | number

    the current byte value or undefined if past end of the buffer.

  • Match an unsigned hex number

    Returns boolean

    True if a hex number has been parsed at the current location, false if none has been found and the cursor has been rewound

  • Match an unsigned hex number with a C style prefix (0x 0X)

    Returns boolean

    True if a hex number has been parsed at the current location, false if none has been found and the cursor has been rewound

  • Match against any one of the chars in a token and return the index in the token.

    Parameters

    • encoded: Uint8Array

    Returns undefined | number

    True if the match was successful.

  • Match an integer

    Returns boolean

    True if an integer has been parsed at the current location, false if no integer has been found and the cursor has been rewound

  • Try and match a single matching function, with rewind semantics. (i.e. return to the initial cursor on failure).

    Parameters

    • against: (buffer: Uint8Array, cursor: number, end: number) => undefined | number

      The matching function to run.

    Returns boolean

    True if a match is found. False otherwise.

  • Looks at the current cursor value without advancing.

    Returns undefined | number

    the current byte value or undefined if past end of the buffer.

  • Read an unsigned integer from UTF-8 or ASCII.

    Rewinds the stream if no match is found.

    Returns undefined | number

    The number, or undefined if no match is found.

  • Read a real valued number from the current stream, supports scientific notation.

    Rewinds the stream if no match is found.

    Returns undefined | number

    The number, or undefined if no match is found.

  • Read an unsigned integer from UTF-8 or ASCII.

    Rewinds the stream if no match is found.

    Returns undefined | number

    The number, or undefined if no match is found.

  • Match a real

    Returns boolean

    True if an integer has been parsed at the current location, false if no integer has been found and the cursor has been rewound

  • Reinitialize this with a new buffer, initial offset and end offset.

    Parameters

    • buffer: Uint8Array

      The buffer to initialize this with.

    • initialOffset: number

      The initial offset in bytes to start the cursor at in the buffer.

    • endOffset: number

      The end offset of the buffer in bytes, relative to the start of the buffer.

    Returns void

  • Rollback a parsing transaction, moving the cursor back to its value at the beginning of a transaction.

    Returns void

  • Try and run a sequence of parsing operations, with rewind semantics (i.e. return to the initial cursor on failure).

    Parameters

    • ...against: () => boolean[]

      The sequence to run.

    Returns boolean

    True if all operations in the sequence return true in order. False otherwise.

  • Try and run a sequence of parsing operations, with rewind semantics (i.e. return to the initial cursor on failure).

    Also eats whitespace before each match.

    Parameters

    • ...against: () => boolean[]

      The sequence to run.

    Returns boolean

    True if all operations in the sequence return true in order. False otherwise.

  • Match an encoded token at the current cursor, and rewind if it's not matched.

    Parameters

    • encoded: Uint8Array

      The encoded token.

    Returns boolean

    True if the token is matched.

  • Match an unsigned number.

    Returns boolean

    True if an integer has been parsed at the current location, false if no integer has been found and the cursor has been rewound

  • Eat whitespace from the stream.

    Returns boolean

    Always true, but this allows this to be used as a matcher