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
44 #include "gromacs/legacyheaders/smalloc.h"
45 #include "gromacs/legacyheaders/string2.h"
46 #include "gromacs/legacyheaders/vec.h"
48 #include "gromacs/selection/position.h"
49 #include "gromacs/selection/selmethod.h"
50 #include "gromacs/selection/selparam.h"
51 #include "gromacs/utility/errorcodes.h"
52 #include "gromacs/utility/exceptions.h"
53 #include "gromacs/utility/gmxassert.h"
54 #include "gromacs/utility/messagestringcollector.h"
55 #include "gromacs/utility/stringutil.h"
57 #include "parsetree.h"
65 using gmx::SelectionTreeElement;
66 using gmx::SelectionTreeElementPointer;
69 * \param[in] name Name of the parameter to search.
70 * \param[in] nparam Number of parameters in the \p param array.
71 * \param[in] param Parameter array to search.
72 * \returns Pointer to the parameter in the \p param
73 * or NULL if no parameter with name \p name was found.
75 * The comparison is case-sensitive.
78 gmx_ana_selparam_find(const char *name, int nparam, gmx_ana_selparam_t *param)
86 /* Find the first non-null parameter */
88 while (i < nparam && param[i].name == NULL)
92 /* Process the special case of a NULL parameter */
95 return (i == 0) ? NULL : ¶m[i-1];
97 for ( ; i < nparam; ++i)
99 if (!strcmp(param[i].name, name))
103 /* Check for 'no' prefix on boolean parameters */
104 if (param[i].val.type == NO_VALUE
105 && strlen(name) > 2 && name[0] == 'n' && name[1] == 'o'
106 && !strcmp(param[i].name, name+2))
115 * Does a type conversion on a \c t_selexpr_value.
117 * \param[in,out] value Value to convert.
118 * \param[in] type Type to convert to.
119 * \param[in] scanner Scanner data structure.
120 * \returns 0 on success, a non-zero value on error.
123 convert_value(t_selexpr_value *value, e_selvalue_t type, void *scanner)
125 if (value->type == type || type == NO_VALUE)
129 if (value->hasExpressionValue())
131 /* Conversion from atom selection to position using default
132 * reference positions. */
133 if (value->type == GROUP_VALUE && type == POS_VALUE)
136 _gmx_sel_init_position(value->expr, NULL, scanner);
137 // FIXME: Use exceptions
149 /* Integers to floating point are easy */
150 if (value->type == INT_VALUE && type == REAL_VALUE)
152 real r1 = (real)value->u.i.i1;
153 real r2 = (real)value->u.i.i2;
159 /* Reals that are integer-valued can also be converted */
160 if (value->type == REAL_VALUE && type == INT_VALUE
161 && gmx_within_tol(value->u.r.r1, (int)value->u.r.r1, GMX_REAL_EPS)
162 && gmx_within_tol(value->u.r.r2, (int)value->u.r.r2, GMX_REAL_EPS))
164 int i1 = (int)value->u.r.r1;
165 int i2 = (int)value->u.r.r2;
176 * Does a type conversion on a list of values.
178 * \param[in,out] values Values to convert.
179 * \param[in] type Type to convert to.
180 * \param[in] scanner Scanner data structure.
181 * \returns 0 on success, a non-zero value on error.
184 convert_values(t_selexpr_value *values, e_selvalue_t type, void *scanner)
186 t_selexpr_value *value;
193 rc1 = convert_value(value, type, scanner);
194 if (rc1 != 0 && rc == 0)
200 /* FIXME: More informative error messages */
205 * Adds a child element for a parameter, keeping the parameter order.
207 * \param[in,out] root Root element to which the child is added.
208 * \param[in] child Child to add.
209 * \param[in] param Parameter for which this child is a value.
211 * Puts \p child in the child list of \p root such that the list remains
212 * in the same order as the corresponding parameters.
215 place_child(const SelectionTreeElementPointer &root,
216 const SelectionTreeElementPointer &child,
217 gmx_ana_selparam_t *param)
219 gmx_ana_selparam_t *ps;
222 ps = root->u.expr.method->param;
224 /* Put the child element in the correct place */
225 if (!root->child || n < root->child->u.param - ps)
227 child->next = root->child;
232 SelectionTreeElementPointer prev = root->child;
233 while (prev->next && prev->next->u.param - ps >= n)
237 child->next = prev->next;
243 * Comparison function for sorting integer ranges.
245 * \param[in] a Pointer to the first range.
246 * \param[in] b Pointer to the second range.
247 * \returns -1, 0, or 1 depending on the relative order of \p a and \p b.
249 * The ranges are primarily sorted based on their starting point, and
250 * secondarily based on length (longer ranges come first).
253 cmp_int_range(const void *a, const void *b)
255 if (((int *)a)[0] < ((int *)b)[0])
259 if (((int *)a)[0] > ((int *)b)[0])
263 if (((int *)a)[1] > ((int *)b)[1])
271 * Comparison function for sorting real ranges.
273 * \param[in] a Pointer to the first range.
274 * \param[in] b Pointer to the second range.
275 * \returns -1, 0, or 1 depending on the relative order of \p a and \p b.
277 * The ranges are primarily sorted based on their starting point, and
278 * secondarily based on length (longer ranges come first).
281 cmp_real_range(const void *a, const void *b)
283 if (((real *)a)[0] < ((real *)b)[0])
287 if (((real *)a)[0] > ((real *)b)[0])
291 if (((real *)a)[1] > ((real *)b)[1])
299 * Parses the values for a parameter that takes integer or real ranges.
301 * \param[in] nval Number of values in \p values.
302 * \param[in] values Pointer to the list of values.
303 * \param param Parameter to parse.
304 * \param[in] scanner Scanner data structure.
305 * \returns true if the values were parsed successfully, false otherwise.
308 parse_values_range(int nval, t_selexpr_value *values, gmx_ana_selparam_t *param,
311 t_selexpr_value *value;
316 param->flags &= ~SPAR_DYNAMIC;
317 if (param->val.type != INT_VALUE && param->val.type != REAL_VALUE)
319 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid range parameter type");
324 if (param->val.type == INT_VALUE)
336 if (value->hasExpressionValue())
338 _gmx_selparser_error(scanner, "expressions not supported within range parameters");
341 if (value->type != param->val.type)
343 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid range value type");
346 if (param->val.type == INT_VALUE)
348 /* Make sure the input range is in increasing order */
349 if (value->u.i.i1 > value->u.i.i2)
351 int tmp = value->u.i.i1;
352 value->u.i.i1 = value->u.i.i2;
355 /* Check if the new range overlaps or extends the previous one */
356 if (i > 0 && value->u.i.i1 <= idata[i-1]+1 && value->u.i.i2 >= idata[i-2]-1)
358 idata[i-2] = min(idata[i-2], value->u.i.i1);
359 idata[i-1] = max(idata[i-1], value->u.i.i2);
363 idata[i++] = value->u.i.i1;
364 idata[i++] = value->u.i.i2;
369 /* Make sure the input range is in increasing order */
370 if (value->u.r.r1 > value->u.r.r2)
372 real tmp = value->u.r.r1;
373 value->u.r.r1 = value->u.r.r2;
376 /* Check if the new range overlaps or extends the previous one */
377 if (i > 0 && value->u.r.r1 <= rdata[i-1] && value->u.r.r2 >= rdata[i-2])
379 rdata[i-2] = min(rdata[i-2], value->u.r.r1);
380 rdata[i-1] = max(rdata[i-1], value->u.r.r2);
384 rdata[i++] = value->u.r.r1;
385 rdata[i++] = value->u.r.r2;
391 /* Sort the ranges and merge consequent ones */
392 if (param->val.type == INT_VALUE)
394 qsort(idata, n, 2*sizeof(int), &cmp_int_range);
395 for (i = j = 2; i < 2*n; i += 2)
397 if (idata[j-1]+1 >= idata[i])
399 if (idata[i+1] > idata[j-1])
401 idata[j-1] = idata[i+1];
407 idata[j+1] = idata[i+1];
414 qsort(rdata, n, 2*sizeof(real), &cmp_real_range);
415 for (i = j = 2; i < 2*n; i += 2)
417 if (rdata[j-1]+1 >= rdata[i])
419 if (rdata[i+1] > rdata[j-1])
421 rdata[j-1] = rdata[i+1];
427 rdata[j+1] = rdata[i+1];
433 /* Store the values */
434 if (param->flags & SPAR_VARNUM)
437 if (param->val.type == INT_VALUE)
440 _gmx_selvalue_setstore_alloc(¶m->val, idata, j);
445 _gmx_selvalue_setstore_alloc(¶m->val, rdata, j);
450 if (n != param->val.nr)
452 _gmx_selparser_error(scanner, "the value should consist of exactly one range");
457 if (param->val.type == INT_VALUE)
459 memcpy(param->val.u.i, idata, 2*n*sizeof(int));
464 memcpy(param->val.u.r, rdata, 2*n*sizeof(real));
470 *param->nvalptr = param->val.nr;
472 param->nvalptr = NULL;
478 * Parses the values for a parameter that takes a variable number of values.
480 * \param[in] nval Number of values in \p values.
481 * \param[in] values Pointer to the list of values.
482 * \param param Parameter to parse.
483 * \param root Selection element to which child expressions are added.
484 * \param[in] scanner Scanner data structure.
485 * \returns true if the values were parsed successfully, false otherwise.
487 * For integer ranges, the sequence of numbers from the first to second value
488 * is stored, each as a separate value.
491 parse_values_varnum(int nval, t_selexpr_value *values,
492 gmx_ana_selparam_t *param,
493 const SelectionTreeElementPointer &root,
496 t_selexpr_value *value;
499 param->flags &= ~SPAR_DYNAMIC;
500 /* Update nval if there are integer ranges. */
501 if (param->val.type == INT_VALUE)
506 if (value->type == INT_VALUE && !value->hasExpressionValue())
508 nval += abs(value->u.i.i2 - value->u.i.i1);
514 /* Check that the value type is actually implemented */
515 if (param->val.type != INT_VALUE && param->val.type != REAL_VALUE
516 && param->val.type != STR_VALUE && param->val.type != POS_VALUE)
518 GMX_ERROR_NORET(gmx::eeInternalError,
519 "Variable-count value type not implemented");
523 /* Reserve appropriate amount of memory */
524 if (param->val.type == POS_VALUE)
526 gmx_ana_pos_reserve(param->val.u.p, nval, 0);
527 gmx_ana_pos_set_nr(param->val.u.p, nval);
528 gmx_ana_indexmap_init(¶m->val.u.p->m, NULL, NULL, INDEX_UNKNOWN);
532 _gmx_selvalue_reserve(¶m->val, nval);
539 if (value->hasExpressionValue())
541 _gmx_selparser_error(scanner, "expressions not supported within value lists");
544 if (value->type != param->val.type)
546 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid value type");
549 switch (param->val.type)
552 if (value->u.i.i1 <= value->u.i.i2)
554 for (j = value->u.i.i1; j <= value->u.i.i2; ++j)
556 param->val.u.i[i++] = j;
561 for (j = value->u.i.i1; j >= value->u.i.i2; --j)
563 param->val.u.i[i++] = j;
568 if (value->u.r.r1 != value->u.r.r2)
570 _gmx_selparser_error(scanner, "real ranges not supported");
573 param->val.u.r[i++] = value->u.r.r1;
575 case STR_VALUE: param->val.u.s[i++] = strdup(value->u.s); break;
576 case POS_VALUE: copy_rvec(value->u.x, param->val.u.p->x[i++]); break;
577 default: /* Should not be reached */
578 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid value type");
586 *param->nvalptr = param->val.nr;
588 param->nvalptr = NULL;
589 /* Create a dummy child element to store the string values.
590 * This element is responsible for freeing the values, but carries no
592 if (param->val.type == STR_VALUE)
594 SelectionTreeElementPointer child(new SelectionTreeElement(SEL_CONST));
595 _gmx_selelem_set_vtype(child, STR_VALUE);
596 child->setName(param->name);
597 child->flags &= ~SEL_ALLOCVAL;
598 child->flags |= SEL_FLAGSSET | SEL_VARNUMVAL | SEL_ALLOCDATA;
599 child->v.nr = param->val.nr;
600 _gmx_selvalue_setstore(&child->v, param->val.u.s);
601 /* Because the child is not group-valued, the u union is not used
602 * for anything, so we can abuse it by storing the parameter value
603 * as place_child() expects, but this is really ugly... */
604 child->u.param = param;
605 place_child(root, child, param);
612 * Adds a new subexpression reference to a selection element.
614 * \param[in,out] root Root element to which the subexpression is added.
615 * \param[in] param Parameter for which this expression is a value.
616 * \param[in] expr Expression to add.
617 * \param[in] scanner Scanner data structure.
618 * \returns The created child element.
620 * Creates a new \ref SEL_SUBEXPRREF element and adds it into the child
622 * If \p expr is already a \ref SEL_SUBEXPRREF, it is used as it is.
623 * \ref SEL_ALLOCVAL is cleared for the returned element.
625 static SelectionTreeElementPointer
626 add_child(const SelectionTreeElementPointer &root, gmx_ana_selparam_t *param,
627 const SelectionTreeElementPointer &expr, void *scanner)
629 GMX_RELEASE_ASSERT(root->type == SEL_EXPRESSION || root->type == SEL_MODIFIER,
630 "Unsupported root element for selection parameter parser");
631 SelectionTreeElementPointer child;
632 /* Create a subexpression reference element if necessary */
633 if (expr->type == SEL_SUBEXPRREF)
639 child.reset(new SelectionTreeElement(SEL_SUBEXPRREF));
640 _gmx_selelem_set_vtype(child, expr->v.type);
643 /* Setup the child element */
644 child->flags &= ~SEL_ALLOCVAL;
645 child->u.param = param;
646 if (child->v.type != param->val.type)
648 _gmx_selparser_error(scanner, "invalid expression value");
649 // FIXME: Use exceptions.
650 return SelectionTreeElementPointer();
652 _gmx_selelem_update_flags(child, scanner);
653 if ((child->flags & SEL_DYNAMIC) && !(param->flags & SPAR_DYNAMIC))
655 _gmx_selparser_error(scanner, "dynamic values not supported");
656 // FIXME: Use exceptions.
657 return SelectionTreeElementPointer();
659 if (!(child->flags & SEL_DYNAMIC))
661 param->flags &= ~SPAR_DYNAMIC;
663 /* Put the child element in the correct place */
664 place_child(root, child, param);
669 * Parses an expression value for a parameter that takes a variable number of values.
671 * \param[in] nval Number of values in \p values.
672 * \param[in] values Pointer to the list of values.
673 * \param param Parameter to parse.
674 * \param root Selection element to which child expressions are added.
675 * \param[in] scanner Scanner data structure.
676 * \returns true if the values were parsed successfully, false otherwise.
679 parse_values_varnum_expr(int nval, t_selexpr_value *values,
680 gmx_ana_selparam_t *param,
681 const SelectionTreeElementPointer &root,
684 if (nval != 1 || !values->hasExpressionValue())
686 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid expression value");
690 t_selexpr_value *value = values;
691 SelectionTreeElementPointer child
692 = add_child(root, param, value->expr, scanner);
698 /* Process single-valued expressions */
699 /* TODO: We should also handle SEL_SINGLEVAL expressions here */
700 if (child->v.type == POS_VALUE || child->v.type == GROUP_VALUE)
702 /* Set the value storage */
703 _gmx_selvalue_setstore(&child->v, param->val.u.ptr);
707 *param->nvalptr = param->val.nr;
709 param->nvalptr = NULL;
713 if (!(child->flags & SEL_VARNUMVAL))
715 _gmx_selparser_error(scanner, "invalid expression value");
719 child->flags |= SEL_ALLOCVAL;
721 *param->nvalptr = param->val.nr;
722 /* Rest of the initialization is done during compilation in
729 * Initializes the storage of an expression value.
731 * \param[in,out] sel Selection element that evaluates the value.
732 * \param[in] param Parameter to receive the value.
733 * \param[in] i The value of \p sel evaluates the value \p i for
735 * \param[in] scanner Scanner data structure.
737 * Initializes the data pointer of \p sel such that the result is stored
738 * as the value \p i of \p param.
739 * This function is used internally by parse_values_std().
742 set_expr_value_store(const SelectionTreeElementPointer &sel,
743 gmx_ana_selparam_t *param, int i, void *scanner)
745 if (sel->v.type != GROUP_VALUE && !(sel->flags & SEL_SINGLEVAL))
747 _gmx_selparser_error(scanner, "invalid expression value");
752 case INT_VALUE: sel->v.u.i = ¶m->val.u.i[i]; break;
753 case REAL_VALUE: sel->v.u.r = ¶m->val.u.r[i]; break;
754 case STR_VALUE: sel->v.u.s = ¶m->val.u.s[i]; break;
755 case POS_VALUE: sel->v.u.p = ¶m->val.u.p[i]; break;
756 case GROUP_VALUE: sel->v.u.g = ¶m->val.u.g[i]; break;
758 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid value type");
767 * Parses the values for a parameter that takes a constant number of values.
769 * \param[in] nval Number of values in \p values.
770 * \param[in] values Pointer to the list of values.
771 * \param param Parameter to parse.
772 * \param root Selection element to which child expressions are added.
773 * \param[in] scanner Scanner data structure.
774 * \returns true if the values were parsed successfully, false otherwise.
776 * For integer ranges, the sequence of numbers from the first to second value
777 * is stored, each as a separate value.
780 parse_values_std(int nval, t_selexpr_value *values, gmx_ana_selparam_t *param,
781 const SelectionTreeElementPointer &root, void *scanner)
783 t_selexpr_value *value;
787 /* Handle atom-valued parameters */
788 if (param->flags & SPAR_ATOMVAL)
792 _gmx_selparser_error(scanner, "more than one value not supported");
796 if (value->hasExpressionValue())
798 SelectionTreeElementPointer child
799 = add_child(root, param, value->expr, scanner);
804 child->flags |= SEL_ALLOCVAL;
805 if (child->v.type != GROUP_VALUE && (child->flags & SEL_ATOMVAL))
807 /* Rest of the initialization is done during compilation in
809 /* TODO: Positions are not correctly handled */
813 *param->nvalptr = -1;
817 param->flags &= ~SPAR_ATOMVAL;
823 param->nvalptr = NULL;
824 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE
825 || param->val.type == STR_VALUE)
827 _gmx_selvalue_reserve(¶m->val, 1);
829 return set_expr_value_store(child, param, 0, scanner);
831 /* If we reach here, proceed with normal parameter handling */
833 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE
834 || param->val.type == STR_VALUE)
836 _gmx_selvalue_reserve(¶m->val, 1);
838 param->flags &= ~SPAR_ATOMVAL;
839 param->flags &= ~SPAR_DYNAMIC;
845 while (value && i < param->val.nr)
847 if (value->type != param->val.type)
849 _gmx_selparser_error(scanner, "incorrect value skipped");
853 if (value->hasExpressionValue())
855 SelectionTreeElementPointer child
856 = add_child(root, param, value->expr, scanner);
857 /* Check that the expression is valid */
862 if (!set_expr_value_store(child, param, i, scanner))
866 if (child->flags & SEL_DYNAMIC)
873 /* Value is not an expression */
877 if (value->u.i.i1 <= value->u.i.i2)
879 for (j = value->u.i.i1; j <= value->u.i.i2 && i < param->val.nr; ++j)
881 param->val.u.i[i++] = j;
883 if (j != value->u.i.i2 + 1)
885 _gmx_selparser_error(scanner, "extra values skipped");
890 for (j = value->u.i.i1; j >= value->u.i.i2 && i < param->val.nr; --j)
892 param->val.u.i[i++] = j;
894 if (j != value->u.i.i2 - 1)
896 _gmx_selparser_error(scanner, "extra values skipped");
902 if (value->u.r.r1 != value->u.r.r2)
904 _gmx_selparser_error(scanner, "real ranges not supported");
907 param->val.u.r[i] = value->u.r.r1;
910 param->val.u.s[i] = strdup(value->u.s);
913 gmx_ana_pos_init_const(¶m->val.u.p[i], value->u.x);
917 GMX_ERROR_NORET(gmx::eeInternalError,
918 "Invalid non-expression value");
927 _gmx_selparser_error(scanner, "extra values'");
930 if (i < param->val.nr)
932 _gmx_selparser_error(scanner, "not enough values");
937 param->flags &= ~SPAR_DYNAMIC;
941 *param->nvalptr = param->val.nr;
943 param->nvalptr = NULL;
949 * Parses the values for a boolean parameter.
951 * \param[in] name Name by which the parameter was given.
952 * \param[in] nval Number of values in \p values.
953 * \param[in] values Pointer to the list of values.
954 * \param param Parameter to parse.
955 * \param[in] scanner Scanner data structure.
956 * \returns true if the values were parsed successfully, false otherwise.
959 parse_values_bool(const char *name, int nval, t_selexpr_value *values,
960 gmx_ana_selparam_t *param, void *scanner)
965 if (param->val.type != NO_VALUE)
967 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid boolean parameter");
970 if (nval > 1 || (values && values->type != INT_VALUE))
972 _gmx_selparser_error(scanner, "parameter takes only a yes/no/on/off/0/1 value");
977 /* Check if the parameter name is given with a 'no' prefix */
978 len = name != NULL ? strlen(name) : 0;
979 if (len > 2 && name[0] == 'n' && name[1] == 'o'
980 && strncmp(name+2, param->name, len-2) == 0)
984 if (bSetNo && nval > 0)
986 _gmx_selparser_error(scanner, "parameter 'no%s' should not have a value",
990 if (values && values->u.i.i1 == 0)
995 *param->val.u.b = bSetNo ? false : true;
1000 * Parses the values for an enumeration parameter.
1002 * \param[in] nval Number of values in \p values.
1003 * \param[in] values Pointer to the list of values.
1004 * \param param Parameter to parse.
1005 * \param[in] scanner Scanner data structure.
1006 * \returns true if the values were parsed successfully, false otherwise.
1009 parse_values_enum(int nval, t_selexpr_value *values, gmx_ana_selparam_t *param,
1016 _gmx_selparser_error(scanner, "a single value is required");
1019 if (values->type != STR_VALUE || param->val.type != STR_VALUE)
1021 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid enum parameter");
1024 if (values->hasExpressionValue())
1026 _gmx_selparser_error(scanner, "expression value for enumerated parameter not supported");
1030 len = strlen(values->u.s);
1033 while (param->val.u.s[i] != NULL)
1035 if (strncmp(values->u.s, param->val.u.s[i], len) == 0)
1037 /* Check if there is a duplicate match */
1040 _gmx_selparser_error(scanner, "ambiguous value");
1049 _gmx_selparser_error(scanner, "invalid value");
1052 param->val.u.s[0] = param->val.u.s[match];
1057 * Replaces constant expressions with their values.
1059 * \param[in,out] values First element in the value list to process.
1062 convert_const_values(t_selexpr_value *values)
1064 t_selexpr_value *val;
1069 if (val->hasExpressionValue() && val->expr->v.type != GROUP_VALUE &&
1070 val->expr->type == SEL_CONST)
1072 SelectionTreeElementPointer expr = val->expr;
1074 switch (expr->v.type)
1077 val->u.i.i1 = val->u.i.i2 = expr->v.u.i[0];
1080 val->u.r.r1 = val->u.r.r2 = expr->v.u.r[0];
1083 val->u.s = expr->v.u.s[0];
1086 copy_rvec(expr->v.u.p->x[0], val->u.x);
1089 GMX_ERROR_NORET(gmx::eeInternalError,
1090 "Unsupported value type");
1099 * \param pparams List of parameters from the selection parser.
1100 * \param[in] nparam Number of parameters in \p params.
1101 * \param params Array of parameters to parse.
1102 * \param root Selection element to which child expressions are added.
1103 * \param[in] scanner Scanner data structure.
1104 * \returns true if the parameters were parsed successfully, false otherwise.
1106 * Initializes the \p params array based on the parameters in \p pparams.
1107 * See the documentation of \c gmx_ana_selparam_t for different options
1108 * available for parsing.
1110 * The list \p pparams and any associated values are freed after the parameters
1111 * have been processed, no matter is there was an error or not.
1114 _gmx_sel_parse_params(t_selexpr_param *pparams, int nparam, gmx_ana_selparam_t *params,
1115 const SelectionTreeElementPointer &root, void *scanner)
1117 gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
1118 t_selexpr_param *pparam;
1119 gmx_ana_selparam_t *oparam;
1123 /* Check that the value pointers of SPAR_VARNUM parameters are NULL and
1124 * that they are not NULL for other parameters */
1126 for (i = 0; i < nparam; ++i)
1128 std::string contextStr = gmx::formatString("In parameter '%s'", params[i].name);
1129 gmx::MessageStringContext context(errors, contextStr);
1130 if (params[i].val.type != POS_VALUE && (params[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
1132 if (params[i].val.u.ptr != NULL)
1134 _gmx_selparser_error(scanner, "value pointer is not NULL "
1135 "although it should be for SPAR_VARNUM "
1136 "and SPAR_ATOMVAL parameters");
1138 if ((params[i].flags & SPAR_VARNUM)
1139 && (params[i].flags & SPAR_DYNAMIC) && !params[i].nvalptr)
1141 _gmx_selparser_error(scanner, "nvalptr is NULL but both "
1142 "SPAR_VARNUM and SPAR_DYNAMIC are specified");
1148 if (params[i].val.u.ptr == NULL)
1150 _gmx_selparser_error(scanner, "value pointer is NULL");
1157 _gmx_selexpr_free_params(pparams);
1160 /* Parse the parameters */
1165 std::string contextStr;
1166 /* Find the parameter and make some checks */
1167 if (pparam->name != NULL)
1169 contextStr = gmx::formatString("In parameter '%s'", pparam->name);
1171 oparam = gmx_ana_selparam_find(pparam->name, nparam, params);
1175 contextStr = gmx::formatString("In value %d", i + 1);
1176 oparam = ¶ms[i];
1177 if (oparam->name != NULL)
1180 _gmx_selparser_error(scanner, "too many NULL parameters provided");
1182 pparam = pparam->next;
1189 _gmx_selparser_error(scanner, "all NULL parameters should appear in the beginning of the list");
1191 pparam = pparam->next;
1194 gmx::MessageStringContext context(errors, contextStr);
1197 _gmx_selparser_error(scanner, "unknown parameter skipped");
1201 if (oparam->val.type != NO_VALUE && pparam->value == NULL)
1203 GMX_ASSERT(pparam->nval == 0, "Inconsistent values and value count");
1204 _gmx_selparser_error(scanner, "no value provided");
1208 if (oparam->flags & SPAR_SET)
1210 _gmx_selparser_error(scanner, "parameter set multiple times, extra values skipped");
1214 oparam->flags |= SPAR_SET;
1215 /* Process the values for the parameter */
1216 convert_const_values(pparam->value);
1217 if (convert_values(pparam->value, oparam->val.type, scanner) != 0)
1219 _gmx_selparser_error(scanner, "invalid value");
1223 if (oparam->val.type == NO_VALUE)
1225 rc = parse_values_bool(pparam->name, pparam->nval, pparam->value, oparam, scanner);
1227 else if (oparam->flags & SPAR_RANGES)
1229 rc = parse_values_range(pparam->nval, pparam->value, oparam, scanner);
1231 else if (oparam->flags & SPAR_VARNUM)
1233 if (pparam->nval == 1 && pparam->value->hasExpressionValue())
1235 rc = parse_values_varnum_expr(pparam->nval, pparam->value, oparam, root, scanner);
1239 rc = parse_values_varnum(pparam->nval, pparam->value, oparam, root, scanner);
1242 else if (oparam->flags & SPAR_ENUMVAL)
1244 rc = parse_values_enum(pparam->nval, pparam->value, oparam, scanner);
1248 rc = parse_values_std(pparam->nval, pparam->value, oparam, root, scanner);
1254 /* Advance to the next parameter */
1256 pparam = pparam->next;
1258 /* Check that all required parameters are present */
1259 for (i = 0; i < nparam; ++i)
1261 if (!(params[i].flags & SPAR_OPTIONAL) && !(params[i].flags & SPAR_SET))
1263 _gmx_selparser_error(scanner, "required parameter '%s' not specified", params[i].name);
1268 _gmx_selexpr_free_params(pparams);