Skip to content

Commit a263dae

Browse files
committed
Add prism parser support
Resolves #385 Related Prism bug: ruby/prism#3540 (merged) `test/corpus/literal/before/34.rb` contains expressions rejected by Prism (and regexp-related bug, see #385 (comment)): ```bash $ ASDF_RUBY_VERSION=3.4.2 ruby --parser prism test/corpus/literal/before/34.rb test/corpus/literal/before/34.rb:1: syntax errors found (SyntaxError) > 1 | retry | ^~~~~ Invalid retry without rescue > 2 | in {"#{"a"}": 1} then | ^~ unexpected 'in', ignoring it | ^~~~ unexpected 'then', ignoring it | ^~~~ unexpected 'then', expecting end-of-input 3 | true 4 | /\c*a/ 5 | /\c*a\c*/ 6 | /\c*\c*\c*/ > 7 | (break foo) || a | ^~~~~~~~~ Invalid break | ^~~~~~~~~ unexpected void value expression > 8 | (return foo) || a | ^~~~~~~~~~ unexpected void value expression > 9 | a = b || break | ^~~~~ Invalid break > 10 | a = b || next | ^~~~ Invalid next > 11 | a || (break foo) | ^~~~~~~~~ Invalid break > 12 | b or break | ^~~~~ Invalid break > 13 | b or next | ^~~~ Invalid next > 14 | break or b | ^~~~~ Invalid break | ^~~~~ unexpected void value expression > 15 | next or b | ^~~~ Invalid next | ^~~~ unexpected void value expression > 16 | return or a | ^~~~~~ unexpected void value expression ``` The `prism` gem is temporary pointed to the main since `Prism::Translation::ParserCurrent` is not yet released. Let me know if we want to support prism 1.4.
1 parent 1262d12 commit a263dae

File tree

12 files changed

+69
-30
lines changed

12 files changed

+69
-30
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
strategy:
2020
fail-fast: false
2121
matrix:
22-
ruby: [ruby-3.2, ruby-3.3]
22+
ruby: [ruby-3.2, ruby-3.3, ruby-3.4]
2323
os: [ubuntu-latest]
2424
steps:
2525
- uses: actions/checkout@v4
@@ -35,7 +35,7 @@ jobs:
3535
strategy:
3636
fail-fast: false
3737
matrix:
38-
ruby: [ruby-3.2, ruby-3.3]
38+
ruby: [ruby-3.2, ruby-3.3, ruby-3.4]
3939
os: [ubuntu-latest]
4040
steps:
4141
- uses: actions/checkout@v4
@@ -53,7 +53,7 @@ jobs:
5353
strategy:
5454
fail-fast: false
5555
matrix:
56-
ruby: [ruby-3.2, ruby-3.3]
56+
ruby: [ruby-3.2, ruby-3.3, ruby-3.4]
5757
os: [ubuntu-latest]
5858
steps:
5959
- uses: actions/checkout@v4
@@ -69,7 +69,7 @@ jobs:
6969
strategy:
7070
fail-fast: false
7171
matrix:
72-
ruby: [ruby-3.2, ruby-3.3]
72+
ruby: [ruby-3.2, ruby-3.3, ruby-3.4]
7373
os: [ubuntu-latest]
7474
steps:
7575
- uses: actions/checkout@v4
@@ -85,7 +85,7 @@ jobs:
8585
strategy:
8686
fail-fast: false
8787
matrix:
88-
ruby: [ruby-3.2, ruby-3.3]
88+
ruby: [ruby-3.2, ruby-3.3, ruby-3.4]
8989
os: [ubuntu-latest]
9090
steps:
9191
- uses: actions/checkout@v4

Changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Unreleased
2+
3+
[#387](https://github.com/mbj/unparser/pull/387)
4+
5+
* Add `prism` parser support for Ruby 3.4. ([viralpraxis](https://github.com/viralpraxis))
6+
17
# v0.7.0 2025-03-16
28

39
[#366](https://github.com/mbj/unparser/pull/366)

Gemfile.lock

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
PATH
22
remote: .
33
specs:
4-
unparser (0.7.0)
4+
unparser (0.8.0)
55
diff-lcs (~> 1.6)
66
parser (>= 3.3.0)
7+
prism (>= 1.4)
78

89
GEM
910
remote: https://rubygems.org/
@@ -26,6 +27,7 @@ GEM
2627
parser (3.3.7.2)
2728
ast (~> 2.4.1)
2829
racc
30+
prism (1.4.0)
2931
racc (1.8.1)
3032
rainbow (3.1.1)
3133
regexp_parser (2.9.3)

lib/unparser.rb

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
require 'diff/lcs'
44
require 'diff/lcs/hunk'
55
require 'optparse'
6-
require 'parser/current'
76
require 'set'
87

98
require 'unparser/equalizer'
@@ -20,13 +19,27 @@
2019
# Library namespace
2120
module Unparser
2221
# Unparser specific AST builder defaulting to modern AST format
23-
class Builder < Parser::Builders::Default
24-
modernize
22+
if Gem::Version.new(RUBY_VERSION) <= '3.4'
23+
require 'parser/current'
24+
class Builder < Parser::Builders::Default
25+
modernize
2526

26-
def initialize
27-
super
27+
def initialize
28+
super
2829

29-
self.emit_file_line_as_literals = false
30+
self.emit_file_line_as_literals = false
31+
end
32+
end
33+
else
34+
require 'prism'
35+
class Builder < Prism::Translation::Parser::Builder
36+
modernize
37+
38+
def initialize
39+
super
40+
41+
self.emit_file_line_as_literals = false
42+
end
3043
end
3144
end
3245

@@ -203,7 +216,14 @@ def self.parse_ast(source, static_local_variables: Set.new)
203216
#
204217
# @api private
205218
def self.parser
206-
Parser::CurrentRuby.new(Builder.new).tap do |parser|
219+
parser_class =
220+
if Gem::Version.new(RUBY_VERSION) <= '3.4'
221+
Parser::CurrentRuby
222+
else
223+
Prism::Translation::ParserCurrent
224+
end
225+
226+
parser_class.new(Builder.new).tap do |parser|
207227
parser.diagnostics.tap do |diagnostics|
208228
diagnostics.all_errors_are_fatal = true
209229
end

lib/unparser/cli.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require 'pathname'
4+
35
module Unparser
46
# Unparser CLI implementation
57
class CLI

spec/unit/unparser_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,14 @@ def foo(bar)
459459
let(:version_excludes) do
460460
excludes = []
461461

462+
if RUBY_VERSION >= '3.4.'
463+
excludes.concat(
464+
%w[
465+
test/corpus/literal/before/34.rb
466+
]
467+
)
468+
end
469+
462470
if RUBY_VERSION < '3.2.'
463471
excludes.concat(
464472
%w[

test/corpus/literal/before/34.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
retry
2+
in {"#{"a"}": 1} then
3+
true
4+
/\c*a/
5+
/\c*a\c*/
6+
/\c*\c*\c*/
7+
(break foo) || a
8+
(return foo) || a
9+
a = b || break
10+
a = b || next
11+
a || (break foo)
12+
b or break
13+
b or next
14+
break or b
15+
next or b
16+
return or a

test/corpus/literal/binary.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
(a || b).foo
2-
(break foo) || a
3-
(return foo) || a
42
a && b && c
5-
a = b || break
6-
a = b || next
73
a = b || return
84
a and return foo
95
a or return
106
a or return foo
117
a || (b || c)
12-
a || (break foo)
138
a || (return foo)
14-
b or break
15-
b or next
16-
break or b
17-
next or b
18-
return or a
199
a = b or c
2010
a = b and c

test/corpus/literal/control.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
next
22
return
33
break
4-
retry
54
redo
65
return 1
76
return 1, 2

test/corpus/literal/pattern.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
true
1818
in {**nil} then
1919
true
20-
in {"#{"a"}": 1} then
21-
true
2220
in 1 | 2 then
2321
true
2422
in 1 => a then

0 commit comments

Comments
 (0)