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
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/gmxassert.h"
53 #include "gromacs/utility/messagestringcollector.h"
54 #include "gromacs/utility/stringutil.h"
56 #include "parsetree.h"
65 * \param[in] name Name of the parameter to search.
66 * \param[in] nparam Number of parameters in the \p param array.
67 * \param[in] param Parameter array to search.
68 * \returns Pointer to the parameter in the \p param
69 * or NULL if no parameter with name \p name was found.
71 * The comparison is case-sensitive.
74 gmx_ana_selparam_find(const char *name, int nparam, gmx_ana_selparam_t *param)
82 /* Find the first non-null parameter */
84 while (i < nparam && param[i].name == NULL)
88 /* Process the special case of a NULL parameter */
91 return (i == 0) ? NULL : ¶m[i-1];
93 for ( ; i < nparam; ++i)
95 if (!strcmp(param[i].name, name))
99 /* Check for 'no' prefix on boolean parameters */
100 if (param[i].val.type == NO_VALUE
101 && strlen(name) > 2 && name[0] == 'n' && name[1] == 'o'
102 && !strcmp(param[i].name, name+2))
111 * Does a type conversion on a \c t_selexpr_value.
113 * \param[in,out] value Value to convert.
114 * \param[in] type Type to convert to.
115 * \param[in] scanner Scanner data structure.
116 * \returns 0 on success, a non-zero value on error.
119 convert_value(t_selexpr_value *value, e_selvalue_t type, void *scanner)
121 if (value->type == type || type == NO_VALUE)
127 /* Conversion from atom selection to position using default
128 * reference positions. */
129 if (value->type == GROUP_VALUE && type == POS_VALUE)
132 _gmx_sel_init_position(value->u.expr, NULL, scanner);
133 if (value->u.expr == NULL)
144 /* Integers to floating point are easy */
145 if (value->type == INT_VALUE && type == REAL_VALUE)
147 real r1 = (real)value->u.i.i1;
148 real r2 = (real)value->u.i.i2;
154 /* Reals that are integer-valued can also be converted */
155 if (value->type == REAL_VALUE && type == INT_VALUE
156 && gmx_within_tol(value->u.r.r1, (int)value->u.r.r1, GMX_REAL_EPS)
157 && gmx_within_tol(value->u.r.r2, (int)value->u.r.r2, GMX_REAL_EPS))
159 int i1 = (int)value->u.r.r1;
160 int i2 = (int)value->u.r.r2;
171 * Does a type conversion on a list of values.
173 * \param[in,out] values Values to convert.
174 * \param[in] type Type to convert to.
175 * \param[in] scanner Scanner data structure.
176 * \returns 0 on success, a non-zero value on error.
179 convert_values(t_selexpr_value *values, e_selvalue_t type, void *scanner)
181 t_selexpr_value *value;
188 rc1 = convert_value(value, type, scanner);
189 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(t_selelem *root, t_selelem *child, gmx_ana_selparam_t *param)
212 gmx_ana_selparam_t *ps;
215 ps = root->u.expr.method->param;
217 /* Put the child element in the correct place */
218 if (!root->child || n < root->child->u.param - ps)
220 child->next = 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] nval Number of values in \p values.
297 * \param[in] values Pointer to the list of values.
298 * \param param Parameter to parse.
299 * \param[in] scanner Scanner data structure.
300 * \returns true if the values were parsed successfully, false otherwise.
303 parse_values_range(int nval, t_selexpr_value *values, gmx_ana_selparam_t *param,
306 t_selexpr_value *value;
311 param->flags &= ~SPAR_DYNAMIC;
312 if (param->val.type != INT_VALUE && param->val.type != REAL_VALUE)
314 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid range parameter type");
319 if (param->val.type == INT_VALUE)
333 _gmx_selparser_error(scanner, "expressions not supported within range parameters");
336 if (value->type != param->val.type)
338 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid range value type");
341 if (param->val.type == INT_VALUE)
343 /* Make sure the input range is in increasing order */
344 if (value->u.i.i1 > value->u.i.i2)
346 int tmp = value->u.i.i1;
347 value->u.i.i1 = value->u.i.i2;
350 /* Check if the new range overlaps or extends the previous one */
351 if (i > 0 && value->u.i.i1 <= idata[i-1]+1 && value->u.i.i2 >= idata[i-2]-1)
353 idata[i-2] = min(idata[i-2], value->u.i.i1);
354 idata[i-1] = max(idata[i-1], value->u.i.i2);
358 idata[i++] = value->u.i.i1;
359 idata[i++] = value->u.i.i2;
364 /* Make sure the input range is in increasing order */
365 if (value->u.r.r1 > value->u.r.r2)
367 real tmp = value->u.r.r1;
368 value->u.r.r1 = value->u.r.r2;
371 /* Check if the new range overlaps or extends the previous one */
372 if (i > 0 && value->u.r.r1 <= rdata[i-1] && value->u.r.r2 >= rdata[i-2])
374 rdata[i-2] = min(rdata[i-2], value->u.r.r1);
375 rdata[i-1] = max(rdata[i-1], value->u.r.r2);
379 rdata[i++] = value->u.r.r1;
380 rdata[i++] = value->u.r.r2;
386 /* Sort the ranges and merge consequent ones */
387 if (param->val.type == INT_VALUE)
389 qsort(idata, n, 2*sizeof(int), &cmp_int_range);
390 for (i = j = 2; i < 2*n; i += 2)
392 if (idata[j-1]+1 >= idata[i])
394 if (idata[i+1] > idata[j-1])
396 idata[j-1] = idata[i+1];
402 idata[j+1] = idata[i+1];
409 qsort(rdata, n, 2*sizeof(real), &cmp_real_range);
410 for (i = j = 2; i < 2*n; i += 2)
412 if (rdata[j-1]+1 >= rdata[i])
414 if (rdata[i+1] > rdata[j-1])
416 rdata[j-1] = rdata[i+1];
422 rdata[j+1] = rdata[i+1];
428 /* Store the values */
429 if (param->flags & SPAR_VARNUM)
432 if (param->val.type == INT_VALUE)
435 _gmx_selvalue_setstore_alloc(¶m->val, idata, j);
440 _gmx_selvalue_setstore_alloc(¶m->val, rdata, j);
445 if (n != param->val.nr)
447 _gmx_selparser_error(scanner, "the value should consist of exactly one range");
452 if (param->val.type == INT_VALUE)
454 memcpy(param->val.u.i, idata, 2*n*sizeof(int));
459 memcpy(param->val.u.r, rdata, 2*n*sizeof(real));
465 *param->nvalptr = param->val.nr;
467 param->nvalptr = NULL;
473 * Parses the values for a parameter that takes a variable number of values.
475 * \param[in] nval Number of values in \p values.
476 * \param[in] values Pointer to the list of values.
477 * \param param Parameter to parse.
478 * \param root Selection element to which child expressions are added.
479 * \param[in] scanner Scanner data structure.
480 * \returns true if the values were parsed successfully, false otherwise.
482 * For integer ranges, the sequence of numbers from the first to second value
483 * is stored, each as a separate value.
486 parse_values_varnum(int nval, t_selexpr_value *values,
487 gmx_ana_selparam_t *param, t_selelem *root, void *scanner)
489 t_selexpr_value *value;
492 param->flags &= ~SPAR_DYNAMIC;
493 /* Update nval if there are integer ranges. */
494 if (param->val.type == INT_VALUE)
499 if (value->type == INT_VALUE && !value->bExpr)
501 nval += abs(value->u.i.i2 - value->u.i.i1);
507 /* Check that the value type is actually implemented */
508 if (param->val.type != INT_VALUE && param->val.type != REAL_VALUE
509 && param->val.type != STR_VALUE && param->val.type != POS_VALUE)
511 GMX_ERROR_NORET(gmx::eeInternalError,
512 "Variable-count value type not implemented");
516 /* Reserve appropriate amount of memory */
517 if (param->val.type == POS_VALUE)
519 gmx_ana_pos_reserve(param->val.u.p, nval, 0);
520 gmx_ana_pos_set_nr(param->val.u.p, nval);
521 gmx_ana_indexmap_init(¶m->val.u.p->m, NULL, NULL, INDEX_UNKNOWN);
525 _gmx_selvalue_reserve(¶m->val, nval);
534 _gmx_selparser_error(scanner, "expressions not supported within value lists");
537 if (value->type != param->val.type)
539 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid value type");
542 switch (param->val.type)
545 if (value->u.i.i1 <= value->u.i.i2)
547 for (j = value->u.i.i1; j <= value->u.i.i2; ++j)
549 param->val.u.i[i++] = j;
554 for (j = value->u.i.i1; j >= value->u.i.i2; --j)
556 param->val.u.i[i++] = j;
561 if (value->u.r.r1 != value->u.r.r2)
563 _gmx_selparser_error(scanner, "real ranges not supported");
566 param->val.u.r[i++] = value->u.r.r1;
568 case STR_VALUE: param->val.u.s[i++] = strdup(value->u.s); break;
569 case POS_VALUE: copy_rvec(value->u.x, param->val.u.p->x[i++]); break;
570 default: /* Should not be reached */
571 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid value type");
579 *param->nvalptr = param->val.nr;
581 param->nvalptr = NULL;
582 /* Create a dummy child element to store the string values.
583 * This element is responsible for freeing the values, but carries no
585 if (param->val.type == STR_VALUE)
589 child = _gmx_selelem_create(SEL_CONST);
590 _gmx_selelem_set_vtype(child, STR_VALUE);
591 child->name = param->name;
592 child->flags &= ~SEL_ALLOCVAL;
593 child->flags |= SEL_FLAGSSET | SEL_VARNUMVAL | SEL_ALLOCDATA;
594 child->v.nr = param->val.nr;
595 _gmx_selvalue_setstore(&child->v, param->val.u.s);
596 /* Because the child is not group-valued, the u union is not used
597 * for anything, so we can abuse it by storing the parameter value
598 * as place_child() expects, but this is really ugly... */
599 child->u.param = param;
600 place_child(root, child, param);
607 * Adds a new subexpression reference to a selection element.
609 * \param[in,out] root Root element to which the subexpression is added.
610 * \param[in] param Parameter for which this expression is a value.
611 * \param[in] expr Expression to add.
612 * \param[in] scanner Scanner data structure.
613 * \returns The created child element.
615 * Creates a new \ref SEL_SUBEXPRREF element and adds it into the child
617 * If \p expr is already a \ref SEL_SUBEXPRREF, it is used as it is.
618 * \ref SEL_ALLOCVAL is cleared for the returned element.
621 add_child(t_selelem *root, gmx_ana_selparam_t *param, t_selelem *expr,
627 if (root->type != SEL_EXPRESSION && root->type != SEL_MODIFIER)
629 GMX_ERROR_NORET(gmx::eeInternalError,
630 "Unsupported root element for selection parameter parser");
633 /* Create a subexpression reference element if necessary */
634 if (expr->type == SEL_SUBEXPRREF)
640 child = _gmx_selelem_create(SEL_SUBEXPRREF);
645 _gmx_selelem_set_vtype(child, expr->v.type);
648 /* Setup the child element */
649 child->flags &= ~SEL_ALLOCVAL;
650 child->u.param = param;
651 if (child->v.type != param->val.type)
653 _gmx_selparser_error(scanner, "invalid expression value");
656 rc = _gmx_selelem_update_flags(child, scanner);
661 if ((child->flags & SEL_DYNAMIC) && !(param->flags & SPAR_DYNAMIC))
663 _gmx_selparser_error(scanner, "dynamic values not supported");
666 if (!(child->flags & SEL_DYNAMIC))
668 param->flags &= ~SPAR_DYNAMIC;
670 /* Put the child element in the correct place */
671 place_child(root, child, param);
677 _gmx_selelem_free(child);
683 * Parses an expression value for a parameter that takes a variable number of values.
685 * \param[in] nval Number of values in \p values.
686 * \param[in] values Pointer to the list of values.
687 * \param param Parameter to parse.
688 * \param root Selection element to which child expressions are added.
689 * \param[in] scanner Scanner data structure.
690 * \returns true if the values were parsed successfully, false otherwise.
693 parse_values_varnum_expr(int nval, t_selexpr_value *values,
694 gmx_ana_selparam_t *param, t_selelem *root,
697 t_selexpr_value *value;
700 if (nval != 1 || !values->bExpr)
702 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid expression value");
707 child = add_child(root, param, value->u.expr, scanner);
708 value->u.expr = NULL;
714 /* Process single-valued expressions */
715 /* TODO: We should also handle SEL_SINGLEVAL expressions here */
716 if (child->v.type == POS_VALUE || child->v.type == GROUP_VALUE)
718 /* Set the value storage */
719 _gmx_selvalue_setstore(&child->v, param->val.u.ptr);
723 *param->nvalptr = param->val.nr;
725 param->nvalptr = NULL;
729 if (!(child->flags & SEL_VARNUMVAL))
731 _gmx_selparser_error(scanner, "invalid expression value");
735 child->flags |= SEL_ALLOCVAL;
737 *param->nvalptr = param->val.nr;
738 /* Rest of the initialization is done during compilation in
745 * Initializes the storage of an expression value.
747 * \param[in,out] sel Selection element that evaluates the value.
748 * \param[in] param Parameter to receive the value.
749 * \param[in] i The value of \p sel evaluates the value \p i for
751 * \param[in] scanner Scanner data structure.
753 * Initializes the data pointer of \p sel such that the result is stored
754 * as the value \p i of \p param.
755 * This function is used internally by parse_values_std().
758 set_expr_value_store(t_selelem *sel, gmx_ana_selparam_t *param, int i,
761 if (sel->v.type != GROUP_VALUE && !(sel->flags & SEL_SINGLEVAL))
763 _gmx_selparser_error(scanner, "invalid expression value");
768 case INT_VALUE: sel->v.u.i = ¶m->val.u.i[i]; break;
769 case REAL_VALUE: sel->v.u.r = ¶m->val.u.r[i]; break;
770 case STR_VALUE: sel->v.u.s = ¶m->val.u.s[i]; break;
771 case POS_VALUE: sel->v.u.p = ¶m->val.u.p[i]; break;
772 case GROUP_VALUE: sel->v.u.g = ¶m->val.u.g[i]; break;
774 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid value type");
783 * Parses the values for a parameter that takes a constant number of values.
785 * \param[in] nval Number of values in \p values.
786 * \param[in] values Pointer to the list of values.
787 * \param param Parameter to parse.
788 * \param root Selection element to which child expressions are added.
789 * \param[in] scanner Scanner data structure.
790 * \returns true if the values were parsed successfully, false otherwise.
792 * For integer ranges, the sequence of numbers from the first to second value
793 * is stored, each as a separate value.
796 parse_values_std(int nval, t_selexpr_value *values, gmx_ana_selparam_t *param,
797 t_selelem *root, void *scanner)
799 t_selexpr_value *value;
804 /* Handle atom-valued parameters */
805 if (param->flags & SPAR_ATOMVAL)
809 _gmx_selparser_error(scanner, "more than one value not supported");
815 child = add_child(root, param, value->u.expr, scanner);
816 value->u.expr = NULL;
821 child->flags |= SEL_ALLOCVAL;
822 if (child->v.type != GROUP_VALUE && (child->flags & SEL_ATOMVAL))
824 /* Rest of the initialization is done during compilation in
826 /* TODO: Positions are not correctly handled */
830 *param->nvalptr = -1;
834 param->flags &= ~SPAR_ATOMVAL;
840 param->nvalptr = NULL;
841 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE
842 || param->val.type == STR_VALUE)
844 _gmx_selvalue_reserve(¶m->val, 1);
846 return set_expr_value_store(child, param, 0, scanner);
848 /* If we reach here, proceed with normal parameter handling */
850 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE
851 || param->val.type == STR_VALUE)
853 _gmx_selvalue_reserve(¶m->val, 1);
855 param->flags &= ~SPAR_ATOMVAL;
856 param->flags &= ~SPAR_DYNAMIC;
862 while (value && i < param->val.nr)
864 if (value->type != param->val.type)
866 _gmx_selparser_error(scanner, "incorrect value skipped");
872 child = add_child(root, param, value->u.expr, scanner);
873 /* Clear the expression from the value once it is stored */
874 value->u.expr = NULL;
875 /* Check that the expression is valid */
880 if (!set_expr_value_store(child, param, i, scanner))
884 if (child->flags & SEL_DYNAMIC)
891 /* Value is not an expression */
895 if (value->u.i.i1 <= value->u.i.i2)
897 for (j = value->u.i.i1; j <= value->u.i.i2 && i < param->val.nr; ++j)
899 param->val.u.i[i++] = j;
901 if (j != value->u.i.i2 + 1)
903 _gmx_selparser_error(scanner, "extra values skipped");
908 for (j = value->u.i.i1; j >= value->u.i.i2 && i < param->val.nr; --j)
910 param->val.u.i[i++] = j;
912 if (j != value->u.i.i2 - 1)
914 _gmx_selparser_error(scanner, "extra values skipped");
920 if (value->u.r.r1 != value->u.r.r2)
922 _gmx_selparser_error(scanner, "real ranges not supported");
925 param->val.u.r[i] = value->u.r.r1;
928 param->val.u.s[i] = strdup(value->u.s);
931 gmx_ana_pos_init_const(¶m->val.u.p[i], value->u.x);
935 GMX_ERROR_NORET(gmx::eeInternalError,
936 "Invalid non-expression value");
945 _gmx_selparser_error(scanner, "extra values'");
948 if (i < param->val.nr)
950 _gmx_selparser_error(scanner, "not enough values");
955 param->flags &= ~SPAR_DYNAMIC;
959 *param->nvalptr = param->val.nr;
961 param->nvalptr = NULL;
967 * Parses the values for a boolean parameter.
969 * \param[in] name Name by which the parameter was given.
970 * \param[in] nval Number of values in \p values.
971 * \param[in] values Pointer to the list of values.
972 * \param param Parameter to parse.
973 * \param[in] scanner Scanner data structure.
974 * \returns true if the values were parsed successfully, false otherwise.
977 parse_values_bool(const char *name, int nval, t_selexpr_value *values,
978 gmx_ana_selparam_t *param, void *scanner)
983 if (param->val.type != NO_VALUE)
985 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid boolean parameter");
988 if (nval > 1 || (values && values->type != INT_VALUE))
990 _gmx_selparser_error(scanner, "parameter takes only a yes/no/on/off/0/1 value");
995 /* Check if the parameter name is given with a 'no' prefix */
996 len = name != NULL ? strlen(name) : 0;
997 if (len > 2 && name[0] == 'n' && name[1] == 'o'
998 && strncmp(name+2, param->name, len-2) == 0)
1002 if (bSetNo && nval > 0)
1004 _gmx_selparser_error(scanner, "parameter 'no%s' should not have a value",
1008 if (values && values->u.i.i1 == 0)
1013 *param->val.u.b = bSetNo ? false : true;
1018 * Parses the values for an enumeration parameter.
1020 * \param[in] nval Number of values in \p values.
1021 * \param[in] values Pointer to the list of values.
1022 * \param param Parameter to parse.
1023 * \param[in] scanner Scanner data structure.
1024 * \returns true if the values were parsed successfully, false otherwise.
1027 parse_values_enum(int nval, t_selexpr_value *values, gmx_ana_selparam_t *param,
1034 _gmx_selparser_error(scanner, "a single value is required");
1037 if (values->type != STR_VALUE || param->val.type != STR_VALUE)
1039 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid enum parameter");
1044 _gmx_selparser_error(scanner, "expression value for enumerated parameter not supported");
1048 len = strlen(values->u.s);
1051 while (param->val.u.s[i] != NULL)
1053 if (strncmp(values->u.s, param->val.u.s[i], len) == 0)
1055 /* Check if there is a duplicate match */
1058 _gmx_selparser_error(scanner, "ambiguous value");
1067 _gmx_selparser_error(scanner, "invalid value");
1070 param->val.u.s[0] = param->val.u.s[match];
1075 * Replaces constant expressions with their values.
1077 * \param[in,out] values First element in the value list to process.
1080 convert_const_values(t_selexpr_value *values)
1082 t_selexpr_value *val;
1087 if (val->bExpr && val->u.expr->v.type != GROUP_VALUE &&
1088 val->u.expr->type == SEL_CONST)
1090 t_selelem *expr = val->u.expr;
1092 switch (expr->v.type)
1095 val->u.i.i1 = val->u.i.i2 = expr->v.u.i[0];
1098 val->u.r.r1 = val->u.r.r2 = expr->v.u.r[0];
1101 val->u.s = expr->v.u.s[0];
1104 copy_rvec(expr->v.u.p->x[0], val->u.x);
1107 GMX_ERROR_NORET(gmx::eeInternalError,
1108 "Unsupported value type");
1111 _gmx_selelem_free(expr);
1118 * \param pparams List of parameters from the selection parser.
1119 * \param[in] nparam Number of parameters in \p params.
1120 * \param params Array of parameters to parse.
1121 * \param root Selection element to which child expressions are added.
1122 * \param[in] scanner Scanner data structure.
1123 * \returns true if the parameters were parsed successfully, false otherwise.
1125 * Initializes the \p params array based on the parameters in \p pparams.
1126 * See the documentation of \c gmx_ana_selparam_t for different options
1127 * available for parsing.
1129 * The list \p pparams and any associated values are freed after the parameters
1130 * have been processed, no matter is there was an error or not.
1133 _gmx_sel_parse_params(t_selexpr_param *pparams, int nparam, gmx_ana_selparam_t *params,
1134 t_selelem *root, void *scanner)
1136 gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
1137 t_selexpr_param *pparam;
1138 gmx_ana_selparam_t *oparam;
1142 /* Check that the value pointers of SPAR_VARNUM parameters are NULL and
1143 * that they are not NULL for other parameters */
1145 for (i = 0; i < nparam; ++i)
1147 std::string contextStr = gmx::formatString("In parameter '%s'", params[i].name);
1148 gmx::MessageStringContext context(errors, contextStr);
1149 if (params[i].val.type != POS_VALUE && (params[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
1151 if (params[i].val.u.ptr != NULL)
1153 _gmx_selparser_error(scanner, "value pointer is not NULL "
1154 "although it should be for SPAR_VARNUM "
1155 "and SPAR_ATOMVAL parameters");
1157 if ((params[i].flags & SPAR_VARNUM)
1158 && (params[i].flags & SPAR_DYNAMIC) && !params[i].nvalptr)
1160 _gmx_selparser_error(scanner, "nvalptr is NULL but both "
1161 "SPAR_VARNUM and SPAR_DYNAMIC are specified");
1167 if (params[i].val.u.ptr == NULL)
1169 _gmx_selparser_error(scanner, "value pointer is NULL");
1176 _gmx_selexpr_free_params(pparams);
1179 /* Parse the parameters */
1184 std::string contextStr;
1185 /* Find the parameter and make some checks */
1186 if (pparam->name != NULL)
1188 contextStr = gmx::formatString("In parameter '%s'", pparam->name);
1190 oparam = gmx_ana_selparam_find(pparam->name, nparam, params);
1194 contextStr = gmx::formatString("In value %d", i + 1);
1195 oparam = ¶ms[i];
1196 if (oparam->name != NULL)
1199 _gmx_selparser_error(scanner, "too many NULL parameters provided");
1201 pparam = pparam->next;
1208 _gmx_selparser_error(scanner, "all NULL parameters should appear in the beginning of the list");
1210 pparam = pparam->next;
1213 gmx::MessageStringContext context(errors, contextStr);
1216 _gmx_selparser_error(scanner, "unknown parameter skipped");
1220 if (oparam->val.type != NO_VALUE && pparam->value == NULL)
1222 GMX_ASSERT(pparam->nval == 0, "Inconsistent values and value count");
1223 _gmx_selparser_error(scanner, "no value provided");
1227 if (oparam->flags & SPAR_SET)
1229 _gmx_selparser_error(scanner, "parameter set multiple times, extra values skipped");
1233 oparam->flags |= SPAR_SET;
1234 /* Process the values for the parameter */
1235 convert_const_values(pparam->value);
1236 if (convert_values(pparam->value, oparam->val.type, scanner) != 0)
1238 _gmx_selparser_error(scanner, "invalid value");
1242 if (oparam->val.type == NO_VALUE)
1244 rc = parse_values_bool(pparam->name, pparam->nval, pparam->value, oparam, scanner);
1246 else if (oparam->flags & SPAR_RANGES)
1248 rc = parse_values_range(pparam->nval, pparam->value, oparam, scanner);
1250 else if (oparam->flags & SPAR_VARNUM)
1252 if (pparam->nval == 1 && pparam->value->bExpr)
1254 rc = parse_values_varnum_expr(pparam->nval, pparam->value, oparam, root, scanner);
1258 rc = parse_values_varnum(pparam->nval, pparam->value, oparam, root, scanner);
1261 else if (oparam->flags & SPAR_ENUMVAL)
1263 rc = parse_values_enum(pparam->nval, pparam->value, oparam, scanner);
1267 rc = parse_values_std(pparam->nval, pparam->value, oparam, root, scanner);
1273 /* Advance to the next parameter */
1275 pparam = pparam->next;
1277 /* Check that all required parameters are present */
1278 for (i = 0; i < nparam; ++i)
1280 if (!(params[i].flags & SPAR_OPTIONAL) && !(params[i].flags & SPAR_SET))
1282 _gmx_selparser_error(scanner, "required parameter '%s' not specified", params[i].name);
1287 _gmx_selexpr_free_params(pparams);