Skip to content

Commit

Permalink
Merge pull request #389 from yui-knk/TypedMidruleActions
Browse files Browse the repository at this point in the history
Typed Midrule Actions
  • Loading branch information
yui-knk authored Mar 24, 2024
2 parents 992ca95 + c4bc0d2 commit 7ca6f30
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 77 deletions.
40 changes: 40 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
# NEWS for Lrama

## Lrama 0.6.5 (2024-03-xx)

### Typed Midrule Actions

User can specify the type of mid rule action by tag (`<bar>`) instead of specifying it with in an action.

```
primary: k_case expr_value terms?
{
$<val>$ = p->case_labels;
p->case_labels = Qnil;
}
case_body
k_end
{
...
}
```

can be written as

```
primary: k_case expr_value terms?
{
$$ = p->case_labels;
p->case_labels = Qnil;
}<val>
case_body
k_end
{
...
}
```

`%destructor` for midrule action is invoked only when tag is specified by Typed Midrule Actions.

Difference from Bison's Typed Midrule Actions is that tag is postposed in Lrama however it's preposed in Bison.

Bison supports this feature from 3.1.

## Lrama 0.6.4 (2024-03-22)

### Parameterizing rules (preceded, terminated, delimited)
Expand Down
3 changes: 2 additions & 1 deletion lib/lrama/grammar/rule_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ def process_rhs(parameterizing_rule_resolver)
end
when Lrama::Lexer::Token::UserCode
prefix = token.referred ? "@" : "$@"
tag = token.tag || lhs_tag
new_token = Lrama::Lexer::Token::Ident.new(s_value: prefix + @midrule_action_counter.increment.to_s)
@replaced_rhs << new_token

rule_builder = RuleBuilder.new(@rule_counter, @midrule_action_counter, i, lhs_tag: lhs_tag, skip_preprocess_references: true)
rule_builder = RuleBuilder.new(@rule_counter, @midrule_action_counter, i, lhs_tag: tag, skip_preprocess_references: true)
rule_builder.lhs = new_token
rule_builder.user_code = token
rule_builder.complete_input
Expand Down
2 changes: 2 additions & 0 deletions lib/lrama/lexer/token/user_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Lrama
class Lexer
class Token
class UserCode < Token
attr_accessor :tag

def references
@references ||= _references
end
Expand Down
133 changes: 66 additions & 67 deletions lib/lrama/parser.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,11 @@ rule
{
end_c_declaration
}
"}" named_ref_opt
"}" named_ref_opt tag_opt
{
user_code = val[3]
user_code.alias_name = val[6]
user_code.tag = val[7]
builder = val[0]
builder.user_code = user_code
result = builder
Expand Down
1 change: 1 addition & 0 deletions sig/lrama/lexer/token/user_code.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Lrama
class Lexer
class Token
class UserCode < Token
attr_accessor tag: Lexer::Token::Tag
@references: Array[Lrama::Grammar::Reference]

def references: () -> Array[Lrama::Grammar::Reference]
Expand Down
Loading

0 comments on commit 7ca6f30

Please sign in to comment.