Implementation:AGenUI AGenUI TokenParser
| Knowledge Sources | |
|---|---|
| Domains | Theming, UI_Framework |
| Last Updated | 2026-10-24 17:21 GMT |
Overview
Singleton token resolver that maps design token names to concrete values with light/dark mode support, provided by the AGenUI C++ core.
Description
The `TokenParser` is a thread-safe singleton (Meyers singleton via `getInstance()`) that stores design tokens loaded from JSON config. Each token is a `DesignToken` struct with `type` (e.g. "color"), `light` value, and `dark` value.
The three key methods:
- `loadFromJsonString(jsonString)`: parses a `{"designTokens": {...}}` JSON object and populates the internal `_designTokens` map. Each token must have at least one of `light` or `dark` to be stored. Returns `false` on JSON parse failure or missing `designTokens` field.
- `resolve(tokenName)`: looks up the token by name. In dark mode, prefers `dark` value and falls back to `light`. In light mode, prefers `light` and falls back to `dark`. If the token name is not found, returns the original name unchanged (passthrough).
- `setThemeMode(mode)`: updates the internal `_themeMode`. Returns `false` if the mode is the same as current (no-op optimization), `true` if it actually changed. This is checked by `setDayNightMode()` to decide whether to broadcast invalidation.
The constructor auto-loads a built-in default token config (`kDesignTokenConfig`) so the parser is usable without explicit loading.
All methods are protected by a `std::recursive_mutex`.
Usage
- Load custom tokens: call `engine->loadDesignTokenConfig(tokenJson, error)`.
- Resolve tokens: called internally during style resolution and via the `parseToken` builtin functionCall.
- Switch mode: call `engine->setDayNightMode("dark")`; the engine handles token mode update and invalidation broadcast.
Code Reference
Source Location
- Repository: AGenUI
- Header: `core/src/surface/token_parser/agenui_token_parser.h:L31-69`
- Implementation: `core/src/surface/token_parser/agenui_token_parser.cpp:L1-85`
- Default config: `core/src/surface/token_parser/agenui_design_token_config.h` (`kDesignTokenConfig`)
- Builtin: `core/src/function_call/builtins/agenui_parse_token_functioncall.cpp`
Signature
// core/src/surface/token_parser/agenui_token_parser.h
namespace agenui {
enum class ThemeMode { Light, Dark };
struct DesignToken {
std::string type; // e.g. "color"
std::string light; // Value in light mode
std::string dark; // Value in dark mode (may be empty, falls back to light)
};
class TokenParser {
public:
static TokenParser& getInstance();
// Resolve a token name to its actual value
// Returns original name if not found (passthrough)
std::string resolve(const std::string& tokenName) const;
// Load design token config from JSON string
// Format: {"designTokens": {"tokenName": {"type":"color","light":"#xxx","dark":"#xxx"}, ...}}
// Returns true on success, false if JSON parsing fails
bool loadFromJsonString(const std::string& jsonString);
// Set the theme mode
// Returns true if mode actually changed, false if same as current
bool setThemeMode(ThemeMode mode);
private:
TokenParser(); // auto-loads kDesignTokenConfig
mutable std::recursive_mutex _mutex;
std::map<std::string, DesignToken> _designTokens;
ThemeMode _themeMode = ThemeMode::Light;
};
} // namespace agenui
Import
#include "surface/token_parser/agenui_token_parser.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| jsonString | `const std::string&` | Yes | Design token config JSON |
| tokenName | `const std::string&` | Yes | Token name to resolve |
| mode | `ThemeMode` | Yes | Light or Dark |
Outputs
| Name | Type | Description |
|---|---|---|
| resolve returns | Resolved value, or original name if not found | |
| loadFromJsonString returns | true on success, false on parse failure | |
| setThemeMode returns | true if mode changed, false if same as current |
Usage Examples
Loading Custom Design Tokens
#include "agenui_engine_entry.h"
#include "agenui_engine.h"
auto* engine = agenui::initAGenUIEngine();
std::string tokenConfig = R"({
"designTokens": {
"color_primary": {
"type": "color",
"light": "#007AFF",
"dark": "#0A84FF"
},
"color_background": {
"type": "color",
"light": "#FFFFFF",
"dark": "#1C1C1E"
},
"spacing_md": {
"type": "dimension",
"light": "16dp"
}
}
})";
std::string error;
bool ok = engine->loadDesignTokenConfig(tokenConfig, error);
// Tokens now resolvable via parseToken functionCall or style resolution
Using Tokens in A2UI Protocol
{
"component": "Text",
"id": "title",
"text": "Hello",
"styles": {
"color": {"call": "parseToken", "args": {"token": "color_primary"}}
}
}