From cd48d21d944380129d7a2ce68617ea31bf939aab Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Sat, 8 Jul 2006 20:38:14 +0000 Subject: [PATCH] * data/c.m4 (b4_location_initial_column, b4_location_initial_line): New, default to 1. * data/yacc.c, data/glr.c, data/location.cc: Use them. * NEWS, doc/bison.texinfo: The initial column and line are 1 by default. * tests/calc.at: Adjust. --- ChangeLog | 9 +++++++++ NEWS | 3 +++ data/c.m4 | 5 +++++ data/glr.c | 4 ++-- data/location.cc | 16 ++++++---------- data/yacc.c | 4 ++-- doc/bison.texinfo | 7 ++++++- tests/calc.at | 30 +++++++++++++++--------------- 8 files changed, 48 insertions(+), 30 deletions(-) diff --git a/ChangeLog b/ChangeLog index b2aeb56d..81f0478d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2006-07-08 Akim Demaille + + * data/c.m4 (b4_location_initial_column, b4_location_initial_line): + New, default to 1. + * data/yacc.c, data/glr.c, data/location.cc: Use them. + * NEWS, doc/bison.texinfo: The initial column and line are 1 by + default. + * tests/calc.at: Adjust. + 2006-07-08 Akim Demaille * data/c.m4 (b4_dirname): New. diff --git a/NEWS b/NEWS index dd36e633..0b6b2341 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,9 @@ Bison News Changes in version 2.3+: +* Locations columns and lines start at 1. + In accordance with the GNU Coding Standards and Emacs. + * Except for LALR(1) parsers in C with POSIX Yacc emulation enabled (with `-y', `--yacc', or `%yacc'), Bison no longer generates #define statements for associating token numbers with token names. Removing the #define statements diff --git a/data/c.m4 b/data/c.m4 index 4757c58c..33b4ddda 100644 --- a/data/c.m4 +++ b/data/c.m4 @@ -91,6 +91,11 @@ m4_define_default([b4_epilogue], []) # If the %union is not named, its name is YYSTYPE. m4_define_default([b4_union_name], [YYSTYPE]) +# The initial column and line. +m4_define_default([b4_location_initial_column], [1]) +m4_define_default([b4_location_initial_line], [1]) + + ## ------------------------ ## ## Pure/impure interfaces. ## ## ------------------------ ## diff --git a/data/glr.c b/data/glr.c index d956a379..8da20487 100644 --- a/data/glr.c +++ b/data/glr.c @@ -2303,8 +2303,8 @@ yyrecoverSyntaxError (yyGLRStack* yystackp]b4_user_formals[) yylval = yyval_default; ]b4_locations_if([ #if YYLTYPE_IS_TRIVIAL - yylloc.first_line = yylloc.last_line = 1; - yylloc.first_column = yylloc.last_column = 0; + yylloc.first_line = yylloc.last_line = ]b4_location_initial_line[; + yylloc.first_column = yylloc.last_column = ]b4_location_initial_column[; #endif ]) m4_ifdef([b4_initial_action], [ diff --git a/data/location.cc b/data/location.cc index 156115ca..2d32bf77 100644 --- a/data/location.cc +++ b/data/location.cc @@ -36,6 +36,7 @@ b4_copyright([Positions for Bison parsers in C++], # include # include +# include namespace ]b4_namespace[ { @@ -46,7 +47,7 @@ namespace ]b4_namespace[ ]m4_ifdef([b4_location_constructors], [ /// Construct a position. position () - : filename (0), line (1), column (0) + : filename (0), line (]b4_location_initial_line[), column (]b4_location_initial_column[) { } @@ -55,8 +56,8 @@ namespace ]b4_namespace[ inline void initialize (]b4_filename_type[* fn) { filename = fn; - line = 1; - column = 0; + line = ]b4_location_initial_line[; + column = ]b4_location_initial_column[; } /** \name Line and Column related manipulators @@ -65,19 +66,14 @@ namespace ]b4_namespace[ /// (line related) Advance to the COUNT next lines. inline void lines (int count = 1) { - column = 0; + column = ]b4_location_initial_column[; line += count; } /// (column related) Advance to the COUNT next columns. inline void columns (int count = 1) { - int leftmost = 0; - int current = column; - if (leftmost <= current + count) - column += count; - else - column = 0; + column = std::max (]b4_location_initial_column[u, column + count); } /** \} */ diff --git a/data/yacc.c b/data/yacc.c index 11ea2a79..2b1cadba 100644 --- a/data/yacc.c +++ b/data/yacc.c @@ -1064,8 +1064,8 @@ b4_c_function_def([yyparse], [int], b4_parse_param) ]b4_locations_if([[ yylsp = yyls; #if YYLTYPE_IS_TRIVIAL /* Initialize the default location before parsing starts. */ - yylloc.first_line = yylloc.last_line = 1; - yylloc.first_column = yylloc.last_column = 0; + yylloc.first_line = yylloc.last_line = ]b4_location_initial_line[; + yylloc.first_column = yylloc.last_column = ]b4_location_initial_column[; #endif ]]) m4_ifdef([b4_initial_action], [ diff --git a/doc/bison.texinfo b/doc/bison.texinfo index e40788c5..0e181b86 100644 --- a/doc/bison.texinfo +++ b/doc/bison.texinfo @@ -1998,7 +1998,9 @@ type for storing locations is not needed: we will use the type provided by default (@pxref{Location Type, ,Data Types of Locations}), which is a four member structure with the following integer fields: @code{first_line}, @code{first_column}, @code{last_line} and -@code{last_column}. +@code{last_column}. By conventions, and in accordance with the GNU +Coding Standards and common practice, the line and column count both +start at 1. @node Ltcalc Rules @subsection Grammar Rules for @code{ltcalc} @@ -3512,6 +3514,9 @@ typedef struct YYLTYPE @} YYLTYPE; @end example +At the beginning of the parsing, Bison initializes all these fields to 1 +for @code{yylloc}. + @node Actions and Locations @subsection Actions and Locations @cindex location actions diff --git a/tests/calc.at b/tests/calc.at index 75010c11..0ade5ff9 100644 --- a/tests/calc.at +++ b/tests/calc.at @@ -199,7 +199,7 @@ get_char (]AT_LEX_FORMALS[) if (res == '\n') { AT_LOC.last_line++; - AT_LOC.last_column = 0; + AT_LOC.last_column = 1; } else AT_LOC.last_column++; @@ -262,7 +262,7 @@ yylex (]AT_LEX_FORMALS[) { init = 0; ]AT_LOCATION_IF([ - AT_LOC.last_column = 0; + AT_LOC.last_column = 1; AT_LOC.last_line = 1; ])[ } @@ -476,21 +476,21 @@ _AT_CHECK_CALC([$1], # Some syntax errors. _AT_CHECK_CALC_ERROR([$1], [1], [0 0], [15], - [1.2: syntax error, unexpected number]) + [1.3: syntax error, unexpected number]) _AT_CHECK_CALC_ERROR([$1], [1], [1//2], [20], - [1.2: syntax error, unexpected '/', expecting number or '-' or '(' or '!']) + [1.3: syntax error, unexpected '/', expecting number or '-' or '(' or '!']) _AT_CHECK_CALC_ERROR([$1], [1], [error], [5], - [1.0: syntax error, unexpected $undefined]) + [1.1: syntax error, unexpected $undefined]) _AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [30], - [1.6: syntax error, unexpected '=']) + [1.7: syntax error, unexpected '=']) _AT_CHECK_CALC_ERROR([$1], [1], [ +1], [20], - [2.0: syntax error, unexpected '+']) + [2.1: syntax error, unexpected '+']) # Exercise error messages with EOF: work on an empty file. _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4], - [1.0: syntax error, unexpected end of input]) + [1.1: syntax error, unexpected end of input]) # Exercise the error token: without it, we die at the first error, # hence be sure to @@ -511,20 +511,20 @@ _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4], _AT_CHECK_CALC_ERROR([$1], [0], [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1], [250], -[1.1: syntax error, unexpected ')', expecting number or '-' or '(' or '!' -1.17: syntax error, unexpected ')', expecting number or '-' or '(' or '!' -1.22: syntax error, unexpected '*', expecting number or '-' or '(' or '!' -1.40: syntax error, unexpected '*', expecting number or '-' or '(' or '!' +[1.2: syntax error, unexpected ')', expecting number or '-' or '(' or '!' +1.18: syntax error, unexpected ')', expecting number or '-' or '(' or '!' +1.23: syntax error, unexpected '*', expecting number or '-' or '(' or '!' +1.41: syntax error, unexpected '*', expecting number or '-' or '(' or '!' calc: error: 4444 != 1]) # The same, but this time exercising explicitly triggered syntax errors. # POSIX says the lookahead causing the error should not be discarded. _AT_CHECK_CALC_ERROR([$1], [0], [(!) + (0 0) = 1], [102], -[1.9: syntax error, unexpected number +[1.10: syntax error, unexpected number calc: error: 2222 != 1]) _AT_CHECK_CALC_ERROR([$1], [0], [(- *) + (0 0) = 1], [113], -[1.3: syntax error, unexpected '*', expecting number or '-' or '(' or '!' -1.11: syntax error, unexpected number +[1.4: syntax error, unexpected '*', expecting number or '-' or '(' or '!' +1.12: syntax error, unexpected number calc: error: 2222 != 1]) AT_BISON_OPTION_POPDEFS -- 2.45.2