Named-Key Parsing
Format describes the aliases, separators, value policy, and limits of
a named-key entry list.
Parser reads from a
StringCharReader and returns
Entry values. Entries may carry a recognized key, a keyed value, or a
positional value.
Invalid syntax raises a positioned err::ParseError.
The format must outlive its parser.
Register normalized aliases with semantic integer identifiers before reading.
readEntry() returns a terminal entry at the end; readAllEntries() excludes that entry.
Interface
-
class Entry
One entry produced by
Parser.Public Functions
-
Entry() = default
Create the terminal entry.
-
inline Entry(EntryKind kind, Char prefix, int keyIndex, String value) noexcept
Create an entry with all parsed fields.
-
inline bool isKey() const noexcept
Test if this is a key without a value.
-
inline bool isKeyWithValue() const noexcept
Test if this is a key with a value.
-
inline bool isValue() const noexcept
Test if this is a positional value.
-
inline bool isEnd() const noexcept
Test if this is the terminal entry.
-
inline bool hasValue() const noexcept
Test if this entry has a keyed or positional value.
-
inline int keyIndex() const noexcept
Get the key identifier, or
-1for non-key entries.
-
Entry() = default
-
enum class erbsland::text::named_key::EntryKind : uint8_t
The kind of one parsed named-key entry.
Values:
-
enumerator Key
A key without a value.
-
enumerator KeyWithValue
A key with an explicit or compact value.
-
enumerator Value
A positional value.
-
enumerator End
The configured end of the entry list.
-
enumerator Key
-
class Format
The grammar and key table for
Parser.Public Functions
-
Format() = default
Create the default named-key format.
-
Format &setKeys(Keys keys)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
-
std::optional<int> keyIndex(const String &key) const
Find a semantic key identifier from any supported spelling.
-
const String &keyName(int keyIndex) const
Get the first registered normalized alias for a semantic key identifier.
- Parameters:
keyIndex – The semantic key identifier. Must be registered in this format.
-
inline bool uniqueKeysRequired() const noexcept
Test if each semantic key is limited to one occurrence.
-
inline bool keysWithoutValuesAllowed() const noexcept
Test if recognized keys without values are allowed.
-
inline bool valuesAllowed() const noexcept
Test if values attached to recognized keys are allowed.
-
inline bool valueListAllowed() const noexcept
Test if a positional value list is allowed.
-
Format &setMaximumValues(unit::ItemCount maximum) noexcept
Set the maximum number of positional values.
-
Format &setMaximumValueLength(unit::CpCount maximum) noexcept
Set the maximum value length in code points.
-
inline unit::CpCount maximumValueLength() const noexcept
Get the maximum value length in code points.
-
Format &setStopCharacter(Char character) noexcept
Set the required stop character, or end-of-data for an end-terminated list.
-
Format &setAllowedValueChars(CharSet characters) noexcept
Set the allowed value characters. An empty set permits every safe character.
-
Format &setValueWithoutKeySeparatorChars(CharSet characters) noexcept
Set characters that start a compact value immediately after a key.
-
inline const CharSet &valueWithoutKeySeparatorChars() const noexcept
Get characters that start a compact value immediately after a key.
-
void validate() const
Validate this format before parsing.
The format must have valid, unambiguous key names and separators.
-
Format() = default
-
class Parser
A low-level parser for named-key and positional-value entry lists.
All syntax errors are reported as positioned
err::ParseErrorexceptions.Public Functions
-
Parser(StringCharReader &reader, const Format &format)
Create a parser at the first entry.
- Parameters:
format – The named-key format. Must be valid and unambiguous.
reader – The input reader, positioned at the first entry.
-
void setAllowedKeys(util::Set<int> keys)
Restrict recognized keys to the given semantic identifiers.
-
Entry readEntry()
Read one entry or the terminal entry.
- Throws:
err::ParseError – for invalid syntax.
-
util::List<Entry> readAllEntries()
Read all remaining entries, excluding the terminal entry.
- Throws:
err::ParseError – for invalid syntax.
-
Parser(StringCharReader &reader, const Format &format)