diff options
author | Kevin Newton <kddnewton@gmail.com> | 2024-03-12 11:26:03 -0400 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2024-03-12 15:32:25 +0000 |
commit | 1e886c040aeb225978b139a5b31da178dadd799c (patch) | |
tree | 790bb55c2c3bdab59065b3823cdb43bc20596179 /lib/prism/translation/parser/lexer.rb | |
parent | 149a449964d3a1f7498c185e85f8721b59225a7f (diff) |
[ruby/prism] Fix some whitequark/parser lexer compatibilities
https://github.com/ruby/prism/commit/34e521d071
Diffstat (limited to 'lib/prism/translation/parser/lexer.rb')
-rw-r--r-- | lib/prism/translation/parser/lexer.rb | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/lib/prism/translation/parser/lexer.rb b/lib/prism/translation/parser/lexer.rb index 0f16902d40..054e56a4ba 100644 --- a/lib/prism/translation/parser/lexer.rb +++ b/lib/prism/translation/parser/lexer.rb @@ -250,7 +250,7 @@ module Prism when :tNL value = nil when :tFLOAT - value = Float(value) + value = parse_float(value) when :tIMAGINARY value = parse_complex(value) when :tINTEGER @@ -259,7 +259,7 @@ module Prism location = Range.new(source_buffer, offset_cache[token.location.start_offset + 1], offset_cache[token.location.end_offset]) end - value = Integer(value) + value = parse_integer(value) when :tLABEL value.chomp!(":") when :tLABEL_END @@ -267,7 +267,7 @@ module Prism when :tLCURLY type = :tLBRACE if state == EXPR_BEG | EXPR_LABEL when :tNTH_REF - value = Integer(value.delete_prefix("$")) + value = parse_integer(value.delete_prefix("$")) when :tOP_ASGN value.chomp!("=") when :tRATIONAL @@ -339,6 +339,20 @@ module Prism private + # Parse an integer from the string representation. + def parse_integer(value) + Integer(value) + rescue ArgumentError + 0 + end + + # Parse a float from the string representation. + def parse_float(value) + Float(value) + rescue ArgumentError + 0.0 + end + # Parse a complex from the string representation. def parse_complex(value) value.chomp!("i") @@ -346,10 +360,12 @@ module Prism if value.end_with?("r") Complex(0, parse_rational(value)) elsif value.start_with?(/0[BbOoDdXx]/) - Complex(0, Integer(value)) + Complex(0, parse_integer(value)) else Complex(0, value) end + rescue ArgumentError + 0i end # Parse a rational from the string representation. @@ -357,10 +373,12 @@ module Prism value.chomp!("r") if value.start_with?(/0[BbOoDdXx]/) - Rational(Integer(value)) + Rational(parse_integer(value)) else Rational(value) end + rescue ArgumentError + 0r end end end |