-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscope.go
More file actions
167 lines (144 loc) · 4.09 KB
/
scope.go
File metadata and controls
167 lines (144 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2025 TypeFox GmbH
// This program and the accompanying materials are made available under the
// terms of the MIT License, which is available in the project root.
package fastbelt
import (
"iter"
"slices"
"typefox.dev/fastbelt/util/collections"
"typefox.dev/fastbelt/util/extiter"
)
func DefaultLink(scope Scope, text string) (*SymbolDescription, *ReferenceError) {
if scope == nil {
return nil, defaultRefError(text)
}
description := scope.ElementByName(text)
if description == nil {
return nil, defaultRefError(text)
} else {
return description, nil
}
}
func defaultRefError(text string) *ReferenceError {
return NewReferenceError("Could not resolve reference to '" + text + "'.")
}
type Scope interface {
ElementByName(name string) *SymbolDescription
ElementsByName(name string) iter.Seq[*SymbolDescription]
AllElements() iter.Seq[*SymbolDescription]
}
type emptyScope struct{}
func (s *emptyScope) ElementByName(name string) *SymbolDescription {
return nil
}
func (s *emptyScope) ElementsByName(name string) iter.Seq[*SymbolDescription] {
return EmptySymbolDescriptions
}
func (s *emptyScope) AllElements() iter.Seq[*SymbolDescription] {
return EmptySymbolDescriptions
}
var EmptyScope Scope = &emptyScope{}
type SeqScope struct {
elements iter.Seq[*SymbolDescription]
outer Scope
}
func NewSeqScope(elements iter.Seq[*SymbolDescription], outer Scope) *SeqScope {
return &SeqScope{
elements: elements,
outer: outer,
}
}
func (s *SeqScope) ElementByName(name string) *SymbolDescription {
for desc := range s.elements {
if desc.Name.String() == name {
return desc
}
}
if s.outer != nil {
return s.outer.ElementByName(name)
}
return nil
}
func (s *SeqScope) ElementsByName(name string) iter.Seq[*SymbolDescription] {
matching := extiter.Filter(s.elements, func(desc *SymbolDescription) bool {
return desc.Name.String() == name
})
if s.outer != nil {
return extiter.Concat(matching, s.outer.ElementsByName(name))
} else {
return matching
}
}
func (s *SeqScope) AllElements() iter.Seq[*SymbolDescription] {
if s.outer != nil {
return extiter.Concat(s.elements, s.outer.AllElements())
} else {
return s.elements
}
}
type MapScope struct {
elements collections.MultiMap[string, *SymbolDescription]
outer Scope
}
func NewMapScope(elements collections.MultiMap[string, *SymbolDescription], outer Scope) *MapScope {
return &MapScope{
elements: elements,
outer: outer,
}
}
func NewMapScopeFromSlice(elements []*SymbolDescription, outer Scope) *MapScope {
return NewMapScopeFromSeq(slices.Values(elements), outer)
}
func NewMapScopeFromSeq(elements iter.Seq[*SymbolDescription], outer Scope) *MapScope {
elemMap := collections.NewMultiMap[string, *SymbolDescription]()
for desc := range elements {
elemMap.Put(desc.Name.String(), desc)
}
return NewMapScope(elemMap, outer)
}
func (s *MapScope) ElementByName(name string) *SymbolDescription {
if elems, exists := s.elements.TryGet(name); exists && len(elems) > 0 {
return elems[0]
} else if s.outer != nil {
return s.outer.ElementByName(name)
}
return nil
}
func (s *MapScope) ElementsByName(name string) iter.Seq[*SymbolDescription] {
elems := s.elements.Get(name)
if len(elems) == 0 {
if s.outer != nil {
// Delegate directly to outer scope
return s.outer.ElementsByName(name)
} else {
// No elements found and no outer scope
return EmptySymbolDescriptions
}
} else {
seq := slices.Values(elems)
if s.outer == nil {
// No outer scope, return only the local elements
return seq
}
// Concatenate local elements with outer scope elements
return extiter.Concat(seq, s.outer.ElementsByName(name))
}
}
func (s *MapScope) AllElements() iter.Seq[*SymbolDescription] {
if s.elements.Size() == 0 {
if s.outer != nil {
// Delegate directly to outer scope
return s.outer.AllElements()
} else {
return EmptySymbolDescriptions
}
} else {
seq := s.elements.Values()
if s.outer == nil {
// No outer scope, return only the local elements
return seq
}
// Concatenate local elements with outer scope elements
return extiter.Concat(seq, s.outer.AllElements())
}
}