2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source 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
48 #include "gromacs/math/units.h"
49 #include "gromacs/math/vec.h"
50 #include "gromacs/utility/cstringutil.h"
51 #include "gromacs/utility/exceptions.h"
52 #include "gromacs/utility/gmxassert.h"
53 #include "gromacs/utility/smalloc.h"
54 #include "gromacs/utility/stringutil.h"
55 #include "gromacs/utility/unique_cptr.h"
57 #include "parsetree.h"
61 #include "selmethod.h"
67 * \param[in] name Name of the parameter to search.
68 * \param[in] nparam Number of parameters in the \p param array.
69 * \param[in] param Parameter array to search.
70 * \returns Pointer to the parameter in the \p param
71 * or NULL if no parameter with name \p name was found.
73 * The comparison is case-sensitive.
75 gmx_ana_selparam_t* 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 == nullptr)
89 /* Process the special case of a NULL parameter */
92 return (i == 0) ? nullptr : ¶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 && strlen(name) > 2 && name[0] == 'n' && name[1] == 'o'
102 && !strcmp(param[i].name, name + 2))
111 * Does a type conversion on a SelectionParserValue.
113 * \param[in,out] value Value to convert.
114 * \param[in] type Type to convert to.
115 * \param[in] errors Errors will be reported into this as nested exceptions.
116 * \param[in] scanner Scanner data structure.
118 static void convert_value(SelectionParserValue* value, e_selvalue_t type, ExceptionInitializer* errors, void* scanner)
120 if (value->type == type || type == NO_VALUE)
124 if (value->hasExpressionValue())
126 /* Conversion from atom selection to position using default
127 * reference positions. */
128 if (value->type == GROUP_VALUE && type == POS_VALUE)
132 SelectionTreeElementPointer expr = _gmx_sel_init_position(value->expr, nullptr, scanner);
133 *value = SelectionParserValue::createExpr(expr);
135 catch (UserInputError& ex)
137 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
138 std::string context(formatString("In '%s'", text.c_str()));
139 ex.prependContext(context);
140 errors->addCurrentExceptionAsNested();
147 /* Integers to floating point are easy */
148 if (value->type == INT_VALUE && type == REAL_VALUE)
150 *value = SelectionParserValue::createRealRange(value->u.i.i1, value->u.i.i2,
154 /* Reals that are integer-valued can also be converted */
155 if (value->type == REAL_VALUE && type == INT_VALUE)
157 int i1 = static_cast<int>(value->u.r.r1);
158 int i2 = static_cast<int>(value->u.r.r2);
159 if (gmx_within_tol(value->u.r.r1, i1, GMX_REAL_EPS)
160 && gmx_within_tol(value->u.r.r2, i2, GMX_REAL_EPS))
162 *value = SelectionParserValue::createIntegerRange(i1, i2, value->location());
167 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
168 std::string message(formatString(
169 "Expression '%s' evaluates to a type is not valid in this context", text.c_str()));
170 InvalidInputError ex(message);
171 errors->addNested(ex);
175 * Does a type conversion on a list of values.
177 * \param[in,out] values Values to convert.
178 * \param[in] type Type to convert to.
179 * \param[in] scanner Scanner data structure.
181 static void convert_values(SelectionParserValueList* values, e_selvalue_t type, void* scanner)
183 ExceptionInitializer errors("");
184 SelectionParserValueList::iterator value;
185 for (value = values->begin(); value != values->end(); ++value)
187 convert_value(&*value, type, &errors, scanner);
189 if (errors.hasNestedExceptions())
191 GMX_THROW(InvalidInputError(errors));
196 * Adds a child element for a parameter, keeping the parameter order.
198 * \param[in,out] root Root element to which the child is added.
199 * \param[in] child Child to add.
200 * \param[in] param Parameter for which this child is a value.
202 * Puts \p child in the child list of \p root such that the list remains
203 * in the same order as the corresponding parameters.
205 static void 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 ranges.
235 * \param[in] a First range.
236 * \param[in] b Second range.
237 * \returns return true if a < b
239 * The ranges are primarily sorted based on their starting point, and
240 * secondarily based on length (longer ranges come first).
243 static bool cmp_range(const std::array<T, 2>& a, const std::array<T, 2>& b)
245 return a[0] < b[0] || (a[0] == b[0] && a[1] > b[1]);
249 * Parses the values for a parameter that takes integer or real ranges.
251 * \param[in] values List of values.
252 * \param param Parameter to parse.
253 * \param[in] scanner Scanner data structure.
255 static void parse_values_range(const SelectionParserValueList& values, gmx_ana_selparam_t* param, void* scanner)
259 param->flags &= ~SPAR_DYNAMIC;
260 GMX_RELEASE_ASSERT(param->val.type == INT_VALUE || param->val.type == REAL_VALUE,
261 "Invalid range parameter type");
262 int* idata = nullptr;
263 real* rdata = nullptr;
264 sfree_guard dataGuard;
265 if (param->val.type == INT_VALUE)
267 snew(idata, values.size() * 2);
268 dataGuard.reset(idata);
272 snew(rdata, values.size() * 2);
273 dataGuard.reset(rdata);
276 SelectionParserValueList::const_iterator value;
277 for (value = values.begin(); value != values.end(); ++value)
279 GMX_RELEASE_ASSERT(value->type == param->val.type,
280 "Invalid range value type (should have been caught earlier)");
281 if (value->hasExpressionValue())
283 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
285 "Only simple values or 'A to B' ranges are "
286 "supported in this context");
287 InvalidInputError ex(message);
288 ex.prependContext(formatString("Invalid expression '%s'", text.c_str()));
291 if (param->val.type == INT_VALUE)
293 int i1 = std::min(value->u.i.i1, value->u.i.i2);
294 int i2 = std::max(value->u.i.i1, value->u.i.i2);
295 /* Check if the new range overlaps or extends the previous one */
296 if (i > 0 && i1 <= idata[i - 1] + 1 && i2 >= idata[i - 2] - 1)
298 idata[i - 2] = std::min(idata[i - 2], i1);
299 idata[i - 1] = std::max(idata[i - 1], i2);
309 real r1 = std::min(value->u.r.r1, value->u.r.r2);
310 real r2 = std::max(value->u.r.r1, value->u.r.r2);
311 /* Check if the new range overlaps or extends the previous one */
312 if (i > 0 && r1 <= rdata[i - 1] && r2 >= rdata[i - 2])
314 rdata[i - 2] = std::min(rdata[i - 2], r1);
315 rdata[i - 1] = std::max(rdata[i - 1], r2);
325 /* Sort the ranges and merge consequent ones */
326 if (param->val.type == INT_VALUE)
328 const auto range_data = reinterpret_cast<std::array<int, 2>*>(idata);
329 sort(range_data, range_data + n, cmp_range<int>);
330 for (i = j = 2; i < 2 * n; i += 2)
332 if (idata[j - 1] + 1 >= idata[i])
334 if (idata[i + 1] > idata[j - 1])
336 idata[j - 1] = idata[i + 1];
342 idata[j + 1] = idata[i + 1];
349 const auto range_data = reinterpret_cast<std::array<real, 2>*>(rdata);
350 sort(range_data, range_data + n, cmp_range<real>);
351 for (i = j = 2; i < 2 * n; i += 2)
353 if (rdata[j - 1] >= rdata[i])
355 if (rdata[i + 1] > rdata[j - 1])
357 rdata[j - 1] = rdata[i + 1];
363 rdata[j + 1] = rdata[i + 1];
369 /* Store the values */
370 if (param->flags & SPAR_VARNUM)
372 (void)dataGuard.release();
374 if (param->val.type == INT_VALUE)
377 _gmx_selvalue_setstore_alloc(¶m->val, idata, j);
382 _gmx_selvalue_setstore_alloc(¶m->val, rdata, j);
387 if (n != param->val.nr)
389 GMX_ASSERT(n == 1, "Range parameters with a fixed count > 1 do not make sense");
391 InvalidInputError("Only one value or 'A to B' range is "
392 "supported in this context"));
394 if (param->val.type == INT_VALUE)
396 memcpy(param->val.u.i, idata, 2 * n * sizeof(int));
400 memcpy(param->val.u.r, rdata, 2 * n * sizeof(real));
405 *param->nvalptr = param->val.nr;
407 param->nvalptr = nullptr;
411 * Parses the values for a parameter that takes a variable number of values.
413 * \param[in] values List of values.
414 * \param param Parameter to parse.
415 * \param root Selection element to which child expressions are added.
416 * \param[in] scanner Scanner data structure.
418 * For integer ranges, the sequence of numbers from the first to second value
419 * is stored, each as a separate value.
421 static void parse_values_varnum(const SelectionParserValueList& values,
422 gmx_ana_selparam_t* param,
423 const SelectionTreeElementPointer& root,
428 param->flags &= ~SPAR_DYNAMIC;
429 /* Compute number of values, considering also integer ranges. */
430 int valueCount = ssize(values);
431 if (param->val.type == INT_VALUE)
433 SelectionParserValueList::const_iterator value;
434 for (value = values.begin(); value != values.end(); ++value)
436 if (value->type == INT_VALUE && !value->hasExpressionValue())
438 valueCount += abs(value->u.i.i2 - value->u.i.i1);
443 /* Check that the value type is actually implemented */
444 if (param->val.type != INT_VALUE && param->val.type != REAL_VALUE
445 && param->val.type != STR_VALUE && param->val.type != POS_VALUE)
447 GMX_THROW(InternalError("Variable-count value type not implemented"));
450 /* Reserve appropriate amount of memory */
451 if (param->val.type == POS_VALUE)
453 gmx_ana_pos_reserve(param->val.u.p, valueCount, 0);
454 gmx_ana_indexmap_init(¶m->val.u.p->m, nullptr, nullptr, INDEX_UNKNOWN);
455 gmx_ana_pos_set_nr(param->val.u.p, valueCount);
459 _gmx_selvalue_reserve(¶m->val, valueCount);
461 /* Create a dummy child element to store the string values.
462 * This element is responsible for freeing the values, but carries no
464 if (param->val.type == STR_VALUE)
466 SelectionTreeElementPointer child(
467 new SelectionTreeElement(SEL_CONST, SelectionLocation::createEmpty()));
468 _gmx_selelem_set_vtype(child, STR_VALUE);
469 child->setName(param->name);
470 child->flags &= ~SEL_ALLOCVAL;
471 child->flags |= SEL_FLAGSSET | SEL_VARNUMVAL | SEL_ALLOCDATA;
472 child->v.nr = valueCount;
473 _gmx_selvalue_setstore(&child->v, param->val.u.s);
474 /* Because the child is not group-valued, the u union is not used
475 * for anything, so we can abuse it by storing the parameter value
476 * as place_child() expects, but this is really ugly... */
477 child->u.param = param;
478 place_child(root, child, param);
480 param->val.nr = valueCount;
483 SelectionParserValueList::const_iterator value;
484 for (value = values.begin(); value != values.end(); ++value)
486 GMX_RELEASE_ASSERT(value->type == param->val.type,
487 "Invalid value type (should have been caught earlier)");
488 if (value->hasExpressionValue())
490 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
492 "Selection expressions are not supported in this "
493 "context when multiple values are provided");
494 InvalidInputError ex(message);
495 ex.prependContext(formatString("Invalid expression '%s'", text.c_str()));
498 switch (param->val.type)
501 if (value->u.i.i1 <= value->u.i.i2)
503 for (j = value->u.i.i1; j <= value->u.i.i2; ++j)
505 param->val.u.i[i++] = j;
510 for (j = value->u.i.i1; j >= value->u.i.i2; --j)
512 param->val.u.i[i++] = j;
517 if (value->u.r.r1 != value->u.r.r2)
519 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
520 std::string message = formatString(
521 "Real range ('%s') is not supported in this context", text.c_str());
522 InvalidInputError ex(message);
525 param->val.u.r[i++] = value->u.r.r1;
527 case STR_VALUE: param->val.u.s[i++] = gmx_strdup(value->stringValue().c_str()); break;
528 case POS_VALUE: copy_rvec(value->u.x, param->val.u.p->x[i++]); break;
529 default: /* Should not be reached */
530 GMX_RELEASE_ASSERT(false, "Variable-count value type not implemented");
533 GMX_RELEASE_ASSERT(i == valueCount,
534 "Inconsistent value count wrt. the actual value population");
537 *param->nvalptr = param->val.nr;
539 param->nvalptr = nullptr;
543 * Adds a new subexpression reference to a selection element.
545 * \param[in,out] root Root element to which the subexpression is added.
546 * \param[in] param Parameter for which this expression is a value.
547 * \param[in] expr Expression to add.
548 * \param[in] scanner Scanner data structure.
549 * \returns The created child element.
551 * Creates a new \ref SEL_SUBEXPRREF element and adds it into the child
553 * If \p expr is already a \ref SEL_SUBEXPRREF, it is used as it is.
554 * \ref SEL_ALLOCVAL is cleared for the returned element.
556 static SelectionTreeElementPointer add_child(const SelectionTreeElementPointer& root,
557 gmx_ana_selparam_t* param,
558 const SelectionTreeElementPointer& expr,
561 GMX_RELEASE_ASSERT(root->type == SEL_EXPRESSION || root->type == SEL_MODIFIER,
562 "Unsupported root element for selection parameter parser");
563 SelectionTreeElementPointer child;
564 /* Create a subexpression reference element if necessary */
565 if (expr->type == SEL_SUBEXPRREF)
571 // TODO: Initialize such that it includes the parameter.
572 child = std::make_shared<SelectionTreeElement>(SEL_SUBEXPRREF, expr->location());
573 _gmx_selelem_set_vtype(child, expr->v.type);
576 /* Setup the child element */
577 child->flags &= ~SEL_ALLOCVAL;
578 child->u.param = param;
579 if (child->v.type != param->val.type)
581 // TODO: It would be nice to say what is the expected type.
582 std::string text(_gmx_sel_lexer_get_text(scanner, expr->location()));
583 std::string message = formatString(
584 "Expression '%s' is not valid in this context "
585 "(produces the wrong type of values)",
587 GMX_THROW(InvalidInputError(message));
589 _gmx_selelem_update_flags(child);
590 if ((child->flags & SEL_DYNAMIC) && !(param->flags & SPAR_DYNAMIC))
592 std::string text(_gmx_sel_lexer_get_text(scanner, expr->location()));
593 std::string message = formatString(
594 "Expression '%s' is dynamic, which is not "
595 "valid in this context",
597 GMX_THROW(InvalidInputError(message));
599 if (!(child->flags & SEL_DYNAMIC))
601 param->flags &= ~SPAR_DYNAMIC;
603 /* Put the child element in the correct place */
604 place_child(root, child, param);
609 * Parses an expression value for a parameter that takes a variable number of values.
611 * \param[in] values List of values.
612 * \param param Parameter to parse.
613 * \param root Selection element to which child expressions are added.
614 * \param[in] scanner Scanner data structure.
616 static void parse_values_varnum_expr(const SelectionParserValueList& values,
617 gmx_ana_selparam_t* param,
618 const SelectionTreeElementPointer& root,
621 GMX_RELEASE_ASSERT(values.size() == 1 && values.front().hasExpressionValue(),
622 "Called with an invalid type of value");
624 SelectionTreeElementPointer child = add_child(root, param, values.front().expr, scanner);
626 /* Process single-valued expressions */
627 /* TODO: We should also handle SEL_SINGLEVAL expressions here */
628 if (child->v.type == POS_VALUE || child->v.type == GROUP_VALUE)
630 /* Set the value storage */
631 _gmx_selvalue_setstore(&child->v, param->val.u.ptr);
635 *param->nvalptr = param->val.nr;
637 param->nvalptr = nullptr;
641 if (!(child->flags & SEL_VARNUMVAL))
643 std::string text(_gmx_sel_lexer_get_text(scanner, values.front().location()));
644 std::string message = formatString("Expression '%s' is invalid in this context", text.c_str());
645 GMX_THROW(InvalidInputError(message));
648 child->flags |= SEL_ALLOCVAL;
650 *param->nvalptr = param->val.nr;
651 /* Rest of the initialization is done during compilation in
656 * Initializes the storage of an expression value.
658 * \param[in,out] sel Selection element that evaluates the value.
659 * \param[in] param Parameter to receive the value.
660 * \param[in] i The value of \p sel evaluates the value \p i for
662 * \param[in] scanner Scanner data structure.
664 * Initializes the data pointer of \p sel such that the result is stored
665 * as the value \p i of \p param.
666 * This function is used internally by parse_values_std().
668 static void set_expr_value_store(const SelectionTreeElementPointer& sel,
669 gmx_ana_selparam_t* param,
673 if (sel->v.type != GROUP_VALUE && !(sel->flags & SEL_SINGLEVAL))
675 std::string text(_gmx_sel_lexer_get_text(scanner, sel->location()));
676 std::string message = formatString("Expression '%s' is invalid in this context", text.c_str());
677 GMX_THROW(InvalidInputError(message));
681 case INT_VALUE: sel->v.u.i = ¶m->val.u.i[i]; break;
682 case REAL_VALUE: sel->v.u.r = ¶m->val.u.r[i]; break;
683 case STR_VALUE: sel->v.u.s = ¶m->val.u.s[i]; break;
684 case POS_VALUE: sel->v.u.p = ¶m->val.u.p[i]; break;
685 case GROUP_VALUE: sel->v.u.g = ¶m->val.u.g[i]; break;
686 default: /* Error */ GMX_THROW(InternalError("Invalid value type"));
693 * Parses the values for a parameter that takes a constant number of values.
695 * \param[in] values List of values.
696 * \param param Parameter to parse.
697 * \param root Selection element to which child expressions are added.
698 * \param[in] scanner Scanner data structure.
700 * For integer ranges, the sequence of numbers from the first to second value
701 * is stored, each as a separate value.
703 static void parse_values_std(const SelectionParserValueList& values,
704 gmx_ana_selparam_t* param,
705 const SelectionTreeElementPointer& root,
711 /* Handle atom-valued parameters */
712 if (param->flags & SPAR_ATOMVAL)
714 if (values.size() > 1)
717 InvalidInputError("Only a single value or a single expression is "
718 "supported in this context"));
720 if (values.front().hasExpressionValue())
722 SelectionTreeElementPointer child = add_child(root, param, values.front().expr, scanner);
723 child->flags |= SEL_ALLOCVAL;
724 if (child->v.type != GROUP_VALUE && (child->flags & SEL_ATOMVAL))
726 /* Rest of the initialization is done during compilation in
728 /* TODO: Positions are not correctly handled */
732 *param->nvalptr = -1;
736 param->flags &= ~SPAR_ATOMVAL;
742 param->nvalptr = nullptr;
743 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE || param->val.type == STR_VALUE)
745 _gmx_selvalue_reserve(¶m->val, 1);
747 set_expr_value_store(child, param, 0, scanner);
750 /* If we reach here, proceed with normal parameter handling */
752 if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE || param->val.type == STR_VALUE)
754 _gmx_selvalue_reserve(¶m->val, 1);
756 param->flags &= ~SPAR_ATOMVAL;
757 param->flags &= ~SPAR_DYNAMIC;
762 SelectionParserValueList::const_iterator value;
763 for (value = values.begin(); value != values.end() && i < param->val.nr; ++value)
765 GMX_RELEASE_ASSERT(value->type == param->val.type,
766 "Invalid value type (should have been caught earlier)");
767 if (value->hasExpressionValue())
769 SelectionTreeElementPointer child = add_child(root, param, value->expr, scanner);
770 set_expr_value_store(child, param, i, scanner);
771 if (child->flags & SEL_DYNAMIC)
778 /* Value is not an expression */
784 if (value->u.i.i1 <= value->u.i.i2)
786 for (j = value->u.i.i1; j <= value->u.i.i2 && i < param->val.nr; ++j)
788 param->val.u.i[i++] = j;
790 bTooManyValues = (j != value->u.i.i2 + 1);
794 for (j = value->u.i.i1; j >= value->u.i.i2 && i < param->val.nr; --j)
796 param->val.u.i[i++] = j;
798 bTooManyValues = (j != value->u.i.i2 - 1);
802 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
803 std::string message = formatString(
804 "Range ('%s') produces more values than is "
805 "accepted in this context",
807 GMX_THROW(InvalidInputError(message));
813 if (value->u.r.r1 != value->u.r.r2)
815 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
816 std::string message = formatString(
817 "Real range ('%s') is not supported in this context", text.c_str());
818 GMX_THROW(InvalidInputError(message));
820 param->val.u.r[i] = value->u.r.r1;
822 case STR_VALUE: param->val.u.s[i] = gmx_strdup(value->stringValue().c_str()); break;
823 case POS_VALUE: gmx_ana_pos_init_const(¶m->val.u.p[i], value->u.x); break;
825 case GROUP_VALUE: GMX_THROW(InternalError("Invalid non-expression value type"));
830 if (value != values.end())
832 std::string message = formatString("Too many values provided, expected %d", param->val.nr);
833 GMX_THROW(InvalidInputError(message));
835 if (i < param->val.nr)
837 std::string message = formatString("Too few values provided, expected %d", param->val.nr);
838 GMX_THROW(InvalidInputError(message));
842 param->flags &= ~SPAR_DYNAMIC;
846 *param->nvalptr = param->val.nr;
848 param->nvalptr = nullptr;
852 * Parses the values for a boolean parameter.
854 * \param[in] name Name by which the parameter was given.
855 * \param[in] values List of values.
856 * \param param Parameter to parse.
857 * \param[in] scanner Scanner data structure.
859 static void parse_values_bool(const std::string& name,
860 const SelectionParserValueList& values,
861 gmx_ana_selparam_t* param,
864 GMX_UNUSED_VALUE(scanner);
865 GMX_ASSERT(param->val.type == NO_VALUE, "Boolean parser called for non-boolean parameter");
866 if (values.size() > 1 || (!values.empty() && values.front().type != INT_VALUE))
868 std::string message =
869 formatString("'%s' only accepts yes/no/on/off/0/1 (and empty) as a value", param->name);
870 GMX_THROW(InvalidInputError(message));
874 /* Check if the parameter name is given with a 'no' prefix */
875 if (name.length() > 2 && name[0] == 'n' && name[1] == 'o'
876 && name.compare(2, name.length() - 2, param->name) == 0)
880 if (bSetNo && !values.empty())
882 std::string message = formatString("'no%s' cannot be followed by any value", param->name);
883 GMX_THROW(InvalidInputError(message));
885 if (!values.empty() && values.front().u.i.i1 == 0)
890 *param->val.u.b = !bSetNo;
894 * Parses the values for an enumeration parameter.
896 * \param[in] values List of values.
897 * \param param Parameter to parse.
898 * \param[in] scanner Scanner data structure.
899 * \returns true if the values were parsed successfully, false otherwise.
901 static void parse_values_enum(const SelectionParserValueList& values, gmx_ana_selparam_t* param, void* scanner)
903 GMX_ASSERT(param->val.type == STR_VALUE, "Enum parser called for non-string parameter");
904 if (values.size() != 1)
906 GMX_THROW(InvalidInputError("Only a single string value is supported in this context"));
908 const SelectionParserValue& value = values.front();
909 GMX_RELEASE_ASSERT(value.type == param->val.type,
910 "Invalid value type (should have been caught earlier)");
911 if (value.hasExpressionValue())
913 std::string text(_gmx_sel_lexer_get_text(scanner, value.location()));
914 std::string message =
915 formatString("Expression ('%s') is not supported in this context", text.c_str());
916 GMX_THROW(InvalidInputError(message));
919 const std::string& svalue = value.stringValue();
922 while (param->val.u.s[i] != nullptr)
924 if (startsWith(param->val.u.s[i], svalue))
926 /* Check if there is a duplicate match */
929 std::string message = formatString("Value '%s' is ambiguous", svalue.c_str());
930 GMX_THROW(InvalidInputError(message));
938 std::string message = formatString("Value '%s' is not recognized", svalue.c_str());
939 GMX_THROW(InvalidInputError(message));
941 param->val.u.s[0] = param->val.u.s[match];
945 * Replaces constant expressions with their values.
947 * \param[in,out] values First element in the value list to process.
949 static void convert_const_values(SelectionParserValueList* values)
951 SelectionParserValueList::iterator value;
952 for (value = values->begin(); value != values->end(); ++value)
954 if (value->hasExpressionValue() && value->expr->v.type != GROUP_VALUE && value->expr->type == SEL_CONST)
956 SelectionTreeElementPointer expr = value->expr;
957 const SelectionLocation& location = value->location();
958 switch (expr->v.type)
961 *value = SelectionParserValue::createInteger(expr->v.u.i[0], location);
964 *value = SelectionParserValue::createReal(expr->v.u.r[0], location);
967 *value = SelectionParserValue::createString(expr->v.u.s[0], location);
970 *value = SelectionParserValue::createPosition(expr->v.u.p->x[0], location);
972 default: GMX_RELEASE_ASSERT(false, "Unsupported constant expression value type");
979 * \param pparams List of parameters from the selection parser.
980 * \param[in] nparam Number of parameters in \p params.
981 * \param params Array of parameters to parse.
982 * \param root Selection element to which child expressions are added.
983 * \param[in] scanner Scanner data structure.
985 * Initializes the \p params array based on the parameters in \p pparams.
986 * See the documentation of \c gmx_ana_selparam_t for different options
987 * available for parsing.
989 * The list \p pparams and any associated values are freed after the parameters
990 * have been processed, no matter is there was an error or not.
992 void _gmx_sel_parse_params(const gmx::SelectionParserParameterList& pparams,
994 gmx_ana_selparam_t* params,
995 const gmx::SelectionTreeElementPointer& root,
998 ExceptionInitializer errors("");
999 /* Check that the value pointers of SPAR_VARNUM parameters are NULL and
1000 * that they are not NULL for other parameters */
1001 for (int i = 0; i < nparam; ++i)
1003 if (params[i].val.type != POS_VALUE && (params[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
1005 GMX_RELEASE_ASSERT(params[i].val.u.ptr == nullptr,
1006 "value pointer is not NULL "
1007 "although it should be for SPAR_VARNUM "
1008 "and SPAR_ATOMVAL parameters");
1009 GMX_RELEASE_ASSERT(!((params[i].flags & SPAR_VARNUM) && (params[i].flags & SPAR_DYNAMIC))
1010 || params[i].nvalptr != nullptr,
1011 "nvalptr is NULL but both "
1012 "SPAR_VARNUM and SPAR_DYNAMIC are specified");
1016 GMX_RELEASE_ASSERT(params[i].val.u.ptr != nullptr, "value pointer is NULL");
1019 /* Parse the parameters */
1020 int nullParamIndex = 0;
1021 SelectionParserParameterList::const_iterator pparam;
1022 for (pparam = pparams.begin(); pparam != pparams.end(); ++pparam)
1026 // Always assigned afterwards, but clang does not see that.
1027 gmx_ana_selparam_t* oparam = nullptr;
1028 /* Find the parameter and make some checks */
1029 if (!pparam->name().empty())
1031 nullParamIndex = -1;
1032 oparam = gmx_ana_selparam_find(pparam->name().c_str(), nparam, params);
1033 GMX_RELEASE_ASSERT(oparam != nullptr, "Inconsistent selection parameter");
1035 else if (nullParamIndex >= 0)
1037 oparam = ¶ms[nullParamIndex];
1038 if (oparam->name != nullptr)
1040 std::string text(_gmx_sel_lexer_get_text(scanner, pparam->location()));
1041 std::string message = formatString("Unexpected '%s'", text.c_str());
1042 GMX_THROW(InvalidInputError(message));
1048 GMX_RELEASE_ASSERT(false,
1049 "All NULL parameters should appear in "
1050 "the beginning of the list");
1052 if (oparam->flags & SPAR_SET)
1054 std::string message =
1055 formatString("'%s' appears multiple times", pparam->name().c_str());
1056 GMX_THROW(InvalidInputError(message));
1058 oparam->flags |= SPAR_SET;
1059 if (oparam->val.type != NO_VALUE && pparam->values().empty())
1062 if (pparam->name().empty())
1064 text = root->name();
1068 text = _gmx_sel_lexer_get_text(scanner, pparam->location());
1070 std::string message =
1071 formatString("'%s' should be followed by a value/expression", text.c_str());
1072 GMX_THROW(InvalidInputError(message));
1074 /* Process the values for the parameter */
1075 convert_const_values(pparam->values_.get());
1076 convert_values(pparam->values_.get(), oparam->val.type, scanner);
1077 if (oparam->val.type == NO_VALUE)
1079 parse_values_bool(pparam->name(), pparam->values(), oparam, scanner);
1081 else if (oparam->flags & SPAR_RANGES)
1083 parse_values_range(pparam->values(), oparam, scanner);
1085 else if (oparam->flags & SPAR_VARNUM)
1087 if (pparam->values().size() == 1 && pparam->values().front().hasExpressionValue())
1089 parse_values_varnum_expr(pparam->values(), oparam, root, scanner);
1093 parse_values_varnum(pparam->values(), oparam, root, scanner);
1096 else if (oparam->flags & SPAR_ENUMVAL)
1098 parse_values_enum(pparam->values(), oparam, scanner);
1102 parse_values_std(pparam->values(), oparam, root, scanner);
1105 catch (UserInputError& ex)
1107 if (!pparam->name().empty())
1109 std::string text(_gmx_sel_lexer_get_text(scanner, pparam->location()));
1110 ex.prependContext(formatString("In '%s'", text.c_str()));
1112 errors.addCurrentExceptionAsNested();
1115 /* Check that all required parameters are present */
1116 for (int i = 0; i < nparam; ++i)
1118 if (!(params[i].flags & SPAR_OPTIONAL) && !(params[i].flags & SPAR_SET))
1120 std::string message;
1121 if (params[i].name == nullptr)
1123 message = formatString("'%s' should be followed by a value/expression",
1124 root->name().c_str());
1128 message = formatString("'%s' is missing", params[i].name);
1130 InvalidInputError ex(message);
1131 errors.addNested(ex);
1134 if (errors.hasNestedExceptions())
1136 GMX_THROW(InvalidInputError(errors));