Class: Prism::Result
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Subclasses:
|
|
| Inherits: | Object |
| Defined in: | lib/prism/parse_result.rb, ext/prism/extension.c |
Overview
This represents the result of a call to parse or parse_file. It contains the requested structure, any comments that were encounters, and any errors that were encountered.
Class Method Summary
-
.new(magic_comments, data_loc, errors, warnings, continuable, source) ⇒ Result
constructor
Create a new result object with the given values.
Instance Attribute Summary
-
#continuable? ⇒ Boolean
readonly
Returns true if the parsed source is an incomplete expression that could become valid with additional input.
-
#data_loc
readonly
An optional location that represents the location of the END marker and the rest of the content of the file.
-
#errors
readonly
The list of errors that were generated during parsing.
-
#failure? ⇒ Boolean
readonly
Returns true if there were errors during parsing and false if there were not.
-
#magic_comments
readonly
The list of magic comments that were encountered during parsing.
-
#source
readonly
A Source instance that represents the source code that was parsed.
-
#success? ⇒ Boolean
readonly
Returns true if there were no errors during parsing and false if there were.
-
#warnings
readonly
The list of warnings that were generated during parsing.
Instance Method Summary
-
#code_units_cache(encoding)
Create a code units cache for the given encoding.
-
#comments
The list of comments that were encountered during parsing.
-
#encoding
Returns the encoding of the source code that was parsed.
-
#deconstruct_keys(keys)
Internal use only
Implement the hash pattern matching interface for
Result.
Constructor Details
.new(magic_comments, data_loc, errors, warnings, continuable, source) ⇒ Result
# File 'lib/prism/parse_result.rb', line 934
def initialize(magic_comments, data_loc, errors, warnings, continuable, source) @magic_comments = magic_comments @data_loc = data_loc @errors = errors @warnings = warnings @continuable = continuable @source = source end
Instance Attribute Details
#continuable? ⇒ Boolean (readonly)
Returns true if the parsed source is an incomplete expression that could become valid with additional input. This is useful for REPL contexts (such as IRB) where the user may be entering a multi-line expression one line at a time and the implementation needs to determine whether to wait for more input or to evaluate what has been entered so far.
Concretely, this returns true when every error present is caused by the
parser reaching the end of the input before a construct was closed (e.g.
an unclosed string, array, block, or keyword), and returns false when any
error is caused by a token that makes the input structurally invalid
regardless of what might follow (e.g. a stray end, ], or ) with no
matching opener).
Examples:
Prism.parse("1 + [").continuable? #=> true (unclosed array)
Prism.parse("1 + ]").continuable? #=> false (stray ])
Prism.parse("tap do").continuable? #=> true (unclosed block)
Prism.parse("end.tap do").continuable? #=> false (stray end)
: () -> bool
# File 'lib/prism/parse_result.rb', line 1001
def continuable? @continuable end
#data_loc (readonly)
An optional location that represents the location of the END marker and the rest of the content of the file. This content is loaded into the DATA constant when the file being parsed is the main file being executed.
# File 'lib/prism/parse_result.rb', line 920
attr_reader :data_loc #: Location?
#errors (readonly)
The list of errors that were generated during parsing.
# File 'lib/prism/parse_result.rb', line 923
attr_reader :errors #: Array[ParseError]
#failure? ⇒ Boolean (readonly)
Returns true if there were errors during parsing and false if there were not.
: () -> bool
# File 'lib/prism/parse_result.rb', line 976
def failure? !success? end
#magic_comments (readonly)
The list of magic comments that were encountered during parsing.
# File 'lib/prism/parse_result.rb', line 915
attr_reader :magic_comments #: Array[MagicComment]
#source (readonly)
A Source instance that represents the source code that was parsed.
# File 'lib/prism/parse_result.rb', line 929
attr_reader :source #: Source
#success? ⇒ Boolean (readonly)
Returns true if there were no errors during parsing and false if there were.
: () -> bool
# File 'lib/prism/parse_result.rb', line 968
def success? errors.empty? end
#warnings (readonly)
The list of warnings that were generated during parsing.
# File 'lib/prism/parse_result.rb', line 926
attr_reader :warnings #: Array[ParseWarning]
Instance Method Details
#code_units_cache(encoding)
Create a code units cache for the given encoding.
: (Encoding encoding) -> _CodeUnitsCache
#comments
The list of comments that were encountered during parsing.
: () -> Array
# File 'lib/prism/parse_result.rb', line 946
def comments source.comments end
#deconstruct_keys(keys)
Implement the hash pattern matching interface for Result.
: (Array? keys) -> Hash[Symbol, untyped]
# File 'lib/prism/parse_result.rb', line 953
def deconstruct_keys(keys) # :nodoc: { comments: comments, magic_comments: magic_comments, data_loc: data_loc, errors: errors, warnings: warnings } end
#encoding
Returns the encoding of the source code that was parsed.
: () -> Encoding
# File 'lib/prism/parse_result.rb', line 960
def encoding source.encoding end