summaryrefslogtreecommitdiff
path: root/lib/prism
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 /lib/prism
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
3 files changed, 115 insertions, 15 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])