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/selection/selmethod.h"
50 #include "gromacs/selection/selparam.h"
51 #include "gromacs/utility/cstringutil.h"
52 #include "gromacs/utility/exceptions.h"
53 #include "gromacs/utility/gmxassert.h"
54 #include "gromacs/utility/messagestringcollector.h"
55 #include "gromacs/utility/smalloc.h"
56 #include "gromacs/utility/stringutil.h"
58 #include "parsetree.h"
63 using gmx::SelectionParserValue;
64 using gmx::SelectionParserValueList;
65 using gmx::SelectionParserParameter;
66 using gmx::SelectionParserParameterList;
67 using gmx::SelectionTreeElement;
68 using gmx::SelectionTreeElementPointer;
71 * \param[in] name Name of the parameter to search.
72 * \param[in] nparam Number of parameters in the \p param array.
73 * \param[in] param Parameter array to search.
74 * \returns Pointer to the parameter in the \p param
75 * or NULL if no parameter with name \p name was found.
77 * The comparison is case-sensitive.
80 gmx_ana_selparam_find(const char *name, int nparam, gmx_ana_selparam_t *param)
88 /* Find the first non-null parameter */
90 while (i < nparam && param[i].name == NULL)
94 /* Process the special case of a NULL parameter */
97 return (i == 0) ? NULL : ¶m[i-1];
99 for (; i < nparam; ++i)
101 if (!strcmp(param[i].name, name))
105 /* Check for 'no' prefix on boolean parameters */
106 if (param[i].val.type == NO_VALUE
107 && strlen(name) > 2 && name[0] == 'n' && name[1] == 'o'
108 && !strcmp(param[i].name, name+2))
117 * Does a type conversion on a SelectionParserValue.
119 * \param[in,out] value Value to convert.
120 * \param[in] type Type to convert to.
121 * \param[in] scanner Scanner data structure.
122 * \returns 0 on success, a non-zero value on error.
125 convert_value(SelectionParserValue *value, e_selvalue_t type, void *scanner)
127 if (value->type == type || type == NO_VALUE)
131 if (value->hasExpressionValue())
133 /* Conversion from atom selection to position using default
134 * reference positions. */
135 if (value->type == GROUP_VALUE && type == POS_VALUE)
137 SelectionTreeElementPointer expr =
138 _gmx_sel_init_position(value->expr, NULL, scanner);
139 // FIXME: Use exceptions
144 *value = SelectionParserValue::createExpr(expr);
151 /* Integers to floating point are easy */
152 if (value->type == INT_VALUE && type == REAL_VALUE)
154 *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);
175 * Does a type conversion on a list of values.
177 * \param[in,out] values Values to convert.
178 * \param[in] type Type to convert to.
179 * \param[in] scanner Scanner data structure.
180 * \returns 0 on success, a non-zero value on error.
183 convert_values(SelectionParserValueList *values, e_selvalue_t type, void *scanner)
186 SelectionParserValueList::iterator value;
187 for (value = values->begin(); value != values->end(); ++value)
189 int rc1 = convert_value(&*value, type, scanner);
190 if (rc1 != 0 && rc == 0)
195 /* FIXME: More informative error messages */
200 * Adds a child element for a parameter, keeping the parameter order.
202 * \param[in,out] root Root element to which the child is added.
203 * \param[in] child Child to add.
204 * \param[in] param Parameter for which this child is a value.
206 * Puts \p child in the child list of \p root such that the list remains
207 * in the same order as the corresponding parameters.
210 place_child(const SelectionTreeElementPointer &root,
211 const SelectionTreeElementPointer &child,
212 gmx_ana_selparam_t *param)
214 gmx_ana_selparam_t *ps;
217 ps = root->u.expr.method->param;
219 /* Put the child element in the correct place */
220 if (!root->child || n < root->child->u.param - ps)
222 child->next = root->child;
227 SelectionTreeElementPointer prev = root->child;
228 while (prev->next && prev->next->u.param - ps >= n)
232 child->next = prev->next;
238 * Comparison function for sorting integer ranges.
240 * \param[in] a Pointer to the first range.
241 * \param[in] b Pointer to the second range.
242 * \returns -1, 0, or 1 depending on the relative order of \p a and \p b.
244 * The ranges are primarily sorted based on their starting point, and
245 * secondarily based on length (longer ranges come first).
248 cmp_int_range(const void *a, const void *b)
250 if (((int *)a)[0] < ((int *)b)[0])
254 if (((int *)a)[0] > ((int *)b)[0])
258 if (((int *)a)[1] > ((int *)b)[1])
266 * Comparison function for sorting real ranges.
268 * \param[in] a Pointer to the first range.
269 * \param[in] b Pointer to the second range.
270 * \returns -1, 0, or 1 depending on the relative order of \p a and \p b.
272 * The ranges are primarily sorted based on their starting point, and
273 * secondarily based on length (longer ranges come first).
276 cmp_real_range(const void *a, const void *b)
278 if (((real *)a)[0] < ((real *)b)[0])
282 if (((real *)a)[0] > ((real *)b)[0])
286 if (((real *)a)[1] > ((real *)b)[1])
294 * Parses the values for a parameter that takes integer or real ranges.
296 * \param[in] values List of values.
297 * \param param Parameter to parse.
298 * \param[in] scanner Scanner data structure.
299 * \returns true if the values were parsed successfully, false otherwise.
302 parse_values_range(const SelectionParserValueList &values,
303 gmx_ana_selparam_t *param, void *scanner)
309 param->flags &= ~SPAR_DYNAMIC;
310 GMX_RELEASE_ASSERT(param->val.type == INT_VALUE || param->val.type == REAL_VALUE,
311 "Invalid range parameter type");
314 if (param->val.type == INT_VALUE)
316 snew(idata, values.size()*2);
320 snew(rdata, values.size()*2);
323 SelectionParserValueList::const_iterator value;
324 for (value = values.begin(); value != values.end(); ++value)
326 if (value->hasExpressionValue())
328 _gmx_selparser_error(scanner, "expressions not supported within range parameters");
331 GMX_RELEASE_ASSERT(value->type == param->val.type,
332 "Invalid range value type (should have been caught earlier)");
333 if (param->val.type == INT_VALUE)
335 int i1 = std::min(value->u.i.i1, value->u.i.i2);
336 int i2 = std::max(value->u.i.i1, value->u.i.i2);
337 /* Check if the new range overlaps or extends the previous one */
338 if (i > 0 && i1 <= idata[i-1]+1 && i2 >= idata[i-2]-1)
340 idata[i-2] = std::min(idata[i-2], i1);
341 idata[i-1] = std::max(idata[i-1], i2);
351 real r1 = std::min(value->u.r.r1, value->u.r.r2);
352 real r2 = std::max(value->u.r.r1, value->u.r.r2);
353 /* Check if the new range overlaps or extends the previous one */
354 if (i > 0 && r1 <= rdata[i-1] && r2 >= rdata[i-2])
356 rdata[i-2] = std::min(rdata[i-2], r1);
357 rdata[i-1] = std::max(rdata[i-1], r2);
367 /* Sort the ranges and merge consequent ones */
368 if (param->val.type == INT_VALUE)
370 qsort(idata, n, 2*sizeof(int), &cmp_int_range);
371 for (i = j = 2; i < 2*n; i += 2)
373 if (idata[j-1]+1 >= idata[i])
375 if (idata[i+1] > idata[j-1])
377 idata[j-1] = idata[i+1];
383 idata[j+1] = idata[i+1];
390 qsort(rdata, n, 2*sizeof(real), &cmp_real_range);
391 for (i = j = 2; i < 2*n; i += 2)
393 if (rdata[j-1]+1 >= rdata[i])
395 if (rdata[i+1] > rdata[j-1])
397 rdata[j-1] = rdata[i+1];
403 rdata[j+1] = rdata[i+1];
409 /* Store the values */
410 if (param->flags & SPAR_VARNUM)
413 if (param->val.type == INT_VALUE)
416 _gmx_selvalue_setstore_alloc(¶m->val, idata, j);
421 _gmx_selvalue_setstore_alloc(¶m->val, rdata, j);
426 if (n != param->val.nr)
428 _gmx_selparser_error(scanner, "the value should consist of exactly one range");
433 if (param->val.type == INT_VALUE)
435 memcpy(param->val.u.i, idata, 2*n*sizeof(int));
440 memcpy(param->val.u.r, rdata, 2*n*sizeof(real));
446 *param->nvalptr = param->val.nr;
448 param->nvalptr = NULL;
454 * Parses the values for a parameter that takes a variable number of values.
456 * \param[in] values List of values.
457 * \param param Parameter to parse.
458 * \param root Selection element to which child expressions are added.
459 * \param[in] scanner Scanner data structure.
460 * \returns true if the values were parsed successfully, false otherwise.
462 * For integer ranges, the sequence of numbers from the first to second value
463 * is stored, each as a separate value.
466 parse_values_varnum(const SelectionParserValueList &values,
467 gmx_ana_selparam_t *param,
468 const SelectionTreeElementPointer &root,
473 param->flags &= ~SPAR_DYNAMIC;
474 /* Compute number of values, considering also integer ranges. */
475 size_t valueCount = values.size();
476 if (param->val.type == INT_VALUE)
478 SelectionParserValueList::const_iterator value;
479 for (value = values.begin(); value != values.end(); ++value)
481 if (value->type == INT_VALUE && !value->hasExpressionValue())
483 valueCount += abs(value->u.i.i2 - value->u.i.i1);
488 /* Check that the value type is actually implemented */
489 if (param->val.type != INT_VALUE && param->val.type != REAL_VALUE
490 && param->val.type != STR_VALUE && param->val.type != POS_VALUE)
492 GMX_THROW(gmx::InternalError("Variable-count value type not implemented"));
495 /* Reserve appropriate amount of memory */
496 if (param->val.type == POS_VALUE)
498 gmx_ana_pos_reserve(param->val.u.p, valueCount, 0);
499 gmx_ana_indexmap_init(¶m->val.u.p->m, NULL, NULL, INDEX_UNKNOWN);
500 gmx_ana_pos_set_nr(param->val.u.p, valueCount);
504 _gmx_selvalue_reserve(¶m->val, valueCount);
508 SelectionParserValueList::const_iterator value;
509 for (value = values.begin(); value != values.end(); ++value)
511 if (value->hasExpressionValue())
513 _gmx_selparser_error(scanner, "expressions not supported within value lists");
516 GMX_RELEASE_ASSERT(value->type == param->val.type,
517 "Invalid value type (should have been caught earlier)");
518 switch (param->val.type)
521 if (value->u.i.i1 <= value->u.i.i2)
523 for (j = value->u.i.i1; j <= value->u.i.i2; ++j)
525 param->val.u.i[i++] = j;
530 for (j = value->u.i.i1; j >= value->u.i.i2; --j)
532 param->val.u.i[i++] = j;
537 if (value->u.r.r1 != value->u.r.r2)
539 _gmx_selparser_error(scanner, "real ranges not supported");
542 param->val.u.r[i++] = value->u.r.r1;
545 param->val.u.s[i++] = gmx_strdup(value->stringValue().c_str());
547 case POS_VALUE: copy_rvec(value->u.x, param->val.u.p->x[i++]); break;
548 default: /* Should not be reached */
549 GMX_THROW(gmx::InternalError("Variable-count value type not implemented"));
555 *param->nvalptr = param->val.nr;
557 param->nvalptr = NULL;
558 /* Create a dummy child element to store the string values.
559 * This element is responsible for freeing the values, but carries no
561 if (param->val.type == STR_VALUE)
563 SelectionTreeElementPointer child(new SelectionTreeElement(SEL_CONST));
564 _gmx_selelem_set_vtype(child, STR_VALUE);
565 child->setName(param->name);
566 child->flags &= ~SEL_ALLOCVAL;
567 child->flags |= SEL_FLAGSSET | SEL_VARNUMVAL | SEL_ALLOCDATA;
568 child->v.nr = param->val.nr;
569 _gmx_selvalue_setstore(&child->v, param->val.u.s);
570 /* Because the child is not group-valued, the u union is not used
571 * for anything, so we can abuse it by storing the parameter value
572 * as place_child() expects, but this is really ugly... */
573 child->u.param = param;
574 place_child(root, child, param);
581 * Adds a new subexpression reference to a selection element.
583 * \param[in,out] root Root element to which the subexpression is added.
584 * \param[in] param Parameter for which this expression is a value.
585 * \param[in] expr Expression to add.
586 * \param[in] scanner Scanner data structure.
587 * \returns The created child element.
589 * Creates a new \ref SEL_SUBEXPRREF element and adds it into the child
591 * If \p expr is already a \ref SEL_SUBEXPRREF, it is used as it is.
592 * \ref SEL_ALLOCVAL is cleared for the returned element.
594 static SelectionTreeElementPointer
595 add_child(const SelectionTreeElementPointer &root, gmx_ana_selparam_t *param,
596 const SelectionTreeElementPointer &expr, void *scanner)
598 GMX_RELEASE_ASSERT(root->type == SEL_EXPRESSION || root->type == SEL_MODIFIER,
599 "Unsupported root element for selection parameter parser");
600 SelectionTreeElementPointer child;
601 /* Create a subexpression reference element if necessary */
602 if (expr->type == SEL_SUBEXPRREF)
608 child.reset(new SelectionTreeElement(SEL_SUBEXPRREF));
609 _gmx_selelem_set_vtype(child, expr->v.type);
612 /* Setup the child element */
613 child->flags &= ~SEL_ALLOCVAL;
614 child->u.param = param;
615 if (child->v.type != param->val.type)
617 _gmx_selparser_error(scanner, "invalid expression value");
618 // FIXME: Use exceptions.
619 return SelectionTreeElementPointer();
621 _gmx_selelem_update_flags(child);
622 if ((child->flags & SEL_DYNAMIC) && !(param->flags & SPAR_DYNAMIC))
624 _gmx_selparser_error(scanner, "dynamic values not supported");
625 // FIXME: Use exceptions.
626 return SelectionTreeElementPointer();
628 if (!(child->flags & SEL_DYNAMIC))
630 param->flags &= ~SPAR_DYNAMIC;
632 /* Put the child element in the correct place */
633 place_child(root, child, param);
638 * Parses an expression value for a parameter that takes a variable number of values.
640 * \param[in] values List of values.
641 * \param param Parameter to parse.
642 * \param root Selection element to which child expressions are added.
643 * \param[in] scanner Scanner data structure.
644 * \returns true if the values were parsed successfully, false otherwise.
647 parse_values_varnum_expr(const SelectionParserValueList &values,
648 gmx_ana_selparam_t *param,
649 const SelectionTreeElementPointer &root,
652 GMX_RELEASE_ASSERT(values.size() == 1 && values.front().hasExpressionValue(),
653 "Called with an invalid type of value");
655 SelectionTreeElementPointer child
656 = add_child(root, param, values.front().expr, scanner);
662 /* Process single-valued expressions */
663 /* TODO: We should also handle SEL_SINGLEVAL expressions here */
664 if (child->v.type == POS_VALUE || child->v.type == GROUP_VALUE)
666 /* Set the value storage */
667 _gmx_selvalue_setstore(&child->v, param->val.u.ptr);
671 *param->nvalptr = param->val.nr;
673 param->nvalptr = NULL;
677 if (!(child->flags & SEL_VARNUMVAL))
679 _gmx_selparser_error(scanner, "invalid expression value");
683 child->flags |= SEL_ALLOCVAL;
685 *param->nvalptr = param->val.nr;
686 /* Rest of the initialization is done during compilation in
693 * Initializes the storage of an expression value.
695 * \param[in,out] sel Selection element that evaluates the value.
696 * \param[in] param Parameter to receive the value.
697 * \param[in] i The value of \p sel evaluates the value \p i for
699 * \param[in] scanner Scanner data structure.
701 * Initializes the data pointer of \p sel such that the result is stored
702 * as the value \p i of \p param.
703 * This function is used internally by parse_values_std().
706 set_expr_value_store(const SelectionTreeElementPointer &sel,
707 gmx_ana_selparam_t *param, int i, void *scanner)
709 if (sel->v.type != GROUP_VALUE && !(sel->flags & SEL_SINGLEVAL))
711 _gmx_selparser_error(scanner, "invalid expression value");
716 case INT_VALUE: sel->v.u.i = ¶m->val.u.i[i]; break;
717 case REAL_VALUE: sel->v.u.r = ¶m->val.u.r[i]; break;
718 case STR_VALUE: sel->v.u.s = ¶m->val.u.s[i]; break;
719 case POS_VALUE: sel->v.u.p = ¶m->val.u.p[i]; break;
720 case GROUP_VALUE: sel->v.u.g = ¶m->val.u.g[i]; break;
722 GMX_THROW(gmx::InternalError("Invalid value type"));
730 * Parses the values for a parameter that takes a constant number of values.
732 * \param[in] values List of values.
733 * \param param Parameter to parse.
734 * \param root Selection element to which child expressions are added.
735 * \param[in] scanner Scanner data structure.
736 * \returns true if the values were parsed successfully, false otherwise.
738 * For integer ranges, the sequence of numbers from the first to second value
739 * is stored, each as a separate value.
742 parse_values_std(const SelectionParserValueList &values,
743 gmx_ana_selparam_t *param,
744 const SelectionTreeElementPointer &root, void *scanner)
749 /* Handle atom-valued parameters */
750 if (param->flags & SPAR_ATOMVAL)
752 if (values.size() > 1)
754 _gmx_selparser_error(scanner, "more than one value not supported");
757 if (values.front().hasExpressionValue())
759 SelectionTreeElementPointer child
760 = add_child(root, param, values.front().expr, scanner);
765 child->flags |= SEL_ALLOCVAL;
766 if (child->v.type != GROUP_VALUE && (child->flags & SEL_ATOMVAL))
768 /* Rest of the initialization is done during compilation in
770 /* TODO: Positions are not correctly handled */
774 *param->nvalptr = -1;
778 param->flags &= ~SPAR_ATOMVAL;
784 param->nvalptr = NULL;
785 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE
786 || param->val.type == STR_VALUE)
788 _gmx_selvalue_reserve(¶m->val, 1);
790 return set_expr_value_store(child, param, 0, scanner);
792 /* If we reach here, proceed with normal parameter handling */
794 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE
795 || param->val.type == STR_VALUE)
797 _gmx_selvalue_reserve(¶m->val, 1);
799 param->flags &= ~SPAR_ATOMVAL;
800 param->flags &= ~SPAR_DYNAMIC;
805 SelectionParserValueList::const_iterator value;
806 for (value = values.begin(); value != values.end() && i < param->val.nr; ++value)
808 if (value->type != param->val.type)
810 _gmx_selparser_error(scanner, "incorrect value skipped");
813 if (value->hasExpressionValue())
815 SelectionTreeElementPointer child
816 = add_child(root, param, value->expr, scanner);
817 /* Check that the expression is valid */
822 if (!set_expr_value_store(child, param, i, scanner))
826 if (child->flags & SEL_DYNAMIC)
833 /* Value is not an expression */
837 if (value->u.i.i1 <= value->u.i.i2)
839 for (j = value->u.i.i1; j <= value->u.i.i2 && i < param->val.nr; ++j)
841 param->val.u.i[i++] = j;
843 if (j != value->u.i.i2 + 1)
845 _gmx_selparser_error(scanner, "extra values skipped");
850 for (j = value->u.i.i1; j >= value->u.i.i2 && i < param->val.nr; --j)
852 param->val.u.i[i++] = j;
854 if (j != value->u.i.i2 - 1)
856 _gmx_selparser_error(scanner, "extra values skipped");
862 if (value->u.r.r1 != value->u.r.r2)
864 _gmx_selparser_error(scanner, "real ranges not supported");
867 param->val.u.r[i] = value->u.r.r1;
870 param->val.u.s[i] = gmx_strdup(value->stringValue().c_str());
873 gmx_ana_pos_init_const(¶m->val.u.p[i], value->u.x);
877 GMX_THROW(gmx::InternalError("Invalid non-expression value type"));
882 if (value != values.end())
884 _gmx_selparser_error(scanner, "extra values");
887 if (i < param->val.nr)
889 _gmx_selparser_error(scanner, "not enough values");
894 param->flags &= ~SPAR_DYNAMIC;
898 *param->nvalptr = param->val.nr;
900 param->nvalptr = NULL;
906 * Parses the values for a boolean parameter.
908 * \param[in] name Name by which the parameter was given.
909 * \param[in] values List of values.
910 * \param param Parameter to parse.
911 * \param[in] scanner Scanner data structure.
912 * \returns true if the values were parsed successfully, false otherwise.
915 parse_values_bool(const std::string &name,
916 const SelectionParserValueList &values,
917 gmx_ana_selparam_t *param, void *scanner)
919 GMX_ASSERT(param->val.type == NO_VALUE,
920 "Boolean parser called for non-boolean parameter");
921 if (values.size() > 1 || (!values.empty() && values.front().type != INT_VALUE))
923 _gmx_selparser_error(scanner, "parameter takes only a yes/no/on/off/0/1 value");
928 /* Check if the parameter name is given with a 'no' prefix */
929 if (name.length() > 2 && name[0] == 'n' && name[1] == 'o'
930 && name.compare(2, name.length() - 2, param->name) == 0)
934 if (bSetNo && !values.empty())
936 _gmx_selparser_error(scanner, "parameter 'no%s' should not have a value",
940 if (!values.empty() && values.front().u.i.i1 == 0)
945 *param->val.u.b = bSetNo ? false : true;
950 * Parses the values for an enumeration parameter.
952 * \param[in] values List of values.
953 * \param param Parameter to parse.
954 * \param[in] scanner Scanner data structure.
955 * \returns true if the values were parsed successfully, false otherwise.
958 parse_values_enum(const SelectionParserValueList &values,
959 gmx_ana_selparam_t *param,
962 GMX_ASSERT(param->val.type == STR_VALUE,
963 "Enum parser called for non-string parameter");
964 if (values.size() != 1)
966 _gmx_selparser_error(scanner, "a single value is required");
969 const SelectionParserValue &value = values.front();
970 GMX_RELEASE_ASSERT(value.type == param->val.type,
971 "Invalid value type (should have been caught earlier)");
972 if (value.hasExpressionValue())
974 _gmx_selparser_error(scanner, "expression value for enumerated parameter not supported");
978 const std::string &svalue = value.stringValue();
981 while (param->val.u.s[i] != NULL)
983 if (gmx::startsWith(param->val.u.s[i], svalue))
985 /* Check if there is a duplicate match */
988 _gmx_selparser_error(scanner, "ambiguous value");
997 _gmx_selparser_error(scanner, "invalid value");
1000 param->val.u.s[0] = param->val.u.s[match];
1005 * Replaces constant expressions with their values.
1007 * \param[in,out] values First element in the value list to process.
1010 convert_const_values(SelectionParserValueList *values)
1012 SelectionParserValueList::iterator value;
1013 for (value = values->begin(); value != values->end(); ++value)
1015 if (value->hasExpressionValue() && value->expr->v.type != GROUP_VALUE &&
1016 value->expr->type == SEL_CONST)
1018 SelectionTreeElementPointer expr = value->expr;
1019 switch (expr->v.type)
1022 *value = SelectionParserValue::createInteger(expr->v.u.i[0]);
1025 *value = SelectionParserValue::createReal(expr->v.u.r[0]);
1028 *value = SelectionParserValue::createString(expr->v.u.s[0]);
1031 *value = SelectionParserValue::createPosition(expr->v.u.p->x[0]);
1034 GMX_THROW(gmx::InternalError(
1035 "Unsupported constant expression value type"));
1042 * \param pparams List of parameters from the selection parser.
1043 * \param[in] nparam Number of parameters in \p params.
1044 * \param params Array of parameters to parse.
1045 * \param root Selection element to which child expressions are added.
1046 * \param[in] scanner Scanner data structure.
1047 * \returns true if the parameters were parsed successfully, false otherwise.
1049 * Initializes the \p params array based on the parameters in \p pparams.
1050 * See the documentation of \c gmx_ana_selparam_t for different options
1051 * available for parsing.
1053 * The list \p pparams and any associated values are freed after the parameters
1054 * have been processed, no matter is there was an error or not.
1057 _gmx_sel_parse_params(const gmx::SelectionParserParameterList &pparams,
1058 int nparam, gmx_ana_selparam_t *params,
1059 const gmx::SelectionTreeElementPointer &root,
1062 gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
1063 gmx_ana_selparam_t *oparam;
1067 /* Check that the value pointers of SPAR_VARNUM parameters are NULL and
1068 * that they are not NULL for other parameters */
1070 for (i = 0; i < nparam; ++i)
1072 std::string contextStr = gmx::formatString("In parameter '%s'", params[i].name);
1073 gmx::MessageStringContext context(errors, contextStr);
1074 if (params[i].val.type != POS_VALUE && (params[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
1076 if (params[i].val.u.ptr != NULL)
1078 _gmx_selparser_error(scanner, "value pointer is not NULL "
1079 "although it should be for SPAR_VARNUM "
1080 "and SPAR_ATOMVAL parameters");
1082 if ((params[i].flags & SPAR_VARNUM)
1083 && (params[i].flags & SPAR_DYNAMIC) && !params[i].nvalptr)
1085 _gmx_selparser_error(scanner, "nvalptr is NULL but both "
1086 "SPAR_VARNUM and SPAR_DYNAMIC are specified");
1092 if (params[i].val.u.ptr == NULL)
1094 _gmx_selparser_error(scanner, "value pointer is NULL");
1103 /* Parse the parameters */
1105 SelectionParserParameterList::const_iterator pparam;
1106 for (pparam = pparams.begin(); pparam != pparams.end(); ++pparam)
1108 std::string contextStr;
1109 /* Find the parameter and make some checks */
1110 if (!pparam->name().empty())
1112 contextStr = gmx::formatString("In parameter '%s'", pparam->name().c_str());
1114 oparam = gmx_ana_selparam_find(pparam->name().c_str(), nparam, params);
1118 contextStr = gmx::formatString("In value %d", i + 1);
1119 oparam = ¶ms[i];
1120 if (oparam->name != NULL)
1123 _gmx_selparser_error(scanner, "too many NULL parameters provided");
1131 _gmx_selparser_error(scanner, "all NULL parameters should appear in the beginning of the list");
1135 gmx::MessageStringContext context(errors, contextStr);
1138 _gmx_selparser_error(scanner, "unknown parameter skipped");
1142 if (oparam->val.type != NO_VALUE && pparam->values().empty())
1144 _gmx_selparser_error(scanner, "no value provided");
1148 if (oparam->flags & SPAR_SET)
1150 _gmx_selparser_error(scanner, "parameter set multiple times, extra values skipped");
1154 oparam->flags |= SPAR_SET;
1155 /* Process the values for the parameter */
1156 convert_const_values(pparam->values_.get());
1157 if (convert_values(pparam->values_.get(), oparam->val.type, scanner) != 0)
1159 _gmx_selparser_error(scanner, "invalid value");
1163 if (oparam->val.type == NO_VALUE)
1165 rc = parse_values_bool(pparam->name(), pparam->values(), oparam, scanner);
1167 else if (oparam->flags & SPAR_RANGES)
1169 rc = parse_values_range(pparam->values(), oparam, scanner);
1171 else if (oparam->flags & SPAR_VARNUM)
1173 if (pparam->values().size() == 1
1174 && pparam->values().front().hasExpressionValue())
1176 rc = parse_values_varnum_expr(pparam->values(), oparam, root, scanner);
1180 rc = parse_values_varnum(pparam->values(), oparam, root, scanner);
1183 else if (oparam->flags & SPAR_ENUMVAL)
1185 rc = parse_values_enum(pparam->values(), oparam, scanner);
1189 rc = parse_values_std(pparam->values(), oparam, root, scanner);
1196 /* Check that all required parameters are present */
1197 for (i = 0; i < nparam; ++i)
1199 if (!(params[i].flags & SPAR_OPTIONAL) && !(params[i].flags & SPAR_SET))
1201 _gmx_selparser_error(scanner, "required parameter '%s' not specified", params[i].name);