lib/prism/parse_result.rb in prism-0.30.0 vs lib/prism/parse_result.rb in prism-1.0.0
- old
+ new
@@ -8,11 +8,15 @@
# Create a new source object with the given source code. This method should
# be used instead of `new` and it will return either a `Source` or a
# specialized and more performant `ASCIISource` if no multibyte characters
# are present in the source code.
def self.for(source, start_line = 1, offsets = [])
- source.ascii_only? ? ASCIISource.new(source, start_line, offsets): new(source, start_line, offsets)
+ if source.ascii_only?
+ ASCIISource.new(source, start_line, offsets)
+ else
+ new(source, start_line, offsets)
+ end
end
# The source code that this source object represents.
attr_reader :source
@@ -85,11 +89,16 @@
# This method is tested with UTF-8, UTF-16, and UTF-32. If there is the
# concept of code units that differs from the number of characters in other
# encodings, it is not captured here.
def code_units_offset(byte_offset, encoding)
byteslice = (source.byteslice(0, byte_offset) or raise).encode(encoding)
- (encoding == Encoding::UTF_16LE || encoding == Encoding::UTF_16BE) ? (byteslice.bytesize / 2) : byteslice.length
+
+ if encoding == Encoding::UTF_16LE || encoding == Encoding::UTF_16BE
+ byteslice.bytesize / 2
+ else
+ byteslice.length
+ end
end
# Returns the column number in code units for the given encoding for the
# given byte offset.
def code_units_column(byte_offset, encoding)
@@ -573,13 +582,15 @@
end
# This is a result specific to the `parse` and `parse_file` methods.
class ParseResult < Result
autoload :Comments, "prism/parse_result/comments"
+ autoload :Errors, "prism/parse_result/errors"
autoload :Newlines, "prism/parse_result/newlines"
private_constant :Comments
+ private_constant :Errors
private_constant :Newlines
# The syntax tree that was parsed from the source code.
attr_reader :value
@@ -602,10 +613,16 @@
# Walk the tree and mark nodes that are on a new line, loosely emulating
# the behavior of CRuby's `:line` tracepoint event.
def mark_newlines!
value.accept(Newlines.new(source.offsets.size)) # steep:ignore
end
+
+ # Returns a string representation of the syntax tree with the errors
+ # displayed inline.
+ def errors_format
+ Errors.new(self).format
+ end
end
# This is a result specific to the `lex` and `lex_file` methods.
class LexResult < Result
# The list of tokens that were parsed from the source code.
@@ -691,8 +708,14 @@
# Returns true if the given other token is equal to this token.
def ==(other)
Token === other &&
other.type == type &&
other.value == value
+ end
+
+ # Returns a string representation of this token.
+ def inspect
+ location
+ super
end
end
end