Skip to content

Commit

Permalink
Add %after-shift-error-token
Browse files Browse the repository at this point in the history
  • Loading branch information
yui-knk committed Jan 31, 2024
1 parent fd861b6 commit 540da2d
Show file tree
Hide file tree
Showing 8 changed files with 495 additions and 457 deletions.
2 changes: 1 addition & 1 deletion lib/lrama/grammar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Grammar
attr_accessor :union, :expect,
:printers, :error_tokens,
:lex_param, :parse_param, :initial_action,
:after_shift, :before_reduce, :after_reduce, :after_pop_stack,
:after_shift, :before_reduce, :after_reduce, :after_shift_error_token, :after_pop_stack,
:symbols, :types,
:rules, :rule_builders,
:sym_to_rules, :no_stdlib
Expand Down
1 change: 1 addition & 0 deletions lib/lrama/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Lexer
%error-token
%before-reduce
%after-reduce
%after-shift-error-token
%after-shift
%after-pop-stack
%empty
Expand Down
11 changes: 11 additions & 0 deletions lib/lrama/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ def after_reduce_function(comment = "")
STR
end

def after_shift_error_token_function(comment = "")
return "" unless @grammar.after_shift_error_token

<<-STR
#{comment}
#line #{@grammar.after_shift_error_token.line} "#{@grammar_file_path}"
{#{@grammar.after_shift_error_token.s_value}(#{parse_param_name});}
#line [@oline@] [@ofile@]
STR
end

def after_pop_stack_function(len, comment = "")
return "" unless @grammar.after_pop_stack

Expand Down
915 changes: 463 additions & 452 deletions lib/lrama/parser.rb

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ rule
{
@grammar.after_reduce = val[1]
}
| "%after-shift-error-token" IDENTIFIER
{
@grammar.after_shift_error_token = val[1]
}
| "%after-pop-stack" IDENTIFIER
{
@grammar.after_pop_stack = val[1]
Expand Down
7 changes: 7 additions & 0 deletions spec/fixtures/integration/after_shift.y
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ after_reduce(int len)
printf("after-reduce: %d, %d\n", __LINE__, len);
}

static void
after_shift_error_token(void)
{
printf("after-shift-error-token: %d\n", __LINE__);
}

static void
after_pop_stack(int len)
{
Expand All @@ -39,6 +45,7 @@ after_pop_stack(int len)
%after-shift after_shift
%before-reduce before_reduce
%after-reduce after_reduce
%after-shift-error-token after_shift_error_token
%after-pop-stack after_pop_stack

%token <val> NUM
Expand Down
11 changes: 7 additions & 4 deletions spec/lrama/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,19 @@ def generate_object(grammar_file_path, c_path, obj_path, command_args: [])
before-reduce: 18, 1
after-reduce: 24, 1
after-shift: 12
after-pop-stack: 30, 1
after-pop-stack: 30, 1
after-pop-stack: 36, 1
after-pop-stack: 36, 1
after-shift-error-token: 30
before-reduce: 18, 1
error (-1)
after-reduce: 24, 1
after-pop-stack: 30, 1
after-pop-stack: 36, 1
after-shift-error-token: 30
before-reduce: 18, 1
error (-1)
after-reduce: 24, 1
after-pop-stack: 30, 1
after-pop-stack: 36, 1
after-shift-error-token: 30
before-reduce: 18, 1
error (-1)
after-reduce: 24, 1
Expand Down
1 change: 1 addition & 0 deletions template/bison/yacc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1988,6 +1988,7 @@ YYLTYPE yylloc = yyloc_default;

/* Shift the error token. */
YY_SYMBOL_PRINT ("Shifting", YY_ACCESSING_SYMBOL (yyn), yyvsp, yylsp<%= output.user_args %>);
<%= output.after_shift_error_token_function("/* %after-shift-error-token code. */") %>

yystate = yyn;
goto yynewstate;
Expand Down

0 comments on commit 540da2d

Please sign in to comment.