6.2. nested parser

Start felix section to tut/tutorial/tut-5.02-0.flx[1 /1 ]
     1: #line 5491 "./lpsrc/flx_tutorial.pak"
     2: //Check parser generator
     3: #import <flx.flxh>
     4: 
     5: union token_t =
     6:   | TOK_EOF
     7:   | TOK_PLUS
     8:   | TOK_INT of int
     9: ;
    10: 
    11: // a token stream generator
    12: gen get_token (s:string) ():token_t = {
    13:   var i: int; forall i in 0 upto (len s) - 1 do
    14:     yield
    15:       match s.[i to i+1] with
    16:       | "+" => TOK_PLUS
    17:       | "1" => TOK_INT 1
    18:       | "2" => TOK_INT 2
    19:       | "3" => TOK_INT 3
    20:       endmatch
    21:     ;
    22:   done;
    23:   return TOK_EOF;
    24: }
    25: 
    26: union expr_t =
    27:   | Integr of int
    28: ;
    29: 
    30: nonterm eexpr : expr_t =
    31: | xx:eexpr TOK_PLUS y:TOK_INT =>
    32:   match xx with
    33:   | Integr ?i => Integr (i+y)
    34:   endmatch
    35: 
    36: | y:TOK_INT => Integr y
    37: ;
    38: 
    39: proc try_parse() {
    40:   // the input string
    41:   data := "1+2+3";
    42: 
    43:   var z : 1 + int =
    44:     parse get_token data with
    45:     | e: eexpr => match e with | Integr ?i => i endmatch
    46:     endmatch
    47:   ;
    48: 
    49:   match z with
    50:   | case 0 => { print "Error"; }
    51:   | case 1 (?i) => { print i; }
    52:   endmatch;
    53:   endl;
    54: }
    55: 
    56: try_parse();
End felix section to tut/tutorial/tut-5.02-0.flx[1]
Start data section to tut/tutorial/tut-5.02-0.expect[1 /1 ]
     1: 6
End data section to tut/tutorial/tut-5.02-0.expect[1]