Skip to content

Commit beb5ccd

Browse files
authored
Supress warnings during parsing (#1013)
* Supress warnings during regexp creation User input can contain arbitrary regexes. Something like `Parser::CurrentRuby.parse("/[\177\01\1778]/")` shows a warning: > character class has duplicated range: /[\x7f\x01\x7f8]/ * Don't emit warnings when parsing floats `Parser::CurrentRuby.parse("9.9999e999")` > Float 9.9999e999 out of range
1 parent 75c6ce0 commit beb5ccd

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

lib/parser/builders/default.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -2263,7 +2263,12 @@ def static_regexp(parts, options)
22632263
source
22642264
end
22652265

2266-
Regexp.new(source, (Regexp::EXTENDED if options.children.include?(:x)))
2266+
begin
2267+
old_verbose, $VERBOSE = $VERBOSE, nil
2268+
Regexp.new(source, (Regexp::EXTENDED if options.children.include?(:x)))
2269+
ensure
2270+
$VERBOSE = old_verbose
2271+
end
22672272
end
22682273

22692274
def static_regexp_node(node)

lib/parser/lexer.rl

+13-4
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,23 @@ class Parser::Lexer
116116
@emit_integer_if = lambda { |chars, p| emit(:tINTEGER, chars, @ts, @te - 2); p - 2 }
117117
@emit_integer_rescue = lambda { |chars, p| emit(:tINTEGER, chars, @ts, @te - 6); p - 6 }
118118

119-
@emit_float = lambda { |chars, p| emit(:tFLOAT, Float(chars)); p }
120-
@emit_imaginary_float = lambda { |chars, p| emit(:tIMAGINARY, Complex(0, Float(chars))); p }
121-
@emit_float_if = lambda { |chars, p| emit(:tFLOAT, Float(chars), @ts, @te - 2); p - 2 }
122-
@emit_float_rescue = lambda { |chars, p| emit(:tFLOAT, Float(chars), @ts, @te - 6); p - 6 }
119+
@emit_float = lambda { |chars, p| emit(:tFLOAT, construct_float(chars)); p }
120+
@emit_imaginary_float = lambda { |chars, p| emit(:tIMAGINARY, Complex(0, construct_float(chars))); p }
121+
@emit_float_if = lambda { |chars, p| emit(:tFLOAT, construct_float(chars), @ts, @te - 2); p - 2 }
122+
@emit_float_rescue = lambda { |chars, p| emit(:tFLOAT, construct_float(chars), @ts, @te - 6); p - 6 }
123123

124124
reset
125125
end
126126

127+
def construct_float(chars)
128+
begin
129+
old_verbose, $VERBOSE = $VERBOSE, nil
130+
Float(chars)
131+
ensure
132+
$VERBOSE = old_verbose
133+
end
134+
end
135+
127136
def reset(reset_state=true)
128137
# Ragel state:
129138
if reset_state

0 commit comments

Comments
 (0)