Skip to content

Commit 3708a6c

Browse files
committed
Parser: Improve UnexpectedAttribute error message
1 parent d77d16b commit 3708a6c

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

include/NZSL/Lang/ErrorList.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ NZSL_SHADERLANG_PARSER_ERROR(ModuleFeatureMultipleUnique, "module feature {} has
4848
NZSL_SHADERLANG_PARSER_ERROR(ReservedKeyword, "reserved keyword")
4949
NZSL_SHADERLANG_PARSER_ERROR(UnknownAttribute, "unknown attribute \"{}\"", std::string)
5050
NZSL_SHADERLANG_PARSER_ERROR(UnknownType, "unknown type")
51-
NZSL_SHADERLANG_PARSER_ERROR(UnexpectedAttribute, "unexpected attribute {}", Ast::AttributeType)
51+
NZSL_SHADERLANG_PARSER_ERROR(UnexpectedAttribute, "unexpected attribute {} on {}", Ast::AttributeType, std::string)
5252
NZSL_SHADERLANG_PARSER_ERROR(UnexpectedEndOfFile, "unexpected end of file")
5353
NZSL_SHADERLANG_PARSER_ERROR(UnexpectedToken, "unexpected token {}", TokenType)
5454

src/NZSL/Parser.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ namespace nzsl
377377
}
378378

379379
default:
380-
throw ParserUnexpectedAttributeError{ attribute.sourceLocation, attribute.type };
380+
throw ParserUnexpectedAttributeError{ attribute.sourceLocation, attribute.type, "module statement" };
381381
}
382382
}
383383

@@ -615,7 +615,7 @@ namespace nzsl
615615
break;
616616

617617
default:
618-
throw ParserUnexpectedAttributeError{ attribute.sourceLocation, attribute.type };
618+
throw ParserUnexpectedAttributeError{ attribute.sourceLocation, attribute.type, "const declaration" };
619619
}
620620
}
621621

@@ -712,7 +712,7 @@ namespace nzsl
712712
break;
713713

714714
default:
715-
throw ParserUnexpectedAttributeError{ attribute.sourceLocation, attribute.type };
715+
throw ParserUnexpectedAttributeError{ attribute.sourceLocation, attribute.type, "external block" };
716716
}
717717
}
718718

@@ -761,7 +761,7 @@ namespace nzsl
761761
break;
762762

763763
default:
764-
throw ParserUnexpectedAttributeError{ attribute.sourceLocation, attribute.type };
764+
throw ParserUnexpectedAttributeError{ attribute.sourceLocation, attribute.type, "external variable" };
765765
}
766766
}
767767
}
@@ -824,7 +824,7 @@ namespace nzsl
824824
break;
825825

826826
default:
827-
throw ParserUnexpectedAttributeError{ attribute.sourceLocation, attribute.type };
827+
throw ParserUnexpectedAttributeError{ attribute.sourceLocation, attribute.type, "for loop" };
828828
}
829829
}
830830

@@ -848,7 +848,7 @@ namespace nzsl
848848
break;
849849

850850
default:
851-
throw ParserUnexpectedAttributeError{ attribute.sourceLocation, attribute.type };
851+
throw ParserUnexpectedAttributeError{ attribute.sourceLocation, attribute.type, "foreach loop" };
852852
}
853853
}
854854

@@ -952,7 +952,7 @@ namespace nzsl
952952
}
953953

954954
default:
955-
throw ParserUnexpectedAttributeError{ attribute.sourceLocation, attribute.type };
955+
throw ParserUnexpectedAttributeError{ attribute.sourceLocation, attribute.type, "function declaration" };
956956
}
957957
}
958958

@@ -1070,7 +1070,7 @@ namespace nzsl
10701070
{
10711071
case TokenType::Alias:
10721072
if (!attributes.empty())
1073-
throw ParserUnexpectedAttributeError{ attributes.front().sourceLocation, attributes.front().type };
1073+
throw ParserUnexpectedAttributeError{ attributes.front().sourceLocation, attributes.front().type, "alias declaration" };
10741074

10751075
return ParseAliasDeclaration();
10761076

@@ -1086,9 +1086,12 @@ namespace nzsl
10861086
case TokenType::External:
10871087
return ParseExternalBlock(std::move(attributes));
10881088

1089+
case TokenType::FunctionDeclaration:
1090+
return ParseFunctionDeclaration(std::move(attributes));
1091+
10891092
case TokenType::Import:
10901093
if (!attributes.empty())
1091-
throw ParserUnexpectedAttributeError{ attributes.front().sourceLocation, attributes.front().type };
1094+
throw ParserUnexpectedAttributeError{ attributes.front().sourceLocation, attributes.front().type, "import statement"};
10921095

10931096
return ParseImportStatement();
10941097

@@ -1103,14 +1106,11 @@ namespace nzsl
11031106
case TokenType::Option:
11041107
{
11051108
if (!attributes.empty())
1106-
throw ParserUnexpectedAttributeError{ attributes.front().sourceLocation, attributes.front().type };
1109+
throw ParserUnexpectedAttributeError{ attributes.front().sourceLocation, attributes.front().type, "option declaration" };
11071110

11081111
return ParseOptionDeclaration();
11091112
}
11101113

1111-
case TokenType::FunctionDeclaration:
1112-
return ParseFunctionDeclaration(std::move(attributes));
1113-
11141114
case TokenType::Struct:
11151115
return ParseStructDeclaration(std::move(attributes));
11161116

@@ -1278,7 +1278,7 @@ namespace nzsl
12781278
break;
12791279

12801280
default:
1281-
throw ParserUnexpectedAttributeError{ attribute.sourceLocation, attribute.type };
1281+
throw ParserUnexpectedAttributeError{ attribute.sourceLocation, attribute.type, "struct declaration" };
12821282
}
12831283
}
12841284

@@ -1334,7 +1334,7 @@ namespace nzsl
13341334
break;
13351335

13361336
default:
1337-
throw ParserUnexpectedAttributeError{ attribute.sourceLocation, attribute.type };
1337+
throw ParserUnexpectedAttributeError{ attribute.sourceLocation, attribute.type, "struct member" };
13381338
}
13391339
}
13401340
}
@@ -1406,7 +1406,7 @@ namespace nzsl
14061406
break;
14071407

14081408
default:
1409-
throw ParserUnexpectedAttributeError{ attribute.sourceLocation, attribute.type };
1409+
throw ParserUnexpectedAttributeError{ attribute.sourceLocation, attribute.type, "while loop" };
14101410
}
14111411
}
14121412

tests/src/Tests/ErrorsTests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ module;
7070
7171
[cond(false)]
7272
alias vec3f32 = vec3[f32];
73-
)"), "(5,2 -> 12): PUnexpectedAttribute error: unexpected attribute cond");
73+
)"), "(5,2 -> 12): PUnexpectedAttribute error: unexpected attribute cond on alias declaration");
7474

7575
// import statements don't support cond attribute
7676
CHECK_THROWS_WITH(nzsl::Parse(R"(
@@ -79,7 +79,7 @@ module;
7979
8080
[cond(true)]
8181
import Stuff;
82-
)"), "(5,2 -> 11): PUnexpectedAttribute error: unexpected attribute cond");
82+
)"), "(5,2 -> 11): PUnexpectedAttribute error: unexpected attribute cond on import statement");
8383

8484
// option statements don't support attributes
8585
CHECK_THROWS_WITH(nzsl::Parse(R"(
@@ -88,7 +88,7 @@ module;
8888
8989
[cond(false)]
9090
option enable: bool;
91-
)"), "(5,2 -> 12): PUnexpectedAttribute error: unexpected attribute cond");
91+
)"), "(5,2 -> 12): PUnexpectedAttribute error: unexpected attribute cond on option declaration");
9292
}
9393

9494
SECTION("Checking compiler errors")

0 commit comments

Comments
 (0)