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/exceptions.h"
49 #include "gromacs/utility/gmxassert.h"
50 #include "gromacs/utility/messagestringcollector.h"
51 #include "gromacs/utility/stringutil.h"
53 #include "parsetree.h"
58 using gmx::SelectionParserValue;
59 using gmx::SelectionParserValueList;
60 using gmx::SelectionParserParameter;
61 using gmx::SelectionParserParameterList;
62 using gmx::SelectionTreeElement;
63 using gmx::SelectionTreeElementPointer;
66 * \param[in] name Name of the parameter to search.
67 * \param[in] nparam Number of parameters in the \p param array.
68 * \param[in] param Parameter array to search.
69 * \returns Pointer to the parameter in the \p param
70 * or NULL if no parameter with name \p name was found.
72 * The comparison is case-sensitive.
75 gmx_ana_selparam_find(const char *name, int nparam, gmx_ana_selparam_t *param)
83 /* Find the first non-null parameter */
85 while (i < nparam && param[i].name == NULL)
89 /* Process the special case of a NULL parameter */
92 return (i == 0) ? NULL : ¶m[i-1];
94 for ( ; i < nparam; ++i)
96 if (!strcmp(param[i].name, name))
100 /* Check for 'no' prefix on boolean parameters */
101 if (param[i].val.type == NO_VALUE
102 && strlen(name) > 2 && name[0] == 'n' && name[1] == 'o'
103 && !strcmp(param[i].name, name+2))
112 * Does a type conversion on a SelectionParserValue.
114 * \param[in,out] value Value to convert.
115 * \param[in] type Type to convert to.
116 * \param[in] scanner Scanner data structure.
117 * \returns 0 on success, a non-zero value on error.
120 convert_value(SelectionParserValue *value, e_selvalue_t type, void *scanner)
122 if (value->type == type || type == NO_VALUE)
126 if (value->hasExpressionValue())
128 /* Conversion from atom selection to position using default
129 * reference positions. */
130 if (value->type == GROUP_VALUE && type == POS_VALUE)
132 SelectionTreeElementPointer expr =
133 _gmx_sel_init_position(value->expr, NULL, scanner);
134 // FIXME: Use exceptions
139 *value = SelectionParserValue::createExpr(expr);
146 /* Integers to floating point are easy */
147 if (value->type == INT_VALUE && type == REAL_VALUE)
149 *value = SelectionParserValue::createRealRange(value->u.i.i1,
153 /* Reals that are integer-valued can also be converted */
154 if (value->type == REAL_VALUE && type == INT_VALUE)
156 int i1 = static_cast<int>(value->u.r.r1);
157 int i2 = static_cast<int>(value->u.r.r2);
158 if (gmx_within_tol(value->u.r.r1, i1, GMX_REAL_EPS)
159 && gmx_within_tol(value->u.r.r2, i2, GMX_REAL_EPS))
161 *value = SelectionParserValue::createIntegerRange(i1, i2);
170 * Does a type conversion on a list of values.
172 * \param[in,out] values Values to convert.
173 * \param[in] type Type to convert to.
174 * \param[in] scanner Scanner data structure.
175 * \returns 0 on success, a non-zero value on error.
178 convert_values(SelectionParserValueList *values, e_selvalue_t type, void *scanner)
181 SelectionParserValueList::iterator value;
182 for (value = values->begin(); value != values->end(); ++value)
184 int rc1 = convert_value(&*value, type, scanner);
185 if (rc1 != 0 && rc == 0)
190 /* FIXME: More informative error messages */
195 * Adds a child element for a parameter, keeping the parameter order.
197 * \param[in,out] root Root element to which the child is added.
198 * \param[in] child Child to add.
199 * \param[in] param Parameter for which this child is a value.
201 * Puts \p child in the child list of \p root such that the list remains
202 * in the same order as the corresponding parameters.
205 place_child(const SelectionTreeElementPointer &root,
206 const SelectionTreeElementPointer &child,
207 gmx_ana_selparam_t *param)
209 gmx_ana_selparam_t *ps;
212 ps = root->u.expr.method->param;
214 /* Put the child element in the correct place */
215 if (!root->child || n < root->child->u.param - ps)
217 child->next = root->child;
222 SelectionTreeElementPointer prev = root->child;
223 while (prev->next && prev->next->u.param - ps >= n)
227 child->next = prev->next;
233 * Comparison function for sorting integer ranges.
235 * \param[in] a Pointer to the first range.
236 * \param[in] b Pointer to the second range.
237 * \returns -1, 0, or 1 depending on the relative order of \p a and \p b.
239 * The ranges are primarily sorted based on their starting point, and
240 * secondarily based on length (longer ranges come first).
243 cmp_int_range(const void *a, const void *b)
245 if (((int *)a)[0] < ((int *)b)[0])
249 if (((int *)a)[0] > ((int *)b)[0])
253 if (((int *)a)[1] > ((int *)b)[1])
261 * Comparison function for sorting real ranges.
263 * \param[in] a Pointer to the first range.
264 * \param[in] b Pointer to the second range.
265 * \returns -1, 0, or 1 depending on the relative order of \p a and \p b.
267 * The ranges are primarily sorted based on their starting point, and
268 * secondarily based on length (longer ranges come first).
271 cmp_real_range(const void *a, const void *b)
273 if (((real *)a)[0] < ((real *)b)[0])
277 if (((real *)a)[0] > ((real *)b)[0])
281 if (((real *)a)[1] > ((real *)b)[1])
289 * Parses the values for a parameter that takes integer or real ranges.
291 * \param[in] values List of values.
292 * \param param Parameter to parse.
293 * \param[in] scanner Scanner data structure.
294 * \returns true if the values were parsed successfully, false otherwise.
297 parse_values_range(const SelectionParserValueList &values,
298 gmx_ana_selparam_t *param, void *scanner)
304 param->flags &= ~SPAR_DYNAMIC;
305 GMX_RELEASE_ASSERT(param->val.type == INT_VALUE || param->val.type == REAL_VALUE,
306 "Invalid range parameter type");
309 if (param->val.type == INT_VALUE)
311 snew(idata, values.size()*2);
315 snew(rdata, values.size()*2);
318 SelectionParserValueList::const_iterator value;
319 for (value = values.begin(); value != values.end(); ++value)
321 if (value->hasExpressionValue())
323 _gmx_selparser_error(scanner, "expressions not supported within range parameters");
326 GMX_RELEASE_ASSERT(value->type == param->val.type,
327 "Invalid range value type (should have been caught earlier)");
328 if (param->val.type == INT_VALUE)
330 int i1 = std::min(value->u.i.i1, value->u.i.i2);
331 int i2 = std::max(value->u.i.i1, value->u.i.i2);
332 /* Check if the new range overlaps or extends the previous one */
333 if (i > 0 && i1 <= idata[i-1]+1 && i2 >= idata[i-2]-1)
335 idata[i-2] = std::min(idata[i-2], i1);
336 idata[i-1] = std::max(idata[i-1], i2);
346 real r1 = std::min(value->u.r.r1, value->u.r.r2);
347 real r2 = std::max(value->u.r.r1, value->u.r.r2);
348 /* Check if the new range overlaps or extends the previous one */
349 if (i > 0 && r1 <= rdata[i-1] && r2 >= rdata[i-2])
351 rdata[i-2] = std::min(rdata[i-2], r1);
352 rdata[i-1] = std::max(rdata[i-1], r2);
362 /* Sort the ranges and merge consequent ones */
363 if (param->val.type == INT_VALUE)
365 qsort(idata, n, 2*sizeof(int), &cmp_int_range);
366 for (i = j = 2; i < 2*n; i += 2)
368 if (idata[j-1]+1 >= idata[i])
370 if (idata[i+1] > idata[j-1])
372 idata[j-1] = idata[i+1];
378 idata[j+1] = idata[i+1];
385 qsort(rdata, n, 2*sizeof(real), &cmp_real_range);
386 for (i = j = 2; i < 2*n; i += 2)
388 if (rdata[j-1]+1 >= rdata[i])
390 if (rdata[i+1] > rdata[j-1])
392 rdata[j-1] = rdata[i+1];
398 rdata[j+1] = rdata[i+1];
404 /* Store the values */
405 if (param->flags & SPAR_VARNUM)
408 if (param->val.type == INT_VALUE)
411 _gmx_selvalue_setstore_alloc(¶m->val, idata, j);
416 _gmx_selvalue_setstore_alloc(¶m->val, rdata, j);
421 if (n != param->val.nr)
423 _gmx_selparser_error(scanner, "the value should consist of exactly one range");
428 if (param->val.type == INT_VALUE)
430 memcpy(param->val.u.i, idata, 2*n*sizeof(int));
435 memcpy(param->val.u.r, rdata, 2*n*sizeof(real));
441 *param->nvalptr = param->val.nr;
443 param->nvalptr = NULL;
449 * Parses the values for a parameter that takes a variable number of values.
451 * \param[in] values List of values.
452 * \param param Parameter to parse.
453 * \param root Selection element to which child expressions are added.
454 * \param[in] scanner Scanner data structure.
455 * \returns true if the values were parsed successfully, false otherwise.
457 * For integer ranges, the sequence of numbers from the first to second value
458 * is stored, each as a separate value.
461 parse_values_varnum(const SelectionParserValueList &values,
462 gmx_ana_selparam_t *param,
463 const SelectionTreeElementPointer &root,
468 param->flags &= ~SPAR_DYNAMIC;
469 /* Compute number of values, considering also integer ranges. */
470 size_t valueCount = values.size();
471 if (param->val.type == INT_VALUE)
473 SelectionParserValueList::const_iterator value;
474 for (value = values.begin(); value != values.end(); ++value)
476 if (value->type == INT_VALUE && !value->hasExpressionValue())
478 valueCount += abs(value->u.i.i2 - value->u.i.i1);
483 /* Check that the value type is actually implemented */
484 if (param->val.type != INT_VALUE && param->val.type != REAL_VALUE
485 && param->val.type != STR_VALUE && param->val.type != POS_VALUE)
487 GMX_THROW(gmx::InternalError("Variable-count value type not implemented"));
490 /* Reserve appropriate amount of memory */
491 if (param->val.type == POS_VALUE)
493 gmx_ana_pos_reserve(param->val.u.p, valueCount, 0);
494 gmx_ana_pos_set_nr(param->val.u.p, valueCount);
495 gmx_ana_indexmap_init(¶m->val.u.p->m, NULL, NULL, INDEX_UNKNOWN);
499 _gmx_selvalue_reserve(¶m->val, valueCount);
503 SelectionParserValueList::const_iterator value;
504 for (value = values.begin(); value != values.end(); ++value)
506 if (value->hasExpressionValue())
508 _gmx_selparser_error(scanner, "expressions not supported within value lists");
511 GMX_RELEASE_ASSERT(value->type == param->val.type,
512 "Invalid value type (should have been caught earlier)");
513 switch (param->val.type)
516 if (value->u.i.i1 <= value->u.i.i2)
518 for (j = value->u.i.i1; j <= value->u.i.i2; ++j)
520 param->val.u.i[i++] = j;
525 for (j = value->u.i.i1; j >= value->u.i.i2; --j)
527 param->val.u.i[i++] = j;
532 if (value->u.r.r1 != value->u.r.r2)
534 _gmx_selparser_error(scanner, "real ranges not supported");
537 param->val.u.r[i++] = value->u.r.r1;
540 param->val.u.s[i++] = strdup(value->stringValue().c_str());
542 case POS_VALUE: copy_rvec(value->u.x, param->val.u.p->x[i++]); break;
543 default: /* Should not be reached */
544 GMX_THROW(gmx::InternalError("Variable-count value type not implemented"));
550 *param->nvalptr = param->val.nr;
552 param->nvalptr = NULL;
553 /* Create a dummy child element to store the string values.
554 * This element is responsible for freeing the values, but carries no
556 if (param->val.type == STR_VALUE)
558 SelectionTreeElementPointer child(new SelectionTreeElement(SEL_CONST));
559 _gmx_selelem_set_vtype(child, STR_VALUE);
560 child->setName(param->name);
561 child->flags &= ~SEL_ALLOCVAL;
562 child->flags |= SEL_FLAGSSET | SEL_VARNUMVAL | SEL_ALLOCDATA;
563 child->v.nr = param->val.nr;
564 _gmx_selvalue_setstore(&child->v, param->val.u.s);
565 /* Because the child is not group-valued, the u union is not used
566 * for anything, so we can abuse it by storing the parameter value
567 * as place_child() expects, but this is really ugly... */
568 child->u.param = param;
569 place_child(root, child, param);
576 * Adds a new subexpression reference to a selection element.
578 * \param[in,out] root Root element to which the subexpression is added.
579 * \param[in] param Parameter for which this expression is a value.
580 * \param[in] expr Expression to add.
581 * \param[in] scanner Scanner data structure.
582 * \returns The created child element.
584 * Creates a new \ref SEL_SUBEXPRREF element and adds it into the child
586 * If \p expr is already a \ref SEL_SUBEXPRREF, it is used as it is.
587 * \ref SEL_ALLOCVAL is cleared for the returned element.
589 static SelectionTreeElementPointer
590 add_child(const SelectionTreeElementPointer &root, gmx_ana_selparam_t *param,
591 const SelectionTreeElementPointer &expr, void *scanner)
593 GMX_RELEASE_ASSERT(root->type == SEL_EXPRESSION || root->type == SEL_MODIFIER,
594 "Unsupported root element for selection parameter parser");
595 SelectionTreeElementPointer child;
596 /* Create a subexpression reference element if necessary */
597 if (expr->type == SEL_SUBEXPRREF)
603 child.reset(new SelectionTreeElement(SEL_SUBEXPRREF));
604 _gmx_selelem_set_vtype(child, expr->v.type);
607 /* Setup the child element */
608 child->flags &= ~SEL_ALLOCVAL;
609 child->u.param = param;
610 if (child->v.type != param->val.type)
612 _gmx_selparser_error(scanner, "invalid expression value");
613 // FIXME: Use exceptions.
614 return SelectionTreeElementPointer();
616 _gmx_selelem_update_flags(child, scanner);
617 if ((child->flags & SEL_DYNAMIC) && !(param->flags & SPAR_DYNAMIC))
619 _gmx_selparser_error(scanner, "dynamic values not supported");
620 // FIXME: Use exceptions.
621 return SelectionTreeElementPointer();
623 if (!(child->flags & SEL_DYNAMIC))
625 param->flags &= ~SPAR_DYNAMIC;
627 /* Put the child element in the correct place */
628 place_child(root, child, param);
633 * Parses an expression value for a parameter that takes a variable number of values.
635 * \param[in] values List of values.
636 * \param param Parameter to parse.
637 * \param root Selection element to which child expressions are added.
638 * \param[in] scanner Scanner data structure.
639 * \returns true if the values were parsed successfully, false otherwise.
642 parse_values_varnum_expr(const SelectionParserValueList &values,
643 gmx_ana_selparam_t *param,
644 const SelectionTreeElementPointer &root,
647 GMX_RELEASE_ASSERT(values.size() == 1 && values.front().hasExpressionValue(),
648 "Called with an invalid type of value");
650 SelectionTreeElementPointer child
651 = add_child(root, param, values.front().expr, scanner);
657 /* Process single-valued expressions */
658 /* TODO: We should also handle SEL_SINGLEVAL expressions here */
659 if (child->v.type == POS_VALUE || child->v.type == GROUP_VALUE)
661 /* Set the value storage */
662 _gmx_selvalue_setstore(&child->v, param->val.u.ptr);
666 *param->nvalptr = param->val.nr;
668 param->nvalptr = NULL;
672 if (!(child->flags & SEL_VARNUMVAL))
674 _gmx_selparser_error(scanner, "invalid expression value");
678 child->flags |= SEL_ALLOCVAL;
680 *param->nvalptr = param->val.nr;
681 /* Rest of the initialization is done during compilation in
688 * Initializes the storage of an expression value.
690 * \param[in,out] sel Selection element that evaluates the value.
691 * \param[in] param Parameter to receive the value.
692 * \param[in] i The value of \p sel evaluates the value \p i for
694 * \param[in] scanner Scanner data structure.
696 * Initializes the data pointer of \p sel such that the result is stored
697 * as the value \p i of \p param.
698 * This function is used internally by parse_values_std().
701 set_expr_value_store(const SelectionTreeElementPointer &sel,
702 gmx_ana_selparam_t *param, int i, void *scanner)
704 if (sel->v.type != GROUP_VALUE && !(sel->flags & SEL_SINGLEVAL))
706 _gmx_selparser_error(scanner, "invalid expression value");
711 case INT_VALUE: sel->v.u.i = ¶m->val.u.i[i]; break;
712 case REAL_VALUE: sel->v.u.r = ¶m->val.u.r[i]; break;
713 case STR_VALUE: sel->v.u.s = ¶m->val.u.s[i]; break;
714 case POS_VALUE: sel->v.u.p = ¶m->val.u.p[i]; break;
715 case GROUP_VALUE: sel->v.u.g = ¶m->val.u.g[i]; break;
717 GMX_THROW(gmx::InternalError("Invalid value type"));
725 * Parses the values for a parameter that takes a constant number of values.
727 * \param[in] values List of values.
728 * \param param Parameter to parse.
729 * \param root Selection element to which child expressions are added.
730 * \param[in] scanner Scanner data structure.
731 * \returns true if the values were parsed successfully, false otherwise.
733 * For integer ranges, the sequence of numbers from the first to second value
734 * is stored, each as a separate value.
737 parse_values_std(const SelectionParserValueList &values,
738 gmx_ana_selparam_t *param,
739 const SelectionTreeElementPointer &root, void *scanner)
744 /* Handle atom-valued parameters */
745 if (param->flags & SPAR_ATOMVAL)
747 if (values.size() > 1)
749 _gmx_selparser_error(scanner, "more than one value not supported");
752 if (values.front().hasExpressionValue())
754 SelectionTreeElementPointer child
755 = add_child(root, param, values.front().expr, scanner);
760 child->flags |= SEL_ALLOCVAL;
761 if (child->v.type != GROUP_VALUE && (child->flags & SEL_ATOMVAL))
763 /* Rest of the initialization is done during compilation in
765 /* TODO: Positions are not correctly handled */
769 *param->nvalptr = -1;
773 param->flags &= ~SPAR_ATOMVAL;
779 param->nvalptr = NULL;
780 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE
781 || param->val.type == STR_VALUE)
783 _gmx_selvalue_reserve(¶m->val, 1);
785 return set_expr_value_store(child, param, 0, scanner);
787 /* If we reach here, proceed with normal parameter handling */
789 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE
790 || param->val.type == STR_VALUE)
792 _gmx_selvalue_reserve(¶m->val, 1);
794 param->flags &= ~SPAR_ATOMVAL;
795 param->flags &= ~SPAR_DYNAMIC;
800 SelectionParserValueList::const_iterator value;
801 for (value = values.begin(); value != values.end() && i < param->val.nr; ++value)
803 if (value->type != param->val.type)
805 _gmx_selparser_error(scanner, "incorrect value skipped");
808 if (value->hasExpressionValue())
810 SelectionTreeElementPointer child
811 = add_child(root, param, value->expr, scanner);
812 /* Check that the expression is valid */
817 if (!set_expr_value_store(child, param, i, scanner))
821 if (child->flags & SEL_DYNAMIC)
828 /* Value is not an expression */
832 if (value->u.i.i1 <= value->u.i.i2)
834 for (j = value->u.i.i1; j <= value->u.i.i2 && i < param->val.nr; ++j)
836 param->val.u.i[i++] = j;
838 if (j != value->u.i.i2 + 1)
840 _gmx_selparser_error(scanner, "extra values skipped");
845 for (j = value->u.i.i1; j >= value->u.i.i2 && i < param->val.nr; --j)
847 param->val.u.i[i++] = j;
849 if (j != value->u.i.i2 - 1)
851 _gmx_selparser_error(scanner, "extra values skipped");
857 if (value->u.r.r1 != value->u.r.r2)
859 _gmx_selparser_error(scanner, "real ranges not supported");
862 param->val.u.r[i] = value->u.r.r1;
865 param->val.u.s[i] = strdup(value->stringValue().c_str());
868 gmx_ana_pos_init_const(¶m->val.u.p[i], value->u.x);
872 GMX_THROW(gmx::InternalError("Invalid non-expression value type"));
877 if (value != values.end())
879 _gmx_selparser_error(scanner, "extra values");
882 if (i < param->val.nr)
884 _gmx_selparser_error(scanner, "not enough values");
889 param->flags &= ~SPAR_DYNAMIC;
893 *param->nvalptr = param->val.nr;
895 param->nvalptr = NULL;
901 * Parses the values for a boolean parameter.
903 * \param[in] name Name by which the parameter was given.
904 * \param[in] values List of values.
905 * \param param Parameter to parse.
906 * \param[in] scanner Scanner data structure.
907 * \returns true if the values were parsed successfully, false otherwise.
910 parse_values_bool(const std::string &name,
911 const SelectionParserValueList &values,
912 gmx_ana_selparam_t *param, void *scanner)
914 GMX_ASSERT(param->val.type == NO_VALUE,
915 "Boolean parser called for non-boolean parameter");
916 if (values.size() > 1 || (!values.empty() && values.front().type != INT_VALUE))
918 _gmx_selparser_error(scanner, "parameter takes only a yes/no/on/off/0/1 value");
923 /* Check if the parameter name is given with a 'no' prefix */
924 if (name.length() > 2 && name[0] == 'n' && name[1] == 'o'
925 && name.compare(2, name.length() - 2, param->name) == 0)
929 if (bSetNo && !values.empty())
931 _gmx_selparser_error(scanner, "parameter 'no%s' should not have a value",
935 if (!values.empty() && values.front().u.i.i1 == 0)
940 *param->val.u.b = bSetNo ? false : true;
945 * Parses the values for an enumeration parameter.
947 * \param[in] values List of values.
948 * \param param Parameter to parse.
949 * \param[in] scanner Scanner data structure.
950 * \returns true if the values were parsed successfully, false otherwise.
953 parse_values_enum(const SelectionParserValueList &values,
954 gmx_ana_selparam_t *param,
957 GMX_ASSERT(param->val.type == STR_VALUE,
958 "Enum parser called for non-string parameter");
959 if (values.size() != 1)
961 _gmx_selparser_error(scanner, "a single value is required");
964 const SelectionParserValue &value = values.front();
965 GMX_RELEASE_ASSERT(value.type == param->val.type,
966 "Invalid value type (should have been caught earlier)");
967 if (value.hasExpressionValue())
969 _gmx_selparser_error(scanner, "expression value for enumerated parameter not supported");
973 const std::string &svalue = value.stringValue();
976 while (param->val.u.s[i] != NULL)
978 if (gmx::startsWith(param->val.u.s[i], svalue))
980 /* Check if there is a duplicate match */
983 _gmx_selparser_error(scanner, "ambiguous value");
992 _gmx_selparser_error(scanner, "invalid value");
995 param->val.u.s[0] = param->val.u.s[match];
1000 * Replaces constant expressions with their values.
1002 * \param[in,out] values First element in the value list to process.
1005 convert_const_values(SelectionParserValueList *values)
1007 SelectionParserValueList::iterator value;
1008 for (value = values->begin(); value != values->end(); ++value)
1010 if (value->hasExpressionValue() && value->expr->v.type != GROUP_VALUE &&
1011 value->expr->type == SEL_CONST)
1013 SelectionTreeElementPointer expr = value->expr;
1014 switch (expr->v.type)
1017 *value = SelectionParserValue::createInteger(expr->v.u.i[0]);
1020 *value = SelectionParserValue::createReal(expr->v.u.r[0]);
1023 *value = SelectionParserValue::createString(expr->v.u.s[0]);
1026 *value = SelectionParserValue::createPosition(expr->v.u.p->x[0]);
1029 GMX_THROW(gmx::InternalError(
1030 "Unsupported constant expression value type"));
1037 * \param pparams List of parameters from the selection parser.
1038 * \param[in] nparam Number of parameters in \p params.
1039 * \param params Array of parameters to parse.
1040 * \param root Selection element to which child expressions are added.
1041 * \param[in] scanner Scanner data structure.
1042 * \returns true if the parameters were parsed successfully, false otherwise.
1044 * Initializes the \p params array based on the parameters in \p pparams.
1045 * See the documentation of \c gmx_ana_selparam_t for different options
1046 * available for parsing.
1048 * The list \p pparams and any associated values are freed after the parameters
1049 * have been processed, no matter is there was an error or not.
1052 _gmx_sel_parse_params(const SelectionParserParameterList &pparams,
1053 int nparam, gmx_ana_selparam_t *params,
1054 const SelectionTreeElementPointer &root, void *scanner)
1056 gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
1057 gmx_ana_selparam_t *oparam;
1061 /* Check that the value pointers of SPAR_VARNUM parameters are NULL and
1062 * that they are not NULL for other parameters */
1064 for (i = 0; i < nparam; ++i)
1066 std::string contextStr = gmx::formatString("In parameter '%s'", params[i].name);
1067 gmx::MessageStringContext context(errors, contextStr);
1068 if (params[i].val.type != POS_VALUE && (params[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
1070 if (params[i].val.u.ptr != NULL)
1072 _gmx_selparser_error(scanner, "value pointer is not NULL "
1073 "although it should be for SPAR_VARNUM "
1074 "and SPAR_ATOMVAL parameters");
1076 if ((params[i].flags & SPAR_VARNUM)
1077 && (params[i].flags & SPAR_DYNAMIC) && !params[i].nvalptr)
1079 _gmx_selparser_error(scanner, "nvalptr is NULL but both "
1080 "SPAR_VARNUM and SPAR_DYNAMIC are specified");
1086 if (params[i].val.u.ptr == NULL)
1088 _gmx_selparser_error(scanner, "value pointer is NULL");
1097 /* Parse the parameters */
1099 SelectionParserParameterList::const_iterator pparam;
1100 for (pparam = pparams.begin(); pparam != pparams.end(); ++pparam)
1102 std::string contextStr;
1103 /* Find the parameter and make some checks */
1104 if (!pparam->name().empty())
1106 contextStr = gmx::formatString("In parameter '%s'", pparam->name().c_str());
1108 oparam = gmx_ana_selparam_find(pparam->name().c_str(), nparam, params);
1112 contextStr = gmx::formatString("In value %d", i + 1);
1113 oparam = ¶ms[i];
1114 if (oparam->name != NULL)
1117 _gmx_selparser_error(scanner, "too many NULL parameters provided");
1125 _gmx_selparser_error(scanner, "all NULL parameters should appear in the beginning of the list");
1129 gmx::MessageStringContext context(errors, contextStr);
1132 _gmx_selparser_error(scanner, "unknown parameter skipped");
1136 if (oparam->val.type != NO_VALUE && pparam->values().empty())
1138 _gmx_selparser_error(scanner, "no value provided");
1142 if (oparam->flags & SPAR_SET)
1144 _gmx_selparser_error(scanner, "parameter set multiple times, extra values skipped");
1148 oparam->flags |= SPAR_SET;
1149 /* Process the values for the parameter */
1150 convert_const_values(pparam->values_.get());
1151 if (convert_values(pparam->values_.get(), oparam->val.type, scanner) != 0)
1153 _gmx_selparser_error(scanner, "invalid value");
1157 if (oparam->val.type == NO_VALUE)
1159 rc = parse_values_bool(pparam->name(), pparam->values(), oparam, scanner);
1161 else if (oparam->flags & SPAR_RANGES)
1163 rc = parse_values_range(pparam->values(), oparam, scanner);
1165 else if (oparam->flags & SPAR_VARNUM)
1167 if (pparam->values().size() == 1
1168 && pparam->values().front().hasExpressionValue())
1170 rc = parse_values_varnum_expr(pparam->values(), oparam, root, scanner);
1174 rc = parse_values_varnum(pparam->values(), oparam, root, scanner);
1177 else if (oparam->flags & SPAR_ENUMVAL)
1179 rc = parse_values_enum(pparam->values(), oparam, scanner);
1183 rc = parse_values_std(pparam->values(), oparam, root, scanner);
1190 /* Check that all required parameters are present */
1191 for (i = 0; i < nparam; ++i)
1193 if (!(params[i].flags & SPAR_OPTIONAL) && !(params[i].flags & SPAR_SET))
1195 _gmx_selparser_error(scanner, "required parameter '%s' not specified", params[i].name);