6.2. nested parser

Start felix section to tut/examples/tut_beg311.flx[1 /1 ]
     1: #line 3358 "./lpsrc/flx_tutorial.pak"
     2: #import <flx.flxh>
     3: 
     4: data := "1+2+3$";
     5: 
     6: union token_t =
     7:   | TOK_EOF
     8:   | TOK_PLUS
     9:   | TOK_INT of int
    10: ;
    11: 
    12: var i = 0;
    13: 
    14: fun get_token():token_t =
    15: {
    16:   ch := data.[i to i+1];
    17:   ++i;
    18:   tok :=
    19:     match ch with
    20:     | "$" => TOK_EOF
    21:     | "+" => TOK_PLUS
    22:     | "1" => TOK_INT 1
    23:     | "2" => TOK_INT 2
    24:     | "3" => TOK_INT 3
    25:     endmatch
    26:   ;
    27:   return tok;
    28: }
    29: 
    30: union expr_t =
    31:   | Integer of int
    32: ;
    33: 
    34: nonterm eexpr : expr_t =
    35: | xx:eexpr TOK_PLUS y:TOK_INT =>
    36:   match xx with
    37:   | Integer ?i => Integer (i+y)
    38:   endmatch
    39: 
    40: | y:TOK_INT => Integer y
    41: ;
    42: 
    43: proc try_parse() {
    44:   var z : 1 + int =
    45:     parse (the get_token) with
    46:     | e: eexpr => match e with | Integer ?i => i endmatch
    47:     endmatch
    48:   ;
    49: 
    50:   match z with
    51:   | case 0 => { print "Error"; }
    52:   | case 1 (?i) => { print i; }
    53:   endmatch;
    54:   endl;
    55: }
    56: 
    57: try_parse();
    58: 
End felix section to tut/examples/tut_beg311.flx[1]