Skip to content

Commit

Permalink
Add more syntax highlighting to README (#937)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-tan committed Sep 14, 2023
1 parent e3813de commit cb0fc45
Showing 1 changed file with 48 additions and 40 deletions.
88 changes: 48 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,55 +22,63 @@ MacRuby and RubyMotion support sponsored by [CodeClimate](http://codeclimate.com
Load Parser (see the [backwards compatibility](#backwards-compatibility) section
below for explanation of `emit_*` calls):

require 'parser/current'
# opt-in to most recent AST format:
Parser::Builders::Default.emit_lambda = true
Parser::Builders::Default.emit_procarg0 = true
Parser::Builders::Default.emit_encoding = true
Parser::Builders::Default.emit_index = true
Parser::Builders::Default.emit_arg_inside_procarg0 = true
Parser::Builders::Default.emit_forward_arg = true
Parser::Builders::Default.emit_kwargs = true
Parser::Builders::Default.emit_match_pattern = true
```ruby
require 'parser/current'
# opt-in to most recent AST format:
Parser::Builders::Default.emit_lambda = true
Parser::Builders::Default.emit_procarg0 = true
Parser::Builders::Default.emit_encoding = true
Parser::Builders::Default.emit_index = true
Parser::Builders::Default.emit_arg_inside_procarg0 = true
Parser::Builders::Default.emit_forward_arg = true
Parser::Builders::Default.emit_kwargs = true
Parser::Builders::Default.emit_match_pattern = true
```

Parse a chunk of code:

p Parser::CurrentRuby.parse("2 + 2")
# (send
# (int 2) :+
# (int 2))
```ruby
p Parser::CurrentRuby.parse("2 + 2")
# (send
# (int 2) :+
# (int 2))
```

Access the AST's source map:

p Parser::CurrentRuby.parse("2 + 2").loc
# #<Parser::Source::Map::Send:0x007fe5a1ac2388
# @dot=nil,
# @begin=nil,
# @end=nil,
# @selector=#<Source::Range (string) 2...3>,
# @expression=#<Source::Range (string) 0...5>>

p Parser::CurrentRuby.parse("2 + 2").loc.selector.source
# "+"
```ruby
p Parser::CurrentRuby.parse("2 + 2").loc
# #<Parser::Source::Map::Send:0x007fe5a1ac2388
# @dot=nil,
# @begin=nil,
# @end=nil,
# @selector=#<Source::Range (string) 2...3>,
# @expression=#<Source::Range (string) 0...5>>

p Parser::CurrentRuby.parse("2 + 2").loc.selector.source
# "+"
```

Traverse the AST: see the documentation for [gem ast](https://whitequark.github.io/ast/).

Parse a chunk of code and display all diagnostics:

parser = Parser::CurrentRuby.new
parser.diagnostics.consumer = lambda do |diag|
puts diag.render
end

buffer = Parser::Source::Buffer.new('(string)', source: "foo *bar")

p parser.parse(buffer)
# (string):1:5: warning: `*' interpreted as argument prefix
# foo *bar
# ^
# (send nil :foo
# (splat
# (send nil :bar)))
```ruby
parser = Parser::CurrentRuby.new
parser.diagnostics.consumer = lambda do |diag|
puts diag.render
end

buffer = Parser::Source::Buffer.new('(string)', source: "foo *bar")

p parser.parse(buffer)
# (string):1:5: warning: `*' interpreted as argument prefix
# foo *bar
# ^
# (send nil :foo
# (splat
# (send nil :bar)))
```

If you reuse the same parser object for multiple `#parse` runs, you need to
`#reset` it.
Expand Down Expand Up @@ -138,7 +146,7 @@ Several Parser nodes seem to be confusing enough to warrant a dedicated README s

The `(block)` node passes a Ruby block, that is, a closure, to a method call represented by its first child, a `(send)`, `(super)` or `(zsuper)` node. To demonstrate:

```
```bash
$ ruby-parse -e 'foo { |x| x + 2 }'
(block
(send nil :foo)
Expand All @@ -162,7 +170,7 @@ Both `(begin)` and `(kwbegin)` nodes represent compound statements, that is, sev

and so on.

```
```bash
$ ruby-parse -e '(foo; bar)'
(begin
(send nil :foo)
Expand Down

0 comments on commit cb0fc45

Please sign in to comment.