2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012,2013 by the GROMACS development team.
5 * Copyright (c) 2014,2015,2016,2017,2018 by the GROMACS development team.
6 * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
39 * Implements functions in selparam.h.
41 * \author Teemu Murtola <teemu.murtola@gmail.com>
42 * \ingroup module_selection
50 #include "gromacs/math/units.h"
51 #include "gromacs/math/utilities.h"
52 #include "gromacs/math/vec.h"
53 #include "gromacs/utility/cstringutil.h"
54 #include "gromacs/utility/exceptions.h"
55 #include "gromacs/utility/gmxassert.h"
56 #include "gromacs/utility/smalloc.h"
57 #include "gromacs/utility/stringutil.h"
58 #include "gromacs/utility/unique_cptr.h"
60 #include "parsetree.h"
64 #include "selmethod.h"
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.
78 gmx_ana_selparam_t* gmx_ana_selparam_find(const char* name, int nparam, gmx_ana_selparam_t* param)
86 /* Find the first non-null parameter */
88 while (i < nparam && param[i].name == nullptr)
92 /* Process the special case of a NULL parameter */
95 return (i == 0) ? nullptr : ¶m[i - 1];
97 for (; i < nparam; ++i)
99 if (!strcmp(param[i].name, name))
103 /* Check for 'no' prefix on boolean parameters */
104 if (param[i].val.type == NO_VALUE && strlen(name) > 2 && name[0] == 'n' && name[1] == 'o'
105 && !strcmp(param[i].name, name + 2))
114 * Does a type conversion on a SelectionParserValue.
116 * \param[in,out] value Value to convert.
117 * \param[in] type Type to convert to.
118 * \param[in] errors Errors will be reported into this as nested exceptions.
119 * \param[in] scanner Scanner data structure.
121 static void convert_value(SelectionParserValue* value, e_selvalue_t type, ExceptionInitializer* errors, void* scanner)
123 if (value->type == type || type == NO_VALUE)
127 if (value->hasExpressionValue())
129 /* Conversion from atom selection to position using default
130 * reference positions. */
131 if (value->type == GROUP_VALUE && type == POS_VALUE)
135 SelectionTreeElementPointer expr = _gmx_sel_init_position(value->expr, nullptr, scanner);
136 *value = SelectionParserValue::createExpr(expr);
138 catch (UserInputError& ex)
140 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
141 std::string context(formatString("In '%s'", text.c_str()));
142 ex.prependContext(context);
143 errors->addCurrentExceptionAsNested();
150 /* Integers to floating point are easy */
151 if (value->type == INT_VALUE && type == REAL_VALUE)
153 *value = SelectionParserValue::createRealRange(
154 value->u.i.i1, value->u.i.i2, value->location());
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, value->location());
170 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
171 std::string message(formatString(
172 "Expression '%s' evaluates to a type is not valid in this context", text.c_str()));
173 InvalidInputError ex(message);
174 errors->addNested(ex);
178 * Does a type conversion on a list of values.
180 * \param[in,out] values Values to convert.
181 * \param[in] type Type to convert to.
182 * \param[in] scanner Scanner data structure.
184 static void convert_values(SelectionParserValueList* values, e_selvalue_t type, void* scanner)
186 ExceptionInitializer errors("");
187 SelectionParserValueList::iterator value;
188 for (value = values->begin(); value != values->end(); ++value)
190 convert_value(&*value, type, &errors, scanner);
192 if (errors.hasNestedExceptions())
194 GMX_THROW(InvalidInputError(errors));
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.
208 static void place_child(const SelectionTreeElementPointer& root,
209 const SelectionTreeElementPointer& child,
210 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;
225 SelectionTreeElementPointer prev = root->child;
226 while (prev->next && prev->next->u.param - ps >= n)
230 child->next = prev->next;
236 * Comparison function for sorting ranges.
238 * \param[in] a First range.
239 * \param[in] b Second range.
240 * \returns return true if a < b
242 * The ranges are primarily sorted based on their starting point, and
243 * secondarily based on length (longer ranges come first).
246 static bool cmp_range(const std::array<T, 2>& a, const std::array<T, 2>& b)
248 return a[0] < b[0] || (a[0] == b[0] && a[1] > b[1]);
252 * Parses the values for a parameter that takes integer or real ranges.
254 * \param[in] values List of values.
255 * \param param Parameter to parse.
256 * \param[in] scanner Scanner data structure.
258 static void parse_values_range(const SelectionParserValueList& values, gmx_ana_selparam_t* param, void* scanner)
262 param->flags &= ~SPAR_DYNAMIC;
263 GMX_RELEASE_ASSERT(param->val.type == INT_VALUE || param->val.type == REAL_VALUE,
264 "Invalid range parameter type");
265 int* idata = nullptr;
266 real* rdata = nullptr;
267 sfree_guard dataGuard;
268 if (param->val.type == INT_VALUE)
270 snew(idata, values.size() * 2);
271 dataGuard.reset(idata);
275 snew(rdata, values.size() * 2);
276 dataGuard.reset(rdata);
279 SelectionParserValueList::const_iterator value;
280 for (value = values.begin(); value != values.end(); ++value)
282 GMX_RELEASE_ASSERT(value->type == param->val.type,
283 "Invalid range value type (should have been caught earlier)");
284 if (value->hasExpressionValue())
286 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
288 "Only simple values or 'A to B' ranges are "
289 "supported in this context");
290 InvalidInputError ex(message);
291 ex.prependContext(formatString("Invalid expression '%s'", text.c_str()));
294 if (param->val.type == INT_VALUE)
296 int i1 = std::min(value->u.i.i1, value->u.i.i2);
297 int i2 = std::max(value->u.i.i1, value->u.i.i2);
298 /* Check if the new range overlaps or extends the previous one */
299 if (i > 0 && i1 <= idata[i - 1] + 1 && i2 >= idata[i - 2] - 1)
301 idata[i - 2] = std::min(idata[i - 2], i1);
302 idata[i - 1] = std::max(idata[i - 1], i2);
312 real r1 = std::min(value->u.r.r1, value->u.r.r2);
313 real r2 = std::max(value->u.r.r1, value->u.r.r2);
314 /* Check if the new range overlaps or extends the previous one */
315 if (i > 0 && r1 <= rdata[i - 1] && r2 >= rdata[i - 2])
317 rdata[i - 2] = std::min(rdata[i - 2], r1);
318 rdata[i - 1] = std::max(rdata[i - 1], r2);
328 /* Sort the ranges and merge consequent ones */
329 if (param->val.type == INT_VALUE)
331 auto* range_data = reinterpret_cast<std::array<int, 2>*>(idata);
332 sort(range_data, range_data + n, cmp_range<int>);
333 for (i = j = 2; i < 2 * n; i += 2)
335 if (idata[j - 1] + 1 >= idata[i])
337 if (idata[i + 1] > idata[j - 1])
339 idata[j - 1] = idata[i + 1];
345 idata[j + 1] = idata[i + 1];
352 auto* range_data = reinterpret_cast<std::array<real, 2>*>(rdata);
353 sort(range_data, range_data + n, cmp_range<real>);
354 for (i = j = 2; i < 2 * n; i += 2)
356 if (rdata[j - 1] >= rdata[i])
358 if (rdata[i + 1] > rdata[j - 1])
360 rdata[j - 1] = rdata[i + 1];
366 rdata[j + 1] = rdata[i + 1];
372 /* Store the values */
373 if (param->flags & SPAR_VARNUM)
375 (void)dataGuard.release();
377 if (param->val.type == INT_VALUE)
380 _gmx_selvalue_setstore_alloc(¶m->val, idata, j);
385 _gmx_selvalue_setstore_alloc(¶m->val, rdata, j);
390 if (n != param->val.nr)
392 GMX_ASSERT(n == 1, "Range parameters with a fixed count > 1 do not make sense");
394 InvalidInputError("Only one value or 'A to B' range is "
395 "supported in this context"));
397 if (param->val.type == INT_VALUE)
399 memcpy(param->val.u.i, idata, 2 * n * sizeof(int));
403 memcpy(param->val.u.r, rdata, 2 * n * sizeof(real));
408 *param->nvalptr = param->val.nr;
410 param->nvalptr = nullptr;
414 * Parses the values for a parameter that takes a variable number of values.
416 * \param[in] values List of values.
417 * \param param Parameter to parse.
418 * \param root Selection element to which child expressions are added.
419 * \param[in] scanner Scanner data structure.
421 * For integer ranges, the sequence of numbers from the first to second value
422 * is stored, each as a separate value.
424 static void parse_values_varnum(const SelectionParserValueList& values,
425 gmx_ana_selparam_t* param,
426 const SelectionTreeElementPointer& root,
431 param->flags &= ~SPAR_DYNAMIC;
432 /* Compute number of values, considering also integer ranges. */
433 int valueCount = ssize(values);
434 if (param->val.type == INT_VALUE)
436 SelectionParserValueList::const_iterator value;
437 for (value = values.begin(); value != values.end(); ++value)
439 if (value->type == INT_VALUE && !value->hasExpressionValue())
441 valueCount += abs(value->u.i.i2 - value->u.i.i1);
446 /* Check that the value type is actually implemented */
447 if (param->val.type != INT_VALUE && param->val.type != REAL_VALUE
448 && param->val.type != STR_VALUE && param->val.type != POS_VALUE)
450 GMX_THROW(InternalError("Variable-count value type not implemented"));
453 /* Reserve appropriate amount of memory */
454 if (param->val.type == POS_VALUE)
456 gmx_ana_pos_reserve(param->val.u.p, valueCount, 0);
457 gmx_ana_indexmap_init(¶m->val.u.p->m, nullptr, nullptr, INDEX_UNKNOWN);
458 gmx_ana_pos_set_nr(param->val.u.p, valueCount);
462 _gmx_selvalue_reserve(¶m->val, valueCount);
464 /* Create a dummy child element to store the string values.
465 * This element is responsible for freeing the values, but carries no
467 if (param->val.type == STR_VALUE)
469 SelectionTreeElementPointer child(
470 new SelectionTreeElement(SEL_CONST, SelectionLocation::createEmpty()));
471 _gmx_selelem_set_vtype(child, STR_VALUE);
472 child->setName(param->name);
473 child->flags &= ~SEL_ALLOCVAL;
474 child->flags |= SEL_FLAGSSET | SEL_VARNUMVAL | SEL_ALLOCDATA;
475 child->v.nr = valueCount;
476 _gmx_selvalue_setstore(&child->v, param->val.u.s);
477 /* Because the child is not group-valued, the u union is not used
478 * for anything, so we can abuse it by storing the parameter value
479 * as place_child() expects, but this is really ugly... */
480 child->u.param = param;
481 place_child(root, child, param);
483 param->val.nr = valueCount;
486 SelectionParserValueList::const_iterator value;
487 for (value = values.begin(); value != values.end(); ++value)
489 GMX_RELEASE_ASSERT(value->type == param->val.type,
490 "Invalid value type (should have been caught earlier)");
491 if (value->hasExpressionValue())
493 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
495 "Selection expressions are not supported in this "
496 "context when multiple values are provided");
497 InvalidInputError ex(message);
498 ex.prependContext(formatString("Invalid expression '%s'", text.c_str()));
501 switch (param->val.type)
504 if (value->u.i.i1 <= value->u.i.i2)
506 for (j = value->u.i.i1; j <= value->u.i.i2; ++j)
508 param->val.u.i[i++] = j;
513 for (j = value->u.i.i1; j >= value->u.i.i2; --j)
515 param->val.u.i[i++] = j;
520 if (value->u.r.r1 != value->u.r.r2)
522 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
523 std::string message = formatString(
524 "Real range ('%s') is not supported in this context", text.c_str());
525 InvalidInputError ex(message);
528 param->val.u.r[i++] = value->u.r.r1;
530 case STR_VALUE: param->val.u.s[i++] = gmx_strdup(value->stringValue().c_str()); break;
531 case POS_VALUE: copy_rvec(value->u.x, param->val.u.p->x[i++]); break;
532 default: /* Should not be reached */
533 GMX_RELEASE_ASSERT(false, "Variable-count value type not implemented");
536 GMX_RELEASE_ASSERT(i == valueCount, "Inconsistent value count wrt. the actual value population");
539 *param->nvalptr = param->val.nr;
541 param->nvalptr = nullptr;
545 * Adds a new subexpression reference to a selection element.
547 * \param[in,out] root Root element to which the subexpression is added.
548 * \param[in] param Parameter for which this expression is a value.
549 * \param[in] expr Expression to add.
550 * \param[in] scanner Scanner data structure.
551 * \returns The created child element.
553 * Creates a new \ref SEL_SUBEXPRREF element and adds it into the child
555 * If \p expr is already a \ref SEL_SUBEXPRREF, it is used as it is.
556 * \ref SEL_ALLOCVAL is cleared for the returned element.
558 static SelectionTreeElementPointer add_child(const SelectionTreeElementPointer& root,
559 gmx_ana_selparam_t* param,
560 const SelectionTreeElementPointer& expr,
563 GMX_RELEASE_ASSERT(root->type == SEL_EXPRESSION || root->type == SEL_MODIFIER,
564 "Unsupported root element for selection parameter parser");
565 SelectionTreeElementPointer child;
566 /* Create a subexpression reference element if necessary */
567 if (expr->type == SEL_SUBEXPRREF)
573 // TODO: Initialize such that it includes the parameter.
574 child = std::make_shared<SelectionTreeElement>(SEL_SUBEXPRREF, expr->location());
575 _gmx_selelem_set_vtype(child, expr->v.type);
578 /* Setup the child element */
579 child->flags &= ~SEL_ALLOCVAL;
580 child->u.param = param;
581 if (child->v.type != param->val.type)
583 // TODO: It would be nice to say what is the expected type.
584 std::string text(_gmx_sel_lexer_get_text(scanner, expr->location()));
585 std::string message = formatString(
586 "Expression '%s' is not valid in this context "
587 "(produces the wrong type of values)",
589 GMX_THROW(InvalidInputError(message));
591 _gmx_selelem_update_flags(child);
592 if ((child->flags & SEL_DYNAMIC) && !(param->flags & SPAR_DYNAMIC))
594 std::string text(_gmx_sel_lexer_get_text(scanner, expr->location()));
595 std::string message = formatString(
596 "Expression '%s' is dynamic, which is not "
597 "valid in this context",
599 GMX_THROW(InvalidInputError(message));
601 if (!(child->flags & SEL_DYNAMIC))
603 param->flags &= ~SPAR_DYNAMIC;
605 /* Put the child element in the correct place */
606 place_child(root, child, param);
611 * Parses an expression value for a parameter that takes a variable number of values.
613 * \param[in] values List of values.
614 * \param param Parameter to parse.
615 * \param root Selection element to which child expressions are added.
616 * \param[in] scanner Scanner data structure.
618 static void parse_values_varnum_expr(const SelectionParserValueList& values,
619 gmx_ana_selparam_t* param,
620 const SelectionTreeElementPointer& root,
623 GMX_RELEASE_ASSERT(values.size() == 1 && values.front().hasExpressionValue(),
624 "Called with an invalid type of value");
626 SelectionTreeElementPointer child = add_child(root, param, values.front().expr, scanner);
628 /* Process single-valued expressions */
629 /* TODO: We should also handle SEL_SINGLEVAL expressions here */
630 if (child->v.type == POS_VALUE || child->v.type == GROUP_VALUE)
632 /* Set the value storage */
633 _gmx_selvalue_setstore(&child->v, param->val.u.ptr);
637 *param->nvalptr = param->val.nr;
639 param->nvalptr = nullptr;
643 if (!(child->flags & SEL_VARNUMVAL))
645 std::string text(_gmx_sel_lexer_get_text(scanner, values.front().location()));
646 std::string message = formatString("Expression '%s' is invalid in this context", text.c_str());
647 GMX_THROW(InvalidInputError(message));
650 child->flags |= SEL_ALLOCVAL;
652 *param->nvalptr = param->val.nr;
653 /* Rest of the initialization is done during compilation in
658 * Initializes the storage of an expression value.
660 * \param[in,out] sel Selection element that evaluates the value.
661 * \param[in] param Parameter to receive the value.
662 * \param[in] i The value of \p sel evaluates the value \p i for
664 * \param[in] scanner Scanner data structure.
666 * Initializes the data pointer of \p sel such that the result is stored
667 * as the value \p i of \p param.
668 * This function is used internally by parse_values_std().
670 static void set_expr_value_store(const SelectionTreeElementPointer& sel,
671 gmx_ana_selparam_t* param,
675 if (sel->v.type != GROUP_VALUE && !(sel->flags & SEL_SINGLEVAL))
677 std::string text(_gmx_sel_lexer_get_text(scanner, sel->location()));
678 std::string message = formatString("Expression '%s' is invalid in this context", text.c_str());
679 GMX_THROW(InvalidInputError(message));
683 case INT_VALUE: sel->v.u.i = ¶m->val.u.i[i]; break;
684 case REAL_VALUE: sel->v.u.r = ¶m->val.u.r[i]; break;
685 case STR_VALUE: sel->v.u.s = ¶m->val.u.s[i]; break;
686 case POS_VALUE: sel->v.u.p = ¶m->val.u.p[i]; break;
687 case GROUP_VALUE: sel->v.u.g = ¶m->val.u.g[i]; break;
688 default: /* Error */ GMX_THROW(InternalError("Invalid value type"));
695 * Parses the values for a parameter that takes a constant number of values.
697 * \param[in] values List of values.
698 * \param param Parameter to parse.
699 * \param root Selection element to which child expressions are added.
700 * \param[in] scanner Scanner data structure.
702 * For integer ranges, the sequence of numbers from the first to second value
703 * is stored, each as a separate value.
705 static void parse_values_std(const SelectionParserValueList& values,
706 gmx_ana_selparam_t* param,
707 const SelectionTreeElementPointer& root,
713 /* Handle atom-valued parameters */
714 if (param->flags & SPAR_ATOMVAL)
716 if (values.size() > 1)
719 InvalidInputError("Only a single value or a single expression is "
720 "supported in this context"));
722 if (values.front().hasExpressionValue())
724 SelectionTreeElementPointer child = add_child(root, param, values.front().expr, scanner);
725 child->flags |= SEL_ALLOCVAL;
726 if (child->v.type != GROUP_VALUE && (child->flags & SEL_ATOMVAL))
728 /* Rest of the initialization is done during compilation in
730 /* TODO: Positions are not correctly handled */
734 *param->nvalptr = -1;
738 param->flags &= ~SPAR_ATOMVAL;
744 param->nvalptr = nullptr;
745 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE || param->val.type == STR_VALUE)
747 _gmx_selvalue_reserve(¶m->val, 1);
749 set_expr_value_store(child, param, 0, scanner);
752 /* If we reach here, proceed with normal parameter handling */
754 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE || param->val.type == STR_VALUE)
756 _gmx_selvalue_reserve(¶m->val, 1);
758 param->flags &= ~SPAR_ATOMVAL;
759 param->flags &= ~SPAR_DYNAMIC;
764 SelectionParserValueList::const_iterator value;
765 for (value = values.begin(); value != values.end() && i < param->val.nr; ++value)
767 GMX_RELEASE_ASSERT(value->type == param->val.type,
768 "Invalid value type (should have been caught earlier)");
769 if (value->hasExpressionValue())
771 SelectionTreeElementPointer child = add_child(root, param, value->expr, scanner);
772 set_expr_value_store(child, param, i, scanner);
773 if (child->flags & SEL_DYNAMIC)
780 /* Value is not an expression */
786 if (value->u.i.i1 <= value->u.i.i2)
788 for (j = value->u.i.i1; j <= value->u.i.i2 && i < param->val.nr; ++j)
790 param->val.u.i[i++] = j;
792 bTooManyValues = (j != value->u.i.i2 + 1);
796 for (j = value->u.i.i1; j >= value->u.i.i2 && i < param->val.nr; --j)
798 param->val.u.i[i++] = j;
800 bTooManyValues = (j != value->u.i.i2 - 1);
804 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
805 std::string message = formatString(
806 "Range ('%s') produces more values than is "
807 "accepted in this context",
809 GMX_THROW(InvalidInputError(message));
815 if (value->u.r.r1 != value->u.r.r2)
817 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
818 std::string message = formatString(
819 "Real range ('%s') is not supported in this context", text.c_str());
820 GMX_THROW(InvalidInputError(message));
822 param->val.u.r[i] = value->u.r.r1;
824 case STR_VALUE: param->val.u.s[i] = gmx_strdup(value->stringValue().c_str()); break;
825 case POS_VALUE: gmx_ana_pos_init_const(¶m->val.u.p[i], value->u.x); break;
827 case GROUP_VALUE: GMX_THROW(InternalError("Invalid non-expression value type"));
832 if (value != values.end())
834 std::string message = formatString("Too many values provided, expected %d", param->val.nr);
835 GMX_THROW(InvalidInputError(message));
837 if (i < param->val.nr)
839 std::string message = formatString("Too few values provided, expected %d", param->val.nr);
840 GMX_THROW(InvalidInputError(message));
844 param->flags &= ~SPAR_DYNAMIC;
848 *param->nvalptr = param->val.nr;
850 param->nvalptr = nullptr;
854 * Parses the values for a boolean parameter.
856 * \param[in] name Name by which the parameter was given.
857 * \param[in] values List of values.
858 * \param param Parameter to parse.
859 * \param[in] scanner Scanner data structure.
861 static void parse_values_bool(const std::string& name,
862 const SelectionParserValueList& values,
863 gmx_ana_selparam_t* param,
866 GMX_UNUSED_VALUE(scanner);
867 GMX_ASSERT(param->val.type == NO_VALUE, "Boolean parser called for non-boolean parameter");
868 if (values.size() > 1 || (!values.empty() && values.front().type != INT_VALUE))
870 std::string message =
871 formatString("'%s' only accepts yes/no/on/off/0/1 (and empty) as a value", param->name);
872 GMX_THROW(InvalidInputError(message));
876 /* Check if the parameter name is given with a 'no' prefix */
877 if (name.length() > 2 && name[0] == 'n' && name[1] == 'o'
878 && name.compare(2, name.length() - 2, param->name) == 0)
882 if (bSetNo && !values.empty())
884 std::string message = formatString("'no%s' cannot be followed by any value", param->name);
885 GMX_THROW(InvalidInputError(message));
887 if (!values.empty() && values.front().u.i.i1 == 0)
892 *param->val.u.b = !bSetNo;
896 * Parses the values for an enumeration parameter.
898 * \param[in] values List of values.
899 * \param param Parameter to parse.
900 * \param[in] scanner Scanner data structure.
901 * \returns true if the values were parsed successfully, false otherwise.
903 static void parse_values_enum(const SelectionParserValueList& values, gmx_ana_selparam_t* param, void* scanner)
905 GMX_ASSERT(param->val.type == STR_VALUE, "Enum parser called for non-string parameter");
906 if (values.size() != 1)
908 GMX_THROW(InvalidInputError("Only a single string value is supported in this context"));
910 const SelectionParserValue& value = values.front();
911 GMX_RELEASE_ASSERT(value.type == param->val.type,
912 "Invalid value type (should have been caught earlier)");
913 if (value.hasExpressionValue())
915 std::string text(_gmx_sel_lexer_get_text(scanner, value.location()));
916 std::string message =
917 formatString("Expression ('%s') is not supported in this context", text.c_str());
918 GMX_THROW(InvalidInputError(message));
921 const std::string& svalue = value.stringValue();
924 while (param->val.u.s[i] != nullptr)
926 if (startsWith(param->val.u.s[i], svalue))
928 /* Check if there is a duplicate match */
931 std::string message = formatString("Value '%s' is ambiguous", svalue.c_str());
932 GMX_THROW(InvalidInputError(message));
940 std::string message = formatString("Value '%s' is not recognized", svalue.c_str());
941 GMX_THROW(InvalidInputError(message));
943 param->val.u.s[0] = param->val.u.s[match];
947 * Replaces constant expressions with their values.
949 * \param[in,out] values First element in the value list to process.
951 static void convert_const_values(SelectionParserValueList* values)
953 SelectionParserValueList::iterator value;
954 for (value = values->begin(); value != values->end(); ++value)
956 if (value->hasExpressionValue() && value->expr->v.type != GROUP_VALUE && value->expr->type == SEL_CONST)
958 SelectionTreeElementPointer expr = value->expr;
959 const SelectionLocation& location = value->location();
960 switch (expr->v.type)
963 *value = SelectionParserValue::createInteger(expr->v.u.i[0], location);
966 *value = SelectionParserValue::createReal(expr->v.u.r[0], location);
969 *value = SelectionParserValue::createString(expr->v.u.s[0], location);
972 *value = SelectionParserValue::createPosition(expr->v.u.p->x[0], location);
974 default: GMX_RELEASE_ASSERT(false, "Unsupported constant expression value type");
981 * \param pparams List of parameters from the selection parser.
982 * \param[in] nparam Number of parameters in \p params.
983 * \param params Array of parameters to parse.
984 * \param root Selection element to which child expressions are added.
985 * \param[in] scanner Scanner data structure.
987 * Initializes the \p params array based on the parameters in \p pparams.
988 * See the documentation of \c gmx_ana_selparam_t for different options
989 * available for parsing.
991 * The list \p pparams and any associated values are freed after the parameters
992 * have been processed, no matter is there was an error or not.
994 void _gmx_sel_parse_params(const gmx::SelectionParserParameterList& pparams,
996 gmx_ana_selparam_t* params,
997 const gmx::SelectionTreeElementPointer& root,
1000 ExceptionInitializer errors("");
1001 /* Check that the value pointers of SPAR_VARNUM parameters are NULL and
1002 * that they are not NULL for other parameters */
1003 for (int i = 0; i < nparam; ++i)
1005 if (params[i].val.type != POS_VALUE && (params[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
1007 GMX_RELEASE_ASSERT(params[i].val.u.ptr == nullptr,
1008 "value pointer is not NULL "
1009 "although it should be for SPAR_VARNUM "
1010 "and SPAR_ATOMVAL parameters");
1011 GMX_RELEASE_ASSERT(!((params[i].flags & SPAR_VARNUM) && (params[i].flags & SPAR_DYNAMIC))
1012 || params[i].nvalptr != nullptr,
1013 "nvalptr is NULL but both "
1014 "SPAR_VARNUM and SPAR_DYNAMIC are specified");
1018 GMX_RELEASE_ASSERT(params[i].val.u.ptr != nullptr, "value pointer is NULL");
1021 /* Parse the parameters */
1022 int nullParamIndex = 0;
1023 SelectionParserParameterList::const_iterator pparam;
1024 for (pparam = pparams.begin(); pparam != pparams.end(); ++pparam)
1028 // Always assigned afterwards, but clang does not see that.
1029 gmx_ana_selparam_t* oparam = nullptr;
1030 /* Find the parameter and make some checks */
1031 if (!pparam->name().empty())
1033 nullParamIndex = -1;
1034 oparam = gmx_ana_selparam_find(pparam->name().c_str(), nparam, params);
1035 GMX_RELEASE_ASSERT(oparam != nullptr, "Inconsistent selection parameter");
1037 else if (nullParamIndex >= 0)
1039 oparam = ¶ms[nullParamIndex];
1040 if (oparam->name != nullptr)
1042 std::string text(_gmx_sel_lexer_get_text(scanner, pparam->location()));
1043 std::string message = formatString("Unexpected '%s'", text.c_str());
1044 GMX_THROW(InvalidInputError(message));
1050 GMX_RELEASE_ASSERT(false,
1051 "All NULL parameters should appear in "
1052 "the beginning of the list");
1054 if (oparam->flags & SPAR_SET)
1056 std::string message =
1057 formatString("'%s' appears multiple times", pparam->name().c_str());
1058 GMX_THROW(InvalidInputError(message));
1060 oparam->flags |= SPAR_SET;
1061 if (oparam->val.type != NO_VALUE && pparam->values().empty())
1064 if (pparam->name().empty())
1066 text = root->name();
1070 text = _gmx_sel_lexer_get_text(scanner, pparam->location());
1072 std::string message =
1073 formatString("'%s' should be followed by a value/expression", text.c_str());
1074 GMX_THROW(InvalidInputError(message));
1076 /* Process the values for the parameter */
1077 convert_const_values(pparam->values_.get());
1078 convert_values(pparam->values_.get(), oparam->val.type, scanner);
1079 if (oparam->val.type == NO_VALUE)
1081 parse_values_bool(pparam->name(), pparam->values(), oparam, scanner);
1083 else if (oparam->flags & SPAR_RANGES)
1085 parse_values_range(pparam->values(), oparam, scanner);
1087 else if (oparam->flags & SPAR_VARNUM)
1089 if (pparam->values().size() == 1 && pparam->values().front().hasExpressionValue())
1091 parse_values_varnum_expr(pparam->values(), oparam, root, scanner);
1095 parse_values_varnum(pparam->values(), oparam, root, scanner);
1098 else if (oparam->flags & SPAR_ENUMVAL)
1100 parse_values_enum(pparam->values(), oparam, scanner);
1104 parse_values_std(pparam->values(), oparam, root, scanner);
1107 catch (UserInputError& ex)
1109 if (!pparam->name().empty())
1111 std::string text(_gmx_sel_lexer_get_text(scanner, pparam->location()));
1112 ex.prependContext(formatString("In '%s'", text.c_str()));
1114 errors.addCurrentExceptionAsNested();
1117 /* Check that all required parameters are present */
1118 for (int i = 0; i < nparam; ++i)
1120 if (!(params[i].flags & SPAR_OPTIONAL) && !(params[i].flags & SPAR_SET))
1122 std::string message;
1123 if (params[i].name == nullptr)
1125 message = formatString("'%s' should be followed by a value/expression",
1126 root->name().c_str());
1130 message = formatString("'%s' is missing", params[i].name);
1132 InvalidInputError ex(message);
1133 errors.addNested(ex);
1136 if (errors.hasNestedExceptions())
1138 GMX_THROW(InvalidInputError(errors));