Class: Kapusta::LSP::ScopeWalker
- Inherits:
-
Object
- Object
- Kapusta::LSP::ScopeWalker
- Defined in:
- lib/kapusta/lsp/scope_walker.rb
Defined Under Namespace
Classes: Binding, EndMarker, Reference, Scope
Constant Summary collapse
- DISPATCHERS =
{ 'macros' => :skip, 'quasi-sym' => :skip, 'quasi-list' => :skip, 'quasi-list-tail' => :skip, 'quasi-vec' => :skip, 'quasi-vec-tail' => :skip, 'quasi-hash' => :skip, 'quasi-gensym' => :skip, 'let' => :walk_let, 'local' => :walk_local_var, 'var' => :walk_local_var, 'global' => :walk_global, 'set' => :walk_set, 'fn' => :walk_fn, 'defn' => :walk_fn, 'lambda' => :walk_fn, 'λ' => :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
-
#bindings ⇒ Object
readonly
Returns the value of attribute bindings.
-
#end_markers ⇒ Object
readonly
Returns the value of attribute end_markers.
-
#references ⇒ Object
readonly
Returns the value of attribute references.
-
#root_scope ⇒ Object
readonly
Returns the value of attribute root_scope.
Class Method Summary collapse
Instance Method Summary collapse
- #binding_at(line, column) ⇒ Object
- #end_form?(form) ⇒ Boolean
-
#initialize ⇒ ScopeWalker
constructor
A new instance of ScopeWalker.
- #record_end_marker(form, target) ⇒ Object
- #reference_at(line, column) ⇒ Object
- #sym_at(line, column) ⇒ Object
- #walk_form_run(forms, start, scope, header_target: nil) ⇒ Object
- #walk_top(forms) ⇒ Object
Constructor Details
#initialize ⇒ ScopeWalker
Returns a new instance of ScopeWalker.
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/kapusta/lsp/scope_walker.rb', line 66 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 (readonly)
Returns the value of attribute bindings.
58 59 60 |
# File 'lib/kapusta/lsp/scope_walker.rb', line 58 def bindings @bindings end |
#end_markers ⇒ Object (readonly)
Returns the value of attribute end_markers.
58 59 60 |
# File 'lib/kapusta/lsp/scope_walker.rb', line 58 def end_markers @end_markers end |
#references ⇒ Object (readonly)
Returns the value of attribute references.
58 59 60 |
# File 'lib/kapusta/lsp/scope_walker.rb', line 58 def references @references end |
#root_scope ⇒ Object (readonly)
Returns the value of attribute root_scope.
58 59 60 |
# File 'lib/kapusta/lsp/scope_walker.rb', line 58 def root_scope @root_scope end |
Class Method Details
.analyze(forms) ⇒ Object
60 61 62 63 64 |
# File 'lib/kapusta/lsp/scope_walker.rb', line 60 def self.analyze(forms) walker = new walker.walk_top(forms) walker end |
Instance Method Details
#binding_at(line, column) ⇒ Object
117 118 119 120 121 122 |
# File 'lib/kapusta/lsp/scope_walker.rb', line 117 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 |
#end_form?(form) ⇒ Boolean
113 114 115 |
# File 'lib/kapusta/lsp/scope_walker.rb', line 113 def end_form?(form) form.is_a?(List) && !form.empty? && form.head.is_a?(Sym) && form.head.name == 'end' end |
#record_end_marker(form, target) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/kapusta/lsp/scope_walker.rb', line 101 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
124 125 126 127 128 129 |
# File 'lib/kapusta/lsp/scope_walker.rb', line 124 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 |
#sym_at(line, column) ⇒ Object
131 132 133 |
# File 'lib/kapusta/lsp/scope_walker.rb', line 131 def sym_at(line, column) binding_at(line, column) || reference_at(line, column) end |
#walk_form_run(forms, start, scope, header_target: nil) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/kapusta/lsp/scope_walker.rb', line 81 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, header_target) if header_target return i + 1 end if 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
77 78 79 |
# File 'lib/kapusta/lsp/scope_walker.rb', line 77 def walk_top(forms) walk_form_run(forms, 0, @root_scope) end |