Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/lrama/grammar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ class Grammar
attr_accessor :expect #: Integer
attr_accessor :printers #: Array[Printer]
attr_accessor :error_tokens #: Array[ErrorToken]
attr_accessor :lex_param #: String
attr_accessor :parse_param #: String
attr_reader :lex_params #: Array[String]
attr_reader :parse_params #: Array[String]
attr_accessor :initial_action #: Grammar::Code::InitialActionCode
attr_accessor :after_shift #: Lexer::Token::Base
attr_accessor :before_reduce #: Lexer::Token::Base
Expand Down Expand Up @@ -119,6 +119,8 @@ def initialize(rule_counter, midrule_action_counter, locations, define = {})
@printers = []
@destructors = []
@error_tokens = []
@lex_params = []
@parse_params = []
@symbols_resolver = Grammar::Symbols::Resolver.new
@types = []
@rule_builders = []
Expand Down Expand Up @@ -332,13 +334,19 @@ def _evaluate_nodes
self.epilogue = node.code.s_value
when Node::RequireDecl
@required = true
when Node::ParamDecl
node.params.each do |param|
code = Grammar::Code::NoReferenceCode.new(type: :param, token_code: param).token_code.s_value
lex_params << code
parse_params << code
end
when Node::LexParamDecl
node.params.each do |param|
self.lex_param = Grammar::Code::NoReferenceCode.new(type: :lex_param, token_code: param).token_code.s_value
lex_params << Grammar::Code::NoReferenceCode.new(type: :lex_param, token_code: param).token_code.s_value
end
when Node::ParseParamDecl
node.params.each do |param|
self.parse_param = Grammar::Code::NoReferenceCode.new(type: :parse_param, token_code: param).token_code.s_value
parse_params << Grammar::Code::NoReferenceCode.new(type: :parse_param, token_code: param).token_code.s_value
end
when Node::UnionDecl
self.set_union(
Expand Down
1 change: 1 addition & 0 deletions lib/lrama/grammar/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
require_relative "node/locations_decl"
require_relative "node/no_stdlib_decl"
require_relative "node/nterm_decl"
require_relative "node/param_decl"
require_relative "node/parameterized_rule"
require_relative "node/parse_param_decl"
require_relative "node/precedence_decl"
Expand Down
18 changes: 18 additions & 0 deletions lib/lrama/grammar/node/param_decl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# rbs_inline: enabled
# frozen_string_literal: true

module Lrama
class Grammar
module Node
class ParamDecl < Base
attr_reader :params #: Array[Lexer::Token::UserCode]

# @rbs (params: Array[Lexer::Token::UserCode], location: Lexer::Location) -> void
def initialize(params:, location:)
@params = params
super(location: location)
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/lrama/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Lexer
%require
%printer
%destructor
%param
%lex-param
%parse-param
%initial-action
Expand Down
36 changes: 9 additions & 27 deletions lib/lrama/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,24 +265,16 @@ def omit_blanks(param)

# b4_parse_param
def parse_param
if @grammar.parse_param
omit_blanks(@grammar.parse_param)
else
""
end
@grammar.parse_params.map { |param| omit_blanks(param) }.join(", ")
end

def lex_param
if @grammar.lex_param
omit_blanks(@grammar.lex_param)
else
""
end
@grammar.lex_params.map { |param| omit_blanks(param) }.join(", ")
end

# b4_user_formals
def user_formals
if @grammar.parse_param
if @grammar.parse_params.any?
", #{parse_param}"
else
""
Expand All @@ -291,7 +283,7 @@ def user_formals

# b4_user_args
def user_args
if @grammar.parse_param
if @grammar.parse_params.any?
", #{parse_param_name}"
else
""
Expand All @@ -303,19 +295,11 @@ def extract_param_name(param)
end

def parse_param_name
if @grammar.parse_param
extract_param_name(parse_param)
else
""
end
@grammar.parse_params.map { |param| extract_param_name(param) }.join(", ")
end

def lex_param_name
if @grammar.lex_param
extract_param_name(lex_param)
else
""
end
@grammar.lex_params.map { |param| extract_param_name(param) }.join(", ")
end

# b4_parse_param_use
Expand All @@ -325,9 +309,7 @@ def parse_param_use(val, loc)
YY_USE (#{loc});
STR

if @grammar.parse_param
str << " YY_USE (#{parse_param_name});"
end
str << @grammar.parse_params.map { |param| " YY_USE (#{extract_param_name(param)});" }.join("\n")

str
end
Expand All @@ -337,7 +319,7 @@ def yylex_formals
ary = ["&yylval"]
ary << "&yylloc" if @grammar.locations

if @grammar.lex_param
if @grammar.lex_params.any?
ary << lex_param_name
end

Expand All @@ -357,7 +339,7 @@ def table_value_equals(table, value, literal, symbol)
def yyerror_args
ary = ["&yylloc"]

if @grammar.parse_param
if @grammar.parse_params.any?
ary << parse_param_name
end

Expand Down
Loading
Loading