| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
begin |
| 5 |
required_version = ">= 3.3.7.2" |
| 6 |
gem "parser", required_version |
| 7 |
require "parser" |
| 8 |
rescue LoadError |
| 9 |
warn(<<~MSG) |
| 10 |
Error: Unable to load parser #{required_version}. \ |
| 11 |
Add `gem "parser"` to your Gemfile or run `bundle update parser`. |
| 12 |
MSG |
| 13 |
exit(1) |
| 14 |
end |
| 15 |
|
| 16 |
module Prism |
| 17 |
module Translation |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class Parser < ::Parser::Base |
| 30 |
Diagnostic = ::Parser::Diagnostic |
| 31 |
private_constant :Diagnostic |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class PrismDiagnostic < Diagnostic |
| 37 |
|
| 38 |
attr_reader :message |
| 39 |
|
| 40 |
|
| 41 |
def initialize(message, level, reason, location) |
| 42 |
@message = message |
| 43 |
super(level, reason, {}, location, []) |
| 44 |
end |
| 45 |
end |
| 46 |
|
| 47 |
Racc_debug_parser = false |
| 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 |
def initialize(builder = Prism::Translation::Parser::Builder.new, parser: Prism) |
| 75 |
if !builder.is_a?(Prism::Translation::Parser::Builder) |
| 76 |
warn(<<~MSG, uplevel: 1, category: :deprecated) |
| 77 |
[deprecation]: The builder passed to `Prism::Translation::Parser.new` is not a \ |
| 78 |
`Prism::Translation::Parser::Builder` subclass. This will raise in the next major version. |
| 79 |
MSG |
| 80 |
end |
| 81 |
@parser = parser |
| 82 |
|
| 83 |
super(builder) |
| 84 |
end |
| 85 |
|
| 86 |
def version |
| 87 |
41 |
| 88 |
end |
| 89 |
|
| 90 |
|
| 91 |
def default_encoding |
| 92 |
Encoding::UTF_8 |
| 93 |
end |
| 94 |
|
| 95 |
def yyerror |
| 96 |
end |
| 97 |
|
| 98 |
|
| 99 |
def parse(source_buffer) |
| 100 |
@source_buffer = source_buffer |
| 101 |
source = source_buffer.source |
| 102 |
|
| 103 |
offset_cache = build_offset_cache(source) |
| 104 |
result = unwrap(@parser.parse(source, **prism_options), offset_cache) |
| 105 |
|
| 106 |
build_ast(result.value, offset_cache) |
| 107 |
ensure |
| 108 |
@source_buffer = nil |
| 109 |
end |
| 110 |
|
| 111 |
|
| 112 |
def parse_with_comments(source_buffer) |
| 113 |
@source_buffer = source_buffer |
| 114 |
source = source_buffer.source |
| 115 |
|
| 116 |
offset_cache = build_offset_cache(source) |
| 117 |
result = unwrap(@parser.parse(source, **prism_options), offset_cache) |
| 118 |
|
| 119 |
[ |
| 120 |
build_ast(result.value, offset_cache), |
| 121 |
build_comments(result.comments, offset_cache) |
| 122 |
] |
| 123 |
ensure |
| 124 |
@source_buffer = nil |
| 125 |
end |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
def tokenize(source_buffer, recover = false) |
| 130 |
@source_buffer = source_buffer |
| 131 |
source = source_buffer.source |
| 132 |
|
| 133 |
offset_cache = build_offset_cache(source) |
| 134 |
result = |
| 135 |
begin |
| 136 |
unwrap(@parser.parse_lex(source, **prism_options), offset_cache) |
| 137 |
rescue ::Parser::SyntaxError |
| 138 |
raise if !recover |
| 139 |
end |
| 140 |
|
| 141 |
program, tokens = result.value |
| 142 |
ast = build_ast(program, offset_cache) if result.success? |
| 143 |
|
| 144 |
[ |
| 145 |
ast, |
| 146 |
build_comments(result.comments, offset_cache), |
| 147 |
build_tokens(tokens, offset_cache) |
| 148 |
] |
| 149 |
ensure |
| 150 |
@source_buffer = nil |
| 151 |
end |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
def try_declare_numparam(node) |
| 156 |
node.children[0].match?(/\A_[1-9]\z/) |
| 157 |
end |
| 158 |
|
| 159 |
private |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
def valid_error?(error) |
| 164 |
true |
| 165 |
end |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
def valid_warning?(warning) |
| 170 |
true |
| 171 |
end |
| 172 |
|
| 173 |
|
| 174 |
def error_diagnostic(error, offset_cache) |
| 175 |
location = error.location |
| 176 |
diagnostic_location = build_range(location, offset_cache) |
| 177 |
|
| 178 |
case error.type |
| 179 |
when :argument_block_multi |
| 180 |
Diagnostic.new(:error, :block_and_blockarg, {}, diagnostic_location, []) |
| 181 |
when :argument_formal_constant |
| 182 |
Diagnostic.new(:error, :argument_const, {}, diagnostic_location, []) |
| 183 |
when :argument_formal_class |
| 184 |
Diagnostic.new(:error, :argument_cvar, {}, diagnostic_location, []) |
| 185 |
when :argument_formal_global |
| 186 |
Diagnostic.new(:error, :argument_gvar, {}, diagnostic_location, []) |
| 187 |
when :argument_formal_ivar |
| 188 |
Diagnostic.new(:error, :argument_ivar, {}, diagnostic_location, []) |
| 189 |
when :argument_no_forwarding_amp |
| 190 |
Diagnostic.new(:error, :no_anonymous_blockarg, {}, diagnostic_location, []) |
| 191 |
when :argument_no_forwarding_star |
| 192 |
Diagnostic.new(:error, :no_anonymous_restarg, {}, diagnostic_location, []) |
| 193 |
when :argument_no_forwarding_star_star |
| 194 |
Diagnostic.new(:error, :no_anonymous_kwrestarg, {}, diagnostic_location, []) |
| 195 |
when :begin_lonely_else |
| 196 |
location = location.copy(length: 4) |
| 197 |
diagnostic_location = build_range(location, offset_cache) |
| 198 |
Diagnostic.new(:error, :useless_else, {}, diagnostic_location, []) |
| 199 |
when :class_name, :module_name |
| 200 |
Diagnostic.new(:error, :module_name_const, {}, diagnostic_location, []) |
| 201 |
when :class_in_method |
| 202 |
Diagnostic.new(:error, :class_in_def, {}, diagnostic_location, []) |
| 203 |
when :def_endless_setter |
| 204 |
Diagnostic.new(:error, :endless_setter, {}, diagnostic_location, []) |
| 205 |
when :embdoc_term |
| 206 |
Diagnostic.new(:error, :embedded_document, {}, diagnostic_location, []) |
| 207 |
when :incomplete_variable_class, :incomplete_variable_class_3_3 |
| 208 |
location = location.copy(length: location.length + 1) |
| 209 |
diagnostic_location = build_range(location, offset_cache) |
| 210 |
|
| 211 |
Diagnostic.new(:error, :cvar_name, { name: location.slice }, diagnostic_location, []) |
| 212 |
when :incomplete_variable_instance, :incomplete_variable_instance_3_3 |
| 213 |
location = location.copy(length: location.length + 1) |
| 214 |
diagnostic_location = build_range(location, offset_cache) |
| 215 |
|
| 216 |
Diagnostic.new(:error, :ivar_name, { name: location.slice }, diagnostic_location, []) |
| 217 |
when :invalid_variable_global, :invalid_variable_global_3_3 |
| 218 |
Diagnostic.new(:error, :gvar_name, { name: location.slice }, diagnostic_location, []) |
| 219 |
when :module_in_method |
| 220 |
Diagnostic.new(:error, :module_in_def, {}, diagnostic_location, []) |
| 221 |
when :numbered_parameter_ordinary |
| 222 |
Diagnostic.new(:error, :ordinary_param_defined, {}, diagnostic_location, []) |
| 223 |
when :numbered_parameter_outer_scope |
| 224 |
Diagnostic.new(:error, :numparam_used_in_outer_scope, {}, diagnostic_location, []) |
| 225 |
when :parameter_circular |
| 226 |
Diagnostic.new(:error, :circular_argument_reference, { var_name: location.slice }, diagnostic_location, []) |
| 227 |
when :parameter_name_repeat |
| 228 |
Diagnostic.new(:error, :duplicate_argument, {}, diagnostic_location, []) |
| 229 |
when :parameter_numbered_reserved |
| 230 |
Diagnostic.new(:error, :reserved_for_numparam, { name: location.slice }, diagnostic_location, []) |
| 231 |
when :regexp_unknown_options |
| 232 |
Diagnostic.new(:error, :regexp_options, { options: location.slice[1..] }, diagnostic_location, []) |
| 233 |
when :singleton_for_literals |
| 234 |
Diagnostic.new(:error, :singleton_literal, {}, diagnostic_location, []) |
| 235 |
when :string_literal_eof |
| 236 |
Diagnostic.new(:error, :string_eof, {}, diagnostic_location, []) |
| 237 |
when :unexpected_token_ignore |
| 238 |
Diagnostic.new(:error, :unexpected_token, { token: location.slice }, diagnostic_location, []) |
| 239 |
when :write_target_in_method |
| 240 |
Diagnostic.new(:error, :dynamic_const, {}, diagnostic_location, []) |
| 241 |
else |
| 242 |
PrismDiagnostic.new(error.message, :error, error.type, diagnostic_location) |
| 243 |
end |
| 244 |
end |
| 245 |
|
| 246 |
|
| 247 |
def warning_diagnostic(warning, offset_cache) |
| 248 |
diagnostic_location = build_range(warning.location, offset_cache) |
| 249 |
|
| 250 |
case warning.type |
| 251 |
when :ambiguous_first_argument_plus |
| 252 |
Diagnostic.new(:warning, :ambiguous_prefix, { prefix: "+" }, diagnostic_location, []) |
| 253 |
when :ambiguous_first_argument_minus |
| 254 |
Diagnostic.new(:warning, :ambiguous_prefix, { prefix: "-" }, diagnostic_location, []) |
| 255 |
when :ambiguous_prefix_ampersand |
| 256 |
Diagnostic.new(:warning, :ambiguous_prefix, { prefix: "&" }, diagnostic_location, []) |
| 257 |
when :ambiguous_prefix_star |
| 258 |
Diagnostic.new(:warning, :ambiguous_prefix, { prefix: "*" }, diagnostic_location, []) |
| 259 |
when :ambiguous_prefix_star_star |
| 260 |
Diagnostic.new(:warning, :ambiguous_prefix, { prefix: "**" }, diagnostic_location, []) |
| 261 |
when :ambiguous_slash |
| 262 |
Diagnostic.new(:warning, :ambiguous_regexp, {}, diagnostic_location, []) |
| 263 |
when :dot_dot_dot_eol |
| 264 |
Diagnostic.new(:warning, :triple_dot_at_eol, {}, diagnostic_location, []) |
| 265 |
when :duplicated_hash_key |
| 266 |
|
| 267 |
else |
| 268 |
PrismDiagnostic.new(warning.message, :warning, warning.type, diagnostic_location) |
| 269 |
end |
| 270 |
end |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
def unwrap(result, offset_cache) |
| 275 |
result.errors.each do |error| |
| 276 |
next unless valid_error?(error) |
| 277 |
diagnostics.process(error_diagnostic(error, offset_cache)) |
| 278 |
end |
| 279 |
|
| 280 |
result.warnings.each do |warning| |
| 281 |
next unless valid_warning?(warning) |
| 282 |
diagnostic = warning_diagnostic(warning, offset_cache) |
| 283 |
diagnostics.process(diagnostic) if diagnostic |
| 284 |
end |
| 285 |
|
| 286 |
result |
| 287 |
end |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
def build_offset_cache(source) |
| 297 |
if source.bytesize == source.length |
| 298 |
-> (offset) { offset } |
| 299 |
else |
| 300 |
offset_cache = [] |
| 301 |
offset = 0 |
| 302 |
|
| 303 |
source.each_char do |char| |
| 304 |
char.bytesize.times { offset_cache << offset } |
| 305 |
offset += 1 |
| 306 |
end |
| 307 |
|
| 308 |
offset_cache << offset |
| 309 |
end |
| 310 |
end |
| 311 |
|
| 312 |
|
| 313 |
def build_ast(program, offset_cache) |
| 314 |
program.accept(Compiler.new(self, offset_cache)) |
| 315 |
end |
| 316 |
|
| 317 |
|
| 318 |
def build_comments(comments, offset_cache) |
| 319 |
comments.map do |comment| |
| 320 |
::Parser::Source::Comment.new(build_range(comment.location, offset_cache)) |
| 321 |
end |
| 322 |
end |
| 323 |
|
| 324 |
|
| 325 |
def build_tokens(tokens, offset_cache) |
| 326 |
Lexer.new(source_buffer, tokens, offset_cache).to_a |
| 327 |
end |
| 328 |
|
| 329 |
|
| 330 |
def build_range(location, offset_cache) |
| 331 |
::Parser::Source::Range.new( |
| 332 |
source_buffer, |
| 333 |
offset_cache[location.start_offset], |
| 334 |
offset_cache[location.end_offset] |
| 335 |
) |
| 336 |
end |
| 337 |
|
| 338 |
|
| 339 |
def prism_options |
| 340 |
options = { |
| 341 |
filepath: @source_buffer.name, |
| 342 |
version: convert_for_prism(version), |
| 343 |
partial_script: true, |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
options[:encoding] = false if @source_buffer.source.encoding != Encoding::BINARY |
| 348 |
|
| 349 |
options |
| 350 |
end |
| 351 |
|
| 352 |
|
| 353 |
def convert_for_prism(version) |
| 354 |
case version |
| 355 |
when 33 |
| 356 |
"3.3.1" |
| 357 |
when 34 |
| 358 |
"3.4.0" |
| 359 |
when 35, 40 |
| 360 |
"4.0.0" |
| 361 |
when 41 |
| 362 |
"4.1.0" |
| 363 |
else |
| 364 |
"latest" |
| 365 |
end |
| 366 |
end |
| 367 |
|
| 368 |
require_relative "parser/builder" |
| 369 |
require_relative "parser/compiler" |
| 370 |
require_relative "parser/lexer" |
| 371 |
|
| 372 |
private_constant :Compiler |
| 373 |
private_constant :Lexer |
| 374 |
end |
| 375 |
end |
| 376 |
end |