TypeScript Visitor Design mode

Source: Internet
Author: User

The following translation of the head of the VBF project, try Typescript can rewrite.

Class regexpr {    accept<t> (convert: converter<t>)  {     }} Class symbolexpr extends regexpr {    symbol :  string    constructor (symbol: string)  {         this. Symbol = symbol;        super ();     }     Accept<T> (convert: converter<t>)  {         return convert. ACCEPTSYMBOLEXPR (this);     }}class kleenestarexpr extends regexpr {     InnerExpression: RegExpr    Accept<T> (convert:  converter<t>)  {        return convert. ACCEPTKLEENESTAREXPR (this);     }}class alternationexpr extends regexpr {    expr1: regexpr    expr2: regexpr     Accept<T> (convert: converter<t>)  {         return convert. ACCEPTALTERNATIONEXPR (this);     }}class concatenationexpr extends regexpr  {    Left: RegExpr    Right: RegExpr     Accept<T> (convert: converter<t>)  {         Return convert. ACCEPTCONCATENATIONEXPR (this);     }}class emptyexpr extends regexpr {     Accept<T> (convert: converter<t>)  {         return convert. Acceptempty (this);     } interface converter<t> {     Acceptempty (expr: regexpr): t;    acceptsymbolexpr (expr: symbolexpr): t;     Acceptkleenestarexpr (expr: kleenestarexpr):  t;    acceptalternationexpr (expr: &NBSP;ALTERNATIONEXPR):  t;    acceptconcatenationexpr (expr: ConcatenationExpr):  t;} class regularexprprettyprint implements converter<string> {     Acceptempty (expr: emptyexpr)  {        return  "ε";     }    acceptsymbolexpr (expr: symbolexpr)  {         return expr. Symbol;    }    acceptkleenestarexpr (expr: KleeneStarExpr)  {         return  "("  + expr. Innerexpression.accept (This)  +  ") *";     }    acceptalterNationexpr (expr: alternationexpr)  {        return  "("  + expr. Expr1.accept (This)  +  "|"  + expr. Expr2.accept (This)  +  ")";     }    acceptconcatenationexpr ( expr: concatenationexpr)  {        return  "("  +  expr. Left.accept (This)  + expr. Right.accept (This)  +  ")";     }} window.onload =  ()  => {     var s = new symbolexpr ("A");    var  Kstart = new kleenestarexpr ();     kstart. innerexpression = s;    var formatter = new  Regularexprprettyprint ();     var str = kstart. Accept (Formatter)     alert (str);};

TypeScript Visitor Design mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.