3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
33 * Implements functions in selparam.h.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_selection
41 #include "gromacs/legacyheaders/smalloc.h"
42 #include "gromacs/legacyheaders/string2.h"
43 #include "gromacs/legacyheaders/vec.h"
45 #include "gromacs/selection/position.h"
46 #include "gromacs/selection/selmethod.h"
47 #include "gromacs/selection/selparam.h"
48 #include "gromacs/utility/errorcodes.h"
49 #include "gromacs/utility/exceptions.h"
50 #include "gromacs/utility/gmxassert.h"
51 #include "gromacs/utility/messagestringcollector.h"
52 #include "gromacs/utility/stringutil.h"
54 #include "parsetree.h"
62 using gmx::SelectionParserParameterPointer;
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 \c t_selexpr_value.
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(t_selexpr_value *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)
135 _gmx_sel_init_position(value->expr, NULL, scanner);
136 // FIXME: Use exceptions
148 /* Integers to floating point are easy */
149 if (value->type == INT_VALUE && type == REAL_VALUE)
151 real r1 = (real)value->u.i.i1;
152 real r2 = (real)value->u.i.i2;
158 /* Reals that are integer-valued can also be converted */
159 if (value->type == REAL_VALUE && type == INT_VALUE
160 && gmx_within_tol(value->u.r.r1, (int)value->u.r.r1, GMX_REAL_EPS)
161 && gmx_within_tol(value->u.r.r2, (int)value->u.r.r2, GMX_REAL_EPS))
163 int i1 = (int)value->u.r.r1;
164 int i2 = (int)value->u.r.r2;
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(t_selexpr_value *values, e_selvalue_t type, void *scanner)
185 t_selexpr_value *value;
192 rc1 = convert_value(value, type, scanner);
193 if (rc1 != 0 && rc == 0)
199 /* FIXME: More informative error messages */
204 * Adds a child element for a parameter, keeping the parameter order.
206 * \param[in,out] root Root element to which the child is added.
207 * \param[in] child Child to add.
208 * \param[in] param Parameter for which this child is a value.
210 * Puts \p child in the child list of \p root such that the list remains
211 * in the same order as the corresponding parameters.
214 place_child(const SelectionTreeElementPointer &root,
215 const SelectionTreeElementPointer &child,
216 gmx_ana_selparam_t *param)
218 gmx_ana_selparam_t *ps;
221 ps = root->u.expr.method->param;
223 /* Put the child element in the correct place */
224 if (!root->child || n < root->child->u.param - ps)
226 child->next = root->child;
231 SelectionTreeElementPointer prev = root->child;
232 while (prev->next && prev->next->u.param - ps >= n)
236 child->next = prev->next;
242 * Comparison function for sorting integer ranges.
244 * \param[in] a Pointer to the first range.
245 * \param[in] b Pointer to the second range.
246 * \returns -1, 0, or 1 depending on the relative order of \p a and \p b.
248 * The ranges are primarily sorted based on their starting point, and
249 * secondarily based on length (longer ranges come first).
252 cmp_int_range(const void *a, const void *b)
254 if (((int *)a)[0] < ((int *)b)[0])
258 if (((int *)a)[0] > ((int *)b)[0])
262 if (((int *)a)[1] > ((int *)b)[1])
270 * Comparison function for sorting real ranges.
272 * \param[in] a Pointer to the first range.
273 * \param[in] b Pointer to the second range.
274 * \returns -1, 0, or 1 depending on the relative order of \p a and \p b.
276 * The ranges are primarily sorted based on their starting point, and
277 * secondarily based on length (longer ranges come first).
280 cmp_real_range(const void *a, const void *b)
282 if (((real *)a)[0] < ((real *)b)[0])
286 if (((real *)a)[0] > ((real *)b)[0])
290 if (((real *)a)[1] > ((real *)b)[1])
298 * Parses the values for a parameter that takes integer or real ranges.
300 * \param[in] nval Number of values in \p values.
301 * \param[in] values Pointer to the list of values.
302 * \param param Parameter to parse.
303 * \param[in] scanner Scanner data structure.
304 * \returns true if the values were parsed successfully, false otherwise.
307 parse_values_range(int nval, t_selexpr_value *values, gmx_ana_selparam_t *param,
310 t_selexpr_value *value;
315 param->flags &= ~SPAR_DYNAMIC;
316 if (param->val.type != INT_VALUE && param->val.type != REAL_VALUE)
318 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid range parameter type");
323 if (param->val.type == INT_VALUE)
335 if (value->hasExpressionValue())
337 _gmx_selparser_error(scanner, "expressions not supported within range parameters");
340 if (value->type != param->val.type)
342 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid range value type");
345 if (param->val.type == INT_VALUE)
347 /* Make sure the input range is in increasing order */
348 if (value->u.i.i1 > value->u.i.i2)
350 int tmp = value->u.i.i1;
351 value->u.i.i1 = value->u.i.i2;
354 /* Check if the new range overlaps or extends the previous one */
355 if (i > 0 && value->u.i.i1 <= idata[i-1]+1 && value->u.i.i2 >= idata[i-2]-1)
357 idata[i-2] = min(idata[i-2], value->u.i.i1);
358 idata[i-1] = max(idata[i-1], value->u.i.i2);
362 idata[i++] = value->u.i.i1;
363 idata[i++] = value->u.i.i2;
368 /* Make sure the input range is in increasing order */
369 if (value->u.r.r1 > value->u.r.r2)
371 real tmp = value->u.r.r1;
372 value->u.r.r1 = value->u.r.r2;
375 /* Check if the new range overlaps or extends the previous one */
376 if (i > 0 && value->u.r.r1 <= rdata[i-1] && value->u.r.r2 >= rdata[i-2])
378 rdata[i-2] = min(rdata[i-2], value->u.r.r1);
379 rdata[i-1] = max(rdata[i-1], value->u.r.r2);
383 rdata[i++] = value->u.r.r1;
384 rdata[i++] = value->u.r.r2;
390 /* Sort the ranges and merge consequent ones */
391 if (param->val.type == INT_VALUE)
393 qsort(idata, n, 2*sizeof(int), &cmp_int_range);
394 for (i = j = 2; i < 2*n; i += 2)
396 if (idata[j-1]+1 >= idata[i])
398 if (idata[i+1] > idata[j-1])
400 idata[j-1] = idata[i+1];
406 idata[j+1] = idata[i+1];
413 qsort(rdata, n, 2*sizeof(real), &cmp_real_range);
414 for (i = j = 2; i < 2*n; i += 2)
416 if (rdata[j-1]+1 >= rdata[i])
418 if (rdata[i+1] > rdata[j-1])
420 rdata[j-1] = rdata[i+1];
426 rdata[j+1] = rdata[i+1];
432 /* Store the values */
433 if (param->flags & SPAR_VARNUM)
436 if (param->val.type == INT_VALUE)
439 _gmx_selvalue_setstore_alloc(¶m->val, idata, j);
444 _gmx_selvalue_setstore_alloc(¶m->val, rdata, j);
449 if (n != param->val.nr)
451 _gmx_selparser_error(scanner, "the value should consist of exactly one range");
456 if (param->val.type == INT_VALUE)
458 memcpy(param->val.u.i, idata, 2*n*sizeof(int));
463 memcpy(param->val.u.r, rdata, 2*n*sizeof(real));
469 *param->nvalptr = param->val.nr;
471 param->nvalptr = NULL;
477 * Parses the values for a parameter that takes a variable number of values.
479 * \param[in] nval Number of values in \p values.
480 * \param[in] values Pointer to the list of values.
481 * \param param Parameter to parse.
482 * \param root Selection element to which child expressions are added.
483 * \param[in] scanner Scanner data structure.
484 * \returns true if the values were parsed successfully, false otherwise.
486 * For integer ranges, the sequence of numbers from the first to second value
487 * is stored, each as a separate value.
490 parse_values_varnum(int nval, t_selexpr_value *values,
491 gmx_ana_selparam_t *param,
492 const SelectionTreeElementPointer &root,
495 t_selexpr_value *value;
498 param->flags &= ~SPAR_DYNAMIC;
499 /* Update nval if there are integer ranges. */
500 if (param->val.type == INT_VALUE)
505 if (value->type == INT_VALUE && !value->hasExpressionValue())
507 nval += abs(value->u.i.i2 - value->u.i.i1);
513 /* Check that the value type is actually implemented */
514 if (param->val.type != INT_VALUE && param->val.type != REAL_VALUE
515 && param->val.type != STR_VALUE && param->val.type != POS_VALUE)
517 GMX_ERROR_NORET(gmx::eeInternalError,
518 "Variable-count value type not implemented");
522 /* Reserve appropriate amount of memory */
523 if (param->val.type == POS_VALUE)
525 gmx_ana_pos_reserve(param->val.u.p, nval, 0);
526 gmx_ana_pos_set_nr(param->val.u.p, nval);
527 gmx_ana_indexmap_init(¶m->val.u.p->m, NULL, NULL, INDEX_UNKNOWN);
531 _gmx_selvalue_reserve(¶m->val, nval);
538 if (value->hasExpressionValue())
540 _gmx_selparser_error(scanner, "expressions not supported within value lists");
543 if (value->type != param->val.type)
545 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid value type");
548 switch (param->val.type)
551 if (value->u.i.i1 <= value->u.i.i2)
553 for (j = value->u.i.i1; j <= value->u.i.i2; ++j)
555 param->val.u.i[i++] = j;
560 for (j = value->u.i.i1; j >= value->u.i.i2; --j)
562 param->val.u.i[i++] = j;
567 if (value->u.r.r1 != value->u.r.r2)
569 _gmx_selparser_error(scanner, "real ranges not supported");
572 param->val.u.r[i++] = value->u.r.r1;
574 case STR_VALUE: param->val.u.s[i++] = strdup(value->u.s); break;
575 case POS_VALUE: copy_rvec(value->u.x, param->val.u.p->x[i++]); break;
576 default: /* Should not be reached */
577 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid value type");
585 *param->nvalptr = param->val.nr;
587 param->nvalptr = NULL;
588 /* Create a dummy child element to store the string values.
589 * This element is responsible for freeing the values, but carries no
591 if (param->val.type == STR_VALUE)
593 SelectionTreeElementPointer child(new SelectionTreeElement(SEL_CONST));
594 _gmx_selelem_set_vtype(child, STR_VALUE);
595 child->setName(param->name);
596 child->flags &= ~SEL_ALLOCVAL;
597 child->flags |= SEL_FLAGSSET | SEL_VARNUMVAL | SEL_ALLOCDATA;
598 child->v.nr = param->val.nr;
599 _gmx_selvalue_setstore(&child->v, param->val.u.s);
600 /* Because the child is not group-valued, the u union is not used
601 * for anything, so we can abuse it by storing the parameter value
602 * as place_child() expects, but this is really ugly... */
603 child->u.param = param;
604 place_child(root, child, param);
611 * Adds a new subexpression reference to a selection element.
613 * \param[in,out] root Root element to which the subexpression is added.
614 * \param[in] param Parameter for which this expression is a value.
615 * \param[in] expr Expression to add.
616 * \param[in] scanner Scanner data structure.
617 * \returns The created child element.
619 * Creates a new \ref SEL_SUBEXPRREF element and adds it into the child
621 * If \p expr is already a \ref SEL_SUBEXPRREF, it is used as it is.
622 * \ref SEL_ALLOCVAL is cleared for the returned element.
624 static SelectionTreeElementPointer
625 add_child(const SelectionTreeElementPointer &root, gmx_ana_selparam_t *param,
626 const SelectionTreeElementPointer &expr, void *scanner)
628 GMX_RELEASE_ASSERT(root->type == SEL_EXPRESSION || root->type == SEL_MODIFIER,
629 "Unsupported root element for selection parameter parser");
630 SelectionTreeElementPointer child;
631 /* Create a subexpression reference element if necessary */
632 if (expr->type == SEL_SUBEXPRREF)
638 child.reset(new SelectionTreeElement(SEL_SUBEXPRREF));
639 _gmx_selelem_set_vtype(child, expr->v.type);
642 /* Setup the child element */
643 child->flags &= ~SEL_ALLOCVAL;
644 child->u.param = param;
645 if (child->v.type != param->val.type)
647 _gmx_selparser_error(scanner, "invalid expression value");
648 // FIXME: Use exceptions.
649 return SelectionTreeElementPointer();
651 _gmx_selelem_update_flags(child, scanner);
652 if ((child->flags & SEL_DYNAMIC) && !(param->flags & SPAR_DYNAMIC))
654 _gmx_selparser_error(scanner, "dynamic values not supported");
655 // FIXME: Use exceptions.
656 return SelectionTreeElementPointer();
658 if (!(child->flags & SEL_DYNAMIC))
660 param->flags &= ~SPAR_DYNAMIC;
662 /* Put the child element in the correct place */
663 place_child(root, child, param);
668 * Parses an expression value for a parameter that takes a variable number of values.
670 * \param[in] nval Number of values in \p values.
671 * \param[in] values Pointer to the list of values.
672 * \param param Parameter to parse.
673 * \param root Selection element to which child expressions are added.
674 * \param[in] scanner Scanner data structure.
675 * \returns true if the values were parsed successfully, false otherwise.
678 parse_values_varnum_expr(int nval, t_selexpr_value *values,
679 gmx_ana_selparam_t *param,
680 const SelectionTreeElementPointer &root,
683 if (nval != 1 || !values->hasExpressionValue())
685 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid expression value");
689 t_selexpr_value *value = values;
690 SelectionTreeElementPointer child
691 = add_child(root, param, value->expr, scanner);
697 /* Process single-valued expressions */
698 /* TODO: We should also handle SEL_SINGLEVAL expressions here */
699 if (child->v.type == POS_VALUE || child->v.type == GROUP_VALUE)
701 /* Set the value storage */
702 _gmx_selvalue_setstore(&child->v, param->val.u.ptr);
706 *param->nvalptr = param->val.nr;
708 param->nvalptr = NULL;
712 if (!(child->flags & SEL_VARNUMVAL))
714 _gmx_selparser_error(scanner, "invalid expression value");
718 child->flags |= SEL_ALLOCVAL;
720 *param->nvalptr = param->val.nr;
721 /* Rest of the initialization is done during compilation in
728 * Initializes the storage of an expression value.
730 * \param[in,out] sel Selection element that evaluates the value.
731 * \param[in] param Parameter to receive the value.
732 * \param[in] i The value of \p sel evaluates the value \p i for
734 * \param[in] scanner Scanner data structure.
736 * Initializes the data pointer of \p sel such that the result is stored
737 * as the value \p i of \p param.
738 * This function is used internally by parse_values_std().
741 set_expr_value_store(const SelectionTreeElementPointer &sel,
742 gmx_ana_selparam_t *param, int i, void *scanner)
744 if (sel->v.type != GROUP_VALUE && !(sel->flags & SEL_SINGLEVAL))
746 _gmx_selparser_error(scanner, "invalid expression value");
751 case INT_VALUE: sel->v.u.i = ¶m->val.u.i[i]; break;
752 case REAL_VALUE: sel->v.u.r = ¶m->val.u.r[i]; break;
753 case STR_VALUE: sel->v.u.s = ¶m->val.u.s[i]; break;
754 case POS_VALUE: sel->v.u.p = ¶m->val.u.p[i]; break;
755 case GROUP_VALUE: sel->v.u.g = ¶m->val.u.g[i]; break;
757 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid value type");
766 * Parses the values for a parameter that takes a constant number of values.
768 * \param[in] nval Number of values in \p values.
769 * \param[in] values Pointer to the list of values.
770 * \param param Parameter to parse.
771 * \param root Selection element to which child expressions are added.
772 * \param[in] scanner Scanner data structure.
773 * \returns true if the values were parsed successfully, false otherwise.
775 * For integer ranges, the sequence of numbers from the first to second value
776 * is stored, each as a separate value.
779 parse_values_std(int nval, t_selexpr_value *values, gmx_ana_selparam_t *param,
780 const SelectionTreeElementPointer &root, void *scanner)
782 t_selexpr_value *value;
786 /* Handle atom-valued parameters */
787 if (param->flags & SPAR_ATOMVAL)
791 _gmx_selparser_error(scanner, "more than one value not supported");
795 if (value->hasExpressionValue())
797 SelectionTreeElementPointer child
798 = add_child(root, param, value->expr, scanner);
803 child->flags |= SEL_ALLOCVAL;
804 if (child->v.type != GROUP_VALUE && (child->flags & SEL_ATOMVAL))
806 /* Rest of the initialization is done during compilation in
808 /* TODO: Positions are not correctly handled */
812 *param->nvalptr = -1;
816 param->flags &= ~SPAR_ATOMVAL;
822 param->nvalptr = NULL;
823 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE
824 || param->val.type == STR_VALUE)
826 _gmx_selvalue_reserve(¶m->val, 1);
828 return set_expr_value_store(child, param, 0, scanner);
830 /* If we reach here, proceed with normal parameter handling */
832 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE
833 || param->val.type == STR_VALUE)
835 _gmx_selvalue_reserve(¶m->val, 1);
837 param->flags &= ~SPAR_ATOMVAL;
838 param->flags &= ~SPAR_DYNAMIC;
844 while (value && i < param->val.nr)
846 if (value->type != param->val.type)
848 _gmx_selparser_error(scanner, "incorrect value skipped");
852 if (value->hasExpressionValue())
854 SelectionTreeElementPointer child
855 = add_child(root, param, value->expr, scanner);
856 /* Check that the expression is valid */
861 if (!set_expr_value_store(child, param, i, scanner))
865 if (child->flags & SEL_DYNAMIC)
872 /* Value is not an expression */
876 if (value->u.i.i1 <= value->u.i.i2)
878 for (j = value->u.i.i1; j <= value->u.i.i2 && i < param->val.nr; ++j)
880 param->val.u.i[i++] = j;
882 if (j != value->u.i.i2 + 1)
884 _gmx_selparser_error(scanner, "extra values skipped");
889 for (j = value->u.i.i1; j >= value->u.i.i2 && i < param->val.nr; --j)
891 param->val.u.i[i++] = j;
893 if (j != value->u.i.i2 - 1)
895 _gmx_selparser_error(scanner, "extra values skipped");
901 if (value->u.r.r1 != value->u.r.r2)
903 _gmx_selparser_error(scanner, "real ranges not supported");
906 param->val.u.r[i] = value->u.r.r1;
909 param->val.u.s[i] = strdup(value->u.s);
912 gmx_ana_pos_init_const(¶m->val.u.p[i], value->u.x);
916 GMX_ERROR_NORET(gmx::eeInternalError,
917 "Invalid non-expression value");
926 _gmx_selparser_error(scanner, "extra values'");
929 if (i < param->val.nr)
931 _gmx_selparser_error(scanner, "not enough values");
936 param->flags &= ~SPAR_DYNAMIC;
940 *param->nvalptr = param->val.nr;
942 param->nvalptr = NULL;
948 * Parses the values for a boolean parameter.
950 * \param[in] name Name by which the parameter was given.
951 * \param[in] nval Number of values in \p values.
952 * \param[in] values Pointer to the 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_bool(const std::string &name, int nval, t_selexpr_value *values,
959 gmx_ana_selparam_t *param, void *scanner)
963 if (param->val.type != NO_VALUE)
965 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid boolean parameter");
968 if (nval > 1 || (values && values->type != INT_VALUE))
970 _gmx_selparser_error(scanner, "parameter takes only a yes/no/on/off/0/1 value");
975 /* Check if the parameter name is given with a 'no' prefix */
976 if (name.length() > 2 && name[0] == 'n' && name[1] == 'o'
977 && name.compare(2, name.length() - 2, param->name) == 0)
981 if (bSetNo && nval > 0)
983 _gmx_selparser_error(scanner, "parameter 'no%s' should not have a value",
987 if (values && values->u.i.i1 == 0)
992 *param->val.u.b = bSetNo ? false : true;
997 * Parses the values for an enumeration parameter.
999 * \param[in] nval Number of values in \p values.
1000 * \param[in] values Pointer to the list of values.
1001 * \param param Parameter to parse.
1002 * \param[in] scanner Scanner data structure.
1003 * \returns true if the values were parsed successfully, false otherwise.
1006 parse_values_enum(int nval, t_selexpr_value *values, gmx_ana_selparam_t *param,
1013 _gmx_selparser_error(scanner, "a single value is required");
1016 if (values->type != STR_VALUE || param->val.type != STR_VALUE)
1018 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid enum parameter");
1021 if (values->hasExpressionValue())
1023 _gmx_selparser_error(scanner, "expression value for enumerated parameter not supported");
1027 len = strlen(values->u.s);
1030 while (param->val.u.s[i] != NULL)
1032 if (strncmp(values->u.s, param->val.u.s[i], len) == 0)
1034 /* Check if there is a duplicate match */
1037 _gmx_selparser_error(scanner, "ambiguous value");
1046 _gmx_selparser_error(scanner, "invalid value");
1049 param->val.u.s[0] = param->val.u.s[match];
1054 * Replaces constant expressions with their values.
1056 * \param[in,out] values First element in the value list to process.
1059 convert_const_values(t_selexpr_value *values)
1061 t_selexpr_value *val;
1066 if (val->hasExpressionValue() && val->expr->v.type != GROUP_VALUE &&
1067 val->expr->type == SEL_CONST)
1069 SelectionTreeElementPointer expr = val->expr;
1071 switch (expr->v.type)
1074 val->u.i.i1 = val->u.i.i2 = expr->v.u.i[0];
1077 val->u.r.r1 = val->u.r.r2 = expr->v.u.r[0];
1080 val->u.s = expr->v.u.s[0];
1083 copy_rvec(expr->v.u.p->x[0], val->u.x);
1086 GMX_ERROR_NORET(gmx::eeInternalError,
1087 "Unsupported value type");
1096 * \param pparams List of parameters from the selection parser.
1097 * \param[in] nparam Number of parameters in \p params.
1098 * \param params Array of parameters to parse.
1099 * \param root Selection element to which child expressions are added.
1100 * \param[in] scanner Scanner data structure.
1101 * \returns true if the parameters were parsed successfully, false otherwise.
1103 * Initializes the \p params array based on the parameters in \p pparams.
1104 * See the documentation of \c gmx_ana_selparam_t for different options
1105 * available for parsing.
1107 * The list \p pparams and any associated values are freed after the parameters
1108 * have been processed, no matter is there was an error or not.
1111 _gmx_sel_parse_params(const SelectionParserParameterList &pparams,
1112 int nparam, gmx_ana_selparam_t *params,
1113 const SelectionTreeElementPointer &root, void *scanner)
1115 gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
1116 gmx_ana_selparam_t *oparam;
1120 /* Check that the value pointers of SPAR_VARNUM parameters are NULL and
1121 * that they are not NULL for other parameters */
1123 for (i = 0; i < nparam; ++i)
1125 std::string contextStr = gmx::formatString("In parameter '%s'", params[i].name);
1126 gmx::MessageStringContext context(errors, contextStr);
1127 if (params[i].val.type != POS_VALUE && (params[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
1129 if (params[i].val.u.ptr != NULL)
1131 _gmx_selparser_error(scanner, "value pointer is not NULL "
1132 "although it should be for SPAR_VARNUM "
1133 "and SPAR_ATOMVAL parameters");
1135 if ((params[i].flags & SPAR_VARNUM)
1136 && (params[i].flags & SPAR_DYNAMIC) && !params[i].nvalptr)
1138 _gmx_selparser_error(scanner, "nvalptr is NULL but both "
1139 "SPAR_VARNUM and SPAR_DYNAMIC are specified");
1145 if (params[i].val.u.ptr == NULL)
1147 _gmx_selparser_error(scanner, "value pointer is NULL");
1156 /* Parse the parameters */
1158 SelectionParserParameterList::const_iterator piter;
1159 for (piter = pparams.begin(); piter != pparams.end(); ++piter)
1161 const SelectionParserParameterPointer &pparam = *piter;
1162 std::string contextStr;
1163 /* Find the parameter and make some checks */
1164 if (!pparam->name().empty())
1166 contextStr = gmx::formatString("In parameter '%s'", pparam->name().c_str());
1168 oparam = gmx_ana_selparam_find(pparam->name().c_str(), nparam, params);
1172 contextStr = gmx::formatString("In value %d", i + 1);
1173 oparam = ¶ms[i];
1174 if (oparam->name != NULL)
1177 _gmx_selparser_error(scanner, "too many NULL parameters provided");
1185 _gmx_selparser_error(scanner, "all NULL parameters should appear in the beginning of the list");
1189 gmx::MessageStringContext context(errors, contextStr);
1192 _gmx_selparser_error(scanner, "unknown parameter skipped");
1196 if (oparam->val.type != NO_VALUE && pparam->value == NULL)
1198 GMX_ASSERT(pparam->nval == 0, "Inconsistent values and value count");
1199 _gmx_selparser_error(scanner, "no value provided");
1203 if (oparam->flags & SPAR_SET)
1205 _gmx_selparser_error(scanner, "parameter set multiple times, extra values skipped");
1209 oparam->flags |= SPAR_SET;
1210 /* Process the values for the parameter */
1211 convert_const_values(pparam->value);
1212 if (convert_values(pparam->value, oparam->val.type, scanner) != 0)
1214 _gmx_selparser_error(scanner, "invalid value");
1218 if (oparam->val.type == NO_VALUE)
1220 rc = parse_values_bool(pparam->name(), pparam->nval, pparam->value, oparam, scanner);
1222 else if (oparam->flags & SPAR_RANGES)
1224 rc = parse_values_range(pparam->nval, pparam->value, oparam, scanner);
1226 else if (oparam->flags & SPAR_VARNUM)
1228 if (pparam->nval == 1 && pparam->value->hasExpressionValue())
1230 rc = parse_values_varnum_expr(pparam->nval, pparam->value, oparam, root, scanner);
1234 rc = parse_values_varnum(pparam->nval, pparam->value, oparam, root, scanner);
1237 else if (oparam->flags & SPAR_ENUMVAL)
1239 rc = parse_values_enum(pparam->nval, pparam->value, oparam, scanner);
1243 rc = parse_values_std(pparam->nval, pparam->value, oparam, root, scanner);
1250 /* Check that all required parameters are present */
1251 for (i = 0; i < nparam; ++i)
1253 if (!(params[i].flags & SPAR_OPTIONAL) && !(params[i].flags & SPAR_SET))
1255 _gmx_selparser_error(scanner, "required parameter '%s' not specified", params[i].name);