Class: Kapusta::LSP::ScopeWalker
- Inherits:
-
Object
- Object
- Kapusta::LSP::ScopeWalker
show all
- Defined in:
- lib/kapusta/lsp/scope_walker.rb
Defined Under Namespace
Classes: Binding, EndMarker, Reference, Scope
Constant Summary
collapse
- DISPATCHERS =
{
'macros' => :skip,
**Compiler::Language::QUASI_HEADS.to_h { |head| [head, :skip] },
'let' => :walk_let,
**Compiler::Language::BINDING_HEADS.to_h { |head| [head, :walk_local_var] },
'global' => :walk_global,
'set' => :walk_set,
**Compiler::Language::FUNCTION_DEFINITION_HEADS.to_h { |head| [head, :walk_fn] },
'for' => :walk_for,
'each' => :walk_each_like,
'collect' => :walk_each_like,
'icollect' => :walk_each_like,
'fcollect' => :walk_for_like,
'accumulate' => :walk_accumulate,
'faccumulate' => :walk_faccumulate,
'case' => :walk_case_match,
'match' => :walk_case_match,
'try' => :walk_try,
'module' => :walk_module_class,
'class' => :walk_module_class,
'hashfn' => :walk_hashfn,
'macro' => :walk_macro_def,
'import-macros' => :walk_import_macros,
'ivar' => :walk_sigil_form,
'cvar' => :walk_sigil_form,
'gvar' => :walk_sigil_form
}.freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of ScopeWalker.
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/kapusta/lsp/scope_walker.rb', line 56
def initialize
@bindings = []
@references = []
@end_markers = []
@scope_seq = 0
@root_scope = make_scope(nil, :file)
@gvar_scope = make_scope(nil, :gvars)
@in_module_or_class = 0
@sigil_scope_stack = [make_sigil_scopes]
end
|
Instance Attribute Details
#bindings ⇒ Object
Returns the value of attribute bindings.
48
49
50
|
# File 'lib/kapusta/lsp/scope_walker.rb', line 48
def bindings
@bindings
end
|
#end_markers ⇒ Object
Returns the value of attribute end_markers.
48
49
50
|
# File 'lib/kapusta/lsp/scope_walker.rb', line 48
def end_markers
@end_markers
end
|
#references ⇒ Object
Returns the value of attribute references.
48
49
50
|
# File 'lib/kapusta/lsp/scope_walker.rb', line 48
def references
@references
end
|
#root_scope ⇒ Object
Returns the value of attribute root_scope.
48
49
50
|
# File 'lib/kapusta/lsp/scope_walker.rb', line 48
def root_scope
@root_scope
end
|
Class Method Details
.analyze(forms) ⇒ Object
50
51
52
53
54
|
# File 'lib/kapusta/lsp/scope_walker.rb', line 50
def self.analyze(forms)
walker = new
walker.walk_top(forms)
walker
end
|
Instance Method Details
#binding_at(line, column) ⇒ Object
118
119
120
121
122
123
|
# File 'lib/kapusta/lsp/scope_walker.rb', line 118
def binding_at(line, column)
@bindings.each do |b|
return b if b.line == line && column >= b.column && column <= b.end_column
end
nil
end
|
#record_end_marker(form, target) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/kapusta/lsp/scope_walker.rb', line 102
def record_end_marker(form, target)
head = form.head
return unless head.is_a?(Sym) && head.respond_to?(:line) && head.line
@end_markers << EndMarker.new(
line: head.line,
column: head.column,
end_column: head.column + head.name.length,
target:
)
end
|
#reference_at(line, column) ⇒ Object
125
126
127
128
129
130
|
# File 'lib/kapusta/lsp/scope_walker.rb', line 125
def reference_at(line, column)
@references.each do |r|
return r if r.line == line && column >= r.column && column <= r.end_column
end
nil
end
|
#resolve_late_references ⇒ Object
72
73
74
75
76
77
78
79
80
|
# File 'lib/kapusta/lsp/scope_walker.rb', line 72
def resolve_late_references
@references.each do |r|
next if r.target
next unless r.scope
target = r.scope.lookup(r.name)
r.target = target if target
end
end
|
#sym_at(line, column) ⇒ Object
132
133
134
|
# File 'lib/kapusta/lsp/scope_walker.rb', line 132
def sym_at(line, column)
binding_at(line, column) || reference_at(line, column)
end
|
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/kapusta/lsp/scope_walker.rb', line 82
def walk_form_run(forms, start, scope, header_target: nil)
i = start
while i < forms.length
form = forms[i]
if end_form?(form)
record_end_marker(form, ) if
return i + 1
end
if Compiler::Language.bodyless_header?(form)
i = walk_bodyless_header(form, forms, i + 1, scope)
next
end
walk_form(form, scope)
i += 1
end
i
end
|
#walk_top(forms) ⇒ Object
67
68
69
70
|
# File 'lib/kapusta/lsp/scope_walker.rb', line 67
def walk_top(forms)
walk_form_run(forms, 0, @root_scope)
resolve_late_references
end
|