summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Newton <[email protected]>2024-03-06 14:49:25 -0500
committerKevin Newton <[email protected]>2024-03-06 21:42:54 -0500
commitd266b714672734a9604f19c55d291d12c20718a3 ()
treef133e74cd64d5e13d667c41236fd5f52b0ee573c
parent48ca2ce5fc6a7ed8f8fd0e5ead40c160369e2a4c (diff)
[ruby/prism] Use the diagnostic types in the parser translation layer
https://.com/ruby/prism/commit/1a8a0063dc
-rw-r--r--lib/prism/parse_result.rb6
-rw-r--r--lib/prism/translation/parser.rb108
-rw-r--r--lib/prism/translation/parser/lexer.rb16
-rw-r--r--prism/templates/src/diagnostic.c.erb3
-rw-r--r--test/prism/parser_test.rb17
5 files changed, 118 insertions, 32 deletions
@@ -366,7 +366,8 @@ module Prism
# This represents an error that was encountered during parsing.
class ParseError
- # The type of error.
attr_reader :type
# The message associated with this error.
@@ -399,7 +400,8 @@ module Prism
# This represents a warning that was encountered during parsing.
class ParseWarning
- # The type of warning.
attr_reader :type
# The message associated with this warning.
@@ -9,11 +9,14 @@ module Prism
# the parser gem, and overrides the parse* methods to parse with prism and
# then translate.
class Parser < ::Parser::Base
# The parser gem has a list of diagnostics with a hard-coded set of error
# messages. We create our own diagnostic class in order to set our own
# error messages.
- class Diagnostic < ::Parser::Diagnostic
- # The message generated by prism.
attr_reader :message
# Initialize a new diagnostic with the given message and location.
@@ -112,20 +115,109 @@ module Prism
true
end
# If there was a error generated during the parse, then raise an
# appropriate syntax error. Otherwise return the result.
def unwrap(result, offset_cache)
result.errors.each do |error|
next unless valid_error?(error)
-
- location = build_range(error.location, offset_cache)
- diagnostics.process(Diagnostic.new(error.message, :error, :prism_error, location))
end
result.warnings.each do |warning|
next unless valid_warning?(warning)
-
- location = build_range(warning.location, offset_cache)
- diagnostics.process(Diagnostic.new(warning.message, :warning, :prism_warning, location))
end
result
@@ -213,9 +213,11 @@ module Prism
# Convert the prism tokens into the expected format for the parser gem.
def to_a
tokens = []
index = 0
- while index < lexed.length
token, state = lexed[index]
index += 1
next if %i[IGNORED_NEWLINE __END__ EOF].include?(token.type)
@@ -229,14 +231,18 @@ module Prism
value.delete_prefix!("?")
when :tCOMMENT
if token.type == :EMBDOC_BEGIN
- until (next_token = lexed[index][0]) && next_token.type == :EMBDOC_END
value += next_token.value
index += 1
end
- value += next_token.value
- location = Range.new(source_buffer, offset_cache[token.location.start_offset], offset_cache[lexed[index][0].location.end_offset])
- index += 1
else
value.chomp!
location = Range.new(source_buffer, offset_cache[token.location.start_offset], offset_cache[token.location.end_offset - 1])
@@ -331,6 +331,9 @@ pm_diagnostic_id_human(pm_diagnostic_id_t diag_id) {
case PM_WARN_<%= warning.name %>: return "<%= warning.name.downcase %>";
<%- end -%>
}
}
static inline const char *
@@ -95,21 +95,6 @@ module Prism
end
end
- def test_warnings
- buffer = Parser::Source::Buffer.new("inline ruby with warning", 1)
- buffer.source = "do_something *array"
-
- parser = Prism::Translation::Parser33.new
- parser.diagnostics.all_errors_are_fatal = false
-
- warning = nil
- parser.diagnostics.consumer = ->(received) { warning = received }
- parser.parse(buffer)
-
- assert_equal :warning, warning.level
- assert_includes warning.message, "has been interpreted as"
- end
-
private
def assert_equal_parses(filepath, compare_tokens: true)
@@ -186,8 +171,6 @@ module Prism
actual_token[0] = expected_token[0] if %i[kDO_BLOCK kDO_LAMBDA].include?(expected_token[0])
when :tLPAREN
actual_token[0] = expected_token[0] if expected_token[0] == :tLPAREN2
- when :tLCURLY
- actual_token[0] = expected_token[0] if %i[tLBRACE tLBRACE_ARG].include?(expected_token[0])
when :tPOW
actual_token[0] = expected_token[0] if expected_token[0] == :tDSTAR
end