SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
FilterGrammar.h
Go to the documentation of this file.
1 //=========================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //=========================================================================
10 #ifndef smtk_model_FilterGrammar_h
11 #define smtk_model_FilterGrammar_h
12 
14 #include "smtk/model/LimitingClause.h"
15 
16 #include "tao/pegtl.hpp"
17 
18 namespace smtk
19 {
20 namespace model
21 {
22 
23 using namespace tao::pegtl;
24 
25 // clang-format off
27 // -----
33 namespace floating_point
34 {
35 struct plus_minus : opt< one< '+', '-' > > {};
36 struct dot : one< '.' > {};
37 struct inf : seq< TAO_PEGTL_ISTRING("inf"),
38  opt< TAO_PEGTL_ISTRING("inity") > > {};
39 struct nan : seq< TAO_PEGTL_ISTRING("nan"),
40  opt< one< '(' >,
41  plus< alnum >,
42  one< ')' > > > {};
43 template< typename D >
44 struct number :
45  if_then_else< dot,
46  plus< D >,
47  seq< plus< D >, opt< dot, star< D > > >
48  > {};
49 struct e : one< 'e', 'E' > {};
50 struct p : one< 'p', 'P' > {};
51 struct exponent : seq< plus_minus, plus< digit > > {};
52 struct decimal : seq< number< digit >, opt< e, exponent > > {};
53 struct hexadecimal : seq< one< '0' >, one< 'x', 'X' >, number< xdigit >, opt< p, exponent > > {};
54 struct value : seq< plus_minus, sor< hexadecimal, decimal, inf, nan > > {};
55 }
56 // -----
57 
60 template <char left, char right, typename... arguments>
61 struct enclosed : if_must<one<left>, until<one<right>, arguments...> > {};
62 
63 template <typename... arguments>
64 struct bracketed : enclosed<'[', ']', arguments...> {};
65 
66 template <typename... arguments>
67 struct braced : enclosed<'{', '}', arguments...> {};
68 
69 template <typename... arguments>
70 struct parenthesized : enclosed<'(', ')', arguments...> {};
71 
72 template <typename... arguments>
73 struct quoted : enclosed<'\'', '\'', arguments...> {};
74 
75 template <typename... arguments>
76 struct slashed : enclosed<'/', '/', arguments...> {};
77 
78 // Define tags for the three supported property types.
79 struct int_property_name : pad<TAO_PEGTL_ISTRING("integer"), space> {};
80 struct float_property_name : pad<TAO_PEGTL_ISTRING("floating-point"), space> {};
81 struct string_property_name : pad<TAO_PEGTL_ISTRING("string"), space> {};
82 
91 
92 struct name_property_value : plus<not_one<'\''> > {};
93 struct name_property : pad<quoted<name_property_value>, space> {};
94 
95 struct name_property_regex_value : plus<not_one<'/'> > {};
96 struct name_property_regex : pad<slashed<name_property_regex_value>, space> {};
97 
98 struct int_property_value : plus<digit> {};
99 struct int_property : pad<int_property_value, space> {};
100 
101 struct float_property_value : floating_point::value {};
102 struct float_property : pad<float_property_value, space>{};
103 
104 struct string_property_value : plus<not_one<'\''> > {};
105 struct string_property : pad<quoted<string_property_value>, space> {};
106 
107 struct string_property_regex_value : plus<not_one<'/'> > {};
108 struct string_property_regex : pad<slashed<string_property_regex_value>, space> {};
109 
112 template <typename property>
113 struct property_sequence
114  : pad<sor<property, parenthesized<list<property, one<','>, space> > >, space> {};
115 
117 // clang-format on
118 
121 template<typename property>
123 
124 template<>
125 struct property_traits<int_property>
126 {
127  typedef int_property_name name;
128  typedef property_sequence<int_property> sequence;
129 };
130 
131 template<>
132 struct property_traits<float_property>
133 {
134  typedef float_property_name name;
135  typedef property_sequence<float_property> sequence;
136 };
137 
138 template<>
139 struct property_traits<string_property>
140 {
141  typedef string_property_name name;
142  typedef property_sequence<sor<string_property, string_property_regex>> sequence;
143 };
144 
148 template<typename property>
150  : pad<
151  seq<
152  typename property_traits<property>::name,
153  opt<braced<
154  sor<name_property, name_property_regex>,
155  opt<pad<TAO_PEGTL_ISTRING("="), space>, typename property_traits<property>::sequence>>>>,
156  space>
157 {
158 };
159 
161 struct SMTKCORE_EXPORT FilterGrammar
162  : bracketed<
163  sor<grammar_for<int_property>, grammar_for<float_property>, grammar_for<string_property>>>
164 {
165 };
166 
168 template<typename Rule>
169 struct SMTKCORE_EXPORT FilterAction : nothing<Rule>
170 {
171 };
172 
173 template<>
174 struct FilterAction<int_property_name>
175 {
176  template<typename Input>
177  static void apply(const Input&, LimitingClause& clause)
178  {
180  }
181 };
182 
183 template<>
184 struct FilterAction<float_property_name>
185 {
186  template<typename Input>
187  static void apply(const Input&, LimitingClause& clause)
188  {
190  }
191 };
192 
193 template<>
194 struct FilterAction<string_property_name>
195 {
196  template<typename Input>
197  static void apply(const Input&, LimitingClause& clause)
198  {
200  }
201 };
202 
203 template<>
204 struct FilterAction<name_property_value>
205 {
206  template<typename Input>
207  static void apply(const Input& in, LimitingClause& clause)
208  {
209  clause.m_propName = in.string();
210  }
211 };
212 
213 template<>
214 struct FilterAction<name_property_regex_value>
215 {
216  template<typename Input>
217  static void apply(const Input& in, LimitingClause& clause)
218  {
219  clause.m_propName = in.string();
220  clause.m_propNameIsRegex = true;
221  }
222 };
223 
224 template<>
225 struct FilterAction<int_property_value>
226 {
227  template<typename Input>
228  static void apply(const Input& in, LimitingClause& clause)
229  {
230  clause.m_propIntValues.push_back(std::stoi(in.string()));
231  }
232 };
233 
234 template<>
235 struct FilterAction<float_property_value>
236 {
237  template<typename Input>
238  static void apply(const Input& in, LimitingClause& clause)
239  {
240  clause.m_propFloatValues.push_back(std::stod(in.string()));
241  }
242 };
243 
244 template<>
245 struct FilterAction<string_property_value>
246 {
247  template<typename Input>
248  static void apply(const Input& in, LimitingClause& clause)
249  {
250  clause.m_propStringValues.push_back(in.string());
251  clause.m_propStringIsRegex.push_back(false);
252  }
253 };
254 
255 template<>
256 struct FilterAction<string_property_regex_value>
257 {
258  template<typename Input>
259  static void apply(const Input& in, LimitingClause& clause)
260  {
261  clause.m_propStringValues.push_back(in.string());
262  clause.m_propStringIsRegex.push_back(true);
263  }
264 };
265 } // namespace model
266 } // namespace smtk
267 
268 #endif // smtk_model_FilterGrammar_h
smtk
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
smtk::apply
std::enable_if< I==sizeof...(Ts), void >::type apply(Functor &f, std::tuple< Ts... > tup)
Apply a functor to each element of a tuple.
Definition: TupleTraits.h:455
smtk::model::FilterAction
Actions on the state in response to encountered grammar.
Definition: FilterGrammar.h:169
smtk::model::property_traits
We use a traits class to describe analogous features between the different property types.
Definition: FilterGrammar.h:122
smtk::model::grammar_for
With the differences between the property types factored out into the above traits class,...
Definition: FilterGrammar.h:149
smtk::resource::STRING_PROPERTY
@ STRING_PROPERTY
Property is an array of strings.
Definition: PropertyType.h:34
smtk::model::LimitingClause
!
Definition: LimitingClause.h:31
smtk::model::FilterGrammar
The filter grammar is a composition of the grammar for each property type.
Definition: FilterGrammar.h:161
smtk::resource::FLOAT_PROPERTY
@ FLOAT_PROPERTY
Property is an array of floating-point numbers.
Definition: PropertyType.h:33
smtk::resource::INTEGER_PROPERTY
@ INTEGER_PROPERTY
Property is an array of integers.
Definition: PropertyType.h:35