MessageDefinitionParser.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2014 by Ralf Kaestner *
3  * ralf.kaestner@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the Lesser GNU General Public License as published by*
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * Lesser GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the Lesser GNU General Public License *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  ******************************************************************************/
18 
19 #include <sstream>
20 
22 
23 namespace variant_topic_tools {
24 
25 /*****************************************************************************/
26 /* Static initializers */
27 /*****************************************************************************/
28 
30  boost::regex("#.*$");
31 
33  boost::regex("^==+$");
34 
36  boost::regex("^\\h*MSG:\\h*([a-zA-Z][a-zA-Z0-9_/]*).*$");
37 
39  boost::regex("[a-zA-Z][a-zA-Z0-9_]*");
40 
42  boost::regex("[a-zA-Z][a-zA-Z0-9_/]*");
43 
45  boost::regex("("+memberTypeExpression.str()+")\\[([0-9]*)\\]");
46 
48  boost::regex("[^\\h]+");
49 
51  boost::regex("^\\h*("+memberTypeExpression.str()+")(\\[[0-9]*\\])?\\h+("+
52  memberNameExpression.str()+").*$");
53 
55  boost::regex("^\\h*("+memberTypeExpression.str()+")\\h+("+
56  memberNameExpression.str()+")\\h*=\\h*("+memberValueExpression.str()+
57  ")\\h*("+commentExpression.str()+")?$");
58 
60  boost::regex("^\\h*(string)\\h+("+memberNameExpression.str()+
61  ")\\h*=\\h*(.*?)\\h*$");
62 
64  boost::regex("^\\h*("+memberTypeExpression.str()+")(\\[[0-9]*\\])?\\h+("+
65  memberNameExpression.str()+")\\h*("+commentExpression.str()+")?$");
66 
68  boost::regex("^\\h*"+memberArrayTypeExpression.str()+"\\h+("+
69  memberNameExpression.str()+")\\h*("+commentExpression.str()+")?$");
70 
71 /*****************************************************************************/
72 /* Constructors and Destructor */
73 /*****************************************************************************/
74 
76 }
77 
79 }
80 
81 /*****************************************************************************/
82 /* Methods */
83 /*****************************************************************************/
84 
85 size_t MessageDefinitionParser::parse(const std::string& messageDataType, const
86  std::string& messageDefinition, std::vector<MessageType>& messageTypes) {
87  messageTypes.clear();
88 
89  std::istringstream stream(messageDefinition);
90  std::string currentMessageType = messageDataType;
91  std::string currentMessageDefinition;
92  std::string line;
93 
94  while (std::getline(stream, line)) {
95  boost::smatch match;
96 
97  if (boost::regex_match(line, match, messageTypeExpression)) {
98  if (!currentMessageDefinition.empty())
99  messageTypes.push_back(MessageType(currentMessageType, "*",
100  currentMessageDefinition));
101 
102  currentMessageType = std::string(match[1].first, match[1].second);
103  currentMessageDefinition.clear();
104  }
105  else if (!boost::regex_match(line, match, separatorExpression)) {
106  if (!currentMessageDefinition.empty())
107  currentMessageDefinition += "\n";
108  currentMessageDefinition += line;
109  }
110  }
111 
112  if (!currentMessageDefinition.empty())
113  messageTypes.push_back(MessageType(currentMessageType, "*",
114  currentMessageDefinition));
115 
116  return messageTypes.size();
117 }
118 
119 bool MessageDefinitionParser::matchType(const std::string& expression) {
120  boost::smatch match;
121  return boost::regex_match(expression, match, memberTypeExpression);
122 }
123 
124 bool MessageDefinitionParser::matchArrayType(const std::string& expression,
125  std::string& memberType, size_t& size) {
126  boost::smatch match;
127 
128  if (boost::regex_match(expression, match, memberArrayTypeExpression)) {
129  memberType = std::string(match[1].first, match[1].second);
130 
131  if (match[2].first != match[2].second)
132  size = boost::lexical_cast<size_t>(
133  std::string(match[2].first, match[2].second));
134  else
135  size = 0;
136 
137  return true;
138  }
139  else
140  return false;
141 }
142 
143 bool MessageDefinitionParser::match(const std::string& expression, std::string&
144  name, std::string& type) {
145  boost::smatch match;
146 
147  if (boost::regex_match(expression, match, memberExpression)) {
148  name = std::string(match[3].first, match[3].second);
149  type = std::string(match[1].first, match[1].second)+
150  std::string(match[2].first, match[2].second);
151 
152  return true;
153  }
154  else
155  return false;
156 }
157 
158 bool MessageDefinitionParser::matchConstant(const std::string& expression,
159  std::string& name, std::string& type, std::string& value) {
160  boost::smatch match;
161 
162  if (boost::regex_match(expression, match, constantStringMemberExpression) ||
163  (boost::regex_match(expression, match, constantMemberExpression))) {
164  name = std::string(match[2].first, match[2].second);
165  type = std::string(match[1].first, match[1].second);
166  value = std::string(match[3].first, match[3].second);
167 
168  return true;
169  }
170  else
171  return false;
172 }
173 
174 bool MessageDefinitionParser::matchVariable(const std::string& expression,
175  std::string& name, std::string& type) {
176  boost::smatch match;
177 
178  if (boost::regex_match(expression, match, variableMemberExpression)) {
179  name = std::string(match[3].first, match[3].second);
180  type = std::string(match[1].first, match[1].second)+
181  std::string(match[2].first, match[2].second);
182 
183  return true;
184  }
185  else
186  return false;
187 }
188 
189 bool MessageDefinitionParser::matchArray(const std::string& expression,
190  std::string& name, std::string& memberType, size_t& size) {
191  boost::smatch match;
192 
193  if (boost::regex_match(expression, match, arrayMemberExpression)) {
194  name = std::string(match[3].first, match[3].second);
195  memberType = std::string(match[1].first, match[1].second);
196 
197  if (match[2].first != match[2].second)
198  size = boost::lexical_cast<size_t>(
199  std::string(match[2].first, match[2].second));
200  else
201  size = 0;
202 
203  return true;
204  }
205  else
206  return false;
207 }
208 
209 bool MessageDefinitionParser::matchSeparator(const std::string& expression) {
210  boost::smatch match;
211 
212  return boost::regex_match(expression, match, separatorExpression);
213 }
214 
215 }
static bool matchConstant(const std::string &expression, std::string &name, std::string &type, std::string &value)
Match a constant message member expression.
Variant message type information.
Definition: MessageType.h:33
static bool match(const std::string &expression, std::string &name, std::string &type)
Match any message member expression.
variant_topic_tools::MessageDefinition messageDefinition
Definition: info.cpp:30
Header file providing the MessageDefinitionParser class interface.
static bool matchSeparator(const std::string &expression)
Match a separator expression.
static const boost::regex constantStringMemberExpression
Regular expression for matching a constant string message member.
static bool matchArray(const std::string &expression, std::string &name, std::string &memberType, size_t &size)
Match an array message member expression.
static const boost::regex messageTypeExpression
Regular expression for matching a message type definition.
static const boost::regex constantMemberExpression
Regular expression for matching a constant message member.
static bool matchArrayType(const std::string &expression, std::string &memberType, size_t &size)
Match an array type expression.
static const boost::regex variableMemberExpression
Regular expression for matching a variable message member.
static const boost::regex memberNameExpression
Regular expression for matching a message member name.
static const boost::regex commentExpression
Regular expression for matching a comment.
static bool matchType(const std::string &expression)
Match any message type expression.
static size_t parse(const std::string &messageDataType, const std::string &messageDefinition, std::vector< MessageType > &messageTypes)
Parse a message definition and generate the message types.
static const boost::regex separatorExpression
Regular expression for matching a message type separator.
static const boost::regex memberValueExpression
Regular expression for matching a message member value.
static const boost::regex arrayMemberExpression
Regular expression for matching an array message member.
static const boost::regex memberArrayTypeExpression
Regular expression for matching an array message member type.
static const boost::regex memberExpression
Regular expression for matching any message member.
static bool matchVariable(const std::string &expression, std::string &name, std::string &type)
Match a variable message member expression.
static const boost::regex memberTypeExpression
Regular expression for matching any message member type.


variant_topic_tools
Author(s): Ralf Kaestner
autogenerated on Sat Jan 9 2021 03:56:49