3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
33 * Handling of intermediate selection parser data.
35 * The data types declared in this header are used by the parser to store
36 * intermediate data when constructing method expressions.
37 * In particular, the parameters for the method are stored.
38 * The intermediate data is freed once a gmx::SelectionTreeElement object can
41 * This is an implementation header: there should be no need to use it outside
44 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
45 * \ingroup module_selection
47 #ifndef GMX_SELECTION_PARSETREE_H
48 #define GMX_SELECTION_PARSETREE_H
54 #include "gromacs/legacyheaders/types/simple.h"
55 #include "gromacs/legacyheaders/vec.h"
57 #include "gromacs/utility/gmxassert.h"
58 #include "gromacs/utility/uniqueptr.h"
63 struct gmx_ana_indexgrps_t;
64 struct gmx_ana_selmethod_t;
65 struct gmx_ana_selparam_t;
72 * String matching mode for string keyword expressions.
74 * \ingroup module_selection
76 enum SelectionStringMatchType
78 eStringMatchType_Auto, //!< Deduce from the string.
79 eStringMatchType_Exact, //!< Match as a literal string.
80 eStringMatchType_Wildcard, //!< Match using ? and * as wildcards.
81 eStringMatchType_RegularExpression //!< Match using regular expressions.
85 class SelectionParserValue;
87 //! Container for a list of SelectionParserValue objects.
88 typedef std::list<SelectionParserValue>
89 SelectionParserValueList;
90 //! Smart pointer type for managing a SelectionParserValueList.
91 typedef gmx::gmx_unique_ptr<SelectionParserValueList>::type
92 SelectionParserValueListPointer;
95 * Describes a parsed value, possibly resulting from expression evaluation.
97 * All factory methods and the constructors may throw an std::bad_alloc if
100 * \ingroup module_selection
102 class SelectionParserValue
105 //! Allocates and initializes an empty value list.
106 static SelectionParserValueListPointer createList()
108 return SelectionParserValueListPointer(new SelectionParserValueList);
111 * Allocates and initializes a value list with a single value.
113 * \param[in] value Initial value to put in the list.
114 * \returns Pointer to a new value list that contains \p value.
116 static SelectionParserValueListPointer
117 createList(const SelectionParserValue &value)
119 SelectionParserValueListPointer list(new SelectionParserValueList);
120 list->push_back(value);
124 * Allocates and initializes an expression value.
126 * \param[in] expr Root of the expression tree to assign to the value.
127 * \returns The newly created value.
129 static SelectionParserValue
130 createExpr(const gmx::SelectionTreeElementPointer &expr)
132 return SelectionParserValue(expr);
135 * Allocates and initializes a constant integer value.
137 * \param[in] value Integer value to assign to the value.
138 * \returns The newly created value.
140 static SelectionParserValue createInteger(int value)
142 SelectionParserValue result(INT_VALUE);
143 result.u.i.i1 = result.u.i.i2 = value;
147 * Allocates and initializes a constant integer range value.
149 * \param[in] from Beginning of the range to assign to the value.
150 * \param[in] to End of the range to assign to the value.
151 * \returns The newly created value.
153 static SelectionParserValue createIntegerRange(int from, int to)
155 SelectionParserValue result(INT_VALUE);
156 result.u.i.i1 = from;
161 * Allocates and initializes a constant floating-point value.
163 * \param[in] value Floating-point value to assign to the value.
164 * \returns The newly created value.
166 static SelectionParserValue createReal(real value)
168 SelectionParserValue result(REAL_VALUE);
169 result.u.r.r1 = result.u.r.r2 = value;
173 * Allocates and initializes a constant floating-point range value.
175 * \param[in] from Beginning of the range to assign to the value.
176 * \param[in] to End of the range to assign to the value.
177 * \returns The newly created value.
179 static SelectionParserValue createRealRange(real from, real to)
181 SelectionParserValue result(REAL_VALUE);
182 result.u.r.r1 = from;
187 * Allocates and initializes a constant string value.
189 * \param[in] value String to assign to the value.
190 * \returns The newly created value.
192 static SelectionParserValue createString(const char *value)
194 SelectionParserValue result(STR_VALUE);
199 * Allocates and initializes a constant position value.
201 * \param[in] value Position vector to assign to the value.
202 * \returns The newly created value.
204 static SelectionParserValue createPosition(rvec value)
206 SelectionParserValue result(POS_VALUE);
207 copy_rvec(value, result.u.x);
211 //! Returns true if the value comes from expression evaluation.
212 bool hasExpressionValue() const { return expr; }
214 //! Returns the string value (\a type must be ::STR_VALUE).
215 const std::string &stringValue() const
217 GMX_ASSERT(type == STR_VALUE && !hasExpressionValue(),
218 "Attempted to retrieve string value from a non-string value");
222 // TODO: boost::any or similar could be nicer for the implementation.
223 //! Type of the value.
225 //! Expression pointer if the value is the result of an expression.
226 gmx::SelectionTreeElementPointer expr;
227 //! String value for \a type ::STR_VALUE.
229 //! The actual value if \a expr is NULL and \a type is not ::STR_VALUE.
231 //! The integer value/range (\a type ::INT_VALUE).
233 //! Beginning of the range.
235 //! End of the range; equals \a i1 for a single integer.
238 //! The real value/range (\a type ::REAL_VALUE).
240 //! Beginning of the range.
242 //! End of the range; equals \a r1 for a single number.
245 //! The position value (\a type ::POS_VALUE).
251 * Initializes a new value.
253 * \param[in] type Type for the new value.
255 explicit SelectionParserValue(e_selvalue_t type);
257 * Initializes a new expression value.
259 * \param[in] expr Expression for the value.
261 explicit SelectionParserValue(const gmx::SelectionTreeElementPointer &expr);
264 class SelectionParserParameter;
266 //! Container for a list of SelectionParserParameter objects.
267 typedef std::list<SelectionParserParameter>
268 SelectionParserParameterList;
269 //! Smart pointer type for managing a SelectionParserParameterList.
270 typedef gmx::gmx_unique_ptr<SelectionParserParameterList>::type
271 SelectionParserParameterListPointer;
274 * Describes a parsed method parameter.
276 * \ingroup module_selection
278 class SelectionParserParameter
281 //! Allocates and initializes an empty parameter list.
282 static SelectionParserParameterListPointer createList()
284 return SelectionParserParameterListPointer(
285 new SelectionParserParameterList);
288 * Allocates and initializes a parsed method parameter.
290 * \param[in] name Name for the new parameter (can be NULL).
291 * \param[in] values List of values for the parameter.
292 * \returns Pointer to the newly allocated parameter.
293 * \throws std::bad_alloc if out of memory.
295 static SelectionParserParameter
296 create(const char *name, SelectionParserValueListPointer values)
298 return SelectionParserParameter(name, move(values));
300 //! \copydoc create(const char *, SelectionParserValueListPointer)
301 static SelectionParserParameter
302 create(const std::string &name, SelectionParserValueListPointer values)
304 return SelectionParserParameter(name.c_str(), move(values));
307 * Allocates and initializes a parsed method parameter.
309 * \param[in] name Name for the new parameter (can be NULL).
310 * \param[in] value Value for the parameter.
311 * \returns Pointer to the newly allocated parameter.
312 * \throws std::bad_alloc if out of memory.
314 * This overload is a convenience wrapper for the case when creating
315 * parameters outside the actual Bison parser and only a single value
318 static SelectionParserParameter
319 create(const char *name, const SelectionParserValue &value)
321 return create(name, SelectionParserValue::createList(value));
324 * Allocates and initializes a parsed method parameter.
326 * \param[in] name Name for the new parameter (can be NULL).
327 * \param[in] expr Expression value for the parameter.
328 * \returns Pointer to the newly allocated parameter.
329 * \throws std::bad_alloc if out of memory.
331 * This overload is a convenience wrapper for the case when creating
332 * parameters outside the actual Bison parser and only a single
333 * expression value is necessary.
335 static SelectionParserParameter
336 createFromExpression(const char *name,
337 const SelectionTreeElementPointer &expr)
339 return create(name, SelectionParserValue::createExpr(expr));
341 //! \copydoc createFromExpression(const char *, const SelectionTreeElementPointer &)
342 static SelectionParserParameter
343 createFromExpression(const std::string &name,
344 const SelectionTreeElementPointer &expr)
346 return create(name.c_str(), SelectionParserValue::createExpr(expr));
350 * Initializes a parsed method parameter.
352 * \param[in] name Name for the new parameter (can be NULL).
353 * \param[in] values List of values for the parameter.
354 * \throws std::bad_alloc if out of memory.
356 SelectionParserParameter(const char *name,
357 SelectionParserValueListPointer values);
359 //! Returns the name of the parameter (may be empty).
360 const std::string &name() const { return name_; }
361 //! Returns the values for the parameter.
362 const SelectionParserValueList &values() const { return *values_; }
364 //! Name of the parameter.
366 //! Values for this parameter.
367 SelectionParserValueListPointer values_;
372 /** Error reporting function for the selection parser. */
374 _gmx_selparser_error(void *scanner, const char *fmt, ...);
375 /** Handle exceptions caught within the Bison code. */
377 _gmx_selparser_handle_exception(void *scanner, const std::exception &ex);
379 /** Propagates the flags for selection elements. */
381 _gmx_selelem_update_flags(const gmx::SelectionTreeElementPointer &sel,
384 /** Initializes the method parameter data of \ref SEL_EXPRESSION and
385 * \ref SEL_MODIFIER elements. */
387 _gmx_selelem_init_method_params(const gmx::SelectionTreeElementPointer &sel,
389 /** Initializes the method for a \ref SEL_EXPRESSION selection element. */
391 _gmx_selelem_set_method(const gmx::SelectionTreeElementPointer &sel,
392 struct gmx_ana_selmethod_t *method, void *scanner);
394 /** Creates a gmx::SelectionTreeElement for arithmetic expression evaluation. */
395 gmx::SelectionTreeElementPointer
396 _gmx_sel_init_arithmetic(const gmx::SelectionTreeElementPointer &left,
397 const gmx::SelectionTreeElementPointer &right,
398 char op, void *scanner);
399 /** Creates a gmx::SelectionTreeElement for comparsion expression evaluation. */
400 gmx::SelectionTreeElementPointer
401 _gmx_sel_init_comparison(const gmx::SelectionTreeElementPointer &left,
402 const gmx::SelectionTreeElementPointer &right,
403 const char *cmpop, void *scanner);
404 /** Creates a gmx::SelectionTreeElement for a keyword expression from the parsed data. */
405 gmx::SelectionTreeElementPointer
406 _gmx_sel_init_keyword(struct gmx_ana_selmethod_t *method,
407 gmx::SelectionParserValueListPointer args,
408 const char *rpost, void *scanner);
409 /** Creates a gmx::SelectionTreeElement for string-matching keyword expression. */
410 gmx::SelectionTreeElementPointer
411 _gmx_sel_init_keyword_strmatch(struct gmx_ana_selmethod_t *method,
412 gmx::SelectionStringMatchType matchType,
413 gmx::SelectionParserValueListPointer args,
414 const char *rpost, void *scanner);
415 /** Creates a gmx::SelectionTreeElement for a method expression from the parsed data. */
416 gmx::SelectionTreeElementPointer
417 _gmx_sel_init_method(struct gmx_ana_selmethod_t *method,
418 gmx::SelectionParserParameterListPointer params,
419 const char *rpost, void *scanner);
420 /** Creates a gmx::SelectionTreeElement for a modifier expression from the parsed data. */
421 gmx::SelectionTreeElementPointer
422 _gmx_sel_init_modifier(struct gmx_ana_selmethod_t *mod,
423 gmx::SelectionParserParameterListPointer params,
424 const gmx::SelectionTreeElementPointer &sel,
426 /** Creates a gmx::SelectionTreeElement for evaluation of reference positions. */
427 gmx::SelectionTreeElementPointer
428 _gmx_sel_init_position(const gmx::SelectionTreeElementPointer &expr,
429 const char *type, void *scanner);
431 /** Creates a gmx::SelectionTreeElement for a constant position. */
432 gmx::SelectionTreeElementPointer
433 _gmx_sel_init_const_position(real x, real y, real z);
434 /** Creates a gmx::SelectionTreeElement for a index group expression using group name. */
435 gmx::SelectionTreeElementPointer
436 _gmx_sel_init_group_by_name(const char *name, void *scanner);
437 /** Creates a gmx::SelectionTreeElement for a index group expression using group index. */
438 gmx::SelectionTreeElementPointer
439 _gmx_sel_init_group_by_id(int id, void *scanner);
440 /** Creates a gmx::SelectionTreeElement for a variable reference */
441 gmx::SelectionTreeElementPointer
442 _gmx_sel_init_variable_ref(const gmx::SelectionTreeElementPointer &sel);
444 /** Creates a root gmx::SelectionTreeElement for a selection. */
445 gmx::SelectionTreeElementPointer
446 _gmx_sel_init_selection(const char *name,
447 const gmx::SelectionTreeElementPointer &sel,
449 /** Creates a root gmx::SelectionTreeElement elements for a variable assignment. */
450 gmx::SelectionTreeElementPointer
451 _gmx_sel_assign_variable(const char *name,
452 const gmx::SelectionTreeElementPointer &expr,
454 /** Appends a root gmx::SelectionTreeElement to a selection collection. */
455 gmx::SelectionTreeElementPointer
456 _gmx_sel_append_selection(const gmx::SelectionTreeElementPointer &sel,
457 gmx::SelectionTreeElementPointer last,
459 /** Check whether the parser should finish. */
461 _gmx_sel_parser_should_finish(void *scanner);
463 /** Handle empty commands. */
465 _gmx_sel_handle_empty_cmd(void *scanner);
466 /** Process help commands. */
468 _gmx_sel_handle_help_cmd(const gmx::SelectionParserValueListPointer &topic,
472 /** Initializes an array of parameters based on input from the selection parser. */
474 _gmx_sel_parse_params(const gmx::SelectionParserParameterList ¶ms,
475 int nparam, struct gmx_ana_selparam_t *param,
476 const gmx::SelectionTreeElementPointer &root,