-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
css2: Support stringifying parse errors
- Loading branch information
1 parent
45940dd
commit aa33aab
Showing
3 changed files
with
37 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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_) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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, "?"); | ||
|
||
|