Class: YARD::Parser::OrderedParser

Inherits:
Object
  • Object
show all
Defined in:
lib/yard/parser/source_parser.rb

Overview

Responsible for parsing a list of files in order. The #parse method of this class can be called from the SourceParser#globals globals state list to re-enter parsing for the remainder of files in the list recursively.

Handlers that need an object from a file later in the list can be deferred (see Handlers::Base#ensure_loaded!). Deferred handlers are processed again once all files have been parsed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(global_state, files) ⇒ OrderedParser

Note:

OrderedParser sets itself as the ordered_parser key on global_state for later use in Handlers::Processor.

Creates a new OrderedParser with the global state and a list of files to parse.

Parameters:

  • global_state (OpenStruct) —

    a structure containing all global state during parsing

  • files (Array<String>) —

    the list of files to parse



42
43
44
45
46
47
48
# File 'lib/yard/parser/source_parser.rb', line 42

def initialize(global_state, files)
  @global_state = global_state
  @files = files.dup
  @deferred_statements = []
  @deferring = true
  @global_state.ordered_parser = self
end

Instance Attribute Details

#deferred_statements ⇒ Array<Array(Handlers::Processor, Object)> (readonly)

Returns the statements whose handlers were deferred until all files are parsed, each with the processor state to process it with.

Returns:

  • (Array<Array(Handlers::Processor, Object)>) —

    the statements whose handlers were deferred until all files are parsed, each with the processor state to process it with

See Also:



32
33
34
# File 'lib/yard/parser/source_parser.rb', line 32

def deferred_statements
  @deferred_statements
end

#files ⇒ Array<String>

Returns the list of remaining files to parse.

Returns:

  • (Array<String>) —

    the list of remaining files to parse



26
27
28
# File 'lib/yard/parser/source_parser.rb', line 26

def files
  @files
end

Instance Method Details

#deferring? ⇒ Boolean

Returns whether handlers can currently be deferred. Handlers can no longer be deferred once all files have been parsed and no deferred handler could be resolved.

Returns:

  • (Boolean) —

    whether handlers can currently be deferred. Handlers can no longer be deferred once all files have been parsed and no deferred handler could be resolved.



81
82
83
# File 'lib/yard/parser/source_parser.rb', line 81

def deferring?
  @deferring
end

#parse ⇒ Object

Parses the remainder of the #files list, then processes any handlers that were deferred while parsing.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/yard/parser/source_parser.rb', line 54

def parse
  until files.empty?
    file = files.shift
    log.capture("Parsing #{file}") do
      SourceParser.new(SourceParser.parser_type, @global_state).parse(file)
    end
  end

  # Process deferred statements in passes until they all complete. A
  # pass that ends with as many deferred as it started with made no
  # progress: the rest wait on objects that will never be resolved, so
  # they are processed one last time without deferring, which raises
  # their missing object errors.
  until @deferred_statements.empty?
    statements = @deferred_statements
    @deferred_statements = []
    statements.each do |processor, statement|
      log.debug "Re-processing #{processor.file}:#{statement.line}..."
      processor.process([statement])
    end
    @deferring = false if @deferred_statements.size >= statements.size
  end
end