Updated examples for the first of the 6.0 changes: removed write eof and added
[external/ragel.git] / examples / concurrent.rl
1 /*
2  * Show off concurrent abilities.
3  */
4
5 #include <iostream>
6 #include <stdlib.h>
7 #include <stdio.h>
8
9 using namespace std;
10
11 #define BUFSIZE 2048
12
13 struct Concurrent
14 {
15         int cur_char;
16         int start_word;
17         int start_comment;
18         int start_literal;
19
20         int cs;
21
22         int init( );
23         int execute( const char *data, int len );
24         int finish( );
25 };
26
27 %%{
28         machine Concurrent;
29
30         action next_char {
31                 cur_char += 1;
32         }
33
34         action start_word {
35                 start_word = cur_char;
36         }
37         action end_word {
38                 cout << "word: " << start_word << 
39                                 " " << cur_char-1 << endl;
40         }
41
42         action start_comment {
43                 start_comment = cur_char;
44         }
45         action end_comment {
46                 cout << "comment: " << start_comment <<
47                                 " " << cur_char-1 << endl;
48         }
49
50         action start_literal {
51                 start_literal = cur_char;
52         }
53         action end_literal {
54                 cout << "literal: " << start_literal <<
55                                 " " << cur_char-1 << endl;
56         }
57
58         # Count characters.
59         chars = ( any @next_char )*;
60
61         # Words are non-whitespace. 
62         word = ( any-space )+ >start_word %end_word;
63         words = ( ( word | space ) $1 %0 )*;
64
65         # Finds C style comments. 
66         comment = ( '/*' any* :>> '*/' ) >start_comment %end_comment;
67         comments = ( comment | any )**;
68
69         # Finds single quoted strings. 
70         literalChar = ( any - ['\\] ) | ( '\\' . any );
71         literal = ('\'' literalChar* '\'' ) >start_literal %end_literal;
72         literals = ( ( literal | (any-'\'') ) $1 %0 )*;
73
74         main := chars | words | comments | literals;
75 }%%
76
77 %% write data;
78
79 int Concurrent::init( )
80 {
81         %% write init;
82         cur_char = 0;
83         return 1;
84 }
85
86 int Concurrent::execute( const char *data, int len )
87 {
88         const char *p = data;
89         const char *pe = data + len;
90
91         %% write exec;
92
93         if ( cs == Concurrent_error )
94                 return -1;
95         if ( cs >= Concurrent_first_final )
96                 return 1;
97         return 0;
98 }
99
100 int Concurrent::finish( )
101 {
102         if ( cs == Concurrent_error )
103                 return -1;
104         if ( cs >= Concurrent_first_final )
105                 return 1;
106         return 0;
107 }
108
109 Concurrent concurrent;
110 char buf[BUFSIZE];
111
112 int main()
113 {
114         concurrent.init();
115         while ( 1 ) {
116                 int len = fread( buf, 1, BUFSIZE, stdin );
117                 concurrent.execute( buf, len );
118                 if ( len != BUFSIZE )
119                         break;
120         }
121
122         if ( concurrent.finish() <= 0 )
123                 cerr << "concurrent: error parsing input" << endl;
124         return 0;
125 }