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
45 #include "gromacs/math/vec.h"
46 #include "gromacs/selection/position.h"
47 #include "gromacs/selection/selmethod.h"
48 #include "gromacs/selection/selparam.h"
49 #include "gromacs/utility/exceptions.h"
50 #include "gromacs/utility/gmxassert.h"
51 #include "gromacs/utility/messagestringcollector.h"
52 #include "gromacs/utility/smalloc.h"
53 #include "gromacs/utility/stringutil.h"
55 #include "parsetree.h"
60 using gmx::SelectionParserValue;
61 using gmx::SelectionParserValueList;
62 using gmx::SelectionParserParameter;
63 using gmx::SelectionParserParameterList;
64 using gmx::SelectionTreeElement;
65 using gmx::SelectionTreeElementPointer;
68 * \param[in] name Name of the parameter to search.
69 * \param[in] nparam Number of parameters in the \p param array.
70 * \param[in] param Parameter array to search.
71 * \returns Pointer to the parameter in the \p param
72 * or NULL if no parameter with name \p name was found.
74 * The comparison is case-sensitive.
77 gmx_ana_selparam_find(const char *name, int nparam, gmx_ana_selparam_t *param)
85 /* Find the first non-null parameter */
87 while (i < nparam && param[i].name == NULL)
91 /* Process the special case of a NULL parameter */
94 return (i == 0) ? NULL : ¶m[i-1];
96 for (; i < nparam; ++i)
98 if (!strcmp(param[i].name, name))
102 /* Check for 'no' prefix on boolean parameters */
103 if (param[i].val.type == NO_VALUE
104 && strlen(name) > 2 && name[0] == 'n' && name[1] == 'o'
105 && !strcmp(param[i].name, name+2))
114 * Does a type conversion on a SelectionParserValue.
116 * \param[in,out] value Value to convert.
117 * \param[in] type Type to convert to.
118 * \param[in] scanner Scanner data structure.
119 * \returns 0 on success, a non-zero value on error.
122 convert_value(SelectionParserValue *value, e_selvalue_t type, void *scanner)
124 if (value->type == type || type == NO_VALUE)
128 if (value->hasExpressionValue())
130 /* Conversion from atom selection to position using default
131 * reference positions. */
132 if (value->type == GROUP_VALUE && type == POS_VALUE)
134 SelectionTreeElementPointer expr =
135 _gmx_sel_init_position(value->expr, NULL, scanner);
136 // FIXME: Use exceptions
141 *value = SelectionParserValue::createExpr(expr);
148 /* Integers to floating point are easy */
149 if (value->type == INT_VALUE && type == REAL_VALUE)
151 *value = SelectionParserValue::createRealRange(value->u.i.i1,
155 /* Reals that are integer-valued can also be converted */
156 if (value->type == REAL_VALUE && type == INT_VALUE)
158 int i1 = static_cast<int>(value->u.r.r1);
159 int i2 = static_cast<int>(value->u.r.r2);
160 if (gmx_within_tol(value->u.r.r1, i1, GMX_REAL_EPS)
161 && gmx_within_tol(value->u.r.r2, i2, GMX_REAL_EPS))
163 *value = SelectionParserValue::createIntegerRange(i1, i2);
172 * Does a type conversion on a list of values.
174 * \param[in,out] values Values to convert.
175 * \param[in] type Type to convert to.
176 * \param[in] scanner Scanner data structure.
177 * \returns 0 on success, a non-zero value on error.
180 convert_values(SelectionParserValueList *values, e_selvalue_t type, void *scanner)
183 SelectionParserValueList::iterator value;
184 for (value = values->begin(); value != values->end(); ++value)
186 int rc1 = convert_value(&*value, type, scanner);
187 if (rc1 != 0 && rc == 0)
192 /* FIXME: More informative error messages */
197 * Adds a child element for a parameter, keeping the parameter order.
199 * \param[in,out] root Root element to which the child is added.
200 * \param[in] child Child to add.
201 * \param[in] param Parameter for which this child is a value.
203 * Puts \p child in the child list of \p root such that the list remains
204 * in the same order as the corresponding parameters.
207 place_child(const SelectionTreeElementPointer &root,
208 const SelectionTreeElementPointer &child,
209 gmx_ana_selparam_t *param)
211 gmx_ana_selparam_t *ps;
214 ps = root->u.expr.method->param;
216 /* Put the child element in the correct place */
217 if (!root->child || n < root->child->u.param - ps)
219 child->next = root->child;
224 SelectionTreeElementPointer prev = root->child;
225 while (prev->next && prev->next->u.param - ps >= n)
229 child->next = prev->next;
235 * Comparison function for sorting integer ranges.
237 * \param[in] a Pointer to the first range.
238 * \param[in] b Pointer to the second range.
239 * \returns -1, 0, or 1 depending on the relative order of \p a and \p b.
241 * The ranges are primarily sorted based on their starting point, and
242 * secondarily based on length (longer ranges come first).
245 cmp_int_range(const void *a, const void *b)
247 if (((int *)a)[0] < ((int *)b)[0])
251 if (((int *)a)[0] > ((int *)b)[0])
255 if (((int *)a)[1] > ((int *)b)[1])
263 * Comparison function for sorting real ranges.
265 * \param[in] a Pointer to the first range.
266 * \param[in] b Pointer to the second range.
267 * \returns -1, 0, or 1 depending on the relative order of \p a and \p b.
269 * The ranges are primarily sorted based on their starting point, and
270 * secondarily based on length (longer ranges come first).
273 cmp_real_range(const void *a, const void *b)
275 if (((real *)a)[0] < ((real *)b)[0])
279 if (((real *)a)[0] > ((real *)b)[0])
283 if (((real *)a)[1] > ((real *)b)[1])
291 * Parses the values for a parameter that takes integer or real ranges.
293 * \param[in] values List of values.
294 * \param param Parameter to parse.
295 * \param[in] scanner Scanner data structure.
296 * \returns true if the values were parsed successfully, false otherwise.
299 parse_values_range(const SelectionParserValueList &values,
300 gmx_ana_selparam_t *param, void *scanner)
306 param->flags &= ~SPAR_DYNAMIC;
307 GMX_RELEASE_ASSERT(param->val.type == INT_VALUE || param->val.type == REAL_VALUE,
308 "Invalid range parameter type");
311 if (param->val.type == INT_VALUE)
313 snew(idata, values.size()*2);
317 snew(rdata, values.size()*2);
320 SelectionParserValueList::const_iterator value;
321 for (value = values.begin(); value != values.end(); ++value)
323 if (value->hasExpressionValue())
325 _gmx_selparser_error(scanner, "expressions not supported within range parameters");
328 GMX_RELEASE_ASSERT(value->type == param->val.type,
329 "Invalid range value type (should have been caught earlier)");
330 if (param->val.type == INT_VALUE)
332 int i1 = std::min(value->u.i.i1, value->u.i.i2);
333 int i2 = std::max(value->u.i.i1, value->u.i.i2);
334 /* Check if the new range overlaps or extends the previous one */
335 if (i > 0 && i1 <= idata[i-1]+1 && i2 >= idata[i-2]-1)
337 idata[i-2] = std::min(idata[i-2], i1);
338 idata[i-1] = std::max(idata[i-1], i2);
348 real r1 = std::min(value->u.r.r1, value->u.r.r2);
349 real r2 = std::max(value->u.r.r1, value->u.r.r2);
350 /* Check if the new range overlaps or extends the previous one */
351 if (i > 0 && r1 <= rdata[i-1] && r2 >= rdata[i-2])
353 rdata[i-2] = std::min(rdata[i-2], r1);
354 rdata[i-1] = std::max(rdata[i-1], r2);
364 /* Sort the ranges and merge consequent ones */
365 if (param->val.type == INT_VALUE)
367 qsort(idata, n, 2*sizeof(int), &cmp_int_range);
368 for (i = j = 2; i < 2*n; i += 2)
370 if (idata[j-1]+1 >= idata[i])
372 if (idata[i+1] > idata[j-1])
374 idata[j-1] = idata[i+1];
380 idata[j+1] = idata[i+1];
387 qsort(rdata, n, 2*sizeof(real), &cmp_real_range);
388 for (i = j = 2; i < 2*n; i += 2)
390 if (rdata[j-1]+1 >= rdata[i])
392 if (rdata[i+1] > rdata[j-1])
394 rdata[j-1] = rdata[i+1];
400 rdata[j+1] = rdata[i+1];
406 /* Store the values */
407 if (param->flags & SPAR_VARNUM)
410 if (param->val.type == INT_VALUE)
413 _gmx_selvalue_setstore_alloc(¶m->val, idata, j);
418 _gmx_selvalue_setstore_alloc(¶m->val, rdata, j);
423 if (n != param->val.nr)
425 _gmx_selparser_error(scanner, "the value should consist of exactly one range");
430 if (param->val.type == INT_VALUE)
432 memcpy(param->val.u.i, idata, 2*n*sizeof(int));
437 memcpy(param->val.u.r, rdata, 2*n*sizeof(real));
443 *param->nvalptr = param->val.nr;
445 param->nvalptr = NULL;
451 * Parses the values for a parameter that takes a variable number of values.
453 * \param[in] values List of values.
454 * \param param Parameter to parse.
455 * \param root Selection element to which child expressions are added.
456 * \param[in] scanner Scanner data structure.
457 * \returns true if the values were parsed successfully, false otherwise.
459 * For integer ranges, the sequence of numbers from the first to second value
460 * is stored, each as a separate value.
463 parse_values_varnum(const SelectionParserValueList &values,
464 gmx_ana_selparam_t *param,
465 const SelectionTreeElementPointer &root,
470 param->flags &= ~SPAR_DYNAMIC;
471 /* Compute number of values, considering also integer ranges. */
472 size_t valueCount = values.size();
473 if (param->val.type == INT_VALUE)
475 SelectionParserValueList::const_iterator value;
476 for (value = values.begin(); value != values.end(); ++value)
478 if (value->type == INT_VALUE && !value->hasExpressionValue())
480 valueCount += abs(value->u.i.i2 - value->u.i.i1);
485 /* Check that the value type is actually implemented */
486 if (param->val.type != INT_VALUE && param->val.type != REAL_VALUE
487 && param->val.type != STR_VALUE && param->val.type != POS_VALUE)
489 GMX_THROW(gmx::InternalError("Variable-count value type not implemented"));
492 /* Reserve appropriate amount of memory */
493 if (param->val.type == POS_VALUE)
495 gmx_ana_pos_reserve(param->val.u.p, valueCount, 0);
496 gmx_ana_indexmap_init(¶m->val.u.p->m, NULL, NULL, INDEX_UNKNOWN);
497 gmx_ana_pos_set_nr(param->val.u.p, valueCount);
501 _gmx_selvalue_reserve(¶m->val, valueCount);
505 SelectionParserValueList::const_iterator value;
506 for (value = values.begin(); value != values.end(); ++value)
508 if (value->hasExpressionValue())
510 _gmx_selparser_error(scanner, "expressions not supported within value lists");
513 GMX_RELEASE_ASSERT(value->type == param->val.type,
514 "Invalid value type (should have been caught earlier)");
515 switch (param->val.type)
518 if (value->u.i.i1 <= value->u.i.i2)
520 for (j = value->u.i.i1; j <= value->u.i.i2; ++j)
522 param->val.u.i[i++] = j;
527 for (j = value->u.i.i1; j >= value->u.i.i2; --j)
529 param->val.u.i[i++] = j;
534 if (value->u.r.r1 != value->u.r.r2)
536 _gmx_selparser_error(scanner, "real ranges not supported");
539 param->val.u.r[i++] = value->u.r.r1;
542 param->val.u.s[i++] = strdup(value->stringValue().c_str());
544 case POS_VALUE: copy_rvec(value->u.x, param->val.u.p->x[i++]); break;
545 default: /* Should not be reached */
546 GMX_THROW(gmx::InternalError("Variable-count value type not implemented"));
552 *param->nvalptr = param->val.nr;
554 param->nvalptr = NULL;
555 /* Create a dummy child element to store the string values.
556 * This element is responsible for freeing the values, but carries no
558 if (param->val.type == STR_VALUE)
560 SelectionTreeElementPointer child(new SelectionTreeElement(SEL_CONST));
561 _gmx_selelem_set_vtype(child, STR_VALUE);
562 child->setName(param->name);
563 child->flags &= ~SEL_ALLOCVAL;
564 child->flags |= SEL_FLAGSSET | SEL_VARNUMVAL | SEL_ALLOCDATA;
565 child->v.nr = param->val.nr;
566 _gmx_selvalue_setstore(&child->v, param->val.u.s);
567 /* Because the child is not group-valued, the u union is not used
568 * for anything, so we can abuse it by storing the parameter value
569 * as place_child() expects, but this is really ugly... */
570 child->u.param = param;
571 place_child(root, child, param);
578 * Adds a new subexpression reference to a selection element.
580 * \param[in,out] root Root element to which the subexpression is added.
581 * \param[in] param Parameter for which this expression is a value.
582 * \param[in] expr Expression to add.
583 * \param[in] scanner Scanner data structure.
584 * \returns The created child element.
586 * Creates a new \ref SEL_SUBEXPRREF element and adds it into the child
588 * If \p expr is already a \ref SEL_SUBEXPRREF, it is used as it is.
589 * \ref SEL_ALLOCVAL is cleared for the returned element.
591 static SelectionTreeElementPointer
592 add_child(const SelectionTreeElementPointer &root, gmx_ana_selparam_t *param,
593 const SelectionTreeElementPointer &expr, void *scanner)
595 GMX_RELEASE_ASSERT(root->type == SEL_EXPRESSION || root->type == SEL_MODIFIER,
596 "Unsupported root element for selection parameter parser");
597 SelectionTreeElementPointer child;
598 /* Create a subexpression reference element if necessary */
599 if (expr->type == SEL_SUBEXPRREF)
605 child.reset(new SelectionTreeElement(SEL_SUBEXPRREF));
606 _gmx_selelem_set_vtype(child, expr->v.type);
609 /* Setup the child element */
610 child->flags &= ~SEL_ALLOCVAL;
611 child->u.param = param;
612 if (child->v.type != param->val.type)
614 _gmx_selparser_error(scanner, "invalid expression value");
615 // FIXME: Use exceptions.
616 return SelectionTreeElementPointer();
618 _gmx_selelem_update_flags(child);
619 if ((child->flags & SEL_DYNAMIC) && !(param->flags & SPAR_DYNAMIC))
621 _gmx_selparser_error(scanner, "dynamic values not supported");
622 // FIXME: Use exceptions.
623 return SelectionTreeElementPointer();
625 if (!(child->flags & SEL_DYNAMIC))
627 param->flags &= ~SPAR_DYNAMIC;
629 /* Put the child element in the correct place */
630 place_child(root, child, param);
635 * Parses an expression value for a parameter that takes a variable number of values.
637 * \param[in] values List of values.
638 * \param param Parameter to parse.
639 * \param root Selection element to which child expressions are added.
640 * \param[in] scanner Scanner data structure.
641 * \returns true if the values were parsed successfully, false otherwise.
644 parse_values_varnum_expr(const SelectionParserValueList &values,
645 gmx_ana_selparam_t *param,
646 const SelectionTreeElementPointer &root,
649 GMX_RELEASE_ASSERT(values.size() == 1 && values.front().hasExpressionValue(),
650 "Called with an invalid type of value");
652 SelectionTreeElementPointer child
653 = add_child(root, param, values.front().expr, scanner);
659 /* Process single-valued expressions */
660 /* TODO: We should also handle SEL_SINGLEVAL expressions here */
661 if (child->v.type == POS_VALUE || child->v.type == GROUP_VALUE)
663 /* Set the value storage */
664 _gmx_selvalue_setstore(&child->v, param->val.u.ptr);
668 *param->nvalptr = param->val.nr;
670 param->nvalptr = NULL;
674 if (!(child->flags & SEL_VARNUMVAL))
676 _gmx_selparser_error(scanner, "invalid expression value");
680 child->flags |= SEL_ALLOCVAL;
682 *param->nvalptr = param->val.nr;
683 /* Rest of the initialization is done during compilation in
690 * Initializes the storage of an expression value.
692 * \param[in,out] sel Selection element that evaluates the value.
693 * \param[in] param Parameter to receive the value.
694 * \param[in] i The value of \p sel evaluates the value \p i for
696 * \param[in] scanner Scanner data structure.
698 * Initializes the data pointer of \p sel such that the result is stored
699 * as the value \p i of \p param.
700 * This function is used internally by parse_values_std().
703 set_expr_value_store(const SelectionTreeElementPointer &sel,
704 gmx_ana_selparam_t *param, int i, void *scanner)
706 if (sel->v.type != GROUP_VALUE && !(sel->flags & SEL_SINGLEVAL))
708 _gmx_selparser_error(scanner, "invalid expression value");
713 case INT_VALUE: sel->v.u.i = ¶m->val.u.i[i]; break;
714 case REAL_VALUE: sel->v.u.r = ¶m->val.u.r[i]; break;
715 case STR_VALUE: sel->v.u.s = ¶m->val.u.s[i]; break;
716 case POS_VALUE: sel->v.u.p = ¶m->val.u.p[i]; break;
717 case GROUP_VALUE: sel->v.u.g = ¶m->val.u.g[i]; break;
719 GMX_THROW(gmx::InternalError("Invalid value type"));
727 * Parses the values for a parameter that takes a constant number of values.
729 * \param[in] values List of values.
730 * \param param Parameter to parse.
731 * \param root Selection element to which child expressions are added.
732 * \param[in] scanner Scanner data structure.
733 * \returns true if the values were parsed successfully, false otherwise.
735 * For integer ranges, the sequence of numbers from the first to second value
736 * is stored, each as a separate value.
739 parse_values_std(const SelectionParserValueList &values,
740 gmx_ana_selparam_t *param,
741 const SelectionTreeElementPointer &root, void *scanner)
746 /* Handle atom-valued parameters */
747 if (param->flags & SPAR_ATOMVAL)
749 if (values.size() > 1)
751 _gmx_selparser_error(scanner, "more than one value not supported");
754 if (values.front().hasExpressionValue())
756 SelectionTreeElementPointer child
757 = add_child(root, param, values.front().expr, scanner);
762 child->flags |= SEL_ALLOCVAL;
763 if (child->v.type != GROUP_VALUE && (child->flags & SEL_ATOMVAL))
765 /* Rest of the initialization is done during compilation in
767 /* TODO: Positions are not correctly handled */
771 *param->nvalptr = -1;
775 param->flags &= ~SPAR_ATOMVAL;
781 param->nvalptr = NULL;
782 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE
783 || param->val.type == STR_VALUE)
785 _gmx_selvalue_reserve(¶m->val, 1);
787 return set_expr_value_store(child, param, 0, scanner);
789 /* If we reach here, proceed with normal parameter handling */
791 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE
792 || param->val.type == STR_VALUE)
794 _gmx_selvalue_reserve(¶m->val, 1);
796 param->flags &= ~SPAR_ATOMVAL;
797 param->flags &= ~SPAR_DYNAMIC;
802 SelectionParserValueList::const_iterator value;
803 for (value = values.begin(); value != values.end() && i < param->val.nr; ++value)
805 if (value->type != param->val.type)
807 _gmx_selparser_error(scanner, "incorrect value skipped");
810 if (value->hasExpressionValue())
812 SelectionTreeElementPointer child
813 = add_child(root, param, value->expr, scanner);
814 /* Check that the expression is valid */
819 if (!set_expr_value_store(child, param, i, scanner))
823 if (child->flags & SEL_DYNAMIC)
830 /* Value is not an expression */
834 if (value->u.i.i1 <= value->u.i.i2)
836 for (j = value->u.i.i1; j <= value->u.i.i2 && i < param->val.nr; ++j)
838 param->val.u.i[i++] = j;
840 if (j != value->u.i.i2 + 1)
842 _gmx_selparser_error(scanner, "extra values skipped");
847 for (j = value->u.i.i1; j >= value->u.i.i2 && i < param->val.nr; --j)
849 param->val.u.i[i++] = j;
851 if (j != value->u.i.i2 - 1)
853 _gmx_selparser_error(scanner, "extra values skipped");
859 if (value->u.r.r1 != value->u.r.r2)
861 _gmx_selparser_error(scanner, "real ranges not supported");
864 param->val.u.r[i] = value->u.r.r1;
867 param->val.u.s[i] = strdup(value->stringValue().c_str());
870 gmx_ana_pos_init_const(¶m->val.u.p[i], value->u.x);
874 GMX_THROW(gmx::InternalError("Invalid non-expression value type"));
879 if (value != values.end())
881 _gmx_selparser_error(scanner, "extra values");
884 if (i < param->val.nr)
886 _gmx_selparser_error(scanner, "not enough values");
891 param->flags &= ~SPAR_DYNAMIC;
895 *param->nvalptr = param->val.nr;
897 param->nvalptr = NULL;
903 * Parses the values for a boolean parameter.
905 * \param[in] name Name by which the parameter was given.
906 * \param[in] values List of values.
907 * \param param Parameter to parse.
908 * \param[in] scanner Scanner data structure.
909 * \returns true if the values were parsed successfully, false otherwise.
912 parse_values_bool(const std::string &name,
913 const SelectionParserValueList &values,
914 gmx_ana_selparam_t *param, void *scanner)
916 GMX_ASSERT(param->val.type == NO_VALUE,
917 "Boolean parser called for non-boolean parameter");
918 if (values.size() > 1 || (!values.empty() && values.front().type != INT_VALUE))
920 _gmx_selparser_error(scanner, "parameter takes only a yes/no/on/off/0/1 value");
925 /* Check if the parameter name is given with a 'no' prefix */
926 if (name.length() > 2 && name[0] == 'n' && name[1] == 'o'
927 && name.compare(2, name.length() - 2, param->name) == 0)
931 if (bSetNo && !values.empty())
933 _gmx_selparser_error(scanner, "parameter 'no%s' should not have a value",
937 if (!values.empty() && values.front().u.i.i1 == 0)
942 *param->val.u.b = bSetNo ? false : true;
947 * Parses the values for an enumeration parameter.
949 * \param[in] values List of values.
950 * \param param Parameter to parse.
951 * \param[in] scanner Scanner data structure.
952 * \returns true if the values were parsed successfully, false otherwise.
955 parse_values_enum(const SelectionParserValueList &values,
956 gmx_ana_selparam_t *param,
959 GMX_ASSERT(param->val.type == STR_VALUE,
960 "Enum parser called for non-string parameter");
961 if (values.size() != 1)
963 _gmx_selparser_error(scanner, "a single value is required");
966 const SelectionParserValue &value = values.front();
967 GMX_RELEASE_ASSERT(value.type == param->val.type,
968 "Invalid value type (should have been caught earlier)");
969 if (value.hasExpressionValue())
971 _gmx_selparser_error(scanner, "expression value for enumerated parameter not supported");
975 const std::string &svalue = value.stringValue();
978 while (param->val.u.s[i] != NULL)
980 if (gmx::startsWith(param->val.u.s[i], svalue))
982 /* Check if there is a duplicate match */
985 _gmx_selparser_error(scanner, "ambiguous value");
994 _gmx_selparser_error(scanner, "invalid value");
997 param->val.u.s[0] = param->val.u.s[match];
1002 * Replaces constant expressions with their values.
1004 * \param[in,out] values First element in the value list to process.
1007 convert_const_values(SelectionParserValueList *values)
1009 SelectionParserValueList::iterator value;
1010 for (value = values->begin(); value != values->end(); ++value)
1012 if (value->hasExpressionValue() && value->expr->v.type != GROUP_VALUE &&
1013 value->expr->type == SEL_CONST)
1015 SelectionTreeElementPointer expr = value->expr;
1016 switch (expr->v.type)
1019 *value = SelectionParserValue::createInteger(expr->v.u.i[0]);
1022 *value = SelectionParserValue::createReal(expr->v.u.r[0]);
1025 *value = SelectionParserValue::createString(expr->v.u.s[0]);
1028 *value = SelectionParserValue::createPosition(expr->v.u.p->x[0]);
1031 GMX_THROW(gmx::InternalError(
1032 "Unsupported constant expression value type"));
1039 * \param pparams List of parameters from the selection parser.
1040 * \param[in] nparam Number of parameters in \p params.
1041 * \param params Array of parameters to parse.
1042 * \param root Selection element to which child expressions are added.
1043 * \param[in] scanner Scanner data structure.
1044 * \returns true if the parameters were parsed successfully, false otherwise.
1046 * Initializes the \p params array based on the parameters in \p pparams.
1047 * See the documentation of \c gmx_ana_selparam_t for different options
1048 * available for parsing.
1050 * The list \p pparams and any associated values are freed after the parameters
1051 * have been processed, no matter is there was an error or not.
1054 _gmx_sel_parse_params(const gmx::SelectionParserParameterList &pparams,
1055 int nparam, gmx_ana_selparam_t *params,
1056 const gmx::SelectionTreeElementPointer &root,
1059 gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
1060 gmx_ana_selparam_t *oparam;
1064 /* Check that the value pointers of SPAR_VARNUM parameters are NULL and
1065 * that they are not NULL for other parameters */
1067 for (i = 0; i < nparam; ++i)
1069 std::string contextStr = gmx::formatString("In parameter '%s'", params[i].name);
1070 gmx::MessageStringContext context(errors, contextStr);
1071 if (params[i].val.type != POS_VALUE && (params[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
1073 if (params[i].val.u.ptr != NULL)
1075 _gmx_selparser_error(scanner, "value pointer is not NULL "
1076 "although it should be for SPAR_VARNUM "
1077 "and SPAR_ATOMVAL parameters");
1079 if ((params[i].flags & SPAR_VARNUM)
1080 && (params[i].flags & SPAR_DYNAMIC) && !params[i].nvalptr)
1082 _gmx_selparser_error(scanner, "nvalptr is NULL but both "
1083 "SPAR_VARNUM and SPAR_DYNAMIC are specified");
1089 if (params[i].val.u.ptr == NULL)
1091 _gmx_selparser_error(scanner, "value pointer is NULL");
1100 /* Parse the parameters */
1102 SelectionParserParameterList::const_iterator pparam;
1103 for (pparam = pparams.begin(); pparam != pparams.end(); ++pparam)
1105 std::string contextStr;
1106 /* Find the parameter and make some checks */
1107 if (!pparam->name().empty())
1109 contextStr = gmx::formatString("In parameter '%s'", pparam->name().c_str());
1111 oparam = gmx_ana_selparam_find(pparam->name().c_str(), nparam, params);
1115 contextStr = gmx::formatString("In value %d", i + 1);
1116 oparam = ¶ms[i];
1117 if (oparam->name != NULL)
1120 _gmx_selparser_error(scanner, "too many NULL parameters provided");
1128 _gmx_selparser_error(scanner, "all NULL parameters should appear in the beginning of the list");
1132 gmx::MessageStringContext context(errors, contextStr);
1135 _gmx_selparser_error(scanner, "unknown parameter skipped");
1139 if (oparam->val.type != NO_VALUE && pparam->values().empty())
1141 _gmx_selparser_error(scanner, "no value provided");
1145 if (oparam->flags & SPAR_SET)
1147 _gmx_selparser_error(scanner, "parameter set multiple times, extra values skipped");
1151 oparam->flags |= SPAR_SET;
1152 /* Process the values for the parameter */
1153 convert_const_values(pparam->values_.get());
1154 if (convert_values(pparam->values_.get(), oparam->val.type, scanner) != 0)
1156 _gmx_selparser_error(scanner, "invalid value");
1160 if (oparam->val.type == NO_VALUE)
1162 rc = parse_values_bool(pparam->name(), pparam->values(), oparam, scanner);
1164 else if (oparam->flags & SPAR_RANGES)
1166 rc = parse_values_range(pparam->values(), oparam, scanner);
1168 else if (oparam->flags & SPAR_VARNUM)
1170 if (pparam->values().size() == 1
1171 && pparam->values().front().hasExpressionValue())
1173 rc = parse_values_varnum_expr(pparam->values(), oparam, root, scanner);
1177 rc = parse_values_varnum(pparam->values(), oparam, root, scanner);
1180 else if (oparam->flags & SPAR_ENUMVAL)
1182 rc = parse_values_enum(pparam->values(), oparam, scanner);
1186 rc = parse_values_std(pparam->values(), oparam, root, scanner);
1193 /* Check that all required parameters are present */
1194 for (i = 0; i < nparam; ++i)
1196 if (!(params[i].flags & SPAR_OPTIONAL) && !(params[i].flags & SPAR_SET))
1198 _gmx_selparser_error(scanner, "required parameter '%s' not specified", params[i].name);