import ByteBitSet from "./char_bit_set.js"; import { EncodedToken, EncodedUnicodeCharacter as EncodedAsciiCharacter } from "./token_parsing.js"; /** * Inteface encoding a function that matches a parse. */ export interface IParsingMatcher { match(buffer: Uint8Array, cursor: number, endCursor: number): number | undefined; } /** * 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. */ export default class ParsingBuffer { readonly buffer: Uint8Array; private cursor_; private lineCount_; private initialOffset_; private rewindStack_; readonly end: number; /** * Get the current linecount for this parser. * * @return {number} The line count. */ get lineCount(): number; /** * Get the current position in the buffer this parser is at. * * @return {number} The cursor position in bytes. */ get cursor(): number; /** * Has this parsing buffer hit or parsed its end mark? * * @return {boolean} True if the parsing buffer is not passed its end mark. */ get unfinished(): boolean; /** * Has this parsing buffer hit or parsed its end mark? * * @return {boolean} True if the parsing buffer has passed its end mark. */ get finished(): boolean; /** * Get the current cursor address of the stream, relative the initial offset. * * @return {number} The cursor relative the initial offset, * instead of absolute the start of the buffer. */ get address(): number; /** * Reinitialize this with a new buffer, initial offset and end offset. * * @param buffer The buffer to initialize this with. * @param initialOffset The initial offset in bytes to start the cursor at in * the buffer. * @param endOffset The end offset of the buffer in bytes, relative to the start * of the buffer. */ reinit(buffer: Uint8Array, initialOffset: number, endOffset: number): void; /** * Construct this with a buffer, initial offset into the buffer and an end offset. * * @param buffer Input datasource. * @param initialOffset The initial offset in the buffer to start parsing from (will default to 0) * @param endOffset The (exclusive) offset to stop parsing/treat as the end of the buffer * (will default to the buffer's length) */ constructor(buffer: Uint8Array, initialOffset?: number, endOffset?: number); /** * Begin a parsing cursor transaction, that allows us to rollback to a parse point. */ begin: () => void; /** * Move the cursor forwards while a particular char isn't found. * * @param chars */ whileNot: (chars: ByteBitSet) => void; /** * Commit a parsing cursor transaction, ending the * transaction at the current cursor and accepting it. * * @return {void} */ commit: () => void; /** * Rollback a parsing transaction, moving the cursor * back to its value at the beginning of a transaction. * * @return {void} */ rollback: () => void; /** * Eat whitespace from the stream. * * @return {boolean} Always true, but this allows this to be used as a matcher */ whitespace: () => boolean; /** * Match an encoded token at the current cursor, * and rewind if it's not matched. * * @param encoded The encoded token. * @return {boolean} True if the token is matched. */ token: (encoded: EncodedToken) => boolean; /** * Match an integer * * @return {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 */ integer: () => boolean; /** * Try and match a single matching function, with rewind semantics. * (i.e. return to the initial cursor on failure). * * @param against The matching function to run. * @return {boolean} True if a match is found. False otherwise. */ match: (against: (buffer: Uint8Array, cursor: number, end: number) => number | undefined) => boolean; /** * Try and run a sequence of parsing operations, with rewind semantics * (i.e. return to the initial cursor on failure). * * @param against The sequence to run. * @return {boolean} True if all operations in the sequence return true in order. False otherwise. */ sequence: (...against: (() => boolean)[]) => boolean; /** * 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. * * @param against The sequence to run. * @return {boolean} True if all operations in the sequence return true in order. False otherwise. */ sequencews: (...against: (() => boolean)[]) => boolean; /** * In order of specification, try each operation in turn until one succeeds, * rewinding the cursor on each failure. * * @param choices The sequence to run. * @return {boolean} True if any operations in the sequence return true. False otherwise. */ choice: (...choices: (() => boolean)[]) => boolean; /** * Match an unsigned number. * * @return {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 */ unsigned: () => boolean; /** * Match against a single char and move the cursor forwards if so. * * @param value A char in the ascii range that's been encoded into a number. * @return {boolean} True if the match was successful. */ char: (value: EncodedAsciiCharacter) => boolean; /** * Match against any one of the chars in a token and return the index in the token. * * @param encoded The encoded token to match against * @return {number | undefined} True if the match was successful. */ indexof: (encoded: EncodedToken) => number | undefined; /** * Match an unsigned hex number * * @return {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 */ hex: () => boolean; /** * Match an unsigned hex number with a C style prefix (0x 0X) * * @return {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 */ hexc: () => boolean; /** * Match a real * * @return {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 */ real: () => boolean; /** * Move the cursor forwards 1 byte */ step(): void; /** * Looks at the current cursor value without advancing. * * @return {number | undefined} the current byte value or undefined if past end of the buffer. */ peek(): number | undefined; /** * Looks at the current cursor value and advance. * * @return {number | undefined} the current byte value or undefined if past end of the buffer. */ get(): number | undefined; /** * Read a real valued number from the current stream, supports scientific notation. * * Rewinds the stream if no match is found. * * @return {number | undefined} The number, or undefined if no match is found. */ readReal: () => number | undefined; /** * Read an unsigned integer from UTF-8 or ASCII. * * Rewinds the stream if no match is found. * * @return {number | undefined} The number, or undefined if no match is found. */ readUnsigned: () => number | undefined; /** * Read an unsigned integer from UTF-8 or ASCII. * * Rewinds the stream if no match is found. * * @return {number | undefined} The number, or undefined if no match is found. */ readInteger: () => number | undefined; } //# sourceMappingURL=parsing_buffer.d.ts.map