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 << makeInputLoc( fileName, line, column ) << ": ";
249 bool Scanner::duplicateInclude( char *inclFileName, char *inclSectionName )
251 for ( IncludeHistory::Iter hi = parser->includeHistory; hi.lte(); hi++ ) {
252 if ( strcmp( hi->fileName, inclFileName ) == 0 &&
253 strcmp( hi->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 **includeChecks = 0;
307 /* Implement defaults for the input file and section name. */
308 if ( inclSectionName == 0 )
309 inclSectionName = parser->sectionName;
312 includeChecks = makeIncludePathChecks( fileName, lit, lit_len );
314 char *test = new char[strlen(fileName)+1];
315 strcpy( test, fileName );
317 includeChecks = new char*[2];
319 includeChecks[0] = test;
320 includeChecks[1] = 0;
324 ifstream *inFile = tryOpenInclude( includeChecks, found );
326 scan_error() << "include: failed to locate file" << endl;
327 char **tried = includeChecks;
328 while ( *tried != 0 )
329 scan_error() << "include: attempted: \"" << *tried++ << '\"' << endl;
332 /* Don't include anything that's already been included. */
333 if ( !duplicateInclude( includeChecks[found], inclSectionName ) ) {
334 parser->includeHistory.append( IncludeHistoryItem(
335 includeChecks[found], inclSectionName ) );
337 Scanner scanner( includeChecks[found], *inFile, output, parser,
338 inclSectionName, includeDepth+1, false );
346 void Scanner::handleImport()
349 char **importChecks = makeIncludePathChecks( fileName, lit, lit_len );
351 /* Open the input file for reading. */
353 ifstream *inFile = tryOpenInclude( importChecks, found );
355 scan_error() << "import: could not open import file " <<
356 "for reading" << endl;
357 char **tried = importChecks;
358 while ( *tried != 0 )
359 scan_error() << "import: attempted: \"" << *tried++ << '\"' << endl;
362 Scanner scanner( importChecks[found], *inFile, output, parser,
363 0, includeDepth+1, true );
365 scanner.importToken( 0, 0, 0 );
366 scanner.flushImport();
372 machine section_parse;
374 # Need the defines representing tokens.
377 action clear_words { word = lit = 0; word_len = lit_len = 0; }
378 action store_word { word = tokdata; word_len = toklen; }
379 action store_lit { lit = tokdata; lit_len = toklen; }
381 action mach_err { scan_error() << "bad machine statement" << endl; }
382 action incl_err { scan_error() << "bad include statement" << endl; }
383 action import_err { scan_error() << "bad import statement" << endl; }
384 action write_err { scan_error() << "bad write statement" << endl; }
386 action handle_machine { handleMachine(); }
387 action handle_include { handleInclude(); }
388 action handle_import { handleImport(); }
391 ( KW_Machine TK_Word @store_word ';' ) @handle_machine
392 <>err mach_err <>eof mach_err;
395 TK_Word @store_word ( TK_Literal @store_lit )? |
396 TK_Literal @store_lit
400 ( KW_Include include_names ';' ) @handle_include
401 <>err incl_err <>eof incl_err;
404 ( KW_Import TK_Literal @store_lit ';' ) @handle_import
405 <>err import_err <>eof import_err;
409 if ( active() && machineSpec == 0 && machineName == 0 ) {
411 " def_name=\"" << parser->sectionName << "\""
412 " line=\"" << line << "\""
413 " col=\"" << column << "\""
420 if ( active() && machineSpec == 0 && machineName == 0 )
421 output << "<arg>" << tokdata << "</arg>";
426 if ( active() && machineSpec == 0 && machineName == 0 )
427 output << "</write>\n";
431 ( KW_Write @write_command
432 ( TK_Word @write_arg )+ ';' @write_close )
433 <>err write_err <>eof write_err;
437 /* Send the token off to the parser. */
439 directToParser( parser, fileName, line, column, type, tokdata, toklen );
442 # Catch everything else.
444 ^( KW_Machine | KW_Include | KW_Import | KW_Write ) @handle_token;
455 void Scanner::token( int type, char c )
457 token( type, &c, &c + 1 );
460 void Scanner::token( int type )
465 void Scanner::token( int type, char *start, char *end )
471 tokdata = new char[toklen+1];
472 memcpy( tokdata, start, toklen );
476 processToken( type, tokdata, toklen );
479 void Scanner::processToken( int type, char *tokdata, int toklen )
493 machine section_parse;
499 /* Record the last token for use in controlling the scan of subsequent
504 void Scanner::startSection( )
506 parserExistsError = false;
508 if ( includeDepth == 0 ) {
509 if ( machineSpec == 0 && machineName == 0 )
510 output << "</host>\n";
513 sectionLoc.fileName = fileName;
514 sectionLoc.line = line;
518 void Scanner::endSection( )
520 /* Execute the eof actions for the section parser. */
521 processToken( -1, 0, 0 );
523 /* Close off the section with the parser. */
526 loc.fileName = fileName;
530 parser->token( loc, TK_EndSection, 0, 0 );
533 if ( includeDepth == 0 ) {
534 if ( machineSpec == 0 && machineName == 0 ) {
535 /* The end section may include a newline on the end, so
536 * we use the last line, which will count the newline. */
537 output << "<host line=\"" << line << "\">";
542 bool isAbsolutePath( const char *path )
545 return isalpha( path[0] ) && path[1] == ':' && path[2] == '\\';
547 return path[0] == '/';
551 char **Scanner::makeIncludePathChecks( char *thisFileName, char *fileName, int fnlen )
553 char **checks = new char*[2];
556 bool caseInsensitive = false;
558 char *data = prepareLitString( InputLoc(), fileName, fnlen,
559 length, caseInsensitive );
562 if ( isAbsolutePath( data ) )
563 checks[nextCheck++] = data;
565 /* Search from the the location of the current file. */
566 const char *lastSlash = strrchr( thisFileName, PATH_SEP );
567 if ( lastSlash == 0 )
568 checks[nextCheck++] = data;
570 long givenPathLen = (lastSlash - thisFileName) + 1;
571 long checklen = givenPathLen + length;
572 char *check = new char[checklen+1];
573 memcpy( check, thisFileName, givenPathLen );
574 memcpy( check+givenPathLen, data, length );
576 checks[nextCheck++] = check;
579 /* Search from the include paths given on the command line. */
580 for ( ArgsVector::Iter incp = includePaths; incp.lte(); incp++ ) {
581 long pathLen = strlen( *incp );
582 long checkLen = pathLen + 1 + length;
583 char *check = new char[checkLen+1];
584 memcpy( check, *incp, pathLen );
585 check[pathLen] = PATH_SEP;
586 memcpy( check+pathLen+1, data, length );
588 checks[nextCheck++] = check;
592 checks[nextCheck] = 0;
596 ifstream *Scanner::tryOpenInclude( char **pathChecks, long &found )
598 char **check = pathChecks;
599 ifstream *inFile = new ifstream;
601 while ( *check != 0 ) {
602 inFile->open( *check );
603 if ( inFile->is_open() ) {
604 found = check - pathChecks;
618 # This is sent by the driver code.
628 # Identifiers, numbers, commetns, and other common things.
629 ident = ( alpha | '_' ) ( alpha |digit |'_' )*;
631 hex_number = '0x' [0-9a-fA-F]+;
634 '/*' ( any | NL )* :>> '*/';
639 c_cpp_comment = c_comment | cpp_comment;
641 ruby_comment = '#' [^\n]* NL;
643 # These literal forms are common to host code and ragel.
644 s_literal = "'" ([^'\\] | NL | '\\' (any | NL))* "'";
645 d_literal = '"' ([^"\\] | NL | '\\' (any | NL))* '"';
646 host_re_literal = '/' ([^/\\] | NL | '\\' (any | NL))* '/';
648 whitespace = [ \t] | NL;
649 pound_comment = '#' [^\n]* NL;
651 # An inline block of code for Ruby.
652 inline_code_ruby := |*
653 # Inline expression keywords.
654 "fpc" => { token( KW_PChar ); };
655 "fc" => { token( KW_Char ); };
656 "fcurs" => { token( KW_CurState ); };
657 "ftargs" => { token( KW_TargState ); };
659 whitespaceOn = false;
663 # Inline statement keywords.
665 whitespaceOn = false;
668 "fexec" => { token( KW_Exec, 0, 0 ); };
670 whitespaceOn = false;
674 whitespaceOn = false;
678 whitespaceOn = false;
682 whitespaceOn = false;
686 whitespaceOn = false;
690 ident => { token( TK_Word, ts, te ); };
692 number => { token( TK_UInt, ts, te ); };
693 hex_number => { token( TK_Hex, ts, te ); };
695 ( s_literal | d_literal | host_re_literal )
696 => { token( IL_Literal, ts, te ); };
700 token( IL_WhiteSpace, ts, te );
703 ruby_comment => { token( IL_Comment, ts, te ); };
705 "::" => { token( TK_NameSep, ts, te ); };
707 # Some symbols need to go to the parser as with their cardinal value as
708 # the token type (as opposed to being sent as anonymous symbols)
709 # because they are part of the sequences which we interpret. The * ) ;
710 # symbols cause whitespace parsing to come back on. This gets turned
711 # off by some keywords.
715 token( *ts, ts, te );
716 if ( inlineBlockType == SemiTerminated )
722 token( *ts, ts, te );
725 [,(] => { token( *ts, ts, te ); };
728 token( IL_Symbol, ts, te );
733 if ( --curly_count == 0 && inlineBlockType == CurlyDelimited ) {
734 /* Inline code block ends. */
739 /* Either a semi terminated inline block or only the closing
740 * brace of some inner scope, not the block's closing brace. */
741 token( IL_Symbol, ts, te );
746 scan_error() << "unterminated code block" << endl;
749 # Send every other character as a symbol.
750 any => { token( IL_Symbol, ts, te ); };
754 # An inline block of code for languages other than Ruby.
756 # Inline expression keywords.
757 "fpc" => { token( KW_PChar ); };
758 "fc" => { token( KW_Char ); };
759 "fcurs" => { token( KW_CurState ); };
760 "ftargs" => { token( KW_TargState ); };
762 whitespaceOn = false;
766 # Inline statement keywords.
768 whitespaceOn = false;
771 "fexec" => { token( KW_Exec, 0, 0 ); };
773 whitespaceOn = false;
777 whitespaceOn = false;
781 whitespaceOn = false;
785 whitespaceOn = false;
789 whitespaceOn = false;
793 ident => { token( TK_Word, ts, te ); };
795 number => { token( TK_UInt, ts, te ); };
796 hex_number => { token( TK_Hex, ts, te ); };
798 ( s_literal | d_literal )
799 => { token( IL_Literal, ts, te ); };
803 token( IL_WhiteSpace, ts, te );
806 c_cpp_comment => { token( IL_Comment, ts, te ); };
808 "::" => { token( TK_NameSep, ts, te ); };
810 # Some symbols need to go to the parser as with their cardinal value as
811 # the token type (as opposed to being sent as anonymous symbols)
812 # because they are part of the sequences which we interpret. The * ) ;
813 # symbols cause whitespace parsing to come back on. This gets turned
814 # off by some keywords.
818 token( *ts, ts, te );
819 if ( inlineBlockType == SemiTerminated )
825 token( *ts, ts, te );
828 [,(] => { token( *ts, ts, te ); };
831 token( IL_Symbol, ts, te );
836 if ( --curly_count == 0 && inlineBlockType == CurlyDelimited ) {
837 /* Inline code block ends. */
842 /* Either a semi terminated inline block or only the closing
843 * brace of some inner scope, not the block's closing brace. */
844 token( IL_Symbol, ts, te );
849 scan_error() << "unterminated code block" << endl;
852 # Send every other character as a symbol.
853 any => { token( IL_Symbol, ts, te ); };
857 # Escape sequences in OR expressions.
858 '\\0' => { token( RE_Char, '\0' ); };
859 '\\a' => { token( RE_Char, '\a' ); };
860 '\\b' => { token( RE_Char, '\b' ); };
861 '\\t' => { token( RE_Char, '\t' ); };
862 '\\n' => { token( RE_Char, '\n' ); };
863 '\\v' => { token( RE_Char, '\v' ); };
864 '\\f' => { token( RE_Char, '\f' ); };
865 '\\r' => { token( RE_Char, '\r' ); };
866 '\\\n' => { updateCol(); };
867 '\\' any => { token( RE_Char, ts+1, te ); };
869 # Range dash in an OR expression.
870 '-' => { token( RE_Dash, 0, 0 ); };
872 # Terminate an OR expression.
873 ']' => { token( RE_SqClose ); fret; };
876 scan_error() << "unterminated OR literal" << endl;
879 # Characters in an OR expression.
880 [^\]] => { token( RE_Char, ts, te ); };
884 ragel_re_literal := |*
885 # Escape sequences in regular expressions.
886 '\\0' => { token( RE_Char, '\0' ); };
887 '\\a' => { token( RE_Char, '\a' ); };
888 '\\b' => { token( RE_Char, '\b' ); };
889 '\\t' => { token( RE_Char, '\t' ); };
890 '\\n' => { token( RE_Char, '\n' ); };
891 '\\v' => { token( RE_Char, '\v' ); };
892 '\\f' => { token( RE_Char, '\f' ); };
893 '\\r' => { token( RE_Char, '\r' ); };
894 '\\\n' => { updateCol(); };
895 '\\' any => { token( RE_Char, ts+1, te ); };
897 # Terminate an OR expression.
899 token( RE_Slash, ts, te );
903 # Special characters.
904 '.' => { token( RE_Dot ); };
905 '*' => { token( RE_Star ); };
907 '[' => { token( RE_SqOpen ); fcall or_literal; };
908 '[^' => { token( RE_SqOpenNeg ); fcall or_literal; };
911 scan_error() << "unterminated regular expression" << endl;
914 # Characters in an OR expression.
915 [^\/] => { token( RE_Char, ts, te ); };
918 # We need a separate token space here to avoid the ragel keywords.
919 write_statement := |*
920 ident => { token( TK_Word, ts, te ); } ;
921 [ \t\n]+ => { updateCol(); };
922 ';' => { token( ';' ); fgoto parser_def; };
925 scan_error() << "unterminated write statement" << endl;
929 # Parser definitions.
931 'machine' => { token( KW_Machine ); };
932 'include' => { token( KW_Include ); };
933 'import' => { token( KW_Import ); };
936 fgoto write_statement;
938 'action' => { token( KW_Action ); };
939 'alphtype' => { token( KW_AlphType ); };
940 'prepush' => { token( KW_PrePush ); };
941 'postpop' => { token( KW_PostPop ); };
943 # FIXME: Enable this post 5.17.
944 # 'range' => { token( KW_Range ); };
948 inlineBlockType = SemiTerminated;
949 if ( hostLang->lang == HostLang::Ruby )
950 fcall inline_code_ruby;
956 inlineBlockType = SemiTerminated;
957 if ( hostLang->lang == HostLang::Ruby )
958 fcall inline_code_ruby;
963 token( KW_Variable );
964 inlineBlockType = SemiTerminated;
965 if ( hostLang->lang == HostLang::Ruby )
966 fcall inline_code_ruby;
970 'when' => { token( KW_When ); };
971 'inwhen' => { token( KW_InWhen ); };
972 'outwhen' => { token( KW_OutWhen ); };
973 'eof' => { token( KW_Eof ); };
974 'err' => { token( KW_Err ); };
975 'lerr' => { token( KW_Lerr ); };
976 'to' => { token( KW_To ); };
977 'from' => { token( KW_From ); };
978 'export' => { token( KW_Export ); };
981 ident => { token( TK_Word, ts, te ); } ;
984 number => { token( TK_UInt, ts, te ); };
985 hex_number => { token( TK_Hex, ts, te ); };
987 # Literals, with optionals.
988 ( s_literal | d_literal ) [i]?
989 => { token( TK_Literal, ts, te ); };
991 '[' => { token( RE_SqOpen ); fcall or_literal; };
992 '[^' => { token( RE_SqOpenNeg ); fcall or_literal; };
994 '/' => { token( RE_Slash ); fgoto ragel_re_literal; };
997 pound_comment => { updateCol(); };
999 ':=' => { token( TK_ColonEquals ); };
1002 ">~" => { token( TK_StartToState ); };
1003 "$~" => { token( TK_AllToState ); };
1004 "%~" => { token( TK_FinalToState ); };
1005 "<~" => { token( TK_NotStartToState ); };
1006 "@~" => { token( TK_NotFinalToState ); };
1007 "<>~" => { token( TK_MiddleToState ); };
1009 # From State actions
1010 ">*" => { token( TK_StartFromState ); };
1011 "$*" => { token( TK_AllFromState ); };
1012 "%*" => { token( TK_FinalFromState ); };
1013 "<*" => { token( TK_NotStartFromState ); };
1014 "@*" => { token( TK_NotFinalFromState ); };
1015 "<>*" => { token( TK_MiddleFromState ); };
1018 ">/" => { token( TK_StartEOF ); };
1019 "$/" => { token( TK_AllEOF ); };
1020 "%/" => { token( TK_FinalEOF ); };
1021 "</" => { token( TK_NotStartEOF ); };
1022 "@/" => { token( TK_NotFinalEOF ); };
1023 "<>/" => { token( TK_MiddleEOF ); };
1025 # Global Error actions.
1026 ">!" => { token( TK_StartGblError ); };
1027 "$!" => { token( TK_AllGblError ); };
1028 "%!" => { token( TK_FinalGblError ); };
1029 "<!" => { token( TK_NotStartGblError ); };
1030 "@!" => { token( TK_NotFinalGblError ); };
1031 "<>!" => { token( TK_MiddleGblError ); };
1033 # Local error actions.
1034 ">^" => { token( TK_StartLocalError ); };
1035 "$^" => { token( TK_AllLocalError ); };
1036 "%^" => { token( TK_FinalLocalError ); };
1037 "<^" => { token( TK_NotStartLocalError ); };
1038 "@^" => { token( TK_NotFinalLocalError ); };
1039 "<>^" => { token( TK_MiddleLocalError ); };
1042 "<>" => { token( TK_Middle ); };
1045 '>?' => { token( TK_StartCond ); };
1046 '$?' => { token( TK_AllCond ); };
1047 '%?' => { token( TK_LeavingCond ); };
1049 '..' => { token( TK_DotDot ); };
1050 '**' => { token( TK_StarStar ); };
1051 '--' => { token( TK_DashDash ); };
1052 '->' => { token( TK_Arrow ); };
1053 '=>' => { token( TK_DoubleArrow ); };
1055 ":>" => { token( TK_ColonGt ); };
1056 ":>>" => { token( TK_ColonGtGt ); };
1057 "<:" => { token( TK_LtColon ); };
1059 # Opening of longest match.
1060 "|*" => { token( TK_BarStar ); };
1062 # Separater for name references.
1063 "::" => { token( TK_NameSep, ts, te ); };
1071 [ \t\r]+ => { updateCol(); };
1073 # If we are in a single line machine then newline may end the spec.
1076 if ( singleLineSpec ) {
1083 if ( lastToken == KW_Export || lastToken == KW_Entry )
1088 inlineBlockType = CurlyDelimited;
1089 if ( hostLang->lang == HostLang::Ruby )
1090 fcall inline_code_ruby;
1097 scan_error() << "unterminated ragel section" << endl;
1100 any => { token( *ts ); } ;
1103 # Outside code scanner. These tokens get passed through.
1105 ident => { pass( IMP_Word, ts, te ); };
1106 number => { pass( IMP_UInt, ts, te ); };
1107 ruby_comment => { pass(); };
1108 ( s_literal | d_literal | host_re_literal )
1109 => { pass( IMP_Literal, ts, te ); };
1113 singleLineSpec = false;
1119 singleLineSpec = true;
1123 whitespace+ => { pass(); };
1125 any => { pass( *ts, 0, 0 ); };
1128 # Outside code scanner. These tokens get passed through.
1130 'define' => { pass( IMP_Define, 0, 0 ); };
1131 ident => { pass( IMP_Word, ts, te ); };
1132 number => { pass( IMP_UInt, ts, te ); };
1133 c_cpp_comment => { pass(); };
1134 ( s_literal | d_literal ) => { pass( IMP_Literal, ts, te ); };
1138 singleLineSpec = false;
1144 singleLineSpec = true;
1148 whitespace+ => { pass(); };
1150 any => { pass( *ts, 0, 0 ); };
1156 void Scanner::do_scan()
1159 char *buf = new char[bufsize];
1160 int cs, act, have = 0;
1163 /* The stack is two deep, one level for going into ragel defs from the main
1164 * machines which process outside code, and another for going into or literals
1165 * from either a ragel spec, or a regular expression. */
1167 int curly_count = 0;
1168 bool execute = true;
1169 bool singleLineSpec = false;
1170 InlineBlockType inlineBlockType = CurlyDelimited;
1172 /* Init the section parser and the character scanner. */
1176 /* Set up the start state. FIXME: After 5.20 is released the nocs write
1177 * init option should be used, the main machine eliminated and this statement moved
1178 * above the write init. */
1179 if ( hostLang->lang == HostLang::Ruby )
1180 cs = rlscan_en_main_ruby;
1182 cs = rlscan_en_main;
1185 char *p = buf + have;
1186 int space = bufsize - have;
1189 /* We filled up the buffer trying to scan a token. Grow it. */
1190 bufsize = bufsize * 2;
1191 char *newbuf = new char[bufsize];
1193 /* Recompute p and space. */
1195 space = bufsize - have;
1197 /* Patch up pointers possibly in use. */
1199 ts = newbuf + ( ts - buf );
1200 te = newbuf + ( te - buf );
1202 /* Copy the new buffer in. */
1203 memcpy( newbuf, buf, have );
1208 input.read( p, space );
1209 int len = input.gcount();
1212 /* If we see eof then append the eof var. */
1221 /* Check if we failed. */
1222 if ( cs == rlscan_error ) {
1223 /* Machine failed before finding a token. I'm not yet sure if this
1225 scan_error() << "scanner error" << endl;
1229 /* Decide if we need to preserve anything. */
1230 char *preserve = ts;
1232 /* Now set up the prefix. */
1233 if ( preserve == 0 )
1236 /* There is data that needs to be shifted over. */
1237 have = pe - preserve;
1238 memmove( buf, preserve, have );
1239 unsigned int shiftback = preserve - buf;