Skip to content

Commit 889e046

Browse files
committed
Change to not use native parser backend anymore
1 parent 48a39a6 commit 889e046

File tree

5 files changed

+38
-54
lines changed

5 files changed

+38
-54
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ unparser
44
![CI](https://github.com/mbj/unparser/workflows/CI/badge.svg)
55
[![Gem Version](https://img.shields.io/gem/v/unparser.svg)](https://rubygems.org/gems/unparser)
66

7-
Generate equivalent source for ASTs from [parser](https://github.com/whitequark/parser).
7+
Generate equivalent source for ASTs from [prism](https://github.com/ruby/prism) via its parser translation layer.
8+
9+
**Parser Backend**: Unparser uses [Prism](https://github.com/ruby/prism) (Ruby's official parser) with its [parser translation layer](https://github.com/ruby/prism/blob/main/docs/parser_translation.md) to produce `Parser::AST::Node` compatible ASTs. This requires both the `prism` and `parser` gems as dependencies.
810

911
The following constraints apply:
1012

@@ -30,7 +32,6 @@ Usage
3032
-----
3133

3234
```ruby
33-
require 'parser/current'
3435
require 'unparser'
3536

3637
ast = Unparser.parse('your(ruby(code))')
@@ -41,7 +42,6 @@ Unparser.unparse(ast) # => 'your(ruby(code))'
4142
To preserve the comments from the source:
4243

4344
```ruby
44-
require 'parser/current'
4545
require 'unparser'
4646

4747
ast, comments = Unparser.parser.parse_with_comments(Unparser.buffer('your(ruby(code)) # with comments'))

bin/parser-round-trip-test

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#!/usr/bin/env ruby
22
# frozen_string_literal: true
33

4-
if Gem::Version.new(RUBY_VERSION) <= '3.4'
5-
load 'bin/parser-whitequark-round-trip-test'
6-
else
7-
load 'bin/parser-prism-round-trip-test'
8-
end
4+
# Unparser now uses Prism translation layer for all Ruby 3.3+ versions
5+
load 'bin/parser-prism-round-trip-test'

lib/unparser.rb

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,52 +18,41 @@
1818

1919
# Library namespace
2020
module Unparser # rubocop:disable Metrics/ModuleLength
21-
# Unparser specific AST builder defaulting to modern AST format
22-
if Gem::Version.new(RUBY_VERSION) <= '3.4'
23-
require 'parser/current'
24-
class Builder < Parser::Builders::Default
25-
modernize
26-
27-
# mutant:disable
28-
def initialize
29-
super
21+
require 'prism'
3022

31-
self.emit_file_line_as_literals = false
32-
end
33-
end
34-
else
35-
require 'prism'
36-
class Builder < Prism::Translation::Parser::Builder
37-
modernize
23+
# Unparser specific AST builder defaulting to modern AST format
24+
class Builder < Prism::Translation::Parser::Builder
25+
modernize
3826

39-
# mutant:disable
40-
def initialize
41-
super
27+
# mutant:disable
28+
def initialize
29+
super
4230

43-
self.emit_file_line_as_literals = false
44-
end
31+
self.emit_file_line_as_literals = false
4532
end
4633
end
4734

35+
# Select appropriate Prism translation parser based on Ruby version
4836
PARSER_CLASS =
49-
if Gem::Version.new(RUBY_VERSION) <= '3.4'
50-
Class.new(Parser::CurrentRuby) do
51-
def declare_local_variable(local_variable)
52-
static_env.declare(local_variable)
53-
end
54-
end
37+
if RUBY_VERSION >= '3.5'
38+
Prism::Translation::Parser35
39+
elsif RUBY_VERSION >= '3.4'
40+
Prism::Translation::Parser34
5541
else
56-
Class.new(Prism::Translation::Parser34) do
57-
def declare_local_variable(local_variable)
58-
(@local_variables ||= Set.new) << local_variable
59-
end
60-
61-
def prism_options
62-
super.merge(scopes: [@local_variables.to_a])
63-
end
64-
end
42+
Prism::Translation::Parser33
43+
end
44+
45+
# Create parser instance with local variable declaration support
46+
PARSER_INSTANCE_CLASS = Class.new(PARSER_CLASS) do
47+
def declare_local_variable(local_variable)
48+
(@local_variables ||= Set.new) << local_variable
6549
end
6650

51+
def prism_options
52+
super.merge(scopes: [@local_variables.to_a])
53+
end
54+
end
55+
6756
EMPTY_STRING = ''.freeze
6857
EMPTY_ARRAY = [].freeze
6958

@@ -238,7 +227,7 @@ def self.parse_ast(source, static_local_variables: Set.new)
238227
# @api private
239228
# mutant:disable
240229
def self.parser
241-
PARSER_CLASS.new(Builder.new).tap do |parser|
230+
PARSER_INSTANCE_CLASS.new(Builder.new).tap do |parser|
242231
parser.diagnostics.tap do |diagnostics|
243232
diagnostics.all_errors_are_fatal = true
244233
end

spec/spec_helper.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
require 'mutant'
55
require 'yaml'
66

7-
require 'parser/current'
8-
97
RSpec.shared_examples_for 'a command method' do
108
it 'returns self' do
119
should equal(object)

spec/unit/unparser_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -459,13 +459,13 @@ 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
462+
# "before/34.rb" contains syntax valid before Ruby 3.4 but invalid in 3.4+
463+
# So we need to exclude it on ALL versions (3.3 can't parse it, 3.4+ rejects it)
464+
excludes.concat(
465+
%w[
466+
test/corpus/literal/before/34.rb
467+
]
468+
)
469469

470470
excludes.flat_map { |file| ['--ignore', file] }
471471
end

0 commit comments

Comments
 (0)