Skip to content

Commit 0625562

Browse files
committed
feat: Add APIs to stop parsing
Signed-off-by: Roberto Raggi <[email protected]>
1 parent f3b9b98 commit 0625562

File tree

7 files changed

+67
-49
lines changed

7 files changed

+67
-49
lines changed

src/lsp/cxx/lsp/cxx_document.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ void CxxDocument::Private::configure() {
172172
CxxDocument::CxxDocument(const CLI& cli, long version)
173173
: d(std::make_unique<Private>(cli, version)) {}
174174

175-
void CxxDocument::parse(std::string source, std::string fileName) {
175+
void CxxDocument::parse(std::string source, std::string fileName,
176+
std::function<bool()> stopParsingPredicate) {
176177
d->configure();
177178

178179
auto& unit = d->unit;
@@ -188,6 +189,7 @@ void CxxDocument::parse(std::string source, std::string fileName) {
188189
.staticAssert = cli.opt_fstatic_assert || cli.opt_fcheck,
189190
.reflect = !cli.opt_fno_reflect,
190191
.templates = cli.opt_ftemplates,
192+
.stopParsingPredicate = std::move(stopParsingPredicate),
191193
});
192194
}
193195

src/lsp/cxx/lsp/cxx_document.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class CxxDocument {
3434
explicit CxxDocument(const CLI& cli, long version);
3535
~CxxDocument();
3636

37-
void parse(std::string source, std::string fileName);
37+
void parse(std::string source, std::string fileName,
38+
std::function<bool()> stopParsingPredicate = {});
3839

3940
[[nodiscard]] auto version() const -> long;
4041
[[nodiscard]] auto diagnostics() const -> Vector<Diagnostic>;

src/parser/cxx/parser.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,12 @@ auto Parser::expect(TokenKind tk, SourceLocation& location) -> bool {
869869

870870
void Parser::operator()(UnitAST*& ast) { parse(ast); }
871871

872+
auto Parser::config() const -> const ParserConfiguration& { return config_; }
873+
874+
void Parser::setConfig(ParserConfiguration config) {
875+
config_ = std::move(config);
876+
}
877+
872878
void Parser::parse(UnitAST*& ast) { parse_translation_unit(ast); }
873879

874880
void Parser::parse_warn(std::string message) {
@@ -1211,6 +1217,8 @@ void Parser::parse_top_level_declaration_seq(UnitAST*& yyast) {
12111217
LoopParser loop(this);
12121218

12131219
while (LA()) {
1220+
if (shouldStopParsing()) break;
1221+
12141222
loop.start();
12151223

12161224
DeclarationAST* declaration = nullptr;
@@ -1233,6 +1241,8 @@ void Parser::parse_declaration_seq(List<DeclarationAST*>*& yyast) {
12331241
LoopParser loop(this);
12341242

12351243
while (LA()) {
1244+
if (shouldStopParsing()) break;
1245+
12361246
if (lookat(TokenKind::T_RBRACE)) break;
12371247

12381248
if (parse_maybe_module()) break;
@@ -8111,6 +8121,8 @@ void Parser::parse_namespace_body(NamespaceDefinitionAST* yyast) {
81118121
LoopParser loop{this};
81128122

81138123
while (LA()) {
8124+
if (shouldStopParsing()) break;
8125+
81148126
if (lookat(TokenKind::T_RBRACE)) break;
81158127

81168128
loop.start();
@@ -9160,6 +9172,8 @@ void Parser::parse_class_body(List<DeclarationAST*>*& yyast) {
91609172
LoopParser loop{this};
91619173

91629174
while (LA()) {
9175+
if (shouldStopParsing()) break;
9176+
91639177
if (lookat(TokenKind::T_RBRACE)) break;
91649178

91659179
loop.start();

src/parser/cxx/parser.h

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <algorithm>
3131
#include <deque>
32+
#include <functional>
3233
#include <optional>
3334
#include <unordered_map>
3435
#include <unordered_set>
@@ -54,26 +55,6 @@ class Parser final {
5455

5556
[[nodiscard]] auto control() const -> Control* { return control_; }
5657

57-
[[nodiscard]] auto config() const -> const ParserConfiguration& {
58-
return config_;
59-
}
60-
61-
void setConfig(const ParserConfiguration& config) { config_ = config; }
62-
63-
/**
64-
* Whether to enable fuzzy template resolution.
65-
*/
66-
[[nodiscard]] auto fuzzyTemplateResolution() const -> bool;
67-
68-
/**
69-
* Sets whether to enable fuzzy template resolution.
70-
*
71-
* When enabled, the parser will try to resolve template names
72-
*
73-
* @param fuzzyTemplateResolution whether to enable fuzzy template resolution
74-
*/
75-
void setFuzzyTemplateResolution(bool fuzzyTemplateResolution);
76-
7758
/**
7859
* Parse the given unit.
7960
*/
@@ -84,6 +65,9 @@ class Parser final {
8465
*/
8566
void operator()(UnitAST*& ast);
8667

68+
[[nodiscard]] auto config() const -> const ParserConfiguration&;
69+
void setConfig(ParserConfiguration config);
70+
8771
private:
8872
struct DeclSpecs;
8973
struct Decl;
@@ -123,6 +107,11 @@ class Parser final {
123107

124108
static auto prec(TokenKind tk) -> Prec;
125109

110+
[[nodiscard]] auto shouldStopParsing() const -> bool {
111+
if (config_.stopParsingPredicate) return config_.stopParsingPredicate();
112+
return false;
113+
}
114+
126115
[[nodiscard]] auto context_allows_function_definition(
127116
BindingContext ctx) const -> bool {
128117
if (ctx == BindingContext::kBlock) return false;

src/parser/cxx/parser_fwd.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#pragma once
2323

24+
#include <functional>
25+
2426
namespace cxx {
2527

2628
class Parser;
@@ -31,6 +33,7 @@ struct ParserConfiguration {
3133
bool staticAssert = false;
3234
bool reflect = true;
3335
bool templates = false;
36+
std::function<bool()> stopParsingPredicate;
3437
};
3538

3639
} // namespace cxx

src/parser/cxx/translation_unit.cc

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,27 @@ void TranslationUnit::endPreprocessing() {
9696
preprocessor_->squeeze();
9797
}
9898

99+
auto TranslationUnit::fatalErrors() const -> bool {
100+
return diagnosticsClient_->fatalErrors();
101+
}
102+
103+
void TranslationUnit::setFatalErrors(bool fatalErrors) {
104+
diagnosticsClient_->setFatalErrors(fatalErrors);
105+
}
106+
107+
auto TranslationUnit::blockErrors(bool blockErrors) -> bool {
108+
return diagnosticsClient_->blockErrors(blockErrors);
109+
}
110+
111+
void TranslationUnit::error(SourceLocation loc, std::string message) const {
112+
diagnosticsClient_->report(tokenAt(loc), Severity::Error, std::move(message));
113+
}
114+
115+
void TranslationUnit::warning(SourceLocation loc, std::string message) const {
116+
TranslationUnit::diagnosticsClient_->report(tokenAt(loc), Severity::Warning,
117+
std::move(message));
118+
}
119+
99120
auto TranslationUnit::tokenLength(SourceLocation loc) const -> int {
100121
const auto& tk = tokenAt(loc);
101122
if (tk.kind() == TokenKind::T_IDENTIFIER) {
@@ -143,13 +164,13 @@ auto TranslationUnit::tokenEndPosition(SourceLocation loc) const
143164
return preprocessor_->tokenEndPosition(tokenAt(loc));
144165
}
145166

146-
void TranslationUnit::parse(const ParserConfiguration& config) {
167+
void TranslationUnit::parse(ParserConfiguration config) {
147168
if (ast_) {
148169
cxx_runtime_error("translation unit already parsed");
149170
}
150171

151172
Parser parse(this);
152-
parse.setConfig(config);
173+
parse.setConfig(std::move(config));
153174
parse(ast_);
154175
}
155176

@@ -158,6 +179,10 @@ auto TranslationUnit::globalScope() const -> Scope* {
158179
return globalNamespace_->scope();
159180
}
160181

182+
auto TranslationUnit::fileName() const -> const std::string& {
183+
return fileName_;
184+
}
185+
161186
auto TranslationUnit::load(std::span<const std::uint8_t> data) -> bool {
162187
#ifndef CXX_NO_FLATBUFFERS
163188
ASTDecoder decode{this};

src/parser/cxx/translation_unit.h

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ class TranslationUnit {
6060

6161
[[nodiscard]] auto globalScope() const -> Scope*;
6262

63-
[[nodiscard]] auto fileName() const -> const std::string& {
64-
return fileName_;
65-
}
63+
[[nodiscard]] auto fileName() const -> const std::string&;
6664

6765
[[nodiscard]] auto preprocessor() const -> Preprocessor* {
6866
return preprocessor_.get();
6967
}
7068

69+
void parse(ParserConfiguration config = {});
70+
7171
// set source and preprocess, deprecated.
7272
void setSource(std::string source, std::string fileName);
7373

@@ -77,27 +77,13 @@ class TranslationUnit {
7777

7878
void endPreprocessing();
7979

80-
[[nodiscard]] auto fatalErrors() const -> bool {
81-
return diagnosticsClient_->fatalErrors();
82-
}
83-
84-
void setFatalErrors(bool fatalErrors) {
85-
diagnosticsClient_->setFatalErrors(fatalErrors);
86-
}
87-
88-
auto blockErrors(bool blockErrors = true) -> bool {
89-
return diagnosticsClient_->blockErrors(blockErrors);
90-
}
80+
[[nodiscard]] auto fatalErrors() const -> bool;
81+
void setFatalErrors(bool fatalErrors);
9182

92-
void error(SourceLocation loc, std::string message) const {
93-
diagnosticsClient_->report(tokenAt(loc), Severity::Error,
94-
std::move(message));
95-
}
83+
auto blockErrors(bool blockErrors = true) -> bool;
9684

97-
void warning(SourceLocation loc, std::string message) const {
98-
diagnosticsClient_->report(tokenAt(loc), Severity::Warning,
99-
std::move(message));
100-
}
85+
void error(SourceLocation loc, std::string message) const;
86+
void warning(SourceLocation loc, std::string message) const;
10187

10288
// tokens
10389
[[nodiscard]] inline auto tokenCount() const -> unsigned {
@@ -134,8 +120,6 @@ class TranslationUnit {
134120

135121
[[nodiscard]] auto literal(SourceLocation loc) const -> const Literal*;
136122

137-
void parse(const ParserConfiguration& config = {});
138-
139123
[[nodiscard]] auto load(std::span<const std::uint8_t> data) -> bool;
140124

141125
[[nodiscard]] auto serialize(std::ostream& out) -> bool;
@@ -146,13 +130,13 @@ class TranslationUnit {
146130
private:
147131
std::unique_ptr<Control> control_;
148132
std::unique_ptr<Arena> arena_;
133+
std::unique_ptr<Preprocessor> preprocessor_;
149134
std::vector<Token> tokens_;
150135
std::string fileName_;
151136
UnitAST* ast_ = nullptr;
152137
const char* yyptr = nullptr;
153138
DiagnosticsClient* diagnosticsClient_ = nullptr;
154139
NamespaceSymbol* globalNamespace_ = nullptr;
155-
std::unique_ptr<Preprocessor> preprocessor_;
156140
};
157141

158142
} // namespace cxx

0 commit comments

Comments
 (0)