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 inputItem->pd = parser->pd;
418 inputItems.append( inputItem );
424 if ( active() && machineSpec == 0 && machineName == 0 )
425 inputItems.tail->writeArgs.append( strdup(tokdata) );
430 if ( active() && machineSpec == 0 && machineName == 0 )
431 inputItems.tail->writeArgs.append( 0 );
435 ( KW_Write @write_command
436 ( TK_Word @write_arg )+ ';' @write_close )
437 <>err write_err <>eof write_err;
441 /* Send the token off to the parser. */
443 directToParser( parser, fileName, line, column, type, tokdata, toklen );
446 # Catch everything else.
448 ^( KW_Machine | KW_Include | KW_Import | KW_Write ) @handle_token;
459 void Scanner::token( int type, char c )
461 token( type, &c, &c + 1 );
464 void Scanner::token( int type )
469 void Scanner::token( int type, char *start, char *end )
475 tokdata = new char[toklen+1];
476 memcpy( tokdata, start, toklen );
480 processToken( type, tokdata, toklen );
483 void Scanner::processToken( int type, char *tokdata, int toklen )
497 machine section_parse;
503 /* Record the last token for use in controlling the scan of subsequent
508 void Scanner::startSection( )
510 parserExistsError = false;
512 sectionLoc.fileName = fileName;
513 sectionLoc.line = line;
514 sectionLoc.col = column;
517 void Scanner::endSection( )
519 /* Execute the eof actions for the section parser. */
520 processToken( -1, 0, 0 );
522 /* Close off the section with the parser. */
525 loc.fileName = fileName;
529 parser->token( loc, TK_EndSection, 0, 0 );
532 if ( includeDepth == 0 ) {
533 if ( machineSpec == 0 && machineName == 0 ) {
534 /* The end section may include a newline on the end, so
535 * we use the last line, which will count the newline. */
536 InputItem *inputItem = new InputItem;
537 inputItem->type = InputItem::HostData;
538 inputItem->loc.line = line;
539 inputItem->loc.col = column;
540 inputItems.append( inputItem );
545 bool isAbsolutePath( const char *path )
548 return isalpha( path[0] ) && path[1] == ':' && path[2] == '\\';
550 return path[0] == '/';
554 char **Scanner::makeIncludePathChecks( const char *thisFileName,
555 const char *fileName, int fnlen )
557 char **checks = new char*[2];
560 bool caseInsensitive = false;
562 char *data = prepareLitString( InputLoc(), fileName, fnlen,
563 length, caseInsensitive );
566 if ( isAbsolutePath( data ) )
567 checks[nextCheck++] = data;
569 /* Search from the the location of the current file. */
570 const char *lastSlash = strrchr( thisFileName, PATH_SEP );
571 if ( lastSlash == 0 )
572 checks[nextCheck++] = data;
574 long givenPathLen = (lastSlash - thisFileName) + 1;
575 long checklen = givenPathLen + length;
576 char *check = new char[checklen+1];
577 memcpy( check, thisFileName, givenPathLen );
578 memcpy( check+givenPathLen, data, length );
580 checks[nextCheck++] = check;
583 /* Search from the include paths given on the command line. */
584 for ( ArgsVector::Iter incp = includePaths; incp.lte(); incp++ ) {
585 long pathLen = strlen( *incp );
586 long checkLen = pathLen + 1 + length;
587 char *check = new char[checkLen+1];
588 memcpy( check, *incp, pathLen );
589 check[pathLen] = PATH_SEP;
590 memcpy( check+pathLen+1, data, length );
592 checks[nextCheck++] = check;
596 checks[nextCheck] = 0;
600 ifstream *Scanner::tryOpenInclude( char **pathChecks, long &found )
602 char **check = pathChecks;
603 ifstream *inFile = new ifstream;
605 while ( *check != 0 ) {
606 inFile->open( *check );
607 if ( inFile->is_open() ) {
608 found = check - pathChecks;
622 # This is sent by the driver code.
632 # Identifiers, numbers, commetns, and other common things.
633 ident = ( alpha | '_' ) ( alpha |digit |'_' )*;
635 hex_number = '0x' [0-9a-fA-F]+;
638 '/*' ( any | NL )* :>> '*/';
643 c_cpp_comment = c_comment | cpp_comment;
645 ruby_comment = '#' [^\n]* NL;
647 # These literal forms are common to host code and ragel.
648 s_literal = "'" ([^'\\] | NL | '\\' (any | NL))* "'";
649 d_literal = '"' ([^"\\] | NL | '\\' (any | NL))* '"';
650 host_re_literal = '/' ([^/\\] | NL | '\\' (any | NL))* '/';
652 whitespace = [ \t] | NL;
653 pound_comment = '#' [^\n]* NL;
655 # An inline block of code for Ruby.
656 inline_code_ruby := |*
657 # Inline expression keywords.
658 "fpc" => { token( KW_PChar ); };
659 "fc" => { token( KW_Char ); };
660 "fcurs" => { token( KW_CurState ); };
661 "ftargs" => { token( KW_TargState ); };
663 whitespaceOn = false;
667 # Inline statement keywords.
669 whitespaceOn = false;
672 "fexec" => { token( KW_Exec, 0, 0 ); };
674 whitespaceOn = false;
678 whitespaceOn = false;
682 whitespaceOn = false;
686 whitespaceOn = false;
690 whitespaceOn = false;
694 ident => { token( TK_Word, ts, te ); };
696 number => { token( TK_UInt, ts, te ); };
697 hex_number => { token( TK_Hex, ts, te ); };
699 ( s_literal | d_literal | host_re_literal )
700 => { token( IL_Literal, ts, te ); };
704 token( IL_WhiteSpace, ts, te );
707 ruby_comment => { token( IL_Comment, ts, te ); };
709 "::" => { token( TK_NameSep, ts, te ); };
711 # Some symbols need to go to the parser as with their cardinal value as
712 # the token type (as opposed to being sent as anonymous symbols)
713 # because they are part of the sequences which we interpret. The * ) ;
714 # symbols cause whitespace parsing to come back on. This gets turned
715 # off by some keywords.
719 token( *ts, ts, te );
720 if ( inlineBlockType == SemiTerminated )
726 token( *ts, ts, te );
729 [,(] => { token( *ts, ts, te ); };
732 token( IL_Symbol, ts, te );
737 if ( --curly_count == 0 && inlineBlockType == CurlyDelimited ) {
738 /* Inline code block ends. */
743 /* Either a semi terminated inline block or only the closing
744 * brace of some inner scope, not the block's closing brace. */
745 token( IL_Symbol, ts, te );
750 scan_error() << "unterminated code block" << endl;
753 # Send every other character as a symbol.
754 any => { token( IL_Symbol, ts, te ); };
758 # An inline block of code for languages other than Ruby.
760 # Inline expression keywords.
761 "fpc" => { token( KW_PChar ); };
762 "fc" => { token( KW_Char ); };
763 "fcurs" => { token( KW_CurState ); };
764 "ftargs" => { token( KW_TargState ); };
766 whitespaceOn = false;
770 # Inline statement keywords.
772 whitespaceOn = false;
775 "fexec" => { token( KW_Exec, 0, 0 ); };
777 whitespaceOn = false;
781 whitespaceOn = false;
785 whitespaceOn = false;
789 whitespaceOn = false;
793 whitespaceOn = false;
797 ident => { token( TK_Word, ts, te ); };
799 number => { token( TK_UInt, ts, te ); };
800 hex_number => { token( TK_Hex, ts, te ); };
802 ( s_literal | d_literal )
803 => { token( IL_Literal, ts, te ); };
807 token( IL_WhiteSpace, ts, te );
810 c_cpp_comment => { token( IL_Comment, ts, te ); };
812 "::" => { token( TK_NameSep, ts, te ); };
814 # Some symbols need to go to the parser as with their cardinal value as
815 # the token type (as opposed to being sent as anonymous symbols)
816 # because they are part of the sequences which we interpret. The * ) ;
817 # symbols cause whitespace parsing to come back on. This gets turned
818 # off by some keywords.
822 token( *ts, ts, te );
823 if ( inlineBlockType == SemiTerminated )
829 token( *ts, ts, te );
832 [,(] => { token( *ts, ts, te ); };
835 token( IL_Symbol, ts, te );
840 if ( --curly_count == 0 && inlineBlockType == CurlyDelimited ) {
841 /* Inline code block ends. */
846 /* Either a semi terminated inline block or only the closing
847 * brace of some inner scope, not the block's closing brace. */
848 token( IL_Symbol, ts, te );
853 scan_error() << "unterminated code block" << endl;
856 # Send every other character as a symbol.
857 any => { token( IL_Symbol, ts, te ); };
861 # Escape sequences in OR expressions.
862 '\\0' => { token( RE_Char, '\0' ); };
863 '\\a' => { token( RE_Char, '\a' ); };
864 '\\b' => { token( RE_Char, '\b' ); };
865 '\\t' => { token( RE_Char, '\t' ); };
866 '\\n' => { token( RE_Char, '\n' ); };
867 '\\v' => { token( RE_Char, '\v' ); };
868 '\\f' => { token( RE_Char, '\f' ); };
869 '\\r' => { token( RE_Char, '\r' ); };
870 '\\\n' => { updateCol(); };
871 '\\' any => { token( RE_Char, ts+1, te ); };
873 # Range dash in an OR expression.
874 '-' => { token( RE_Dash, 0, 0 ); };
876 # Terminate an OR expression.
877 ']' => { token( RE_SqClose ); fret; };
880 scan_error() << "unterminated OR literal" << endl;
883 # Characters in an OR expression.
884 [^\]] => { token( RE_Char, ts, te ); };
888 ragel_re_literal := |*
889 # Escape sequences in regular expressions.
890 '\\0' => { token( RE_Char, '\0' ); };
891 '\\a' => { token( RE_Char, '\a' ); };
892 '\\b' => { token( RE_Char, '\b' ); };
893 '\\t' => { token( RE_Char, '\t' ); };
894 '\\n' => { token( RE_Char, '\n' ); };
895 '\\v' => { token( RE_Char, '\v' ); };
896 '\\f' => { token( RE_Char, '\f' ); };
897 '\\r' => { token( RE_Char, '\r' ); };
898 '\\\n' => { updateCol(); };
899 '\\' any => { token( RE_Char, ts+1, te ); };
901 # Terminate an OR expression.
903 token( RE_Slash, ts, te );
907 # Special characters.
908 '.' => { token( RE_Dot ); };
909 '*' => { token( RE_Star ); };
911 '[' => { token( RE_SqOpen ); fcall or_literal; };
912 '[^' => { token( RE_SqOpenNeg ); fcall or_literal; };
915 scan_error() << "unterminated regular expression" << endl;
918 # Characters in an OR expression.
919 [^\/] => { token( RE_Char, ts, te ); };
922 # We need a separate token space here to avoid the ragel keywords.
923 write_statement := |*
924 ident => { token( TK_Word, ts, te ); } ;
925 [ \t\n]+ => { updateCol(); };
926 ';' => { token( ';' ); fgoto parser_def; };
929 scan_error() << "unterminated write statement" << endl;
933 # Parser definitions.
935 'machine' => { token( KW_Machine ); };
936 'include' => { token( KW_Include ); };
937 'import' => { token( KW_Import ); };
940 fgoto write_statement;
942 'action' => { token( KW_Action ); };
943 'alphtype' => { token( KW_AlphType ); };
944 'prepush' => { token( KW_PrePush ); };
945 'postpop' => { token( KW_PostPop ); };
947 # FIXME: Enable this post 5.17.
948 # 'range' => { token( KW_Range ); };
952 inlineBlockType = SemiTerminated;
953 if ( hostLang->lang == HostLang::Ruby )
954 fcall inline_code_ruby;
960 inlineBlockType = SemiTerminated;
961 if ( hostLang->lang == HostLang::Ruby )
962 fcall inline_code_ruby;
967 token( KW_Variable );
968 inlineBlockType = SemiTerminated;
969 if ( hostLang->lang == HostLang::Ruby )
970 fcall inline_code_ruby;
974 'when' => { token( KW_When ); };
975 'inwhen' => { token( KW_InWhen ); };
976 'outwhen' => { token( KW_OutWhen ); };
977 'eof' => { token( KW_Eof ); };
978 'err' => { token( KW_Err ); };
979 'lerr' => { token( KW_Lerr ); };
980 'to' => { token( KW_To ); };
981 'from' => { token( KW_From ); };
982 'export' => { token( KW_Export ); };
985 ident => { token( TK_Word, ts, te ); } ;
988 number => { token( TK_UInt, ts, te ); };
989 hex_number => { token( TK_Hex, ts, te ); };
991 # Literals, with optionals.
992 ( s_literal | d_literal ) [i]?
993 => { token( TK_Literal, ts, te ); };
995 '[' => { token( RE_SqOpen ); fcall or_literal; };
996 '[^' => { token( RE_SqOpenNeg ); fcall or_literal; };
998 '/' => { token( RE_Slash ); fgoto ragel_re_literal; };
1001 pound_comment => { updateCol(); };
1003 ':=' => { token( TK_ColonEquals ); };
1006 ">~" => { token( TK_StartToState ); };
1007 "$~" => { token( TK_AllToState ); };
1008 "%~" => { token( TK_FinalToState ); };
1009 "<~" => { token( TK_NotStartToState ); };
1010 "@~" => { token( TK_NotFinalToState ); };
1011 "<>~" => { token( TK_MiddleToState ); };
1013 # From State actions
1014 ">*" => { token( TK_StartFromState ); };
1015 "$*" => { token( TK_AllFromState ); };
1016 "%*" => { token( TK_FinalFromState ); };
1017 "<*" => { token( TK_NotStartFromState ); };
1018 "@*" => { token( TK_NotFinalFromState ); };
1019 "<>*" => { token( TK_MiddleFromState ); };
1022 ">/" => { token( TK_StartEOF ); };
1023 "$/" => { token( TK_AllEOF ); };
1024 "%/" => { token( TK_FinalEOF ); };
1025 "</" => { token( TK_NotStartEOF ); };
1026 "@/" => { token( TK_NotFinalEOF ); };
1027 "<>/" => { token( TK_MiddleEOF ); };
1029 # Global Error actions.
1030 ">!" => { token( TK_StartGblError ); };
1031 "$!" => { token( TK_AllGblError ); };
1032 "%!" => { token( TK_FinalGblError ); };
1033 "<!" => { token( TK_NotStartGblError ); };
1034 "@!" => { token( TK_NotFinalGblError ); };
1035 "<>!" => { token( TK_MiddleGblError ); };
1037 # Local error actions.
1038 ">^" => { token( TK_StartLocalError ); };
1039 "$^" => { token( TK_AllLocalError ); };
1040 "%^" => { token( TK_FinalLocalError ); };
1041 "<^" => { token( TK_NotStartLocalError ); };
1042 "@^" => { token( TK_NotFinalLocalError ); };
1043 "<>^" => { token( TK_MiddleLocalError ); };
1046 "<>" => { token( TK_Middle ); };
1049 '>?' => { token( TK_StartCond ); };
1050 '$?' => { token( TK_AllCond ); };
1051 '%?' => { token( TK_LeavingCond ); };
1053 '..' => { token( TK_DotDot ); };
1054 '**' => { token( TK_StarStar ); };
1055 '--' => { token( TK_DashDash ); };
1056 '->' => { token( TK_Arrow ); };
1057 '=>' => { token( TK_DoubleArrow ); };
1059 ":>" => { token( TK_ColonGt ); };
1060 ":>>" => { token( TK_ColonGtGt ); };
1061 "<:" => { token( TK_LtColon ); };
1063 # Opening of longest match.
1064 "|*" => { token( TK_BarStar ); };
1066 # Separater for name references.
1067 "::" => { token( TK_NameSep, ts, te ); };
1075 [ \t\r]+ => { updateCol(); };
1077 # If we are in a single line machine then newline may end the spec.
1080 if ( singleLineSpec ) {
1087 if ( lastToken == KW_Export || lastToken == KW_Entry )
1092 inlineBlockType = CurlyDelimited;
1093 if ( hostLang->lang == HostLang::Ruby )
1094 fcall inline_code_ruby;
1101 scan_error() << "unterminated ragel section" << endl;
1104 any => { token( *ts ); } ;
1107 # Outside code scanner. These tokens get passed through.
1109 ident => { pass( IMP_Word, ts, te ); };
1110 number => { pass( IMP_UInt, ts, te ); };
1111 ruby_comment => { pass(); };
1112 ( s_literal | d_literal | host_re_literal )
1113 => { pass( IMP_Literal, ts, te ); };
1117 singleLineSpec = false;
1123 singleLineSpec = true;
1127 whitespace+ => { pass(); };
1129 any => { pass( *ts, 0, 0 ); };
1132 # Outside code scanner. These tokens get passed through.
1134 'define' => { pass( IMP_Define, 0, 0 ); };
1135 ident => { pass( IMP_Word, ts, te ); };
1136 number => { pass( IMP_UInt, ts, te ); };
1137 c_cpp_comment => { pass(); };
1138 ( s_literal | d_literal ) => { pass( IMP_Literal, ts, te ); };
1142 singleLineSpec = false;
1148 singleLineSpec = true;
1152 whitespace+ => { pass(); };
1154 any => { pass( *ts, 0, 0 ); };
1160 void Scanner::do_scan()
1163 char *buf = new char[bufsize];
1164 int cs, act, have = 0;
1167 /* The stack is two deep, one level for going into ragel defs from the main
1168 * machines which process outside code, and another for going into or literals
1169 * from either a ragel spec, or a regular expression. */
1171 int curly_count = 0;
1172 bool execute = true;
1173 bool singleLineSpec = false;
1174 InlineBlockType inlineBlockType = CurlyDelimited;
1176 /* Init the section parser and the character scanner. */
1180 /* Set up the start state. FIXME: After 5.20 is released the nocs write
1181 * init option should be used, the main machine eliminated and this statement moved
1182 * above the write init. */
1183 if ( hostLang->lang == HostLang::Ruby )
1184 cs = rlscan_en_main_ruby;
1186 cs = rlscan_en_main;
1189 char *p = buf + have;
1190 int space = bufsize - have;
1193 /* We filled up the buffer trying to scan a token. Grow it. */
1194 bufsize = bufsize * 2;
1195 char *newbuf = new char[bufsize];
1197 /* Recompute p and space. */
1199 space = bufsize - have;
1201 /* Patch up pointers possibly in use. */
1203 ts = newbuf + ( ts - buf );
1204 te = newbuf + ( te - buf );
1206 /* Copy the new buffer in. */
1207 memcpy( newbuf, buf, have );
1212 input.read( p, space );
1213 int len = input.gcount();
1216 /* If we see eof then append the eof var. */
1225 /* Check if we failed. */
1226 if ( cs == rlscan_error ) {
1227 /* Machine failed before finding a token. I'm not yet sure if this
1229 scan_error() << "scanner error" << endl;
1233 /* Decide if we need to preserve anything. */
1234 char *preserve = ts;
1236 /* Now set up the prefix. */
1237 if ( preserve == 0 )
1240 /* There is data that needs to be shifted over. */
1241 have = pe - preserve;
1242 memmove( buf, preserve, have );
1243 unsigned int shiftback = preserve - buf;