Module: Peruby::Lexer::Number
- Defined in:
- lib/peruby/lexer/number.rb
Overview
Converts a Perl numeric literal to a Ruby value.
Constant Summary collapse
- PATTERN =
/\A(?: v\d+(?:\.\d+)+ | 0[xX][0-9a-fA-F_]+ | 0[bB][01_]+ | 0[oO][0-7_]+ | 0[0-7_]+ | (?:\d[\d_]*\.?[\d_]*|\.[\d_]+)(?:[eE][+-]?[\d_]+)? )/x
Class Method Summary collapse
Class Method Details
.convert(literal) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/peruby/lexer/number.rb', line 20 def convert(literal) clean = literal.delete('_') return clean.delete_prefix('v').split('.').map(&:to_i) if clean.start_with?('v') return clean.to_i(16) if clean.match?(/\A0x/i) return clean.to_i(2) if clean.match?(/\A0b/i) return clean.to_i(8) if clean.match?(/\A0o/i) || clean.match?(/\A0[0-7]+\z/) return clean.to_f if clean.match?(/[.e]/i) clean.to_i end |
.scan(source) ⇒ Object
14 15 16 17 18 |
# File 'lib/peruby/lexer/number.rb', line 14 def scan(source) literal = PATTERN.match(source)&.[](0) literal = literal.chop if literal&.end_with?('.') && source[literal.length] == '.' [literal, convert(literal)] if literal end |