summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKevin Newton <[email protected]>2023-10-30 14:19:16 -0400
committerKevin Newton <[email protected]>2023-11-01 13:10:29 -0400
commit953138698e4c2ba7a698ee133fbdc5d075556a5d ()
treef8badb22386067250d7a321730338e0ccb9b7bc8 /lib
parent0a460b23e0e8a4cced92e752b46c2170e83cec63 (diff)
[ruby/prism] Docs for node.rb and parse_result.rb
https://.com/ruby/prism/commit/085da4feb9
-rw-r--r--lib/prism/parse_result.rb113
1 files changed, 104 insertions, 9 deletions
@@ -5,19 +5,28 @@ module Prism
# conjunction with locations to allow them to resolve line numbers and source
# ranges.
class Source
- attr_reader :source, :offsets
def initialize(source, offsets = compute_offsets(source))
@source = source
@offsets = offsets
end
def slice(offset, length)
source.byteslice(offset, length)
end
# Binary search through the offsets to find the line number for the given
- # offset.
def line(value)
left = 0
right = offsets.length - 1
@@ -36,10 +45,13 @@ module Prism
left - 1
end
def line_offset(value)
offsets[line(value)]
end
def column(value)
value - offsets[line(value)]
end
@@ -69,6 +81,8 @@ module Prism
# The list of comments attached to this location
attr_reader :comments
def initialize(source, start_offset, length)
@source = source
@start_offset = start_offset
@@ -128,14 +142,17 @@ module Prism
source.column(end_offset)
end
def deconstruct_keys(keys)
{ start_offset: start_offset, end_offset: end_offset }
end
def pretty_print(q)
q.text("(#{start_line},#{start_column})-(#{end_line},#{end_column})")
end
def ==(other)
other.is_a?(Location) &&
other.start_offset == start_offset &&
@@ -152,6 +169,9 @@ module Prism
Location.new(source, start_offset, other.end_offset - start_offset)
end
def self.null
new(nil, 0, 0)
end
@@ -159,24 +179,41 @@ module Prism
# This represents a comment that was encountered during parsing.
class Comment
TYPES = [:inline, :embdoc, :__END__]
- attr_reader :type, :location
def initialize(type, location)
@type = type
@location = location
end
def deconstruct_keys(keys)
{ type: type, location: location }
end
- # Returns true if the comment happens on the same line as other code and false if the comment is by itself
def trailing?
type == :inline && !location.start_line_slice.strip.empty?
end
def inspect
"#<Prism::Comment @type=#{@type.inspect} @location=#{@location.inspect}>"
end
@@ -184,25 +221,34 @@ module Prism
# This represents a magic comment that was encountered during parsing.
class MagicComment
- attr_reader :key_loc, :value_loc
def initialize(key_loc, value_loc)
@key_loc = key_loc
@value_loc = value_loc
end
def key
key_loc.slice
end
def value
value_loc.slice
end
def deconstruct_keys(keys)
{ key_loc: key_loc, value_loc: value_loc }
end
def inspect
"#<Prism::MagicComment @key=#{key.inspect} @value=#{value.inspect}>"
end
@@ -210,17 +256,24 @@ module Prism
# This represents an error that was encountered during parsing.
class ParseError
- attr_reader :message, :location
def initialize(message, location)
@message = message
@location = location
end
def deconstruct_keys(keys)
{ message: message, location: location }
end
def inspect
"#<Prism::ParseError @message=#{@message.inspect} @location=#{@location.inspect}>"
end
@@ -228,17 +281,24 @@ module Prism
# This represents a warning that was encountered during parsing.
class ParseWarning
- attr_reader :message, :location
def initialize(message, location)
@message = message
@location = location
end
def deconstruct_keys(keys)
{ message: message, location: location }
end
def inspect
"#<Prism::ParseWarning @message=#{@message.inspect} @location=#{@location.inspect}>"
end
@@ -248,8 +308,27 @@ module Prism
# the AST, any comments that were encounters, and any errors that were
# encountered.
class ParseResult
- attr_reader :value, :comments, :magic_comments, :errors, :warnings, :source
def initialize(value, comments, magic_comments, errors, warnings, source)
@value = value
@comments = comments
@@ -259,14 +338,19 @@ module Prism
@source = source
end
def deconstruct_keys(keys)
{ value: value, comments: comments, magic_comments: magic_comments, errors: errors, warnings: warnings }
end
def success?
errors.empty?
end
def failure?
!success?
end
@@ -274,18 +358,28 @@ module Prism
# This represents a token from the Ruby source.
class Token
- attr_reader :type, :value, :location
def initialize(type, value, location)
@type = type
@value = value
@location = location
end
def deconstruct_keys(keys)
{ type: type, value: value, location: location }
end
def pretty_print(q)
q.group do
q.text(type.to_s)
@@ -300,6 +394,7 @@ module Prism
end
end
def ==(other)
other.is_a?(Token) &&
other.type == type &&