@scinorandex/sparse
Version:
Yet another parser generator
114 lines (113 loc) • 4.28 kB
TypeScript
import { Slex, Token } from "@scinorandex/slex";
import { Stack } from "./utils/Stack";
import { Result } from "./utils/Result";
import { Production } from "./meta/common";
export type TableAction = {
type: "shift" | "reduce" | "goto";
value: number;
};
export declare class TableState {
readonly actions: Map<string, TableAction>;
constructor(actions?: Map<string, TableAction>);
getTerminalAction(terminal: string): TableAction | undefined;
getVariableAction(variable: string): TableAction | undefined;
toJSObject(): {
[k: string]: TableAction;
};
static fromJSObject(json: ReturnType<TableState["toJSObject"]>): TableState;
}
type ReducerType<TokenType, Metadata, Node> = (newInput: {
bag: any;
name: string | null;
}, oldInput: {
input: LR1StackSymbol<TokenType, Metadata, Node>[];
index: number;
}) => Node;
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: ReducerType<TokenType, Metadata, 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: ReducerType<TokenType, Metadata, 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: ReducerType<TokenType, Metadata, 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 {};