File: gauntlet_parser.rb

package info (click to toggle)
ruby-whitequark-parser 3.3.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,828 kB
  • sloc: yacc: 40,699; ruby: 20,395; makefile: 12; sh: 8
file content (123 lines) | stat: -rw-r--r-- 2,624 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
# frozen_string_literal: true

require 'gauntlet'
require_relative 'parser/all'
require 'shellwords'

class ParserGauntlet < Gauntlet
  RUBY20 = 'ruby'
  RUBY19 = 'ruby1.9.1'
  RUBY18 = '/opt/rubies/ruby-1.8.7-p370/bin/ruby'

  def try(parser, ruby, file, show_ok: false)
    try_ruby = lambda do |e|
      Process.spawn(%{#{ruby} -c #{Shellwords.escape file}},
                    :err => '/dev/null', :out => '/dev/null')
      _, status = Process.wait2

      if status.success?
        # Bug in Parser.
        puts "Parser bug."
        @result[file] = { parser.to_s => "#{e.class}: #{e.to_s}" }
      else
        # No, this file is not Ruby.
        yield if block_given?
      end
    end

    begin
      parser.parse_file(file)

    rescue Parser::SyntaxError => e
      if e.diagnostic.location.resize(2).is?('<%')
        puts "ERb."
        return
      end

      try_ruby.call(e)

    rescue ArgumentError, RegexpError,
           Encoding::UndefinedConversionError => e
      puts "#{file}: #{e.class}: #{e.to_s}"

      try_ruby.call(e)

    rescue Interrupt
      raise

    rescue Exception => e
      puts "Parser bug: #{file} #{e.class}: #{e.to_s}"
      @result[file] = { parser.to_s => "#{e.class}: #{e.to_s}" }

    else
      puts "Ok." if show_ok
    end
  end

  def parse(name)
    puts "GEM: #{name}"

    @result = {}

    if ENV.include?('FAST')
      total_size = Dir["**/*.rb"].map(&File.method(:size)).reduce(:+)
      if total_size > 300_000
        puts "Skip."
        return
      end
    end

    Dir["**/*.rb"].each do |file|
      next if File.directory? file

      try(Parser::Ruby20, RUBY20, file) do
        puts "Trying 1.9:"
        try(Parser::Ruby19, RUBY19, file, show_ok: true) do
          puts "Trying 1.8:"
          try(Parser::Ruby18, RUBY18, file, show_ok: true) do
            puts "Invalid syntax."
          end
        end
      end
    end

    @result
  end

  def run(name)
    data[name] = parse(name)
    self.dirty = true
  end

  def should_skip?(name)
    data[name] == {}
  end

  def load_yaml(*)
    data = super
    @was_errors = data.count { |_name, errs| errs != {} }

    data
  end

  def shutdown
    super

    errors  = data.count { |_name, errs| errs != {} }
    total   = data.count
    percent = "%.5f" % [100 - errors.to_f / total * 100]
    puts "!!! was: #{@was_errors} now: #{errors} total: #{total} frac: #{percent}%"
  end
end

filter = ARGV.shift
filter = Regexp.new filter if filter

gauntlet = ParserGauntlet.new

if ENV.include? 'UPDATE'
  gauntlet.source_index
  gauntlet.update_gem_tarballs
end

gauntlet.run_the_gauntlet filter