Skip to content

Commit 7f4176a

Browse files
committed
Support lexing with Prism
1 parent a7d6991 commit 7f4176a

6 files changed

Lines changed: 53 additions & 19 deletions

File tree

.github/workflows/ci.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,22 @@ jobs:
4343
- name: test
4444
run: bin/rake test
4545
continue-on-error: ${{ matrix.ruby == 'head' }}
46+
47+
test-disable-prism:
48+
needs: ruby-versions
49+
runs-on: ubuntu-latest
50+
strategy:
51+
fail-fast: false
52+
matrix:
53+
ruby: ${{ fromJson(needs.ruby-versions.outputs.versions) }}
54+
steps:
55+
- name: Checkout code
56+
uses: actions/checkout@v4.1.1
57+
- name: Set up Ruby
58+
uses: ruby/setup-ruby@v1
59+
with:
60+
ruby-version: ${{ matrix.ruby }}
61+
bundler-cache: true
62+
- name: test
63+
run: SYNTAX_SUGGEST_DISABLE_PRISM=1 bin/rake test
64+
continue-on-error: ${{ matrix.ruby == 'head' }}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## HEAD (unreleased)
22

3+
- Support prism parser (https://github.com/ruby/syntax_suggest/pull/208).
34
- No longer supports EOL versions of Ruby. (https://github.com/ruby/syntax_suggest/pull/210)
45
- Handle Ruby 3.3 new eval source location format (https://github.com/ruby/syntax_suggest/pull/200).
56

lib/syntax_suggest/api.rb

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,27 @@
55
require "tmpdir"
66
require "stringio"
77
require "pathname"
8+
require "timeout"
89

9-
# rubocop:disable Style/IdenticalConditionalBranches
10-
if ENV["SYNTAX_SUGGEST_DISABLE_PRISM"] # For testing dual ripper/prism support
11-
require "ripper"
10+
# We need Ripper loaded for `Prism.lex_compat` even if we're using Prism
11+
# for lexing and parsing
12+
require "ripper"
13+
14+
# Prism is the new parser, replacing Ripper
15+
#
16+
# We need to "dual boot" both for now because syntax_suggest
17+
# supports older rubies that do not ship with syntax suggest.
18+
#
19+
# We also need the ability to control loading of this library
20+
# so we can test that both modes work correctly in CI.
21+
if (value = ENV["SYNTAX_SUGGEST_DISABLE_PRISM"])
22+
warn "Skipping loading prism due to SYNTAX_SUGGEST_DISABLE_PRISM=#{value}"
1223
else
13-
# TODO remove require
14-
# Allow both to be loaded to enable more atomic commits
15-
require "ripper"
1624
begin
1725
require "prism"
1826
rescue LoadError
19-
require "ripper"
2027
end
2128
end
22-
# rubocop:enable Style/IdenticalConditionalBranches
23-
24-
require "timeout"
2529

2630
module SyntaxSuggest
2731
# Used to indicate a default value that cannot

lib/syntax_suggest/code_line.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,19 @@ def ignore_newline_not_beg?
180180
# EOM
181181
# expect(lines.first.trailing_slash?).to eq(true)
182182
#
183-
def trailing_slash?
184-
last = @lex.last
185-
return false unless last
186-
return false unless last.type == :on_sp
183+
if SyntaxSuggest.use_prism_parser?
184+
def trailing_slash?
185+
last = @lex.last
186+
last&.type == :on_tstring_end
187+
end
188+
else
189+
def trailing_slash?
190+
last = @lex.last
191+
return false unless last
192+
return false unless last.type == :on_sp
187193

188-
last.token == TRAILING_SLASH
194+
last.token == TRAILING_SLASH
195+
end
189196
end
190197

191198
# Endless method detection

lib/syntax_suggest/lex_all.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,15 @@ def initialize(source:, source_lines: nil)
3232
}
3333
end
3434

35-
# rubocop:disable Style/IdenticalConditionalBranches
3635
if SyntaxSuggest.use_prism_parser?
3736
def self.lex(source, line_number)
38-
# Prism.lex_compat(source, line: line_number).value.sort_by {|values| values[0] }
39-
Ripper::Lexer.new(source, "-", line_number).parse.sort_by(&:pos)
37+
Prism.lex_compat(source, line: line_number).value.sort_by { |values| values[0] }
4038
end
4139
else
4240
def self.lex(source, line_number)
4341
Ripper::Lexer.new(source, "-", line_number).parse.sort_by(&:pos)
4442
end
4543
end
46-
# rubocop:enable Style/IdenticalConditionalBranches
4744

4845
def to_a
4946
@lex

spec/unit/api_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88

99
module SyntaxSuggest
1010
RSpec.describe "Top level SyntaxSuggest api" do
11+
it "doesn't load prism if env var is set" do
12+
skip("SYNTAX_SUGGEST_DISABLE_PRISM not set") unless ENV["SYNTAX_SUGGEST_DISABLE_PRISM"]
13+
14+
expect(SyntaxSuggest.use_prism_parser?).to be_falsey
15+
end
16+
1117
it "has a `handle_error` interface" do
1218
fake_error = Object.new
1319
def fake_error.message

0 commit comments

Comments
 (0)