Skip to content

Commit 4d1249a

Browse files
committed
Revert "libSyntax: Ensure round-trip printing when we build syntax tree from parser incrementally. (#12709)"
This reverts commit 0d98c4c.
1 parent cabb6dd commit 4d1249a

File tree

11 files changed

+124
-261
lines changed

11 files changed

+124
-261
lines changed

include/swift/AST/Module.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ namespace syntax {
8383
class SourceFileSyntax;
8484
class SyntaxParsingContext;
8585
class SyntaxParsingContextRoot;
86-
struct RawSyntaxInfo;
86+
struct RawTokenInfo;
8787
}
8888

8989
/// Discriminator for file-units.
@@ -1092,7 +1092,7 @@ class SourceFile final : public FileUnit {
10921092
Optional<std::vector<Token>> AllCorrectedTokens;
10931093

10941094
/// All of the raw token syntax nodes in the underlying source.
1095-
std::vector<syntax::RawSyntaxInfo> AllRawTokenSyntax;
1095+
std::vector<syntax::RawTokenInfo> AllRawTokenSyntax;
10961096

10971097
SourceFileSyntaxInfo &SyntaxInfo;
10981098

include/swift/Parse/Lexer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ class Lexer {
246246
}
247247

248248
/// Lex a full token including leading and trailing trivia.
249-
syntax::RawSyntaxInfo fullLex();
249+
syntax::RawTokenInfo fullLex();
250250

251251
bool isKeepingComments() const {
252252
return RetainComments == CommentRetentionMode::ReturnAsTokens;

include/swift/Parse/Parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@ tokenizeWithTrivia(const LangOptions &LangOpts,
14291429
void populateTokenSyntaxMap(const LangOptions &LangOpts,
14301430
const SourceManager &SM,
14311431
unsigned BufferID,
1432-
std::vector<syntax::RawSyntaxInfo> &Result);
1432+
std::vector<syntax::RawTokenInfo> &Result);
14331433
} // end namespace swift
14341434

14351435
#endif

include/swift/Syntax/SyntaxParsingContext.h

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,12 @@
2323

2424
namespace swift {
2525
class SourceFile;
26-
class Token;
2726

2827
namespace syntax {
2928

30-
/// The handler for parser to generate libSyntax entities.
31-
struct RawSyntaxInfo {
32-
/// Start location of this syntax node.
33-
SourceLoc StartLoc;
34-
35-
/// The number of tokens belong to the syntax node.
36-
unsigned TokCount;
37-
38-
/// The raw node.
39-
RC<RawSyntax> RawNode;
40-
RawSyntaxInfo(SourceLoc StartLoc, RC<RawSyntax> RawNode):
41-
RawSyntaxInfo(StartLoc, 1, RawNode) {}
42-
RawSyntaxInfo(SourceLoc StartLoc, unsigned TokCount, RC<RawSyntax> RawNode);
43-
template <typename SyntaxNode>
44-
SyntaxNode makeSyntax() const { return make<SyntaxNode>(RawNode); }
45-
46-
template <typename RawSyntaxNode>
47-
RC<RawSyntaxNode> getRaw() const {
48-
return RC<RawSyntaxNode>(cast<RawSyntaxNode>(RawNode));
49-
}
29+
struct RawTokenInfo {
30+
SourceLoc Loc;
31+
RC<RawTokenSyntax> Token;
5032
};
5133

5234
enum class SyntaxParsingContextKind: uint8_t {
@@ -58,7 +40,7 @@ enum class SyntaxParsingContextKind: uint8_t {
5840
/// create syntax nodes.
5941
class SyntaxParsingContext {
6042
protected:
61-
SyntaxParsingContext(SourceFile &SF, unsigned BufferID);
43+
SyntaxParsingContext(bool Enabled);
6244
SyntaxParsingContext(SyntaxParsingContext &Another);
6345
public:
6446
struct ContextInfo;
@@ -84,10 +66,12 @@ class SyntaxParsingContext {
8466
// of all other entity-specific contexts. This is the context Parser
8567
// has when the parser instance is firstly created.
8668
class SyntaxParsingContextRoot: public SyntaxParsingContext {
87-
SourceFile &File;
8869
public:
89-
SyntaxParsingContextRoot(SourceFile &File, unsigned BufferID):
90-
SyntaxParsingContext(File, BufferID), File(File) {}
70+
struct GlobalInfo;
71+
72+
// Contains global information of the source file under parsing.
73+
GlobalInfo &GlobalData;
74+
SyntaxParsingContextRoot(SourceFile &SF, unsigned BufferID);
9175
~SyntaxParsingContextRoot();
9276
void addTokenSyntax(SourceLoc Loc) override {};
9377
void makeNode(SyntaxKind Kind) override {};
@@ -96,23 +80,21 @@ class SyntaxParsingContextRoot: public SyntaxParsingContext {
9680
};
9781
};
9882

99-
enum class SyntaxContextKind: uint8_t{
100-
Expr,
101-
Decl,
102-
};
103-
10483
// The base class for contexts that are created from a parent context.
10584
// The stack instance will set the context holder when the context
10685
// is firstly created and reset the context holder to the parent when
10786
// it's destructed.
10887
class SyntaxParsingContextChild: public SyntaxParsingContext {
10988
SyntaxParsingContext *Parent;
11089
SyntaxParsingContext *&ContextHolder;
111-
const SyntaxContextKind Kind;
112-
Token &Tok;
90+
const SyntaxKind FinalKind;
11391
public:
11492
SyntaxParsingContextChild(SyntaxParsingContext *&ContextHolder,
115-
SyntaxContextKind Kind, Token &Tok);
93+
SyntaxKind FinalKind):
94+
SyntaxParsingContext(*ContextHolder), Parent(ContextHolder),
95+
ContextHolder(ContextHolder), FinalKind(FinalKind) {
96+
ContextHolder = this;
97+
}
11698
~SyntaxParsingContextChild();
11799
void makeNode(SyntaxKind Kind) override;
118100
void addTokenSyntax(SourceLoc Loc) override;

lib/Parse/Lexer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ static bool rangeContainsPlaceholderEnd(const char *CurPtr,
737737
return false;
738738
}
739739

740-
syntax::RawSyntaxInfo Lexer::fullLex() {
740+
syntax::RawTokenInfo Lexer::fullLex() {
741741
if (NextToken.isEscapedIdentifier()) {
742742
LeadingTrivia.push_back(syntax::TriviaPiece::backtick());
743743
TrailingTrivia.insert(TrailingTrivia.begin(),

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#include <algorithm>
3838

3939
using namespace swift;
40-
using namespace syntax;
4140

4241
namespace {
4342
/// A RAII object for deciding whether this DeclKind needs special
@@ -2147,8 +2146,7 @@ void Parser::delayParseFromBeginningToHere(ParserPosition BeginParserPosition,
21472146
ParserResult<Decl>
21482147
Parser::parseDecl(ParseDeclOptions Flags,
21492148
llvm::function_ref<void(Decl*)> Handler) {
2150-
SyntaxParsingContextChild DeclParsingContext(SyntaxContext,
2151-
SyntaxContextKind::Decl, Tok);
2149+
21522150
if (Tok.is(tok::pound_if)) {
21532151
auto IfConfigResult = parseIfConfig(
21542152
[&](SmallVectorImpl<ASTNode> &Decls, bool IsActive) {

lib/Parse/ParseExpr.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ using namespace swift::syntax;
4040
/// \param isExprBasic Whether we're only parsing an expr-basic.
4141
ParserResult<Expr> Parser::parseExprImpl(Diag<> Message, bool isExprBasic) {
4242
// Start a context for creating expression syntax.
43-
SyntaxParsingContextChild ExprParsingContext(SyntaxContext,
44-
SyntaxContextKind::Expr, Tok);
43+
SyntaxParsingContextChild ExprParsingContext(SyntaxContext, SyntaxKind::Expr);
4544

4645
// If we are parsing a refutable pattern, check to see if this is the start
4746
// of a let/var/is pattern. If so, parse it to an UnresolvedPatternExpr and
@@ -1805,8 +1804,7 @@ ParserResult<Expr> Parser::parseExprStringLiteral() {
18051804
// Create a syntax node for string literal.
18061805
SyntaxContext->addTokenSyntax(Tok.getLoc());
18071806
SyntaxContext->makeNode(SyntaxKind::StringLiteralExpr);
1808-
SyntaxParsingContextChild LocalContext(SyntaxContext,
1809-
SyntaxContextKind::Expr, Tok);
1807+
SyntaxParsingContextChild LocalContext(SyntaxContext, SyntaxKind::Expr);
18101808

18111809
// FIXME: Avoid creating syntax nodes for string interpolation.
18121810
LocalContext.disable();

lib/Parse/Parser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ swift::tokenizeWithTrivia(const LangOptions &LangOpts,
268268
syntax::AbsolutePosition>> Tokens;
269269
syntax::AbsolutePosition RunningPos;
270270
do {
271-
auto ThisToken = L.fullLex().getRaw<syntax::RawTokenSyntax>();
271+
auto ThisToken = L.fullLex().Token;
272272
auto ThisTokenPos = ThisToken->accumulateAbsolutePosition(RunningPos);
273273
Tokens.push_back({ThisToken, ThisTokenPos});
274274
} while (Tokens.back().first->isNot(tok::eof));
@@ -279,15 +279,15 @@ swift::tokenizeWithTrivia(const LangOptions &LangOpts,
279279
void swift::populateTokenSyntaxMap(const LangOptions &LangOpts,
280280
const SourceManager &SM,
281281
unsigned BufferID,
282-
std::vector<syntax::RawSyntaxInfo> &Result) {
282+
std::vector<syntax::RawTokenInfo> &Result) {
283283
if (!Result.empty())
284284
return;
285285
Lexer L(LangOpts, SM, BufferID, /*Diags=*/nullptr, /*InSILMode=*/false,
286286
CommentRetentionMode::AttachToNextToken,
287287
TriviaRetentionMode::WithTrivia);
288288
do {
289289
Result.emplace_back(L.fullLex());
290-
if (Result.back().getRaw<syntax::RawTokenSyntax>()->is(tok::eof))
290+
if (Result.back().Token->is(tok::eof))
291291
return;
292292
} while (true);
293293
}

0 commit comments

Comments
 (0)