Skip to content

Commit cb0fc45

Browse files
authored
Add more syntax highlighting to README (#937)
1 parent e3813de commit cb0fc45

File tree

1 file changed

+48
-40
lines changed

1 file changed

+48
-40
lines changed

README.md

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,55 +22,63 @@ MacRuby and RubyMotion support sponsored by [CodeClimate](http://codeclimate.com
2222
Load Parser (see the [backwards compatibility](#backwards-compatibility) section
2323
below for explanation of `emit_*` calls):
2424

25-
require 'parser/current'
26-
# opt-in to most recent AST format:
27-
Parser::Builders::Default.emit_lambda = true
28-
Parser::Builders::Default.emit_procarg0 = true
29-
Parser::Builders::Default.emit_encoding = true
30-
Parser::Builders::Default.emit_index = true
31-
Parser::Builders::Default.emit_arg_inside_procarg0 = true
32-
Parser::Builders::Default.emit_forward_arg = true
33-
Parser::Builders::Default.emit_kwargs = true
34-
Parser::Builders::Default.emit_match_pattern = true
25+
```ruby
26+
require 'parser/current'
27+
# opt-in to most recent AST format:
28+
Parser::Builders::Default.emit_lambda = true
29+
Parser::Builders::Default.emit_procarg0 = true
30+
Parser::Builders::Default.emit_encoding = true
31+
Parser::Builders::Default.emit_index = true
32+
Parser::Builders::Default.emit_arg_inside_procarg0 = true
33+
Parser::Builders::Default.emit_forward_arg = true
34+
Parser::Builders::Default.emit_kwargs = true
35+
Parser::Builders::Default.emit_match_pattern = true
36+
```
3537

3638
Parse a chunk of code:
3739

38-
p Parser::CurrentRuby.parse("2 + 2")
39-
# (send
40-
# (int 2) :+
41-
# (int 2))
40+
```ruby
41+
p Parser::CurrentRuby.parse("2 + 2")
42+
# (send
43+
# (int 2) :+
44+
# (int 2))
45+
```
4246

4347
Access the AST's source map:
4448

45-
p Parser::CurrentRuby.parse("2 + 2").loc
46-
# #<Parser::Source::Map::Send:0x007fe5a1ac2388
47-
# @dot=nil,
48-
# @begin=nil,
49-
# @end=nil,
50-
# @selector=#<Source::Range (string) 2...3>,
51-
# @expression=#<Source::Range (string) 0...5>>
52-
53-
p Parser::CurrentRuby.parse("2 + 2").loc.selector.source
54-
# "+"
49+
```ruby
50+
p Parser::CurrentRuby.parse("2 + 2").loc
51+
# #<Parser::Source::Map::Send:0x007fe5a1ac2388
52+
# @dot=nil,
53+
# @begin=nil,
54+
# @end=nil,
55+
# @selector=#<Source::Range (string) 2...3>,
56+
# @expression=#<Source::Range (string) 0...5>>
57+
58+
p Parser::CurrentRuby.parse("2 + 2").loc.selector.source
59+
# "+"
60+
```
5561

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

5864
Parse a chunk of code and display all diagnostics:
5965

60-
parser = Parser::CurrentRuby.new
61-
parser.diagnostics.consumer = lambda do |diag|
62-
puts diag.render
63-
end
64-
65-
buffer = Parser::Source::Buffer.new('(string)', source: "foo *bar")
66-
67-
p parser.parse(buffer)
68-
# (string):1:5: warning: `*' interpreted as argument prefix
69-
# foo *bar
70-
# ^
71-
# (send nil :foo
72-
# (splat
73-
# (send nil :bar)))
66+
```ruby
67+
parser = Parser::CurrentRuby.new
68+
parser.diagnostics.consumer = lambda do |diag|
69+
puts diag.render
70+
end
71+
72+
buffer = Parser::Source::Buffer.new('(string)', source: "foo *bar")
73+
74+
p parser.parse(buffer)
75+
# (string):1:5: warning: `*' interpreted as argument prefix
76+
# foo *bar
77+
# ^
78+
# (send nil :foo
79+
# (splat
80+
# (send nil :bar)))
81+
```
7482

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

139147
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:
140148

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

163171
and so on.
164172

165-
```
173+
```bash
166174
$ ruby-parse -e '(foo; bar)'
167175
(begin
168176
(send nil :foo)

0 commit comments

Comments
 (0)