File: runner.rb

package info (click to toggle)
ruby-whitequark-parser 3.3.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,848 kB
  • sloc: yacc: 40,706; ruby: 20,588; makefile: 12; sh: 8
file content (299 lines) | stat: -rw-r--r-- 7,651 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# frozen_string_literal: true

require 'find'
require 'optparse'

require_relative '../parser'

module Parser

  class Runner
    def self.go(options)
      new.execute(options)
    end

    def initialize
      @option_parser = OptionParser.new { |opts| setup_option_parsing(opts) }
      @legacy = {}
      @parser_class  = nil
      @parser        = nil
      @files         = []
      @fragments     = []
      @warnings      = false
      @benchmark     = false

      @source_count = 0
      @source_size  = 0
    end

    def execute(options)
      parse_options(options)
      setup_builder_default
      prepare_parser

      process_all_input
    end

    private

    LEGACY_MODES = %i[lambda procarg0 encoding index arg_inside_procarg0 forward_arg kwargs match_pattern].freeze

    def runner_name
      raise NotImplementedError, "implement #{self.class}##{__callee__}"
    end

    def setup_option_parsing(opts)
      opts.banner = "Usage: #{runner_name} [options] FILE|DIRECTORY..."

      opts.on_tail '-h', '--help', 'Display this help message and exit' do
        puts opts.help
        puts <<-HELP

  If you specify a DIRECTORY, then all *.rb files are fetched
  from it recursively and appended to the file list.

  The default parsing mode is for current Ruby (#{RUBY_VERSION}).
        HELP
        exit
      end

      opts.on_tail '-V', '--version', 'Output version information and exit' do
        puts "#{runner_name} based on parser version #{Parser::VERSION}"
        exit
      end

      opts.on '--18', 'Parse as Ruby 1.8.7 would' do
        require_relative 'ruby18'
        @parser_class = Parser::Ruby18
      end

      opts.on '--19', 'Parse as Ruby 1.9.3 would' do
        require_relative 'ruby19'
        @parser_class = Parser::Ruby19
      end

      opts.on '--20', 'Parse as Ruby 2.0 would' do
        require_relative 'ruby20'
        @parser_class = Parser::Ruby20
      end

      opts.on '--21', 'Parse as Ruby 2.1 would' do
        require_relative 'ruby21'
        @parser_class = Parser::Ruby21
      end

      opts.on '--22', 'Parse as Ruby 2.2 would' do
        require_relative 'ruby22'
        @parser_class = Parser::Ruby22
      end

      opts.on '--23', 'Parse as Ruby 2.3 would' do
        require_relative 'ruby23'
        @parser_class = Parser::Ruby23
      end

      opts.on '--24', 'Parse as Ruby 2.4 would' do
        require_relative 'ruby24'
        @parser_class = Parser::Ruby24
      end

      opts.on '--25', 'Parse as Ruby 2.5 would' do
        require_relative 'ruby25'
        @parser_class = Parser::Ruby25
      end

      opts.on '--26', 'Parse as Ruby 2.6 would' do
        require_relative 'ruby26'
        @parser_class = Parser::Ruby26
      end

      opts.on '--27', 'Parse as Ruby 2.7 would' do
        require_relative 'ruby27'
        @parser_class = Parser::Ruby27
      end

      opts.on '--30', 'Parse as Ruby 3.0 would' do
        require_relative 'ruby30'
        @parser_class = Parser::Ruby30
      end

      opts.on '--31', 'Parse as Ruby 3.1 would' do
        require_relative 'ruby31'
        @parser_class = Parser::Ruby31
      end

      opts.on '--32', 'Parse as Ruby 3.2 would' do
        require_relative 'ruby32'
        @parser_class = Parser::Ruby32
      end

      opts.on '--33', 'Parse as Ruby 3.3 would' do
        require_relative 'ruby33'
        @parser_class = Parser::Ruby33
      end

      opts.on '--34', 'Parse as Ruby 3.4 would' do
        require_relative 'ruby34'
        @parser_class = Parser::Ruby34
      end

      opts.on '--mac', 'Parse as MacRuby 0.12 would' do
        require_relative 'macruby'
        @parser_class = Parser::MacRuby
      end

      opts.on '--ios', 'Parse as mid-2015 RubyMotion would' do
        require_relative 'rubymotion'
        @parser_class = Parser::RubyMotion
      end

      opts.on '--legacy', "Parse with all legacy modes" do
        @legacy = Hash.new(true)
      end

      LEGACY_MODES.each do |mode|
        opt_name = "--legacy-#{mode.to_s.gsub('_', '-')}"
        opts.on opt_name, "Parse with legacy mode for emit_#{mode}" do
          @legacy[mode] = true
        end
      end

      opts.on '-w', '--warnings', 'Enable warnings' do |w|
        @warnings = w
      end

      opts.on '-B',  '--benchmark', 'Benchmark the processor' do |b|
        @benchmark = b
      end

      opts.on '-e fragment', 'Process a fragment of Ruby code' do |fragment|
        @fragments << fragment
      end
    end

    def parse_options(options)
      @option_parser.parse!(options)

      # Slop has just removed recognized options from `options`.
      @fragments << $stdin.read if options.delete('-')
      options.each do |file_or_dir|
        if File.directory?(file_or_dir)
          Find.find(file_or_dir) do |path|
            @files << path if path.end_with? '.rb'
          end
        else
          @files << file_or_dir
        end
      end

      if @files.empty? && @fragments.empty?
        $stderr.puts 'Need something to parse!'
        exit 1
      end

      if @parser_class.nil?
        require_relative 'current'
        @parser_class = Parser::CurrentRuby
      end
    end

    def setup_builder_default
      LEGACY_MODES.each do |mode|
        Parser::Builders::Default.send(:"emit_#{mode}=", !@legacy[mode])
      end
    end

    def prepare_parser
      @parser = @parser_class.new

      @parser.diagnostics.all_errors_are_fatal = true
      @parser.diagnostics.ignore_warnings      = !@warnings

      @parser.diagnostics.consumer = lambda do |diagnostic|
        puts(diagnostic.render)
      end
    end

    def input_size
      @files.size + @fragments.size
    end

    def process_all_input
      time_start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
      process_fragments
      process_files
      time_elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - time_start

      if @benchmark
        report_with_time(time_elapsed)
      end
    end

    def process_fragments
      @fragments.each_with_index do |fragment, index|
        fragment = fragment.dup.force_encoding(@parser.default_encoding)

        buffer = Source::Buffer.new("(fragment:#{index})")
        buffer.source = fragment

        process_buffer(buffer)
      end
    end

    def process_files
      @files.each do |filename|
        source = begin
          File.read(filename).force_encoding(@parser.default_encoding)
        rescue Errno::EISDIR
          # Will happen for a folder called `foo.rb`. Just catch this here, it's cheaper than checking every file.
          next
        end

        buffer = Parser::Source::Buffer.new(filename)

        if @parser.class.name == 'Parser::Ruby18'
          buffer.raw_source = source
        else
          buffer.source     = source
        end

        process_buffer(buffer)
      end
    end

    def process_buffer(buffer)
      @parser.reset

      process(buffer)

      @source_count += 1
      @source_size  += buffer.source.size

    rescue Parser::SyntaxError
      # skip

    rescue StandardError
      $stderr.puts("Failed on: #{buffer.name}")
      raise
    end

    def process(buffer)
      raise NotImplementedError, "implement #{self.class}##{__callee__}"
    end

    def report_with_time(time_elapsed)
      speed = '%.3f' % (@source_size / time_elapsed / 1000)
      puts "Parsed #{@source_count} files (#{@source_size} characters)" \
           " in #{'%.2f' % time_elapsed} seconds (#{speed} kchars/s)."

      if defined?(RUBY_ENGINE)
        engine = RUBY_ENGINE
      else
        engine = 'ruby'
      end

      puts "Running on #{engine} #{RUBY_VERSION}."
    end
  end

end