Skip to content

Commit

Permalink
css2: Support stringifying parse errors
Browse files Browse the repository at this point in the history
  • Loading branch information
robinlinden committed Jan 12, 2025
1 parent 45940dd commit aa33aab
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
17 changes: 16 additions & 1 deletion css2/tokenizer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021-2024 Robin Lindén <[email protected]>
// SPDX-FileCopyrightText: 2021-2025 Robin Lindén <[email protected]>
// SPDX-FileCopyrightText: 2022 Mikael Larsson <[email protected]>
//
// SPDX-License-Identifier: BSD-2-Clause
Expand Down Expand Up @@ -42,6 +42,21 @@ constexpr bool is_digit(std::optional<char> c) {

} // namespace

std::string_view to_string(ParseError e) {
switch (e) {
case ParseError::EofInComment:
return "EofInComment";
case ParseError::EofInEscapeSequence:
return "EofInEscapeSequence";
case ParseError::EofInString:
return "EofInString";
case ParseError::NewlineInString:
return "NewlineInString";
}

return "Unknown parse error";
}

void Tokenizer::run() {
while (true) {
switch (state_) {
Expand Down
4 changes: 3 additions & 1 deletion css2/tokenizer.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021-2024 Robin Lindén <[email protected]>
// SPDX-FileCopyrightText: 2021-2025 Robin Lindén <[email protected]>
// SPDX-FileCopyrightText: 2022 Mikael Larsson <[email protected]>
//
// SPDX-License-Identifier: BSD-2-Clause
Expand Down Expand Up @@ -39,6 +39,8 @@ enum class ParseError : std::uint8_t {
NewlineInString,
};

std::string_view to_string(ParseError);

class Tokenizer {
public:
Tokenizer(std::string_view input, std::function<void(Token &&)> on_emit, std::function<void(ParseError)> on_error)
Expand Down
19 changes: 18 additions & 1 deletion css2/tokenizer_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021-2024 Robin Lindén <[email protected]>
// SPDX-FileCopyrightText: 2021-2025 Robin Lindén <[email protected]>
// SPDX-FileCopyrightText: 2022 Mikael Larsson <[email protected]>
//
// SPDX-License-Identifier: BSD-2-Clause
Expand Down Expand Up @@ -68,6 +68,23 @@ void expect_error(

int main() {
etest::Suite s{};

s.add_test("to_string(ParseError)", [](etest::IActions &a) {
static constexpr auto kFirstError = ParseError::EofInComment;
static constexpr auto kLastError = ParseError::NewlineInString;

auto error = static_cast<int>(kFirstError);
a.expect_eq(error, 0);

while (error <= static_cast<int>(kLastError)) {
a.expect(to_string(static_cast<ParseError>(error)) != "Unknown parse error",
std::to_string(error) + " is missing an error message");
error += 1;
}

a.expect_eq(to_string(static_cast<ParseError>(error + 1)), "Unknown parse error");
});

s.add_test("delimiter", [](etest::IActions &a) {
auto output = run_tokenizer(a, "?");

Expand Down

0 comments on commit aa33aab

Please sign in to comment.