Skip to content

Commit 803ba0e

Browse files
committed
[Parser] Introduce PersistentParserState::ParserPos to encapsulate a Parser/Lexer independent info for restoring parsing from a certain token location.
Swift SVN r6593
1 parent cfd3e09 commit 803ba0e

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

include/swift/Parse/Parser.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,9 @@ class Parser {
161161
PreviousLoc);
162162
}
163163

164-
ParserPosition getParserPosition(SourceLoc Start, SourceLoc PrevLoc) {
165-
return ParserPosition(L->getStateForBeginningOfTokenLoc(Start), PrevLoc);
164+
ParserPosition getParserPosition(const PersistentParserState::ParserPos &Pos){
165+
return ParserPosition(L->getStateForBeginningOfTokenLoc(Pos.Loc),
166+
Pos.PrevLoc);
166167
}
167168

168169
void restoreParserPosition(ParserPosition PP) {

include/swift/Parse/PersistentParserState.h

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,16 @@ namespace swift {
2626

2727
/// \brief Parser state persistent across multiple parses.
2828
class PersistentParserState {
29+
public:
30+
struct ParserPos {
31+
SourceLoc Loc;
32+
SourceLoc PrevLoc;
33+
34+
bool isValid() const { return Loc.isValid(); }
35+
};
2936

3037
class FunctionBodyState {
31-
SourceRange BodyRange;
32-
SourceLoc PreviousLoc;
38+
ParserPos BodyPos;
3339
SavedScope Scope;
3440
friend class Parser;
3541

@@ -40,16 +46,17 @@ class PersistentParserState {
4046
public:
4147
FunctionBodyState(SourceRange BodyRange, SourceLoc PreviousLoc,
4248
SavedScope &&Scope)
43-
: BodyRange(BodyRange), PreviousLoc(PreviousLoc), Scope(std::move(Scope))
49+
: BodyPos{BodyRange.Start, PreviousLoc}, Scope(std::move(Scope))
4450
{}
4551
};
4652

53+
private:
4754
ScopeInfo ScopeInfo;
4855
typedef llvm::DenseMap<FuncExpr *, std::unique_ptr<FunctionBodyState>>
4956
DelayedBodiesTy;
5057
DelayedBodiesTy DelayedBodies;
51-
SourceLoc ParserPos;
52-
SourceLoc PrevParserLoc;
58+
/// \brief Parser sets this if it stopped parsing before the buffer ended.
59+
ParserPos MarkedPos;
5360

5461
public:
5562
swift::ScopeInfo &getScopeInfo() { return ScopeInfo; }
@@ -59,14 +66,13 @@ class PersistentParserState {
5966
std::unique_ptr<FunctionBodyState> takeBodyState(FuncExpr *FE);
6067

6168
void markParserPosition(SourceLoc Loc, SourceLoc PrevLoc) {
62-
ParserPos = Loc;
63-
PrevParserLoc = PrevLoc;
69+
MarkedPos = {Loc, PrevLoc};
6470
}
6571

66-
std::pair<SourceLoc, SourceLoc> takeParserPosition() {
67-
auto Pos = std::make_pair(ParserPos, PrevParserLoc);
68-
ParserPos = SourceLoc();
69-
PrevParserLoc = SourceLoc();
72+
/// \brief Returns the marked parser position and resets it.
73+
ParserPos takeParserPosition() {
74+
ParserPos Pos = MarkedPos;
75+
MarkedPos = ParserPos();
7076
return Pos;
7177
}
7278
};

lib/Parse/ParseDecl.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,11 +1377,8 @@ bool Parser::parseDeclFuncBodyDelayed(FuncDecl *FD) {
13771377
if (!FunctionParserState)
13781378
return false;
13791379

1380-
auto BeginParserPosition =
1381-
getParserPosition(FunctionParserState->BodyRange.Start,
1382-
FunctionParserState->PreviousLoc);
1383-
auto EndLexerState =
1384-
L->getStateForBeginningOfTokenLoc(FunctionParserState->BodyRange.End);
1380+
auto BeginParserPosition = getParserPosition(FunctionParserState->BodyPos);
1381+
auto EndLexerState = L->getStateForBeginningOfTokenLoc(FE->getEndLoc());
13851382

13861383
// Ensure that we restore the parser state at exit.
13871384
ParserPositionRAII PPR(*this);

lib/Parse/Parser.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,9 @@ Parser::Parser(unsigned BufferID, TranslationUnit *TU,
229229
TU, TU->getASTContext().Diags, SIL, PersistentState) {
230230
this->IsMainModule = IsMainModule;
231231
auto ParserPos = State->takeParserPosition();
232-
if (ParserPos.first.isValid() &&
233-
SourceMgr.FindBufferContainingLoc(ParserPos.first.Value)== int(BufferID)){
234-
auto BeginParserPosition =
235-
getParserPosition(ParserPos.first, ParserPos.second);
232+
if (ParserPos.isValid() &&
233+
SourceMgr.FindBufferContainingLoc(ParserPos.Loc.Value) == int(BufferID)) {
234+
auto BeginParserPosition = getParserPosition(ParserPos);
236235
restoreParserPosition(BeginParserPosition);
237236
}
238237
}

0 commit comments

Comments
 (0)