Class: Ibex::Frontend::Resolution

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/frontend/resolution.rb,
sig/ibex/frontend/resolution.rbs

Overview

Immutable result of resolving one root grammar and its fragment closure.

Constant Summary collapse

EMPTY_CHAIN =

Signature:

  • Array[IR::source_provenance]

Returns:

  • (Array[IR::source_provenance])
Array.new(0).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root:, root_path:, root_directory:, files:, include_chains:) ⇒ Resolution

Returns a new instance of Resolution.

RBS:

  • (root: AST::Root, root_path: String, root_directory: String, files: Array[String], include_chains: Hash[AST::Rule, Array[IR::source_provenance]]) -> void

Parameters:

  • root: (AST::Root)
  • root_path: (String)
  • root_directory: (String)
  • files: (Array[String])
  • include_chains: (Hash[AST::Rule, Array[IR::source_provenance]])


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ibex/frontend/resolution.rb', line 17

def initialize(root:, root_path:, root_directory:, files:, include_chains:)
  @root = deep_freeze_ast(root)
  @root_path = root_path.dup.freeze
  @root_directory = root_directory.dup.freeze
  @files = files.map { |file| file.dup.freeze }.freeze
  chains = {} #: Hash[AST::Rule, Array[IR::source_provenance]]
  @include_chains = chains.compare_by_identity
  include_chains.each do |rule, chain|
    frozen_chain = chain.map { |entry| copy_provenance(entry) }.freeze
    @include_chains[rule] = frozen_chain
  end
  @include_chains.freeze
  freeze
end

Instance Attribute Details

#filesArray[String] (readonly)

Signature:

  • Array[String]

Returns:

  • (Array[String])


13
14
15
# File 'lib/ibex/frontend/resolution.rb', line 13

def files
  @files
end

#rootAST::Root (readonly)

Signature:

  • AST::Root

Returns:



10
11
12
# File 'lib/ibex/frontend/resolution.rb', line 10

def root
  @root
end

#root_directoryString (readonly)

Signature:

  • String

Returns:

  • (String)


12
13
14
# File 'lib/ibex/frontend/resolution.rb', line 12

def root_directory
  @root_directory
end

#root_pathString (readonly)

Signature:

  • String

Returns:

  • (String)


11
12
13
# File 'lib/ibex/frontend/resolution.rb', line 11

def root_path
  @root_path
end

Instance Method Details

#copy_provenance(entry) ⇒ IR::source_provenance

RBS:

  • (IR::source_provenance entry) -> IR::source_provenance

Parameters:

  • entry (IR::source_provenance)

Returns:

  • (IR::source_provenance)


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ibex/frontend/resolution.rb', line 56

def copy_provenance(entry)
  span = entry[:byte_span]
  frozen_span = span && { start: span[:start], end: span[:end] } #: IR::byte_span?
  frozen_span&.freeze
  provenance = {
    file: entry[:file]&.dup&.freeze,
    root: entry[:root]&.dup&.freeze,
    byte_span: frozen_span
  } #: IR::source_provenance
  provenance.freeze
end

#deep_freeze_ast(value) ⇒ Object

RBS:

  • (untyped value) -> untyped

Parameters:

  • value (Object)

Returns:

  • (Object)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ibex/frontend/resolution.rb', line 40

def deep_freeze_ast(value)
  case value
  when Struct
    value.each_pair { |_name, child| deep_freeze_ast(child) }
  when Array
    value.each { |child| deep_freeze_ast(child) }
  when Hash
    value.each do |key, child|
      deep_freeze_ast(key)
      deep_freeze_ast(child)
    end
  end
  value.freeze
end

#include_chain_for(rule) ⇒ Array[IR::source_provenance]

RBS:

  • (AST::Rule rule) -> Array[IR::source_provenance]

Parameters:

Returns:

  • (Array[IR::source_provenance])


33
34
35
# File 'lib/ibex/frontend/resolution.rb', line 33

def include_chain_for(rule)
  @include_chains.fetch(rule, EMPTY_CHAIN)
end