2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012,2013,2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements functions in selparam.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_selection
47 #include "gromacs/math/vec.h"
48 #include "gromacs/selection/position.h"
49 #include "gromacs/utility/cstringutil.h"
50 #include "gromacs/utility/exceptions.h"
51 #include "gromacs/utility/gmxassert.h"
52 #include "gromacs/utility/scoped_cptr.h"
53 #include "gromacs/utility/smalloc.h"
54 #include "gromacs/utility/stringutil.h"
56 #include "parsetree.h"
59 #include "selmethod.h"
65 * \param[in] name Name of the parameter to search.
66 * \param[in] nparam Number of parameters in the \p param array.
67 * \param[in] param Parameter array to search.
68 * \returns Pointer to the parameter in the \p param
69 * or NULL if no parameter with name \p name was found.
71 * The comparison is case-sensitive.
74 gmx_ana_selparam_find(const char *name, int nparam, gmx_ana_selparam_t *param)
82 /* Find the first non-null parameter */
84 while (i < nparam && param[i].name == NULL)
88 /* Process the special case of a NULL parameter */
91 return (i == 0) ? NULL : ¶m[i-1];
93 for (; i < nparam; ++i)
95 if (!strcmp(param[i].name, name))
99 /* Check for 'no' prefix on boolean parameters */
100 if (param[i].val.type == NO_VALUE
101 && strlen(name) > 2 && name[0] == 'n' && name[1] == 'o'
102 && !strcmp(param[i].name, name+2))
111 * Does a type conversion on a SelectionParserValue.
113 * \param[in,out] value Value to convert.
114 * \param[in] type Type to convert to.
115 * \param[in] errors Errors will be reported into this as nested exceptions.
116 * \param[in] scanner Scanner data structure.
119 convert_value(SelectionParserValue *value, e_selvalue_t type,
120 ExceptionInitializer *errors, void *scanner)
122 if (value->type == type || type == NO_VALUE)
126 if (value->hasExpressionValue())
128 /* Conversion from atom selection to position using default
129 * reference positions. */
130 if (value->type == GROUP_VALUE && type == POS_VALUE)
134 SelectionTreeElementPointer expr =
135 _gmx_sel_init_position(value->expr, NULL, scanner);
136 *value = SelectionParserValue::createExpr(expr);
138 catch (UserInputError &ex)
140 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
141 std::string context(formatString("In '%s'", text.c_str()));
142 ex.prependContext(context);
143 errors->addCurrentExceptionAsNested();
150 /* Integers to floating point are easy */
151 if (value->type == INT_VALUE && type == REAL_VALUE)
153 *value = SelectionParserValue::createRealRange(value->u.i.i1,
158 /* Reals that are integer-valued can also be converted */
159 if (value->type == REAL_VALUE && type == INT_VALUE)
161 int i1 = static_cast<int>(value->u.r.r1);
162 int i2 = static_cast<int>(value->u.r.r2);
163 if (gmx_within_tol(value->u.r.r1, i1, GMX_REAL_EPS)
164 && gmx_within_tol(value->u.r.r2, i2, GMX_REAL_EPS))
166 *value = SelectionParserValue::createIntegerRange(i1, i2, value->location());
171 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
173 formatString("Expression '%s' evaluates to a type is not valid in this context",
175 InvalidInputError ex(message);
176 errors->addNested(ex);
180 * Does a type conversion on a list of values.
182 * \param[in,out] values Values to convert.
183 * \param[in] type Type to convert to.
184 * \param[in] scanner Scanner data structure.
187 convert_values(SelectionParserValueList *values, e_selvalue_t type, void *scanner)
189 ExceptionInitializer errors("");
190 SelectionParserValueList::iterator value;
191 for (value = values->begin(); value != values->end(); ++value)
193 convert_value(&*value, type, &errors, scanner);
195 if (errors.hasNestedExceptions())
197 GMX_THROW(InvalidInputError(errors));
202 * Adds a child element for a parameter, keeping the parameter order.
204 * \param[in,out] root Root element to which the child is added.
205 * \param[in] child Child to add.
206 * \param[in] param Parameter for which this child is a value.
208 * Puts \p child in the child list of \p root such that the list remains
209 * in the same order as the corresponding parameters.
212 place_child(const SelectionTreeElementPointer &root,
213 const SelectionTreeElementPointer &child,
214 gmx_ana_selparam_t *param)
216 gmx_ana_selparam_t *ps;
219 ps = root->u.expr.method->param;
221 /* Put the child element in the correct place */
222 if (!root->child || n < root->child->u.param - ps)
224 child->next = root->child;
229 SelectionTreeElementPointer prev = root->child;
230 while (prev->next && prev->next->u.param - ps >= n)
234 child->next = prev->next;
240 * Comparison function for sorting integer ranges.
242 * \param[in] a Pointer to the first range.
243 * \param[in] b Pointer to the second range.
244 * \returns -1, 0, or 1 depending on the relative order of \p a and \p b.
246 * The ranges are primarily sorted based on their starting point, and
247 * secondarily based on length (longer ranges come first).
250 cmp_int_range(const void *a, const void *b)
252 if (((int *)a)[0] < ((int *)b)[0])
256 if (((int *)a)[0] > ((int *)b)[0])
260 if (((int *)a)[1] > ((int *)b)[1])
268 * Comparison function for sorting real ranges.
270 * \param[in] a Pointer to the first range.
271 * \param[in] b Pointer to the second range.
272 * \returns -1, 0, or 1 depending on the relative order of \p a and \p b.
274 * The ranges are primarily sorted based on their starting point, and
275 * secondarily based on length (longer ranges come first).
278 cmp_real_range(const void *a, const void *b)
280 if (((real *)a)[0] < ((real *)b)[0])
284 if (((real *)a)[0] > ((real *)b)[0])
288 if (((real *)a)[1] > ((real *)b)[1])
296 * Parses the values for a parameter that takes integer or real ranges.
298 * \param[in] values List of values.
299 * \param param Parameter to parse.
300 * \param[in] scanner Scanner data structure.
303 parse_values_range(const SelectionParserValueList &values,
304 gmx_ana_selparam_t *param, void *scanner)
308 param->flags &= ~SPAR_DYNAMIC;
309 GMX_RELEASE_ASSERT(param->val.type == INT_VALUE || param->val.type == REAL_VALUE,
310 "Invalid range parameter type");
313 scoped_guard_sfree dataGuard;
314 if (param->val.type == INT_VALUE)
316 snew(idata, values.size()*2);
317 dataGuard.reset(idata);
321 snew(rdata, values.size()*2);
322 dataGuard.reset(rdata);
325 SelectionParserValueList::const_iterator value;
326 for (value = values.begin(); value != values.end(); ++value)
328 GMX_RELEASE_ASSERT(value->type == param->val.type,
329 "Invalid range value type (should have been caught earlier)");
330 if (value->hasExpressionValue())
332 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
333 std::string message("Only simple values or 'A to B' ranges are "
334 "supported in this context");
335 InvalidInputError ex(message);
336 ex.prependContext(formatString("Invalid expression '%s'", text.c_str()));
339 if (param->val.type == INT_VALUE)
341 int i1 = std::min(value->u.i.i1, value->u.i.i2);
342 int i2 = std::max(value->u.i.i1, value->u.i.i2);
343 /* Check if the new range overlaps or extends the previous one */
344 if (i > 0 && i1 <= idata[i-1]+1 && i2 >= idata[i-2]-1)
346 idata[i-2] = std::min(idata[i-2], i1);
347 idata[i-1] = std::max(idata[i-1], i2);
357 real r1 = std::min(value->u.r.r1, value->u.r.r2);
358 real r2 = std::max(value->u.r.r1, value->u.r.r2);
359 /* Check if the new range overlaps or extends the previous one */
360 if (i > 0 && r1 <= rdata[i-1] && r2 >= rdata[i-2])
362 rdata[i-2] = std::min(rdata[i-2], r1);
363 rdata[i-1] = std::max(rdata[i-1], r2);
373 /* Sort the ranges and merge consequent ones */
374 if (param->val.type == INT_VALUE)
376 qsort(idata, n, 2*sizeof(int), &cmp_int_range);
377 for (i = j = 2; i < 2*n; i += 2)
379 if (idata[j-1]+1 >= idata[i])
381 if (idata[i+1] > idata[j-1])
383 idata[j-1] = idata[i+1];
389 idata[j+1] = idata[i+1];
396 qsort(rdata, n, 2*sizeof(real), &cmp_real_range);
397 for (i = j = 2; i < 2*n; i += 2)
399 if (rdata[j-1]+1 >= rdata[i])
401 if (rdata[i+1] > rdata[j-1])
403 rdata[j-1] = rdata[i+1];
409 rdata[j+1] = rdata[i+1];
415 /* Store the values */
416 if (param->flags & SPAR_VARNUM)
420 if (param->val.type == INT_VALUE)
423 _gmx_selvalue_setstore_alloc(¶m->val, idata, j);
428 _gmx_selvalue_setstore_alloc(¶m->val, rdata, j);
433 if (n != param->val.nr)
436 "Range parameters with a fixed count > 1 do not make sense");
437 GMX_THROW(InvalidInputError("Only one value or 'A to B' range is "
438 "supported in this context"));
440 if (param->val.type == INT_VALUE)
442 memcpy(param->val.u.i, idata, 2*n*sizeof(int));
446 memcpy(param->val.u.r, rdata, 2*n*sizeof(real));
451 *param->nvalptr = param->val.nr;
453 param->nvalptr = NULL;
457 * Parses the values for a parameter that takes a variable number of values.
459 * \param[in] values List of values.
460 * \param param Parameter to parse.
461 * \param root Selection element to which child expressions are added.
462 * \param[in] scanner Scanner data structure.
464 * For integer ranges, the sequence of numbers from the first to second value
465 * is stored, each as a separate value.
468 parse_values_varnum(const SelectionParserValueList &values,
469 gmx_ana_selparam_t *param,
470 const SelectionTreeElementPointer &root,
475 param->flags &= ~SPAR_DYNAMIC;
476 /* Compute number of values, considering also integer ranges. */
477 int valueCount = static_cast<int>(values.size());
478 if (param->val.type == INT_VALUE)
480 SelectionParserValueList::const_iterator value;
481 for (value = values.begin(); value != values.end(); ++value)
483 if (value->type == INT_VALUE && !value->hasExpressionValue())
485 valueCount += abs(value->u.i.i2 - value->u.i.i1);
490 /* Check that the value type is actually implemented */
491 if (param->val.type != INT_VALUE && param->val.type != REAL_VALUE
492 && param->val.type != STR_VALUE && param->val.type != POS_VALUE)
494 GMX_THROW(InternalError("Variable-count value type not implemented"));
497 /* Reserve appropriate amount of memory */
498 if (param->val.type == POS_VALUE)
500 gmx_ana_pos_reserve(param->val.u.p, valueCount, 0);
501 gmx_ana_indexmap_init(¶m->val.u.p->m, NULL, NULL, INDEX_UNKNOWN);
502 gmx_ana_pos_set_nr(param->val.u.p, valueCount);
506 _gmx_selvalue_reserve(¶m->val, valueCount);
508 /* Create a dummy child element to store the string values.
509 * This element is responsible for freeing the values, but carries no
511 if (param->val.type == STR_VALUE)
513 SelectionTreeElementPointer child(
514 new SelectionTreeElement(SEL_CONST, SelectionLocation::createEmpty()));
515 _gmx_selelem_set_vtype(child, STR_VALUE);
516 child->setName(param->name);
517 child->flags &= ~SEL_ALLOCVAL;
518 child->flags |= SEL_FLAGSSET | SEL_VARNUMVAL | SEL_ALLOCDATA;
519 child->v.nr = valueCount;
520 _gmx_selvalue_setstore(&child->v, param->val.u.s);
521 /* Because the child is not group-valued, the u union is not used
522 * for anything, so we can abuse it by storing the parameter value
523 * as place_child() expects, but this is really ugly... */
524 child->u.param = param;
525 place_child(root, child, param);
527 param->val.nr = valueCount;
530 SelectionParserValueList::const_iterator value;
531 for (value = values.begin(); value != values.end(); ++value)
533 GMX_RELEASE_ASSERT(value->type == param->val.type,
534 "Invalid value type (should have been caught earlier)");
535 if (value->hasExpressionValue())
537 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
538 std::string message("Selection expressions are not supported in this "
539 "context when multiple values are provided");
540 InvalidInputError ex(message);
541 ex.prependContext(formatString("Invalid expression '%s'", text.c_str()));
544 switch (param->val.type)
547 if (value->u.i.i1 <= value->u.i.i2)
549 for (j = value->u.i.i1; j <= value->u.i.i2; ++j)
551 param->val.u.i[i++] = j;
556 for (j = value->u.i.i1; j >= value->u.i.i2; --j)
558 param->val.u.i[i++] = j;
563 if (value->u.r.r1 != value->u.r.r2)
565 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
567 = formatString("Real range ('%s') is not supported in this context",
569 InvalidInputError ex(message);
572 param->val.u.r[i++] = value->u.r.r1;
575 param->val.u.s[i++] = gmx_strdup(value->stringValue().c_str());
577 case POS_VALUE: copy_rvec(value->u.x, param->val.u.p->x[i++]); break;
578 default: /* Should not be reached */
579 GMX_RELEASE_ASSERT(false, "Variable-count value type not implemented");
582 GMX_RELEASE_ASSERT(i == valueCount,
583 "Inconsistent value count wrt. the actual value population");
586 *param->nvalptr = param->val.nr;
588 param->nvalptr = NULL;
592 * Adds a new subexpression reference to a selection element.
594 * \param[in,out] root Root element to which the subexpression is added.
595 * \param[in] param Parameter for which this expression is a value.
596 * \param[in] expr Expression to add.
597 * \param[in] scanner Scanner data structure.
598 * \returns The created child element.
600 * Creates a new \ref SEL_SUBEXPRREF element and adds it into the child
602 * If \p expr is already a \ref SEL_SUBEXPRREF, it is used as it is.
603 * \ref SEL_ALLOCVAL is cleared for the returned element.
605 static SelectionTreeElementPointer
606 add_child(const SelectionTreeElementPointer &root, gmx_ana_selparam_t *param,
607 const SelectionTreeElementPointer &expr, void *scanner)
609 GMX_RELEASE_ASSERT(root->type == SEL_EXPRESSION || root->type == SEL_MODIFIER,
610 "Unsupported root element for selection parameter parser");
611 SelectionTreeElementPointer child;
612 /* Create a subexpression reference element if necessary */
613 if (expr->type == SEL_SUBEXPRREF)
619 // TODO: Initialize such that it includes the parameter.
620 child.reset(new SelectionTreeElement(SEL_SUBEXPRREF, expr->location()));
621 _gmx_selelem_set_vtype(child, expr->v.type);
624 /* Setup the child element */
625 child->flags &= ~SEL_ALLOCVAL;
626 child->u.param = param;
627 if (child->v.type != param->val.type)
629 // TODO: It would be nice to say what is the expected type.
630 std::string text(_gmx_sel_lexer_get_text(scanner, expr->location()));
632 = formatString("Expression '%s' is not valid in this context "
633 "(produces the wrong type of values)",
635 GMX_THROW(InvalidInputError(message));
637 _gmx_selelem_update_flags(child);
638 if ((child->flags & SEL_DYNAMIC) && !(param->flags & SPAR_DYNAMIC))
640 std::string text(_gmx_sel_lexer_get_text(scanner, expr->location()));
642 = formatString("Expression '%s' is dynamic, which is not "
643 "valid in this context",
645 GMX_THROW(InvalidInputError(message));
647 if (!(child->flags & SEL_DYNAMIC))
649 param->flags &= ~SPAR_DYNAMIC;
651 /* Put the child element in the correct place */
652 place_child(root, child, param);
657 * Parses an expression value for a parameter that takes a variable number of values.
659 * \param[in] values List of values.
660 * \param param Parameter to parse.
661 * \param root Selection element to which child expressions are added.
662 * \param[in] scanner Scanner data structure.
665 parse_values_varnum_expr(const SelectionParserValueList &values,
666 gmx_ana_selparam_t *param,
667 const SelectionTreeElementPointer &root,
670 GMX_RELEASE_ASSERT(values.size() == 1 && values.front().hasExpressionValue(),
671 "Called with an invalid type of value");
673 SelectionTreeElementPointer child
674 = add_child(root, param, values.front().expr, scanner);
676 /* Process single-valued expressions */
677 /* TODO: We should also handle SEL_SINGLEVAL expressions here */
678 if (child->v.type == POS_VALUE || child->v.type == GROUP_VALUE)
680 /* Set the value storage */
681 _gmx_selvalue_setstore(&child->v, param->val.u.ptr);
685 *param->nvalptr = param->val.nr;
687 param->nvalptr = NULL;
691 if (!(child->flags & SEL_VARNUMVAL))
693 std::string text(_gmx_sel_lexer_get_text(scanner, values.front().location()));
695 = formatString("Expression '%s' is invalid in this context",
697 GMX_THROW(InvalidInputError(message));
700 child->flags |= SEL_ALLOCVAL;
702 *param->nvalptr = param->val.nr;
703 /* Rest of the initialization is done during compilation in
708 * Initializes the storage of an expression value.
710 * \param[in,out] sel Selection element that evaluates the value.
711 * \param[in] param Parameter to receive the value.
712 * \param[in] i The value of \p sel evaluates the value \p i for
714 * \param[in] scanner Scanner data structure.
716 * Initializes the data pointer of \p sel such that the result is stored
717 * as the value \p i of \p param.
718 * This function is used internally by parse_values_std().
721 set_expr_value_store(const SelectionTreeElementPointer &sel,
722 gmx_ana_selparam_t *param, int i, void *scanner)
724 if (sel->v.type != GROUP_VALUE && !(sel->flags & SEL_SINGLEVAL))
726 std::string text(_gmx_sel_lexer_get_text(scanner, sel->location()));
728 = formatString("Expression '%s' is invalid in this context",
730 GMX_THROW(InvalidInputError(message));
734 case INT_VALUE: sel->v.u.i = ¶m->val.u.i[i]; break;
735 case REAL_VALUE: sel->v.u.r = ¶m->val.u.r[i]; break;
736 case STR_VALUE: sel->v.u.s = ¶m->val.u.s[i]; break;
737 case POS_VALUE: sel->v.u.p = ¶m->val.u.p[i]; break;
738 case GROUP_VALUE: sel->v.u.g = ¶m->val.u.g[i]; break;
740 GMX_THROW(InternalError("Invalid value type"));
747 * Parses the values for a parameter that takes a constant number of values.
749 * \param[in] values List of values.
750 * \param param Parameter to parse.
751 * \param root Selection element to which child expressions are added.
752 * \param[in] scanner Scanner data structure.
754 * For integer ranges, the sequence of numbers from the first to second value
755 * is stored, each as a separate value.
758 parse_values_std(const SelectionParserValueList &values,
759 gmx_ana_selparam_t *param,
760 const SelectionTreeElementPointer &root, void *scanner)
765 /* Handle atom-valued parameters */
766 if (param->flags & SPAR_ATOMVAL)
768 if (values.size() > 1)
770 GMX_THROW(InvalidInputError(
771 "Only a single value or a single expression is "
772 "supported in this context"));
774 if (values.front().hasExpressionValue())
776 SelectionTreeElementPointer child
777 = add_child(root, param, values.front().expr, scanner);
778 child->flags |= SEL_ALLOCVAL;
779 if (child->v.type != GROUP_VALUE && (child->flags & SEL_ATOMVAL))
781 /* Rest of the initialization is done during compilation in
783 /* TODO: Positions are not correctly handled */
787 *param->nvalptr = -1;
791 param->flags &= ~SPAR_ATOMVAL;
797 param->nvalptr = NULL;
798 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE
799 || param->val.type == STR_VALUE)
801 _gmx_selvalue_reserve(¶m->val, 1);
803 set_expr_value_store(child, param, 0, scanner);
806 /* If we reach here, proceed with normal parameter handling */
808 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE
809 || param->val.type == STR_VALUE)
811 _gmx_selvalue_reserve(¶m->val, 1);
813 param->flags &= ~SPAR_ATOMVAL;
814 param->flags &= ~SPAR_DYNAMIC;
819 SelectionParserValueList::const_iterator value;
820 for (value = values.begin(); value != values.end() && i < param->val.nr; ++value)
822 GMX_RELEASE_ASSERT(value->type == param->val.type,
823 "Invalid value type (should have been caught earlier)");
824 if (value->hasExpressionValue())
826 SelectionTreeElementPointer child
827 = add_child(root, param, value->expr, scanner);
828 set_expr_value_store(child, param, i, scanner);
829 if (child->flags & SEL_DYNAMIC)
836 /* Value is not an expression */
840 if (value->u.i.i1 <= value->u.i.i2)
842 for (j = value->u.i.i1; j <= value->u.i.i2 && i < param->val.nr; ++j)
844 param->val.u.i[i++] = j;
846 if (j != value->u.i.i2 + 1)
848 _gmx_selparser_error(scanner, "extra values skipped");
853 for (j = value->u.i.i1; j >= value->u.i.i2 && i < param->val.nr; --j)
855 param->val.u.i[i++] = j;
857 if (j != value->u.i.i2 - 1)
859 _gmx_selparser_error(scanner, "extra values skipped");
865 if (value->u.r.r1 != value->u.r.r2)
867 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
869 = formatString("Real range ('%s') is not supported in this context",
871 GMX_THROW(InvalidInputError(message));
873 param->val.u.r[i] = value->u.r.r1;
876 param->val.u.s[i] = gmx_strdup(value->stringValue().c_str());
879 gmx_ana_pos_init_const(¶m->val.u.p[i], value->u.x);
883 GMX_THROW(InternalError("Invalid non-expression value type"));
888 if (value != values.end())
891 = formatString("Too many values provided, expected %d",
893 GMX_THROW(InvalidInputError(message));
895 if (i < param->val.nr)
898 = formatString("Too few values provided, expected %d",
900 GMX_THROW(InvalidInputError(message));
904 param->flags &= ~SPAR_DYNAMIC;
908 *param->nvalptr = param->val.nr;
910 param->nvalptr = NULL;
914 * Parses the values for a boolean parameter.
916 * \param[in] name Name by which the parameter was given.
917 * \param[in] values List of values.
918 * \param param Parameter to parse.
919 * \param[in] scanner Scanner data structure.
922 parse_values_bool(const std::string &name,
923 const SelectionParserValueList &values,
924 gmx_ana_selparam_t *param, void *scanner)
926 GMX_UNUSED_VALUE(scanner);
927 GMX_ASSERT(param->val.type == NO_VALUE,
928 "Boolean parser called for non-boolean parameter");
929 if (values.size() > 1 || (!values.empty() && values.front().type != INT_VALUE))
932 = formatString("'%s' only accepts yes/no/on/off/0/1 (and empty) as a value",
934 GMX_THROW(InvalidInputError(message));
938 /* Check if the parameter name is given with a 'no' prefix */
939 if (name.length() > 2 && name[0] == 'n' && name[1] == 'o'
940 && name.compare(2, name.length() - 2, param->name) == 0)
944 if (bSetNo && !values.empty())
947 = formatString("'no%s' cannot be followed by any value",
949 GMX_THROW(InvalidInputError(message));
951 if (!values.empty() && values.front().u.i.i1 == 0)
956 *param->val.u.b = bSetNo ? false : true;
960 * Parses the values for an enumeration parameter.
962 * \param[in] values List of values.
963 * \param param Parameter to parse.
964 * \param[in] scanner Scanner data structure.
965 * \returns true if the values were parsed successfully, false otherwise.
968 parse_values_enum(const SelectionParserValueList &values,
969 gmx_ana_selparam_t *param,
972 GMX_ASSERT(param->val.type == STR_VALUE,
973 "Enum parser called for non-string parameter");
974 if (values.size() != 1)
976 GMX_THROW(InvalidInputError(
977 "Only a single string value is supported in this context"));
979 const SelectionParserValue &value = values.front();
980 GMX_RELEASE_ASSERT(value.type == param->val.type,
981 "Invalid value type (should have been caught earlier)");
982 if (value.hasExpressionValue())
984 std::string text(_gmx_sel_lexer_get_text(scanner, value.location()));
986 = formatString("Expression ('%s') is not supported in this context",
988 GMX_THROW(InvalidInputError(message));
991 const std::string &svalue = value.stringValue();
994 while (param->val.u.s[i] != NULL)
996 if (startsWith(param->val.u.s[i], svalue))
998 /* Check if there is a duplicate match */
1002 = formatString("Value '%s' is ambiguous", svalue.c_str());
1003 GMX_THROW(InvalidInputError(message));
1012 = formatString("Value '%s' is not recognized", svalue.c_str());
1013 GMX_THROW(InvalidInputError(message));
1015 param->val.u.s[0] = param->val.u.s[match];
1019 * Replaces constant expressions with their values.
1021 * \param[in,out] values First element in the value list to process.
1024 convert_const_values(SelectionParserValueList *values)
1026 SelectionParserValueList::iterator value;
1027 for (value = values->begin(); value != values->end(); ++value)
1029 if (value->hasExpressionValue() && value->expr->v.type != GROUP_VALUE &&
1030 value->expr->type == SEL_CONST)
1032 SelectionTreeElementPointer expr = value->expr;
1033 const SelectionLocation &location = value->location();
1034 switch (expr->v.type)
1037 *value = SelectionParserValue::createInteger(expr->v.u.i[0], location);
1040 *value = SelectionParserValue::createReal(expr->v.u.r[0], location);
1043 *value = SelectionParserValue::createString(expr->v.u.s[0], location);
1046 *value = SelectionParserValue::createPosition(expr->v.u.p->x[0], location);
1049 GMX_RELEASE_ASSERT(false,
1050 "Unsupported constant expression value type");
1057 * \param pparams List of parameters from the selection parser.
1058 * \param[in] nparam Number of parameters in \p params.
1059 * \param params Array of parameters to parse.
1060 * \param root Selection element to which child expressions are added.
1061 * \param[in] scanner Scanner data structure.
1063 * Initializes the \p params array based on the parameters in \p pparams.
1064 * See the documentation of \c gmx_ana_selparam_t for different options
1065 * available for parsing.
1067 * The list \p pparams and any associated values are freed after the parameters
1068 * have been processed, no matter is there was an error or not.
1071 _gmx_sel_parse_params(const gmx::SelectionParserParameterList &pparams,
1072 int nparam, gmx_ana_selparam_t *params,
1073 const gmx::SelectionTreeElementPointer &root,
1076 ExceptionInitializer errors("");
1077 /* Check that the value pointers of SPAR_VARNUM parameters are NULL and
1078 * that they are not NULL for other parameters */
1079 for (int i = 0; i < nparam; ++i)
1081 if (params[i].val.type != POS_VALUE
1082 && (params[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
1084 GMX_RELEASE_ASSERT(params[i].val.u.ptr == NULL,
1085 "value pointer is not NULL "
1086 "although it should be for SPAR_VARNUM "
1087 "and SPAR_ATOMVAL parameters");
1088 GMX_RELEASE_ASSERT(!((params[i].flags & SPAR_VARNUM)
1089 && (params[i].flags & SPAR_DYNAMIC))
1090 || params[i].nvalptr != NULL,
1091 "nvalptr is NULL but both "
1092 "SPAR_VARNUM and SPAR_DYNAMIC are specified");
1096 GMX_RELEASE_ASSERT(params[i].val.u.ptr != NULL,
1097 "value pointer is NULL");
1100 /* Parse the parameters */
1101 int nullParamIndex = 0;
1102 SelectionParserParameterList::const_iterator pparam;
1103 for (pparam = pparams.begin(); pparam != pparams.end(); ++pparam)
1107 // Always assigned afterwards, but cppcheck does not see that.
1108 gmx_ana_selparam_t *oparam = NULL;
1109 /* Find the parameter and make some checks */
1110 if (!pparam->name().empty())
1112 nullParamIndex = -1;
1114 = gmx_ana_selparam_find(pparam->name().c_str(), nparam, params);
1115 GMX_RELEASE_ASSERT(oparam != NULL, "Inconsistent selection parameter");
1117 else if (nullParamIndex >= 0)
1119 oparam = ¶ms[nullParamIndex];
1120 if (oparam->name != NULL)
1122 std::string text(_gmx_sel_lexer_get_text(scanner, pparam->location()));
1124 = formatString("Unexpected '%s'", text.c_str());
1125 GMX_THROW(InvalidInputError(message));
1131 GMX_RELEASE_ASSERT(false, "All NULL parameters should appear in "
1132 "the beginning of the list");
1134 if (oparam->flags & SPAR_SET)
1137 = formatString("'%s' appears multiple times",
1138 pparam->name().c_str());
1139 GMX_THROW(InvalidInputError(message));
1141 oparam->flags |= SPAR_SET;
1142 if (oparam->val.type != NO_VALUE && pparam->values().empty())
1145 if (pparam->name().empty())
1147 text = root->name();
1151 text = _gmx_sel_lexer_get_text(scanner, pparam->location());
1154 = formatString("'%s' should be followed by a value/expression",
1156 GMX_THROW(InvalidInputError(message));
1158 /* Process the values for the parameter */
1159 convert_const_values(pparam->values_.get());
1160 convert_values(pparam->values_.get(), oparam->val.type, scanner);
1161 if (oparam->val.type == NO_VALUE)
1163 parse_values_bool(pparam->name(), pparam->values(), oparam, scanner);
1165 else if (oparam->flags & SPAR_RANGES)
1167 parse_values_range(pparam->values(), oparam, scanner);
1169 else if (oparam->flags & SPAR_VARNUM)
1171 if (pparam->values().size() == 1
1172 && pparam->values().front().hasExpressionValue())
1174 parse_values_varnum_expr(pparam->values(), oparam, root, scanner);
1178 parse_values_varnum(pparam->values(), oparam, root, scanner);
1181 else if (oparam->flags & SPAR_ENUMVAL)
1183 parse_values_enum(pparam->values(), oparam, scanner);
1187 parse_values_std(pparam->values(), oparam, root, scanner);
1190 catch (UserInputError &ex)
1192 if (!pparam->name().empty())
1194 std::string text(_gmx_sel_lexer_get_text(scanner, pparam->location()));
1195 ex.prependContext(formatString("In '%s'", text.c_str()));
1197 errors.addCurrentExceptionAsNested();
1200 /* Check that all required parameters are present */
1201 for (int i = 0; i < nparam; ++i)
1203 if (!(params[i].flags & SPAR_OPTIONAL) && !(params[i].flags & SPAR_SET))
1205 std::string message;
1206 if (params[i].name == NULL)
1208 message = formatString("'%s' should be followed by a value/expression",
1209 root->name().c_str());
1213 message = formatString("'%s' is missing", params[i].name);
1215 InvalidInputError ex(message);
1216 errors.addNested(ex);
1219 if (errors.hasNestedExceptions())
1221 GMX_THROW(InvalidInputError(errors));