Consolidate ParseTree onto PrettyStackTraceFunction (#3002)

The function is already in use by semantics, it's intended to be cleaner
than having to write a dedicated printer class.
This commit is contained in:
Jon Ross-Perkins
2023-07-21 01:09:41 +00:00
committed by GitHub
parent e51f971bce
commit c8e02c14ce
4 changed files with 30 additions and 32 deletions
+3 -32
View File
@@ -11,43 +11,13 @@
#include "common/error.h"
#include "llvm/ADT/Sequence.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "toolchain/common/pretty_stack_trace_function.h"
#include "toolchain/lexer/tokenized_buffer.h"
#include "toolchain/parser/parse_node_kind.h"
#include "toolchain/parser/parser_context.h"
namespace Carbon {
class PrettyStackTraceParserContext : public llvm::PrettyStackTraceEntry {
public:
explicit PrettyStackTraceParserContext(const ParserContext* context)
: context_(context) {}
~PrettyStackTraceParserContext() override = default;
auto print(llvm::raw_ostream& output) const -> void override {
output << "Parser stack:\n";
for (int i = 0; i < static_cast<int>(context_->state_stack().size()); ++i) {
const auto& entry = context_->state_stack()[i];
output << "\t" << i << ".\t" << entry.state;
Print(output, entry.token);
}
output << "\tcursor\tposition_";
Print(output, *context_->position());
}
private:
auto Print(llvm::raw_ostream& output, TokenizedBuffer::Token token) const
-> void {
auto line = context_->tokens().GetLine(token);
output << " @ " << context_->tokens().GetLineNumber(line) << ":"
<< context_->tokens().GetColumnNumber(token) << ":"
<< " token " << token << " : " << context_->tokens().GetKind(token)
<< "\n";
}
const ParserContext* context_;
};
auto ParseTree::Parse(TokenizedBuffer& tokens, DiagnosticConsumer& consumer,
llvm::raw_ostream* vlog_stream) -> ParseTree {
TokenizedBuffer::TokenLocationTranslator translator(
@@ -57,7 +27,8 @@ auto ParseTree::Parse(TokenizedBuffer& tokens, DiagnosticConsumer& consumer,
// Delegate to the parser.
ParseTree tree(tokens);
ParserContext context(tree, tokens, emitter, vlog_stream);
PrettyStackTraceParserContext pretty_context(&context);
PrettyStackTraceFunction context_dumper(
[&](llvm::raw_ostream& output) { context.PrintForStackDump(output); });
context.PushState(ParserState::DeclarationScopeLoop);