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
|
#include "yql_ast_annotation.h"
#include <util/string/printf.h>
#include <util/string/split.h>
#include <util/string/cast.h>
#include <util/string/builder.h>
#include <library/cpp/containers/stack_vector/stack_vec.h>
namespace NYql {
namespace {
TAstNode* AnnotateNodePosition(TAstNode& node, TMemoryPool& pool) {
auto newPosition = node.GetPosition();
TAstNode* pos = PositionAsNode(node.GetPosition(), pool);
TAstNode* shallowClone = &node;
if (node.IsList()) {
TSmallVec<TAstNode*> listChildren(node.GetChildrenCount());
for (ui32 index = 0; index < node.GetChildrenCount(); ++index) {
listChildren[index] = AnnotateNodePosition(*node.GetChild(index), pool);
}
shallowClone = TAstNode::NewList(node.GetPosition(), listChildren.data(), listChildren.size(), pool);
}
return TAstNode::NewList(newPosition, pool, pos, shallowClone);
}
TAstNode* RemoveNodeAnnotations(TAstNode& node, TMemoryPool& pool) {
if (!node.IsList())
return nullptr;
if (node.GetChildrenCount() == 0)
return nullptr;
auto lastNode = node.GetChild(node.GetChildrenCount() - 1);
auto res = lastNode;
if (lastNode->IsList()) {
TSmallVec<TAstNode*> listChildren(lastNode->GetChildrenCount());
for (ui32 index = 0; index < lastNode->GetChildrenCount(); ++index) {
auto item = RemoveNodeAnnotations(*lastNode->GetChild(index), pool);
if (!item)
return nullptr;
listChildren[index] = item;
}
res = TAstNode::NewList(lastNode->GetPosition(), listChildren.data(), listChildren.size(), pool);
}
return res;
}
TAstNode* ExtractNodeAnnotations(TAstNode& node, TAnnotationNodeMap& annotations, TMemoryPool& pool) {
if (!node.IsList())
return nullptr;
if (node.GetChildrenCount() == 0)
return nullptr;
auto lastNode = node.GetChild(node.GetChildrenCount() - 1);
auto res = lastNode;
if (lastNode->IsList()) {
TSmallVec<TAstNode*> listChildren(lastNode->GetChildrenCount());
for (ui32 index = 0; index < lastNode->GetChildrenCount(); ++index) {
auto item = ExtractNodeAnnotations(*lastNode->GetChild(index), annotations, pool);
if (!item)
return nullptr;
listChildren[index] = item;
}
res = TAstNode::NewList(lastNode->GetPosition(), listChildren.data(), listChildren.size(), pool);
}
auto& v = annotations[res];
v.resize(node.GetChildrenCount() - 1);
for (ui32 index = 0; index + 1 < node.GetChildrenCount(); ++index) {
v[index] = node.GetChild(index);
}
return res;
}
TAstNode* ApplyNodePositionAnnotations(TAstNode& node, ui32 annotationIndex, TMemoryPool& pool) {
if (!node.IsList())
return nullptr;
if (node.GetChildrenCount() < annotationIndex + 2)
return nullptr;
auto annotation = node.GetChild(annotationIndex);
auto str = annotation->GetContent();
TStringBuf rowPart;
TStringBuf colPart;
TString filePart;
GetNext(str, ':', rowPart);
GetNext(str, ':', colPart);
filePart = str;
ui32 row = 0, col = 0;
if (!TryFromString(rowPart, row) || !TryFromString(colPart, col))
return nullptr;
TSmallVec<TAstNode*> listChildren(node.GetChildrenCount());
for (ui32 index = 0; index < node.GetChildrenCount() - 1; ++index) {
listChildren[index] = node.GetChild(index);
}
auto lastNode = node.GetChild(node.GetChildrenCount() - 1);
TAstNode* lastResNode;
if (lastNode->IsAtom()) {
lastResNode = TAstNode::NewAtom(TPosition(col, row, filePart), lastNode->GetContent(), pool, lastNode->GetFlags());
} else {
TSmallVec<TAstNode*> lastNodeChildren(lastNode->GetChildrenCount());
for (ui32 index = 0; index < lastNode->GetChildrenCount(); ++index) {
lastNodeChildren[index] = ApplyNodePositionAnnotations(*lastNode->GetChild(index), annotationIndex, pool);
}
lastResNode = TAstNode::NewList(TPosition(col, row, filePart), lastNodeChildren.data(), lastNodeChildren.size(), pool);
}
listChildren[node.GetChildrenCount() - 1] = lastResNode;
return TAstNode::NewList(node.GetPosition(), listChildren.data(), listChildren.size(), pool);
}
bool ApplyNodePositionAnnotationsInplace(TAstNode& node, ui32 annotationIndex) {
if (!node.IsList())
return false;
if (node.GetChildrenCount() < annotationIndex + 2)
return false;
auto annotation = node.GetChild(annotationIndex);
TStringBuf str = annotation->GetContent();
TStringBuf rowPart;
TStringBuf colPart;
TString filePart;
GetNext(str, ':', rowPart);
GetNext(str, ':', colPart);
filePart = str;
ui32 row = 0, col = 0;
if (!TryFromString(rowPart, row) || !TryFromString(colPart, col))
return false;
auto lastNode = node.GetChild(node.GetChildrenCount() - 1);
lastNode->SetPosition(TPosition(col, row, filePart));
if (lastNode->IsList()) {
for (ui32 index = 0; index < lastNode->GetChildrenCount(); ++index) {
if (!ApplyNodePositionAnnotationsInplace(*lastNode->GetChild(index), annotationIndex))
return false;
}
}
return true;
}
}
TAstNode* AnnotatePositions(TAstNode& root, TMemoryPool& pool) {
return AnnotateNodePosition(root, pool);
}
TAstNode* RemoveAnnotations(TAstNode& root, TMemoryPool& pool) {
return RemoveNodeAnnotations(root, pool);
}
TAstNode* ApplyPositionAnnotations(TAstNode& root, ui32 annotationIndex, TMemoryPool& pool) {
return ApplyNodePositionAnnotations(root, annotationIndex, pool);
}
bool ApplyPositionAnnotationsInplace(TAstNode& root, ui32 annotationIndex) {
return ApplyNodePositionAnnotationsInplace(root, annotationIndex);
}
TAstNode* PositionAsNode(TPosition position, TMemoryPool& pool) {
TStringBuilder str;
str << position.Row << ':' << position.Column;
if (!position.File.empty()) {
str << ':' << position.File;
}
return TAstNode::NewAtom(position, str, pool);
}
TAstNode* ExtractAnnotations(TAstNode& root, TAnnotationNodeMap& annotations, TMemoryPool& pool) {
return ExtractNodeAnnotations(root, annotations, pool);
}
}
|