Skip to content

Commit 8bb1167

Browse files
committed
[libSyntax] Restructure RawSyntax to more closely resemble the SwiftSyntax implementation
1 parent fb70ab4 commit 8bb1167

27 files changed

+743
-601
lines changed

include/swift/Parse/Parser.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ namespace swift {
5959
struct EnumElementInfo;
6060

6161
namespace syntax {
62-
class AbsolutePosition;
6362
class RawSyntax;
6463
enum class SyntaxKind;
6564
}// end of syntax namespace
@@ -1817,14 +1816,10 @@ bool isKeywordPossibleDeclStart(const Token &Tok);
18171816

18181817
/// Lex and return a vector of `TokenSyntax` tokens, which include
18191818
/// leading and trailing trivia.
1820-
std::vector<std::pair<RC<syntax::RawSyntax>,
1821-
syntax::AbsolutePosition>>
1822-
tokenizeWithTrivia(const LangOptions &LangOpts,
1823-
const SourceManager &SM,
1824-
unsigned BufferID,
1825-
unsigned Offset = 0,
1826-
unsigned EndOffset = 0,
1827-
DiagnosticEngine *Diags = nullptr);
1819+
std::vector<std::pair<RC<syntax::RawSyntax>, syntax::AbsoluteOffsetPosition>>
1820+
tokenizeWithTrivia(const LangOptions &LangOpts, const SourceManager &SM,
1821+
unsigned BufferID, unsigned Offset = 0,
1822+
unsigned EndOffset = 0, DiagnosticEngine *Diags = nullptr);
18281823
} // end namespace swift
18291824

18301825
#endif

include/swift/Parse/SyntaxParsingCache.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ struct SourceEdit {
4848
};
4949

5050
struct SyntaxReuseRegion {
51-
AbsolutePosition Start;
52-
AbsolutePosition End;
51+
AbsoluteOffsetPosition Start;
52+
AbsoluteOffsetPosition End;
5353
};
5454

5555
class SyntaxParsingCache {
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
//===--- AbsoluteRawSyntax.h ------------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_SYNTAX_ABSOLUTERAWSYNTAX_H
14+
#define SWIFT_SYNTAX_ABSOLUTERAWSYNTAX_H
15+
16+
#include "swift/Basic/SourceLoc.h"
17+
#include "swift/Syntax/RawSyntax.h"
18+
19+
namespace swift {
20+
namespace syntax {
21+
22+
/// Type-safe wrapper around a \c size_t that describes a node's index in its
23+
/// tree. This represents the index at which the node will be encountered during
24+
/// a depth-first traversal of the tree.
25+
class SyntaxIndexInTree {
26+
size_t IndexInTree;
27+
28+
explicit SyntaxIndexInTree(size_t IndexInTree) : IndexInTree(IndexInTree) {}
29+
30+
public:
31+
static SyntaxIndexInTree zero() { return SyntaxIndexInTree(0); }
32+
33+
/// Assuming that this index points to the start of \p Raw, advance it so that
34+
/// it points to the next sibling of \p Raw.
35+
SyntaxIndexInTree advancedBy(const RC<RawSyntax> &Raw) const;
36+
37+
/// Assuming that this index points to the next sibling of \p Raw, reverse it
38+
/// so that it points to the start of \p Raw.
39+
SyntaxIndexInTree reversedBy(const RC<RawSyntax> &Raw) const;
40+
41+
/// Advance this index to point to its first immediate child.
42+
SyntaxIndexInTree advancedToFirstChild() const;
43+
};
44+
45+
/// A syntax identifier that globally identifies a \c Syntax node.
46+
/// In contrast to \c NodeId of \c RawSyntax, this also takes into account the
47+
/// node's position in the tree. For example, a source file may contain two
48+
/// int literals with value 0. The corresponding \c RawSyntax nodes can share
49+
/// the same \c NodeId. However, because the literals occur in different
50+
/// locations of the syntax tree, their \c SyntaxIdentifiers are different.
51+
class SyntaxIdentifier {
52+
public:
53+
using RootIdType = size_t;
54+
55+
private:
56+
static std::atomic<RootIdType> NextUnusedRootId;
57+
58+
/// An integer that identifies the tree in which the node represented by this
59+
/// identifier lives.
60+
RootIdType RootId;
61+
62+
/// The position of the node represented by this identifier in the syntax
63+
/// tree.
64+
SyntaxIndexInTree IndexInTree;
65+
66+
public:
67+
SyntaxIdentifier(RootIdType RootId, SyntaxIndexInTree IndexInTree)
68+
: RootId(RootId), IndexInTree(IndexInTree) {
69+
assert(RootId < NextUnusedRootId && "New RootIds should only be created "
70+
"using the newRoot constructor.");
71+
}
72+
73+
/// Create a SyntaxIdentifier that refers to the root of a new syntax tree.
74+
static SyntaxIdentifier newRoot() {
75+
return SyntaxIdentifier(NextUnusedRootId++, SyntaxIndexInTree::zero());
76+
}
77+
78+
RootIdType getRootId() const { return RootId; }
79+
SyntaxIndexInTree getIndexInTree() const { return IndexInTree; }
80+
81+
/// Assuming that this identifier points to the start of \p Raw, advance it so
82+
/// that it points to the next sibling of \p Raw.
83+
SyntaxIdentifier advancedBy(const RC<RawSyntax> &Raw) const {
84+
auto NewIndexInTree = IndexInTree.advancedBy(Raw);
85+
return SyntaxIdentifier(RootId, NewIndexInTree);
86+
}
87+
88+
/// Assuming that this identifier points to the next sibling of \p Raw,
89+
/// reverse it so that it points to the start of \p Raw.
90+
SyntaxIdentifier reversedBy(const RC<RawSyntax> &Raw) const {
91+
auto NewIndexInTree = IndexInTree.reversedBy(Raw);
92+
return SyntaxIdentifier(RootId, NewIndexInTree);
93+
}
94+
95+
/// Get the identifier of the first immediate child.
96+
SyntaxIdentifier advancedToFirstChild() const {
97+
auto NewIndexInTree = IndexInTree.advancedToFirstChild();
98+
return SyntaxIdentifier(RootId, NewIndexInTree);
99+
}
100+
};
101+
102+
/// Represents a node's position in a syntax tree, described by its overal
103+
/// textual offset and the position within its parent.
104+
class AbsoluteSyntaxPosition {
105+
public:
106+
using OffsetType = uint32_t;
107+
using IndexInParentType = uint32_t;
108+
109+
private:
110+
/// The text offset where this node starts within its syntax tree, counted in
111+
/// UTF-8 bytes.
112+
OffsetType Offset;
113+
114+
/// The node's index within its parent, i.e.
115+
/// `node.parent.childAt(IndexInParent) = node`.
116+
IndexInParentType IndexInParent;
117+
118+
public:
119+
AbsoluteSyntaxPosition(OffsetType Offset, IndexInParentType IndexInParent)
120+
: Offset(Offset), IndexInParent(IndexInParent) {}
121+
122+
/// Create a new \c AbsoluteSyntaxPosition that refers to the root of a syntax
123+
/// tree.
124+
static AbsoluteSyntaxPosition forRoot() {
125+
return AbsoluteSyntaxPosition(0, 0);
126+
}
127+
128+
OffsetType getOffset() const { return Offset; }
129+
IndexInParentType getIndexInParent() const { return IndexInParent; }
130+
131+
/// Assuming that this position points to the start of \p Raw, advance it so
132+
/// that it points to the next sibling of \p Raw.
133+
AbsoluteSyntaxPosition advancedBy(const RC<RawSyntax> &Raw) const;
134+
135+
/// Assuming that this position points to the next sibling of \p Raw, reverse
136+
/// it so that it points to the start of \p Raw.
137+
AbsoluteSyntaxPosition reversedBy(const RC<RawSyntax> &Raw) const;
138+
139+
/// Get the position of the node's first immediate child.
140+
AbsoluteSyntaxPosition advancedToFirstChild() const {
141+
return AbsoluteSyntaxPosition(Offset, 0);
142+
}
143+
};
144+
145+
/// A type-safe wrapper that describes a node's textual position within a source
146+
/// file, represented by its UTF-8 byte offset from the start.
147+
class AbsoluteOffsetPosition {
148+
AbsoluteSyntaxPosition::OffsetType Offset;
149+
150+
public:
151+
explicit AbsoluteOffsetPosition(AbsoluteSyntaxPosition::OffsetType Offset)
152+
: Offset(Offset) {}
153+
AbsoluteOffsetPosition(AbsoluteSyntaxPosition Position)
154+
: Offset(Position.getOffset()) {}
155+
156+
AbsoluteSyntaxPosition::OffsetType getOffset() const { return Offset; }
157+
158+
/// Return a position that has been advanced by \p Advance UTF-8 bytes.s
159+
AbsoluteOffsetPosition advancedBy(int Advance) {
160+
return AbsoluteOffsetPosition(Offset + Advance);
161+
}
162+
};
163+
164+
/// Various information that enrich a \c RawSyntax node with information on how
165+
/// it's located within the syntax tree.
166+
class AbsoluteSyntaxInfo {
167+
AbsoluteSyntaxPosition Position;
168+
SyntaxIdentifier NodeId;
169+
170+
public:
171+
AbsoluteSyntaxInfo(AbsoluteSyntaxPosition Position, SyntaxIdentifier NodeId)
172+
: Position(Position), NodeId(NodeId) {}
173+
174+
static AbsoluteSyntaxInfo forRoot() {
175+
return AbsoluteSyntaxInfo(AbsoluteSyntaxPosition::forRoot(),
176+
SyntaxIdentifier::newRoot());
177+
}
178+
179+
AbsoluteSyntaxPosition getPosition() const { return Position; }
180+
SyntaxIdentifier getNodeId() const { return NodeId; }
181+
182+
/// Assuming that this info points to the start of \p Raw, advance it so
183+
/// that it points to the next sibling of \p Raw.
184+
AbsoluteSyntaxInfo advancedBy(const RC<RawSyntax> &Raw) const {
185+
auto NewNodeId = NodeId.advancedBy(Raw);
186+
auto NewPosition = Position.advancedBy(Raw);
187+
return AbsoluteSyntaxInfo(NewPosition, NewNodeId);
188+
}
189+
190+
/// Assuming that this info points to the next sibling of \p Raw, reverse
191+
/// it so that it points to the start of \p Raw.
192+
AbsoluteSyntaxInfo reversedBy(const RC<RawSyntax> &Raw) const {
193+
auto NewNodeId = NodeId.reversedBy(Raw);
194+
auto NewPosition = Position.reversedBy(Raw);
195+
return AbsoluteSyntaxInfo(NewPosition, NewNodeId);
196+
}
197+
198+
/// Get the information of the node's first immediate child.
199+
AbsoluteSyntaxInfo advancedToFirstChild() const {
200+
auto NewNodeId = NodeId.advancedToFirstChild();
201+
auto NewPosition = Position.advancedToFirstChild();
202+
return AbsoluteSyntaxInfo(NewPosition, NewNodeId);
203+
}
204+
};
205+
206+
/// A \c RawSyntax node that is enrichted with information of its position
207+
/// within the syntax tree it lives in.
208+
struct AbsoluteRawSyntax {
209+
const RC<RawSyntax> Raw;
210+
const AbsoluteSyntaxInfo Info;
211+
212+
public:
213+
AbsoluteRawSyntax(const RC<RawSyntax> &Raw, AbsoluteSyntaxInfo Info)
214+
: Raw(Raw), Info(Info) {}
215+
216+
/// Construct a \c AbsoluteRawSyntax for a \c RawSyntax node that represents
217+
/// the syntax tree's root.
218+
static AbsoluteRawSyntax forRoot(const RC<RawSyntax> &Raw) {
219+
return AbsoluteRawSyntax(Raw, AbsoluteSyntaxInfo::forRoot());
220+
}
221+
222+
const RC<RawSyntax> &getRaw() const { return Raw; }
223+
224+
AbsoluteSyntaxInfo getInfo() const { return Info; }
225+
226+
/// Get the position at which the leading triva of this node starts.
227+
AbsoluteSyntaxPosition getPosition() const { return Info.getPosition(); };
228+
229+
SyntaxIdentifier getNodeId() const { return Info.getNodeId(); };
230+
231+
AbsoluteSyntaxPosition::IndexInParentType getIndexInParent() const {
232+
return getPosition().getIndexInParent();
233+
}
234+
};
235+
236+
} // end namespace syntax
237+
} // end namespace swift
238+
239+
namespace llvm {
240+
raw_ostream &operator<<(raw_ostream &OS,
241+
swift::syntax::AbsoluteOffsetPosition Pos);
242+
} // end namespace llvm
243+
244+
#endif // SWIFT_SYNTAX_ABSOLUTERAWSYNTAX_H

0 commit comments

Comments
 (0)