/
position_builder.go
207 lines (176 loc) · 5.26 KB
/
position_builder.go
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package positionbuilder
import (
"github.com/i582/php2go/src/php/node"
"github.com/i582/php2go/src/php/position"
"github.com/i582/php2go/src/php/scanner"
)
// PositionBuilder provide functions to constuct positions
type PositionBuilder struct{}
type startPos struct {
startLine int
startPos int
}
type endPos struct {
endLine int
endPos int
}
func (b *PositionBuilder) getListStartPos(l []node.Node) startPos {
if l == nil {
return startPos{-1, -1}
}
if len(l) == 0 {
return startPos{-1, -1}
}
return b.getNodeStartPos(l[0])
}
func (b *PositionBuilder) getNodeStartPos(n node.Node) startPos {
sl := -1
sp := -1
if n == nil {
return startPos{-1, -1}
}
p := n.GetPosition()
if p != nil {
sl = p.StartLine
sp = p.StartPos
}
return startPos{sl, sp}
}
func (b *PositionBuilder) getListEndPos(l []node.Node) endPos {
if l == nil {
return endPos{-1, -1}
}
if len(l) == 0 {
return endPos{-1, -1}
}
return b.getNodeEndPos(l[len(l)-1])
}
func (b *PositionBuilder) getNodeEndPos(n node.Node) endPos {
el := -1
ep := -1
if n == nil {
return endPos{-1, -1}
}
p := n.GetPosition()
if p != nil {
el = p.EndLine
ep = p.EndPos
}
return endPos{el, ep}
}
// NewNodeListPosition returns new Position
func (b *PositionBuilder) NewNodeListPosition(list []node.Node) *position.Position {
return &position.Position{
StartLine: b.getListStartPos(list).startLine,
EndLine: b.getListEndPos(list).endLine,
StartPos: b.getListStartPos(list).startPos,
EndPos: b.getListEndPos(list).endPos,
}
}
// NewNodePosition returns new Position
func (b *PositionBuilder) NewNodePosition(n node.Node) *position.Position {
return &position.Position{
StartLine: b.getNodeStartPos(n).startLine,
EndLine: b.getNodeEndPos(n).endLine,
StartPos: b.getNodeStartPos(n).startPos,
EndPos: b.getNodeEndPos(n).endPos,
}
}
// NewTokenPosition returns new Position
func (b *PositionBuilder) NewTokenPosition(t *scanner.Token) *position.Position {
return &position.Position{
StartLine: t.StartLine,
EndLine: t.EndLine,
StartPos: t.StartPos,
EndPos: t.EndPos,
}
}
// NewTokensPosition returns new Position
func (b *PositionBuilder) NewTokensPosition(startToken *scanner.Token, endToken *scanner.Token) *position.Position {
return &position.Position{
StartLine: startToken.StartLine,
EndLine: endToken.EndLine,
StartPos: startToken.StartPos,
EndPos: endToken.EndPos,
}
}
// NewTokenNodePosition returns new Position
func (b *PositionBuilder) NewTokenNodePosition(t *scanner.Token, n node.Node) *position.Position {
return &position.Position{
StartLine: t.StartLine,
EndLine: b.getNodeEndPos(n).endLine,
StartPos: t.StartPos,
EndPos: b.getNodeEndPos(n).endPos,
}
}
// NewNodeTokenPosition returns new Position
func (b *PositionBuilder) NewNodeTokenPosition(n node.Node, t *scanner.Token) *position.Position {
return &position.Position{
StartLine: b.getNodeStartPos(n).startLine,
EndLine: t.EndLine,
StartPos: b.getNodeStartPos(n).startPos,
EndPos: t.EndPos,
}
}
// NewNodesPosition returns new Position
func (b *PositionBuilder) NewNodesPosition(startNode node.Node, endNode node.Node) *position.Position {
return &position.Position{
StartLine: b.getNodeStartPos(startNode).startLine,
EndLine: b.getNodeEndPos(endNode).endLine,
StartPos: b.getNodeStartPos(startNode).startPos,
EndPos: b.getNodeEndPos(endNode).endPos,
}
}
// NewNodeListTokenPosition returns new Position
func (b *PositionBuilder) NewNodeListTokenPosition(list []node.Node, t *scanner.Token) *position.Position {
return &position.Position{
StartLine: b.getListStartPos(list).startLine,
EndLine: t.EndLine,
StartPos: b.getListStartPos(list).startPos,
EndPos: t.EndPos,
}
}
// NewTokenNodeListPosition returns new Position
func (b *PositionBuilder) NewTokenNodeListPosition(t *scanner.Token, list []node.Node) *position.Position {
return &position.Position{
StartLine: t.StartLine,
EndLine: b.getListEndPos(list).endLine,
StartPos: t.StartPos,
EndPos: b.getListEndPos(list).endPos,
}
}
// NewNodeNodeListPosition returns new Position
func (b *PositionBuilder) NewNodeNodeListPosition(n node.Node, list []node.Node) *position.Position {
return &position.Position{
StartLine: b.getNodeStartPos(n).startLine,
EndLine: b.getListEndPos(list).endLine,
StartPos: b.getNodeStartPos(n).startPos,
EndPos: b.getListEndPos(list).endPos,
}
}
// NewNodeListNodePosition returns new Position
func (b *PositionBuilder) NewNodeListNodePosition(list []node.Node, n node.Node) *position.Position {
return &position.Position{
StartLine: b.getListStartPos(list).startLine,
EndLine: b.getNodeEndPos(n).endLine,
StartPos: b.getListStartPos(list).startPos,
EndPos: b.getNodeEndPos(n).endPos,
}
}
// NewOptionalListTokensPosition returns new Position
func (b *PositionBuilder) NewOptionalListTokensPosition(list []node.Node, t *scanner.Token, endToken *scanner.Token) *position.Position {
if list == nil {
return &position.Position{
StartLine: t.StartLine,
EndLine: endToken.EndLine,
StartPos: t.StartPos,
EndPos: endToken.EndPos,
}
}
return &position.Position{
StartLine: b.getListStartPos(list).startLine,
EndLine: endToken.EndLine,
StartPos: b.getListStartPos(list).startPos,
EndPos: endToken.EndPos,
}
}