Skip to content

Commit

Permalink
- circumvent a racc bug that could cause Parser#parse and depende…
Browse files Browse the repository at this point in the history
…nts to return `false` instead of `nil`
  • Loading branch information
marcandre committed Jul 22, 2020
1 parent 07ab97e commit 79f541a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/parser/base.rb
Expand Up @@ -177,16 +177,16 @@ def reset
end

##
# Parses a source buffer and returns the AST.
# Parses a source buffer and returns the AST, or `nil` in case of a non fatal error.
#
# @param [Parser::Source::Buffer] source_buffer The source buffer to parse.
# @return [Parser::AST::Node]
# @return [Parser::AST::Node, nil]
#
def parse(source_buffer)
@lexer.source_buffer = source_buffer
@source_buffer = source_buffer

do_parse
do_parse || nil # Force `false` to `nil`, see https://github.com/ruby/racc/pull/136
ensure
# Don't keep references to the source file.
@source_buffer = nil
Expand All @@ -210,8 +210,9 @@ def parse_with_comments(source_buffer)

##
# Parses a source buffer and returns the AST, the source code comments,
# and the tokens emitted by the lexer. If `recover` is true and a fatal
# {SyntaxError} is encountered, `nil` is returned instead of the AST, and
# and the tokens emitted by the lexer. In case of a fatal error, a {SyntaxError}
# is raised, unless `recover` is true. In case of an error
# (non-fatal or recovered), `nil` is returned instead of the AST, and
# comments as well as tokens are only returned up to the location of
# the error.
#
Expand Down
10 changes: 10 additions & 0 deletions test/test_parser.rb
Expand Up @@ -9796,4 +9796,14 @@ def test_find_pattern
%q{ ~~~~~~~~ expression (in_pattern.find_pattern)},
SINCE_2_8)
end

def test_invalid_source
with_versions(ALL_VERSIONS) do |_ver, parser|
source_file = Parser::Source::Buffer.new('(comments)', source: "def foo; en")

parser.diagnostics.all_errors_are_fatal = false
ast = parser.parse(source_file)
assert_nil(ast)
end
end
end

0 comments on commit 79f541a

Please sign in to comment.