2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012,2013,2014,2015, 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] >= 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 */
842 if (value->u.i.i1 <= value->u.i.i2)
844 for (j = value->u.i.i1; j <= value->u.i.i2 && i < param->val.nr; ++j)
846 param->val.u.i[i++] = j;
848 bTooManyValues = (j != value->u.i.i2 + 1);
852 for (j = value->u.i.i1; j >= value->u.i.i2 && i < param->val.nr; --j)
854 param->val.u.i[i++] = j;
856 bTooManyValues = (j != value->u.i.i2 - 1);
860 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
862 = formatString("Range ('%s') produces more values than is "
863 "accepted in this context",
865 GMX_THROW(InvalidInputError(message));
871 if (value->u.r.r1 != value->u.r.r2)
873 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
875 = formatString("Real range ('%s') is not supported in this context",
877 GMX_THROW(InvalidInputError(message));
879 param->val.u.r[i] = value->u.r.r1;
882 param->val.u.s[i] = gmx_strdup(value->stringValue().c_str());
885 gmx_ana_pos_init_const(¶m->val.u.p[i], value->u.x);
889 GMX_THROW(InternalError("Invalid non-expression value type"));
894 if (value != values.end())
897 = formatString("Too many values provided, expected %d",
899 GMX_THROW(InvalidInputError(message));
901 if (i < param->val.nr)
904 = formatString("Too few values provided, expected %d",
906 GMX_THROW(InvalidInputError(message));
910 param->flags &= ~SPAR_DYNAMIC;
914 *param->nvalptr = param->val.nr;
916 param->nvalptr = NULL;
920 * Parses the values for a boolean parameter.
922 * \param[in] name Name by which the parameter was given.
923 * \param[in] values List of values.
924 * \param param Parameter to parse.
925 * \param[in] scanner Scanner data structure.
928 parse_values_bool(const std::string &name,
929 const SelectionParserValueList &values,
930 gmx_ana_selparam_t *param, void *scanner)
932 GMX_UNUSED_VALUE(scanner);
933 GMX_ASSERT(param->val.type == NO_VALUE,
934 "Boolean parser called for non-boolean parameter");
935 if (values.size() > 1 || (!values.empty() && values.front().type != INT_VALUE))
938 = formatString("'%s' only accepts yes/no/on/off/0/1 (and empty) as a value",
940 GMX_THROW(InvalidInputError(message));
944 /* Check if the parameter name is given with a 'no' prefix */
945 if (name.length() > 2 && name[0] == 'n' && name[1] == 'o'
946 && name.compare(2, name.length() - 2, param->name) == 0)
950 if (bSetNo && !values.empty())
953 = formatString("'no%s' cannot be followed by any value",
955 GMX_THROW(InvalidInputError(message));
957 if (!values.empty() && values.front().u.i.i1 == 0)
962 *param->val.u.b = bSetNo ? false : true;
966 * Parses the values for an enumeration parameter.
968 * \param[in] values List of values.
969 * \param param Parameter to parse.
970 * \param[in] scanner Scanner data structure.
971 * \returns true if the values were parsed successfully, false otherwise.
974 parse_values_enum(const SelectionParserValueList &values,
975 gmx_ana_selparam_t *param,
978 GMX_ASSERT(param->val.type == STR_VALUE,
979 "Enum parser called for non-string parameter");
980 if (values.size() != 1)
982 GMX_THROW(InvalidInputError(
983 "Only a single string value is supported in this context"));
985 const SelectionParserValue &value = values.front();
986 GMX_RELEASE_ASSERT(value.type == param->val.type,
987 "Invalid value type (should have been caught earlier)");
988 if (value.hasExpressionValue())
990 std::string text(_gmx_sel_lexer_get_text(scanner, value.location()));
992 = formatString("Expression ('%s') is not supported in this context",
994 GMX_THROW(InvalidInputError(message));
997 const std::string &svalue = value.stringValue();
1000 while (param->val.u.s[i] != NULL)
1002 if (startsWith(param->val.u.s[i], svalue))
1004 /* Check if there is a duplicate match */
1008 = formatString("Value '%s' is ambiguous", svalue.c_str());
1009 GMX_THROW(InvalidInputError(message));
1018 = formatString("Value '%s' is not recognized", svalue.c_str());
1019 GMX_THROW(InvalidInputError(message));
1021 param->val.u.s[0] = param->val.u.s[match];
1025 * Replaces constant expressions with their values.
1027 * \param[in,out] values First element in the value list to process.
1030 convert_const_values(SelectionParserValueList *values)
1032 SelectionParserValueList::iterator value;
1033 for (value = values->begin(); value != values->end(); ++value)
1035 if (value->hasExpressionValue() && value->expr->v.type != GROUP_VALUE &&
1036 value->expr->type == SEL_CONST)
1038 SelectionTreeElementPointer expr = value->expr;
1039 const SelectionLocation &location = value->location();
1040 switch (expr->v.type)
1043 *value = SelectionParserValue::createInteger(expr->v.u.i[0], location);
1046 *value = SelectionParserValue::createReal(expr->v.u.r[0], location);
1049 *value = SelectionParserValue::createString(expr->v.u.s[0], location);
1052 *value = SelectionParserValue::createPosition(expr->v.u.p->x[0], location);
1055 GMX_RELEASE_ASSERT(false,
1056 "Unsupported constant expression value type");
1063 * \param pparams List of parameters from the selection parser.
1064 * \param[in] nparam Number of parameters in \p params.
1065 * \param params Array of parameters to parse.
1066 * \param root Selection element to which child expressions are added.
1067 * \param[in] scanner Scanner data structure.
1069 * Initializes the \p params array based on the parameters in \p pparams.
1070 * See the documentation of \c gmx_ana_selparam_t for different options
1071 * available for parsing.
1073 * The list \p pparams and any associated values are freed after the parameters
1074 * have been processed, no matter is there was an error or not.
1077 _gmx_sel_parse_params(const gmx::SelectionParserParameterList &pparams,
1078 int nparam, gmx_ana_selparam_t *params,
1079 const gmx::SelectionTreeElementPointer &root,
1082 ExceptionInitializer errors("");
1083 /* Check that the value pointers of SPAR_VARNUM parameters are NULL and
1084 * that they are not NULL for other parameters */
1085 for (int i = 0; i < nparam; ++i)
1087 if (params[i].val.type != POS_VALUE
1088 && (params[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
1090 GMX_RELEASE_ASSERT(params[i].val.u.ptr == NULL,
1091 "value pointer is not NULL "
1092 "although it should be for SPAR_VARNUM "
1093 "and SPAR_ATOMVAL parameters");
1094 GMX_RELEASE_ASSERT(!((params[i].flags & SPAR_VARNUM)
1095 && (params[i].flags & SPAR_DYNAMIC))
1096 || params[i].nvalptr != NULL,
1097 "nvalptr is NULL but both "
1098 "SPAR_VARNUM and SPAR_DYNAMIC are specified");
1102 GMX_RELEASE_ASSERT(params[i].val.u.ptr != NULL,
1103 "value pointer is NULL");
1106 /* Parse the parameters */
1107 int nullParamIndex = 0;
1108 SelectionParserParameterList::const_iterator pparam;
1109 for (pparam = pparams.begin(); pparam != pparams.end(); ++pparam)
1113 // Always assigned afterwards, but cppcheck does not see that.
1114 gmx_ana_selparam_t *oparam = NULL;
1115 /* Find the parameter and make some checks */
1116 if (!pparam->name().empty())
1118 nullParamIndex = -1;
1120 = gmx_ana_selparam_find(pparam->name().c_str(), nparam, params);
1121 GMX_RELEASE_ASSERT(oparam != NULL, "Inconsistent selection parameter");
1123 else if (nullParamIndex >= 0)
1125 oparam = ¶ms[nullParamIndex];
1126 if (oparam->name != NULL)
1128 std::string text(_gmx_sel_lexer_get_text(scanner, pparam->location()));
1130 = formatString("Unexpected '%s'", text.c_str());
1131 GMX_THROW(InvalidInputError(message));
1137 GMX_RELEASE_ASSERT(false, "All NULL parameters should appear in "
1138 "the beginning of the list");
1140 if (oparam->flags & SPAR_SET)
1143 = formatString("'%s' appears multiple times",
1144 pparam->name().c_str());
1145 GMX_THROW(InvalidInputError(message));
1147 oparam->flags |= SPAR_SET;
1148 if (oparam->val.type != NO_VALUE && pparam->values().empty())
1151 if (pparam->name().empty())
1153 text = root->name();
1157 text = _gmx_sel_lexer_get_text(scanner, pparam->location());
1160 = formatString("'%s' should be followed by a value/expression",
1162 GMX_THROW(InvalidInputError(message));
1164 /* Process the values for the parameter */
1165 convert_const_values(pparam->values_.get());
1166 convert_values(pparam->values_.get(), oparam->val.type, scanner);
1167 if (oparam->val.type == NO_VALUE)
1169 parse_values_bool(pparam->name(), pparam->values(), oparam, scanner);
1171 else if (oparam->flags & SPAR_RANGES)
1173 parse_values_range(pparam->values(), oparam, scanner);
1175 else if (oparam->flags & SPAR_VARNUM)
1177 if (pparam->values().size() == 1
1178 && pparam->values().front().hasExpressionValue())
1180 parse_values_varnum_expr(pparam->values(), oparam, root, scanner);
1184 parse_values_varnum(pparam->values(), oparam, root, scanner);
1187 else if (oparam->flags & SPAR_ENUMVAL)
1189 parse_values_enum(pparam->values(), oparam, scanner);
1193 parse_values_std(pparam->values(), oparam, root, scanner);
1196 catch (UserInputError &ex)
1198 if (!pparam->name().empty())
1200 std::string text(_gmx_sel_lexer_get_text(scanner, pparam->location()));
1201 ex.prependContext(formatString("In '%s'", text.c_str()));
1203 errors.addCurrentExceptionAsNested();
1206 /* Check that all required parameters are present */
1207 for (int i = 0; i < nparam; ++i)
1209 if (!(params[i].flags & SPAR_OPTIONAL) && !(params[i].flags & SPAR_SET))
1211 std::string message;
1212 if (params[i].name == NULL)
1214 message = formatString("'%s' should be followed by a value/expression",
1215 root->name().c_str());
1219 message = formatString("'%s' is missing", params[i].name);
1221 InvalidInputError ex(message);
1222 errors.addNested(ex);
1225 if (errors.hasNestedExceptions())
1227 GMX_THROW(InvalidInputError(errors));