Skip to content

Commit d68e4be

Browse files
committed
lrama pre-0.8.0
master as of 2026-03-01. ruby/lrama@e8dbb06
1 parent 45dce19 commit d68e4be

File tree

6 files changed

+53
-31
lines changed

6 files changed

+53
-31
lines changed

tool/lrama/NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# NEWS for Lrama
22

3+
## Lrama 0.8.0 (2026-xx-xx)
4+
35
## Lrama 0.7.1 (2025-12-24)
46

57
### Optimize IELR

tool/lrama/lib/lrama/grammar/binding.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def concatenated_args_str(token)
3030
def create_instantiate_rule(sym)
3131
Lrama::Lexer::Token::InstantiateRule.new(
3232
s_value: sym.s_value,
33+
alias_name: sym.alias_name,
3334
location: sym.location,
3435
args: resolve_args(sym.args),
3536
lhs_tag: sym.lhs_tag

tool/lrama/lib/lrama/grammar/code/rule_action.rb

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ class RuleAction < Code
1111
#
1212
# @rbs!
1313
# @rule: Rule
14+
# @grammar: Grammar
1415

15-
# @rbs (type: ::Symbol, token_code: Lexer::Token::UserCode, rule: Rule) -> void
16-
def initialize(type:, token_code:, rule:)
16+
# @rbs (type: ::Symbol, token_code: Lexer::Token::UserCode, rule: Rule, grammar: Grammar) -> void
17+
def initialize(type:, token_code:, rule:, grammar:)
1718
super(type: type, token_code: token_code)
1819
@rule = rule
20+
@grammar = grammar
1921
end
2022

2123
private
@@ -53,19 +55,31 @@ def reference_to_c(ref)
5355
case
5456
when ref.type == :dollar && ref.name == "$" # $$
5557
tag = ref.ex_tag || lhs.tag
56-
raise_tag_not_found_error(ref) unless tag
57-
# @type var tag: Lexer::Token::Tag
58-
"(yyval.#{tag.member})"
58+
if tag
59+
# @type var tag: Lexer::Token::Tag
60+
"(yyval.#{tag.member})"
61+
elsif union_not_defined?
62+
# When %union is not defined, YYSTYPE defaults to int
63+
"(yyval)"
64+
else
65+
raise_tag_not_found_error(ref)
66+
end
5967
when ref.type == :at && ref.name == "$" # @$
6068
"(yyloc)"
6169
when ref.type == :index && ref.name == "$" # $:$
6270
raise "$:$ is not supported"
6371
when ref.type == :dollar # $n
6472
i = -position_in_rhs + ref.index
6573
tag = ref.ex_tag || rhs[ref.index - 1].tag
66-
raise_tag_not_found_error(ref) unless tag
67-
# @type var tag: Lexer::Token::Tag
68-
"(yyvsp[#{i}].#{tag.member})"
74+
if tag
75+
# @type var tag: Lexer::Token::Tag
76+
"(yyvsp[#{i}].#{tag.member})"
77+
elsif union_not_defined?
78+
# When %union is not defined, YYSTYPE defaults to int
79+
"(yyvsp[#{i}])"
80+
else
81+
raise_tag_not_found_error(ref)
82+
end
6983
when ref.type == :at # @n
7084
i = -position_in_rhs + ref.index
7185
"(yylsp[#{i}])"
@@ -99,6 +113,11 @@ def lhs
99113
@rule.lhs
100114
end
101115

116+
# @rbs () -> bool
117+
def union_not_defined?
118+
@grammar.union.nil?
119+
end
120+
102121
# @rbs (Reference ref) -> bot
103122
def raise_tag_not_found_error(ref)
104123
raise "Tag is not specified for '$#{ref.value}' in '#{@rule.display_name}'"

tool/lrama/lib/lrama/grammar/rule.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ def initial_rule?
104104
id == 0
105105
end
106106

107-
# @rbs () -> String?
108-
def translated_code
107+
# @rbs (Grammar grammar) -> String?
108+
def translated_code(grammar)
109109
return nil unless token_code
110110

111-
Code::RuleAction.new(type: :rule_action, token_code: token_code, rule: self).translated_code
111+
Code::RuleAction.new(type: :rule_action, token_code: token_code, rule: self, grammar: grammar).translated_code
112112
end
113113

114114
# @rbs () -> bool

tool/lrama/lib/lrama/output.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def user_actions
246246
<<-STR
247247
case #{rule.id + 1}: /* #{rule.as_comment} */
248248
#line #{code.line} "#{@grammar_file_path}"
249-
#{spaces}{#{rule.translated_code}}
249+
#{spaces}{#{rule.translated_code(@grammar)}}
250250
#line [@oline@] [@ofile@]
251251
break;
252252

tool/lrama/lib/lrama/parser.rb

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -767,14 +767,14 @@ def raise_parse_error(error_message, location)
767767
105, 53, 53, 52, 52, 110, 110, 53, 53, 52,
768768
209, 110, 110, 53, 53, 209, 52, 110, 110, 53,
769769
53, 209, 52, 110, 193, 194, 195, 137, 216, 222,
770-
229, 217, 217, 217, 53, 53, 52, 52, 193, 194,
770+
230, 217, 217, 217, 53, 53, 52, 52, 193, 194,
771771
195, 57, 57, 57, 57, 66, 67, 68, 69, 70,
772772
72, 72, 72, 86, 89, 47, 57, 57, 113, 117,
773773
117, 79, 123, 124, 131, 47, 133, 137, 139, 143,
774774
149, 150, 151, 152, 133, 155, 156, 157, 110, 166,
775775
149, 169, 172, 173, 72, 175, 176, 183, 189, 166,
776776
196, 137, 200, 202, 137, 166, 211, 166, 137, 72,
777-
176, 218, 176, 72, 72, 227, 137, 72 ]
777+
176, 218, 176, 72, 137, 227, 137, 72, 72 ]
778778

779779
racc_action_check = [
780780
51, 97, 51, 97, 41, 75, 165, 75, 165, 75,
@@ -806,7 +806,7 @@ def raise_parse_error(error_message, location)
806806
124, 125, 126, 127, 133, 136, 137, 138, 144, 150,
807807
151, 153, 156, 158, 162, 163, 164, 170, 174, 176,
808808
178, 179, 182, 184, 187, 189, 199, 200, 204, 205,
809-
207, 209, 212, 214, 216, 221, 222, 228 ]
809+
207, 209, 212, 214, 216, 221, 222, 224, 228 ]
810810

811811
racc_action_pointer = [
812812
32, 23, 52, 93, nil, 31, 63, nil, 123, 68,
@@ -830,16 +830,16 @@ def raise_parse_error(error_message, location)
830830
189, nil, 236, nil, 239, nil, 162, 229, 194, 235,
831831
10, nil, nil, nil, nil, nil, 195, nil, nil, 284,
832832
237, 15, 200, nil, 233, 281, nil, 241, 173, 247,
833-
176, nil, 243, 174, 285, nil, 286, 201, 206, nil,
834-
nil, 278, 241, nil, nil, nil, 175, nil, 289, nil,
835-
nil ]
833+
176, nil, 243, 174, 285, nil, 239, 201, 206, nil,
834+
nil, 278, 241, nil, 289, nil, 175, nil, 290, nil,
835+
nil, nil ]
836836

837837
racc_action_default = [
838838
-1, -136, -1, -3, -10, -136, -136, -2, -3, -136,
839839
-14, -14, -136, -136, -136, -136, -136, -136, -136, -28,
840840
-29, -34, -35, -36, -136, -136, -136, -136, -136, -136,
841841
-136, -136, -136, -54, -54, -54, -136, -136, -136, -136,
842-
-136, -136, -136, -13, 231, -4, -136, -14, -16, -17,
842+
-136, -136, -136, -13, 232, -4, -136, -14, -16, -17,
843843
-20, -131, -100, -101, -130, -18, -23, -89, -24, -25,
844844
-136, -27, -37, -136, -136, -136, -41, -42, -43, -44,
845845
-45, -46, -55, -136, -47, -136, -48, -49, -92, -136,
@@ -856,9 +856,9 @@ def raise_parse_error(error_message, location)
856856
-136, -124, -136, -109, -100, -110, -72, -72, -136, -70,
857857
-69, -75, -76, -116, -117, -118, -136, -78, -79, -136,
858858
-70, -108, -136, -111, -72, -54, -115, -63, -136, -100,
859-
-119, -125, -65, -136, -54, -114, -54, -136, -136, -120,
860-
-121, -136, -72, -112, -77, -122, -136, -126, -54, -123,
861-
-113 ]
859+
-119, -125, -65, -136, -54, -114, -72, -136, -136, -120,
860+
-121, -136, -72, -112, -54, -122, -136, -126, -54, -77,
861+
-123, -113 ]
862862

863863
racc_goto_table = [
864864
73, 118, 136, 54, 48, 49, 164, 96, 91, 120,
@@ -873,14 +873,14 @@ def raise_parse_error(error_message, location)
873873
170, 177, 220, 199, 203, 205, 221, 186, 153, nil,
874874
nil, nil, nil, 116, 116, nil, 198, nil, nil, nil,
875875
nil, nil, 214, 78, 206, nil, 177, nil, nil, nil,
876-
nil, nil, 210, nil, nil, nil, nil, 186, 210, 174,
876+
nil, nil, 210, nil, 224, nil, nil, 186, 210, 174,
877877
228, nil, nil, nil, nil, nil, nil, nil, nil, nil,
878878
nil, nil, nil, 225, 210, nil, nil, nil, nil, nil,
879879
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
880880
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
881881
nil, nil, 215, nil, nil, nil, nil, nil, nil, nil,
882-
nil, 223, nil, 224, nil, nil, nil, nil, nil, nil,
883-
nil, nil, nil, nil, nil, 230 ]
882+
nil, 223, nil, nil, nil, nil, nil, nil, nil, nil,
883+
nil, 229, nil, nil, nil, 231 ]
884884

885885
racc_goto_check = [
886886
29, 22, 42, 31, 14, 14, 35, 16, 8, 48,
@@ -895,14 +895,14 @@ def raise_parse_error(error_message, location)
895895
53, 22, 55, 56, 42, 42, 57, 22, 58, nil,
896896
nil, nil, nil, 31, 31, nil, 22, nil, nil, nil,
897897
nil, nil, 42, 31, 22, nil, 22, nil, nil, nil,
898-
nil, nil, 22, nil, nil, nil, nil, 22, 22, 29,
898+
nil, nil, 22, nil, 42, nil, nil, 22, 22, 29,
899899
42, nil, nil, nil, nil, nil, nil, nil, nil, nil,
900900
nil, nil, nil, 22, 22, nil, nil, nil, nil, nil,
901901
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
902902
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
903903
nil, nil, 29, nil, nil, nil, nil, nil, nil, nil,
904-
nil, 29, nil, 29, nil, nil, nil, nil, nil, nil,
905-
nil, nil, nil, nil, nil, 29 ]
904+
nil, 29, nil, nil, nil, nil, nil, nil, nil, nil,
905+
nil, 29, nil, nil, nil, 29 ]
906906

907907
racc_goto_pointer = [
908908
nil, 38, nil, nil, nil, -52, 23, 68, -38, -29,
@@ -998,7 +998,7 @@ def raise_parse_error(error_message, location)
998998
1, 94, :_reduce_74,
999999
3, 94, :_reduce_75,
10001000
3, 94, :_reduce_76,
1001-
6, 94, :_reduce_77,
1001+
7, 94, :_reduce_77,
10021002
3, 94, :_reduce_78,
10031003
3, 94, :_reduce_79,
10041004
0, 102, :_reduce_none,
@@ -1060,7 +1060,7 @@ def raise_parse_error(error_message, location)
10601060

10611061
racc_reduce_n = 136
10621062

1063-
racc_shift_n = 231
1063+
racc_shift_n = 232
10641064

10651065
racc_token_table = {
10661066
false => 0,
@@ -1828,7 +1828,7 @@ def _reduce_76(val, _values, result)
18281828
def _reduce_77(val, _values, result)
18291829
on_action_error("intermediate %prec in a rule", val[1]) if @trailing_prec_seen
18301830
builder = val[0]
1831-
builder.symbols << Lrama::Lexer::Token::InstantiateRule.new(s_value: val[1].s_value, location: @lexer.location, args: val[3], lhs_tag: val[5])
1831+
builder.symbols << Lrama::Lexer::Token::InstantiateRule.new(s_value: val[1].s_value, alias_name: val[5], location: @lexer.location, args: val[3], lhs_tag: val[6])
18321832
result = builder
18331833
18341834
result

0 commit comments

Comments
 (0)