3 * Show off concurrent abilities.
24 // Initialize the machine. Invokes any init statement blocks. Returns 0
25 // if the machine begins in a non-accepting state and 1 if the machine
26 // begins in an accepting state.
29 // Execute the machine on a block of data. Returns -1 if after processing
30 // the data, the machine is in the error state and can never accept, 0 if
31 // the machine is in a non-accepting state and 1 if the machine is in an
33 void execute( const char *data, int len );
35 // Indicate that there is no more data. Returns -1 if the machine finishes
36 // in the error state and does not accept, 0 if the machine finishes
37 // in any other non-accepting state and 1 if the machine finishes in an
50 start_word = cur_char;
53 cout << "word: " << start_word <<
54 " " << cur_char-1 << endl;
57 action start_comment {
58 start_comment = cur_char;
61 cout << "comment: " << start_comment <<
62 " " << cur_char-1 << endl;
65 action start_literal {
66 start_literal = cur_char;
69 cout << "literal: " << start_literal <<
70 " " << cur_char-1 << endl;
74 chars = ( any @next_char )*;
76 # Words are non-whitespace.
77 word = ( any-space )+ >start_word %end_word;
78 words = ( ( word | space ) $1 %0 )*;
80 # Finds C style comments.
81 comment = ( '/*' any* $0 '*/'@1 ) >start_comment %end_comment;
82 comments = ( ( comment | any ) $1 %0 )*;
84 # Finds single quoted strings.
85 literalChar = ( any - ['\\] ) | ( '\\' . any );
86 literal = ('\'' literalChar* '\'' ) >start_literal %end_literal;
87 literals = ( ( literal | (any-'\'') ) $1 %0 )*;
89 main := chars | words | comments | literals;
94 void Concurrent::init( )
100 void Concurrent::execute( const char *data, int len )
102 const char *p = data;
103 const char *pe = data + len;
104 const char *eof = pe;
109 int Concurrent::finish( )
111 if ( cs == Concurrent_error )
113 if ( cs >= Concurrent_first_final )
118 void test( const char *buf )
120 Concurrent concurrent;
122 concurrent.execute( buf, strlen(buf) );
123 if ( concurrent.finish() > 0 )
124 cout << "ACCEPT" << endl;
126 cout << "FAIL" << endl;
133 " * ' and now in a literal string\n"
136 "the comment has now ended but the literal string lives on\n"
138 "' comment closed\n" );
139 test( "/* * ' \\' */ \\' '\n" );
140 test( "/**/'\\''/*/*/\n" );
144 #ifdef _____OUTPUT_____