4 * Test works with split code gen.
13 action pass { fsm->pass(fc); }
14 action buf { fsm->buf(fc); }
16 action emit_slit { fsm->token( TK_Slit ); }
17 action emit_dlit { fsm->token( TK_Dlit ); }
18 action emit_id { fsm->token( TK_Id ); }
19 action emit_integer_decimal { fsm->token( TK_IntegerDecimal ); }
20 action emit_integer_octal { fsm->token( TK_IntegerOctal ); }
21 action emit_integer_hex { fsm->token( TK_IntegerHex ); }
22 action emit_float { fsm->token( TK_Float ); }
23 action emit_symbol { fsm->token( fsm->tokBuf.data[0] ); }
24 action tokst { fsm->tokStart = fsm->col; }
26 # Single and double literals.
27 slit = ( 'L'? ( "'" ( [^'\\\n] | /\\./ )* "'" ) $buf ) >tokst %emit_slit;
28 dlit = ( 'L'? ( '"' ( [^"\\\n] | /\\./ )* '"' ) $buf ) >tokst %emit_dlit;
31 id = ( [a-zA-Z_] [a-zA-Z0-9_]* ) >tokst $buf %emit_id;
34 fract_const = digit* '.' digit+ | digit+ '.';
35 exponent = [eE] [+\-]? digit+;
36 float_suffix = [flFL];
38 ( fract_const exponent? float_suffix? |
39 digit+ exponent float_suffix? ) >tokst $buf %emit_float;
41 # Integer decimal. Leading part buffered by float.
42 integer_decimal = ( ( '0' | [1-9] [0-9]* ) [ulUL]{0,3} $buf ) %emit_integer_decimal;
44 # Integer octal. Leading part buffered by float.
45 integer_octal = ( '0' [0-9]+ [ulUL]{0,2} $buf ) %emit_integer_octal;
47 # Integer hex. Leading 0 buffered by float.
48 integer_hex = ( '0' ( 'x' [0-9a-fA-F]+ [ulUL]{0,2} ) $buf ) %emit_integer_hex;
50 # Only buffer the second item, first buffered by symbol. */
51 namesep = '::' @buf %{fsm->token( TK_NameSep );};
52 deqs = '==' @buf %{fsm->token( TK_EqualsEquals );};
53 neqs = '!=' @buf %{fsm->token( TK_NotEquals );};
54 and_and = '&&' @buf %{fsm->token( TK_AndAnd );};
55 or_or = '||' @buf %{fsm->token( TK_OrOr );};
56 mult_assign = '*=' @buf %{fsm->token( TK_MultAssign );};
57 percent_assign = '%=' @buf %{fsm->token( TK_PercentAssign );};
58 plus_assign = '+=' @buf %{fsm->token( TK_PlusAssign );};
59 minus_assign = '-=' @buf %{fsm->token( TK_MinusAssign );};
60 amp_assign = '&=' @buf %{fsm->token( TK_AmpAssign );};
61 caret_assign = '^=' @buf %{fsm->token( TK_CaretAssign );};
62 bar_assign = '|=' @buf %{fsm->token( TK_BarAssign );};
63 plus_plus = '++' @buf %{fsm->token( TK_PlusPlus );};
64 minus_minus = '--' @buf %{fsm->token( TK_MinusMinus );};
65 arrow = '->' @buf %{fsm->token( TK_Arrow );};
66 arrow_star = '->*' @buf %{fsm->token( TK_ArrowStar );};
67 dot_star = '.*' @buf %{fsm->token( TK_DotStar );};
69 # Buffer both items. *
70 div_assign = '/=' @{fsm->buf('/');fsm->buf(fc);} %{fsm->token( TK_DivAssign );};
72 # Double dot is sent as two dots.
73 dot_dot = '..' %{fsm->token('.'); fsm->buf('.'); fsm->token('.');};
75 # Three char compounds, first item already buffered. */
76 dot_dot_dot = '...' %{fsm->buf('.'); fsm->buf('.'); fsm->token( TK_DotDotDot );};
79 compound = namesep | deqs | neqs | and_and | or_or | mult_assign |
80 div_assign | percent_assign | plus_assign | minus_assign |
81 amp_assign | caret_assign | bar_assign | plus_plus | minus_minus |
82 arrow | arrow_star | dot_star | dot_dot | dot_dot_dot;
84 # Single char symbols.
86 ( punct - [./_"'] ) >tokst $buf %emit_symbol |
87 # Do not immediately buffer slash, may be start of comment.
88 '/' >tokst %{ fsm->buf('/'); fsm->token( '/' ); } |
89 # Dot covered by float.
92 # Comments and whitespace.
93 commc = '/*' @{fsm->pass('/'); fsm->pass('*');} ( any* $0 '*/' @1 ) $pass;
94 commcc = '//' @{fsm->pass('/'); fsm->pass('/');} ( any* $0 '\n' @1 ) $pass;
95 whitespace = ( any - ( 0 | 33..126 ) )+ $pass;
98 /* On EOF char, write out the non token buffer. */
99 fsm->nonTokBuf.append(0);
100 cout << fsm->nonTokBuf.data;
101 fsm->nonTokBuf.clear();
104 # Using 0 as eof. If seeingAs a result all null characters get ignored.
107 # All outside code tokens.
109 id | slit | dlit | float | integer_decimal |
110 integer_octal | integer_hex | compound | symbol );
111 nontok = ( commc | commcc | whitespace | EOF );
114 '\n' @{ fsm->line += 1; fsm->col = 1; } |
115 [^\n] @{ fsm->col += 1; } )*;
117 main := ( ( tokens | nontok )** ) & position;
122 void Scanner::init( )
125 /* A count of the number of characters in
126 * a token. Used for % sequences. */
134 int Scanner::execute( const char *data, int len )
137 const char *p = data;
138 const char *pe = data + len;
139 const char *eof = pe;
142 if ( cs == Scanner_error )
144 if ( cs >= Scanner_first_final )
149 int Scanner::finish( )
151 if ( cs == Scanner_error )
153 if ( cs >= Scanner_first_final )
158 void Scanner::token( int id )
161 if ( nonTokBuf.length > 0 ) {
163 cout << nonTokBuf.data;
169 cout << '<' << id << '>' << tokBuf.data;
184 void Buffer::upAllocate( int len )
187 data = (char*) malloc( len );
189 data = (char*) realloc( data, len );
193 void test( const char *buf )
195 Scanner scanner(cout);
197 scanner.execute( buf, strlen(buf) );
199 /* The last token is ignored (because there is no next token). Send
200 * trailing null to force the last token into whitespace. */
202 if ( scanner.execute( &eof, 1 ) <= 0 ) {
203 cerr << "cppscan: scan failed" << endl;
216 "/* Construct an fsmmachine from a graph. */\n"
217 "RedFsmAp::RedFsmAp( FsmAp *graph, bool complete )\n"
221 " assert( sizeof(RedTransAp) <= sizeof(TransAp) );\n"
223 " reduceMachine();\n"
227 " /* Get the transition that we want to extend. */\n"
228 " RedTransAp *extendTrans = list[pos].value;\n"
230 " /* Look ahead in the transition list. */\n"
231 " for ( int next = pos + 1; next < list.length(); pos++, next++ ) {\n"
232 " if ( ! keyOps->eq( list[pos].highKey, nextKey ) )\n"
250 #ifdef _____OUTPUT_____
255 /* Construct an fsmmachine from a graph. */
256 <195>RedFsmAp<197>::<195>RedFsmAp<40>( <195>FsmAp <42>*<195>graph<44>, <195>bool <195>complete <41>)
258 <195>graph<40>(<195>graph<41>)<44>,
260 <195>assert<40>( <195>sizeof<40>(<195>RedTransAp<41>) <60><<61>= <195>sizeof<40>(<195>TransAp<41>) <41>)<59>;
262 <195>reduceMachine<40>(<41>)<59>;
266 /* Get the transition that we want to extend. */
267 <195>RedTransAp <42>*<195>extendTrans <61>= <195>list<91>[<195>pos<93>]<46>.<195>value<59>;
269 /* Look ahead in the transition list. */
270 <195>for <40>( <195>int <195>next <61>= <195>pos <43>+ <218>1<59>; <195>next <60>< <195>list<46>.<195>length<40>(<41>)<59>; <195>pos<212>++<44>, <195>next<212>++ <41>) <123>{
271 <195>if <40>( <33>! <195>keyOps<211>-><195>eq<40>( <195>list<91>[<195>pos<93>]<46>.<195>highKey<44>, <195>nextKey <41>) <41>)
274 <195>return <195>false<59>;