6.2. nested parser

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