02111-1307 USA
*/
-%option debug nodefault noyywrap nounput never-interactive stack
+%option debug nodefault noyywrap never-interactive
%option prefix="gram_" outfile="lex.yy.c"
%{
#include "mbswidth.h"
#include "complain.h"
#include "quote.h"
+#include "struniq.h"
#include "getargs.h"
#include "gram.h"
#include "reader.h"
#define YY_USER_INIT \
do { \
LOCATION_RESET (*yylloc); \
- yylloc->file = infile; \
+ yylloc->file = current_file; \
/* This is only to avoid GCC warnings. */ \
if (yycontrol) {;}; \
} while (0)
used, and which is used by YY_OBS_FREE to free the last string. */
static struct obstack string_obstack;
-char *last_string;
+
+/* A string representing the most recently saved token. */
+static char *last_string;
+
#define YY_OBS_GROW \
obstack_grow (&string_obstack, yytext, yyleng)
last_string = obstack_finish (&string_obstack); \
} while (0)
-#define YY_OBS_FREE \
- do { \
- obstack_free (&string_obstack, last_string); \
- } while (0)
+#define YY_OBS_FREE \
+ obstack_free (&string_obstack, last_string)
void
scanner_last_string_free (void)
YY_OBS_FREE;
}
-
-static int braces_level = 0;
-static int percent_percent_count = 0;
-
/* Within well-formed rules, RULE_LENGTH is the number of values in
the current rule so far, which says where to find `$0' with respect
to the top of the stack. It is not the same as the rule->length in
char *cp, location_t location);
static void handle_at (braced_code_t code_kind,
char *cp, location_t location);
+static void handle_syncline (char *args, location_t *location);
static int convert_ucn_to_byte (char const *hex_text);
+static void unexpected_end_of_file (location_t *, char const *);
%}
%x SC_COMMENT SC_LINE_COMMENT SC_YACC_COMMENT
%x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
%x SC_BRACED_CODE SC_PROLOGUE SC_EPILOGUE
-letter [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
-id {letter}({letter}|[0-9])*
-int [0-9]+
+letter [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
+id {letter}({letter}|[0-9])*
+directive %{letter}({letter}|[0-9]|-)*
+int [0-9]+
/* POSIX says that a tag must be both an id and a C union member, but
historically almost any character is allowed in a tag. We disallow
%%
%{
+ /* Nesting level of the current code in braces. */
+ int braces_level IF_LINT (= 0);
+
+ /* Scanner context when scanning C code. */
+ int c_context IF_LINT (= 0);
+
/* At each yylex invocation, mark the current position as the
start of the next token. */
YY_STEP;
"%verbose" return PERCENT_VERBOSE;
"%yacc" return PERCENT_YACC;
+ {directive} {
+ complain_at (*yylloc, _("invalid directive: %s"), quote (yytext));
+ YY_STEP;
+ }
+
+ ^"#line "{int}" \"".*"\"\n" {
+ handle_syncline (yytext + sizeof "#line " - 1, yylloc);
+ YY_STEP;
+ }
+
"=" return EQUAL;
":" rule_length = 0; return COLON;
"|" rule_length = 0; return PIPE;
"," return COMMA;
";" return SEMICOLON;
- [ \f\n\t\v]+ YY_STEP;
+ [ \f\n\t\v] YY_STEP;
{id} {
yylval->symbol = symbol_get (yytext, *yylloc);
num = strtoul (yytext, 0, 10);
if (INT_MAX < num || errno)
{
- complain_at (*yylloc, _("invalid value: %s"), quote (yytext));
+ complain_at (*yylloc, _("integer out of range: %s"), quote (yytext));
num = INT_MAX;
}
yylval->integer = num;
}
/* Characters. We don't check there is only one. */
- "'" YY_OBS_GROW; yy_push_state (SC_ESCAPED_CHARACTER);
+ "'" YY_OBS_GROW; BEGIN SC_ESCAPED_CHARACTER;
/* Strings. */
- "\"" YY_OBS_GROW; yy_push_state (SC_ESCAPED_STRING);
+ "\"" YY_OBS_GROW; BEGIN SC_ESCAPED_STRING;
/* Comments. */
"/*" BEGIN SC_YACC_COMMENT;
"//".* YY_STEP;
/* Prologue. */
- "%{" yy_push_state (SC_PROLOGUE);
+ "%{" BEGIN SC_PROLOGUE;
/* Code in between braces. */
- "{" YY_OBS_GROW; ++braces_level; yy_push_state (SC_BRACED_CODE);
+ "{" YY_OBS_GROW; braces_level = 0; BEGIN SC_BRACED_CODE;
/* A type. */
"<"{tag}">" {
obstack_grow (&string_obstack, yytext + 1, yyleng - 2);
YY_OBS_FINISH;
- yylval->string = last_string;
+ yylval->struniq = struniq_new (last_string);
+ YY_OBS_FREE;
return TYPE;
}
-
- "%%" {
+ "%%" {
+ static int percent_percent_count;
if (++percent_percent_count == 2)
- yy_push_state (SC_EPILOGUE);
+ BEGIN SC_EPILOGUE;
return PERCENT_PERCENT;
}
- . {
+ . {
complain_at (*yylloc, _("invalid character: %s"), quote (yytext));
YY_STEP;
}
}
- /*-------------------------------------------------------------------.
- | Whatever the start condition (but those which correspond to |
- | entities `swallowed' by Bison: SC_YACC_COMMENT, SC_ESCAPED_STRING, |
- | and SC_ESCAPED_CHARACTER), no M4 character must escape as is. |
- `-------------------------------------------------------------------*/
-
-<SC_COMMENT,SC_LINE_COMMENT,SC_STRING,SC_CHARACTER,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
-{
- \[ obstack_sgrow (&string_obstack, "@<:@");
- \] obstack_sgrow (&string_obstack, "@:>@");
-}
-
-
/*---------------------------------------------------------------.
| Scanning a Yacc comment. The initial `/ *' is already eaten. |
`---------------------------------------------------------------*/
BEGIN INITIAL;
}
- [^*]+|"*" ;
-
- <<EOF>> {
- complain_at (*yylloc, _("unexpected end of file in a comment"));
- BEGIN INITIAL;
- }
+ .|\n ;
+ <<EOF>> unexpected_end_of_file (yylloc, "*/");
}
<SC_COMMENT>
{
- "*"{splice}"/" YY_OBS_GROW; yy_pop_state ();
- [^*\[\]]+|"*" YY_OBS_GROW;
-
- <<EOF>> {
- complain_at (*yylloc, _("unexpected end of file in a comment"));
- yy_pop_state ();
- }
+ "*"{splice}"/" YY_OBS_GROW; BEGIN c_context;
+ <<EOF>> unexpected_end_of_file (yylloc, "*/");
}
<SC_LINE_COMMENT>
{
- "\n" YY_OBS_GROW; yy_pop_state ();
- ([^\n\[\]]|{splice})+ YY_OBS_GROW;
- <<EOF>> yy_pop_state ();
+ "\n" YY_OBS_GROW; BEGIN c_context;
+ {splice} YY_OBS_GROW;
+ <<EOF>> BEGIN c_context;
}
<SC_ESCAPED_STRING>
{
"\"" {
- assert (yy_top_state () == INITIAL);
YY_OBS_GROW;
YY_OBS_FINISH;
yylval->string = last_string;
- yy_pop_state ();
rule_length++;
+ BEGIN INITIAL;
return STRING;
}
- [^\"\\]+ YY_OBS_GROW;
-
- <<EOF>> {
- complain_at (*yylloc, _("unexpected end of file in a string"));
- assert (yy_top_state () == INITIAL);
- YY_OBS_FINISH;
- yylval->string = last_string;
- yy_pop_state ();
- return STRING;
- }
+ .|\n YY_OBS_GROW;
+ <<EOF>> unexpected_end_of_file (yylloc, "\"");
}
/*---------------------------------------------------------------.
{
"'" {
YY_OBS_GROW;
- assert (yy_top_state () == INITIAL);
- {
- YY_OBS_FINISH;
- yylval->symbol = symbol_get (last_string, *yylloc);
- symbol_class_set (yylval->symbol, token_sym, *yylloc);
- symbol_user_token_number_set (yylval->symbol,
- (unsigned char) last_string[1], *yylloc);
- YY_OBS_FREE;
- yy_pop_state ();
- rule_length++;
- return ID;
- }
- }
-
- [^\'\\]+ YY_OBS_GROW;
-
- <<EOF>> {
- complain_at (*yylloc, _("unexpected end of file in a character"));
- assert (yy_top_state () == INITIAL);
YY_OBS_FINISH;
- yylval->string = last_string;
- yy_pop_state ();
- return CHARACTER;
+ yylval->symbol = symbol_get (last_string, *yylloc);
+ symbol_class_set (yylval->symbol, token_sym, *yylloc);
+ symbol_user_token_number_set (yylval->symbol,
+ (unsigned char) last_string[1], *yylloc);
+ YY_OBS_FREE;
+ rule_length++;
+ BEGIN INITIAL;
+ return ID;
}
+
+ .|\n YY_OBS_GROW;
+ <<EOF>> unexpected_end_of_file (yylloc, "'");
}
unsigned long c = strtoul (yytext + 1, 0, 8);
if (UCHAR_MAX < c)
{
- complain_at (*yylloc, _("invalid escape: %s"), quote (yytext));
+ complain_at (*yylloc, _("invalid escape sequence: %s"),
+ quote (yytext));
YY_STEP;
}
else
c = strtoul (yytext + 2, 0, 16);
if (UCHAR_MAX < c || errno)
{
- complain_at (*yylloc, _("invalid escape: %s"), quote (yytext));
+ complain_at (*yylloc, _("invalid escape sequence: %s"),
+ quote (yytext));
YY_STEP;
}
else
\\r obstack_1grow (&string_obstack, '\r');
\\t obstack_1grow (&string_obstack, '\t');
\\v obstack_1grow (&string_obstack, '\v');
- \\[\"\'?\\] obstack_1grow (&string_obstack, yytext[1]);
+
+ /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */
+ \\("\""|"'"|"?"|"\\") obstack_1grow (&string_obstack, yytext[1]);
+
\\(u|U[0-9a-fA-F]{4})[0-9a-fA-F]{4} {
int c = convert_ucn_to_byte (yytext);
if (c < 0)
{
- complain_at (*yylloc, _("invalid escape: %s"), quote (yytext));
+ complain_at (*yylloc, _("invalid escape sequence: %s"),
+ quote (yytext));
YY_STEP;
}
else
obstack_1grow (&string_obstack, c);
}
\\(.|\n) {
- complain_at (*yylloc, _("unrecognized escape: %s"), quote (yytext));
+ complain_at (*yylloc, _("unrecognized escape sequence: %s"),
+ quote (yytext));
YY_OBS_GROW;
}
- /* FLex wants this rule, in case of a `\<<EOF>>'. */
- \\ YY_OBS_GROW;
}
<SC_CHARACTER>
{
- "'" {
- YY_OBS_GROW;
- assert (yy_top_state () != INITIAL);
- yy_pop_state ();
- }
-
- [^'\[\]\\]+ YY_OBS_GROW;
- \\{splice}[^\[\]] YY_OBS_GROW;
- {splice} YY_OBS_GROW;
- /* Needed for `\<<EOF>>', `\\<<newline>>[', and `\\<<newline>>]'. */
- \\ YY_OBS_GROW;
-
- <<EOF>> {
- complain_at (*yylloc, _("unexpected end of file in a character"));
- assert (yy_top_state () != INITIAL);
- yy_pop_state ();
- }
+ "'" YY_OBS_GROW; BEGIN c_context;
+ \\{splice}[^$@\[\]] YY_OBS_GROW;
+ <<EOF>> unexpected_end_of_file (yylloc, "'");
}
<SC_STRING>
{
- "\"" {
- assert (yy_top_state () != INITIAL);
- YY_OBS_GROW;
- yy_pop_state ();
- }
-
- [^\"\[\]\\]+ YY_OBS_GROW;
- \\{splice}[^\[\]] YY_OBS_GROW;
- {splice} YY_OBS_GROW;
- /* Needed for `\<<EOF>>', `\\<<newline>>[', and `\\<<newline>>]'. */
- \\ YY_OBS_GROW;
-
- <<EOF>> {
- complain_at (*yylloc, _("unexpected end of file in a string"));
- assert (yy_top_state () != INITIAL);
- yy_pop_state ();
- }
+ "\"" YY_OBS_GROW; BEGIN c_context;
+ \\{splice}[^$@\[\]] YY_OBS_GROW;
+ <<EOF>> unexpected_end_of_file (yylloc, "\"");
}
<SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
{
- /* Characters. We don't check there is only one. */
- "'" YY_OBS_GROW; yy_push_state (SC_CHARACTER);
-
- /* Strings. */
- "\"" YY_OBS_GROW; yy_push_state (SC_STRING);
-
- /* Comments. */
- "/"{splice}"*" YY_OBS_GROW; yy_push_state (SC_COMMENT);
- "/"{splice}"/" YY_OBS_GROW; yy_push_state (SC_LINE_COMMENT);
-
- /* Not comments. */
- "/" YY_OBS_GROW;
+ "'" YY_OBS_GROW; c_context = YY_START; BEGIN SC_CHARACTER;
+ "\"" YY_OBS_GROW; c_context = YY_START; BEGIN SC_STRING;
+ "/"{splice}"*" YY_OBS_GROW; c_context = YY_START; BEGIN SC_COMMENT;
+ "/"{splice}"/" YY_OBS_GROW; c_context = YY_START; BEGIN SC_LINE_COMMENT;
}
<SC_BRACED_CODE>
{
+ "{"|"<"{splice}"%" YY_OBS_GROW; braces_level++;
+ "%"{splice}">" YY_OBS_GROW; braces_level--;
"}" {
YY_OBS_GROW;
- if (--braces_level == 0)
+ braces_level--;
+ if (braces_level < 0)
{
- yy_pop_state ();
YY_OBS_FINISH;
yylval->string = last_string;
rule_length++;
+ BEGIN INITIAL;
return BRACED_CODE;
}
}
- "{" YY_OBS_GROW; braces_level++;
+ /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
+ (as `<' `<%'). */
+ "<"{splice}"<" YY_OBS_GROW;
"$"("<"{tag}">")?(-?[0-9]+|"$") { handle_dollar (current_braced_code,
yytext, *yylloc); }
"@"(-?[0-9]+|"$") { handle_at (current_braced_code,
yytext, *yylloc); }
- [^$@\[\]/\'\"\{\}]+ YY_OBS_GROW;
-
- /* A stray $, or /, or etc. */
- . YY_OBS_GROW;
-
- <<EOF>> {
- complain_at (*yylloc, _("unexpected end of file in a braced code"));
- yy_pop_state ();
- YY_OBS_FINISH;
- yylval->string = last_string;
- return BRACED_CODE;
- }
-
+ <<EOF>> unexpected_end_of_file (yylloc, "}");
}
<SC_PROLOGUE>
{
"%}" {
- yy_pop_state ();
YY_OBS_FINISH;
yylval->string = last_string;
+ BEGIN INITIAL;
return PROLOGUE;
}
- [^%\[\]/\'\"]+ YY_OBS_GROW;
- "%" YY_OBS_GROW;
-
- <<EOF>> {
- complain_at (*yylloc, _("unexpected end of file in a prologue"));
- yy_pop_state ();
- YY_OBS_FINISH;
- yylval->string = last_string;
- return PROLOGUE;
- }
+ <<EOF>> unexpected_end_of_file (yylloc, "%}");
}
<SC_EPILOGUE>
{
- [^\[\]]+ YY_OBS_GROW;
-
<<EOF>> {
- yy_pop_state ();
YY_OBS_FINISH;
yylval->string = last_string;
+ BEGIN INITIAL;
return EPILOGUE;
}
}
+ /*----------------------------------------------------------------.
+ | By default, grow the string obstack with the input, escaping M4 |
+ | quoting characters. |
+ `----------------------------------------------------------------*/
+
+<SC_COMMENT,SC_LINE_COMMENT,SC_STRING,SC_CHARACTER,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
+{
+ \$ obstack_sgrow (&string_obstack, "$][");
+ \@ obstack_sgrow (&string_obstack, "@@");
+ \[ obstack_sgrow (&string_obstack, "@{");
+ \] obstack_sgrow (&string_obstack, "@}");
+ .|\n YY_OBS_GROW;
+}
+
+
%%
/*------------------------------------------------------------------.
rule_length, n, type_name);
}
else
- complain_at (location, _("invalid value: %s"), quote (text));
+ complain_at (location, _("integer out of range: %s"), quote (text));
}
}
rule_length, n);
}
else
- complain_at (location, _("invalid value: %s"), quote (text));
+ complain_at (location, _("integer out of range: %s"), quote (text));
}
}
/* A non-ASCII host. Use CODE to index into a table of the C
basic execution character set, which is guaranteed to exist on
all Standard C platforms. This table also includes '$', '@',
- and '`', which not in the basic execution character set but
+ and '`', which are not in the basic execution character set but
which are unibyte characters on all the platforms that we know
about. */
static signed char const table[] =
}
+/*----------------------------------------------------------------.
+| Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
+`----------------------------------------------------------------*/
+
+static void
+handle_syncline (char *args, location_t *location)
+{
+ int lineno = strtol (args, &args, 10);
+ const char *file = NULL;
+ file = strchr (args, '"') + 1;
+ *strchr (file, '"') = 0;
+ current_file = xstrdup (file);
+ location->file = current_file;
+ location->last_line = lineno;
+}
+
+
+/*-------------------------------------------------------------.
+| Report an unexpected end of file at LOC. An end of file was |
+| encountered and the expected TOKEN_END was missing. After |
+| reporting the problem, pretend that TOKEN_END was found. |
+`-------------------------------------------------------------*/
+
+static void
+unexpected_end_of_file (location_t *loc, char const *token_end)
+{
+ size_t i = strlen (token_end);
+
+ complain_at (*loc, _("missing `%s' at end of file"), token_end);
+
+ /* Adjust location's last column so that any later message does not
+ mention the characters just inserted. */
+ loc->last_column -= i;
+
+ while (i != 0)
+ unput (token_end[--i]);
+}
+
+
/*-------------------------.
| Initialize the scanner. |
`-------------------------*/