Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
113 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Ripper translation | ||
|
||
Prism provides the ability to mirror the `Ripper` standard library. You can do this by: | ||
|
||
```ruby | ||
require "prism/translation/ripper/shim" | ||
``` | ||
|
||
This provides the APIs like: | ||
|
||
```ruby | ||
Ripper.lex | ||
Ripper.parse | ||
Ripper.sexp_raw | ||
Ripper.sexp | ||
|
||
Ripper::SexpBuilder | ||
Ripper::SexpBuilderPP | ||
``` | ||
|
||
Briefly, `Ripper` is a streaming parser that allows you to construct your own syntax tree. As an example: | ||
|
||
```ruby | ||
class ArithmeticRipper < Prism::Translation::Ripper | ||
def on_binary(left, operator, right) | ||
left.public_send(operator, right) | ||
end | ||
|
||
def on_int(value) | ||
value.to_i | ||
end | ||
|
||
def on_program(stmts) | ||
stmts | ||
end | ||
|
||
def on_stmts_new | ||
[] | ||
end | ||
|
||
def on_stmts_add(stmts, stmt) | ||
stmts << stmt | ||
stmts | ||
end | ||
end | ||
|
||
ArithmeticRipper.new("1 + 2 - 3").parse # => [0] | ||
``` | ||
|
||
The exact names of the `on_*` methods are listed in the `Ripper` source. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters