@scinorandex/sparse
Version:
Yet another parser generator
103 lines (102 loc) • 4.04 kB
TypeScript
import { Slex, Token } from "@scinorandex/slex";
import { Production } from "./grammarParser";
import { Stack } from "./utils/Stack";
import { Result } from "./utils/Result";
export type TableAction = {
type: "shift" | "reduce" | "goto";
value: number;
};
export declare class TableState {
readonly actions: Map<string, TableAction>;
constructor();
getTerminalAction(terminal: string): TableAction | undefined;
getVariableAction(variable: string): TableAction | undefined;
}
export type LR1StackSymbol<TokenType, Metadata, Node> = {
type: "token";
token: Token<TokenType, Metadata>;
} | {
type: "node";
node: Node;
};
export declare class Sparse<TokenType, Metadata, Node> {
readonly options: {
productions: Production[];
states: TableState[];
toStringifiedTokenType: (tokenType: TokenType) => string;
};
constructor(options: {
productions: Production[];
states: TableState[];
toStringifiedTokenType: (tokenType: TokenType) => string;
});
static tryFromProductions<TokenType, Metadata, Node>(options: {
productions: Production[];
toStringifiedTokenType: (tokenType: TokenType) => string;
}): Result<Sparse<TokenType, Metadata, Node>>;
static fromProductions<TokenType, Metadata, Node>(options: {
productions: Production[];
toStringifiedTokenType: (tokenType: TokenType) => string;
}): Sparse<TokenType, Metadata, Node>;
generate(lexer: ReturnType<Slex<TokenType, Metadata>["generate"]>, options: {
reducer: (input: LR1StackSymbol<TokenType, Metadata, Node>[], productionIndex: number) => Node;
recover?: ParserRecoveryFunction<TokenType, Metadata, Node>;
}): LR1Parser<TokenType, Metadata, Node>;
}
export type ParserRecoveryFunction<TokenType, Metadata, Node> = (options: {
lexer: ReturnType<Slex<TokenType, Metadata>["generate"]>;
statesStack: Stack<number>;
symbolsStack: Stack<LR1StackSymbol<TokenType, Metadata, Node>>;
isSafe: () => boolean;
addError: (reason: string) => void;
crash: (reason: string) => {
success: false;
reason: string;
};
finish: (options?: {
newToken: Token<TokenType, Metadata>;
}) => {
success: true;
token?: Token<TokenType, Metadata>;
};
states: TableState[];
}) => {
success: true;
token?: Token<TokenType, Metadata>;
} | {
success: false;
reason: string;
};
export type ParserError<TokenType, Metadata> = {
token: Token<TokenType, Metadata>;
message: string;
};
export type ParserResult<TokenType, Metadata, Node> = {
result: Node | null;
errors: ParserError<TokenType, Metadata>[];
};
declare class LR1Parser<TokenType, Metadata, Node> {
readonly options: {
productions: Production[];
states: TableState[];
toStringifiedTokenType: (tokenType: TokenType) => string;
};
readonly reducer: (input: LR1StackSymbol<TokenType, Metadata, Node>[], productionIndex: number) => Node;
readonly recover: ParserRecoveryFunction<TokenType, Metadata, Node> | null;
readonly lexer: ReturnType<Slex<TokenType, Metadata>["generate"]>;
statesStack: Stack<number>;
symbolsStack: Stack<LR1StackSymbol<TokenType, Metadata, Node>>;
exceptions: ParserError<TokenType, Metadata>[];
constructor(options: {
productions: Production[];
states: TableState[];
toStringifiedTokenType: (tokenType: TokenType) => string;
}, reducer: (input: LR1StackSymbol<TokenType, Metadata, Node>[], productionIndex: number) => Node, recover: ParserRecoveryFunction<TokenType, Metadata, Node> | null, lexer: ReturnType<Slex<TokenType, Metadata>["generate"]>);
parse(): ParserResult<TokenType, Metadata, Node>;
}
export declare class LR1ParserGraveError<TokenType, Metadata> extends Error {
readonly reason: string;
readonly currentToken: Token<TokenType, Metadata>;
constructor(reason: string, currentToken: Token<TokenType, Metadata>, err?: Error);
}
export {};