Class: Decode::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/decode/index.rb

Overview

A list of symbols organised for quick lookup and lexical enumeration.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIndex

Initialize an empty index.



28
29
30
31
32
33
34
# File 'lib/decode/index.rb', line 28

def initialize
  @sources = {}
  @symbols = {}
  
  # This is essentially a prefix tree:
  @trie = Trie.new
end

Instance Attribute Details

#sourcesObject (readonly)

All source files that have been parsed.



38
39
40
# File 'lib/decode/index.rb', line 38

def sources
  @sources
end

#symbolsObject (readonly)

All symbols which have been parsed.



42
43
44
# File 'lib/decode/index.rb', line 42

def symbols
  @symbols
end

#trieObject (readonly)

A (prefix) trie of lexically scoped symbols.



47
48
49
# File 'lib/decode/index.rb', line 47

def trie
  @trie
end

Instance Method Details

#lookup(reference, relative_to: nil) ⇒ Object

Lookup the specified reference and return matching symbols.

Parameters:

  • reference (Reference)

    The reference to match.

  • relative_to (Symbol) (defaults to: nil)

    Lookup the reference relative to the scope of this symbol.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/decode/index.rb', line 70

def lookup(reference, relative_to: nil)
  if reference.absolute? || relative_to.nil?
    lexical_path = []
  else
    lexical_path = relative_to.lexical_path
  end
  
  path = reference.path
  
  while true
    node = @trie.lookup(lexical_path)
    
    if node.children[path.first]
      if target = node.lookup(path)
        if reference.kind
          return target.values.select{|symbol| symbol.kind == reference.kind}
        else
          return target.values
        end
      else
        return nil
      end
    end
    
    break if lexical_path.empty?
    lexical_path.pop
  end
end

#update(paths) ⇒ Object

Updates the index by parsing the specified files. All extracted symbols are merged into the existing index.

Parameters:

  • paths (Array(String))

    The source file paths.



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/decode/index.rb', line 53

def update(paths)
  paths.each do |path|
    source = Source.new(path)
    @sources[path] = Source.new(path)
    
    source.symbols do |symbol|
      @symbols[symbol.qualified_name] = symbol
      
      @trie.insert(symbol.lexical_path, symbol)
    end
  end
end