mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-11 05:13:04 +00:00
Extract the parsing of user actions from the grammar scanner.
As a consequence, the relation between the grammar scanner and
parser is much simpler. We can also split "composite tokens" back
into simple tokens.
* src/gram.h (ITEM_NUMBER_MAX, RULE_NUMBER_MAX): New.
* src/scan-gram.l (add_column_width, adjust_location): Move to and
rename as...
* src/location.h, src/location.c (add_column_width)
(location_compute): these.
Fix the column count: the initial column is 0.
(location_print): Be robust to ending column being 0.
* src/location.h (boundary_set): New.
* src/main.c: Adjust to scanner_free being renamed as
gram_scanner_free.
* src/output.c: Include scan-code.h.
* src/parse-gram.y: Include scan-gram.h and scan-code.h.
Use boundary_set.
(PERCENT_DESTRUCTOR, PERCENT_PRINTER, PERCENT_INITIAL_ACTION)
(PERCENT_LEX_PARAM, PERCENT_PARSE_PARAM): Remove the {...} part,
which is now, again, a separate token.
Adjust all dependencies.
Whereever actions with $ and @ are used, use translate_code.
(action): Remove this nonterminal which is now useless.
* src/reader.c: Include assert.h, scan-gram.h and scan-code.h.
(grammar_current_rule_action_append): Use translate_code.
(packgram): Bound check ruleno, itemno, and rule_length.
* src/reader.h (gram_in, gram__flex_debug, scanner_cursor)
(last_string, last_braced_code_loc, max_left_semantic_context)
(scanner_initialize, scanner_free, scanner_last_string_free)
(gram_out, gram_lineno, YY_DECL_): Move to...
* src/scan-gram.h: this new file.
(YY_DECL): Rename as...
(GRAM_DECL): this.
* src/scan-code.h, src/scan-code.l, src/scan-code-c.c: New.
* src/scan-gram.l (gram_get_lineno, gram_get_in, gram_get_out):
(gram_get_leng, gram_get_text, gram_set_lineno, gram_set_in):
(gram_set_out, gram_get_debug, gram_set_debug, gram_lex_destroy):
Move these declarations, and...
(obstack_for_string, STRING_GROW, STRING_FINISH, STRING_FREE):
these to...
* src/flex-scanner.h: this new file.
* src/scan-gram.l (rule_length, rule_length_overflow)
(increment_rule_length): Remove.
(last_braced_code_loc): Rename as...
(gram_last_braced_code_loc): this.
Adjust to the changes of the parser.
Move all the handling of $ and @ into...
* src/scan-code.l: here.
* src/scan-gram.l (handle_dollar, handle_at): Remove.
(handle_action_dollar, handle_action_at): Move to...
* src/scan-code.l: here.
* src/Makefile.am (bison_SOURCES): Add flex-scanner.h,
scan-code.h, scan-code-c.c, scan-gram.h.
(EXTRA_bison_SOURCES): Add scan-code.l.
(BUILT_SOURCES): Add scan-code.c.
(yacc): Be robust to white spaces.
* tests/conflicts.at, tests/input.at, tests/reduce.at,
* tests/regression.at: Adjust the column numbers.
* tests/regression.at: Adjust the error message.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
/* Locations for Bison
|
||||
|
||||
Copyright (C) 2002, 2005 Free Software Foundation, Inc.
|
||||
Copyright (C) 2002, 2005, 2006 Free Software Foundation, Inc.
|
||||
|
||||
This file is part of Bison, the GNU Compiler Compiler.
|
||||
|
||||
@@ -28,11 +27,80 @@
|
||||
|
||||
location const empty_location;
|
||||
|
||||
/* If BUF is null, add BUFSIZE (which in this case must be less than
|
||||
INT_MAX) to COLUMN; otherwise, add mbsnwidth (BUF, BUFSIZE, 0) to
|
||||
COLUMN. If an overflow occurs, or might occur but is undetectable,
|
||||
return INT_MAX. Assume COLUMN is nonnegative. */
|
||||
|
||||
static inline int
|
||||
add_column_width (int column, char const *buf, size_t bufsize)
|
||||
{
|
||||
size_t width;
|
||||
unsigned int remaining_columns = INT_MAX - column;
|
||||
|
||||
if (buf)
|
||||
{
|
||||
if (INT_MAX / 2 <= bufsize)
|
||||
return INT_MAX;
|
||||
width = mbsnwidth (buf, bufsize, 0);
|
||||
}
|
||||
else
|
||||
width = bufsize;
|
||||
|
||||
return width <= remaining_columns ? column + width : INT_MAX;
|
||||
}
|
||||
|
||||
/* Set *LOC and adjust scanner cursor to account for token TOKEN of
|
||||
size SIZE. */
|
||||
|
||||
void
|
||||
location_compute (location *loc, boundary *cur, char const *token, size_t size)
|
||||
{
|
||||
int line = cur->line;
|
||||
int column = cur->column;
|
||||
char const *p0 = token;
|
||||
char const *p = token;
|
||||
char const *lim = token + size;
|
||||
|
||||
loc->start = *cur;
|
||||
|
||||
for (p = token; p < lim; p++)
|
||||
switch (*p)
|
||||
{
|
||||
case '\n':
|
||||
line += line < INT_MAX;
|
||||
column = 1;
|
||||
p0 = p + 1;
|
||||
break;
|
||||
|
||||
case '\t':
|
||||
column = add_column_width (column, p0, p - p0);
|
||||
column = add_column_width (column, NULL, 8 - ((column - 1) & 7));
|
||||
p0 = p + 1;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
cur->line = line;
|
||||
cur->column = column = add_column_width (column, p0, p - p0);
|
||||
|
||||
loc->end = *cur;
|
||||
|
||||
if (line == INT_MAX && loc->start.line != INT_MAX)
|
||||
warn_at (*loc, _("line number overflow"));
|
||||
if (column == INT_MAX && loc->start.column != INT_MAX)
|
||||
warn_at (*loc, _("column number overflow"));
|
||||
}
|
||||
|
||||
|
||||
/* Output to OUT the location LOC.
|
||||
Warning: it uses quotearg's slot 3. */
|
||||
void
|
||||
location_print (FILE *out, location loc)
|
||||
{
|
||||
int end_col = 0 < loc.end.column ? loc.end.column - 1 : 0;
|
||||
fprintf (out, "%s:%d.%d",
|
||||
quotearg_n_style (3, escape_quoting_style, loc.start.file),
|
||||
loc.start.line, loc.start.column);
|
||||
@@ -40,9 +108,9 @@ location_print (FILE *out, location loc)
|
||||
if (loc.start.file != loc.end.file)
|
||||
fprintf (out, "-%s:%d.%d",
|
||||
quotearg_n_style (3, escape_quoting_style, loc.end.file),
|
||||
loc.end.line, loc.end.column - 1);
|
||||
loc.end.line, end_col);
|
||||
else if (loc.start.line < loc.end.line)
|
||||
fprintf (out, "-%d.%d", loc.end.line, loc.end.column - 1);
|
||||
else if (loc.start.column < loc.end.column - 1)
|
||||
fprintf (out, "-%d", loc.end.column - 1);
|
||||
fprintf (out, "-%d.%d", loc.end.line, end_col);
|
||||
else if (loc.start.column < end_col)
|
||||
fprintf (out, "-%d", end_col);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user