@light0x00/parser-generator
Version:
A Parser Generator
124 lines (123 loc) • 4.16 kB
TypeScript
import { Production, Terminal, Symbol, NonTerminal, ActionGrammar, RawActionGrammar, TraitsTable } from "../Definition";
import { ILexer, ASTree } from "@light0x00/parser-definition";
export declare class Item {
prod: Production;
private _dot;
lookaheadSet: Set<Terminal>;
constructor(production: Production, dot?: number, lookaheadSet?: Set<Terminal>);
get dotPos(): number;
hasNext(): boolean;
nextSymbol(): Symbol;
nextItem(): Item;
toString(): string;
equals(other: Item): boolean;
isSameCoreWith(other: Item): boolean;
private hashCodeCache;
hashCode(): string;
}
export declare class ItemSet extends Array<Item> {
added: Map<string, Item>;
items: Item[];
constructor(...items: Item[]);
contains(item: Item): boolean;
pushOrMerge(item: Item): void;
push(item: Item): number;
toString(): string;
equals(other: ItemSet): boolean;
getNextSymbols(): Set<Symbol>;
}
export declare class State extends ItemSet {
id: number;
mapping: Map<Symbol, State>;
constructor(id: number, items: Item[]);
toString(): string;
/**
* 添加后继状态
* 添加当前状态输入「指定符号」后去往的「后继状态」
*
* 原则上一个状态对于同一符号只能有一个后继状态,如果重复将抛出异常
* @param symbol 输入符号
* @param nextState 后继状态
*/
addNextState(symbol: Symbol, nextState: State): void;
getNextState(symbol: Symbol): State | undefined;
}
export declare class StateSet extends Array<State> {
mapping: Map<State, Map<Symbol, State>>;
getExisting(state: ItemSet): null | State;
satrtState(): State;
}
export interface AutomataTools {
GOTO(I: ItemSet, inputSymbol: Symbol): ItemSet;
closure(itemSet: ItemSet): ItemSet;
getStartState(grammar: ActionGrammar): State;
}
/**
*
* 将二维数组形式的产生式集(Symbol[][]) 转为带有编号的Production集合,并赋予指定的非终结符
*
* 一个非终结符的产生式集可视为一个Symbol二维数组,
* 为了便于实现LR分析表,需要将这个二维数组转为Production集合,
* 且为每个Production赋予一个编号.
*/
export declare class AugmentedGrammar extends ActionGrammar {
constructor(rawGrammar: RawActionGrammar, traits: TraitsTable);
afterConstruct(): void;
get prodOfStartNT(): Production;
}
export declare abstract class Operation {
abstract equals(obj: Object): boolean;
isReduce(): this is Reduce;
isShift(): this is Shift;
isGoto(): this is Goto;
isAccept(): this is Accept;
}
export declare class Shift extends Operation {
nextState: State;
prec: number;
assoc: boolean;
constructor(nextState: State, prec?: number, assoc?: boolean);
setIfLarger(prec: number, assoc: boolean): void;
toString(): string;
equals(obj: Object): boolean;
isShift(): boolean;
}
export declare class Goto extends Operation {
nextState: State;
constructor(nextState: State);
toString(): string;
equals(obj: Object): boolean;
isGoto(): boolean;
}
export declare class Reduce extends Operation {
prod: Production;
prec: number;
leftAssoc: boolean;
constructor(prod: Production, prec?: number, leftAssoc?: boolean);
toString(): string;
equals(obj: Object): boolean;
isReduce(): boolean;
}
export declare class Accept extends Operation {
nextState: State;
constructor(nextState: State);
equals(obj: Object): boolean;
toString(): string;
isAccept(): boolean;
}
export declare class ParsingTable {
private parsingTable;
get table(): Map<State, Map<Symbol, Operation>>;
put(state: State, symbol: Symbol, op: Operation): void;
has(state: State, symbol: Symbol, op: Operation): boolean;
get(state: State, symbol: Symbol): Operation | undefined;
getExpectedSymbols(state: State): Symbol[];
getExpectedTokens(state: State): Symbol[];
}
export interface IParser {
parse(lexer: ILexer): ASTree;
}
export declare class ProdsInjector {
prodIdCounter: number;
inject(nt: NonTerminal, rawProds: Symbol[][]): void;
}