update(add) packaging directory and spec file from OBSTF:Private, OBS
[external/ragel.git] / test / atoi2.rl
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;
12
13         action begin {
14                 neg = false;
15                 val = 0;
16         }
17
18         action see_neg {
19                 neg = true;
20         }
21
22         action add_digit { 
23                 val = val * 10 + (fc - 48);
24         }
25
26         action finish {
27                 if ( neg )
28                         val = -1 * val;
29         }
30
31         atoi = (
32                 start: (
33                         '-' @see_neg ->om_num | 
34                         '+' ->om_num |
35                         [0-9] @add_digit ->more_nums
36                 ),
37
38                 # One or more nums.
39                 om_num: (
40                         [0-9] @add_digit ->more_nums
41                 ),
42
43                 # Zero ore more nums.
44                 more_nums: (
45                         [0-9] @add_digit ->more_nums |
46                         '' -> final
47                 )
48         ) >begin %finish;
49
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_____ */
64
65 /* _____OUTPUT_____
66 1
67 ACCEPT
68 12
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_____ */