Use the new diagnostics for wgsl the parser

Keep the `ParserImpl::error()` format identical to the old error style for now.
Use the explicit `ParserImpl::diagnostics()` method for the error message tests, updating the tests to match the new, improved output.

Bug: tint:282
Change-Id: Ia7e1237170f0f5203a8cfa256322df29e90e2791
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/31481
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2020-11-02 15:56:28 +00:00 committed by Commit Bot service account
parent 0a06243a70
commit 3d54f13613
3 changed files with 693 additions and 291 deletions

View File

@ -132,27 +132,15 @@ ParserImpl::~ParserImpl() {
}
void ParserImpl::set_error(const Token& t, const std::string& err) {
auto prefix = std::to_string(t.source().range.begin.line) + ":" +
std::to_string(t.source().range.begin.column) + ": ";
if (t.IsReservedKeyword()) {
error_ = prefix + "reserved token (" + t.to_str() + ") found";
return;
}
if (t.IsError()) {
error_ = prefix + t.to_str();
return;
}
if (err.size() != 0) {
error_ = prefix + err;
} else {
error_ = prefix + "invalid token (" + t.to_name() + ") encountered";
}
diag::Diagnostic diagnostic;
diagnostic.severity = diag::Severity::Error;
diagnostic.message = err;
diagnostic.source = t.source();
diags_.add(std::move(diagnostic));
}
void ParserImpl::set_error(const Token& t) {
set_error(t, "");
set_error(t, "invalid token (" + t.to_name() + ") encountered");
}
Token ParserImpl::next() {

View File

@ -45,6 +45,8 @@
#include "src/ast/variable.h"
#include "src/ast/variable_decoration.h"
#include "src/context.h"
#include "src/diagnostic/diagnostic.h"
#include "src/diagnostic/formatter.h"
#include "src/reader/wgsl/token.h"
namespace tint {
@ -99,9 +101,15 @@ class ParserImpl {
bool Parse();
/// @returns true if an error was encountered.
bool has_error() const { return error_.size() > 0; }
bool has_error() const { return diags_.contains_errors(); }
/// @returns the parser error string
const std::string& error() const { return error_; }
std::string error() const {
return diag::Formatter::create(false, false, false)->format(diags_);
}
/// @returns the diagnostic messages
const diag::List& diagnostics() const { return diags_; }
/// @returns the module. The module in the parser will be reset after this.
ast::Module module() { return std::move(module_); }
@ -418,7 +426,7 @@ class ParserImpl {
uint32_t depth);
Context& ctx_;
std::string error_;
diag::List diags_;
std::unique_ptr<Lexer> lexer_;
std::deque<Token> token_queue_;
std::unordered_map<std::string, ast::type::Type*> registered_constructs_;

File diff suppressed because it is too large Load Diff