Add `global_directive` rule to the WGSL parser.

This CL adds the `global_directive` rule into the WGSL parser and
moves `enable_directive` under that rule. This matches the WGSL
specification.

Bug: tint:1633
Change-Id: I48b809cd1d2f2ffa6ec0d83474c716d4015e1dea
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/98041
Auto-Submit: Dan Sinclair <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@chromium.org>
Reviewed-by: Ben Clayton <bclayton@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
dan sinclair 2022-08-02 21:17:05 +00:00 committed by Dawn LUCI CQ
parent 99fa7b9e7f
commit ce6246502c
2 changed files with 18 additions and 11 deletions

View File

@ -310,7 +310,7 @@ bool ParserImpl::Parse() {
}
// translation_unit
// : enable_directive* global_decl* EOF
// : global_directive* global_decl* EOF
void ParserImpl::translation_unit() {
bool after_global_decl = false;
while (continue_parsing()) {
@ -319,17 +319,9 @@ void ParserImpl::translation_unit() {
break;
}
auto ed = enable_directive();
if (ed.matched) {
if (after_global_decl) {
add_error(p, "enable directives must come before all global declarations");
}
} else if (ed.errored) {
// Found a invalid enable directive.
continue;
} else {
auto ed = global_directive(after_global_decl);
if (!ed.matched && !ed.errored) {
auto gd = global_decl();
if (gd.matched) {
after_global_decl = true;
}
@ -347,6 +339,17 @@ void ParserImpl::translation_unit() {
}
}
// global_directive
// : enable_directive
Maybe<bool> ParserImpl::global_directive(bool have_parsed_decl) {
auto& p = peek();
auto ed = enable_directive();
if (ed.matched && have_parsed_decl) {
return add_error(p, "enable directives must come before all global declarations");
}
return ed;
}
// enable_directive
// : enable name SEMICLON
Maybe<bool> ParserImpl::enable_directive() {

View File

@ -385,6 +385,10 @@ class ParserImpl {
void deprecated(const Source& source, const std::string& msg);
/// Parses the `translation_unit` grammar element
void translation_unit();
/// Parses the `global_directive` grammar element, erroring on parse failure.
/// @param has_parsed_decl flag indicating if the parser has consumed a global declaration.
/// @return true on parse success, otherwise an error or no-match.
Maybe<bool> global_directive(bool has_parsed_decl);
/// Parses the `enable_directive` grammar element, erroring on parse failure.
/// @return true on parse success, otherwise an error or no-match.
Maybe<bool> enable_directive();