Initialize Tizen 2.3
[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, bool isEof );
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, bool isEof )
87 {
88         const char *p = data;
89         const char *pe = data + len;
90         const char *eof = isEof ? pe : 0;
91
92         %% write exec;
93
94         if ( cs == Concurrent_error )
95                 return -1;
96         if ( cs >= Concurrent_first_final )
97                 return 1;
98         return 0;
99 }
100
101 int Concurrent::finish( )
102 {
103         if ( cs == Concurrent_error )
104                 return -1;
105         if ( cs >= Concurrent_first_final )
106                 return 1;
107         return 0;
108 }
109
110 Concurrent concurrent;
111 char buf[BUFSIZE];
112
113 int main()
114 {
115         concurrent.init();
116         while ( 1 ) {
117                 int len = fread( buf, 1, BUFSIZE, stdin );
118                 concurrent.execute( buf, len, len != BUFSIZE );
119                 if ( len != BUFSIZE )
120                         break;
121         }
122
123         if ( concurrent.finish() <= 0 )
124                 cerr << "concurrent: error parsing input" << endl;
125         return 0;
126 }