summaryrefslogtreecommitdiff
path: root/lib/prism/parse_result
diff options
context:
space:
mode:
Diffstat (limited to 'lib/prism/parse_result')
-rw-r--r--lib/prism/parse_result/comments.rb219
-rw-r--r--lib/prism/parse_result/errors.rb72
-rw-r--r--lib/prism/parse_result/newlines.rb204
3 files changed, 495 insertions, 0 deletions
diff --git a/lib/prism/parse_result/comments.rb b/lib/prism/parse_result/comments.rb
new file mode 100644
index 0000000000..df80792d39
--- /dev/null
+++ b/lib/prism/parse_result/comments.rb
@@ -0,0 +1,219 @@
+# frozen_string_literal: true
+# :markup: markdown
+#--
+# rbs_inline: enabled
+
+module Prism
+ class ParseResult < Result
+ # When we've parsed the source, we have both the syntax tree and the list of
+ # comments that we found in the source. This class is responsible for
+ # walking the tree and finding the nearest location to attach each comment.
+ #
+ # It does this by first finding the nearest locations to each comment.
+ # Locations can either come from nodes directly or from location fields on
+ # nodes. For example, a `ClassNode` has an overall location encompassing the
+ # entire class, but it also has a location for the `class` keyword.
+ #
+ # Once the nearest locations are found, it determines which one to attach
+ # to. If it's a trailing comment (a comment on the same line as other source
+ # code), it will favor attaching to the nearest location that occurs before
+ # the comment. Otherwise it will favor attaching to the nearest location
+ # that is after the comment.
+ class Comments
+ # @rbs!
+ # # An internal interface for a target that comments can be attached
+ # # to. This is either going to be a NodeTarget or a CommentTarget.
+ # interface _CommentTarget
+ # def start_offset: () -> Integer
+ # def end_offset: () -> Integer
+ # def encloses?: (Comment) -> bool
+ # def leading_comment: (Comment) -> void
+ # def trailing_comment: (Comment) -> void
+ # end
+
+ # A target for attaching comments that is based on a specific node's
+ # location.
+ class NodeTarget # :nodoc:
+ attr_reader :node #: node
+
+ #: (node node) -> void
+ def initialize(node)
+ @node = node
+ end
+
+ #: () -> Integer
+ def start_offset
+ node.start_offset
+ end
+
+ #: () -> Integer
+ def end_offset
+ node.end_offset
+ end
+
+ #: (Comment comment) -> bool
+ def encloses?(comment)
+ start_offset <= comment.location.start_offset &&
+ comment.location.end_offset <= end_offset
+ end
+
+ #: (Comment comment) -> void
+ def leading_comment(comment)
+ node.location.leading_comment(comment)
+ end
+
+ #: (Comment comment) -> void
+ def trailing_comment(comment)
+ node.location.trailing_comment(comment)
+ end
+ end
+
+ # A target for attaching comments that is based on a location field on a
+ # node. For example, the `end` token of a ClassNode.
+ class LocationTarget # :nodoc:
+ attr_reader :location #: Location
+
+ #: (Location location) -> void
+ def initialize(location)
+ @location = location
+ end
+
+ #: () -> Integer
+ def start_offset
+ location.start_offset
+ end
+
+ #: () -> Integer
+ def end_offset
+ location.end_offset
+ end
+
+ #: (Comment comment) -> bool
+ def encloses?(comment)
+ false
+ end
+
+ #: (Comment comment) -> void
+ def leading_comment(comment)
+ location.leading_comment(comment)
+ end
+
+ #: (Comment comment) -> void
+ def trailing_comment(comment)
+ location.trailing_comment(comment)
+ end
+ end
+
+ # The parse result that we are attaching comments to.
+ attr_reader :parse_result #: ParseResult
+
+ # Create a new Comments object that will attach comments to the given
+ # parse result.
+ #--
+ #: (ParseResult parse_result) -> void
+ def initialize(parse_result)
+ @parse_result = parse_result
+ end
+
+ # Attach the comments to their respective locations in the tree by
+ # mutating the parse result.
+ #--
+ #: () -> void
+ def attach!
+ parse_result.comments.each do |comment|
+ preceding, enclosing, following = nearest_targets(parse_result.value, comment)
+
+ if comment.trailing?
+ if preceding
+ preceding.trailing_comment(comment)
+ else
+ (following || enclosing || NodeTarget.new(parse_result.value)).leading_comment(comment)
+ end
+ else
+ # If a comment exists on its own line, prefer a leading comment.
+ if following
+ following.leading_comment(comment)
+ elsif preceding
+ preceding.trailing_comment(comment)
+ else
+ (enclosing || NodeTarget.new(parse_result.value)).leading_comment(comment)
+ end
+ end
+ end
+ end
+
+ private
+
+ # Responsible for finding the nearest targets to the given comment within
+ # the context of the given encapsulating node.
+ #--
+ #: (node node, Comment comment) -> [_CommentTarget?, _CommentTarget?, _CommentTarget?]
+ def nearest_targets(node, comment)
+ comment_start = comment.location.start_offset
+ comment_end = comment.location.end_offset
+
+ targets = [] #: Array[_CommentTarget]
+ node.comment_targets.map do |value|
+ case value
+ when StatementsNode
+ targets.concat(value.body.map { |node| NodeTarget.new(node) })
+ when Node
+ targets << NodeTarget.new(value)
+ when Location
+ targets << LocationTarget.new(value)
+ end
+ end
+
+ targets.sort_by!(&:start_offset)
+ preceding = nil #: _CommentTarget?
+ following = nil #: _CommentTarget?
+
+ left = 0
+ right = targets.length
+
+ # This is a custom binary search that finds the nearest nodes to the
+ # given comment. When it finds a node that completely encapsulates the
+ # comment, it recurses downward into the tree.
+ while left < right
+ middle = (left + right) / 2
+ target = targets[middle]
+
+ target_start = target.start_offset
+ target_end = target.end_offset
+
+ if target.encloses?(comment)
+ # @type var target: NodeTarget
+ # The comment is completely contained by this target. Abandon the
+ # binary search at this level.
+ return nearest_targets(target.node, comment)
+ end
+
+ if target_end <= comment_start
+ # This target falls completely before the comment. Because we will
+ # never consider this target or any targets before it again, this
+ # target must be the closest preceding target we have encountered so
+ # far.
+ preceding = target
+ left = middle + 1
+ next
+ end
+
+ if comment_end <= target_start
+ # This target falls completely after the comment. Because we will
+ # never consider this target or any targets after it again, this
+ # target must be the closest following target we have encountered so
+ # far.
+ following = target
+ right = middle
+ next
+ end
+
+ # This should only happen if there is a bug in this parser.
+ raise "Comment location overlaps with a target location"
+ end
+
+ [preceding, NodeTarget.new(node), following]
+ end
+ end
+ end
+end
diff --git a/lib/prism/parse_result/errors.rb b/lib/prism/parse_result/errors.rb
new file mode 100644
index 0000000000..388309d23d
--- /dev/null
+++ b/lib/prism/parse_result/errors.rb
@@ -0,0 +1,72 @@
+# frozen_string_literal: true
+# :markup: markdown
+#--
+# rbs_inline: enabled
+
+require "stringio"
+
+module Prism
+ class ParseResult < Result
+ # An object to represent the set of errors on a parse result. This object
+ # can be used to format the errors in a human-readable way.
+ class Errors
+ # The parse result that contains the errors.
+ attr_reader :parse_result #: ParseResult
+
+ # Initialize a new set of errors from the given parse result.
+ #--
+ #: (ParseResult parse_result) -> void
+ def initialize(parse_result)
+ @parse_result = parse_result
+ end
+
+ # Formats the errors in a human-readable way and return them as a string.
+ #--
+ #: () -> String
+ def format
+ error_lines = {} #: Hash[Integer, Array[ParseError]]
+ parse_result.errors.each do |error|
+ location = error.location
+ (location.start_line..location.end_line).each do |line|
+ error_lines[line] ||= []
+ error_lines[line] << error
+ end
+ end
+
+ source_lines = parse_result.source.source.lines
+ source_lines << "" if error_lines.key?(source_lines.size + 1)
+
+ io = StringIO.new
+ source_lines.each.with_index(1) do |line, line_number|
+ io.puts(line)
+
+ (error_lines.delete(line_number) || []).each do |error|
+ location = error.location
+
+ case line_number
+ when location.start_line
+ io.print(" " * location.start_column + "^")
+
+ if location.start_line == location.end_line
+ if location.start_column != location.end_column
+ io.print("~" * (location.end_column - location.start_column - 1))
+ end
+
+ io.puts(" " + error.message)
+ else
+ io.puts("~" * (line.bytesize - location.start_column))
+ end
+ when location.end_line
+ io.puts("~" * location.end_column + " " + error.message)
+ else
+ io.puts("~" * line.bytesize)
+ end
+ end
+ end
+
+ io.puts
+ io.string
+ end
+ end
+ end
+end
diff --git a/lib/prism/parse_result/newlines.rb b/lib/prism/parse_result/newlines.rb
new file mode 100644
index 0000000000..450c790226
--- /dev/null
+++ b/lib/prism/parse_result/newlines.rb
@@ -0,0 +1,204 @@
+# frozen_string_literal: true
+# :markup: markdown
+#--
+# rbs_inline: enabled
+
+module Prism
+ class ParseResult < Result
+ # The :line tracepoint event gets fired whenever the Ruby VM encounters an
+ # expression on a new line. The types of expressions that can trigger this
+ # event are:
+ #
+ # * if statements
+ # * unless statements
+ # * nodes that are children of statements lists
+ #
+ # In order to keep track of the newlines, we have a list of offsets that
+ # come back from the parser. We assign these offsets to the first nodes that
+ # we find in the tree that are on those lines.
+ #
+ # Note that the logic in this file should be kept in sync with the Java
+ # MarkNewlinesVisitor, since that visitor is responsible for marking the
+ # newlines for JRuby/TruffleRuby.
+ #
+ # This file is autoloaded only when `mark_newlines!` is called, so the
+ # re-opening of the various nodes in this file will only be performed in
+ # that case. We do that to avoid storing the extra `@newline` instance
+ # variable on every node if we don't need it.
+ class Newlines < Visitor
+ # The map of lines indices to whether or not they have been marked as
+ # emitting a newline event.
+ # @rbs @lines: Array[bool]
+
+ # Create a new Newlines visitor with the given newline offsets.
+ #--
+ #: (Integer lines) -> void
+ def initialize(lines)
+ @lines = Array.new(1 + lines, false)
+ end
+
+ # Permit block nodes to mark newlines within themselves.
+ #--
+ #: (BlockNode node) -> void
+ def visit_block_node(node)
+ old_lines = @lines
+ @lines = Array.new(old_lines.size, false)
+
+ begin
+ super(node)
+ ensure
+ @lines = old_lines
+ end
+ end
+
+ # Permit lambda nodes to mark newlines within themselves.
+ #--
+ #: (LambdaNode node) -> void
+ def visit_lambda_node(node)
+ old_lines = @lines
+ @lines = Array.new(old_lines.size, false)
+
+ begin
+ super(node)
+ ensure
+ @lines = old_lines
+ end
+ end
+
+ # Mark if nodes as newlines.
+ #--
+ #: (IfNode node) -> void
+ def visit_if_node(node)
+ node.newline_flag!(@lines)
+ super(node)
+ end
+
+ # Mark unless nodes as newlines.
+ #--
+ #: (UnlessNode node) -> void
+ def visit_unless_node(node)
+ node.newline_flag!(@lines)
+ super(node)
+ end
+
+ # Permit statements lists to mark newlines within themselves.
+ #--
+ #: (StatementsNode node) -> void
+ def visit_statements_node(node)
+ node.body.each do |child|
+ child.newline_flag!(@lines)
+ end
+ super(node)
+ end
+ end
+ end
+
+ class Node
+ # Tracks whether or not this node should emit a newline event when the
+ # instructions that it represents are executed.
+ # @rbs @newline_flag: bool
+
+ #: () -> bool
+ def newline_flag? # :nodoc:
+ !!defined?(@newline_flag)
+ end
+
+ #: (Array[bool] lines) -> void
+ def newline_flag!(lines) # :nodoc:
+ line = location.start_line
+ unless lines[line]
+ lines[line] = true
+ @newline_flag = true
+ end
+ end
+ end
+
+ class BeginNode < Node
+ #: (Array[bool] lines) -> void
+ def newline_flag!(lines) # :nodoc:
+ # Never mark BeginNode with a newline flag, mark children instead.
+ end
+ end
+
+ class ParenthesesNode < Node
+ #: (Array[bool] lines) -> void
+ def newline_flag!(lines) # :nodoc:
+ # Never mark ParenthesesNode with a newline flag, mark children instead.
+ end
+ end
+
+ class IfNode < Node
+ #: (Array[bool] lines) -> void
+ def newline_flag!(lines) # :nodoc:
+ predicate.newline_flag!(lines)
+ end
+ end
+
+ class UnlessNode < Node
+ #: (Array[bool] lines) -> void
+ def newline_flag!(lines) # :nodoc:
+ predicate.newline_flag!(lines)
+ end
+ end
+
+ class UntilNode < Node
+ #: (Array[bool] lines) -> void
+ def newline_flag!(lines) # :nodoc:
+ predicate.newline_flag!(lines)
+ end
+ end
+
+ class WhileNode < Node
+ #: (Array[bool] lines) -> void
+ def newline_flag!(lines) # :nodoc:
+ predicate.newline_flag!(lines)
+ end
+ end
+
+ class RescueModifierNode < Node
+ #: (Array[bool] lines) -> void
+ def newline_flag!(lines) # :nodoc:
+ expression.newline_flag!(lines)
+ end
+ end
+
+ class InterpolatedMatchLastLineNode < Node
+ #: (Array[bool] lines) -> void
+ def newline_flag!(lines) # :nodoc:
+ first = parts.first
+ first.newline_flag!(lines) if first
+ end
+ end
+
+ class InterpolatedRegularExpressionNode < Node
+ #: (Array[bool] lines) -> void
+ def newline_flag!(lines) # :nodoc:
+ first = parts.first
+ first.newline_flag!(lines) if first
+ end
+ end
+
+ class InterpolatedStringNode < Node
+ #: (Array[bool] lines) -> void
+ def newline_flag!(lines) # :nodoc:
+ first = parts.first
+ first.newline_flag!(lines) if first
+ end
+ end
+
+ class InterpolatedSymbolNode < Node
+ #: (Array[bool] lines) -> void
+ def newline_flag!(lines) # :nodoc:
+ first = parts.first
+ first.newline_flag!(lines) if first
+ end
+ end
+
+ class InterpolatedXStringNode < Node
+ #: (Array[bool] lines) -> void
+ def newline_flag!(lines) # :nodoc:
+ first = parts.first
+ first.newline_flag!(lines) if first
+ end
+ end
+end