Class: Prism::ParseResult

Inherits:
Object show all
Defined in:
lib/prism/parse_result.rb,
lib/prism/parse_result/comments.rb,
lib/prism/parse_result/newlines.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, comments, magic_comments, data_loc, errors, warnings, source) ⇒ ParseResult

Create a new parse result object with the given values.



395
396
397
398
399
400
401
402
403
# File 'lib/prism/parse_result.rb', line 395

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

Instance Attribute Details

#commentsObject (readonly)

The list of comments that were encountered during parsing.



375
376
377
# File 'lib/prism/parse_result.rb', line 375

def comments
  @comments
end

#data_locObject (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.



383
384
385
# File 'lib/prism/parse_result.rb', line 383

def data_loc
  @data_loc
end

#errorsObject (readonly)

The list of errors that were generated during parsing.



386
387
388
# File 'lib/prism/parse_result.rb', line 386

def errors
  @errors
end

#magic_commentsObject (readonly)

The list of magic comments that were encountered during parsing.



378
379
380
# File 'lib/prism/parse_result.rb', line 378

def magic_comments
  @magic_comments
end

#sourceObject (readonly)

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



392
393
394
# File 'lib/prism/parse_result.rb', line 392

def source
  @source
end

#valueObject (readonly)

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.



372
373
374
# File 'lib/prism/parse_result.rb', line 372

def value
  @value
end

#warningsObject (readonly)

The list of warnings that were generated during parsing.



389
390
391
# File 'lib/prism/parse_result.rb', line 389

def warnings
  @warnings
end

Instance Method Details

#attach_comments!Object

Attach the list of comments to their respective locations in the tree.



173
174
175
# File 'lib/prism/parse_result/comments.rb', line 173

def attach_comments!
  Comments.new(self).attach!
end

#deconstruct_keys(keys) ⇒ Object

Implement the hash pattern matching interface for ParseResult.



406
407
408
# File 'lib/prism/parse_result.rb', line 406

def deconstruct_keys(keys)
  { value: value, comments: comments, magic_comments: magic_comments, data_loc: data_loc, errors: errors, warnings: warnings }
end

#failure?Boolean

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

Returns:

  • (Boolean)


418
419
420
# File 'lib/prism/parse_result.rb', line 418

def failure?
  !success?
end

#mark_newlines!Object

Walk the tree and mark nodes that are on a new line.



60
61
62
# File 'lib/prism/parse_result/newlines.rb', line 60

def mark_newlines!
  value.accept(Newlines.new(Array.new(1 + source.offsets.size, false)))
end

#success?Boolean

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

Returns:

  • (Boolean)


412
413
414
# File 'lib/prism/parse_result.rb', line 412

def success?
  errors.empty?
end