Skip to content

Commit 5cf5f15

Browse files
committed
Fix up linting in ripper translation
1 parent f8b973e commit 5cf5f15

File tree

6 files changed

+61
-33
lines changed

6 files changed

+61
-33
lines changed

bin/prism

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,19 @@ module Prism
4848
require "benchmark/ips"
4949
require "parser/current"
5050
require "ruby_parser"
51+
require "ripper"
5152

5253
filepath = argv.fetch(0) { File.expand_path("../lib/prism/node.rb", __dir__) }
54+
source = File.read(filepath)
5355

5456
Benchmark.ips do |x|
55-
x.report("prism") { Prism.parse_file(filepath) }
56-
x.report("parser") { Parser::CurrentRuby.parse_file(filepath) }
57-
x.report("Prism::Translation::Parser") { Prism::Translation::Parser.parse_file(filepath) }
58-
x.report("ruby_parser") { RubyParser.new.parse(File.read(filepath), filepath) }
59-
x.report("Prism::Translation::RubyParser") { Prism::Translation::RubyParser.parse_file(filepath) }
57+
x.report("Prism") { Prism.parse(source, filepath: filepath) }
58+
x.report("Ripper::SexpBuilder") { Ripper.sexp_raw(source, filepath) }
59+
x.report("Prism::Translation::Ripper::SexpBuilder") { Prism::Translation::Ripper.sexp_raw(source, filepath) }
60+
x.report("Parser::CurrentRuby") { Parser::CurrentRuby.parse(source, filepath) }
61+
x.report("Prism::Translation::Parser") { Prism::Translation::Parser.parse(source, filepath) }
62+
x.report("RubyParser") { RubyParser.new.parse(source, filepath) }
63+
x.report("Prism::Translation::RubyParser") { Prism::Translation::RubyParser.new.parse(source, filepath) }
6064
x.compare!
6165
end
6266
end

lib/prism/translation/ripper.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2814,7 +2814,7 @@ def visit_source_file_node(node)
28142814
# ^^^^^^^^
28152815
def visit_source_line_node(node)
28162816
bounds(node.location)
2817-
on_var_ref(on_kw("__LINE__"))
2817+
on_var_ref(on_kw("__LINE__"))
28182818
end
28192819

28202820
# foo(*bar)
@@ -2943,6 +2943,7 @@ def visit_string_node(node)
29432943
end
29442944
end
29452945

2946+
# Visit a heredoc node that is representing a string.
29462947
private def visit_heredoc_string_node(node)
29472948
bounds(node.opening_loc)
29482949
on_heredoc_beg(node.opening)
@@ -2959,6 +2960,7 @@ def visit_string_node(node)
29592960
result
29602961
end
29612962

2963+
# Visit a heredoc node that is representing an xstring.
29622964
private def visit_heredoc_x_string_node(node)
29632965
bounds(node.opening_loc)
29642966
on_heredoc_beg(node.opening)

lib/prism/translation/ripper/sexp.rb

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ class Ripper
88
# This class mirrors the ::Ripper::SexpBuilder subclass of ::Ripper that
99
# returns the arrays of [type, *children].
1010
class SexpBuilder < Ripper
11+
# :stopdoc:
12+
1113
attr_reader :error
12-
14+
1315
private
1416

1517
def dedent_element(e, width)
@@ -18,7 +20,7 @@ def dedent_element(e, width)
1820
end
1921
e
2022
end
21-
23+
2224
def on_heredoc_dedent(val, width)
2325
sub = proc do |cont|
2426
cont.map! do |e|
@@ -38,7 +40,7 @@ def on_heredoc_dedent(val, width)
3840
sub[val]
3941
val
4042
end
41-
43+
4244
events = private_instance_methods(false).grep(/\Aon_/) {$'.to_sym}
4345
(PARSER_EVENTS - events).each do |event|
4446
module_eval(<<-End, __FILE__, __LINE__ + 1)
@@ -47,29 +49,33 @@ def on_#{event}(*args)
4749
end
4850
End
4951
end
50-
52+
5153
SCANNER_EVENTS.each do |event|
5254
module_eval(<<-End, __FILE__, __LINE__ + 1)
5355
def on_#{event}(tok)
5456
[:@#{event}, tok, [lineno(), column()]]
5557
end
5658
End
5759
end
58-
60+
5961
def on_error(mesg)
6062
@error = mesg
6163
end
6264
remove_method :on_parse_error
6365
alias on_parse_error on_error
6466
alias compile_error on_error
67+
68+
# :startdoc:
6569
end
6670

6771
# This class mirrors the ::Ripper::SexpBuilderPP subclass of ::Ripper that
6872
# returns the same values as ::Ripper::SexpBuilder except with a couple of
6973
# niceties that flatten linked lists into arrays.
7074
class SexpBuilderPP < SexpBuilder
75+
# :stopdoc:
76+
7177
private
72-
78+
7379
def on_heredoc_dedent(val, width)
7480
val.map! do |e|
7581
next e if Symbol === e and /_content\z/ =~ e
@@ -82,35 +88,37 @@ def on_heredoc_dedent(val, width)
8288
end
8389
val
8490
end
85-
91+
8692
def _dispatch_event_new
8793
[]
8894
end
89-
95+
9096
def _dispatch_event_push(list, item)
9197
list.push item
9298
list
9399
end
94-
100+
95101
def on_mlhs_paren(list)
96102
[:mlhs, *list]
97103
end
98-
104+
99105
def on_mlhs_add_star(list, star)
100106
list.push([:rest_param, star])
101107
end
102-
108+
103109
def on_mlhs_add_post(list, post)
104110
list.concat(post)
105111
end
106-
112+
107113
PARSER_EVENT_TABLE.each do |event, arity|
108114
if /_new\z/ =~ event and arity == 0
109115
alias_method "on_#{event}", :_dispatch_event_new
110116
elsif /_add\z/ =~ event
111117
alias_method "on_#{event}", :_dispatch_event_push
112118
end
113119
end
120+
121+
# :startdoc:
114122
end
115123
end
116124
end

lib/prism/translation/ripper/shim.rb

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

3+
# This writes the prism ripper translation into the Ripper constant so that
4+
# users can transparently use Ripper without any changes.
35
Ripper = Prism::Translation::Ripper

lib/prism/translation/ruby_parser.rb

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Prism
66
module Translation
77
# This module is the entry-point for converting a prism syntax tree into the
88
# seattlerb/ruby_parser gem's syntax tree.
9-
module RubyParser
9+
class RubyParser
1010
# A prism visitor that builds Sexp objects.
1111
class Compiler < ::Prism::Compiler
1212
# This is the name of the file that we are compiling. We set it on every
@@ -1490,31 +1490,43 @@ def visit_write_value(node)
14901490

14911491
private_constant :Compiler
14921492

1493+
# Parse the given source and translate it into the seattlerb/ruby_parser
1494+
# gem's Sexp format.
1495+
def parse(source, filepath = "(string)")
1496+
translate(Prism.parse(source), filepath)
1497+
end
1498+
1499+
# Parse the given file and translate it into the seattlerb/ruby_parser
1500+
# gem's Sexp format.
1501+
def parse_file(filepath)
1502+
translate(Prism.parse_file(filepath), filepath)
1503+
end
1504+
14931505
class << self
14941506
# Parse the given source and translate it into the seattlerb/ruby_parser
14951507
# gem's Sexp format.
14961508
def parse(source, filepath = "(string)")
1497-
translate(Prism.parse(source), filepath)
1509+
new.parse(source, filepath)
14981510
end
14991511

15001512
# Parse the given file and translate it into the seattlerb/ruby_parser
15011513
# gem's Sexp format.
15021514
def parse_file(filepath)
1503-
translate(Prism.parse_file(filepath), filepath)
1515+
new.parse_file(filepath)
15041516
end
1517+
end
15051518

1506-
private
1507-
1508-
# Translate the given parse result and filepath into the
1509-
# seattlerb/ruby_parser gem's Sexp format.
1510-
def translate(result, filepath)
1511-
if result.failure?
1512-
error = result.errors.first
1513-
raise ::RubyParser::SyntaxError, "#{filepath}:#{error.location.start_line} :: #{error.message}"
1514-
end
1519+
private
15151520

1516-
result.value.accept(Compiler.new(filepath))
1521+
# Translate the given parse result and filepath into the
1522+
# seattlerb/ruby_parser gem's Sexp format.
1523+
def translate(result, filepath)
1524+
if result.failure?
1525+
error = result.errors.first
1526+
raise ::RubyParser::SyntaxError, "#{filepath}:#{error.location.start_line} :: #{error.message}"
15171527
end
1528+
1529+
result.value.accept(Compiler.new(filepath))
15181530
end
15191531
end
15201532
end

rakelib/lint.rake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ task :lint do
4848

4949
File.foreach(filepath).with_index(1) do |line, index|
5050
if line.match?(/[ \t]+$/)
51-
warn("Trailing spaces found in #{filepath} on line #{index}")
51+
warn("Trailing spaces found in #{filepath}:#{index}")
5252
failed = true
5353
end
5454

5555
if line.match?(/^\t/)
56-
warn("Tabs found in #{filepath} on line #{index}")
56+
warn("Tabs found in #{filepath}:#{index}")
5757
failed = true
5858
end
5959
end

0 commit comments

Comments
 (0)