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
46 #include "gromacs/fatalerror/errorcodes.h"
47 #include "gromacs/fatalerror/messagestringcollector.h"
48 #include "gromacs/selection/position.h"
49 #include "gromacs/selection/selmethod.h"
50 #include "gromacs/selection/selparam.h"
51 #include "gromacs/utility/format.h"
53 #include "parsetree.h"
59 static T min(T a, T b)
61 return (a < b) ? a : b;
65 static T max(T a, T b)
67 return (a > b) ? a : b;
71 * \param[in] name Name of the parameter to search.
72 * \param[in] nparam Number of parameters in the \p param array.
73 * \param[in] param Parameter array to search.
74 * \returns Pointer to the parameter in the \p param
75 * or NULL if no parameter with name \p name was found.
77 * The comparison is case-sensitive.
80 gmx_ana_selparam_find(const char *name, int nparam, gmx_ana_selparam_t *param)
88 /* Find the first non-null parameter */
90 while (i < nparam && param[i].name == NULL)
94 /* Process the special case of a NULL parameter */
97 return (i == 0) ? NULL : ¶m[i-1];
99 for ( ; i < nparam; ++i)
101 if (!strcmp(param[i].name, name))
105 /* Check for 'no' prefix on gmx_boolean parameters */
106 if (param[i].val.type == NO_VALUE
107 && strlen(name) > 2 && name[0] == 'n' && name[1] == 'o'
108 && !strcmp(param[i].name, name+2))
117 * Does a type conversion on a \c t_selexpr_value.
119 * \param[in,out] value Value to convert.
120 * \param[in] type Type to convert to.
121 * \param[in] scanner Scanner data structure.
122 * \returns 0 on success, a non-zero value on error.
125 convert_value(t_selexpr_value *value, e_selvalue_t type, void *scanner)
127 if (value->type == type || type == NO_VALUE)
133 /* Conversion from atom selection to position using default
134 * reference positions. */
135 if (value->type == GROUP_VALUE && type == POS_VALUE)
138 _gmx_sel_init_position(value->u.expr, NULL, scanner);
139 if (value->u.expr == NULL)
150 /* Integers to floating point are easy */
151 if (value->type == INT_VALUE && type == REAL_VALUE)
153 real r1 = (real)value->u.i.i1;
154 real r2 = (real)value->u.i.i2;
160 /* Reals that are integer-valued can also be converted */
161 if (value->type == REAL_VALUE && type == INT_VALUE
162 && gmx_within_tol(value->u.r.r1, (int)value->u.r.r1, GMX_REAL_EPS)
163 && gmx_within_tol(value->u.r.r2, (int)value->u.r.r2, GMX_REAL_EPS))
165 int i1 = (int)value->u.r.r1;
166 int i2 = (int)value->u.r.r2;
177 * Does a type conversion on a list of values.
179 * \param[in,out] values Values to convert.
180 * \param[in] type Type to convert to.
181 * \param[in] scanner Scanner data structure.
182 * \returns 0 on success, a non-zero value on error.
185 convert_values(t_selexpr_value *values, e_selvalue_t type, void *scanner)
187 t_selexpr_value *value;
194 rc1 = convert_value(value, type, scanner);
195 if (rc1 != 0 && rc == 0)
201 /* FIXME: More informative error messages */
206 * Adds a child element for a parameter, keeping the parameter order.
208 * \param[in,out] root Root element to which the child is added.
209 * \param[in] child Child to add.
210 * \param[in] param Parameter for which this child is a value.
212 * Puts \p child in the child list of \p root such that the list remains
213 * in the same order as the corresponding parameters.
216 place_child(t_selelem *root, t_selelem *child, 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;
234 while (prev->next && prev->next->u.param - ps >= n)
238 child->next = prev->next;
244 * Comparison function for sorting integer ranges.
246 * \param[in] a Pointer to the first range.
247 * \param[in] b Pointer to the second range.
248 * \returns -1, 0, or 1 depending on the relative order of \p a and \p b.
250 * The ranges are primarily sorted based on their starting point, and
251 * secondarily based on length (longer ranges come first).
254 cmp_int_range(const void *a, const void *b)
256 if (((int *)a)[0] < ((int *)b)[0])
260 if (((int *)a)[0] > ((int *)b)[0])
264 if (((int *)a)[1] > ((int *)b)[1])
272 * Comparison function for sorting real ranges.
274 * \param[in] a Pointer to the first range.
275 * \param[in] b Pointer to the second range.
276 * \returns -1, 0, or 1 depending on the relative order of \p a and \p b.
278 * The ranges are primarily sorted based on their starting point, and
279 * secondarily based on length (longer ranges come first).
282 cmp_real_range(const void *a, const void *b)
284 if (((real *)a)[0] < ((real *)b)[0])
288 if (((real *)a)[0] > ((real *)b)[0])
292 if (((real *)a)[1] > ((real *)b)[1])
300 * Parses the values for a parameter that takes integer or real ranges.
302 * \param[in] nval Number of values in \p values.
303 * \param[in] values Pointer to the list of values.
304 * \param param Parameter to parse.
305 * \param[in] scanner Scanner data structure.
306 * \returns TRUE if the values were parsed successfully, FALSE otherwise.
309 parse_values_range(int nval, t_selexpr_value *values, gmx_ana_selparam_t *param,
312 t_selexpr_value *value;
317 param->flags &= ~SPAR_DYNAMIC;
318 if (param->val.type != INT_VALUE && param->val.type != REAL_VALUE)
320 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid range parameter type");
325 if (param->val.type == INT_VALUE)
339 _gmx_selparser_error(scanner, "expressions not supported within range parameters");
342 if (value->type != param->val.type)
344 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid range value type");
347 if (param->val.type == INT_VALUE)
349 /* Make sure the input range is in increasing order */
350 if (value->u.i.i1 > value->u.i.i2)
352 int tmp = value->u.i.i1;
353 value->u.i.i1 = value->u.i.i2;
356 /* Check if the new range overlaps or extends the previous one */
357 if (i > 0 && value->u.i.i1 <= idata[i-1]+1 && value->u.i.i2 >= idata[i-2]-1)
359 idata[i-2] = min(idata[i-2], value->u.i.i1);
360 idata[i-1] = max(idata[i-1], value->u.i.i2);
364 idata[i++] = value->u.i.i1;
365 idata[i++] = value->u.i.i2;
370 /* Make sure the input range is in increasing order */
371 if (value->u.r.r1 > value->u.r.r2)
373 real tmp = value->u.r.r1;
374 value->u.r.r1 = value->u.r.r2;
377 /* Check if the new range overlaps or extends the previous one */
378 if (i > 0 && value->u.r.r1 <= rdata[i-1] && value->u.r.r2 >= rdata[i-2])
380 rdata[i-2] = min(rdata[i-2], value->u.r.r1);
381 rdata[i-1] = max(rdata[i-1], value->u.r.r2);
385 rdata[i++] = value->u.r.r1;
386 rdata[i++] = value->u.r.r2;
392 /* Sort the ranges and merge consequent ones */
393 if (param->val.type == INT_VALUE)
395 qsort(idata, n, 2*sizeof(int), &cmp_int_range);
396 for (i = j = 2; i < 2*n; i += 2)
398 if (idata[j-1]+1 >= idata[i])
400 if (idata[i+1] > idata[j-1])
402 idata[j-1] = idata[i+1];
408 idata[j+1] = idata[i+1];
415 qsort(rdata, n, 2*sizeof(real), &cmp_real_range);
416 for (i = j = 2; i < 2*n; i += 2)
418 if (rdata[j-1]+1 >= rdata[i])
420 if (rdata[i+1] > rdata[j-1])
422 rdata[j-1] = rdata[i+1];
428 rdata[j+1] = rdata[i+1];
434 /* Store the values */
435 if (param->flags & SPAR_VARNUM)
438 if (param->val.type == INT_VALUE)
441 _gmx_selvalue_setstore_alloc(¶m->val, idata, j);
446 _gmx_selvalue_setstore_alloc(¶m->val, rdata, j);
451 if (n != param->val.nr)
453 _gmx_selparser_error(scanner, "the value should consist of exactly one range");
458 if (param->val.type == INT_VALUE)
460 memcpy(param->val.u.i, idata, 2*n*sizeof(int));
465 memcpy(param->val.u.r, rdata, 2*n*sizeof(real));
471 *param->nvalptr = param->val.nr;
473 param->nvalptr = NULL;
479 * Parses the values for a parameter that takes a variable number of values.
481 * \param[in] nval Number of values in \p values.
482 * \param[in] values Pointer to the list of values.
483 * \param param Parameter to parse.
484 * \param root Selection element to which child expressions are added.
485 * \param[in] scanner Scanner data structure.
486 * \returns TRUE if the values were parsed successfully, FALSE otherwise.
488 * For integer ranges, the sequence of numbers from the first to second value
489 * is stored, each as a separate value.
492 parse_values_varnum(int nval, t_selexpr_value *values,
493 gmx_ana_selparam_t *param, t_selelem *root, void *scanner)
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->bExpr)
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);
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)
595 child = _gmx_selelem_create(SEL_CONST);
596 _gmx_selelem_set_vtype(child, STR_VALUE);
597 child->name = param->name;
598 child->flags &= ~SEL_ALLOCVAL;
599 child->flags |= SEL_FLAGSSET | SEL_VARNUMVAL | SEL_ALLOCDATA;
600 child->v.nr = param->val.nr;
601 _gmx_selvalue_setstore(&child->v, param->val.u.s);
602 /* Because the child is not group-valued, the u union is not used
603 * for anything, so we can abuse it by storing the parameter value
604 * as place_child() expects, but this is really ugly... */
605 child->u.param = param;
606 place_child(root, child, param);
613 * Adds a new subexpression reference to a selection element.
615 * \param[in,out] root Root element to which the subexpression is added.
616 * \param[in] param Parameter for which this expression is a value.
617 * \param[in] expr Expression to add.
618 * \param[in] scanner Scanner data structure.
619 * \returns The created child element.
621 * Creates a new \ref SEL_SUBEXPRREF element and adds it into the child
623 * If \p expr is already a \ref SEL_SUBEXPRREF, it is used as it is.
624 * \ref SEL_ALLOCVAL is cleared for the returned element.
627 add_child(t_selelem *root, gmx_ana_selparam_t *param, t_selelem *expr,
633 if (root->type != SEL_EXPRESSION && root->type != SEL_MODIFIER)
635 GMX_ERROR_NORET(gmx::eeInternalError,
636 "Unsupported root element for selection parameter parser");
639 /* Create a subexpression reference element if necessary */
640 if (expr->type == SEL_SUBEXPRREF)
646 child = _gmx_selelem_create(SEL_SUBEXPRREF);
651 _gmx_selelem_set_vtype(child, expr->v.type);
654 /* Setup the child element */
655 child->flags &= ~SEL_ALLOCVAL;
656 child->u.param = param;
657 if (child->v.type != param->val.type)
659 _gmx_selparser_error(scanner, "invalid expression value");
662 rc = _gmx_selelem_update_flags(child, scanner);
667 if ((child->flags & SEL_DYNAMIC) && !(param->flags & SPAR_DYNAMIC))
669 _gmx_selparser_error(scanner, "dynamic values not supported");
672 if (!(child->flags & SEL_DYNAMIC))
674 param->flags &= ~SPAR_DYNAMIC;
676 /* Put the child element in the correct place */
677 place_child(root, child, param);
683 _gmx_selelem_free(child);
689 * Parses an expression value for a parameter that takes a variable number of values.
691 * \param[in] nval Number of values in \p values.
692 * \param[in] values Pointer to the list of values.
693 * \param param Parameter to parse.
694 * \param root Selection element to which child expressions are added.
695 * \param[in] scanner Scanner data structure.
696 * \returns TRUE if the values were parsed successfully, FALSE otherwise.
699 parse_values_varnum_expr(int nval, t_selexpr_value *values,
700 gmx_ana_selparam_t *param, t_selelem *root,
703 t_selexpr_value *value;
707 if (nval != 1 || !values->bExpr)
709 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid expression value");
714 child = add_child(root, param, value->u.expr, scanner);
715 value->u.expr = NULL;
721 /* Process single-valued expressions */
722 /* TODO: We should also handle SEL_SINGLEVAL expressions here */
723 if (child->v.type == POS_VALUE || child->v.type == GROUP_VALUE)
725 /* Set the value storage */
726 _gmx_selvalue_setstore(&child->v, param->val.u.ptr);
730 *param->nvalptr = param->val.nr;
732 param->nvalptr = NULL;
736 if (!(child->flags & SEL_VARNUMVAL))
738 _gmx_selparser_error(scanner, "invalid expression value");
742 child->flags |= SEL_ALLOCVAL;
744 *param->nvalptr = param->val.nr;
745 /* Rest of the initialization is done during compilation in
752 * Initializes the storage of an expression value.
754 * \param[in,out] sel Selection element that evaluates the value.
755 * \param[in] param Parameter to receive the value.
756 * \param[in] i The value of \p sel evaluates the value \p i for
758 * \param[in] scanner Scanner data structure.
760 * Initializes the data pointer of \p sel such that the result is stored
761 * as the value \p i of \p param.
762 * This function is used internally by parse_values_std().
765 set_expr_value_store(t_selelem *sel, gmx_ana_selparam_t *param, int i,
768 if (sel->v.type != GROUP_VALUE && !(sel->flags & SEL_SINGLEVAL))
770 _gmx_selparser_error(scanner, "invalid expression value");
775 case INT_VALUE: sel->v.u.i = ¶m->val.u.i[i]; break;
776 case REAL_VALUE: sel->v.u.r = ¶m->val.u.r[i]; break;
777 case STR_VALUE: sel->v.u.s = ¶m->val.u.s[i]; break;
778 case POS_VALUE: sel->v.u.p = ¶m->val.u.p[i]; break;
779 case GROUP_VALUE: sel->v.u.g = ¶m->val.u.g[i]; break;
781 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid value type");
790 * Parses the values for a parameter that takes a constant number of values.
792 * \param[in] nval Number of values in \p values.
793 * \param[in] values Pointer to the list of values.
794 * \param param Parameter to parse.
795 * \param root Selection element to which child expressions are added.
796 * \param[in] scanner Scanner data structure.
797 * \returns TRUE if the values were parsed successfully, FALSE otherwise.
799 * For integer ranges, the sequence of numbers from the first to second value
800 * is stored, each as a separate value.
803 parse_values_std(int nval, t_selexpr_value *values, gmx_ana_selparam_t *param,
804 t_selelem *root, void *scanner)
806 t_selexpr_value *value;
811 /* Handle atom-valued parameters */
812 if (param->flags & SPAR_ATOMVAL)
816 _gmx_selparser_error(scanner, "more than one value not supported");
822 child = add_child(root, param, value->u.expr, scanner);
823 value->u.expr = NULL;
828 child->flags |= SEL_ALLOCVAL;
829 if (child->v.type != GROUP_VALUE && (child->flags & SEL_ATOMVAL))
831 /* Rest of the initialization is done during compilation in
833 /* TODO: Positions are not correctly handled */
837 *param->nvalptr = -1;
841 param->flags &= ~SPAR_ATOMVAL;
847 param->nvalptr = NULL;
848 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE
849 || param->val.type == STR_VALUE)
851 _gmx_selvalue_reserve(¶m->val, 1);
853 return set_expr_value_store(child, param, 0, scanner);
855 /* If we reach here, proceed with normal parameter handling */
857 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE
858 || param->val.type == STR_VALUE)
860 _gmx_selvalue_reserve(¶m->val, 1);
862 param->flags &= ~SPAR_ATOMVAL;
863 param->flags &= ~SPAR_DYNAMIC;
869 while (value && i < param->val.nr)
871 if (value->type != param->val.type)
873 _gmx_selparser_error(scanner, "incorrect value skipped");
879 child = add_child(root, param, value->u.expr, scanner);
880 /* Clear the expression from the value once it is stored */
881 value->u.expr = NULL;
882 /* Check that the expression is valid */
887 if (!set_expr_value_store(child, param, i, scanner))
891 if (child->flags & SEL_DYNAMIC)
898 /* Value is not an expression */
902 if (value->u.i.i1 <= value->u.i.i2)
904 for (j = value->u.i.i1; j <= value->u.i.i2 && i < param->val.nr; ++j)
906 param->val.u.i[i++] = j;
908 if (j != value->u.i.i2 + 1)
910 _gmx_selparser_error(scanner, "extra values skipped");
915 for (j = value->u.i.i1; j >= value->u.i.i2 && i < param->val.nr; --j)
917 param->val.u.i[i++] = j;
919 if (j != value->u.i.i2 - 1)
921 _gmx_selparser_error(scanner, "extra values skipped");
927 if (value->u.r.r1 != value->u.r.r2)
929 _gmx_selparser_error(scanner, "real ranges not supported");
932 param->val.u.r[i] = value->u.r.r1;
935 param->val.u.s[i] = strdup(value->u.s);
938 gmx_ana_pos_init_const(¶m->val.u.p[i], value->u.x);
942 GMX_ERROR_NORET(gmx::eeInternalError,
943 "Invalid non-expression value");
952 _gmx_selparser_error(scanner, "extra values'");
955 if (i < param->val.nr)
957 _gmx_selparser_error(scanner, "not enough values");
962 param->flags &= ~SPAR_DYNAMIC;
966 *param->nvalptr = param->val.nr;
968 param->nvalptr = NULL;
974 * Parses the values for a boolean parameter.
976 * \param[in] name Name by which the parameter was given.
977 * \param[in] nval Number of values in \p values.
978 * \param[in] values Pointer to the list of values.
979 * \param param Parameter to parse.
980 * \param[in] scanner Scanner data structure.
981 * \returns TRUE if the values were parsed successfully, FALSE otherwise.
984 parse_values_bool(const char *name, int nval, t_selexpr_value *values,
985 gmx_ana_selparam_t *param, void *scanner)
990 if (param->val.type != NO_VALUE)
992 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid boolean parameter");
995 if (nval > 1 || (values && values->type != INT_VALUE))
997 _gmx_selparser_error(scanner, "parameter takes only a yes/no/on/off/0/1 value");
1002 /* Check if the parameter name is given with a 'no' prefix */
1004 if (len > 2 && name[0] == 'n' && name[1] == 'o'
1005 && strncmp(name+2, param->name, len-2) == 0)
1009 if (bSetNo && nval > 0)
1011 _gmx_selparser_error(scanner, "parameter 'no%s' should not have a value",
1015 if (values && values->u.i.i1 == 0)
1020 *param->val.u.b = bSetNo ? FALSE : TRUE;
1025 * Parses the values for an enumeration parameter.
1027 * \param[in] nval Number of values in \p values.
1028 * \param[in] values Pointer to the list of values.
1029 * \param param Parameter to parse.
1030 * \param[in] scanner Scanner data structure.
1031 * \returns TRUE if the values were parsed successfully, FALSE otherwise.
1034 parse_values_enum(int nval, t_selexpr_value *values, gmx_ana_selparam_t *param,
1041 _gmx_selparser_error(scanner, "a single value is required");
1044 if (values->type != STR_VALUE || param->val.type != STR_VALUE)
1046 GMX_ERROR_NORET(gmx::eeInternalError, "Invalid enum parameter");
1051 _gmx_selparser_error(scanner, "expression value for enumerated parameter not supported");
1055 len = strlen(values->u.s);
1058 while (param->val.u.s[i] != NULL)
1060 if (strncmp(values->u.s, param->val.u.s[i], len) == 0)
1062 /* Check if there is a duplicate match */
1065 _gmx_selparser_error(scanner, "ambiguous value");
1074 _gmx_selparser_error(scanner, "invalid value");
1077 param->val.u.s[0] = param->val.u.s[match];
1082 * Replaces constant expressions with their values.
1084 * \param[in,out] values First element in the value list to process.
1087 convert_const_values(t_selexpr_value *values)
1089 t_selexpr_value *val;
1094 if (val->bExpr && val->u.expr->v.type != GROUP_VALUE &&
1095 val->u.expr->type == SEL_CONST)
1097 t_selelem *expr = val->u.expr;
1099 switch (expr->v.type)
1102 val->u.i.i1 = val->u.i.i2 = expr->v.u.i[0];
1105 val->u.r.r1 = val->u.r.r2 = expr->v.u.r[0];
1108 val->u.s = expr->v.u.s[0];
1111 copy_rvec(expr->v.u.p->x[0], val->u.x);
1114 GMX_ERROR_NORET(gmx::eeInternalError,
1115 "Unsupported value type");
1118 _gmx_selelem_free(expr);
1125 * \param pparams List of parameters from the selection parser.
1126 * \param[in] nparam Number of parameters in \p params.
1127 * \param params Array of parameters to parse.
1128 * \param root Selection element to which child expressions are added.
1129 * \param[in] scanner Scanner data structure.
1130 * \returns TRUE if the parameters were parsed successfully, FALSE otherwise.
1132 * Initializes the \p params array based on the parameters in \p pparams.
1133 * See the documentation of \c gmx_ana_selparam_t for different options
1134 * available for parsing.
1136 * The list \p pparams and any associated values are freed after the parameters
1137 * have been processed, no matter is there was an error or not.
1140 _gmx_sel_parse_params(t_selexpr_param *pparams, int nparam, gmx_ana_selparam_t *params,
1141 t_selelem *root, void *scanner)
1143 gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
1144 t_selexpr_param *pparam;
1145 gmx_ana_selparam_t *oparam;
1149 /* Check that the value pointers of SPAR_VARNUM parameters are NULL and
1150 * that they are not NULL for other parameters */
1152 for (i = 0; i < nparam; ++i)
1154 std::string contextStr = gmx::formatString("In parameter '%s'", params[i].name);
1155 gmx::MessageStringContext context(errors, contextStr);
1156 if (params[i].val.type != POS_VALUE && (params[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
1158 if (params[i].val.u.ptr != NULL)
1160 _gmx_selparser_error(scanner, "value pointer is not NULL "
1161 "although it should be for SPAR_VARNUM "
1162 "and SPAR_ATOMVAL parameters");
1164 if ((params[i].flags & SPAR_VARNUM)
1165 && (params[i].flags & SPAR_DYNAMIC) && !params[i].nvalptr)
1167 _gmx_selparser_error(scanner, "nvalptr is NULL but both "
1168 "SPAR_VARNUM and SPAR_DYNAMIC are specified");
1174 if (params[i].val.u.ptr == NULL)
1176 _gmx_selparser_error(scanner, "value pointer is NULL");
1183 _gmx_selexpr_free_params(pparams);
1186 /* Parse the parameters */
1191 std::string contextStr;
1192 /* Find the parameter and make some checks */
1193 if (pparam->name != NULL)
1195 contextStr = gmx::formatString("In parameter '%s'", pparam->name);
1197 oparam = gmx_ana_selparam_find(pparam->name, nparam, params);
1201 contextStr = gmx::formatString("In value %d", i + 1);
1202 oparam = ¶ms[i];
1203 if (oparam->name != NULL)
1206 _gmx_selparser_error(scanner, "too many NULL parameters provided");
1208 pparam = pparam->next;
1215 _gmx_selparser_error(scanner, "all NULL parameters should appear in the beginning of the list");
1217 pparam = pparam->next;
1220 gmx::MessageStringContext context(errors, contextStr);
1223 _gmx_selparser_error(scanner, "unknown parameter skipped");
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);