From 617b6b7425e055772dd53ff9b06e625cacc825cd Mon Sep 17 00:00:00 2001 From: Futago-za Ryuu Date: Tue, 16 Jan 2018 04:28:27 +0000 Subject: [PATCH] Updated utils and tsd - Exposed 'visitor.ASTVisitor' - Remoed 'util.createVisitor' - Added type 'peg.SourceLocation' - Updated tsd for 'peg.compiler.visitor' - Added tsd for 'peg.util.enforceFastProperties' - Added tsd for new parser modules --- lib/compiler/visitor.js | 1 + lib/typings/api.d.ts | 86 ++++++++++++++++++---------------------- lib/typings/modules.d.ts | 34 +++++++++++++++- lib/util/objects.js | 12 ------ 4 files changed, 72 insertions(+), 61 deletions(-) diff --git a/lib/compiler/visitor.js b/lib/compiler/visitor.js index af44280..f92d452 100644 --- a/lib/compiler/visitor.js +++ b/lib/compiler/visitor.js @@ -26,6 +26,7 @@ class ASTVisitor { } } +ASTVisitor.ASTVisitor = ASTVisitor; module.exports = ASTVisitor; // Helper's to create visitor's for use with the ASTVisitor class diff --git a/lib/typings/api.d.ts b/lib/typings/api.d.ts index 38ac86d..8f6f963 100644 --- a/lib/typings/api.d.ts +++ b/lib/typings/api.d.ts @@ -8,6 +8,7 @@ declare namespace peg { type Grammar = parser.ast.Grammar; type GeneratedParser = gp.API; type SyntaxError = gp.SyntaxErrorConstructor; + type SourceLocation = gp.SourceLocation; /** * PEG.js version (uses semantic versioning). @@ -21,9 +22,9 @@ declare namespace peg { name: string; message: string; - location?: gp.SourceLocation; + location?: SourceLocation; - constructor( message: string, location?: gp.SourceLocation ); + constructor( message: string, location?: SourceLocation ); } @@ -53,7 +54,7 @@ declare namespace peg { interface INode { type: string; - location: gp.SourceLocation; + location: SourceLocation; } @@ -294,42 +295,45 @@ declare namespace peg { } - namespace visitor { + interface IVisitor { - interface Visitor { + ( node: parser.ast.Node, ...args ): R; - ( node: Node, ...args ): R; + } - } + interface IVisitorMap { - 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; + [ key: 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; - } + } + + class visitor implements IVisitorMap { - function build( functions: VisitorMap ): Visitor; + visit: IVisitor; + + static build( functions: IVisitorMap ): IVisitor; + static ASTVisitor: visitor; } @@ -403,21 +407,7 @@ declare namespace peg { function extend( target: {}, source: {} ): {}; function map( object: {}, transformer: IIterator ): {}; function values( object: {}, transformer?: IIterator ): any[]; - - interface IVisitor { - - ( value: {} ): void; - - } - - interface IVisitorCallback { - - ( value: any ): void; - ( value: any, args: any[] ): void; - - } - - function createVisitor( property: string | number, visit: IVisitorCallback ): IVisitor; + function enforceFastProperties( o: {} ): {}; } diff --git a/lib/typings/modules.d.ts b/lib/typings/modules.d.ts index b628246..6606343 100644 --- a/lib/typings/modules.d.ts +++ b/lib/typings/modules.d.ts @@ -18,6 +18,38 @@ declare module "pegjs/lib/parser" { } +declare module "pegjs/lib/parser/ast" { + + export class Node { + + type: string; + location: peg.SourceLocation; + + constructor( type: string, location: peg.SourceLocation ); + + } + + export class Grammar extends Node { + + initializer?: peg.parser.ast.Initializer; + rules: peg.parser.ast.Rule[]; + + constructor( + initializer: void | peg.parser.ast.Initializer, + rules: peg.parser.ast.Rule[], + location: peg.SourceLocation, + ); + + } + +} + +declare module "pegjs/lib/parser/index" { + + export default peg.parser; + +} + declare module "pegjs/lib/peg" { export default peg; @@ -161,7 +193,7 @@ declare module "pegjs/lib/util/objects" { function extend( target: {}, source: {} ): {}; function map( object: {}, transformer: peg.util.IIterator ): {}; function values( object: {}, transformer?: peg.util.IIterator ): any[]; - function createVisitor( property: string | number, visit: peg.util.IVisitorCallback ): peg.util.IVisitor; + function enforceFastProperties( o: {} ): {}; } export default objects; diff --git a/lib/util/objects.js b/lib/util/objects.js index eddd5f1..325e2df 100644 --- a/lib/util/objects.js +++ b/lib/util/objects.js @@ -97,18 +97,6 @@ const objects = { }, - // Will return a function that can be used to visit a specific property - // no matter what object is passed to it. - createVisitor( property, visit ) { - - return function visitProperty( object ) { - - visit( object[ property ], __slice.call( arguments, 1 ) ); - - }; - - }, - // Will ensure on V8 that that property access is always in fast mode // See: https://stackoverflow.com/a/24989927/1518408 enforceFastProperties( o ) {