![]() |
IRSOL
C++ code implementing socket server for interacting with Baumer camera.
|
Parses raw input strings into structured InMessage instances. More...
#include <parser.hpp>
Static Public Member Functions | |
static std::optional< InMessage > | parse (const std::string &line) |
Attempts to parse a single protocol input line into a structured InMessage. | |
Static Private Member Functions | |
static internal::ParserResult< Assignment > | parseAssignment (const std::string &line) |
Parses an assignment message from a protocol line. | |
static internal::ParserResult< Inquiry > | parseInquiry (const std::string &line) |
Parses an inquiry message from a protocol line. | |
static internal::ParserResult< Command > | parseCommand (const std::string &line) |
Parses a command message from a protocol line. | |
static irsol::types::protocol_value_t | parseValue (const std::string &valueString) |
Attempts to parse a raw value string into a typed protocol value. | |
Parses raw input strings into structured InMessage instances.
The Parser
provides static methods to convert protocol input lines (e.g., x=42
, reset
, x?
) into well-formed irsol::protocol::InMessage variants. Parsing is designed to be robust and safe, returning an empty result on failure.
Definition at line 31 of file parser.hpp.
|
static |
Attempts to parse a single protocol input line into a structured InMessage.
This method sequentially tries to interpret the input string as one of the supported message types:
"foo=42"
or "temp[1]=36.5"
): Key-value bindings with optional indexed identifiers."foo?"
or "temp[2]?"
): Used to query the current value of an identifier."reset"
or "shutdown"
): Control messages without arguments.The function internally trims whitespace, applies regular expressions to match expected message formats, and uses helper functions to convert the raw string into a strongly-typed variant.
Parsing proceeds in order of decreasing specificity:
parseAssignment()
is attempted first. If it fails, the reason is recorded.parseInquiry()
is attempted next. If it fails, the reason is recorded.parseCommand()
is attempted last.If none of the parsers succeed, a warning is logged with details from each parsing attempt, and the method returns std::nullopt
.
line | A raw protocol line (e.g., from a client or script), which may contain leading/trailing whitespace. |
std::nullopt
on failure.Definition at line 11 of file parser.cpp.
|
staticprivate |
Parses an assignment message from a protocol line.
Matches lines of the form:
where:
identifier
must begin with a letter and contain alphanumeric characters and underscores.array[0]
, arr[1][2]
.value
is interpreted as an integer, double, or string, depending on its format.Valid examples:
"x=42"
"temperature[1]=36.5"
If the regex matches but value parsing fails, an error string is returned. If the format does not match, a descriptive parsing error is returned.
line | The raw line to parse. |
Definition at line 48 of file parser.cpp.
|
staticprivate |
Parses a command message from a protocol line.
Matches lines consisting of a single bare identifier:
where:
identifier
must start with a letter and contain only alphanumeric characters and underscores.Valid examples:
"reset"
"shutdown"
"ping"
Returns a Command object on success, or an error message on failure.
line | The raw line to parse. |
Definition at line 113 of file parser.cpp.
|
staticprivate |
Parses an inquiry message from a protocol line.
Matches lines of the form:
where:
identifier
must begin with a letter and contain alphanumeric characters and underscores.array[0]
).Valid examples:
"x?"
"temperature[1]?"
"config_setting?"
Returns a valid Inquiry message on success, or a detailed error message on failure.
line | The raw line to parse. |
Definition at line 81 of file parser.cpp.
|
staticprivate |
Attempts to parse a raw value string into a typed protocol value.
The parser uses the following heuristics, in order:
double
, and contains .
or scientific notation (e
/E
), it is treated as a double
.int
range, it is converted to int
.,
"<tt>, or wrapped in</tt>{}<tt>, the delimiters are stripped and
the result is returned as a</tt>std::string`.
4. Otherwise, the raw string is returned as-is.
Examples:
- <tt>"42"</tt> → <tt>int</tt>
- <tt>"3.14"</tt> → <tt>double</tt>
- <tt>"1e-3"</tt> → <tt>double</tt>
- ‘"'hello’"`, `'"hello"'`, `"{hello}"` → `"hello"`
- `"raw_string"` → `"raw_string"`valueString | The string representation of the value (right-hand side of an assignment). |
Definition at line 142 of file parser.cpp.