diff --git a/lib/compiler/index.js b/lib/compiler/index.js index c4569d1..169810a 100644 --- a/lib/compiler/index.js +++ b/lib/compiler/index.js @@ -71,6 +71,7 @@ const compiler = { dependencies: {}, exportVar: null, format: "bare", + header: null, optimize: "speed", output: "parser", trace: false diff --git a/lib/compiler/session.js b/lib/compiler/session.js index 9bb8bf2..aacb48c 100644 --- a/lib/compiler/session.js +++ b/lib/compiler/session.js @@ -20,19 +20,19 @@ function fatal( message, location ) { class Session { - constructor( options ) { + constructor( config ) { - options = typeof options !== "undefined" ? options : {}; + config = typeof config !== "undefined" ? config : {}; - this.grammar = options.grammar; - this.opcodes = options.opcodes || opcodes; - this.parser = options.parser || parser; - this.passes = options.passes || []; - this.visitor = options.visitor || ast.visitor; - this.vm = options.vm || vm; + this.grammar = config.grammar; + this.opcodes = config.opcodes || opcodes; + this.parser = config.parser || parser; + this.passes = config.passes || {}; + this.visitor = config.visitor || ast.visitor; + this.vm = config.vm || vm; - if ( typeof options.warn === "function" ) this.warn = options.warn; - if ( typeof options.error === "function" ) this.error = options.error; + if ( typeof config.warn === "function" ) this.warn = config.warn; + if ( typeof config.error === "function" ) this.error = config.error; Object.defineProperty( this, "fatal", { value: fatal } ); diff --git a/lib/typings/api.d.ts b/lib/typings/api.d.ts index 3371663..b7f0f43 100644 --- a/lib/typings/api.d.ts +++ b/lib/typings/api.d.ts @@ -28,6 +28,9 @@ declare namespace peg { } + /** + * PEG.js AST + */ namespace ast { /** @@ -296,19 +299,32 @@ declare namespace peg { } + interface IVisitor { + + ( node: Object, ...args ): R; + + } + class ASTVisitor implements IVisitorMap { - visit( node: Object, ...args ): R; + visit: IVisitor; } - interface IVisitor { + interface IVisitorBuilder { - ( node: Object, ...args ): R; + ( functions: IVisitorMap ): IVisitor; } - function build( functions: IVisitorMap ): IVisitor; + const build: IVisitorBuilder; + + } + + interface visitor { + + ASTVisitor: visitor.ASTVisitor; + build: visitor.IVisitorBuilder; } @@ -331,6 +347,9 @@ declare namespace peg { } + /** + * The PEG.js compiler. + */ namespace compiler { type FormatOptions = "amd" | "bare" | "commonjs" | "es" | "globals" | "umd"; @@ -342,6 +361,7 @@ declare namespace peg { [ key: string ]: any; allowedStartRules?: string[]; cache?: boolean; + context?: { [ name: string ]: any; }; dependencies?: { [ name: string ]: string; }; exportVar?: string; format?: FormatOptions; @@ -356,6 +376,7 @@ declare namespace peg { allowedStartRules: string[]; cache: boolean; + context: { [ name: string ]: any; }; dependencies: { [ name: string ]: string; }; exportVar: string; format: FormatOptions; @@ -369,7 +390,8 @@ declare namespace peg { interface ICompilerPass { ( node: Grammar ): void; - ( node: Grammar, options: ICompilerPassOptions ): void; + ( node: Grammar, session: Session ): void; + ( node: Grammar, session: Session, options: ICompilerPassOptions ): void; } @@ -379,30 +401,78 @@ declare namespace peg { } + interface IOpcodes { + + [ name: string ]: number; + + } + + interface vm { + + runInContext( code: string, vm$context?: { [ name: string ]: any; } ): any; + + } + const vm: vm; + + interface ISessionMessageEmitter { + + ( message: string, location: SourceLocation ): any; + + } + + interface ISessionConfig { + + [ key: string ]: any; + grammar?: string; + opcodes?: IOpcodes; + parser?: GeneratedParser; + passes?: IPassesMap; + visitor?: ast.visitor; + vm?: vm; + warn?: ISessionMessageEmitter; + error?: ISessionMessageEmitter; + + } + + class Session implements ISessionConfig { + + constructor( config?: ISessionConfig ); + + parse( input: string, options?: parser.IOptions ): Grammar; + + buildVisitor: ast.visitor.IVisitorBuilder; + + warn: ISessionMessageEmitter; + error: ISessionMessageEmitter; + fatal: ISessionMessageEmitter; + + } + 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; + function reportUndefinedRules( ast: Grammar, session: Session, options: ICompilerPassOptions ): void; + function reportDuplicateRules( ast: Grammar, session: Session ): void; + function reportUnusedRules( ast: Grammar, session: Session, options: ICompilerPassOptions ): void; + function reportDuplicateLabels( ast: Grammar, session: Session ): void; + function reportInfiniteRecursion( ast: Grammar, session: Session ): void; + function reportInfiniteRepetition( ast: Grammar, session: Session ): void; } namespace transform { - function removeProxyRules( ast: Grammar, options: ICompilerPassOptions ): void; + function removeProxyRules( ast: Grammar, session: Session, options: ICompilerPassOptions ): void; } namespace generate { - function calcReportFailures( ast: Grammar, options: ICompilerPassOptions ): void; - function inferenceMatchResult( ast: Grammar ): void; - function generateBytecode( ast: Grammar ): void; - function generateJS( ast: Grammar, options: ICompilerPassOptions ): void; + function calcReportFailures( ast: Grammar, session: Session, options: ICompilerPassOptions ): void; + function inferenceMatchResult( ast: Grammar, session: Session ): void; + function generateBytecode( ast: Grammar, session: Session ): void; + function generateJS( ast: Grammar, session: Session, options: ICompilerPassOptions ): void; } @@ -411,60 +481,63 @@ declare namespace peg { /** * Generate's a parser from the PEG.js AST and returns it. */ - function compile( ast: Grammar, passes: IPassesMap, options?: ICompilerOptions ): GeneratedParser | string; + function compile( ast: Grammar, session: Session, 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; + function compile( ast: Grammar, session: Session, 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; + function compile( ast: Grammar, session: Session, options?: ICompilerOptions<"source"> ): string; } - namespace util { + // peg.util - interface IStageMap { + interface IStageMap { - [ stage: string ] - : compiler.ICompilerPass[] - | { [ pass: string ]: compiler.ICompilerPass }; + [ stage: string ] + : compiler.ICompilerPass[] + | { [ pass: string ]: compiler.ICompilerPass }; - } + } - function convertPasses( stages: IStageMap ): compiler.IPassesMap; + interface IIterator { - interface IIterator { + ( value: any ): R; + ( value: any, key: string ): R; - ( value: any ): R; - ( value: any, key: string ): R; + } - } + interface IObjectUtils { - function clone( source: {} ): {}; - function each( object: {}, iterator: IIterator ): void; - function extend( target: {}, source: {} ): {}; - function map( object: {}, transformer: IIterator ): {}; - function values( object: {}, transformer?: IIterator ): any[]; - function enforceFastProperties( o: {} ): {}; + convertPasses( stages: IStageMap ): compiler.IPassesMap; - } + clone( source: {} ): {}; + each( object: {}, iterator: IIterator ): void; + extend( target: {}, source: {} ): {}; + map( object: {}, transformer: IIterator ): {}; + values( object: {}, transformer?: IIterator ): any[]; + enforceFastProperties( o: {} ): {}; - interface IBuildConfig { + } + interface util extends IObjectUtils { - parser: GeneratedParser; - passes: compiler.IPassesMap; + convertPasses( stages: IStageMap ): compiler.IPassesMap; } + const util: util; + + // peg.generate - interface IPlugin { + interface IPlugin { [ key: string ]: any; - use( config: IBuildConfig ): void; - use( config: IBuildConfig, options: IBuildOptions ): void; + use( session: compiler.Session ): void; + use( session: compiler.Session, options: IBuildOptions ): void; } diff --git a/lib/typings/modules.d.ts b/lib/typings/modules.d.ts index ff66bfc..e710871 100644 --- a/lib/typings/modules.d.ts +++ b/lib/typings/modules.d.ts @@ -26,14 +26,7 @@ declare module "pegjs/lib/peg" { declare module "pegjs/lib/ast" { - export const Node: peg.ast.Node; - export const Grammar: peg.ast.Grammar; - export const visitor: { - - ASTVisitor: peg.ast.visitor.ASTVisitor; - build( functions: peg.ast.visitor.IVisitorMap ): peg.ast.visitor.IVisitor; - - }; + export default peg.ast; } @@ -81,11 +74,23 @@ declare module "pegjs/lib/compiler/js" { declare module "pegjs/lib/compiler/opcodes" { - const opcodes: { [ name: string ]: number }; + const opcodes: peg.compiler.IOpcodes; export default opcodes; } +declare module "pegjs/lib/compiler/session" { + + export default peg.compiler.Session; + +} + +declare module "pegjs/lib/compiler/vm" { + + export default peg.compiler.vm; + +} + declare module "pegjs/lib/compiler/passes/calc-report-failures" { export default peg.compiler.passes.generate.calcReportFailures; @@ -146,6 +151,12 @@ declare module "pegjs/lib/compiler/passes/report-undefined-rules" { } +declare module "pegjs/lib/compiler/passes/report-unused-rules" { + + export default peg.compiler.passes.check.reportUnusedRules; + +} + declare module "pegjs/lib/util" { export default peg.util; @@ -166,16 +177,7 @@ declare module "pegjs/lib/util/index" { declare module "pegjs/lib/util/objects" { - namespace objects { - - function clone( source: {} ): {}; - function each( object: {}, iterator: peg.util.IIterator ): void; - function extend( target: {}, source: {} ): {}; - function map( object: {}, transformer: peg.util.IIterator ): {}; - function values( object: {}, transformer?: peg.util.IIterator ): any[]; - function enforceFastProperties( o: {} ): {}; - - } + const objects: peg.IObjectUtils; export default objects; }