2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012, by the GROMACS development team, led by
5 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 * others, as listed in the AUTHORS file in the top-level source
7 * directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements functions in selparam.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_selection
45 #include "gromacs/legacyheaders/smalloc.h"
46 #include "gromacs/legacyheaders/string2.h"
47 #include "gromacs/legacyheaders/vec.h"
49 #include "gromacs/selection/position.h"
50 #include "gromacs/selection/selmethod.h"
51 #include "gromacs/selection/selparam.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"
62 using gmx::SelectionParserValue;
63 using gmx::SelectionParserValueList;
64 using gmx::SelectionParserParameter;
65 using gmx::SelectionParserParameterList;
66 using gmx::SelectionTreeElement;
67 using gmx::SelectionTreeElementPointer;
70 * \param[in] name Name of the parameter to search.
71 * \param[in] nparam Number of parameters in the \p param array.
72 * \param[in] param Parameter array to search.
73 * \returns Pointer to the parameter in the \p param
74 * or NULL if no parameter with name \p name was found.
76 * The comparison is case-sensitive.
79 gmx_ana_selparam_find(const char *name, int nparam, gmx_ana_selparam_t *param)
87 /* Find the first non-null parameter */
89 while (i < nparam && param[i].name == NULL)
93 /* Process the special case of a NULL parameter */
96 return (i == 0) ? NULL : ¶m[i-1];
98 for (; i < nparam; ++i)
100 if (!strcmp(param[i].name, name))
104 /* Check for 'no' prefix on boolean parameters */
105 if (param[i].val.type == NO_VALUE
106 && strlen(name) > 2 && name[0] == 'n' && name[1] == 'o'
107 && !strcmp(param[i].name, name+2))
116 * Does a type conversion on a SelectionParserValue.
118 * \param[in,out] value Value to convert.
119 * \param[in] type Type to convert to.
120 * \param[in] scanner Scanner data structure.
121 * \returns 0 on success, a non-zero value on error.
124 convert_value(SelectionParserValue *value, e_selvalue_t type, void *scanner)
126 if (value->type == type || type == NO_VALUE)
130 if (value->hasExpressionValue())
132 /* Conversion from atom selection to position using default
133 * reference positions. */
134 if (value->type == GROUP_VALUE && type == POS_VALUE)
136 SelectionTreeElementPointer expr =
137 _gmx_sel_init_position(value->expr, NULL, scanner);
138 // FIXME: Use exceptions
143 *value = SelectionParserValue::createExpr(expr);
150 /* Integers to floating point are easy */
151 if (value->type == INT_VALUE && type == REAL_VALUE)
153 *value = SelectionParserValue::createRealRange(value->u.i.i1,
157 /* Reals that are integer-valued can also be converted */
158 if (value->type == REAL_VALUE && type == INT_VALUE)
160 int i1 = static_cast<int>(value->u.r.r1);
161 int i2 = static_cast<int>(value->u.r.r2);
162 if (gmx_within_tol(value->u.r.r1, i1, GMX_REAL_EPS)
163 && gmx_within_tol(value->u.r.r2, i2, GMX_REAL_EPS))
165 *value = SelectionParserValue::createIntegerRange(i1, i2);
174 * Does a type conversion on a list of values.
176 * \param[in,out] values Values to convert.
177 * \param[in] type Type to convert to.
178 * \param[in] scanner Scanner data structure.
179 * \returns 0 on success, a non-zero value on error.
182 convert_values(SelectionParserValueList *values, e_selvalue_t type, void *scanner)
185 SelectionParserValueList::iterator value;
186 for (value = values->begin(); value != values->end(); ++value)
188 int rc1 = convert_value(&*value, type, scanner);
189 if (rc1 != 0 && rc == 0)
194 /* FIXME: More informative error messages */
199 * Adds a child element for a parameter, keeping the parameter order.
201 * \param[in,out] root Root element to which the child is added.
202 * \param[in] child Child to add.
203 * \param[in] param Parameter for which this child is a value.
205 * Puts \p child in the child list of \p root such that the list remains
206 * in the same order as the corresponding parameters.
209 place_child(const SelectionTreeElementPointer &root,
210 const SelectionTreeElementPointer &child,
211 gmx_ana_selparam_t *param)
213 gmx_ana_selparam_t *ps;
216 ps = root->u.expr.method->param;
218 /* Put the child element in the correct place */
219 if (!root->child || n < root->child->u.param - ps)
221 child->next = root->child;
226 SelectionTreeElementPointer prev = root->child;
227 while (prev->next && prev->next->u.param - ps >= n)
231 child->next = prev->next;
237 * Comparison function for sorting integer ranges.
239 * \param[in] a Pointer to the first range.
240 * \param[in] b Pointer to the second range.
241 * \returns -1, 0, or 1 depending on the relative order of \p a and \p b.
243 * The ranges are primarily sorted based on their starting point, and
244 * secondarily based on length (longer ranges come first).
247 cmp_int_range(const void *a, const void *b)
249 if (((int *)a)[0] < ((int *)b)[0])
253 if (((int *)a)[0] > ((int *)b)[0])
257 if (((int *)a)[1] > ((int *)b)[1])
265 * Comparison function for sorting real ranges.
267 * \param[in] a Pointer to the first range.
268 * \param[in] b Pointer to the second range.
269 * \returns -1, 0, or 1 depending on the relative order of \p a and \p b.
271 * The ranges are primarily sorted based on their starting point, and
272 * secondarily based on length (longer ranges come first).
275 cmp_real_range(const void *a, const void *b)
277 if (((real *)a)[0] < ((real *)b)[0])
281 if (((real *)a)[0] > ((real *)b)[0])
285 if (((real *)a)[1] > ((real *)b)[1])
293 * Parses the values for a parameter that takes integer or real ranges.
295 * \param[in] values List of values.
296 * \param param Parameter to parse.
297 * \param[in] scanner Scanner data structure.
298 * \returns true if the values were parsed successfully, false otherwise.
301 parse_values_range(const SelectionParserValueList &values,
302 gmx_ana_selparam_t *param, void *scanner)
308 param->flags &= ~SPAR_DYNAMIC;
309 GMX_RELEASE_ASSERT(param->val.type == INT_VALUE || param->val.type == REAL_VALUE,
310 "Invalid range parameter type");
313 if (param->val.type == INT_VALUE)
315 snew(idata, values.size()*2);
319 snew(rdata, values.size()*2);
322 SelectionParserValueList::const_iterator value;
323 for (value = values.begin(); value != values.end(); ++value)
325 if (value->hasExpressionValue())
327 _gmx_selparser_error(scanner, "expressions not supported within range parameters");
330 GMX_RELEASE_ASSERT(value->type == param->val.type,
331 "Invalid range value type (should have been caught earlier)");
332 if (param->val.type == INT_VALUE)
334 int i1 = std::min(value->u.i.i1, value->u.i.i2);
335 int i2 = std::max(value->u.i.i1, value->u.i.i2);
336 /* Check if the new range overlaps or extends the previous one */
337 if (i > 0 && i1 <= idata[i-1]+1 && i2 >= idata[i-2]-1)
339 idata[i-2] = std::min(idata[i-2], i1);
340 idata[i-1] = std::max(idata[i-1], i2);
350 real r1 = std::min(value->u.r.r1, value->u.r.r2);
351 real r2 = std::max(value->u.r.r1, value->u.r.r2);
352 /* Check if the new range overlaps or extends the previous one */
353 if (i > 0 && r1 <= rdata[i-1] && r2 >= rdata[i-2])
355 rdata[i-2] = std::min(rdata[i-2], r1);
356 rdata[i-1] = std::max(rdata[i-1], r2);
366 /* Sort the ranges and merge consequent ones */
367 if (param->val.type == INT_VALUE)
369 qsort(idata, n, 2*sizeof(int), &cmp_int_range);
370 for (i = j = 2; i < 2*n; i += 2)
372 if (idata[j-1]+1 >= idata[i])
374 if (idata[i+1] > idata[j-1])
376 idata[j-1] = idata[i+1];
382 idata[j+1] = idata[i+1];
389 qsort(rdata, n, 2*sizeof(real), &cmp_real_range);
390 for (i = j = 2; i < 2*n; i += 2)
392 if (rdata[j-1]+1 >= rdata[i])
394 if (rdata[i+1] > rdata[j-1])
396 rdata[j-1] = rdata[i+1];
402 rdata[j+1] = rdata[i+1];
408 /* Store the values */
409 if (param->flags & SPAR_VARNUM)
412 if (param->val.type == INT_VALUE)
415 _gmx_selvalue_setstore_alloc(¶m->val, idata, j);
420 _gmx_selvalue_setstore_alloc(¶m->val, rdata, j);
425 if (n != param->val.nr)
427 _gmx_selparser_error(scanner, "the value should consist of exactly one range");
432 if (param->val.type == INT_VALUE)
434 memcpy(param->val.u.i, idata, 2*n*sizeof(int));
439 memcpy(param->val.u.r, rdata, 2*n*sizeof(real));
445 *param->nvalptr = param->val.nr;
447 param->nvalptr = NULL;
453 * Parses the values for a parameter that takes a variable number of values.
455 * \param[in] values List of values.
456 * \param param Parameter to parse.
457 * \param root Selection element to which child expressions are added.
458 * \param[in] scanner Scanner data structure.
459 * \returns true if the values were parsed successfully, false otherwise.
461 * For integer ranges, the sequence of numbers from the first to second value
462 * is stored, each as a separate value.
465 parse_values_varnum(const SelectionParserValueList &values,
466 gmx_ana_selparam_t *param,
467 const SelectionTreeElementPointer &root,
472 param->flags &= ~SPAR_DYNAMIC;
473 /* Compute number of values, considering also integer ranges. */
474 size_t valueCount = values.size();
475 if (param->val.type == INT_VALUE)
477 SelectionParserValueList::const_iterator value;
478 for (value = values.begin(); value != values.end(); ++value)
480 if (value->type == INT_VALUE && !value->hasExpressionValue())
482 valueCount += abs(value->u.i.i2 - value->u.i.i1);
487 /* Check that the value type is actually implemented */
488 if (param->val.type != INT_VALUE && param->val.type != REAL_VALUE
489 && param->val.type != STR_VALUE && param->val.type != POS_VALUE)
491 GMX_THROW(gmx::InternalError("Variable-count value type not implemented"));
494 /* Reserve appropriate amount of memory */
495 if (param->val.type == POS_VALUE)
497 gmx_ana_pos_reserve(param->val.u.p, valueCount, 0);
498 gmx_ana_pos_set_nr(param->val.u.p, valueCount);
499 gmx_ana_indexmap_init(¶m->val.u.p->m, NULL, NULL, INDEX_UNKNOWN);
503 _gmx_selvalue_reserve(¶m->val, valueCount);
507 SelectionParserValueList::const_iterator value;
508 for (value = values.begin(); value != values.end(); ++value)
510 if (value->hasExpressionValue())
512 _gmx_selparser_error(scanner, "expressions not supported within value lists");
515 GMX_RELEASE_ASSERT(value->type == param->val.type,
516 "Invalid value type (should have been caught earlier)");
517 switch (param->val.type)
520 if (value->u.i.i1 <= value->u.i.i2)
522 for (j = value->u.i.i1; j <= value->u.i.i2; ++j)
524 param->val.u.i[i++] = j;
529 for (j = value->u.i.i1; j >= value->u.i.i2; --j)
531 param->val.u.i[i++] = j;
536 if (value->u.r.r1 != value->u.r.r2)
538 _gmx_selparser_error(scanner, "real ranges not supported");
541 param->val.u.r[i++] = value->u.r.r1;
544 param->val.u.s[i++] = strdup(value->stringValue().c_str());
546 case POS_VALUE: copy_rvec(value->u.x, param->val.u.p->x[i++]); break;
547 default: /* Should not be reached */
548 GMX_THROW(gmx::InternalError("Variable-count value type not implemented"));
554 *param->nvalptr = param->val.nr;
556 param->nvalptr = NULL;
557 /* Create a dummy child element to store the string values.
558 * This element is responsible for freeing the values, but carries no
560 if (param->val.type == STR_VALUE)
562 SelectionTreeElementPointer child(new SelectionTreeElement(SEL_CONST));
563 _gmx_selelem_set_vtype(child, STR_VALUE);
564 child->setName(param->name);
565 child->flags &= ~SEL_ALLOCVAL;
566 child->flags |= SEL_FLAGSSET | SEL_VARNUMVAL | SEL_ALLOCDATA;
567 child->v.nr = param->val.nr;
568 _gmx_selvalue_setstore(&child->v, param->val.u.s);
569 /* Because the child is not group-valued, the u union is not used
570 * for anything, so we can abuse it by storing the parameter value
571 * as place_child() expects, but this is really ugly... */
572 child->u.param = param;
573 place_child(root, child, param);
580 * Adds a new subexpression reference to a selection element.
582 * \param[in,out] root Root element to which the subexpression is added.
583 * \param[in] param Parameter for which this expression is a value.
584 * \param[in] expr Expression to add.
585 * \param[in] scanner Scanner data structure.
586 * \returns The created child element.
588 * Creates a new \ref SEL_SUBEXPRREF element and adds it into the child
590 * If \p expr is already a \ref SEL_SUBEXPRREF, it is used as it is.
591 * \ref SEL_ALLOCVAL is cleared for the returned element.
593 static SelectionTreeElementPointer
594 add_child(const SelectionTreeElementPointer &root, gmx_ana_selparam_t *param,
595 const SelectionTreeElementPointer &expr, void *scanner)
597 GMX_RELEASE_ASSERT(root->type == SEL_EXPRESSION || root->type == SEL_MODIFIER,
598 "Unsupported root element for selection parameter parser");
599 SelectionTreeElementPointer child;
600 /* Create a subexpression reference element if necessary */
601 if (expr->type == SEL_SUBEXPRREF)
607 child.reset(new SelectionTreeElement(SEL_SUBEXPRREF));
608 _gmx_selelem_set_vtype(child, expr->v.type);
611 /* Setup the child element */
612 child->flags &= ~SEL_ALLOCVAL;
613 child->u.param = param;
614 if (child->v.type != param->val.type)
616 _gmx_selparser_error(scanner, "invalid expression value");
617 // FIXME: Use exceptions.
618 return SelectionTreeElementPointer();
620 _gmx_selelem_update_flags(child, scanner);
621 if ((child->flags & SEL_DYNAMIC) && !(param->flags & SPAR_DYNAMIC))
623 _gmx_selparser_error(scanner, "dynamic values not supported");
624 // FIXME: Use exceptions.
625 return SelectionTreeElementPointer();
627 if (!(child->flags & SEL_DYNAMIC))
629 param->flags &= ~SPAR_DYNAMIC;
631 /* Put the child element in the correct place */
632 place_child(root, child, param);
637 * Parses an expression value for a parameter that takes a variable number of values.
639 * \param[in] values List of values.
640 * \param param Parameter to parse.
641 * \param root Selection element to which child expressions are added.
642 * \param[in] scanner Scanner data structure.
643 * \returns true if the values were parsed successfully, false otherwise.
646 parse_values_varnum_expr(const SelectionParserValueList &values,
647 gmx_ana_selparam_t *param,
648 const SelectionTreeElementPointer &root,
651 GMX_RELEASE_ASSERT(values.size() == 1 && values.front().hasExpressionValue(),
652 "Called with an invalid type of value");
654 SelectionTreeElementPointer child
655 = add_child(root, param, values.front().expr, scanner);
661 /* Process single-valued expressions */
662 /* TODO: We should also handle SEL_SINGLEVAL expressions here */
663 if (child->v.type == POS_VALUE || child->v.type == GROUP_VALUE)
665 /* Set the value storage */
666 _gmx_selvalue_setstore(&child->v, param->val.u.ptr);
670 *param->nvalptr = param->val.nr;
672 param->nvalptr = NULL;
676 if (!(child->flags & SEL_VARNUMVAL))
678 _gmx_selparser_error(scanner, "invalid expression value");
682 child->flags |= SEL_ALLOCVAL;
684 *param->nvalptr = param->val.nr;
685 /* Rest of the initialization is done during compilation in
692 * Initializes the storage of an expression value.
694 * \param[in,out] sel Selection element that evaluates the value.
695 * \param[in] param Parameter to receive the value.
696 * \param[in] i The value of \p sel evaluates the value \p i for
698 * \param[in] scanner Scanner data structure.
700 * Initializes the data pointer of \p sel such that the result is stored
701 * as the value \p i of \p param.
702 * This function is used internally by parse_values_std().
705 set_expr_value_store(const SelectionTreeElementPointer &sel,
706 gmx_ana_selparam_t *param, int i, void *scanner)
708 if (sel->v.type != GROUP_VALUE && !(sel->flags & SEL_SINGLEVAL))
710 _gmx_selparser_error(scanner, "invalid expression value");
715 case INT_VALUE: sel->v.u.i = ¶m->val.u.i[i]; break;
716 case REAL_VALUE: sel->v.u.r = ¶m->val.u.r[i]; break;
717 case STR_VALUE: sel->v.u.s = ¶m->val.u.s[i]; break;
718 case POS_VALUE: sel->v.u.p = ¶m->val.u.p[i]; break;
719 case GROUP_VALUE: sel->v.u.g = ¶m->val.u.g[i]; break;
721 GMX_THROW(gmx::InternalError("Invalid value type"));
729 * Parses the values for a parameter that takes a constant number of values.
731 * \param[in] values List of values.
732 * \param param Parameter to parse.
733 * \param root Selection element to which child expressions are added.
734 * \param[in] scanner Scanner data structure.
735 * \returns true if the values were parsed successfully, false otherwise.
737 * For integer ranges, the sequence of numbers from the first to second value
738 * is stored, each as a separate value.
741 parse_values_std(const SelectionParserValueList &values,
742 gmx_ana_selparam_t *param,
743 const SelectionTreeElementPointer &root, void *scanner)
748 /* Handle atom-valued parameters */
749 if (param->flags & SPAR_ATOMVAL)
751 if (values.size() > 1)
753 _gmx_selparser_error(scanner, "more than one value not supported");
756 if (values.front().hasExpressionValue())
758 SelectionTreeElementPointer child
759 = add_child(root, param, values.front().expr, scanner);
764 child->flags |= SEL_ALLOCVAL;
765 if (child->v.type != GROUP_VALUE && (child->flags & SEL_ATOMVAL))
767 /* Rest of the initialization is done during compilation in
769 /* TODO: Positions are not correctly handled */
773 *param->nvalptr = -1;
777 param->flags &= ~SPAR_ATOMVAL;
783 param->nvalptr = NULL;
784 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE
785 || param->val.type == STR_VALUE)
787 _gmx_selvalue_reserve(¶m->val, 1);
789 return set_expr_value_store(child, param, 0, scanner);
791 /* If we reach here, proceed with normal parameter handling */
793 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE
794 || param->val.type == STR_VALUE)
796 _gmx_selvalue_reserve(¶m->val, 1);
798 param->flags &= ~SPAR_ATOMVAL;
799 param->flags &= ~SPAR_DYNAMIC;
804 SelectionParserValueList::const_iterator value;
805 for (value = values.begin(); value != values.end() && i < param->val.nr; ++value)
807 if (value->type != param->val.type)
809 _gmx_selparser_error(scanner, "incorrect value skipped");
812 if (value->hasExpressionValue())
814 SelectionTreeElementPointer child
815 = add_child(root, param, value->expr, scanner);
816 /* Check that the expression is valid */
821 if (!set_expr_value_store(child, param, i, scanner))
825 if (child->flags & SEL_DYNAMIC)
832 /* Value is not an expression */
836 if (value->u.i.i1 <= value->u.i.i2)
838 for (j = value->u.i.i1; j <= value->u.i.i2 && i < param->val.nr; ++j)
840 param->val.u.i[i++] = j;
842 if (j != value->u.i.i2 + 1)
844 _gmx_selparser_error(scanner, "extra values skipped");
849 for (j = value->u.i.i1; j >= value->u.i.i2 && i < param->val.nr; --j)
851 param->val.u.i[i++] = j;
853 if (j != value->u.i.i2 - 1)
855 _gmx_selparser_error(scanner, "extra values skipped");
861 if (value->u.r.r1 != value->u.r.r2)
863 _gmx_selparser_error(scanner, "real ranges not supported");
866 param->val.u.r[i] = value->u.r.r1;
869 param->val.u.s[i] = strdup(value->stringValue().c_str());
872 gmx_ana_pos_init_const(¶m->val.u.p[i], value->u.x);
876 GMX_THROW(gmx::InternalError("Invalid non-expression value type"));
881 if (value != values.end())
883 _gmx_selparser_error(scanner, "extra values");
886 if (i < param->val.nr)
888 _gmx_selparser_error(scanner, "not enough values");
893 param->flags &= ~SPAR_DYNAMIC;
897 *param->nvalptr = param->val.nr;
899 param->nvalptr = NULL;
905 * Parses the values for a boolean parameter.
907 * \param[in] name Name by which the parameter was given.
908 * \param[in] values List of values.
909 * \param param Parameter to parse.
910 * \param[in] scanner Scanner data structure.
911 * \returns true if the values were parsed successfully, false otherwise.
914 parse_values_bool(const std::string &name,
915 const SelectionParserValueList &values,
916 gmx_ana_selparam_t *param, void *scanner)
918 GMX_ASSERT(param->val.type == NO_VALUE,
919 "Boolean parser called for non-boolean parameter");
920 if (values.size() > 1 || (!values.empty() && values.front().type != INT_VALUE))
922 _gmx_selparser_error(scanner, "parameter takes only a yes/no/on/off/0/1 value");
927 /* Check if the parameter name is given with a 'no' prefix */
928 if (name.length() > 2 && name[0] == 'n' && name[1] == 'o'
929 && name.compare(2, name.length() - 2, param->name) == 0)
933 if (bSetNo && !values.empty())
935 _gmx_selparser_error(scanner, "parameter 'no%s' should not have a value",
939 if (!values.empty() && values.front().u.i.i1 == 0)
944 *param->val.u.b = bSetNo ? false : true;
949 * Parses the values for an enumeration parameter.
951 * \param[in] values List of values.
952 * \param param Parameter to parse.
953 * \param[in] scanner Scanner data structure.
954 * \returns true if the values were parsed successfully, false otherwise.
957 parse_values_enum(const SelectionParserValueList &values,
958 gmx_ana_selparam_t *param,
961 GMX_ASSERT(param->val.type == STR_VALUE,
962 "Enum parser called for non-string parameter");
963 if (values.size() != 1)
965 _gmx_selparser_error(scanner, "a single value is required");
968 const SelectionParserValue &value = values.front();
969 GMX_RELEASE_ASSERT(value.type == param->val.type,
970 "Invalid value type (should have been caught earlier)");
971 if (value.hasExpressionValue())
973 _gmx_selparser_error(scanner, "expression value for enumerated parameter not supported");
977 const std::string &svalue = value.stringValue();
980 while (param->val.u.s[i] != NULL)
982 if (gmx::startsWith(param->val.u.s[i], svalue))
984 /* Check if there is a duplicate match */
987 _gmx_selparser_error(scanner, "ambiguous value");
996 _gmx_selparser_error(scanner, "invalid value");
999 param->val.u.s[0] = param->val.u.s[match];
1004 * Replaces constant expressions with their values.
1006 * \param[in,out] values First element in the value list to process.
1009 convert_const_values(SelectionParserValueList *values)
1011 SelectionParserValueList::iterator value;
1012 for (value = values->begin(); value != values->end(); ++value)
1014 if (value->hasExpressionValue() && value->expr->v.type != GROUP_VALUE &&
1015 value->expr->type == SEL_CONST)
1017 SelectionTreeElementPointer expr = value->expr;
1018 switch (expr->v.type)
1021 *value = SelectionParserValue::createInteger(expr->v.u.i[0]);
1024 *value = SelectionParserValue::createReal(expr->v.u.r[0]);
1027 *value = SelectionParserValue::createString(expr->v.u.s[0]);
1030 *value = SelectionParserValue::createPosition(expr->v.u.p->x[0]);
1033 GMX_THROW(gmx::InternalError(
1034 "Unsupported constant expression value type"));
1041 * \param pparams List of parameters from the selection parser.
1042 * \param[in] nparam Number of parameters in \p params.
1043 * \param params Array of parameters to parse.
1044 * \param root Selection element to which child expressions are added.
1045 * \param[in] scanner Scanner data structure.
1046 * \returns true if the parameters were parsed successfully, false otherwise.
1048 * Initializes the \p params array based on the parameters in \p pparams.
1049 * See the documentation of \c gmx_ana_selparam_t for different options
1050 * available for parsing.
1052 * The list \p pparams and any associated values are freed after the parameters
1053 * have been processed, no matter is there was an error or not.
1056 _gmx_sel_parse_params(const SelectionParserParameterList &pparams,
1057 int nparam, gmx_ana_selparam_t *params,
1058 const SelectionTreeElementPointer &root, void *scanner)
1060 gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
1061 gmx_ana_selparam_t *oparam;
1065 /* Check that the value pointers of SPAR_VARNUM parameters are NULL and
1066 * that they are not NULL for other parameters */
1068 for (i = 0; i < nparam; ++i)
1070 std::string contextStr = gmx::formatString("In parameter '%s'", params[i].name);
1071 gmx::MessageStringContext context(errors, contextStr);
1072 if (params[i].val.type != POS_VALUE && (params[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
1074 if (params[i].val.u.ptr != NULL)
1076 _gmx_selparser_error(scanner, "value pointer is not NULL "
1077 "although it should be for SPAR_VARNUM "
1078 "and SPAR_ATOMVAL parameters");
1080 if ((params[i].flags & SPAR_VARNUM)
1081 && (params[i].flags & SPAR_DYNAMIC) && !params[i].nvalptr)
1083 _gmx_selparser_error(scanner, "nvalptr is NULL but both "
1084 "SPAR_VARNUM and SPAR_DYNAMIC are specified");
1090 if (params[i].val.u.ptr == NULL)
1092 _gmx_selparser_error(scanner, "value pointer is NULL");
1101 /* Parse the parameters */
1103 SelectionParserParameterList::const_iterator pparam;
1104 for (pparam = pparams.begin(); pparam != pparams.end(); ++pparam)
1106 std::string contextStr;
1107 /* Find the parameter and make some checks */
1108 if (!pparam->name().empty())
1110 contextStr = gmx::formatString("In parameter '%s'", pparam->name().c_str());
1112 oparam = gmx_ana_selparam_find(pparam->name().c_str(), nparam, params);
1116 contextStr = gmx::formatString("In value %d", i + 1);
1117 oparam = ¶ms[i];
1118 if (oparam->name != NULL)
1121 _gmx_selparser_error(scanner, "too many NULL parameters provided");
1129 _gmx_selparser_error(scanner, "all NULL parameters should appear in the beginning of the list");
1133 gmx::MessageStringContext context(errors, contextStr);
1136 _gmx_selparser_error(scanner, "unknown parameter skipped");
1140 if (oparam->val.type != NO_VALUE && pparam->values().empty())
1142 _gmx_selparser_error(scanner, "no value provided");
1146 if (oparam->flags & SPAR_SET)
1148 _gmx_selparser_error(scanner, "parameter set multiple times, extra values skipped");
1152 oparam->flags |= SPAR_SET;
1153 /* Process the values for the parameter */
1154 convert_const_values(pparam->values_.get());
1155 if (convert_values(pparam->values_.get(), oparam->val.type, scanner) != 0)
1157 _gmx_selparser_error(scanner, "invalid value");
1161 if (oparam->val.type == NO_VALUE)
1163 rc = parse_values_bool(pparam->name(), pparam->values(), oparam, scanner);
1165 else if (oparam->flags & SPAR_RANGES)
1167 rc = parse_values_range(pparam->values(), oparam, scanner);
1169 else if (oparam->flags & SPAR_VARNUM)
1171 if (pparam->values().size() == 1
1172 && pparam->values().front().hasExpressionValue())
1174 rc = parse_values_varnum_expr(pparam->values(), oparam, root, scanner);
1178 rc = parse_values_varnum(pparam->values(), oparam, root, scanner);
1181 else if (oparam->flags & SPAR_ENUMVAL)
1183 rc = parse_values_enum(pparam->values(), oparam, scanner);
1187 rc = parse_values_std(pparam->values(), oparam, root, scanner);
1194 /* Check that all required parameters are present */
1195 for (i = 0; i < nparam; ++i)
1197 if (!(params[i].flags & SPAR_OPTIONAL) && !(params[i].flags & SPAR_SET))
1199 _gmx_selparser_error(scanner, "required parameter '%s' not specified", params[i].name);