diff --git a/lib/typings/api.d.ts b/lib/typings/api.d.ts new file mode 100644 index 0000000..7bcd391 --- /dev/null +++ b/lib/typings/api.d.ts @@ -0,0 +1,408 @@ +import gp from "./generated-parser"; + +export = peg; +export as namespace peg; + +declare namespace peg { + + type Grammar = parser.ast.Grammar; + type GeneratedParser = gp.API; + type SyntaxError = gp.SyntaxErrorConstructor; + + /** + * PEG.js version (uses semantic versioning). + */ + const VERSION: string; + + /** + * Thrown when the grammar contains an error. + */ + class GrammarError { + + name: string; + message: string; + location?: gp.SourceLocation; + + constructor( message: string, location?: gp.SourceLocation ); + + } + + /** + * A generated PEG.js parser to parse PEG.js grammar source's. + */ + namespace parser { + + /** + * Interface's that describe the abstact sytax tree used by PEG.js + */ + namespace ast { + + /** + * Unlike `parser.ast.INode` this interface represent's all PEG.js node's. + */ + type Node + = Grammar + | Initializer + | Rule + | Named + | Expression; + + /** + * Basic representation of a PEG.js node. + */ + interface INode { + + type: string; + location: gp.SourceLocation; + + } + + interface Grammar extends INode { + + // Default properties + + type: "grammar"; + initializer?: Initializer; + rules: Rule[]; + + // Added by Bytecode generator + + consts?: string[]; + + // Added by JavaScript generator + + code?: string; + + } + + interface Initializer extends INode { + + type: "initializer"; + code: string; + + } + + interface Rule extends INode { + + // Default properties + + type: "rule", + name: string; + expression: Named | Expression; + + // Added by bytecode generator + + bytecode?: number[]; + + } + + interface Named extends INode { + + type: "named"; + name: string; + expression: Expression; + + } + + type Expression + = ChoiceExpression + | ActionExpression + | SequenceExpression + | LabeledExpression + | PrefixedExpression + | SuffixedExpression + | PrimaryExpression; + + interface ChoiceExpression extends INode { + + type: "choice"; + alternatives: ( + ActionExpression + | SequenceExpression + | LabeledExpression + | PrefixedExpression + | SuffixedExpression + | PrimaryExpression + )[]; + + } + + interface ActionExpression extends INode { + + type: "action"; + expression: ( + SequenceExpression + | LabeledExpression + | PrefixedExpression + | SuffixedExpression + | PrimaryExpression + ); + code: string; + + } + + interface SequenceExpression extends INode { + + type: "sequence", + elements: ( + LabeledExpression + | PrefixedExpression + | SuffixedExpression + | PrimaryExpression + )[]; + + } + + interface LabeledExpression extends INode { + + type: "labeled"; + label: string; + expression: ( + PrefixedExpression + | SuffixedExpression + | PrimaryExpression + ); + + } + + interface PrefixedExpression extends INode { + + type: "text" | "simple_and" | "simple_not"; + expression: SuffixedExpression | PrimaryExpression; + + } + + interface SuffixedExpression extends INode { + + type: "optional" | "zero_or_more" | "one_or_more"; + expression: PrimaryExpression; + + } + + type PrimaryExpression + = LiteralMatcher + | CharacterClassMatcher + | AnyMatcher + | RuleReferenceExpression + | SemanticPredicateExpression + | GroupExpression; + + interface LiteralMatcher extends INode { + + type: "literal"; + value: string; + ignoreCase: boolean; + + } + + interface CharacterClassMatcher extends INode { + + type: "class"; + parts: ( string[] | string )[]; + inverted: boolean; + ignoreCase: boolean; + + } + + interface AnyMatcher extends INode { + + type: "any"; + + } + + interface RuleReferenceExpression extends INode { + + type: "rule_ref"; + name: string; + + } + + interface SemanticPredicateExpression extends INode { + + type: "semantic_and" | "semantic_not"; + code: string; + + } + + interface GroupExpression extends INode { + + type: "group"; + expression: LabeledExpression | SequenceExpression; + + } + + } + + const SyntaxError: SyntaxError; + function parse( input: string, options?: gp.IOptions ): Grammar; + + } + + namespace compiler { + + type FormatOptions = "amd" | "bare" | "commonjs" | "es" | "globals" | "umd"; + type OptimizeOptions = "size" | "speed"; + type OutputOptions = "parser" | "source"; + + interface ICompilerOptions { + + [ key: string ]: any; + allowedStartRules?: string[]; + cache?: boolean; + dependencies?: { [ name: string ]: string; }; + exportVar?: string; + format?: FormatOptions; + header?: string | string[]; + optimize?: OptimizeOptions; + output?: T; + trace?: boolean; + + } + + interface ICompilerPassOptions extends ICompilerOptions { + + allowedStartRules: string[]; + cache: boolean; + dependencies: { [ name: string ]: string; }; + exportVar: string; + format: FormatOptions; + header: string | string[]; + optimize: OptimizeOptions; + output: OutputOptions; + trace: boolean; + + } + + interface ICompilerPass { + + ( node: Grammar ): void; + ( node: Grammar, options: ICompilerPassOptions ): void; + + } + + interface IPassesMap { + + [ type: string ]: ICompilerPass[]; + + } + + namespace visitor { + + interface Visitor { + + ( node: Node, ...args ): R; + + } + + interface VisitorMap { + + [ type: string ]: any; + grammar( node: Grammar, ...args ): R; + initializer( node: parser.ast.Initializer, ...args ): R; + rule( node: parser.ast.Rule, ...args ): R; + named( node: parser.ast.Named, ...args ): R; + choice( node: parser.ast.ChoiceExpression, ...args ): R; + action( node: parser.ast.ActionExpression, ...args ): R; + sequence( node: parser.ast.SequenceExpression, ...args ): R; + labeled( node: parser.ast.LabeledExpression, ...args ): R; + text( node: parser.ast.PrefixedExpression, ...args ): R; + simple_and( node: parser.ast.PrefixedExpression, ...args ): R; + simple_not( node: parser.ast.PrefixedExpression, ...args ): R; + optional( node: parser.ast.SuffixedExpression, ...args ): R; + zero_or_more( node: parser.ast.SuffixedExpression, ...args ): R; + one_or_more( node: parser.ast.SuffixedExpression, ...args ): R; + literal( node: parser.ast.LiteralMatcher, ...args ): R; + class( node: parser.ast.CharacterClassMatcher, ...args ): R; + any( node: parser.ast.AnyMatcher, ...args ): R; + rule_ref( node: parser.ast.RuleReferenceExpression, ...args ): R; + semantic_and( node: parser.ast.SemanticPredicateExpression, ...args ): R; + semantic_not( node: parser.ast.SemanticPredicateExpression, ...args ): R; + group( node: parser.ast.GroupExpression, ...args ): R; + + } + + function build( functions: VisitorMap ): Visitor; + + } + + namespace passes { + + namespace check { + + function reportUndefinedRules( ast: Grammar, options: ICompilerPassOptions ): void; + function reportDuplicateRules( ast: Grammar ): void; + function reportDuplicateLabels( ast: Grammar ): void; + function reportInfiniteRecursion( ast: Grammar ): void; + function reportInfiniteRepetition( ast: Grammar ): void; + + } + + namespace transform { + + function removeProxyRules( ast: Grammar, options: ICompilerPassOptions ): void; + + } + + namespace generate { + + function generateBytecode( ast: Grammar ): void; + function generateJS( ast: Grammar, options: ICompilerPassOptions ): void; + + } + + } + + /** + * Generate's a parser from the PEG.js AST and returns it. + */ + function compile( ast: Grammar, passes: IPassesMap, options?: ICompilerOptions ): GeneratedParser | string; + + /** + * Generate's a parser from the PEG.js AST, then evaluates's the source before returning the parser object. + */ + function compile( ast: Grammar, passes: IPassesMap, options?: ICompilerOptions<"parser"> ): GeneratedParser; + + /** + * Generate's a parser from the PEG.js AST and returns the JavaScript based source. + */ + function compile( ast: Grammar, passes: IPassesMap, options?: ICompilerOptions<"source"> ): string; + + } + + interface IBuildConfig { + + parser: GeneratedParser; + passes: compiler.IPassesMap; + + } + + interface IPlugin { + + [ key: string ]: any; + use( config: IBuildConfig ): void; + use( config: IBuildConfig, options: IBuildOptions ): void; + + } + + interface IBuildOptions extends compiler.ICompilerOptions { + + plugins?: IPlugin[]; + + } + + /** + * Generate's a parser from the PEG.js grammar and returns it. + */ + function generate( grammar: string, options?: IBuildOptions ): GeneratedParser | string; + + /** + * Generate's a parser from the PEG.js grammar, then evaluates's the source before returning the parser object. + */ + function generate( grammar: string, options?: IBuildOptions<"parser"> ): GeneratedParser; + + /** + * Generate's a parser from the PEG.js grammar and returns the JavaScript based source. + */ + function generate( grammar: string, options?: IBuildOptions<"source"> ): string; + +} diff --git a/lib/typings/generated-parser.d.ts b/lib/typings/generated-parser.d.ts new file mode 100644 index 0000000..f090c39 --- /dev/null +++ b/lib/typings/generated-parser.d.ts @@ -0,0 +1,139 @@ +export = generatedparser; + +declare namespace generatedparser { + + /** + * Provides information pointing to a location within a source. + */ + interface SourcePosition { + + offset: number; + line: number; + column: number; + + } + + /** + * The `start` and `end` position's of an object within the source. + */ + interface SourceLocation { + + start: SourcePosition; + end: SourcePosition; + + } + + /** + * An object that can be used to make a generated parser trace it's progress. + */ + interface ITracer { + + trace( event: { + + type: string; + rule: string; + result?: string; + location: SourceLocation; + + } ); + + } + + interface ISyntaxError { + + name: string; + message: string; + stack?: string | any; + expected?: T; + found?: string; + location: SourceLocation; + + } + + interface SyntaxError { + + new( message: string, expected: T, found: string | null, location: SourceLocation ): ISyntaxError; + readonly prototype: ISyntaxError; + + buildMessage( expected: T, found?: string ): string; + + } + + interface SyntaxExpectation { + + type: string; + description?: string; + text?: string; + parts?: string[]; + inverted?: boolean; + ignoreCase?: boolean; + + } + + interface LiteralExpectation { + + type: "literal"; + text: string; + ignoreCase: boolean; + + } + + interface ClassExpectation { + + type: "class"; + parts: string[]; + inverted: boolean; + ignoreCase: boolean; + + } + + interface AnyExpectation { + + type: "any"; + + } + + interface EndExpectation { + + type: "end"; + + } + + interface OtherExpectation { + + type: "other"; + description: string; + + } + + type SyntaxErrorConstructor + = SyntaxError + | SyntaxError + | SyntaxError + | SyntaxError + | SyntaxError + | SyntaxError; + + /** + * Default options that are shared by all generated parser's. + */ + interface IOptions { + + [ key: string ]: any; + startRule?: string; + tracer?: ITracer; + + } + + /** + * API for the parser generated by PEG.js + */ + interface API { + + SyntaxError: SyntaxErrorConstructor; + DefaultTracer?: ITracer; + parse( input: string, options?: IOptions ): T; + + } + +} diff --git a/lib/typings/modules.d.ts b/lib/typings/modules.d.ts new file mode 100644 index 0000000..5472141 --- /dev/null +++ b/lib/typings/modules.d.ts @@ -0,0 +1,123 @@ +/// + +declare module "pegjs" { + + export default peg; + +} + +declare module "pegjs/lib/grammar-error" { + + export default peg.GrammarError; + +} + +declare module "pegjs/lib/parser" { + + export default peg.parser; + +} + +declare module "pegjs/lib/peg" { + + export default peg; + +} + +declare module "pegjs/lib/compiler" { + + export default peg.compiler; + +} + +declare module "pegjs/lib/compiler/asts" { + + namespace asts { + + function findRule( ast: peg.Grammar, name: string ): peg.parser.ast.Rule | void; + function indexOfRule( ast: peg.Grammar, name: string ): number; + function alwaysConsumesOnSuccess( ast: peg.Grammar, node: peg.parser.ast.Node ): boolean; + + } + export default asts; + +} + +declare module "pegjs/lib/compiler/index" { + + export default peg.compiler; + +} + +declare module "pegjs/lib/compiler/js" { + + namespace js { + + function stringEscape( s: string ): string; + function regexpClassEscape( s: string ): string; + + } + export default js; + +} + +declare module "pegjs/lib/compiler/opcodes" { + + const opcodes: { [ name: string ]: number }; + export default opcodes; + +} + +declare module "pegjs/lib/compiler/visitor" { + + export default peg.compiler.visitor; + +} + +declare module "pegjs/lib/compiler/passes/generate-bytecode" { + + export default peg.compiler.passes.generate.generateBytecode; + +} + +declare module "pegjs/lib/compiler/passes/generate-js" { + + export default peg.compiler.passes.generate.generateJS; + +} + +declare module "pegjs/lib/compiler/passes/remove-proxy-rules" { + + export default peg.compiler.passes.transform.removeProxyRules; + +} + +declare module "pegjs/lib/compiler/passes/report-duplicate-labels" { + + export default peg.compiler.passes.check.reportDuplicateLabels; + +} + +declare module "pegjs/lib/compiler/passes/report-duplicate-rules" { + + export default peg.compiler.passes.check.reportDuplicateRules; + +} + +declare module "pegjs/lib/compiler/passes/report-infinite-recursion" { + + export default peg.compiler.passes.check.reportInfiniteRecursion; + +} + +declare module "pegjs/lib/compiler/passes/report-infinite-repetition" { + + export default peg.compiler.passes.check.reportInfiniteRepetition; + +} + +declare module "pegjs/lib/compiler/passes/report-undefined-rules" { + + export default peg.compiler.passes.check.reportUndefinedRules; + +} diff --git a/lib/typings/pegjs.d.ts b/lib/typings/pegjs.d.ts new file mode 100644 index 0000000..b2c24c0 --- /dev/null +++ b/lib/typings/pegjs.d.ts @@ -0,0 +1,2 @@ +/// +/// diff --git a/package.json b/package.json index feaad9e..83dcfa4 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "lib", "!lib/.eslintrc.js" ], + "types": "lib/typings/pegjs.d.ts", "main": "lib/peg.js", "bin": "bin/peg.js", "repository": "pegjs/pegjs",