Skip to content

Commit a37a989

Browse files
authored
Merge pull request #3508 from Earlopain/parser-current-ruby
2 parents 640b1e7 + 77177f9 commit a37a989

5 files changed

Lines changed: 41 additions & 3 deletions

File tree

docs/parser_translation.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,23 @@ Prism ships with the ability to translate its syntax tree into the syntax tree u
66

77
The `parser` gem provides multiple parsers to support different versions of the Ruby grammar. This includes all of the Ruby versions going back to 1.8, as well as third-party parsers like MacRuby and RubyMotion. The `prism` gem provides another parser that uses the `prism` parser to build the syntax tree.
88

9-
You can use the `prism` parser like you would any other. After requiring the parser, you should be able to call any of the regular `Parser::Base` APIs that you would normally use.
9+
You can use the `prism` parser like you would any other. After requiring `prism`, you should be able to call any of the regular `Parser::Base` APIs that you would normally use.
1010

1111
```ruby
1212
require "prism"
1313

14-
Prism::Translation::Parser.parse_file("path/to/file.rb")
14+
# Same as `Parser::Ruby34`
15+
Prism::Translation::Parser34.parse_file("path/to/file.rb")
16+
17+
# Same as `Parser::CurrentRuby`
18+
Prism::Translation::ParserCurrent.parse("puts 'Hello World!'")
1519
```
1620

21+
All the parsers are autoloaded, so you don't have to worry about requiring them yourself.
22+
1723
### RuboCop
1824

19-
Prism as a parser engine is directly supported since RuboCop 1.62. The class used for parsing is `Prism::Translation::Parser`.
25+
Prism as a parser engine is directly supported since RuboCop 1.62.
2026

2127
First, specify `prism` in your Gemfile:
2228

lib/prism/translation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Prism
55
# syntax trees.
66
module Translation # steep:ignore
77
autoload :Parser, "prism/translation/parser"
8+
autoload :ParserCurrent, "prism/translation/parser_current"
89
autoload :Parser33, "prism/translation/parser33"
910
autoload :Parser34, "prism/translation/parser34"
1011
autoload :Parser35, "prism/translation/parser35"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
# typed: ignore
3+
4+
module Prism
5+
module Translation
6+
case RUBY_VERSION
7+
when /^3\.3\./
8+
ParserCurrent = Parser33
9+
when /^3\.4\./
10+
ParserCurrent = Parser34
11+
when /^3\.5\./
12+
ParserCurrent = Parser35
13+
else
14+
# Keep this in sync with released Ruby.
15+
parser = Parser34
16+
warn "warning: `Prism::Translation::Current` is loading #{parser.name}, " \
17+
"but you are running #{RUBY_VERSION.to_f}."
18+
ParserCurrent = parser
19+
end
20+
end
21+
end

prism.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Gem::Specification.new do |spec|
9696
"lib/prism/string_query.rb",
9797
"lib/prism/translation.rb",
9898
"lib/prism/translation/parser.rb",
99+
"lib/prism/translation/parser_current.rb",
99100
"lib/prism/translation/parser33.rb",
100101
"lib/prism/translation/parser34.rb",
101102
"lib/prism/translation/parser35.rb",

test/prism/ruby/parser_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,15 @@ def test_non_prism_builder_class_deprecated
155155
assert_empty(warnings)
156156
end
157157

158+
if RUBY_VERSION >= "3.3"
159+
def test_current_parser_for_current_ruby
160+
major, minor, _patch = Gem::Version.new(RUBY_VERSION).segments
161+
# Let's just hope there never is a Ruby 3.10 or similar
162+
expected = major * 10 + minor
163+
assert_equal(expected, Translation::ParserCurrent.new.version)
164+
end
165+
end
166+
158167
def test_it_block_parameter_syntax
159168
it_fixture_path = Pathname(__dir__).join("../../../test/prism/fixtures/it.txt")
160169

0 commit comments

Comments
 (0)