10 const int BUFSIZE = 2048;
12 const int TK_Dlit = 192;
13 const int TK_Slit = 193;
14 const int TK_Float = 194;
15 const int TK_Id = 195;
16 const int TK_NameSep = 197;
17 const int TK_Arrow = 211;
18 const int TK_PlusPlus = 212;
19 const int TK_MinusMinus = 213;
20 const int TK_ArrowStar = 214;
21 const int TK_DotStar = 215;
22 const int TK_ShiftLeft = 216;
23 const int TK_ShiftRight = 217;
24 const int TK_IntegerDecimal = 218;
25 const int TK_IntegerOctal = 219;
26 const int TK_IntegerHex = 220;
27 const int TK_EqualsEquals = 223;
28 const int TK_NotEquals = 224;
29 const int TK_AndAnd = 225;
30 const int TK_OrOr = 226;
31 const int TK_MultAssign = 227;
32 const int TK_DivAssign = 228;
33 const int TK_PercentAssign = 229;
34 const int TK_PlusAssign = 230;
35 const int TK_MinusAssign = 231;
36 const int TK_AmpAssign = 232;
37 const int TK_CaretAssign = 233;
38 const int TK_BarAssign = 234;
39 const int TK_DotDotDot = 240;
51 void pass(char c) { nonTokBuf ~= c; }
52 void buf(char c) { tokBuf ~= c; }
56 if ( nonTokBuf.length > 0 ) {
57 printf("%.*s", nonTokBuf);
62 printf("<%d>%.*s", id, tokBuf);
72 action pass { pass(fc); }
73 action buf { buf(fc); }
75 action emit_slit { token( TK_Slit ); }
76 action emit_dlit { token( TK_Dlit ); }
77 action emit_id { token( TK_Id ); }
78 action emit_integer_decimal { token( TK_IntegerDecimal ); }
79 action emit_integer_octal { token( TK_IntegerOctal ); }
80 action emit_integer_hex { token( TK_IntegerHex ); }
81 action emit_float { token( TK_Float ); }
82 action emit_symbol { token( tokBuf[0] ); }
83 action tokst { tokStart = col; }
85 # Single and double literals.
86 slit = ( 'L'? ( "'" ( [^'\\\n] | /\\./ )* "'" ) $buf ) >tokst %emit_slit;
87 dlit = ( 'L'? ( '"' ( [^"\\\n] | /\\./ )* '"' ) $buf ) >tokst %emit_dlit;
90 id = ( [a-zA-Z_] [a-zA-Z0-9_]* ) >tokst $buf %emit_id;
93 fract_const = digit* '.' digit+ | digit+ '.';
94 exponent = [eE] [+\-]? digit+;
95 float_suffix = [flFL];
97 ( fract_const exponent? float_suffix? |
98 digit+ exponent float_suffix? ) >tokst $buf %emit_float;
100 # Integer decimal. Leading part buffered by float.
101 integer_decimal = ( ( '0' | [1-9] [0-9]* ) [ulUL]{0,3} $buf ) %emit_integer_decimal;
103 # Integer octal. Leading part buffered by float.
104 integer_octal = ( '0' [0-9]+ [ulUL]{0,2} $buf ) %emit_integer_octal;
106 # Integer hex. Leading 0 buffered by float.
107 integer_hex = ( '0' ( 'x' [0-9a-fA-F]+ [ulUL]{0,2} ) $buf ) %emit_integer_hex;
109 # Only buffer the second item, first buffered by symbol. */
110 namesep = '::' @buf %{token( TK_NameSep );};
111 deqs = '==' @buf %{token( TK_EqualsEquals );};
112 neqs = '!=' @buf %{token( TK_NotEquals );};
113 and_and = '&&' @buf %{token( TK_AndAnd );};
114 or_or = '||' @buf %{token( TK_OrOr );};
115 mult_assign = '*=' @buf %{token( TK_MultAssign );};
116 percent_assign = '%=' @buf %{token( TK_PercentAssign );};
117 plus_assign = '+=' @buf %{token( TK_PlusAssign );};
118 minus_assign = '-=' @buf %{token( TK_MinusAssign );};
119 amp_assign = '&=' @buf %{token( TK_AmpAssign );};
120 caret_assign = '^=' @buf %{token( TK_CaretAssign );};
121 bar_assign = '|=' @buf %{token( TK_BarAssign );};
122 plus_plus = '++' @buf %{token( TK_PlusPlus );};
123 minus_minus = '--' @buf %{token( TK_MinusMinus );};
124 arrow = '->' @buf %{token( TK_Arrow );};
125 arrow_star = '->*' @buf %{token( TK_ArrowStar );};
126 dot_star = '.*' @buf %{token( TK_DotStar );};
128 # Buffer both items. *
129 div_assign = '/=' @{buf('/');buf(fc);} %{token( TK_DivAssign );};
131 # Double dot is sent as two dots.
132 dot_dot = '..' %{token('.'); buf('.'); token('.');};
134 # Three char compounds, first item already buffered. */
135 dot_dot_dot = '...' %{buf('.'); buf('.'); token( TK_DotDotDot );};
138 compound = namesep | deqs | neqs | and_and | or_or | mult_assign |
139 div_assign | percent_assign | plus_assign | minus_assign |
140 amp_assign | caret_assign | bar_assign | plus_plus | minus_minus |
141 arrow | arrow_star | dot_star | dot_dot | dot_dot_dot;
143 # Single char symbols.
145 ( punct - [./_"'] ) >tokst $buf %emit_symbol |
146 # Do not immediately buffer slash, may be start of comment.
147 '/' >tokst %{ buf('/'); token( '/' ); } |
148 # Dot covered by float.
151 # Comments and whitespace.
152 commc = '/*' @{pass('/'); pass('*');} ( any* $0 '*/' @1 ) $pass;
153 commcc = '//' @{pass('/'); pass('/');} ( any* $0 '\n' @1 ) $pass;
154 whitespace = ( any - ( 0 | 33..126 ) )+ $pass;
157 /* On EOF char, write out the non token buffer. */
158 printf("%.*s", nonTokBuf);
162 # Using 0 as eof. If seeingAs a result all null characters get ignored.
165 # All outside code tokens.
167 id | slit | dlit | float | integer_decimal |
168 integer_octal | integer_hex | compound | symbol );
169 nontok = ( commc | commcc | whitespace | EOF );
172 '\n' @{ line += 1; col = 1; } |
173 [^\n] @{ col += 1; } )*;
175 main := ( ( tokens | nontok )** ) & position;
178 %% write data noprefix;
182 /* A count of the number of characters in
183 * a token. Used for % sequences. */
191 int execute( char* _data, int _len )
194 char *pe = _data + _len;
201 if ( cs >= first_final )
206 // Indicate that there is no more data. Returns -1 if the machine finishes
207 // in the error state and does not accept, 0 if the machine finishes
208 // in any other non-accepting state and 1 if the machine finishes in an
214 if ( cs >= first_final )
220 void test(char[] buf)
222 Scanner scanner = new Scanner();
224 scanner.execute( buf.ptr, buf.length );
226 /* The last token is ignored (because there is no next token). Send
227 * trailing null to force the last token into whitespace. */
229 if ( scanner.execute( &eof_char, 1 ) <= 0 ) {
230 fprintf(stderr, "cppscan: scan failed\n");
241 "RedTransAp *RedFsmAp::reduceTrans( TransAp *trans )\n"
243 " RedAction *action = 0;\n"
244 " if ( trans->actionTable.length() > 0 ) {\n"
245 " if ( actionMap.insert( trans->actionTable, &action ) )\n"
246 " action->id = nextActionId++;\n"
249 " RedStateAp *targ = (RedStateAp*)trans->toState;\n"
250 " if ( action == 0 ) {\n"
255 " trans->~TransAp();\n"
256 " inDict = new(trans) RedTransAp( targ, action, nextTransId++ );\n"
257 " transSet.insert( inDict );\n"
278 <195>RedTransAp <42>*<195>RedFsmAp<197>::<195>reduceTrans<40>( <195>TransAp <42>*<195>trans <41>)
280 <195>RedAction <42>*<195>action <61>= <218>0<59>;
281 <195>if <40>( <195>trans<211>-><195>actionTable<46>.<195>length<40>(<41>) <62>> <218>0 <41>) <123>{
282 <195>if <40>( <195>actionMap<46>.<195>insert<40>( <195>trans<211>-><195>actionTable<44>, <38>&<195>action <41>) <41>)
283 <195>action<211>-><195>id <61>= <195>nextActionId<212>++<59>;
286 <195>RedStateAp <42>*<195>targ <61>= <40>(<195>RedStateAp<42>*<41>)<195>trans<211>-><195>toState<59>;
287 <195>if <40>( <195>action <223>== <218>0 <41>) <123>{
288 <195>delete <195>trans<59>;
289 <195>return <218>0<59>;
292 <195>trans<211>-><126>~<195>TransAp<40>(<41>)<59>;
293 <195>inDict <61>= <195>new<40>(<195>trans<41>) <195>RedTransAp<40>( <195>targ<44>, <195>action<44>, <195>nextTransId<212>++ <41>)<59>;
294 <195>transSet<46>.<195>insert<40>( <195>inDict <41>)<59>;