2 * Copyright 2006-2007 Adrian Thurston <thurston@cs.queensu.ca>
5 /* This file is part of Ragel.
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.
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.
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
52 * The Scanner for Importing
56 machine inline_token_scan;
60 # Import scanner tokens.
65 IMP_Define IMP_Word IMP_UInt => {
66 int base = tok_ts - token_data;
70 directToParser( inclToParser, fileName, line, column, TK_Word,
71 token_strings[base+nameOff], token_lens[base+nameOff] );
72 directToParser( inclToParser, fileName, line, column, '=', 0, 0 );
73 directToParser( inclToParser, fileName, line, column, TK_UInt,
74 token_strings[base+numOff], token_lens[base+numOff] );
75 directToParser( inclToParser, fileName, line, column, ';', 0, 0 );
78 # Assignment of number.
79 IMP_Word '=' IMP_UInt => {
80 int base = tok_ts - token_data;
84 directToParser( inclToParser, fileName, line, column, TK_Word,
85 token_strings[base+nameOff], token_lens[base+nameOff] );
86 directToParser( inclToParser, fileName, line, column, '=', 0, 0 );
87 directToParser( inclToParser, fileName, line, column, TK_UInt,
88 token_strings[base+numOff], token_lens[base+numOff] );
89 directToParser( inclToParser, fileName, line, column, ';', 0, 0 );
93 IMP_Define IMP_Word IMP_Literal => {
94 int base = tok_ts - token_data;
98 directToParser( inclToParser, fileName, line, column, TK_Word,
99 token_strings[base+nameOff], token_lens[base+nameOff] );
100 directToParser( inclToParser, fileName, line, column, '=', 0, 0 );
101 directToParser( inclToParser, fileName, line, column, TK_Literal,
102 token_strings[base+litOff], token_lens[base+litOff] );
103 directToParser( inclToParser, fileName, line, column, ';', 0, 0 );
106 # Assignment of literal.
107 IMP_Word '=' IMP_Literal => {
108 int base = tok_ts - token_data;
112 directToParser( inclToParser, fileName, line, column, TK_Word,
113 token_strings[base+nameOff], token_lens[base+nameOff] );
114 directToParser( inclToParser, fileName, line, column, '=', 0, 0 );
115 directToParser( inclToParser, fileName, line, column, TK_Literal,
116 token_strings[base+litOff], token_lens[base+litOff] );
117 directToParser( inclToParser, fileName, line, column, ';', 0, 0 );
120 # Catch everything else.
127 void Scanner::flushImport()
130 int *pe = token_data + cur_token;
134 machine inline_token_scan;
142 cur_token = pe - tok_ts;
143 int ts_offset = tok_ts - token_data;
144 memmove( token_data, token_data+ts_offset, cur_token*sizeof(token_data[0]) );
145 memmove( token_strings, token_strings+ts_offset, cur_token*sizeof(token_strings[0]) );
146 memmove( token_lens, token_lens+ts_offset, cur_token*sizeof(token_lens[0]) );
150 void Scanner::directToParser( Parser *toParser, char *tokFileName, int tokLine,
151 int tokColumn, int type, char *tokdata, int toklen )
156 cerr << "scanner:" << tokLine << ":" << tokColumn <<
157 ": sending token to the parser " << Parser_lelNames[type];
158 cerr << " " << toklen;
160 cerr << " " << tokdata;
164 loc.fileName = tokFileName;
168 toParser->token( loc, type, tokdata, toklen );
171 void Scanner::importToken( int token, char *start, char *end )
173 if ( cur_token == max_tokens )
176 token_data[cur_token] = token;
178 token_strings[cur_token] = 0;
179 token_lens[cur_token] = 0;
182 int toklen = end-start;
183 token_lens[cur_token] = toklen;
184 token_strings[cur_token] = new char[toklen+1];
185 memcpy( token_strings[cur_token], start, toklen );
186 token_strings[cur_token][toklen] = 0;
191 void Scanner::pass( int token, char *start, char *end )
193 if ( importMachines )
194 importToken( token, start, end );
202 /* If no errors and we are at the bottom of the include stack (the
203 * source file listed on the command line) then write out the data. */
204 if ( includeDepth == 0 && machineSpec == 0 && machineName == 0 )
205 xmlEscapeHost( output, ts, te-ts );
209 * The scanner for processing sections, includes, imports, etc.
213 machine section_parse;
219 void Scanner::init( )
224 bool Scanner::active()
229 if ( parser == 0 && ! parserExistsError ) {
230 scan_error() << "this specification has no name, nor does any previous"
231 " specification" << endl;
232 parserExistsError = true;
241 ostream &Scanner::scan_error()
243 /* Maintain the error count. */
245 cerr << fileName << ":" << line << ":" << column << ": ";
249 bool Scanner::recursiveInclude( char *inclFileName, char *inclSectionName )
251 for ( IncludeStack::Iter si = includeStack; si.lte(); si++ ) {
252 if ( strcmp( si->fileName, inclFileName ) == 0 &&
253 strcmp( si->sectionName, inclSectionName ) == 0 )
261 void Scanner::updateCol()
266 //cerr << "adding " << te - from << " to column" << endl;
271 void Scanner::handleMachine()
273 /* Assign a name to the machine. */
274 char *machine = word;
276 if ( !importMachines && inclSectionTarg == 0 ) {
277 ignoreSection = false;
279 ParserDictEl *pdEl = parserDict.find( machine );
281 pdEl = new ParserDictEl( machine );
282 pdEl->value = new Parser( fileName, machine, sectionLoc );
284 parserDict.insert( pdEl );
287 parser = pdEl->value;
289 else if ( !importMachines && strcmp( inclSectionTarg, machine ) == 0 ) {
290 /* found include target */
291 ignoreSection = false;
292 parser = inclToParser;
295 /* ignoring section */
296 ignoreSection = true;
301 void Scanner::handleInclude()
304 char *inclSectionName = word;
305 char **inclFileName = 0;
307 /* Implement defaults for the input file and section name. */
308 if ( inclSectionName == 0 )
309 inclSectionName = parser->sectionName;
312 inclFileName = makeIncludePathChecks( fileName, lit, lit_len );
314 char *test = new char[strlen(fileName)+1];
315 strcpy( test, fileName );
317 inclFileName = new char*[2];
319 inclFileName[0] = test;
324 ifstream *inFile = tryOpenInclude( inclFileName, found );
326 scan_error() << "include: failed to locate file" << endl;
328 /* Check for a recursive include structure. Add the current file/section
329 * name then check if what we are including is already in the stack. */
330 includeStack.append( IncludeStackItem( fileName, parser->sectionName ) );
332 if ( recursiveInclude( inclFileName[found], inclSectionName ) )
333 scan_error() << "include: this is a recursive include operation" << endl;
335 Scanner scanner( inclFileName[found], *inFile, output, parser,
336 inclSectionName, includeDepth+1, false );
341 /* Remove the last element (len-1) */
342 includeStack.remove( -1 );
347 void Scanner::handleImport()
350 char **importFileName = makeIncludePathChecks( fileName, lit, lit_len );
352 /* Open the input file for reading. */
354 ifstream *inFile = tryOpenInclude( importFileName, found );
356 scan_error() << "import: could not open import file " <<
357 "for reading" << endl;
360 Scanner scanner( importFileName[found], *inFile, output, parser,
361 0, includeDepth+1, true );
363 scanner.importToken( 0, 0, 0 );
364 scanner.flushImport();
370 machine section_parse;
372 # Need the defines representing tokens.
375 action clear_words { word = lit = 0; word_len = lit_len = 0; }
376 action store_word { word = tokdata; word_len = toklen; }
377 action store_lit { lit = tokdata; lit_len = toklen; }
379 action mach_err { scan_error() << "bad machine statement" << endl; }
380 action incl_err { scan_error() << "bad include statement" << endl; }
381 action import_err { scan_error() << "bad import statement" << endl; }
382 action write_err { scan_error() << "bad write statement" << endl; }
384 action handle_machine { handleMachine(); }
385 action handle_include { handleInclude(); }
386 action handle_import { handleImport(); }
389 ( KW_Machine TK_Word @store_word ';' ) @handle_machine
390 <>err mach_err <>eof mach_err;
393 TK_Word @store_word ( TK_Literal @store_lit )? |
394 TK_Literal @store_lit
398 ( KW_Include include_names ';' ) @handle_include
399 <>err incl_err <>eof incl_err;
402 ( KW_Import TK_Literal @store_lit ';' ) @handle_import
403 <>err import_err <>eof import_err;
407 if ( active() && machineSpec == 0 && machineName == 0 ) {
409 " def_name=\"" << parser->sectionName << "\""
410 " line=\"" << line << "\""
411 " col=\"" << column << "\""
418 if ( active() && machineSpec == 0 && machineName == 0 )
419 output << "<arg>" << tokdata << "</arg>";
424 if ( active() && machineSpec == 0 && machineName == 0 )
425 output << "</write>\n";
429 ( KW_Write @write_command
430 ( TK_Word @write_arg )+ ';' @write_close )
431 <>err write_err <>eof write_err;
435 /* Send the token off to the parser. */
437 directToParser( parser, fileName, line, column, type, tokdata, toklen );
440 # Catch everything else.
442 ^( KW_Machine | KW_Include | KW_Import | KW_Write ) @handle_token;
453 void Scanner::token( int type, char c )
455 token( type, &c, &c + 1 );
458 void Scanner::token( int type )
463 void Scanner::token( int type, char *start, char *end )
469 tokdata = new char[toklen+1];
470 memcpy( tokdata, start, toklen );
474 processToken( type, tokdata, toklen );
477 void Scanner::processToken( int type, char *tokdata, int toklen )
491 machine section_parse;
497 /* Record the last token for use in controlling the scan of subsequent
502 void Scanner::startSection( )
504 parserExistsError = false;
506 if ( includeDepth == 0 ) {
507 if ( machineSpec == 0 && machineName == 0 )
508 output << "</host>\n";
511 sectionLoc.fileName = fileName;
512 sectionLoc.line = line;
516 void Scanner::endSection( )
518 /* Execute the eof actions for the section parser. */
519 processToken( -1, 0, 0 );
521 /* Close off the section with the parser. */
524 loc.fileName = fileName;
528 parser->token( loc, TK_EndSection, 0, 0 );
531 if ( includeDepth == 0 ) {
532 if ( machineSpec == 0 && machineName == 0 ) {
533 /* The end section may include a newline on the end, so
534 * we use the last line, which will count the newline. */
535 output << "<host line=\"" << line << "\">";
540 bool isAbsolutePath( const char *path )
543 return isalpha( path[0] ) && path[1] == ':' && path[2] == '\\';
545 return path[0] == '/';
549 char **Scanner::makeIncludePathChecks( char *thisFileName, char *fileName, int fnlen )
551 char **checks = new char*[2];
554 bool caseInsensitive = false;
556 char *data = prepareLitString( InputLoc(), fileName, fnlen,
557 length, caseInsensitive );
560 if ( isAbsolutePath( data ) )
561 checks[nextCheck++] = data;
563 /* Search from the the location of the current file. */
564 const char *lastSlash = strrchr( thisFileName, PATH_SEP );
565 if ( lastSlash == 0 )
566 checks[nextCheck++] = data;
568 long givenPathLen = (lastSlash - thisFileName) + 1;
569 long checklen = givenPathLen + length;
570 char *check = new char[checklen+1];
571 memcpy( check, thisFileName, givenPathLen );
572 memcpy( check+givenPathLen, data, length );
574 checks[nextCheck++] = check;
577 /* Search from the include paths given on the command line. */
578 for ( ArgsVector::Iter incp = includePaths; incp.lte(); incp++ ) {
579 long pathLen = strlen( *incp );
580 long checkLen = pathLen + 1 + length;
581 char *check = new char[checkLen+1];
582 memcpy( check, *incp, pathLen );
583 check[pathLen] = PATH_SEP;
584 memcpy( check+pathLen+1, data, length );
586 checks[nextCheck++] = check;
590 checks[nextCheck] = 0;
594 ifstream *Scanner::tryOpenInclude( char **pathChecks, long &found )
596 char **check = pathChecks;
597 ifstream *inFile = new ifstream;
599 while ( *check != 0 ) {
600 inFile->open( *check );
601 if ( inFile->is_open() ) {
602 found = check - pathChecks;
616 # This is sent by the driver code.
626 # Identifiers, numbers, commetns, and other common things.
627 ident = ( alpha | '_' ) ( alpha |digit |'_' )*;
629 hex_number = '0x' [0-9a-fA-F]+;
632 '/*' ( any | NL )* :>> '*/';
637 c_cpp_comment = c_comment | cpp_comment;
639 ruby_comment = '#' [^\n]* NL;
641 # These literal forms are common to host code and ragel.
642 s_literal = "'" ([^'\\] | NL | '\\' (any | NL))* "'";
643 d_literal = '"' ([^"\\] | NL | '\\' (any | NL))* '"';
644 host_re_literal = '/' ([^/\\] | NL | '\\' (any | NL))* '/';
646 whitespace = [ \t] | NL;
647 pound_comment = '#' [^\n]* NL;
649 # An inline block of code for Ruby.
650 inline_code_ruby := |*
651 # Inline expression keywords.
652 "fpc" => { token( KW_PChar ); };
653 "fc" => { token( KW_Char ); };
654 "fcurs" => { token( KW_CurState ); };
655 "ftargs" => { token( KW_TargState ); };
657 whitespaceOn = false;
661 # Inline statement keywords.
663 whitespaceOn = false;
666 "fexec" => { token( KW_Exec, 0, 0 ); };
668 whitespaceOn = false;
672 whitespaceOn = false;
676 whitespaceOn = false;
680 whitespaceOn = false;
684 whitespaceOn = false;
688 ident => { token( TK_Word, ts, te ); };
690 number => { token( TK_UInt, ts, te ); };
691 hex_number => { token( TK_Hex, ts, te ); };
693 ( s_literal | d_literal | host_re_literal )
694 => { token( IL_Literal, ts, te ); };
698 token( IL_WhiteSpace, ts, te );
701 ruby_comment => { token( IL_Comment, ts, te ); };
703 "::" => { token( TK_NameSep, ts, te ); };
705 # Some symbols need to go to the parser as with their cardinal value as
706 # the token type (as opposed to being sent as anonymous symbols)
707 # because they are part of the sequences which we interpret. The * ) ;
708 # symbols cause whitespace parsing to come back on. This gets turned
709 # off by some keywords.
713 token( *ts, ts, te );
714 if ( inlineBlockType == SemiTerminated )
720 token( *ts, ts, te );
723 [,(] => { token( *ts, ts, te ); };
726 token( IL_Symbol, ts, te );
731 if ( --curly_count == 0 && inlineBlockType == CurlyDelimited ) {
732 /* Inline code block ends. */
737 /* Either a semi terminated inline block or only the closing
738 * brace of some inner scope, not the block's closing brace. */
739 token( IL_Symbol, ts, te );
744 scan_error() << "unterminated code block" << endl;
747 # Send every other character as a symbol.
748 any => { token( IL_Symbol, ts, te ); };
752 # An inline block of code for languages other than Ruby.
754 # Inline expression keywords.
755 "fpc" => { token( KW_PChar ); };
756 "fc" => { token( KW_Char ); };
757 "fcurs" => { token( KW_CurState ); };
758 "ftargs" => { token( KW_TargState ); };
760 whitespaceOn = false;
764 # Inline statement keywords.
766 whitespaceOn = false;
769 "fexec" => { token( KW_Exec, 0, 0 ); };
771 whitespaceOn = false;
775 whitespaceOn = false;
779 whitespaceOn = false;
783 whitespaceOn = false;
787 whitespaceOn = false;
791 ident => { token( TK_Word, ts, te ); };
793 number => { token( TK_UInt, ts, te ); };
794 hex_number => { token( TK_Hex, ts, te ); };
796 ( s_literal | d_literal )
797 => { token( IL_Literal, ts, te ); };
801 token( IL_WhiteSpace, ts, te );
804 c_cpp_comment => { token( IL_Comment, ts, te ); };
806 "::" => { token( TK_NameSep, ts, te ); };
808 # Some symbols need to go to the parser as with their cardinal value as
809 # the token type (as opposed to being sent as anonymous symbols)
810 # because they are part of the sequences which we interpret. The * ) ;
811 # symbols cause whitespace parsing to come back on. This gets turned
812 # off by some keywords.
816 token( *ts, ts, te );
817 if ( inlineBlockType == SemiTerminated )
823 token( *ts, ts, te );
826 [,(] => { token( *ts, ts, te ); };
829 token( IL_Symbol, ts, te );
834 if ( --curly_count == 0 && inlineBlockType == CurlyDelimited ) {
835 /* Inline code block ends. */
840 /* Either a semi terminated inline block or only the closing
841 * brace of some inner scope, not the block's closing brace. */
842 token( IL_Symbol, ts, te );
847 scan_error() << "unterminated code block" << endl;
850 # Send every other character as a symbol.
851 any => { token( IL_Symbol, ts, te ); };
855 # Escape sequences in OR expressions.
856 '\\0' => { token( RE_Char, '\0' ); };
857 '\\a' => { token( RE_Char, '\a' ); };
858 '\\b' => { token( RE_Char, '\b' ); };
859 '\\t' => { token( RE_Char, '\t' ); };
860 '\\n' => { token( RE_Char, '\n' ); };
861 '\\v' => { token( RE_Char, '\v' ); };
862 '\\f' => { token( RE_Char, '\f' ); };
863 '\\r' => { token( RE_Char, '\r' ); };
864 '\\\n' => { updateCol(); };
865 '\\' any => { token( RE_Char, ts+1, te ); };
867 # Range dash in an OR expression.
868 '-' => { token( RE_Dash, 0, 0 ); };
870 # Terminate an OR expression.
871 ']' => { token( RE_SqClose ); fret; };
874 scan_error() << "unterminated OR literal" << endl;
877 # Characters in an OR expression.
878 [^\]] => { token( RE_Char, ts, te ); };
882 ragel_re_literal := |*
883 # Escape sequences in regular expressions.
884 '\\0' => { token( RE_Char, '\0' ); };
885 '\\a' => { token( RE_Char, '\a' ); };
886 '\\b' => { token( RE_Char, '\b' ); };
887 '\\t' => { token( RE_Char, '\t' ); };
888 '\\n' => { token( RE_Char, '\n' ); };
889 '\\v' => { token( RE_Char, '\v' ); };
890 '\\f' => { token( RE_Char, '\f' ); };
891 '\\r' => { token( RE_Char, '\r' ); };
892 '\\\n' => { updateCol(); };
893 '\\' any => { token( RE_Char, ts+1, te ); };
895 # Terminate an OR expression.
897 token( RE_Slash, ts, te );
901 # Special characters.
902 '.' => { token( RE_Dot ); };
903 '*' => { token( RE_Star ); };
905 '[' => { token( RE_SqOpen ); fcall or_literal; };
906 '[^' => { token( RE_SqOpenNeg ); fcall or_literal; };
909 scan_error() << "unterminated regular expression" << endl;
912 # Characters in an OR expression.
913 [^\/] => { token( RE_Char, ts, te ); };
916 # We need a separate token space here to avoid the ragel keywords.
917 write_statement := |*
918 ident => { token( TK_Word, ts, te ); } ;
919 [ \t\n]+ => { updateCol(); };
920 ';' => { token( ';' ); fgoto parser_def; };
923 scan_error() << "unterminated write statement" << endl;
927 # Parser definitions.
929 'machine' => { token( KW_Machine ); };
930 'include' => { token( KW_Include ); };
931 'import' => { token( KW_Import ); };
934 fgoto write_statement;
936 'action' => { token( KW_Action ); };
937 'alphtype' => { token( KW_AlphType ); };
938 'prepush' => { token( KW_PrePush ); };
939 'postpop' => { token( KW_PostPop ); };
941 # FIXME: Enable this post 5.17.
942 # 'range' => { token( KW_Range ); };
946 inlineBlockType = SemiTerminated;
947 if ( hostLang->lang == HostLang::Ruby )
948 fcall inline_code_ruby;
954 inlineBlockType = SemiTerminated;
955 if ( hostLang->lang == HostLang::Ruby )
956 fcall inline_code_ruby;
961 token( KW_Variable );
962 inlineBlockType = SemiTerminated;
963 if ( hostLang->lang == HostLang::Ruby )
964 fcall inline_code_ruby;
968 'when' => { token( KW_When ); };
969 'inwhen' => { token( KW_InWhen ); };
970 'outwhen' => { token( KW_OutWhen ); };
971 'eof' => { token( KW_Eof ); };
972 'err' => { token( KW_Err ); };
973 'lerr' => { token( KW_Lerr ); };
974 'to' => { token( KW_To ); };
975 'from' => { token( KW_From ); };
976 'export' => { token( KW_Export ); };
979 ident => { token( TK_Word, ts, te ); } ;
982 number => { token( TK_UInt, ts, te ); };
983 hex_number => { token( TK_Hex, ts, te ); };
985 # Literals, with optionals.
986 ( s_literal | d_literal ) [i]?
987 => { token( TK_Literal, ts, te ); };
989 '[' => { token( RE_SqOpen ); fcall or_literal; };
990 '[^' => { token( RE_SqOpenNeg ); fcall or_literal; };
992 '/' => { token( RE_Slash ); fgoto ragel_re_literal; };
995 pound_comment => { updateCol(); };
997 ':=' => { token( TK_ColonEquals ); };
1000 ">~" => { token( TK_StartToState ); };
1001 "$~" => { token( TK_AllToState ); };
1002 "%~" => { token( TK_FinalToState ); };
1003 "<~" => { token( TK_NotStartToState ); };
1004 "@~" => { token( TK_NotFinalToState ); };
1005 "<>~" => { token( TK_MiddleToState ); };
1007 # From State actions
1008 ">*" => { token( TK_StartFromState ); };
1009 "$*" => { token( TK_AllFromState ); };
1010 "%*" => { token( TK_FinalFromState ); };
1011 "<*" => { token( TK_NotStartFromState ); };
1012 "@*" => { token( TK_NotFinalFromState ); };
1013 "<>*" => { token( TK_MiddleFromState ); };
1016 ">/" => { token( TK_StartEOF ); };
1017 "$/" => { token( TK_AllEOF ); };
1018 "%/" => { token( TK_FinalEOF ); };
1019 "</" => { token( TK_NotStartEOF ); };
1020 "@/" => { token( TK_NotFinalEOF ); };
1021 "<>/" => { token( TK_MiddleEOF ); };
1023 # Global Error actions.
1024 ">!" => { token( TK_StartGblError ); };
1025 "$!" => { token( TK_AllGblError ); };
1026 "%!" => { token( TK_FinalGblError ); };
1027 "<!" => { token( TK_NotStartGblError ); };
1028 "@!" => { token( TK_NotFinalGblError ); };
1029 "<>!" => { token( TK_MiddleGblError ); };
1031 # Local error actions.
1032 ">^" => { token( TK_StartLocalError ); };
1033 "$^" => { token( TK_AllLocalError ); };
1034 "%^" => { token( TK_FinalLocalError ); };
1035 "<^" => { token( TK_NotStartLocalError ); };
1036 "@^" => { token( TK_NotFinalLocalError ); };
1037 "<>^" => { token( TK_MiddleLocalError ); };
1040 "<>" => { token( TK_Middle ); };
1043 '>?' => { token( TK_StartCond ); };
1044 '$?' => { token( TK_AllCond ); };
1045 '%?' => { token( TK_LeavingCond ); };
1047 '..' => { token( TK_DotDot ); };
1048 '**' => { token( TK_StarStar ); };
1049 '--' => { token( TK_DashDash ); };
1050 '->' => { token( TK_Arrow ); };
1051 '=>' => { token( TK_DoubleArrow ); };
1053 ":>" => { token( TK_ColonGt ); };
1054 ":>>" => { token( TK_ColonGtGt ); };
1055 "<:" => { token( TK_LtColon ); };
1057 # Opening of longest match.
1058 "|*" => { token( TK_BarStar ); };
1060 # Separater for name references.
1061 "::" => { token( TK_NameSep, ts, te ); };
1069 [ \t\r]+ => { updateCol(); };
1071 # If we are in a single line machine then newline may end the spec.
1074 if ( singleLineSpec ) {
1081 if ( lastToken == KW_Export || lastToken == KW_Entry )
1086 inlineBlockType = CurlyDelimited;
1087 if ( hostLang->lang == HostLang::Ruby )
1088 fcall inline_code_ruby;
1095 scan_error() << "unterminated ragel section" << endl;
1098 any => { token( *ts ); } ;
1101 # Outside code scanner. These tokens get passed through.
1103 ident => { pass( IMP_Word, ts, te ); };
1104 number => { pass( IMP_UInt, ts, te ); };
1105 ruby_comment => { pass(); };
1106 ( s_literal | d_literal | host_re_literal )
1107 => { pass( IMP_Literal, ts, te ); };
1111 singleLineSpec = false;
1117 singleLineSpec = true;
1121 whitespace+ => { pass(); };
1123 any => { pass( *ts, 0, 0 ); };
1126 # Outside code scanner. These tokens get passed through.
1128 'define' => { pass( IMP_Define, 0, 0 ); };
1129 ident => { pass( IMP_Word, ts, te ); };
1130 number => { pass( IMP_UInt, ts, te ); };
1131 c_cpp_comment => { pass(); };
1132 ( s_literal | d_literal ) => { pass( IMP_Literal, ts, te ); };
1136 singleLineSpec = false;
1142 singleLineSpec = true;
1146 whitespace+ => { pass(); };
1148 any => { pass( *ts, 0, 0 ); };
1154 void Scanner::do_scan()
1157 char *buf = new char[bufsize];
1158 int cs, act, have = 0;
1161 /* The stack is two deep, one level for going into ragel defs from the main
1162 * machines which process outside code, and another for going into or literals
1163 * from either a ragel spec, or a regular expression. */
1165 int curly_count = 0;
1166 bool execute = true;
1167 bool singleLineSpec = false;
1168 InlineBlockType inlineBlockType = CurlyDelimited;
1170 /* Init the section parser and the character scanner. */
1174 /* Set up the start state. FIXME: After 5.20 is released the nocs write
1175 * init option should be used, the main machine eliminated and this statement moved
1176 * above the write init. */
1177 if ( hostLang->lang == HostLang::Ruby )
1178 cs = rlscan_en_main_ruby;
1180 cs = rlscan_en_main;
1183 char *p = buf + have;
1184 int space = bufsize - have;
1187 /* We filled up the buffer trying to scan a token. Grow it. */
1188 bufsize = bufsize * 2;
1189 char *newbuf = new char[bufsize];
1191 /* Recompute p and space. */
1193 space = bufsize - have;
1195 /* Patch up pointers possibly in use. */
1197 ts = newbuf + ( ts - buf );
1198 te = newbuf + ( te - buf );
1200 /* Copy the new buffer in. */
1201 memcpy( newbuf, buf, have );
1206 input.read( p, space );
1207 int len = input.gcount();
1210 /* If we see eof then append the eof var. */
1219 /* Check if we failed. */
1220 if ( cs == rlscan_error ) {
1221 /* Machine failed before finding a token. I'm not yet sure if this
1223 scan_error() << "scanner error" << endl;
1227 /* Decide if we need to preserve anything. */
1228 char *preserve = ts;
1230 /* Now set up the prefix. */
1231 if ( preserve == 0 )
1234 /* There is data that needs to be shifted over. */
1235 have = pe - preserve;
1236 memmove( buf, preserve, have );
1237 unsigned int shiftback = preserve - buf;