Skip to content

Commit 1161649

Browse files
committed
preserve the indentation in the ouput
Preserve the actions' initial indentation. For instance, on | %define api.value.type {int} | %% | exp: exp '/' exp { if ($3) | $$ = $1 + $3; | else | $$ = 0; } we used to generate | { if (yyvsp[0]) | yyval = yyvsp[-2] + yyvsp[0]; | else | yyval = 0; } now we produce | { if (yyvsp[0]) | yyval = yyvsp[-2] + yyvsp[0]; | else | yyval = 0; } See https://lists.gnu.org/archive/html/bison-patches/2019-06/msg00012.html. * data/skeletons/bison.m4 (b4_symbol_action): Output the code in column 0, leave indentation matters to the C code. * src/output.c (user_actions_output): Preserve the incoming indentation in the output. (prepare_symbol_definitions): Likewise for %printer/%destructor. * tests/synclines.at (Output columns): New.
1 parent 13577a8 commit 1161649

File tree

3 files changed

+84
-9
lines changed

3 files changed

+84
-9
lines changed

data/skeletons/bison.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ m4_define([b4_symbol_action],
449449
[(*yylocationp)])dnl
450450
_b4_symbol_case([$1])[]dnl
451451
b4_syncline([b4_symbol([$1], [$2_line])], [b4_symbol([$1], [$2_file])])dnl
452-
b4_symbol([$1], [$2])
452+
b4_symbol([$1], [$2])
453453
b4_syncline([@oline@], [@ofile@])dnl
454454
break;
455455

src/output.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,9 @@ symbol_numbers_output (FILE *out)
359359
}
360360

361361

362-
/*---------------------------------.
363-
| Output the user actions to OUT. |
364-
`---------------------------------*/
362+
/*-------------------------------------------.
363+
| Output the user reduction actions to OUT. |
364+
`-------------------------------------------*/
365365

366366
static void
367367
user_actions_output (FILE *out)
@@ -370,11 +370,19 @@ user_actions_output (FILE *out)
370370
for (rule_number r = 0; r < nrules; ++r)
371371
if (rules[r].action)
372372
{
373-
fprintf (out, "%s(%d, [b4_syncline(%d, ",
373+
fprintf (out, "%s(%d, [",
374374
rules[r].is_predicate ? "b4_predicate_case" : "b4_case",
375-
r + 1, rules[r].action_loc.start.line);
376-
string_output (out, rules[r].action_loc.start.file);
377-
fprintf (out, ")dnl\n[ %s]])\n\n", rules[r].action);
375+
r + 1);
376+
if (!no_lines_flag)
377+
{
378+
fprintf (out, "b4_syncline(%d, ",
379+
rules[r].action_loc.start.line);
380+
string_output (out, rules[r].action_loc.start.file);
381+
fprintf (out, ")dnl\n");
382+
}
383+
fprintf (out, "[%*s%s]])\n\n",
384+
rules[r].action_loc.start.column - 1, "",
385+
rules[r].action);
378386
}
379387
fputs ("])\n\n", out);
380388
}
@@ -482,7 +490,9 @@ prepare_symbol_definitions (void)
482490
muscle_location_grow (key, p->location);
483491

484492
SET_KEY (pname);
485-
MUSCLE_INSERT_STRING_RAW (key, p->code);
493+
obstack_printf (&muscle_obstack,
494+
"%*s%s", p->location.start.column - 1, "", p->code);
495+
muscle_insert (key, obstack_finish0 (&muscle_obstack));
486496
}
487497
}
488498
#undef SET_KEY2

tests/synclines.at

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,3 +497,68 @@ AT_CLEANUP
497497
m4_map_args([AT_TEST], [yacc.c], [glr.c], [lalr1.cc], [glr.cc])
498498

499499
m4_popdef([AT_TEST])
500+
501+
502+
503+
## ---------------- ##
504+
## Output columns. ##
505+
## ---------------- ##
506+
507+
AT_SETUP([Output columns])
508+
509+
# This test is fragile: its point is to check the compiler's error
510+
# message, but it seems too hard to do portability (even between
511+
# version of GCC). So instead, let's just check the generated code
512+
# itself.
513+
514+
AT_BISON_OPTION_PUSHDEFS
515+
AT_DATA([input.y],
516+
[[%{
517+
]AT_YYERROR_DECLARE_EXTERN[
518+
]AT_YYLEX_DECLARE_EXTERN[
519+
%}
520+
%define api.value.type union
521+
%type <int> '0' exp
522+
%destructor { /* --BEGIN */
523+
destructor
524+
/* --END */ } <*>
525+
%printer { /* --BEGIN */
526+
printer
527+
/* --END */ } <*>
528+
529+
530+
531+
%left '+'
532+
%%
533+
exp: exp '+' exp { /* --BEGIN */
534+
$$ = $1 + $3;
535+
@$ = @1 + @3;
536+
/* --END */ }
537+
| '0'
538+
]])
539+
540+
AT_BISON_CHECK([-o input.c input.y])
541+
AT_CHECK([[sed -ne '/--BEGIN/,/--END/{' \
542+
-e '/input.c/s/ [0-9]* / LINE /;' \
543+
-e 'p;}' \
544+
input.c]], 0,
545+
[[ { /* --BEGIN */
546+
printer
547+
/* --END */ }
548+
{ /* --BEGIN */
549+
printer
550+
/* --END */ }
551+
{ /* --BEGIN */
552+
destructor
553+
/* --END */ }
554+
{ /* --BEGIN */
555+
destructor
556+
/* --END */ }
557+
{ /* --BEGIN */
558+
(yyval.exp) = (yyvsp[-2].exp) + (yyvsp[0].exp);
559+
(yyloc) = (yylsp[-2]) + (yylsp[0]);
560+
/* --END */ }
561+
]])
562+
563+
AT_BISON_OPTION_POPDEFS
564+
AT_CLEANUP

0 commit comments

Comments
 (0)