summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/prism.rb2
-rw-r--r--lib/prism/ffi.rb2
-rw-r--r--lib/prism/lex_compat.rb19
-rw-r--r--lib/prism/parse_result.rb72
-rw-r--r--lib/prism/parse_result/newlines.rb2
5 files changed, 79 insertions, 18 deletions
diff --git a/lib/prism.rb b/lib/prism.rb
index c512cb4015..493bb92774 100644
--- a/lib/prism.rb
+++ b/lib/prism.rb
@@ -37,7 +37,7 @@ module Prism
private_constant :LexRipper
# :call-seq:
- # Prism::lex_compat(source, **options) -> ParseResult
+ # Prism::lex_compat(source, **options) -> LexCompat::Result
#
# Returns a parse result whose value is an array of tokens that closely
# resembles the return value of Ripper::lex. The main difference is that the
diff --git a/lib/prism/ffi.rb b/lib/prism/ffi.rb
index 1fd053f902..2014ccea31 100644
--- a/lib/prism/ffi.rb
+++ b/lib/prism/ffi.rb
@@ -350,7 +350,7 @@ module Prism
node, comments, magic_comments, data_loc, errors, warnings = loader.load_nodes
tokens.each { |token,| token.value.force_encoding(loader.encoding) }
- ParseResult.new([node, tokens], comments, magic_comments, data_loc, errors, warnings, source)
+ ParseLexResult.new([node, tokens], comments, magic_comments, data_loc, errors, warnings, source)
end
end
diff --git a/lib/prism/lex_compat.rb b/lib/prism/lex_compat.rb
index 70cb065201..f199af1883 100644
--- a/lib/prism/lex_compat.rb
+++ b/lib/prism/lex_compat.rb
@@ -10,6 +10,23 @@ module Prism
# generally lines up. However, there are a few cases that require special
# handling.
class LexCompat # :nodoc:
+ # A result class specialized for holding tokens produced by the lexer.
+ class Result < Prism::Result
+ # The list of tokens that were produced by the lexer.
+ attr_reader :value
+
+ # Create a new lex compat result object with the given values.
+ def initialize(value, comments, magic_comments, data_loc, errors, warnings, source)
+ @value = value
+ super(comments, magic_comments, data_loc, errors, warnings, source)
+ end
+
+ # Implement the hash pattern matching interface for Result.
+ def deconstruct_keys(keys)
+ super.merge!(value: value)
+ end
+ end
+
# This is a mapping of prism token types to Ripper token types. This is a
# many-to-one mapping because we split up our token types, whereas Ripper
# tends to group them.
@@ -844,7 +861,7 @@ module Prism
# We sort by location to compare against Ripper's output
tokens.sort_by!(&:location)
- ParseResult.new(tokens, result.comments, result.magic_comments, result.data_loc, result.errors, result.warnings, Source.new(source))
+ Result.new(tokens, result.comments, result.magic_comments, result.data_loc, result.errors, result.warnings, Source.new(source))
end
end
diff --git a/lib/prism/parse_result.rb b/lib/prism/parse_result.rb
index 39e15f6027..2207a44fe5 100644
--- a/lib/prism/parse_result.rb
+++ b/lib/prism/parse_result.rb
@@ -438,14 +438,9 @@ module Prism
end
# This represents the result of a call to ::parse or ::parse_file. It contains
- # the AST, any comments that were encounters, and any errors that were
- # encountered.
- class ParseResult
- # The value that was generated by parsing. Normally this holds the AST, but
- # it can sometimes how a list of tokens or other results passed back from
- # the parser.
- attr_reader :value
-
+ # the requested structure, any comments that were encounters, and any errors
+ # that were encountered.
+ class Result
# The list of comments that were encountered during parsing.
attr_reader :comments
@@ -466,9 +461,8 @@ module Prism
# A Source instance that represents the source code that was parsed.
attr_reader :source
- # Create a new parse result object with the given values.
- def initialize(value, comments, magic_comments, data_loc, errors, warnings, source)
- @value = value
+ # Create a new result object with the given values.
+ def initialize(comments, magic_comments, data_loc, errors, warnings, source)
@comments = comments
@magic_comments = magic_comments
@data_loc = data_loc
@@ -477,9 +471,9 @@ module Prism
@source = source
end
- # Implement the hash pattern matching interface for ParseResult.
+ # Implement the hash pattern matching interface for Result.
def deconstruct_keys(keys)
- { value: value, comments: comments, magic_comments: magic_comments, data_loc: data_loc, errors: errors, warnings: warnings }
+ { comments: comments, magic_comments: magic_comments, data_loc: data_loc, errors: errors, warnings: warnings }
end
# Returns the encoding of the source code that was parsed.
@@ -500,6 +494,58 @@ module Prism
end
end
+ # This is a result specific to the `parse` and `parse_file` methods.
+ class ParseResult < Result
+ # The syntax tree that was parsed from the source code.
+ attr_reader :value
+
+ # Create a new parse result object with the given values.
+ def initialize(value, comments, magic_comments, data_loc, errors, warnings, source)
+ @value = value
+ super(comments, magic_comments, data_loc, errors, warnings, source)
+ end
+
+ # Implement the hash pattern matching interface for ParseResult.
+ def deconstruct_keys(keys)
+ super.merge!(value: value)
+ 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.
+ attr_reader :value
+
+ # Create a new lex result object with the given values.
+ def initialize(value, comments, magic_comments, data_loc, errors, warnings, source)
+ @value = value
+ super(comments, magic_comments, data_loc, errors, warnings, source)
+ end
+
+ # Implement the hash pattern matching interface for LexResult.
+ def deconstruct_keys(keys)
+ super.merge!(value: value)
+ end
+ end
+
+ # This is a result specific to the `parse_lex` and `parse_lex_file` methods.
+ class ParseLexResult < Result
+ # A tuple of the syntax tree and the list of tokens that were parsed from
+ # the source code.
+ attr_reader :value
+
+ # Create a new parse lex result object with the given values.
+ def initialize(value, comments, magic_comments, data_loc, errors, warnings, source)
+ @value = value
+ super(comments, magic_comments, data_loc, errors, warnings, source)
+ end
+
+ # Implement the hash pattern matching interface for ParseLexResult.
+ def deconstruct_keys(keys)
+ super.merge!(value: value)
+ end
+ end
+
# This represents a token from the Ruby source.
class Token
# The Source object that represents the source this token came from.
diff --git a/lib/prism/parse_result/newlines.rb b/lib/prism/parse_result/newlines.rb
index 03acb0b862..927c17fe2f 100644
--- a/lib/prism/parse_result/newlines.rb
+++ b/lib/prism/parse_result/newlines.rb
@@ -58,8 +58,6 @@ module Prism
# Walk the tree and mark nodes that are on a new line.
def mark_newlines!
- value = self.value
- raise "This method should only be called on a parse result that contains a node" unless Node === value
value.accept(Newlines.new(Array.new(1 + source.offsets.size, false))) # steep:ignore
end
end