A fix to the documentation makefile from John D. Mitchell.
[ragel.git] / test / atoi2.rl
blob923e88f03657621a9803f6f815d3d6e61494582a
1 /*
2  * @LANG: indep
3  * This implementes an atoi machine using the statechart paradigm.
4  */
5 bool neg;
6 int val;
7 %%
8 val = 0;
9 neg = false;
10 %%{
11         machine StateChart;
13         action begin {
14                 neg = false;
15                 val = 0;
16         }
18         action see_neg {
19                 neg = true;
20         }
22         action add_digit { 
23                 val = val * 10 + (fc - 48);
24         }
26         action finish {
27                 if ( neg )
28                         val = -1 * val;
29         }
31         atoi = (
32                 start: (
33                         '-' @see_neg ->om_num | 
34                         '+' ->om_num |
35                         [0-9] @add_digit ->more_nums
36                 ),
38                 # One or more nums.
39                 om_num: (
40                         [0-9] @add_digit ->more_nums
41                 ),
43                 # Zero ore more nums.
44                 more_nums: (
45                         [0-9] @add_digit ->more_nums |
46                         '' -> final
47                 )
48         ) >begin %finish;
50         action oneof { printi val; prints "\n"; }
51         main := ( atoi '\n' @oneof )*;
52 }%%
53 /* _____INPUT_____
54 "1\n"
55 "12\n"
56 "222222\n"
57 "+2123\n"
58 "213 3213\n"
59 "-12321\n"
60 "--123\n"
61 "-99\n"
62 " -3000\n"
63 _____INPUT_____ */
65 /* _____OUTPUT_____
67 ACCEPT
69 ACCEPT
70 222222
71 ACCEPT
72 2123
73 ACCEPT
74 FAIL
75 -12321
76 ACCEPT
77 FAIL
78 -99
79 ACCEPT
80 FAIL
81 _____OUTPUT_____ */