json-p3
Version:
JSONPath, JSON Pointer and JSON Patch
75 lines (74 loc) • 2.56 kB
TypeScript
/** A lexer that accepts additional, non-standard tokens. */
import { JSONPathEnvironment } from "./environment";
import { Token, TokenKind } from "./token";
/**
* JSONPath lexical scanner.
*
* Lexer state is shared between this class and the current state function. A
* new _Lexer_ instance is automatically created every time a path is tokenized.
*
* Use {@link tokenize} to get an array of {@link Token}'s for a JSONPath query.
*/
declare class Lexer {
#private;
readonly environment: JSONPathEnvironment;
readonly path: string;
/**
* Filter nesting level.
*/
filterLevel: number;
/**
* A running count of parentheses for each, possibly nested, function call.
*
* If the stack is empty, we are not in a function call. Remember that
* function arguments can use arbitrarily nested in parentheses.
*/
funcCallStack: number[];
/**
* A stack of parentheses and square brackets used to check for balanced
* brackets.
*/
bracketStack: Array<[string, number]>;
/** Tokens resulting from tokenizing a JSONPath query. */
tokens: Token[];
/**
* @param path - A JSONPath query.
*/
constructor(environment: JSONPathEnvironment, path: string);
get pos(): number;
get start(): number;
run(): void;
emit(t: TokenKind): void;
next(): string;
ignore(): void;
backup(): void;
peek(): string;
peekMatch(pattern: RegExp): boolean;
accept(valid: Set<string>): boolean;
acceptMatch(pattern: RegExp): boolean;
acceptRun(valid: Set<string>): boolean;
acceptMatchRun(pattern: RegExp): boolean;
ignoreWhitespace(): boolean;
error(msg: string): void;
}
/**
* Return a lexer for _path_ and an array to be populated with Tokens.
*
* `lexer.run()` must be called to populate the returned tokens array.
*
* You probably want to use {@link tokenize} instead of _lex_. This function
* is mostly for internal use, where we want to test the state of the returned
* _lexer_ after tokens have been populated.
*
* @param path - A JSONPath query.
* @returns A two-tuple containing a lexer for _path_ and an array to populate
* with tokens.
*/
export declare function lex(environment: JSONPathEnvironment, path: string): [Lexer, Token[]];
/**
* Scan _path_ and return an array of tokens to be parsed by the parser.
* @param path - A JSONPath query.
* @returns Tokens to be parsed by the parser.
*/
export declare function tokenize(environment: JSONPathEnvironment, path: string): Token[];
export {};