1 /* scan.l - scanner for flex input -*-C-*- */
4 /* Copyright (c) 1990 The Regents of the University of California. */
5 /* All rights reserved. */
7 /* This code is derived from software contributed to Berkeley by */
10 /* The United States Government has rights in this work pursuant */
11 /* to contract no. DE-AC03-76SF00098 between the United States */
12 /* Department of Energy and the University of California. */
14 /* This file is part of flex. */
16 /* Redistribution and use in source and binary forms, with or without */
17 /* modification, are permitted provided that the following conditions */
20 /* 1. Redistributions of source code must retain the above copyright */
21 /* notice, this list of conditions and the following disclaimer. */
22 /* 2. Redistributions in binary form must reproduce the above copyright */
23 /* notice, this list of conditions and the following disclaimer in the */
24 /* documentation and/or other materials provided with the distribution. */
26 /* Neither the name of the University nor the names of its contributors */
27 /* may be used to endorse or promote products derived from this software */
28 /* without specific prior written permission. */
30 /* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
31 /* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
32 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
37 extern bool tablesverify, tablesext;
38 extern int trlcontxt; /* Set in parse.y for each rule. */
40 #define ACTION_ECHO add_action( yytext )
41 #define ACTION_IFDEF(def, should_define) \
43 if ( should_define ) \
44 action_define( def, 1 ); \
47 #define ACTION_M4_IFDEF(def, should_define) \
49 if ( should_define ) \
50 buf_m4_define( &m4defs_buf, def, NULL);\
52 buf_m4_undefine( &m4defs_buf, def);\
55 #define MARK_END_OF_PROLOG mark_prolog();
61 yylval = (unsigned char) yytext[0]; \
65 if(yyleng < MAXLINE) \
67 strcpy( nmstr, yytext ); \
71 synerr(_("Input line too long\n")); \
72 FLEX_EXIT(EXIT_FAILURE); \
76 #define PUT_BACK_STRING(str, start) \
77 for ( i = strlen( str ) - 1; i >= start; --i ) \
80 #define CHECK_REJECT(str) \
81 if ( all_upper( str ) ) \
84 #define CHECK_YYMORE(str) \
85 if ( all_lower( str ) ) \
88 #define YY_USER_INIT \
89 if ( getenv("POSIXLY_CORRECT") ) \
94 %option caseless nodefault stack noyy_top_state
97 %x SECT2 SECT2PROLOG SECT3 CODEBLOCK PICKUPDEF SC CARETISBOL NUM QUOTE
98 %x FIRSTCCL CCL ACTION RECOVER COMMENT ACTION_STRING PERCENT_BRACE_ACTION
99 %x OPTION LINEDIR CODEBLOCK_MATCH_BRACE
103 NOT_WS [^[:blank:]\r\n]
107 NAME ([[:alpha:]_][[:alnum:]_-]*)
108 NOT_NAME [^[:alpha:]_*\n]+
112 ESCSEQ (\\([^\n]|[0-7]{1,3}|x[[:xdigit:]]{1,2}))
114 FIRST_CCL_CHAR ([^\\\n]|{ESCSEQ})
115 CCL_CHAR ([^\\\n\]]|{ESCSEQ})
116 CCL_EXPR ("[:"[[:alpha:]]+":]")
121 static int bracelevel, didadef, indented_code;
122 static int doing_rule_action = false;
123 static int option_sense;
125 int doing_codeblock = false;
126 int i, brace_depth=0, brace_start_line=0;
131 ^{WS} indented_code = true; BEGIN(CODEBLOCK);
132 ^"/*" ACTION_ECHO; yy_push_state( COMMENT );
133 ^#{OPTWS}line{WS} yy_push_state( LINEDIR );
134 ^"%s"{NAME}? return SCDECL;
135 ^"%x"{NAME}? return XSCDECL;
138 line_directive_out( (FILE *) 0, 1 );
139 indented_code = false;
142 ^"%top"[[:blank:]]*"{"[[:blank:]]*{NL} {
143 brace_start_line = linenum;
145 buf_linedir( &top_buf, infilename?infilename:"<stdin>", linenum);
147 yy_push_state(CODEBLOCK_MATCH_BRACE);
150 ^"%top".* synerr( _("malformed '%top' directive") );
158 line_directive_out( (FILE *) 0, 1 );
163 ^"%pointer".*{NL} yytext_is_array = false; ++linenum;
164 ^"%array".*{NL} yytext_is_array = true; ++linenum;
166 ^"%option" BEGIN(OPTION); return OPTION_OP;
168 ^"%"{LEXOPT}{OPTWS}[[:digit:]]*{OPTWS}{NL} ++linenum; /* ignore */
169 ^"%"{LEXOPT}{WS}.*{NL} ++linenum; /* ignore */
171 /* xgettext: no-c-format */
172 ^"%"[^sxaceknopr{}].* synerr( _( "unrecognized '%' directive" ) );
177 strcpy( nmstr, yytext );
181 synerr( _("Input line too long\n"));
182 FLEX_EXIT(EXIT_FAILURE);
190 ^{OPTWS}{NL} ++linenum; /* allows blank lines in section 1 */
191 {OPTWS}{NL} ACTION_ECHO; ++linenum; /* maybe end of comment line */
196 "*/" ACTION_ECHO; yy_pop_state();
199 [^*\n]*{NL} ++linenum; ACTION_ECHO;
204 [[:digit:]]+ linenum = myctoi( yytext );
207 flex_free( (void *) infilename );
208 infilename = copy_string( yytext + 1 );
209 infilename[strlen( infilename ) - 1] = '\0';
211 . /* ignore spurious characters */
215 ^"%}".*{NL} ++linenum; BEGIN(INITIAL);
217 {NAME}|{NOT_NAME}|. ACTION_ECHO;
227 <CODEBLOCK_MATCH_BRACE>{
229 if( --brace_depth == 0){
233 buf_strnappend(&top_buf, yytext, yyleng);
238 buf_strnappend(&top_buf, yytext, yyleng);
243 buf_strnappend(&top_buf, yytext, yyleng);
247 buf_strnappend(&top_buf, yytext, yyleng);
251 linenum = brace_start_line;
252 synerr(_("Unmatched '{'"));
259 {WS} /* separates name and definition */
264 strcpy( (char *) nmdef, yytext );
268 synerr( _("Input line too long\n"));
269 FLEX_EXIT(EXIT_FAILURE);
271 /* Skip trailing whitespace. */
272 for ( i = strlen( (char *) nmdef ) - 1;
273 i >= 0 && (nmdef[i] == ' ' || nmdef[i] == '\t');
279 ndinstal( nmstr, nmdef );
285 synerr( _( "incomplete name definition" ) );
293 {NL} ++linenum; BEGIN(INITIAL);
294 {WS} option_sense = true;
298 no option_sense = ! option_sense;
300 7bit csize = option_sense ? 128 : 256;
301 8bit csize = option_sense ? 256 : 128;
303 align long_align = option_sense;
305 ACTION_M4_IFDEF( "M4""_YY_ALWAYS_INTERACTIVE", option_sense );
306 interactive = option_sense;
308 array yytext_is_array = option_sense;
309 ansi-definitions ansi_func_defs = option_sense;
310 ansi-prototypes ansi_func_protos = option_sense;
311 backup backing_up_report = option_sense;
312 batch interactive = ! option_sense;
313 bison-bridge bison_bridge_lval = option_sense;
314 bison-locations { if((bison_bridge_lloc = option_sense))
315 bison_bridge_lval = true;
317 "c++" C_plus_plus = option_sense;
318 caseful|case-sensitive caseins = ! option_sense;
319 caseless|case-insensitive caseins = option_sense;
320 debug ddebug = option_sense;
321 default spprdflt = ! option_sense;
322 ecs useecs = option_sense;
324 useecs = usemecs = false;
325 use_read = fullspd = true;
328 useecs = usemecs = false;
329 use_read = fulltbl = true;
331 input ACTION_IFDEF("YY_NO_INPUT", ! option_sense);
332 interactive interactive = option_sense;
333 lex-compat lex_compat = option_sense;
334 posix-compat posix_compat = option_sense;
336 ACTION_M4_IFDEF( "M4""_YY_MAIN", option_sense);
337 /* Override yywrap */
338 if( option_sense == true )
341 meta-ecs usemecs = option_sense;
343 ACTION_M4_IFDEF( "M4""_YY_NEVER_INTERACTIVE", option_sense );
344 interactive = !option_sense;
346 perf-report performance_report += option_sense ? 1 : -1;
347 pointer yytext_is_array = ! option_sense;
348 read use_read = option_sense;
349 reentrant reentrant = option_sense;
350 reject reject_really_used = option_sense;
351 stack ACTION_M4_IFDEF( "M4""_YY_STACK_USED", option_sense );
352 stdinit do_stdinit = option_sense;
353 stdout use_stdout = option_sense;
354 unistd ACTION_IFDEF("YY_NO_UNISTD_H", ! option_sense);
355 unput ACTION_M4_IFDEF("M4""_YY_NO_UNPUT", ! option_sense);
356 verbose printstats = option_sense;
357 warn nowarn = ! option_sense;
358 yylineno do_yylineno = option_sense; ACTION_M4_IFDEF("M4""_YY_USE_LINENO", option_sense);
359 yymore yymore_really_used = option_sense;
360 yywrap do_yywrap = option_sense;
362 yy_push_state ACTION_M4_IFDEF("M4""_YY_NO_PUSH_STATE", ! option_sense);
363 yy_pop_state ACTION_M4_IFDEF("M4""_YY_NO_POP_STATE", ! option_sense);
364 yy_top_state ACTION_M4_IFDEF("M4""_YY_NO_TOP_STATE", ! option_sense);
366 yy_scan_buffer ACTION_M4_IFDEF("M4""_YY_NO_SCAN_BUFFER", ! option_sense);
367 yy_scan_bytes ACTION_M4_IFDEF("M4""_YY_NO_SCAN_BYTES", ! option_sense);
368 yy_scan_string ACTION_M4_IFDEF("M4""_YY_NO_SCAN_STRING", ! option_sense);
370 yyalloc ACTION_M4_IFDEF("M4""_YY_NO_FLEX_ALLOC", ! option_sense);
371 yyrealloc ACTION_M4_IFDEF("M4""_YY_NO_FLEX_REALLOC", ! option_sense);
372 yyfree ACTION_M4_IFDEF("M4""_YY_NO_FLEX_FREE", ! option_sense);
374 yyget_debug ACTION_M4_IFDEF("M4""_YY_NO_GET_DEBUG", ! option_sense);
375 yyset_debug ACTION_M4_IFDEF("M4""_YY_NO_SET_DEBUG", ! option_sense);
376 yyget_extra ACTION_M4_IFDEF("M4""_YY_NO_GET_EXTRA", ! option_sense);
377 yyset_extra ACTION_M4_IFDEF("M4""_YY_NO_SET_EXTRA", ! option_sense);
378 yyget_leng ACTION_M4_IFDEF("M4""_YY_NO_GET_LENG", ! option_sense);
379 yyget_text ACTION_M4_IFDEF("M4""_YY_NO_GET_TEXT", ! option_sense);
380 yyget_lineno ACTION_M4_IFDEF("M4""_YY_NO_GET_LINENO", ! option_sense);
381 yyset_lineno ACTION_M4_IFDEF("M4""_YY_NO_SET_LINENO", ! option_sense);
382 yyget_in ACTION_M4_IFDEF("M4""_YY_NO_GET_IN", ! option_sense);
383 yyset_in ACTION_M4_IFDEF("M4""_YY_NO_SET_IN", ! option_sense);
384 yyget_out ACTION_M4_IFDEF("M4""_YY_NO_GET_OUT", ! option_sense);
385 yyset_out ACTION_M4_IFDEF("M4""_YY_NO_SET_OUT", ! option_sense);
386 yyget_lval ACTION_M4_IFDEF("M4""_YY_NO_GET_LVAL", ! option_sense);
387 yyset_lval ACTION_M4_IFDEF("M4""_YY_NO_SET_LVAL", ! option_sense);
388 yyget_lloc ACTION_M4_IFDEF("M4""_YY_NO_GET_LLOC", ! option_sense);
389 yyset_lloc ACTION_M4_IFDEF("M4""_YY_NO_SET_LLOC", ! option_sense);
391 outfile return OPT_OUTFILE;
392 prefix return OPT_PREFIX;
393 yyclass return OPT_YYCLASS;
394 header(-file)? return OPT_HEADER;
395 tables-file return OPT_TABLES;
397 tablesverify = option_sense;
398 if(!tablesext && option_sense)
404 if(yyleng-1 < MAXLINE)
406 strcpy( nmstr, yytext + 1 );
410 synerr( _("Input line too long\n"));
411 FLEX_EXIT(EXIT_FAILURE);
413 nmstr[strlen( nmstr ) - 1] = '\0';
417 (([a-mo-z]|n[a-np-z])[[:alpha:]\-+]*)|. {
418 format_synerr( _( "unrecognized %%option: %s" ),
424 <RECOVER>.*{NL} ++linenum; BEGIN(INITIAL);
428 ^"%{".* ++bracelevel; yyless( 2 ); /* eat only %{ */
429 ^"%}".* --bracelevel; yyless( 2 ); /* eat only %} */
431 ^{WS}.* ACTION_ECHO; /* indented code in prolog */
433 ^{NOT_WS}.* { /* non-indented code */
434 if ( bracelevel <= 0 )
435 { /* not in %{ ... %} */
436 yyless( 0 ); /* put it all back */
446 {NL} ++linenum; ACTION_ECHO;
451 yyterminate(); /* to stop the parser */
456 ^{OPTWS}{NL} ++linenum; /* allow blank lines in section 2 */
459 indented_code = false;
460 doing_codeblock = true;
462 BEGIN(PERCENT_BRACE_ACTION);
465 ^{OPTWS}"<" BEGIN(SC); return '<';
466 ^{OPTWS}"^" return '^';
467 \" BEGIN(QUOTE); return '"';
470 if ( lex_compat || posix_compat )
471 return BEGIN_REPEAT_POSIX;
473 return BEGIN_REPEAT_FLEX;
475 "$"/([[:blank:]]|{NL}) return '$';
479 BEGIN(PERCENT_BRACE_ACTION);
483 doing_rule_action = true;
488 {WS}"|".*{NL} continued_action = true; ++linenum; return '\n';
491 yyless( yyleng - 2 ); /* put back '/', '*' */
493 continued_action = false;
497 ^{WS} /* allow indented rules */
500 /* This rule is separate from the one below because
501 * otherwise we get variable trailing context, so
502 * we can't build the scanner using -{f,F}.
505 continued_action = false;
510 doing_rule_action = true;
518 continued_action = false;
520 unput( '\n' ); /* so <ACTION> sees it */
524 doing_rule_action = true;
531 "<<EOF>>" return EOF_OP;
536 outn("/* Begin user sect3 */");
537 yyterminate(); /* to stop the parser */
540 "["({FIRST_CCL_CHAR}|{CCL_EXPR})({CCL_CHAR}|{CCL_EXPR})* {
545 strcpy( nmstr, yytext );
549 synerr( _("Input line too long\n"));
550 FLEX_EXIT(EXIT_FAILURE);
553 /* Check to see if we've already encountered this
556 if ( (cclval = ccllookup( (Char *) nmstr )) != 0 )
558 if ( input() != ']' )
559 synerr( _( "bad character class" ) );
567 /* We fudge a bit. We know that this ccl will
568 * soon be numbered as lastccl + 1 by cclinit.
570 cclinstal( (Char *) nmstr, lastccl + 1 );
572 /* Push back everything but the leading bracket
573 * so the ccl can be rescanned.
582 /* Check for :space: at the end of the rule so we don't
583 * wrap the expanded regex in '(' ')' -- breaking trailing
586 "{"{NAME}"}"[[:space:]]? {
587 register Char *nmdefptr;
588 int end_is_ws, end_ch;
590 end_ch = yytext[yyleng-1];
591 end_is_ws = end_ch != '}' ? 1 : 0;
593 if(yyleng-1 < MAXLINE)
595 strcpy( nmstr, yytext + 1 );
599 synerr( _("Input line too long\n"));
600 FLEX_EXIT(EXIT_FAILURE);
602 nmstr[yyleng - 2 - end_is_ws] = '\0'; /* chop trailing brace */
604 if ( (nmdefptr = ndlookup( nmstr )) == 0 )
606 _( "undefined definition {%s}" ),
610 { /* push back name surrounded by ()'s */
611 int len = strlen( (char *) nmdefptr );
615 if ( lex_compat || nmdefptr[0] == '^' ||
616 (len > 0 && nmdefptr[len - 1] == '$')
617 || (end_is_ws && trlcontxt))
618 { /* don't use ()'s after all */
619 PUT_BACK_STRING((char *) nmdefptr, 0);
621 if ( nmdefptr[0] == '^' )
628 PUT_BACK_STRING((char *) nmdefptr, 0);
634 [/|*+?.(){}] return (unsigned char) yytext[0];
640 {OPTWS}{NL}{OPTWS} ++linenum; /* Allow blank lines & continuations */
641 [,*] return (unsigned char) yytext[0];
642 ">" BEGIN(SECT2); return '>';
643 ">"/^ BEGIN(CARETISBOL); return '>';
646 format_synerr( _( "bad <start condition>: %s" ),
651 <CARETISBOL>"^" BEGIN(SECT2); return '^';
656 \" BEGIN(SECT2); return '"';
659 synerr( _( "missing quote" ) );
668 "^"/[^-\]\n] BEGIN(CCL); return '^';
669 "^"/("-"|"]") return '^';
670 . BEGIN(CCL); RETURNCHAR;
674 -/[^\]\n] return '-';
676 "]" BEGIN(SECT2); return ']';
678 synerr( _( "bad character class" ) );
685 "[:alnum:]" BEGIN(CCL); return CCE_ALNUM;
686 "[:alpha:]" BEGIN(CCL); return CCE_ALPHA;
687 "[:blank:]" BEGIN(CCL); return CCE_BLANK;
688 "[:cntrl:]" BEGIN(CCL); return CCE_CNTRL;
689 "[:digit:]" BEGIN(CCL); return CCE_DIGIT;
690 "[:graph:]" BEGIN(CCL); return CCE_GRAPH;
691 "[:lower:]" BEGIN(CCL); return CCE_LOWER;
692 "[:print:]" BEGIN(CCL); return CCE_PRINT;
693 "[:punct:]" BEGIN(CCL); return CCE_PUNCT;
694 "[:space:]" BEGIN(CCL); return CCE_SPACE;
695 "[:upper:]" BEGIN(CCL); return CCE_UPPER;
696 "[:xdigit:]" BEGIN(CCL); return CCE_XDIGIT;
699 _( "bad character class expression: %s" ),
701 BEGIN(CCL); return CCE_ALNUM;
707 yylval = myctoi( yytext );
714 if ( lex_compat || posix_compat )
715 return END_REPEAT_POSIX;
717 return END_REPEAT_FLEX;
721 synerr( _( "bad character inside {}'s" ) );
727 synerr( _( "missing }" ) );
735 <PERCENT_BRACE_ACTION>{
736 {OPTWS}"%}".* bracelevel = 0;
738 <ACTION>"/*" ACTION_ECHO; yy_push_state( COMMENT );
743 CHECK_REJECT(yytext);
747 CHECK_YYMORE(yytext);
751 {NAME}|{NOT_NAME}|. ACTION_ECHO;
755 if ( bracelevel == 0 ||
756 (doing_codeblock && indented_code) )
758 if ( doing_rule_action )
759 add_action( "\tYY_BREAK\n" );
761 doing_rule_action = doing_codeblock = false;
768 /* Reject and YYmore() are checked for above, in PERCENT_BRACE_ACTION */
770 "{" ACTION_ECHO; ++bracelevel;
771 "}" ACTION_ECHO; --bracelevel;
772 [^[:alpha:]_{}"'/\n]+ ACTION_ECHO;
774 "'"([^'\\\n]|\\.)*"'" ACTION_ECHO; /* character constant */
775 \" ACTION_ECHO; BEGIN(ACTION_STRING);
779 if ( bracelevel == 0 )
781 if ( doing_rule_action )
782 add_action( "\tYY_BREAK\n" );
784 doing_rule_action = false;
792 [^"\\\n]+ ACTION_ECHO;
794 {NL} ++linenum; ACTION_ECHO; BEGIN(ACTION);
795 \" ACTION_ECHO; BEGIN(ACTION);
799 <COMMENT,ACTION,ACTION_STRING><<EOF>> {
800 synerr( _( "EOF encountered inside an action" ) );
805 <SECT2,QUOTE,FIRSTCCL,CCL>{ESCSEQ} {
806 yylval = myesc( (Char *) yytext );
808 if ( YY_START == FIRSTCCL )
817 <<EOF>> sectnum = 0; yyterminate();
820 <*>.|\n format_synerr( _( "bad character: %s" ), yytext );
827 if ( --num_input_files > 0 )
829 set_input_file( *++input_files );
838 /* set_input_file - open the given file (if NULL, stdin) for scanning */
840 void set_input_file( file )
843 if ( file && strcmp( file, "-" ) )
845 infilename = copy_string( file );
846 yyin = fopen( infilename, "r" );
849 lerrsf( _( "can't open %s" ), file );
855 infilename = copy_string( "<stdin>" );
862 /* Wrapper routines for accessing the scanner's malloc routines. */
864 void *flex_alloc( size )
867 return (void *) malloc( size );
870 void *flex_realloc( ptr, size )
874 return (void *) realloc( ptr, size );
877 void flex_free( ptr )