import { ASTNode } from "./AST" ; import { ASTWalker } from "./ASTWalker" ; import { isArray , map , zipObject } from "lodash" ; import { visitorKeys } from "./VisitorKeys" ; param method const shouldBeVisitor = ( method ) => { return visitorKeys [ method ] && method . toUpperCase ( ) !== method ; } ; export class implements export class BaseASTWalker implements ASTWalker < ASTNode | ASTNode [ ] > { public Program ( ) { throw new Error ( "Method not implemented." ) ; } public LibraryStatement ( ) { throw new Error ( "Method not implemented." ) ; } public UnTypedIdentifier ( ) { throw new Error ( "Method not implemented." ) ; } public ParameterList ( ) { throw new Error ( "Method not implemented." ) ; } public AdditionExpression ( ) { throw new Error ( "Method not implemented." ) ; } public AssignmentExpression ( ) { throw new Error ( "Method not implemented." ) ; } public MultiplicationExpression ( ) { throw new Error ( "Method not implemented." ) ; } public LogicExpression ( ) { throw new Error ( "Method not implemented." ) ; } public ParenthesisExpression ( ) { throw new Error ( "Method not implemented." ) ; } public ArrayElement ( ) { throw new Error ( "Method not implemented." ) ; } public ArrayExpression ( ) { throw new Error ( "Method not implemented." ) ; } public CallExpression ( ) { throw new Error ( "Method not implemented." ) ; } public ConditionalConst ( ) { throw new Error ( "Method not implemented." ) ; } public ConditionalElseIfStatement ( ) { throw new Error ( "Method not implemented." ) ; } public ConditionalElseStatement ( ) { throw new Error ( "Method not implemented." ) ; } public ConditionalError ( ) { throw new Error ( "Method not implemented." ) ; } public ConditionalIfStatement ( ) { throw new Error ( "Method not implemented." ) ; } public DimStatement ( ) { throw new Error ( "Method not implemented." ) ; } public DotMemberExpression ( ) { throw new Error ( "Method not implemented." ) ; } public ElseIfStatement ( ) { throw new Error ( "Method not implemented." ) ; } public ElseStatement ( ) { throw new Error ( "Method not implemented." ) ; } public EmptyStatement ( ) { throw new Error ( "Method not implemented." ) ; } public Comment ( ) { throw new Error ( "Method not implemented." ) ; } public ForEachStatement ( ) { throw new Error ( "Method not implemented." ) ; } public ForStatement ( ) { throw new Error ( "Method not implemented." ) ; } public FunctionDeclaration ( ) { throw new Error ( "Method not implemented." ) ; } public FunctionExpression ( ) { throw new Error ( "Method not implemented." ) ; } public GoToStatement ( ) { throw new Error ( "Method not implemented." ) ; } public IfStatement ( ) { throw new Error ( "Method not implemented." ) ; } public Literal ( ) { throw new Error ( "Method not implemented." ) ; } public MemberExpression ( ) { throw new Error ( "Method not implemented." ) ; } public NextStatement ( ) { throw new Error ( "Method not implemented." ) ; } public PostfixExpression ( ) { throw new Error ( "Method not implemented." ) ; } public PrintStatement ( ) { throw new Error ( "Method not implemented." ) ; } public RelationExpression ( ) { throw new Error ( "Method not implemented." ) ; } public ReturnStatement ( ) { throw new Error ( "Method not implemented." ) ; } public StopStatement ( ) { throw new Error ( "Method not implemented." ) ; } public SubDeclaration ( ) { throw new Error ( "Method not implemented." ) ; } public SubExpression ( ) { throw new Error ( "Method not implemented." ) ; } public UnaryExpression ( ) { throw new Error ( "Method not implemented." ) ; } public WhileStatement ( ) { throw new Error ( "Method not implemented." ) ; } public RokuTryStatement ( ) { throw new Error ( "Method not implemented." ) ; } public BlockStatement ( ) { throw new Error ( "Method not implemented." ) ; } public TypeAnnotation ( ) { throw new Error ( "Method not implemented." ) ; } public ObjectExpression ( ) { throw new Error ( "Method not implemented." ) ; } public Parameter ( ) { throw new Error ( "Method not implemented." ) ; } public Identifier ( ) { throw new Error ( "Method not implemented." ) ; } public Property ( ) { throw new Error ( "Method not implemented." ) ; } public Arguments ( ) { throw new Error ( "Method not implemented." ) ; } public STRING_LITERAL ( ctx ) { return ctx . loc . source ; } param { ASTNode } ctx returns { ( { [ key : string ] : ASTNode | ASTNode [ ] } ) } memberOf public visit ( ctx ) { if ( ! ctx . type ) { return null ; } const keysToVisit = visitorKeys [ ctx . type ] ; const mapped = map ( keysToVisit , ( key ) => { if ( ! ctx [ key ] ) { return ctx [ key ] ; } if ( ctx [ key ] . type ) { const method = ctx [ key ] . type ; if ( ! this [ method ] ) { if ( shouldBeVisitor ( method ) ) { console . warn ( method ) ; } else { return this . STRING_LITERAL ( ctx [ key ] ) ; } } else { return this [ method ] ( ctx [ key ] ) ; } } else { if ( isArray ( ctx [ key ] ) ) { return map ( ctx [ key ] , ( element ) => { const method = element . type ; if ( ! this [ method ] ) { if ( shouldBeVisitor ( method ) ) { console . warn ( method ) ; } else { return this . STRING_LITERAL ( element ) ; } } else { return this [ method ] ( element ) ; } } ) ; } } } ) ; return zipObject ( keysToVisit , mapped ) ; } protected param { ( ASTNode [ ] | ASTNode ) } value returns { ASTNode [ ] } memberOf public asArray ( value ) { if ( ! value ) { return [ ] ; } return isArray ( value ) ? value : [ value ] ; } protected param { left returns memberOf public binary ( ctx ) { let { left , right , operator } = this . visit ( ctx ) ; left = this . asArray ( left ) ; right = this . asArray ( right ) ; operator = this . asArray ( operator ) ; return this . mergeOperands ( right , left , operator ) ; } protected param { ASTNode [ ] } from param { ASTNode [ ] } to param { ASTNode [ ] } dividers returns memberOf public mergeOperands ( from = [ ] , to = [ ] , dividers = [ ] ) { while ( from . length ) { to . push ( dividers . shift ( ) ) ; to . push ( from . shift ( ) ) ; } return to ; } }