Write statements are no longer in ragel_def elements. They appear on their own
[external/ragel.git] / ragel / rlscan.rl
1 /*
2  *  Copyright 2006 Adrian Thurston <thurston@cs.queensu.ca>
3  */
4
5 /*  This file is part of Ragel.
6  *
7  *  Ragel is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  * 
12  *  Ragel is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  * 
17  *  You should have received a copy of the GNU General Public License
18  *  along with Ragel; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
20  */
21
22 #include <iostream>
23 #include <fstream>
24 #include <string.h>
25
26 #include "ragel.h"
27 #include "rlparse.h"
28 #include "parsedata.h"
29 #include "avltree.h"
30 #include "vector.h"
31
32
33 using std::ifstream;
34 using std::istream;
35 using std::ostream;
36 using std::cout;
37 using std::cerr;
38 using std::endl;
39
40 /* This is used for tracking the current stack of include file/machine pairs. It is
41  * is used to detect and recursive include structure. */
42 struct IncludeStackItem
43 {
44         IncludeStackItem( char *fileName, char *sectionName )
45                 : fileName(fileName), sectionName(sectionName) {}
46
47         char *fileName;
48         char *sectionName;
49 };
50
51 typedef Vector<IncludeStackItem> IncludeStack;
52
53 enum InlineBlockType
54 {
55         CurlyDelimited,
56         SemiTerminated
57 };
58
59 struct Scanner
60 {
61         Scanner( char *fileName, istream &input, ostream &output,
62                         Parser *inclToParser, char *inclSectionTarg,
63                         int includeDepth )
64         : 
65                 fileName(fileName), input(input), output(output),
66                 inclToParser(inclToParser),
67                 inclSectionTarg(inclSectionTarg),
68                 includeDepth(includeDepth),
69                 line(1), column(1), lastnl(0), 
70                 parser(0), active(false), 
71                 parserExistsError(false),
72                 whitespaceOn(true)
73                 {}
74
75         bool recursiveInclude( char *inclFileName, char *inclSectionName );
76
77         char *prepareFileName( char *fileName, int len )
78         {
79                 bool caseInsensitive;
80                 Token tokenFnStr, tokenRes;
81                 tokenFnStr.data = fileName;
82                 tokenFnStr.length = len;
83                 tokenFnStr.prepareLitString( tokenRes, caseInsensitive );
84                 return tokenRes.data;
85         }
86
87         void init();
88         void token( int type, char *start, char *end );
89         void token( int type, char c );
90         void token( int type );
91         void updateCol();
92         void startSection();
93         void endSection();
94         void do_scan();
95         bool parserExists();
96         ostream &scan_error();
97
98         char *fileName;
99         istream &input;
100         ostream &output;
101         Parser *inclToParser;
102         char *inclSectionTarg;
103         int includeDepth;
104
105         int cs;
106         int line;
107         char *word, *lit;
108         int word_len, lit_len;
109         InputLoc sectionLoc;
110         char *tokstart, *tokend;
111         int column;
112         char *lastnl;
113
114         /* Set by machine statements, these persist from section to section
115          * allowing for unnamed sections. */
116         Parser *parser;
117         bool active;
118         IncludeStack includeStack;
119
120         /* This is set if ragel has already emitted an error stating that
121          * no section name has been seen and thus no parser exists. */
122         bool parserExistsError;
123
124         /* This is for inline code. By default it is on. It goes off for
125          * statements and values in inline blocks which are parsed. */
126         bool whitespaceOn;
127 };
128
129 %%{
130         machine section_parse;
131         alphtype int;
132         write data;
133 }%%
134
135 void Scanner::init( )
136 {
137         %% write init;
138 }
139
140 bool Scanner::parserExists()
141 {
142         if ( parser != 0 )
143                 return true;
144
145         if ( ! parserExistsError ) {
146                 scan_error() << "include: there is no previous specification name" << endl;
147                 parserExistsError = true;
148         }
149         return false;
150 }
151
152 ostream &Scanner::scan_error()
153 {
154         /* Maintain the error count. */
155         gblErrorCount += 1;
156         cerr << fileName << ":" << line << ":" << column << ": ";
157         return cerr;
158 }
159
160 bool Scanner::recursiveInclude( char *inclFileName, char *inclSectionName )
161 {
162         for ( IncludeStack::Iter si = includeStack; si.lte(); si++ ) {
163                 if ( strcmp( si->fileName, inclFileName ) == 0 &&
164                                 strcmp( si->sectionName, inclSectionName ) == 0 )
165                 {
166                         return true;
167                 }
168         }
169         return false;   
170 }
171
172 void Scanner::updateCol()
173 {
174         char *from = lastnl;
175         if ( from == 0 )
176                 from = tokstart;
177         //cerr << "adding " << tokend - from << " to column" << endl;
178         column += tokend - from;
179         lastnl = 0;
180 }
181
182 void Scanner::token( int type, char c )
183 {
184         token( type, &c, &c + 1 );
185 }
186
187 void Scanner::token( int type )
188 {
189         token( type, 0, 0 );
190 }
191
192 %%{
193         machine section_parse;
194
195         # This relies on the the kelbt implementation and the order
196         # that tokens are declared.
197         KW_Machine = 128;
198         KW_Include = 129;
199         KW_Write = 130;
200         TK_Word = 131;
201         TK_Literal = 132;
202
203         action clear_words { word = lit = 0; word_len = lit_len = 0; }
204         action store_word { word = tokdata; word_len = toklen; }
205         action store_lit { lit = tokdata; lit_len = toklen; }
206
207         action mach_err { scan_error() << "bad machine statement" << endl; }
208         action incl_err { scan_error() << "bad include statement" << endl; }
209         action write_err { scan_error() << "bad write statement" << endl; }
210
211         action handle_machine
212         {
213                 /* Assign a name to the machine. */
214                 char *machine = word;
215
216                 if ( inclSectionTarg == 0 ) {
217                         active = true;
218
219                         ParserDictEl *pdEl = parserDict.find( machine );
220                         if ( pdEl == 0 ) {
221                                 pdEl = new ParserDictEl( machine );
222                                 pdEl->value = new Parser( fileName, machine, sectionLoc );
223                                 pdEl->value->init();
224                                 parserDict.insert( pdEl );
225                         }
226
227                         parser = pdEl->value;
228                 }
229                 else if ( strcmp( inclSectionTarg, machine ) == 0 ) {
230                         /* found include target */
231                         active = true;
232                         parser = inclToParser;
233                 }
234                 else {
235                         /* ignoring section */
236                         active = false;
237                         parser = 0;
238                 }
239         }
240
241         machine_stmt =
242                 ( KW_Machine TK_Word @store_word ';' ) @handle_machine
243                 <>err mach_err <>eof mach_err;
244
245         action handle_include
246         {
247                 if ( active && parserExists() ) {
248                         char *inclSectionName = word;
249                         char *inclFileName = 0;
250
251                         /* Implement defaults for the input file and section name. */
252                         if ( inclSectionName == 0 )
253                                 inclSectionName = parser->sectionName;
254
255                         if ( lit != 0 ) 
256                                 inclFileName = prepareFileName( lit, lit_len );
257                         else
258                                 inclFileName = fileName;
259
260                         /* Check for a recursive include structure. Add the current file/section
261                          * name then check if what we are including is already in the stack. */
262                         includeStack.append( IncludeStackItem( fileName, parser->sectionName ) );
263
264                         if ( recursiveInclude( inclFileName, inclSectionName ) )
265                                 scan_error() << "include: this is a recursive include operation" << endl;
266                         else {
267                                 /* Open the input file for reading. */
268                                 ifstream *inFile = new ifstream( inclFileName );
269                                 if ( ! inFile->is_open() ) {
270                                         scan_error() << "include: could not open " << 
271                                                         inclFileName << " for reading" << endl;
272                                 }
273
274                                 Scanner scanner( inclFileName, *inFile, output, parser,
275                                                 inclSectionName, includeDepth+1 );
276                                 scanner.init();
277                                 scanner.do_scan( );
278                                 delete inFile;
279                         }
280
281                         /* Remove the last element (len-1) */
282                         includeStack.remove( -1 );
283                 }
284         }
285
286         include_names = (
287                 TK_Word @store_word ( TK_Literal @store_lit )? |
288                 TK_Literal @store_lit
289         ) >clear_words;
290
291         include_stmt =
292                 ( KW_Include include_names ';' ) @handle_include
293                 <>err incl_err <>eof incl_err;
294
295         action write_command
296         {
297                 if ( active ) {
298                         if ( strcmp( tokdata, "data" ) != 0 &&
299                                         strcmp( tokdata, "init" ) != 0 &&
300                                         strcmp( tokdata, "exec" ) != 0 &&
301                                         strcmp( tokdata, "eof" ) != 0 )
302                         {
303                                 scan_error() << "unknown write command" << endl;
304                         }
305                         output << "<write def_name=\"" << parser->sectionName << 
306                                         "\" what=\"" << tokdata << "\">";
307                 }
308         }
309
310         action write_option
311         {
312                 if ( active )
313                         output << "<option>" << tokdata << "</option>";
314         }
315         action write_close
316         {
317                 if ( active )
318                         output << "</write>\n";
319         }
320
321         write_stmt =
322                 ( KW_Write TK_Word @write_command 
323                         ( TK_Word @write_option )* ';' @write_close )
324                 <>err write_err <>eof write_err;
325
326         action handle_token
327         {
328                 /* Send the token off to the parser. */
329                 if ( active && parserExists() ) {
330                         InputLoc loc;
331
332                         #if 0
333                         cerr << "scanner:" << line << ":" << column << 
334                                         ": sending token to the parser " << lelNames[*p];
335                         cerr << " " << toklen;
336                         if ( tokdata != 0 )
337                                 cerr << " " << tokdata;
338                         cerr << endl;
339                         #endif
340
341                         loc.fileName = fileName;
342                         loc.line = line;
343                         loc.col = column;
344
345                         parser->token( loc, type, tokdata, toklen );
346                 }
347         }
348
349         # Catch everything else.
350         everything_else = ^( KW_Machine | KW_Include | KW_Write ) @handle_token;
351
352         main := ( 
353                 machine_stmt |
354                 include_stmt |
355                 write_stmt |
356                 everything_else
357         )*;
358 }%%
359
360 void Scanner::token( int type, char *start, char *end )
361 {
362         char *tokdata = 0;
363         int toklen = 0;
364         int *p = &type;
365         int *pe = &type + 1;
366
367         if ( start != 0 ) {
368                 toklen = end-start;
369                 tokdata = new char[toklen+1];
370                 memcpy( tokdata, start, toklen );
371                 tokdata[toklen] = 0;
372         }
373
374         %%{
375                 machine section_parse;
376                 write exec;
377         }%%
378
379         updateCol();
380 }
381
382 void Scanner::startSection( )
383 {
384         parserExistsError = false;
385
386         if ( includeDepth == 0 ) {
387                 if ( machineSpec == 0 && machineName == 0 )
388                         output << "</host>\n";
389         }
390
391         sectionLoc.fileName = fileName;
392         sectionLoc.line = line;
393         sectionLoc.col = 0;
394 }
395
396 void Scanner::endSection( )
397 {
398         /* Execute the eof actions for the section parser. */
399         %%{
400                 machine section_parse;
401                 write eof;
402         }%%
403
404         /* Close off the section with the parser. */
405         if ( active && parserExists() ) {
406                 InputLoc loc;
407                 loc.fileName = fileName;
408                 loc.line = line;
409                 loc.col = 0;
410
411                 parser->token( loc, TK_EndSection, 0, 0 );
412         }
413
414         if ( includeDepth == 0 ) {
415                 if ( machineSpec == 0 && machineName == 0 ) {
416                         /* The end section may include a newline on the end, so
417                          * we use the last line, which will count the newline. */
418                         output << "<host line=\"" << line << "\">";
419                 }
420         }
421 }
422
423 %%{
424         machine rlscan;
425
426         # This is sent by the driver code.
427         EOF = 0;
428         
429         action inc_nl { 
430                 lastnl = p; 
431                 column = 0;
432                 line++;
433         }
434         NL = '\n' @inc_nl;
435
436         # Identifiers, numbers, commetns, and other common things.
437         ident = ( alpha | '_' ) ( alpha |digit |'_' )*;
438         number = digit+;
439         hex_number = '0x' [0-9a-fA-F]+;
440
441         c_comment = 
442                 '/*' ( any | NL )* :>> '*/';
443
444         cpp_comment =
445                 '//' [^\n]* NL;
446
447         c_cpp_comment = c_comment | cpp_comment;
448
449         # These literal forms are common to C-like host code and ragel.
450         s_literal = "'" ([^'\\] | NL | '\\' (any | NL))* "'";
451         d_literal = '"' ([^"\\] | NL | '\\' (any | NL))* '"';
452
453         whitespace = [ \t] | NL;
454         pound_comment = '#' [^\n]* NL;
455
456         # An inline block of code. This is specified as a scanned, but is sent to
457         # the parser as one long block. The inline_block pointer is used to handle
458         # the preservation of the data.
459         inline_code := |*
460                 # Inline expression keywords.
461                 "fpc" => { token( KW_PChar ); };
462                 "fc" => { token( KW_Char ); };
463                 "fcurs" => { token( KW_CurState ); };
464                 "ftargs" => { token( KW_TargState ); };
465                 "fentry" => { 
466                         whitespaceOn = false; 
467                         token( KW_Entry );
468                 };
469
470                 # Inline statement keywords.
471                 "fhold" => { 
472                         whitespaceOn = false; 
473                         token( KW_Hold );
474                 };
475                 "fexec" => { token( KW_Exec, 0, 0 ); };
476                 "fgoto" => { 
477                         whitespaceOn = false; 
478                         token( KW_Goto );
479                 };
480                 "fnext" => { 
481                         whitespaceOn = false; 
482                         token( KW_Next );
483                 };
484                 "fcall" => { 
485                         whitespaceOn = false; 
486                         token( KW_Call );
487                 };
488                 "fret" => { 
489                         whitespaceOn = false; 
490                         token( KW_Ret );
491                 };
492                 "fbreak" => { 
493                         whitespaceOn = false; 
494                         token( KW_Break );
495                 };
496
497                 ident => { token( TK_Word, tokstart, tokend ); };
498
499                 number => { token( TK_UInt, tokstart, tokend ); };
500                 hex_number => { token( TK_Hex, tokstart, tokend ); };
501
502                 ( s_literal | d_literal ) 
503                         => { token( IL_Literal, tokstart, tokend ); };
504
505                 whitespace+ => { 
506                         if ( whitespaceOn ) 
507                                 token( IL_WhiteSpace, tokstart, tokend );
508                 };
509                 c_cpp_comment => { token( IL_Comment, tokstart, tokend ); };
510
511                 "::" => { token( TK_NameSep, tokstart, tokend ); };
512
513                 # Some symbols need to go to the parser as with their cardinal value as
514                 # the token type (as opposed to being sent as anonymous symbols)
515                 # because they are part of the sequences which we interpret. The * ) ;
516                 # symbols cause whitespace parsing to come back on. This gets turned
517                 # off by some keywords.
518
519                 ";" => {
520                         whitespaceOn = true;
521                         token( *tokstart, tokstart, tokend );
522                         if ( inlineBlockType == SemiTerminated )
523                                 fgoto parser_def;
524                 };
525
526                 [*)] => { 
527                         whitespaceOn = true;
528                         token( *tokstart, tokstart, tokend );
529                 };
530
531                 [,(] => { token( *tokstart, tokstart, tokend ); };
532
533                 '{' => { 
534                         token( IL_Symbol, tokstart, tokend );
535                         curly_count += 1; 
536                 };
537
538                 '}' => { 
539                         if ( --curly_count == 0 && inlineBlockType == CurlyDelimited ) {
540                                 /* Inline code block ends. */
541                                 token( '}' );
542                                 fgoto parser_def;
543                         }
544                         else {
545                                 /* Either a semi terminated inline block or only the closing
546                                  * brace of some inner scope, not the block's closing brace. */
547                                 token( IL_Symbol, tokstart, tokend );
548                         }
549                 };
550
551                 EOF => {
552                         scan_error() << "unterminated code block" << endl;
553                 };
554
555                 # Send every other character as a symbol.
556                 any => { token( IL_Symbol, tokstart, tokend ); };
557         *|;
558
559         or_literal := |*
560                 # Escape sequences in OR expressions.
561                 '\\0' => { token( RE_Char, '\0' ); };
562                 '\\a' => { token( RE_Char, '\a' ); };
563                 '\\b' => { token( RE_Char, '\b' ); };
564                 '\\t' => { token( RE_Char, '\t' ); };
565                 '\\n' => { token( RE_Char, '\n' ); };
566                 '\\v' => { token( RE_Char, '\v' ); };
567                 '\\f' => { token( RE_Char, '\f' ); };
568                 '\\r' => { token( RE_Char, '\r' ); };
569                 '\\\n' => { updateCol(); };
570                 '\\' any => { token( RE_Char, tokstart+1, tokend ); };
571
572                 # Range dash in an OR expression.
573                 '-' => { token( RE_Dash, 0, 0 ); };
574
575                 # Terminate an OR expression.
576                 ']'     => { token( RE_SqClose ); fret; };
577
578                 EOF => {
579                         scan_error() << "unterminated OR literal" << endl;
580                 };
581
582                 # Characters in an OR expression.
583                 [^\]] => { token( RE_Char, tokstart, tokend ); };
584
585         *|;
586
587         re_literal := |*
588                 # Escape sequences in regular expressions.
589                 '\\0' => { token( RE_Char, '\0' ); };
590                 '\\a' => { token( RE_Char, '\a' ); };
591                 '\\b' => { token( RE_Char, '\b' ); };
592                 '\\t' => { token( RE_Char, '\t' ); };
593                 '\\n' => { token( RE_Char, '\n' ); };
594                 '\\v' => { token( RE_Char, '\v' ); };
595                 '\\f' => { token( RE_Char, '\f' ); };
596                 '\\r' => { token( RE_Char, '\r' ); };
597                 '\\\n' => { updateCol(); };
598                 '\\' any => { token( RE_Char, tokstart+1, tokend ); };
599
600                 # Terminate an OR expression.
601                 '/' [i]? => { 
602                         token( RE_Slash, tokstart, tokend ); 
603                         fgoto parser_def;
604                 };
605
606                 # Special characters.
607                 '.' => { token( RE_Dot ); };
608                 '*' => { token( RE_Star ); };
609
610                 '[' => { token( RE_SqOpen ); fcall or_literal; };
611                 '[^' => { token( RE_SqOpenNeg ); fcall or_literal; };
612
613                 EOF => {
614                         scan_error() << "unterminated regular expression" << endl;
615                 };
616
617                 # Characters in an OR expression.
618                 [^\/] => { token( RE_Char, tokstart, tokend ); };
619         *|;
620
621         # We need a separate token space here to avoid the ragel keywords.
622         write_statement := |*
623                 ident => { token( TK_Word, tokstart, tokend ); } ;
624                 [ \t\n]+ => { updateCol(); };
625                 ';' => { token( ';' ); fgoto parser_def; };
626
627                 EOF => {
628                         scan_error() << "unterminated write statement" << endl;
629                 };
630         *|;
631
632         # Parser definitions. 
633         parser_def := |*
634                 'machine' => { token( KW_Machine ); };
635                 'include' => { token( KW_Include ); };
636                 'write' => { 
637                         token( KW_Write );
638                         fgoto write_statement;
639                 };
640                 'action' => { token( KW_Action ); };
641                 'alphtype' => { token( KW_AlphType ); };
642
643                 # FIXME: Enable this post 5.17.
644                 # 'range' => { token( KW_Range ); };
645
646                 'getkey' => { 
647                         token( KW_GetKey );
648                         inlineBlockType = SemiTerminated;
649                         fgoto inline_code;
650                 };
651                 'access' => { 
652                         token( KW_Access );
653                         inlineBlockType = SemiTerminated;
654                         fgoto inline_code;
655                 };
656                 'variable' => { 
657                         token( KW_Variable );
658                         inlineBlockType = SemiTerminated;
659                         fgoto inline_code;
660                 };
661                 'when' => { token( KW_When ); };
662                 'eof' => { token( KW_Eof ); };
663                 'err' => { token( KW_Err ); };
664                 'lerr' => { token( KW_Lerr ); };
665                 'to' => { token( KW_To ); };
666                 'from' => { token( KW_From ); };
667
668                 # Identifiers.
669                 ident => { token( TK_Word, tokstart, tokend ); } ;
670
671                 # Numbers
672                 number => { token( TK_UInt, tokstart, tokend ); };
673                 hex_number => { token( TK_Hex, tokstart, tokend ); };
674
675                 # Literals, with optionals.
676                 ( s_literal | d_literal ) [i]? 
677                         => { token( TK_Literal, tokstart, tokend ); };
678
679                 '[' => { token( RE_SqOpen ); fcall or_literal; };
680                 '[^' => { token( RE_SqOpenNeg ); fcall or_literal; };
681
682                 '/' => { token( RE_Slash ); fgoto re_literal; };
683
684                 # Ignore.
685                 pound_comment => { updateCol(); };
686
687                 ':=' => { token( TK_ColonEquals ); };
688
689                 # To State Actions.
690                 ">~" => { token( TK_StartToState ); };
691                 "$~" => { token( TK_AllToState ); };
692                 "%~" => { token( TK_FinalToState ); };
693                 "<~" => { token( TK_NotStartToState ); };
694                 "@~" => { token( TK_NotFinalToState ); };
695                 "<>~" => { token( TK_MiddleToState ); };
696
697                 # From State actions
698                 ">*" => { token( TK_StartFromState ); };
699                 "$*" => { token( TK_AllFromState ); };
700                 "%*" => { token( TK_FinalFromState ); };
701                 "<*" => { token( TK_NotStartFromState ); };
702                 "@*" => { token( TK_NotFinalFromState ); };
703                 "<>*" => { token( TK_MiddleFromState ); };
704
705                 # EOF Actions.
706                 ">/" => { token( TK_StartEOF ); };
707                 "$/" => { token( TK_AllEOF ); };
708                 "%/" => { token( TK_FinalEOF ); };
709                 "</" => { token( TK_NotStartEOF ); };
710                 "@/" => { token( TK_NotFinalEOF ); };
711                 "<>/" => { token( TK_MiddleEOF ); };
712
713                 # Global Error actions.
714                 ">!" => { token( TK_StartGblError ); };
715                 "$!" => { token( TK_AllGblError ); };
716                 "%!" => { token( TK_FinalGblError ); };
717                 "<!" => { token( TK_NotStartGblError ); };
718                 "@!" => { token( TK_NotFinalGblError ); };
719                 "<>!" => { token( TK_MiddleGblError ); };
720
721                 # Local error actions.
722                 ">^" => { token( TK_StartLocalError ); };
723                 "$^" => { token( TK_AllLocalError ); };
724                 "%^" => { token( TK_FinalLocalError ); };
725                 "<^" => { token( TK_NotStartLocalError ); };
726                 "@^" => { token( TK_NotFinalLocalError ); };
727                 "<>^" => { token( TK_MiddleLocalError ); };
728
729                 # Middle.
730                 "<>" => { token( TK_Middle ); };
731
732                 # Conditions. 
733                 '>?' => { token( TK_StartCond ); };
734                 '$?' => { token( TK_AllCond ); };
735                 '%?' => { token( TK_LeavingCond ); };
736
737                 '..' => { token( TK_DotDot ); };
738                 '**' => { token( TK_StarStar ); };
739                 '--' => { token( TK_DashDash ); };
740                 '->' => { token( TK_Arrow ); };
741                 '=>' => { token( TK_DoubleArrow ); };
742
743                 ":>"  => { token( TK_ColonGt ); };
744                 ":>>" => { token( TK_ColonGtGt ); };
745                 "<:"  => { token( TK_LtColon ); };
746
747                 # Opening of longest match.
748                 "|*" => { token( TK_BarStar ); };
749
750                 '}%%' => { 
751                         updateCol();
752                         endSection();
753                         fgoto main;
754                 };
755
756                 [ \t]+ => { updateCol(); };
757
758                 # If we are in a single line machine then newline may end the spec.
759                 NL => {
760                         updateCol();
761                         if ( singleLineSpec ) {
762                                 endSection();
763                                 fgoto main;
764                         }
765                 };
766
767                 '{' => { 
768                         token( '{' );
769                         curly_count = 1; 
770                         inlineBlockType = CurlyDelimited;
771                         fgoto inline_code;
772                 };
773
774                 EOF => {
775                         scan_error() << "unterminated ragel section" << endl;
776                 };
777
778                 any => { token( *tokstart ); } ;
779         *|;
780
781         action pass {
782                 updateCol();
783
784                 /* If no errors and we are at the bottom of the include stack (the
785                  * source file listed on the command line) then write out the data. */
786                 if ( includeDepth == 0 && machineSpec == 0 && machineName == 0 )
787                         xmlEscapeHost( output, tokstart, tokend-tokstart );
788         }
789
790         # Outside code scanner. These tokens get passed through.
791         main := |*
792                 ident => pass;
793                 number => pass;
794                 c_cpp_comment => pass;
795                 s_literal | d_literal => pass;
796                 '%%{' => { 
797                         updateCol();
798                         singleLineSpec = false;
799                         startSection();
800                         fgoto parser_def;
801                 };
802                 '%%' => { 
803                         updateCol();
804                         singleLineSpec = true;
805                         startSection();
806                         fgoto parser_def;
807                 };
808                 whitespace+ => pass;
809                 EOF;
810                 any => pass;
811         *|;
812
813 }%%
814
815 %% write data;
816
817 void Scanner::do_scan()
818 {
819         int bufsize = 8;
820         char *buf = new char[bufsize];
821         const char last_char = 0;
822         int cs, act, have = 0;
823         int top, stack[1];
824         int curly_count = 0;
825         bool execute = true;
826         bool singleLineSpec = false;
827         InlineBlockType inlineBlockType;
828
829         %% write init;
830
831         while ( execute ) {
832                 char *p = buf + have;
833                 int space = bufsize - have;
834
835                 if ( space == 0 ) {
836                         /* We filled up the buffer trying to scan a token. Grow it. */
837                         bufsize = bufsize * 2;
838                         char *newbuf = new char[bufsize];
839
840                         /* Recompute p and space. */
841                         p = newbuf + have;
842                         space = bufsize - have;
843
844                         /* Patch up pointers possibly in use. */
845                         if ( tokstart != 0 )
846                                 tokstart = newbuf + ( tokstart - buf );
847                         tokend = newbuf + ( tokend - buf );
848
849                         /* Copy the new buffer in. */
850                         memcpy( newbuf, buf, have );
851                         delete[] buf;
852                         buf = newbuf;
853                 }
854
855                 input.read( p, space );
856                 int len = input.gcount();
857
858                 /* If we see eof then append the EOF char. */
859                 if ( len == 0 ) {
860                         p[0] = last_char, len = 1;
861                         execute = false;
862                 }
863
864                 char *pe = p + len;
865                 %% write exec;
866
867                 /* Check if we failed. */
868                 if ( cs == rlscan_error ) {
869                         /* Machine failed before finding a token. I'm not yet sure if this
870                          * is reachable. */
871                         scan_error() << "scanner error" << endl;
872                         exit(1);
873                 }
874
875                 /* Decide if we need to preserve anything. */
876                 char *preserve = tokstart;
877
878                 /* Now set up the prefix. */
879                 if ( preserve == 0 )
880                         have = 0;
881                 else {
882                         /* There is data that needs to be shifted over. */
883                         have = pe - preserve;
884                         memmove( buf, preserve, have );
885                         unsigned int shiftback = preserve - buf;
886                         if ( tokstart != 0 )
887                                 tokstart -= shiftback;
888                         tokend -= shiftback;
889
890                         preserve = buf;
891                 }
892         }
893
894         delete[] buf;
895 }
896
897 void scan( char *fileName, istream &input, ostream &output )
898 {
899         Scanner scanner( fileName, input, output, 0, 0, 0 );
900         scanner.init();
901         scanner.do_scan();
902
903         InputLoc eofLoc;
904         eofLoc.fileName = fileName;
905         eofLoc.col = 1;
906         eofLoc.line = scanner.line;
907 }