2 * Copyright 2006-2007 Adrian Thurston <thurston@complang.org>
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, const 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 inputItems.tail->data.write( 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 /* An approximate check for duplicate includes. Due to aliasing of files it's
250 * possible for duplicates to creep in. */
251 bool Scanner::duplicateInclude( char *inclFileName, char *inclSectionName )
253 for ( IncludeHistory::Iter hi = parser->includeHistory; hi.lte(); hi++ ) {
254 if ( strcmp( hi->fileName, inclFileName ) == 0 &&
255 strcmp( hi->sectionName, inclSectionName ) == 0 )
263 void Scanner::updateCol()
268 //cerr << "adding " << te - from << " to column" << endl;
273 void Scanner::handleMachine()
275 /* Assign a name to the machine. */
276 char *machine = word;
278 if ( !importMachines && inclSectionTarg == 0 ) {
279 ignoreSection = false;
281 ParserDictEl *pdEl = parserDict.find( machine );
283 pdEl = new ParserDictEl( machine );
284 pdEl->value = new Parser( fileName, machine, sectionLoc );
286 parserDict.insert( pdEl );
289 parser = pdEl->value;
291 else if ( !importMachines && strcmp( inclSectionTarg, machine ) == 0 ) {
292 /* found include target */
293 ignoreSection = false;
294 parser = inclToParser;
297 /* ignoring section */
298 ignoreSection = true;
303 void Scanner::handleInclude()
306 char *inclSectionName = word;
307 char **includeChecks = 0;
309 /* Implement defaults for the input file and section name. */
310 if ( inclSectionName == 0 )
311 inclSectionName = parser->sectionName;
314 includeChecks = makeIncludePathChecks( fileName, lit, lit_len );
316 char *test = new char[strlen(fileName)+1];
317 strcpy( test, fileName );
319 includeChecks = new char*[2];
321 includeChecks[0] = test;
322 includeChecks[1] = 0;
326 ifstream *inFile = tryOpenInclude( includeChecks, found );
328 scan_error() << "include: failed to locate file" << endl;
329 char **tried = includeChecks;
330 while ( *tried != 0 )
331 scan_error() << "include: attempted: \"" << *tried++ << '\"' << endl;
334 /* Don't include anything that's already been included. */
335 if ( !duplicateInclude( includeChecks[found], inclSectionName ) ) {
336 parser->includeHistory.append( IncludeHistoryItem(
337 includeChecks[found], inclSectionName ) );
339 Scanner scanner( includeChecks[found], *inFile, parser,
340 inclSectionName, includeDepth+1, false );
348 void Scanner::handleImport()
351 char **importChecks = makeIncludePathChecks( fileName, lit, lit_len );
353 /* Open the input file for reading. */
355 ifstream *inFile = tryOpenInclude( importChecks, found );
357 scan_error() << "import: could not open import file " <<
358 "for reading" << endl;
359 char **tried = importChecks;
360 while ( *tried != 0 )
361 scan_error() << "import: attempted: \"" << *tried++ << '\"' << endl;
364 Scanner scanner( importChecks[found], *inFile, parser,
365 0, includeDepth+1, true );
367 scanner.importToken( 0, 0, 0 );
368 scanner.flushImport();
374 machine section_parse;
376 # Need the defines representing tokens.
379 action clear_words { word = lit = 0; word_len = lit_len = 0; }
380 action store_word { word = tokdata; word_len = toklen; }
381 action store_lit { lit = tokdata; lit_len = toklen; }
383 action mach_err { scan_error() << "bad machine statement" << endl; }
384 action incl_err { scan_error() << "bad include statement" << endl; }
385 action import_err { scan_error() << "bad import statement" << endl; }
386 action write_err { scan_error() << "bad write statement" << endl; }
388 action handle_machine { handleMachine(); }
389 action handle_include { handleInclude(); }
390 action handle_import { handleImport(); }
393 ( KW_Machine TK_Word @store_word ';' ) @handle_machine
394 <>err mach_err <>eof mach_err;
397 TK_Word @store_word ( TK_Literal @store_lit )? |
398 TK_Literal @store_lit
402 ( KW_Include include_names ';' ) @handle_include
403 <>err incl_err <>eof incl_err;
406 ( KW_Import TK_Literal @store_lit ';' ) @handle_import
407 <>err import_err <>eof import_err;
411 if ( active() && machineSpec == 0 && machineName == 0 ) {
412 InputItem *inputItem = new InputItem;
413 inputItem->type = InputItem::Write;
414 inputItem->loc.line = line;
415 inputItem->loc.col = column;
416 inputItem->name = parser->sectionName;
417 inputItems.append( inputItem );
423 if ( active() && machineSpec == 0 && machineName == 0 )
424 inputItems.tail->writeArgs.append( strdup(tokdata) );
429 if ( active() && machineSpec == 0 && machineName == 0 )
430 inputItems.tail->writeArgs.append( 0 );
434 ( KW_Write @write_command
435 ( TK_Word @write_arg )+ ';' @write_close )
436 <>err write_err <>eof write_err;
440 /* Send the token off to the parser. */
442 directToParser( parser, fileName, line, column, type, tokdata, toklen );
445 # Catch everything else.
447 ^( KW_Machine | KW_Include | KW_Import | KW_Write ) @handle_token;
458 void Scanner::token( int type, char c )
460 token( type, &c, &c + 1 );
463 void Scanner::token( int type )
468 void Scanner::token( int type, char *start, char *end )
474 tokdata = new char[toklen+1];
475 memcpy( tokdata, start, toklen );
479 processToken( type, tokdata, toklen );
482 void Scanner::processToken( int type, char *tokdata, int toklen )
496 machine section_parse;
502 /* Record the last token for use in controlling the scan of subsequent
507 void Scanner::startSection( )
509 parserExistsError = false;
511 sectionLoc.fileName = fileName;
512 sectionLoc.line = line;
513 sectionLoc.col = column;
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 InputItem *inputItem = new InputItem;
536 inputItem->type = InputItem::HostData;
537 inputItem->loc.line = line;
538 inputItem->loc.col = column;
539 inputItems.append( inputItem );
544 bool isAbsolutePath( const char *path )
547 return isalpha( path[0] ) && path[1] == ':' && path[2] == '\\';
549 return path[0] == '/';
553 char **Scanner::makeIncludePathChecks( const char *thisFileName,
554 const char *fileName, int fnlen )
556 char **checks = new char*[2];
559 bool caseInsensitive = false;
561 char *data = prepareLitString( InputLoc(), fileName, fnlen,
562 length, caseInsensitive );
565 if ( isAbsolutePath( data ) )
566 checks[nextCheck++] = data;
568 /* Search from the the location of the current file. */
569 const char *lastSlash = strrchr( thisFileName, PATH_SEP );
570 if ( lastSlash == 0 )
571 checks[nextCheck++] = data;
573 long givenPathLen = (lastSlash - thisFileName) + 1;
574 long checklen = givenPathLen + length;
575 char *check = new char[checklen+1];
576 memcpy( check, thisFileName, givenPathLen );
577 memcpy( check+givenPathLen, data, length );
579 checks[nextCheck++] = check;
582 /* Search from the include paths given on the command line. */
583 for ( ArgsVector::Iter incp = includePaths; incp.lte(); incp++ ) {
584 long pathLen = strlen( *incp );
585 long checkLen = pathLen + 1 + length;
586 char *check = new char[checkLen+1];
587 memcpy( check, *incp, pathLen );
588 check[pathLen] = PATH_SEP;
589 memcpy( check+pathLen+1, data, length );
591 checks[nextCheck++] = check;
595 checks[nextCheck] = 0;
599 ifstream *Scanner::tryOpenInclude( char **pathChecks, long &found )
601 char **check = pathChecks;
602 ifstream *inFile = new ifstream;
604 while ( *check != 0 ) {
605 inFile->open( *check );
606 if ( inFile->is_open() ) {
607 found = check - pathChecks;
621 # This is sent by the driver code.
631 # Identifiers, numbers, commetns, and other common things.
632 ident = ( alpha | '_' ) ( alpha |digit |'_' )*;
634 hex_number = '0x' [0-9a-fA-F]+;
637 '/*' ( any | NL )* :>> '*/';
642 c_cpp_comment = c_comment | cpp_comment;
644 ruby_comment = '#' [^\n]* NL;
646 # These literal forms are common to host code and ragel.
647 s_literal = "'" ([^'\\] | NL | '\\' (any | NL))* "'";
648 d_literal = '"' ([^"\\] | NL | '\\' (any | NL))* '"';
649 host_re_literal = '/' ([^/\\] | NL | '\\' (any | NL))* '/';
651 whitespace = [ \t] | NL;
652 pound_comment = '#' [^\n]* NL;
654 # An inline block of code for Ruby.
655 inline_code_ruby := |*
656 # Inline expression keywords.
657 "fpc" => { token( KW_PChar ); };
658 "fc" => { token( KW_Char ); };
659 "fcurs" => { token( KW_CurState ); };
660 "ftargs" => { token( KW_TargState ); };
662 whitespaceOn = false;
666 # Inline statement keywords.
668 whitespaceOn = false;
671 "fexec" => { token( KW_Exec, 0, 0 ); };
673 whitespaceOn = false;
677 whitespaceOn = false;
681 whitespaceOn = false;
685 whitespaceOn = false;
689 whitespaceOn = false;
693 ident => { token( TK_Word, ts, te ); };
695 number => { token( TK_UInt, ts, te ); };
696 hex_number => { token( TK_Hex, ts, te ); };
698 ( s_literal | d_literal | host_re_literal )
699 => { token( IL_Literal, ts, te ); };
703 token( IL_WhiteSpace, ts, te );
706 ruby_comment => { token( IL_Comment, ts, te ); };
708 "::" => { token( TK_NameSep, ts, te ); };
710 # Some symbols need to go to the parser as with their cardinal value as
711 # the token type (as opposed to being sent as anonymous symbols)
712 # because they are part of the sequences which we interpret. The * ) ;
713 # symbols cause whitespace parsing to come back on. This gets turned
714 # off by some keywords.
718 token( *ts, ts, te );
719 if ( inlineBlockType == SemiTerminated )
725 token( *ts, ts, te );
728 [,(] => { token( *ts, ts, te ); };
731 token( IL_Symbol, ts, te );
736 if ( --curly_count == 0 && inlineBlockType == CurlyDelimited ) {
737 /* Inline code block ends. */
742 /* Either a semi terminated inline block or only the closing
743 * brace of some inner scope, not the block's closing brace. */
744 token( IL_Symbol, ts, te );
749 scan_error() << "unterminated code block" << endl;
752 # Send every other character as a symbol.
753 any => { token( IL_Symbol, ts, te ); };
757 # An inline block of code for languages other than Ruby.
759 # Inline expression keywords.
760 "fpc" => { token( KW_PChar ); };
761 "fc" => { token( KW_Char ); };
762 "fcurs" => { token( KW_CurState ); };
763 "ftargs" => { token( KW_TargState ); };
765 whitespaceOn = false;
769 # Inline statement keywords.
771 whitespaceOn = false;
774 "fexec" => { token( KW_Exec, 0, 0 ); };
776 whitespaceOn = false;
780 whitespaceOn = false;
784 whitespaceOn = false;
788 whitespaceOn = false;
792 whitespaceOn = false;
796 ident => { token( TK_Word, ts, te ); };
798 number => { token( TK_UInt, ts, te ); };
799 hex_number => { token( TK_Hex, ts, te ); };
801 ( s_literal | d_literal )
802 => { token( IL_Literal, ts, te ); };
806 token( IL_WhiteSpace, ts, te );
809 c_cpp_comment => { token( IL_Comment, ts, te ); };
811 "::" => { token( TK_NameSep, ts, te ); };
813 # Some symbols need to go to the parser as with their cardinal value as
814 # the token type (as opposed to being sent as anonymous symbols)
815 # because they are part of the sequences which we interpret. The * ) ;
816 # symbols cause whitespace parsing to come back on. This gets turned
817 # off by some keywords.
821 token( *ts, ts, te );
822 if ( inlineBlockType == SemiTerminated )
828 token( *ts, ts, te );
831 [,(] => { token( *ts, ts, te ); };
834 token( IL_Symbol, ts, te );
839 if ( --curly_count == 0 && inlineBlockType == CurlyDelimited ) {
840 /* Inline code block ends. */
845 /* Either a semi terminated inline block or only the closing
846 * brace of some inner scope, not the block's closing brace. */
847 token( IL_Symbol, ts, te );
852 scan_error() << "unterminated code block" << endl;
855 # Send every other character as a symbol.
856 any => { token( IL_Symbol, ts, te ); };
860 # Escape sequences in OR expressions.
861 '\\0' => { token( RE_Char, '\0' ); };
862 '\\a' => { token( RE_Char, '\a' ); };
863 '\\b' => { token( RE_Char, '\b' ); };
864 '\\t' => { token( RE_Char, '\t' ); };
865 '\\n' => { token( RE_Char, '\n' ); };
866 '\\v' => { token( RE_Char, '\v' ); };
867 '\\f' => { token( RE_Char, '\f' ); };
868 '\\r' => { token( RE_Char, '\r' ); };
869 '\\\n' => { updateCol(); };
870 '\\' any => { token( RE_Char, ts+1, te ); };
872 # Range dash in an OR expression.
873 '-' => { token( RE_Dash, 0, 0 ); };
875 # Terminate an OR expression.
876 ']' => { token( RE_SqClose ); fret; };
879 scan_error() << "unterminated OR literal" << endl;
882 # Characters in an OR expression.
883 [^\]] => { token( RE_Char, ts, te ); };
887 ragel_re_literal := |*
888 # Escape sequences in regular expressions.
889 '\\0' => { token( RE_Char, '\0' ); };
890 '\\a' => { token( RE_Char, '\a' ); };
891 '\\b' => { token( RE_Char, '\b' ); };
892 '\\t' => { token( RE_Char, '\t' ); };
893 '\\n' => { token( RE_Char, '\n' ); };
894 '\\v' => { token( RE_Char, '\v' ); };
895 '\\f' => { token( RE_Char, '\f' ); };
896 '\\r' => { token( RE_Char, '\r' ); };
897 '\\\n' => { updateCol(); };
898 '\\' any => { token( RE_Char, ts+1, te ); };
900 # Terminate an OR expression.
902 token( RE_Slash, ts, te );
906 # Special characters.
907 '.' => { token( RE_Dot ); };
908 '*' => { token( RE_Star ); };
910 '[' => { token( RE_SqOpen ); fcall or_literal; };
911 '[^' => { token( RE_SqOpenNeg ); fcall or_literal; };
914 scan_error() << "unterminated regular expression" << endl;
917 # Characters in an OR expression.
918 [^\/] => { token( RE_Char, ts, te ); };
921 # We need a separate token space here to avoid the ragel keywords.
922 write_statement := |*
923 ident => { token( TK_Word, ts, te ); } ;
924 [ \t\n]+ => { updateCol(); };
925 ';' => { token( ';' ); fgoto parser_def; };
928 scan_error() << "unterminated write statement" << endl;
932 # Parser definitions.
934 'machine' => { token( KW_Machine ); };
935 'include' => { token( KW_Include ); };
936 'import' => { token( KW_Import ); };
939 fgoto write_statement;
941 'action' => { token( KW_Action ); };
942 'alphtype' => { token( KW_AlphType ); };
943 'prepush' => { token( KW_PrePush ); };
944 'postpop' => { token( KW_PostPop ); };
946 # FIXME: Enable this post 5.17.
947 # 'range' => { token( KW_Range ); };
951 inlineBlockType = SemiTerminated;
952 if ( hostLang->lang == HostLang::Ruby )
953 fcall inline_code_ruby;
959 inlineBlockType = SemiTerminated;
960 if ( hostLang->lang == HostLang::Ruby )
961 fcall inline_code_ruby;
966 token( KW_Variable );
967 inlineBlockType = SemiTerminated;
968 if ( hostLang->lang == HostLang::Ruby )
969 fcall inline_code_ruby;
973 'when' => { token( KW_When ); };
974 'inwhen' => { token( KW_InWhen ); };
975 'outwhen' => { token( KW_OutWhen ); };
976 'eof' => { token( KW_Eof ); };
977 'err' => { token( KW_Err ); };
978 'lerr' => { token( KW_Lerr ); };
979 'to' => { token( KW_To ); };
980 'from' => { token( KW_From ); };
981 'export' => { token( KW_Export ); };
984 ident => { token( TK_Word, ts, te ); } ;
987 number => { token( TK_UInt, ts, te ); };
988 hex_number => { token( TK_Hex, ts, te ); };
990 # Literals, with optionals.
991 ( s_literal | d_literal ) [i]?
992 => { token( TK_Literal, ts, te ); };
994 '[' => { token( RE_SqOpen ); fcall or_literal; };
995 '[^' => { token( RE_SqOpenNeg ); fcall or_literal; };
997 '/' => { token( RE_Slash ); fgoto ragel_re_literal; };
1000 pound_comment => { updateCol(); };
1002 ':=' => { token( TK_ColonEquals ); };
1005 ">~" => { token( TK_StartToState ); };
1006 "$~" => { token( TK_AllToState ); };
1007 "%~" => { token( TK_FinalToState ); };
1008 "<~" => { token( TK_NotStartToState ); };
1009 "@~" => { token( TK_NotFinalToState ); };
1010 "<>~" => { token( TK_MiddleToState ); };
1012 # From State actions
1013 ">*" => { token( TK_StartFromState ); };
1014 "$*" => { token( TK_AllFromState ); };
1015 "%*" => { token( TK_FinalFromState ); };
1016 "<*" => { token( TK_NotStartFromState ); };
1017 "@*" => { token( TK_NotFinalFromState ); };
1018 "<>*" => { token( TK_MiddleFromState ); };
1021 ">/" => { token( TK_StartEOF ); };
1022 "$/" => { token( TK_AllEOF ); };
1023 "%/" => { token( TK_FinalEOF ); };
1024 "</" => { token( TK_NotStartEOF ); };
1025 "@/" => { token( TK_NotFinalEOF ); };
1026 "<>/" => { token( TK_MiddleEOF ); };
1028 # Global Error actions.
1029 ">!" => { token( TK_StartGblError ); };
1030 "$!" => { token( TK_AllGblError ); };
1031 "%!" => { token( TK_FinalGblError ); };
1032 "<!" => { token( TK_NotStartGblError ); };
1033 "@!" => { token( TK_NotFinalGblError ); };
1034 "<>!" => { token( TK_MiddleGblError ); };
1036 # Local error actions.
1037 ">^" => { token( TK_StartLocalError ); };
1038 "$^" => { token( TK_AllLocalError ); };
1039 "%^" => { token( TK_FinalLocalError ); };
1040 "<^" => { token( TK_NotStartLocalError ); };
1041 "@^" => { token( TK_NotFinalLocalError ); };
1042 "<>^" => { token( TK_MiddleLocalError ); };
1045 "<>" => { token( TK_Middle ); };
1048 '>?' => { token( TK_StartCond ); };
1049 '$?' => { token( TK_AllCond ); };
1050 '%?' => { token( TK_LeavingCond ); };
1052 '..' => { token( TK_DotDot ); };
1053 '**' => { token( TK_StarStar ); };
1054 '--' => { token( TK_DashDash ); };
1055 '->' => { token( TK_Arrow ); };
1056 '=>' => { token( TK_DoubleArrow ); };
1058 ":>" => { token( TK_ColonGt ); };
1059 ":>>" => { token( TK_ColonGtGt ); };
1060 "<:" => { token( TK_LtColon ); };
1062 # Opening of longest match.
1063 "|*" => { token( TK_BarStar ); };
1065 # Separater for name references.
1066 "::" => { token( TK_NameSep, ts, te ); };
1074 [ \t\r]+ => { updateCol(); };
1076 # If we are in a single line machine then newline may end the spec.
1079 if ( singleLineSpec ) {
1086 if ( lastToken == KW_Export || lastToken == KW_Entry )
1091 inlineBlockType = CurlyDelimited;
1092 if ( hostLang->lang == HostLang::Ruby )
1093 fcall inline_code_ruby;
1100 scan_error() << "unterminated ragel section" << endl;
1103 any => { token( *ts ); } ;
1106 # Outside code scanner. These tokens get passed through.
1108 ident => { pass( IMP_Word, ts, te ); };
1109 number => { pass( IMP_UInt, ts, te ); };
1110 ruby_comment => { pass(); };
1111 ( s_literal | d_literal | host_re_literal )
1112 => { pass( IMP_Literal, ts, te ); };
1116 singleLineSpec = false;
1122 singleLineSpec = true;
1126 whitespace+ => { pass(); };
1128 any => { pass( *ts, 0, 0 ); };
1131 # Outside code scanner. These tokens get passed through.
1133 'define' => { pass( IMP_Define, 0, 0 ); };
1134 ident => { pass( IMP_Word, ts, te ); };
1135 number => { pass( IMP_UInt, ts, te ); };
1136 c_cpp_comment => { pass(); };
1137 ( s_literal | d_literal ) => { pass( IMP_Literal, ts, te ); };
1141 singleLineSpec = false;
1147 singleLineSpec = true;
1151 whitespace+ => { pass(); };
1153 any => { pass( *ts, 0, 0 ); };
1159 void Scanner::do_scan()
1162 char *buf = new char[bufsize];
1163 int cs, act, have = 0;
1166 /* The stack is two deep, one level for going into ragel defs from the main
1167 * machines which process outside code, and another for going into or literals
1168 * from either a ragel spec, or a regular expression. */
1170 int curly_count = 0;
1171 bool execute = true;
1172 bool singleLineSpec = false;
1173 InlineBlockType inlineBlockType = CurlyDelimited;
1175 /* Init the section parser and the character scanner. */
1179 /* Set up the start state. FIXME: After 5.20 is released the nocs write
1180 * init option should be used, the main machine eliminated and this statement moved
1181 * above the write init. */
1182 if ( hostLang->lang == HostLang::Ruby )
1183 cs = rlscan_en_main_ruby;
1185 cs = rlscan_en_main;
1188 char *p = buf + have;
1189 int space = bufsize - have;
1192 /* We filled up the buffer trying to scan a token. Grow it. */
1193 bufsize = bufsize * 2;
1194 char *newbuf = new char[bufsize];
1196 /* Recompute p and space. */
1198 space = bufsize - have;
1200 /* Patch up pointers possibly in use. */
1202 ts = newbuf + ( ts - buf );
1203 te = newbuf + ( te - buf );
1205 /* Copy the new buffer in. */
1206 memcpy( newbuf, buf, have );
1211 input.read( p, space );
1212 int len = input.gcount();
1215 /* If we see eof then append the eof var. */
1224 /* Check if we failed. */
1225 if ( cs == rlscan_error ) {
1226 /* Machine failed before finding a token. I'm not yet sure if this
1228 scan_error() << "scanner error" << endl;
1232 /* Decide if we need to preserve anything. */
1233 char *preserve = ts;
1235 /* Now set up the prefix. */
1236 if ( preserve == 0 )
1239 /* There is data that needs to be shifted over. */
1240 have = pe - preserve;
1241 memmove( buf, preserve, have );
1242 unsigned int shiftback = preserve - buf;