Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
172 lines
6.5 KiB
C++
172 lines
6.5 KiB
C++
#include <Void/Text/Tokenizer.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <string>
|
|
#include <variant>
|
|
#include <vector>
|
|
|
|
using namespace Void::Tokenizer;
|
|
|
|
// ── helpers ───────────────────────────────────────────────────────────────────
|
|
|
|
static Tokenizer make_expr_tokenizer()
|
|
{
|
|
Tokenizer t;
|
|
t.add_rule({"NUMBER", integer_literal()});
|
|
t.add_rule({"IDENT", identifier()});
|
|
t.add_rule({"PLUS", literal("+")});
|
|
t.add_rule({"MINUS", literal("-")});
|
|
t.add_rule({"STAR", literal("*")});
|
|
t.add_rule({"SLASH", literal("/")});
|
|
t.add_rule({"LPAREN", literal("(")});
|
|
t.add_rule({"RPAREN", literal(")")});
|
|
t.skip("WS", whitespace());
|
|
return t;
|
|
}
|
|
|
|
// ── literal matching ──────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test tokenizer_empty_input()
|
|
{
|
|
const Tokenizer t;
|
|
const auto result = t.tokenize("");
|
|
const auto* tokens = std::get_if<std::vector<Token>>(&result);
|
|
Void::Testing::expect(tokens != nullptr);
|
|
Void::Testing::expect(tokens->empty());
|
|
}
|
|
|
|
Void::Testing::Test tokenizer_single_token()
|
|
{
|
|
Tokenizer t;
|
|
t.add_rule({"WORD", identifier()});
|
|
const auto tokens = t.tokenize_or_throw("hello");
|
|
Void::Testing::expect_eq(tokens.size(), std::size_t{1});
|
|
Void::Testing::expect_eq(tokens[0].kind, std::string{"WORD"});
|
|
Void::Testing::expect_eq(tokens[0].value, std::string{"hello"});
|
|
}
|
|
|
|
Void::Testing::Test tokenizer_multiple_tokens()
|
|
{
|
|
const auto t = make_expr_tokenizer();
|
|
const auto tokens = t.tokenize_or_throw("x + 42");
|
|
Void::Testing::expect_eq(tokens.size(), std::size_t{3});
|
|
Void::Testing::expect_eq(tokens[0].kind, std::string{"IDENT"});
|
|
Void::Testing::expect_eq(tokens[1].kind, std::string{"PLUS"});
|
|
Void::Testing::expect_eq(tokens[2].kind, std::string{"NUMBER"});
|
|
Void::Testing::expect_eq(tokens[2].value, std::string{"42"});
|
|
}
|
|
|
|
Void::Testing::Test tokenizer_whitespace_skipped()
|
|
{
|
|
const auto t = make_expr_tokenizer();
|
|
const auto tokens = t.tokenize_or_throw(" 1 + 2 ");
|
|
Void::Testing::expect_eq(tokens.size(), std::size_t{3});
|
|
}
|
|
|
|
Void::Testing::Test tokenizer_no_whitespace_skip()
|
|
{
|
|
Tokenizer t;
|
|
t.add_rule({"NUM", digit()});
|
|
// no WS rule — a space triggers an error
|
|
const auto result = t.tokenize("1 2");
|
|
Void::Testing::expect(std::holds_alternative<Tokenizer::Error>(result));
|
|
}
|
|
|
|
// ── line/col tracking ─────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test tokenizer_line_col_first_token()
|
|
{
|
|
Tokenizer t;
|
|
t.add_rule({"ID", identifier()});
|
|
const auto tokens = t.tokenize_or_throw("foo");
|
|
Void::Testing::expect_eq(tokens[0].line, std::size_t{1});
|
|
Void::Testing::expect_eq(tokens[0].col, std::size_t{1});
|
|
}
|
|
|
|
Void::Testing::Test tokenizer_line_col_after_newline()
|
|
{
|
|
Tokenizer t;
|
|
t.add_rule({"ID", identifier()});
|
|
t.skip("NL", literal("\n"));
|
|
t.skip("WS", whitespace());
|
|
const auto tokens = t.tokenize_or_throw("foo\nbar");
|
|
Void::Testing::expect_eq(tokens.size(), std::size_t{2});
|
|
Void::Testing::expect_eq(tokens[1].line, std::size_t{2});
|
|
Void::Testing::expect_eq(tokens[1].col, std::size_t{1});
|
|
}
|
|
|
|
// ── unrecognized character ────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test tokenizer_error_on_unknown()
|
|
{
|
|
Tokenizer t;
|
|
t.add_rule({"NUM", digit()});
|
|
const auto result = t.tokenize("@");
|
|
const auto* err = std::get_if<Tokenizer::Error>(&result);
|
|
Void::Testing::expect(err != nullptr);
|
|
Void::Testing::expect_eq(err->unexpected, '@');
|
|
Void::Testing::expect_eq(err->line, std::size_t{1});
|
|
}
|
|
|
|
// ── built-in matchers ─────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test tokenizer_identifier_rule()
|
|
{
|
|
Tokenizer t;
|
|
t.add_rule({"ID", identifier()});
|
|
const auto tokens = t.tokenize_or_throw("_foo123");
|
|
Void::Testing::expect_eq(tokens[0].value, std::string{"_foo123"});
|
|
}
|
|
|
|
Void::Testing::Test tokenizer_integer_literal_rule()
|
|
{
|
|
Tokenizer t;
|
|
t.add_rule({"NUM", integer_literal()});
|
|
t.skip("WS", whitespace());
|
|
const auto tokens = t.tokenize_or_throw("42 100 0");
|
|
Void::Testing::expect_eq(tokens.size(), std::size_t{3});
|
|
Void::Testing::expect_eq(tokens[1].value, std::string{"100"});
|
|
}
|
|
|
|
Void::Testing::Test tokenizer_quoted_string_rule()
|
|
{
|
|
Tokenizer t;
|
|
t.add_rule({"STR", quoted_string()});
|
|
const auto tokens = t.tokenize_or_throw("\"hello world\"");
|
|
Void::Testing::expect_eq(tokens.size(), std::size_t{1});
|
|
Void::Testing::expect_eq(tokens[0].value, std::string{"\"hello world\""});
|
|
}
|
|
|
|
Void::Testing::Test tokenizer_quoted_string_with_escape()
|
|
{
|
|
Tokenizer t;
|
|
t.add_rule({"STR", quoted_string()});
|
|
const auto tokens = t.tokenize_or_throw("\"he\\\"llo\"");
|
|
Void::Testing::expect_eq(tokens.size(), std::size_t{1});
|
|
}
|
|
|
|
// ── greedy first-match precedence ─────────────────────────────────────────────
|
|
|
|
Void::Testing::Test tokenizer_keyword_over_ident()
|
|
{
|
|
Tokenizer t;
|
|
t.add_rule({"IF", literal("if")}); // keywords before identifiers
|
|
t.add_rule({"IDENT", identifier()});
|
|
t.skip("WS", whitespace());
|
|
const auto tokens = t.tokenize_or_throw("if foo");
|
|
Void::Testing::expect_eq(tokens[0].kind, std::string{"IF"});
|
|
Void::Testing::expect_eq(tokens[1].kind, std::string{"IDENT"});
|
|
}
|
|
|
|
// ── expression tokenization ───────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test tokenizer_expression()
|
|
{
|
|
const auto t = make_expr_tokenizer();
|
|
const auto tokens = t.tokenize_or_throw("(a + b) * 3");
|
|
Void::Testing::expect_eq(tokens.size(), std::size_t{7});
|
|
Void::Testing::expect_eq(tokens[0].kind, std::string{"LPAREN"});
|
|
Void::Testing::expect_eq(tokens[6].kind, std::string{"NUMBER"});
|
|
}
|