Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide functionalities for Bring Your Own Stack #367

Merged
merged 1 commit into from
Feb 14, 2024

Conversation

yui-knk
Copy link
Collaborator

@yui-knk yui-knk commented Feb 4, 2024

Ruby’s Ripper library requires their own semantic value stack to manage Ruby Objects returned by user defined callback method. Currently Ripper uses semantic value stack (yyvsa) which is used by parser to manage Node. This hack introduces some limitation on Ripper.
For example, Ripper can not execute semantic analysis depending on Node structure.

Lrama introduces two features to support another semantic value stack by parser generator users.

  1. Callback entry points

User can emulate semantic value stack by these callbacks. Lrama provides these five callbacks. Registered functions are called when each event happen. For example %after-shift function is called when shift happens on original semantic value stack.

  • %after-shift function_name
  • %before-reduce function_name
  • %after-reduce function_name
  • %after-shift-error-token function_name
  • %after-pop-stack function_name
  1. $:n variable to access index of each grammar symbols

User also needs to access semantic value of their stack in grammar action. $:n provides the way to access to it. $:n is translated to the minus index from the top of the stack. For example

primary: k_if expr_value then compstmt if_tail k_end
          {
          /*% ripper: if!($:2, $:4, $:5) %*/
          /* $:2 = -5, $:4 = -3, $:5 = -2. */
          }

Ruby’s Ripper library requires their own semantic value stack to manage
Ruby Objects returned by user defined callback method.
Currently Ripper uses semantic value stack (`yyvsa`) which is used by parser to manage Node.
This hack introduces some limitation on Ripper.
For example, Ripper can not execute semantic analysis depending on Node structure.

Lrama introduces two features to support another semantic value stack by parser generator users.

1. Callback entry points

User can emulate semantic value stack by these callbacks.
Lrama provides these five callbacks. Registered functions are called when each event happen.
For example %after-shift function is called when shift happens on original semantic value stack.

* `%after-shift` function_name
* `%before-reduce` function_name
* `%after-reduce` function_name
* `%after-shift-error-token` function_name
* `%after-pop-stack` function_name

2. `$:n` variable to access index of each grammar symbols

User also needs to access semantic value of their stack in grammar action.
`$:n` provides the way to access to it. `$:n` is translated to the minus index from the top of the stack.
For example

```
primary: k_if expr_value then compstmt if_tail k_end
          {
          /*% ripper: if!($:2, $:4, $:5) %*/
          /* $:2 = -5, $:4 = -3, $:5 = -2. */
          }
```
@yui-knk yui-knk merged commit 08a77ac into ruby:master Feb 14, 2024
17 checks passed
@yui-knk yui-knk deleted the after_shift branch February 14, 2024 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant