123456789_123456789_123456789_123456789_123456789_

Class: Prism::Result

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: lib/prism/parse_result.rb,
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.

Constant Summary

  • CONTINUABLE = private

    The set of error types whose location the parser places at the opening token of an unclosed construct rather than at the end of the source. These errors always indicate incomplete input regardless of their byte position, so they are checked by type rather than by location.

    # File 'lib/prism/parse_result.rb', line 976
    %i[
      begin_term
      begin_upcase_term
      block_param_pipe_term
      block_term_brace
      block_term_end
      case_missing_conditions
      case_term
      class_term
      conditional_term
      conditional_term_else
      def_term
      embdoc_term
      end_upcase_term
      for_term
      hash_term
      heredoc_term
      lambda_term_brace
      lambda_term_end
      list_i_lower_term
      list_i_upper_term
      list_w_lower_term
      list_w_upper_term
      module_term
      regexp_term
      rescue_term
      string_interpolated_term
      string_literal_eof
      symbol_term_dynamic
      symbol_term_interpolated
      until_term
      while_term
      xstring_term
    ].freeze

Class Method Summary

Instance Attribute Summary

  • #comments readonly

    The list of comments that were encountered during parsing.

  • #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

Constructor Details

.new(comments, magic_comments, data_loc, errors, warnings, source) ⇒ Result

Create a new result object with the given values.

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 902

def initialize(comments, magic_comments, data_loc, errors, warnings, source)
  @comments = comments
  @magic_comments = magic_comments
  @data_loc = data_loc
  @errors = errors
  @warnings = warnings
  @source = source
end

Instance Attribute Details

#comments (readonly)

The list of comments that were encountered during parsing.

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 880

attr_reader :comments #: Array[Comment]

#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)
[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 963

def continuable?
  return false if errors.empty?

  offset = source.source.bytesize
  errors.all? { |error| CONTINUABLE.include?(error.type) || error.location.start_offset == offset }
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.

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 888

attr_reader :data_loc #: Location?

#errors (readonly)

The list of errors that were generated during parsing.

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 891

attr_reader :errors #: Array[ParseError]

#failure?Boolean (readonly)

Returns true if there were errors during parsing and false if there were not.

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 937

def failure?
  !success?
end

#magic_comments (readonly)

The list of magic comments that were encountered during parsing.

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 883

attr_reader :magic_comments #: Array[MagicComment]

#source (readonly)

A Source instance that represents the source code that was parsed.

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 897

attr_reader :source #: Source

#success?Boolean (readonly)

Returns true if there were no errors during parsing and false if there were.

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 929

def success?
  errors.empty?
end

#warnings (readonly)

The list of warnings that were generated during parsing.

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 894

attr_reader :warnings #: Array[ParseWarning]

Instance Method Details

#code_units_cache(encoding)

Create a code units cache for the given encoding.

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 1016

def code_units_cache(encoding)
  source.code_units_cache(encoding)
end

#deconstruct_keys(keys)

This method is for internal use only.

Implement the hash pattern matching interface for Result.

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 914

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.

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 921

def encoding
  source.encoding
end