Added .deps to ignore.
[external/ragel.git] / test / mailbox3.rl
1 /*
2  * @LANG: c++
3  * @CFLAGS: -I../aapl
4  */
5
6 #include <iostream>
7 #include <string.h>
8
9 using std::cin;
10 using std::cout;
11 using std::cerr;
12 using std::endl;
13
14 %%{
15         machine mailbox;
16
17         action prn_char { cout << *p; }
18         action prn_space { cout << ' '; }
19         action prn_word { cout.write(ws, p-ws); cout << ' '; }
20         action prn_addr1 { cout << "| "; cout.write(ws+1, p-ws-2); }
21         action prn_addr2 { cout << "| "; cout.write(ws, p-ws); }
22         action prn_tab { cout << '\t'; }
23         action prn_nl { cout << '\n'; }
24         action prn_separator { cout << "------\n"; } 
25         action prn_from { cout << "FROM\n"; }
26         action prn_to { cout << "TO\n"; }
27         action prn_subj { cout << "SUBJECT\n"; }
28
29         action start_word { ws = p; }
30         action start_headers { preserve = p; }
31         action end_headers {preserve = 0;}
32
33         day = upper lower{2};
34         month = upper lower{2};
35         year = digit{4};
36         time = digit{2} ':' digit{2} 
37                 ( ':' digit{2} )?;
38         letterZone = upper{3};
39         numZone = [+\-] digit{4};
40         zone = letterZone | numZone;
41         dayNum = ( digit | ' ' ) digit;
42
43         date = day ' ' month ' ' 
44                 dayNum ' ' time ' '
45                 ( 
46                         year | 
47                         year ' ' zone | 
48                         zone ' ' year
49                 );
50
51         fromLine = 'From ' [^\n]* ' ' 
52                 date '\n' @start_headers;
53
54         headerChar = print - [ :];
55         headersToPrint = 'From' | 
56                 'To' | 'Subject';
57         headersToConsume = 
58                 headerChar+ - headersToPrint;
59
60         action init_hlen {hlen = 0;}
61         action hlen {hlen++ < 50}
62
63         consumeHeaderBody = 
64                 ':' @init_hlen
65                 ( 
66                         [^\n] | 
67                         ( '\n' [ \t] )
68                 )* when hlen
69                 '\n';
70
71         consumeHeader = 
72                 headersToConsume consumeHeaderBody;
73                 
74         addrWS = ( [ \t]+ | '\n' [ \t]+ );
75         addrComment = '(' [^)]* ')';
76         addrWord = [^"'@,<>() \t\n]+;
77         addrAddr1 = '<' [^>]* '>';
78         addrAddr2 = addrWord '@' addrWord;
79         addrString = 
80                 '"' [^"]* '"' |
81                 "'" [^']* "'";
82
83         addrItem = (
84                         addrAddr1 %prn_addr1 |
85                         addrAddr2 %prn_addr2 |
86                         addrWord %prn_word |
87                         addrString %prn_word
88                 ) >start_word;
89
90         address = ( 
91                         addrWS |
92                         addrComment |
93                         addrItem 
94                 )** >prn_tab;
95
96         addrHeader = ( 
97                         'From' %prn_from | 
98                         'To' %prn_to
99                 ) ':' @init_hlen
100                 ( address ( ',' @prn_nl address )* ) when hlen
101                 '\n' %prn_nl;
102
103         subjectHeader = 
104                 'Subject:' @prn_subj @prn_tab @init_hlen
105                 (
106                         ' '* <:
107                         ( 
108                                 [^\n] @prn_char  | 
109                                 ( '\n' [ \t]+ ) %prn_space
110                         )**
111                 ) when hlen
112                 '\n' %prn_nl;
113
114         header = consumeHeader | 
115                 addrHeader | subjectHeader;
116
117         messageLine = 
118                 ( [^\n]* when hlen '\n' @init_hlen ) - fromLine;
119
120         main := (
121                         fromLine %prn_separator
122                         header* 
123                         '\n' @end_headers @init_hlen
124                         messageLine*
125                 )*;
126  }%%
127  
128 %% write data;
129
130 #define BUFSIZE 8192
131
132 void test( const char *buf )
133 {
134         int cs, len = strlen( buf );
135         const char *preserve = 0, *ws = 0;
136         int hlen = 0;
137
138         %% write init;
139         const char *p = buf;
140         const char *pe = p + len;
141         %% write exec;
142
143         if ( cs < mailbox_first_final ) {
144                 cout << endl << endl;
145                 cout << "DID NOT FINISH IN A FINAL STATE" << endl;
146         }
147 }
148
149 int main()
150 {
151         test(
152                 "From user@host.com Wed Nov 28 13:30:05 2001\n"
153                 "From: \"Adrian D. Thurston\" <thurston@complang.org>\n"
154                 "Subject:   the squirrel has landed\n"
155                 "\n"
156                 "Message goes here. \n"
157                 "From (trick from line).\n"
158                 "From: not really a header\n"
159                 "\n"
160                 "From user2@host2.com Wed Nov 28 13:30:05 2001\n"
161                 "To: \"(kill 1)\" Edgar Allen Poe <ep@net.com> (da man)\n"
162                 "Subject:   (no subject) this is a really long subject which should fail the length constraint \n"
163                 "Other: 0123456789\n"
164                 "\n"
165                 "Message goes here. \n"
166                 "\n"
167         );
168         test(
169                 "From user@host.com Wed Nov 28 13:30:05 2001\n"
170                 "To: \"(kill 2)\" some guy <sg@net.com>\n"
171                 "From: \"Adrian D. Thurston this name is far too long\" <thurston@complang.org>\n"
172                 "Subject:   the squirrel has landed\n"
173                 "\n"
174                 "From user2@host2.com Wed Nov 28 13:30:05 2001\n"
175                 "To: Edgar Allen Poe <ep@net.com> (da man)\n"
176                 "Subject:   (no subject) \n"
177                 "\n"
178         );
179         test(
180                 "From user@host.com Wed Nov 28 13:30:05 2001\n"
181                 "To: \"(kill 3)\" some guy <sg@net.com>\n"
182                 "From: \"Adrian D. Thurston This name is fore sure absolutely too long\" <t@cs.ca>\n"
183                 "Subject:   the squirrel has landed\n"
184                 "\n"
185         );
186         test(
187                 "From user@host.com Wed Nov 28 13:30:05 2001\n"
188                 "From: \"Adrian D. Thurston \" <t@cs.ca>\n"
189                 "Subject:   (kill 4) the squirrel has landed\n"
190                 "Other: This is another header field, not interpreted, that is too long\n"
191                 "\n"
192         );
193         test(
194                 "From user@host.com Wed Nov 28 13:30:05 2001\n"
195                 "From: \"Adrian D. Thurston \" <t@cs.ca>\n"
196                 "Subject:   (kill 5)the squirrel has landed\n"
197                 "\n"
198                 "This message line is okay.\n"
199                 "But this message line is far too long and will cause an error.\n"
200         );
201         return 0;
202 }
203
204 #ifdef _____OUTPUT_____
205 ------
206 FROM
207         "Adrian D. Thurston" | thurston@complang.org
208 SUBJECT
209         the squirrel has landed
210 ------
211 TO
212         "(kill 1)" Edgar Allen Poe | ep@net.com
213 SUBJECT
214         (no subject) this is a really long subject whic
215
216 DID NOT FINISH IN A FINAL STATE
217 ------
218 TO
219         "(kill 2)" some guy | sg@net.com
220 FROM
221         "Adrian D. Thurston this name is far too long" 
222
223 DID NOT FINISH IN A FINAL STATE
224 ------
225 TO
226         "(kill 3)" some guy | sg@net.com
227 FROM
228         
229
230 DID NOT FINISH IN A FINAL STATE
231 ------
232 FROM
233         "Adrian D. Thurston " | t@cs.ca
234 SUBJECT
235         (kill 4) the squirrel has landed
236
237
238 DID NOT FINISH IN A FINAL STATE
239 ------
240 FROM
241         "Adrian D. Thurston " | t@cs.ca
242 SUBJECT
243         (kill 5)the squirrel has landed
244
245
246 DID NOT FINISH IN A FINAL STATE
247 #endif