libcluon  0.0.148
cluon::MessageParser Class Reference

#include <MessageParser.hpp>

Public Types

enum  MessageParserErrorCodes : uint8_t { NO_MESSAGEPARSER_ERROR = 0, SYNTAX_ERROR = 1, DUPLICATE_IDENTIFIERS = 2 }
 

Public Member Functions

 MessageParser ()=default
 
std::pair< std::vector< MetaMessage >, MessageParserErrorCodesparse (const std::string &input)
 

Detailed Description

This class provides a parser for message specifications in .odvd format. The format is inspired by Google Protobuf (https://developers.google.com/protocol-buffers/) but simplified to enforce backwards and forwards compatibility next to numerical message identifiers.

This message specification format is also used by OpenDaVINCI (http://code.opendavinci.org).

The parser is based on https://github.com/yhirose/cpp-peglib.

An example for a .odvd compliant message is demonstrated in the following:

const char *spec = R"(
message myMessage.SubName [id = 1] {
uint8 field1 [id = 1];
uint32 field2 [id = 2];
int64 field3 [id = 3];
string field4 [id = 4];
}
)";
auto retVal = mp.parse(std::string(spec));
if (retVal.second == cluon::MessageParser::MessageParserErrorCodes::NO_MESSAGEPARSER_ERROR) {
auto listOfMessages = retVal.first;
for (auto message : listOfMessages) {
message.accept([](const cluon::MetaMessage &mm){ std::cout << "Message name = " << mm.messageName() <<
std::endl; });
}
}

Member Enumeration Documentation

◆ MessageParserErrorCodes

Enumerator
NO_MESSAGEPARSER_ERROR 
SYNTAX_ERROR 
DUPLICATE_IDENTIFIERS 

Constructor & Destructor Documentation

◆ MessageParser()

cluon::MessageParser::MessageParser ( )
default

Member Function Documentation

◆ parse()

std::pair< std::vector< MetaMessage >, MessageParser::MessageParserErrorCodes > cluon::MessageParser::parse ( const std::string &  input)

This method tries to parse the given message specification.

Parameters
inputMessage specification.
Returns
Pair: List of cluon::MetaMessages describing the specified messages and error code: NO_MESSAGEPARSER_ERROR: The given specification could be parsed successfully (list moght be non-empty). SYNTAX_ERROR: The given specification could not be parsed successfully (list is empty). DUPLICATE_IDENTIFIERS: The given specification contains ambiguous names or identifiers (list is empty).

References ast, cluon::MetaMessage::messageIdentifier(), cluon::MetaMessage::messageName(), mm, cluon::MetaMessage::packageName(), and retVal.

Referenced by cluon::EnvelopeConverter::setMessageSpecification(), and cluon::LCMToGenericMessage::setMessageSpecification().