00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <sstream>
00020
00021 #include "variant_topic_tools/MessageDefinitionParser.h"
00022
00023 namespace variant_topic_tools {
00024
00025
00026
00027
00028
00029 const boost::regex MessageDefinitionParser::commentExpression =
00030 boost::regex("#.*$");
00031
00032 const boost::regex MessageDefinitionParser::separatorExpression =
00033 boost::regex("^==+$");
00034
00035 const boost::regex MessageDefinitionParser::messageTypeExpression =
00036 boost::regex("^\\h*MSG:\\h*([a-zA-Z][a-zA-Z1-9_/]*).*$");
00037
00038 const boost::regex MessageDefinitionParser::memberNameExpression =
00039 boost::regex("[a-zA-Z][a-zA-Z1-9_]*");
00040
00041 const boost::regex MessageDefinitionParser::memberTypeExpression =
00042 boost::regex("[a-zA-Z][a-zA-Z1-9_/]*");
00043
00044 const boost::regex MessageDefinitionParser::memberArrayTypeExpression =
00045 boost::regex("("+memberTypeExpression.str()+")\\[([0-9]*)\\]");
00046
00047 const boost::regex MessageDefinitionParser::memberValueExpression =
00048 boost::regex("[^\\h]+");
00049
00050 const boost::regex MessageDefinitionParser::memberExpression =
00051 boost::regex("^\\h*("+memberTypeExpression.str()+")(\\[[0-9]*\\])?\\h+("+
00052 memberNameExpression.str()+").*$");
00053
00054 const boost::regex MessageDefinitionParser::constantMemberExpression =
00055 boost::regex("^\\h*("+memberTypeExpression.str()+")\\h+("+
00056 memberNameExpression.str()+")\\h*=\\h*("+memberValueExpression.str()+
00057 ")\\h*("+commentExpression.str()+")?$");
00058
00059 const boost::regex MessageDefinitionParser::constantStringMemberExpression =
00060 boost::regex("^\\h*(string)\\h+("+memberNameExpression.str()+
00061 ")\\h*=\\h*(.*?)\\h*$");
00062
00063 const boost::regex MessageDefinitionParser::variableMemberExpression =
00064 boost::regex("^\\h*("+memberTypeExpression.str()+")(\\[[0-9]*\\])?\\h+("+
00065 memberNameExpression.str()+")\\h*("+commentExpression.str()+")?$");
00066
00067 const boost::regex MessageDefinitionParser::arrayMemberExpression =
00068 boost::regex("^\\h*"+memberArrayTypeExpression.str()+"\\h+("+
00069 memberNameExpression.str()+")\\h*("+commentExpression.str()+")?$");
00070
00071
00072
00073
00074
00075 MessageDefinitionParser::MessageDefinitionParser() {
00076 }
00077
00078 MessageDefinitionParser::~MessageDefinitionParser() {
00079 }
00080
00081
00082
00083
00084
00085 size_t MessageDefinitionParser::parse(const std::string& messageDataType, const
00086 std::string& messageDefinition, std::vector<MessageType>& messageTypes) {
00087 messageTypes.clear();
00088
00089 std::istringstream stream(messageDefinition);
00090 std::string currentMessageType = messageDataType;
00091 std::string currentMessageDefinition;
00092 std::string line;
00093
00094 while (std::getline(stream, line)) {
00095 boost::smatch match;
00096
00097 if (boost::regex_match(line, match, messageTypeExpression)) {
00098 if (!currentMessageDefinition.empty())
00099 messageTypes.push_back(MessageType(currentMessageType, "*",
00100 currentMessageDefinition));
00101
00102 currentMessageType = std::string(match[1].first, match[1].second);
00103 currentMessageDefinition.clear();
00104 }
00105 else if (!boost::regex_match(line, match, separatorExpression)) {
00106 if (!currentMessageDefinition.empty())
00107 currentMessageDefinition += "\n";
00108 currentMessageDefinition += line;
00109 }
00110 }
00111
00112 if (!currentMessageDefinition.empty())
00113 messageTypes.push_back(MessageType(currentMessageType, "*",
00114 currentMessageDefinition));
00115
00116 return messageTypes.size();
00117 }
00118
00119 bool MessageDefinitionParser::matchType(const std::string& expression) {
00120 boost::smatch match;
00121 return boost::regex_match(expression, match, memberTypeExpression);
00122 }
00123
00124 bool MessageDefinitionParser::matchArrayType(const std::string& expression,
00125 std::string& memberType, size_t& size) {
00126 boost::smatch match;
00127
00128 if (boost::regex_match(expression, match, memberArrayTypeExpression)) {
00129 memberType = std::string(match[1].first, match[1].second);
00130
00131 if (match[2].first != match[2].second)
00132 size = boost::lexical_cast<size_t>(
00133 std::string(match[2].first, match[2].second));
00134 else
00135 size = 0;
00136
00137 return true;
00138 }
00139 else
00140 return false;
00141 }
00142
00143 bool MessageDefinitionParser::match(const std::string& expression, std::string&
00144 name, std::string& type) {
00145 boost::smatch match;
00146
00147 if (boost::regex_match(expression, match, memberExpression)) {
00148 name = std::string(match[3].first, match[3].second);
00149 type = std::string(match[1].first, match[1].second)+
00150 std::string(match[2].first, match[2].second);
00151
00152 return true;
00153 }
00154 else
00155 return false;
00156 }
00157
00158 bool MessageDefinitionParser::matchConstant(const std::string& expression,
00159 std::string& name, std::string& type, std::string& value) {
00160 boost::smatch match;
00161
00162 if (boost::regex_match(expression, match, constantStringMemberExpression) ||
00163 (boost::regex_match(expression, match, constantMemberExpression))) {
00164 name = std::string(match[2].first, match[2].second);
00165 type = std::string(match[1].first, match[1].second);
00166 value = std::string(match[3].first, match[3].second);
00167
00168 return true;
00169 }
00170 else
00171 return false;
00172 }
00173
00174 bool MessageDefinitionParser::matchVariable(const std::string& expression,
00175 std::string& name, std::string& type) {
00176 boost::smatch match;
00177
00178 if (boost::regex_match(expression, match, variableMemberExpression)) {
00179 name = std::string(match[3].first, match[3].second);
00180 type = std::string(match[1].first, match[1].second)+
00181 std::string(match[2].first, match[2].second);
00182
00183 return true;
00184 }
00185 else
00186 return false;
00187 }
00188
00189 bool MessageDefinitionParser::matchArray(const std::string& expression,
00190 std::string& name, std::string& memberType, size_t& size) {
00191 boost::smatch match;
00192
00193 if (boost::regex_match(expression, match, arrayMemberExpression)) {
00194 name = std::string(match[3].first, match[3].second);
00195 memberType = std::string(match[1].first, match[1].second);
00196
00197 if (match[2].first != match[2].second)
00198 size = boost::lexical_cast<size_t>(
00199 std::string(match[2].first, match[2].second));
00200 else
00201 size = 0;
00202
00203 return true;
00204 }
00205 else
00206 return false;
00207 }
00208
00209 bool MessageDefinitionParser::matchSeparator(const std::string& expression) {
00210 boost::smatch match;
00211
00212 return boost::regex_match(expression, match, separatorExpression);
00213 }
00214
00215 }