license import { ParseSourceSpan } from "./parse_util" ; export interface Node { sourceSpan : ParseSourceSpan ; visit ( visitor , context ) : any ; } export class Text implements Node { constructor ( public value , public sourceSpan ) { } visit ( visitor , context ) { return visitor . visitText ( this , context ) ; } } export class Expansion implements Node { constructor ( public switchValue , public type , public cases , public sourceSpan , public switchValueSourceSpan ) { } visit ( visitor , context ) { return visitor . visitExpansion ( this , context ) ; } } export class ExpansionCase implements Node { constructor ( public value , public expression , public sourceSpan , public valueSourceSpan , public expSourceSpan ) { } visit ( visitor , context ) { return visitor . visitExpansionCase ( this , context ) ; } } export class Attribute implements Node { constructor ( public name , public value , public sourceSpan , public valueSpan ? ) { } visit ( visitor , context ) { return visitor . visitAttribute ( this , context ) ; } } export class Element implements Node { constructor ( public name , public attrs , public children , public sourceSpan , public startSourceSpan = null , public endSourceSpan = null ) { } visit ( visitor , context ) { return visitor . visitElement ( this , context ) ; } } export class Comment implements Node { constructor ( public value , public sourceSpan ) { } visit ( visitor , context ) { return visitor . visitComment ( this , context ) ; } } export interface Visitor { visit ? ( node , context ) : any ; visitElement ( element , context ) : any ; visitAttribute ( attribute , context ) : any ; visitText ( text , context ) : any ; visitComment ( comment , context ) : any ; visitExpansion ( expansion , context ) : any ; visitExpansionCase ( expansionCase , context ) : any ; } export function visitAll ( visitor , nodes , context = null ) { const result = [ ] ; const visit = visitor . visit ? ( ast ) => visitor . visit ! ( ast , context ) || ast . visit ( visitor , context ) : ( ast ) => ast . visit ( visitor , context ) ; nodes . forEach ( ast => { const astResult = visit ( ast ) ; if ( astResult ) { result . push ( astResult ) ; } } ) ; return result ; }