26 std::string _conditionString;
28 void splitConditions(
const std::string&
string, std::vector<std::string>& tokens)
const;
34 IParser(), _aiFactory(aiFactory) {
35 _conditionString = ai::Str::eraseAllSpaces(conditionString);
39 ConditionPtr getCondition();
42 inline void ConditionParser::splitConditions(
const std::string&
string, std::vector<std::string>& tokens)
const {
46 for (std::string::const_iterator i =
string.begin(); i !=
string.end(); ++i) {
49 }
else if (*i ==
'}') {
51 }
else if (*i ==
'(') {
53 }
else if (*i ==
')') {
57 if (inParameter == 0 && inChildren == 0) {
59 tokens.push_back(token);
66 tokens.push_back(token);
69 inline bool ConditionParser::fillInnerFilters(FilterFactoryContext& ctx,
const std::string& filterStr) {
70 std::vector<std::string> conditions;
71 splitConditions(filterStr, conditions);
72 if (conditions.size() > 1) {
73 for (std::vector<std::string>::const_iterator i = conditions.begin(); i != conditions.end(); ++i) {
74 if (!fillInnerFilters(ctx, *i)) {
81 std::string parameters;
82 std::size_t n = filterStr.find(
"(");
83 if (n == std::string::npos || filterStr.find(
"{") < n) {
84 parameters = getBetween(filterStr,
"{",
"}");
85 n = filterStr.find(
"{");
89 if (n != std::string::npos) {
90 name = filterStr.substr(0, n);
94 FilterFactoryContext ctxInner(parameters);
95 n = filterStr.find(
"(");
96 if (n != std::string::npos) {
97 const std::size_t r = filterStr.rfind(
")");
98 if (r == std::string::npos) {
99 setError(
"syntax error, missing closing brace");
102 const std::string& inner = filterStr.substr(n + 1, r - n - 1);
103 if (!fillInnerFilters(ctxInner, inner)) {
107 const FilterPtr& c = _aiFactory.
createFilter(name, ctxInner);
109 setError(
"could not create filter for %s", name.c_str());
112 ctx.filters.push_back(c);
116 inline bool ConditionParser::fillInnerConditions(ConditionFactoryContext& ctx,
const std::string& conditionStr) {
117 std::vector<std::string> conditions;
118 splitConditions(conditionStr, conditions);
119 if (conditions.size() > 1) {
120 for (std::vector<std::string>::const_iterator i = conditions.begin(); i != conditions.end(); ++i) {
121 if (!fillInnerConditions(ctx, *i)) {
126 std::string parameters;
127 std::size_t n = conditionStr.find(
"(");
128 if (n == std::string::npos || conditionStr.find(
"{") < n) {
129 parameters = getBetween(conditionStr,
"{",
"}");
130 n = conditionStr.find(
"{");
134 if (n != std::string::npos) {
135 name = conditionStr.substr(0, n);
141 FilterFactoryContext ctxInner(parameters);
142 n = conditionStr.find(
"(");
143 if (n != std::string::npos) {
144 const std::size_t r = conditionStr.rfind(
")");
145 if (r == std::string::npos) {
146 setError(
"syntax error, missing closing brace");
149 const std::string& inner = conditionStr.substr(n + 1, r - n - 1);
150 if (!fillInnerFilters(ctxInner, inner)) {
154 const FilterPtr& c = _aiFactory.
createFilter(name, ctxInner);
156 setError(
"could not create filter for %s", name.c_str());
159 ctx.filters.push_back(c);
161 ConditionFactoryContext ctxInner(parameters);
162 ctxInner.filter = name == FILTER_NAME;
163 n = conditionStr.find(
"(");
164 if (n != std::string::npos) {
165 const std::size_t r = conditionStr.rfind(
")");
166 if (r == std::string::npos) {
167 setError(
"syntax error, missing closing brace");
170 const std::string& inner = conditionStr.substr(n + 1, r - n - 1);
171 if (!fillInnerConditions(ctxInner, inner)) {
177 setError(
"could not create inner condition for %s", name.c_str());
180 ctx.conditions.push_back(c);
186 inline ConditionPtr ConditionParser::getCondition() {
188 std::string parameters;
189 std::size_t n = _conditionString.find(
"(");
190 if (n == std::string::npos || _conditionString.find(
"{") < n) {
191 parameters = getBetween(_conditionString,
"{",
"}");
192 n = _conditionString.find(
"{");
195 if (n != std::string::npos) {
196 name = _conditionString.substr(0, n);
198 name = _conditionString;
200 ConditionFactoryContext ctx(parameters);
201 ctx.filter = name == FILTER_NAME;
202 n = _conditionString.find(
"(");
203 if (n != std::string::npos) {
204 const std::size_t r = _conditionString.rfind(
")");
205 if (r == std::string::npos) {
206 setError(
"syntax error, missing closing brace");
207 return ConditionPtr();
209 const std::string& inner = _conditionString.substr(n + 1, r - n - 1);
210 if (!fillInnerConditions(ctx, inner)) {
211 return ConditionPtr();
213 }
else if (ctx.filter) {
214 setError(
"missing details for Filter condition");
215 return ConditionPtr();
219 setError(
"could not create condition for %s", name.c_str());
220 return ConditionPtr();
virtual ConditionPtr createCondition(const std::string &type, const ConditionFactoryContext &ctx) const =0
Allocates a new ICondition for the given type. The type must be registered in the AIRegistry for this...
Definition: AIFactories.h:51
virtual FilterPtr createFilter(const std::string &type, const FilterFactoryContext &ctx) const =0
Allocates a new IFilter for the given type. The type must be registered in the AIRegistry for this to...
Definition: AIFactories.h:34
Definition: IAIFactory.h:39
Transforms the string representation of a condition with all its sub conditions and parameters into a...
Definition: ConditionParser.h:23