Skip to content

Commit 51bf7cc

Browse files
authored
Merge pull request swiftlang#21362 from akyrtzi/parserunit-parse
[Parser] Introduce ParserUnit::parse() function to avoid duplication of code
2 parents 4c04a0f + 66be4a5 commit 51bf7cc

File tree

5 files changed

+16
-20
lines changed

5 files changed

+16
-20
lines changed

include/swift/Subsystems.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ namespace swift {
342342

343343
~ParserUnit();
344344

345+
void parse();
346+
345347
Parser &getParser();
346348
SourceFile &getSourceFile();
347349
DiagnosticEngine &getDiagnosticEngine();

lib/IDE/Utils.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,9 @@ ide::isSourceInputComplete(std::unique_ptr<llvm::MemoryBuffer> MemBuf,
104104
SourceManager SM;
105105
auto BufferID = SM.addNewSourceBuffer(std::move(MemBuf));
106106
ParserUnit Parse(SM, SFKind, BufferID);
107-
Parser &P = Parse.getParser();
108-
109-
bool Done;
110-
do {
111-
P.parseTopLevel();
112-
Done = P.Tok.is(tok::eof);
113-
} while (!Done);
114-
107+
Parse.parse();
115108
SourceCompleteResult SCR;
116-
SCR.IsComplete = !P.isInputIncomplete();
109+
SCR.IsComplete = !Parse.getParser().isInputIncomplete();
117110

118111
// Use the same code that was in the REPL code to track the indent level
119112
// for now. In the future we should get this from the Parser if possible.

lib/Parse/Parser.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,16 @@ ParserUnit::~ParserUnit() {
10981098
delete &Impl;
10991099
}
11001100

1101+
void ParserUnit::parse() {
1102+
auto &P = getParser();
1103+
bool Done = false;
1104+
while (!Done) {
1105+
P.parseTopLevel();
1106+
Done = P.Tok.is(tok::eof);
1107+
}
1108+
P.finalizeSyntaxTree();
1109+
}
1110+
11011111
Parser &ParserUnit::getParser() {
11021112
return *Impl.TheParser;
11031113
}

tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -698,13 +698,7 @@ class SwiftDocumentSyntaxInfo {
698698
}
699699

700700
void parse() {
701-
auto &P = Parser->getParser();
702-
bool Done = false;
703-
while (!Done) {
704-
P.parseTopLevel();
705-
Done = P.Tok.is(tok::eof);
706-
}
707-
P.finalizeSyntaxTree();
701+
Parser->parse();
708702
}
709703

710704
SourceFile &getSourceFile() {

tools/driver/swift_format_main.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ class FormatterDocument {
6565
BufferID, CompInv.getLangOptions(),
6666
CompInv.getModuleName()));
6767
Parser->getDiagnosticEngine().addConsumer(DiagConsumer);
68-
auto &P = Parser->getParser();
69-
for (bool Done = false; !Done; Done = P.Tok.is(tok::eof)) {
70-
P.parseTopLevel();
71-
}
68+
Parser->parse();
7269
}
7370

7471
std::pair<LineRange, std::string> reformat(LineRange Range,

0 commit comments

Comments
 (0)