7bf73880fb52ed090b2418a05f552d5131846532
[alexxy/gromacs.git] / src / gromacs / selection / params.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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, 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.
10  *
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.
15  *
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.
20  *
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.
25  *
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.
33  *
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.
36  */
37 /*! \internal \file
38  * \brief
39  * Implements functions in selparam.h.
40  *
41  * \author Teemu Murtola <teemu.murtola@gmail.com>
42  * \ingroup module_selection
43  */
44 #include "gmxpre.h"
45
46 #include <algorithm>
47 #include <array>
48 #include <string>
49
50 #include "gromacs/math/units.h"
51 #include "gromacs/math/vec.h"
52 #include "gromacs/utility/cstringutil.h"
53 #include "gromacs/utility/exceptions.h"
54 #include "gromacs/utility/gmxassert.h"
55 #include "gromacs/utility/smalloc.h"
56 #include "gromacs/utility/stringutil.h"
57 #include "gromacs/utility/unique_cptr.h"
58
59 #include "parsetree.h"
60 #include "position.h"
61 #include "scanner.h"
62 #include "selelem.h"
63 #include "selmethod.h"
64 #include "selparam.h"
65
66 using namespace gmx;
67
68 /*!
69  * \param[in] name   Name of the parameter to search.
70  * \param[in] nparam Number of parameters in the \p param array.
71  * \param[in] param  Parameter array to search.
72  * \returns   Pointer to the parameter in the \p param
73  *   or NULL if no parameter with name \p name was found.
74  *
75  * The comparison is case-sensitive.
76  */
77 gmx_ana_selparam_t* gmx_ana_selparam_find(const char* name, int nparam, gmx_ana_selparam_t* param)
78 {
79     int i;
80
81     if (nparam == 0)
82     {
83         return nullptr;
84     }
85     /* Find the first non-null parameter */
86     i = 0;
87     while (i < nparam && param[i].name == nullptr)
88     {
89         ++i;
90     }
91     /* Process the special case of a NULL parameter */
92     if (name == nullptr)
93     {
94         return (i == 0) ? nullptr : &param[i - 1];
95     }
96     for (; i < nparam; ++i)
97     {
98         if (!strcmp(param[i].name, name))
99         {
100             return &param[i];
101         }
102         /* Check for 'no' prefix on boolean parameters */
103         if (param[i].val.type == NO_VALUE && strlen(name) > 2 && name[0] == 'n' && name[1] == 'o'
104             && !strcmp(param[i].name, name + 2))
105         {
106             return &param[i];
107         }
108     }
109     return nullptr;
110 }
111
112 /*! \brief
113  * Does a type conversion on a SelectionParserValue.
114  *
115  * \param[in,out] value    Value to convert.
116  * \param[in]     type     Type to convert to.
117  * \param[in]     errors   Errors will be reported into this as nested exceptions.
118  * \param[in]     scanner  Scanner data structure.
119  */
120 static void convert_value(SelectionParserValue* value, e_selvalue_t type, ExceptionInitializer* errors, void* scanner)
121 {
122     if (value->type == type || type == NO_VALUE)
123     {
124         return;
125     }
126     if (value->hasExpressionValue())
127     {
128         /* Conversion from atom selection to position using default
129          * reference positions. */
130         if (value->type == GROUP_VALUE && type == POS_VALUE)
131         {
132             try
133             {
134                 SelectionTreeElementPointer expr = _gmx_sel_init_position(value->expr, nullptr, scanner);
135                 *value                           = SelectionParserValue::createExpr(expr);
136             }
137             catch (UserInputError& ex)
138             {
139                 std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
140                 std::string context(formatString("In '%s'", text.c_str()));
141                 ex.prependContext(context);
142                 errors->addCurrentExceptionAsNested();
143             }
144             return;
145         }
146     }
147     else
148     {
149         /* Integers to floating point are easy */
150         if (value->type == INT_VALUE && type == REAL_VALUE)
151         {
152             *value = SelectionParserValue::createRealRange(
153                     value->u.i.i1, value->u.i.i2, value->location());
154             return;
155         }
156         /* Reals that are integer-valued can also be converted */
157         if (value->type == REAL_VALUE && type == INT_VALUE)
158         {
159             int i1 = static_cast<int>(value->u.r.r1);
160             int i2 = static_cast<int>(value->u.r.r2);
161             if (gmx_within_tol(value->u.r.r1, i1, GMX_REAL_EPS)
162                 && gmx_within_tol(value->u.r.r2, i2, GMX_REAL_EPS))
163             {
164                 *value = SelectionParserValue::createIntegerRange(i1, i2, value->location());
165                 return;
166             }
167         }
168     }
169     std::string       text(_gmx_sel_lexer_get_text(scanner, value->location()));
170     std::string       message(formatString(
171             "Expression '%s' evaluates to a type is not valid in this context", text.c_str()));
172     InvalidInputError ex(message);
173     errors->addNested(ex);
174 }
175
176 /*! \brief
177  * Does a type conversion on a list of values.
178  *
179  * \param[in,out] values   Values to convert.
180  * \param[in]     type     Type to convert to.
181  * \param[in]     scanner  Scanner data structure.
182  */
183 static void convert_values(SelectionParserValueList* values, e_selvalue_t type, void* scanner)
184 {
185     ExceptionInitializer               errors("");
186     SelectionParserValueList::iterator value;
187     for (value = values->begin(); value != values->end(); ++value)
188     {
189         convert_value(&*value, type, &errors, scanner);
190     }
191     if (errors.hasNestedExceptions())
192     {
193         GMX_THROW(InvalidInputError(errors));
194     }
195 }
196
197 /*! \brief
198  * Adds a child element for a parameter, keeping the parameter order.
199  *
200  * \param[in,out] root  Root element to which the child is added.
201  * \param[in]     child Child to add.
202  * \param[in]     param Parameter for which this child is a value.
203  *
204  * Puts \p child in the child list of \p root such that the list remains
205  * in the same order as the corresponding parameters.
206  */
207 static void place_child(const SelectionTreeElementPointer& root,
208                         const SelectionTreeElementPointer& child,
209                         gmx_ana_selparam_t*                param)
210 {
211     gmx_ana_selparam_t* ps;
212     int                 n;
213
214     ps = root->u.expr.method->param;
215     n  = param - ps;
216     /* Put the child element in the correct place */
217     if (!root->child || n < root->child->u.param - ps)
218     {
219         child->next = root->child;
220         root->child = child;
221     }
222     else
223     {
224         SelectionTreeElementPointer prev = root->child;
225         while (prev->next && prev->next->u.param - ps >= n)
226         {
227             prev = prev->next;
228         }
229         child->next = prev->next;
230         prev->next  = child;
231     }
232 }
233
234 /*! \brief
235  * Comparison function for sorting ranges.
236  *
237  * \param[in] a First range.
238  * \param[in] b Second range.
239  * \returns   return true if a < b
240  *
241  * The ranges are primarily sorted based on their starting point, and
242  * secondarily based on length (longer ranges come first).
243  */
244 template<typename T>
245 static bool cmp_range(const std::array<T, 2>& a, const std::array<T, 2>& b)
246 {
247     return a[0] < b[0] || (a[0] == b[0] && a[1] > b[1]);
248 }
249
250 /*! \brief
251  * Parses the values for a parameter that takes integer or real ranges.
252  *
253  * \param[in] values List of values.
254  * \param     param  Parameter to parse.
255  * \param[in] scanner Scanner data structure.
256  */
257 static void parse_values_range(const SelectionParserValueList& values, gmx_ana_selparam_t* param, void* scanner)
258 {
259     int i, j, n;
260
261     param->flags &= ~SPAR_DYNAMIC;
262     GMX_RELEASE_ASSERT(param->val.type == INT_VALUE || param->val.type == REAL_VALUE,
263                        "Invalid range parameter type");
264     int*        idata = nullptr;
265     real*       rdata = nullptr;
266     sfree_guard dataGuard;
267     if (param->val.type == INT_VALUE)
268     {
269         snew(idata, values.size() * 2);
270         dataGuard.reset(idata);
271     }
272     else
273     {
274         snew(rdata, values.size() * 2);
275         dataGuard.reset(rdata);
276     }
277     i = 0;
278     SelectionParserValueList::const_iterator value;
279     for (value = values.begin(); value != values.end(); ++value)
280     {
281         GMX_RELEASE_ASSERT(value->type == param->val.type,
282                            "Invalid range value type (should have been caught earlier)");
283         if (value->hasExpressionValue())
284         {
285             std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
286             std::string message(
287                     "Only simple values or 'A to B' ranges are "
288                     "supported in this context");
289             InvalidInputError ex(message);
290             ex.prependContext(formatString("Invalid expression '%s'", text.c_str()));
291             GMX_THROW(ex);
292         }
293         if (param->val.type == INT_VALUE)
294         {
295             int i1 = std::min(value->u.i.i1, value->u.i.i2);
296             int i2 = std::max(value->u.i.i1, value->u.i.i2);
297             /* Check if the new range overlaps or extends the previous one */
298             if (i > 0 && i1 <= idata[i - 1] + 1 && i2 >= idata[i - 2] - 1)
299             {
300                 idata[i - 2] = std::min(idata[i - 2], i1);
301                 idata[i - 1] = std::max(idata[i - 1], i2);
302             }
303             else
304             {
305                 idata[i++] = i1;
306                 idata[i++] = i2;
307             }
308         }
309         else
310         {
311             real r1 = std::min(value->u.r.r1, value->u.r.r2);
312             real r2 = std::max(value->u.r.r1, value->u.r.r2);
313             /* Check if the new range overlaps or extends the previous one */
314             if (i > 0 && r1 <= rdata[i - 1] && r2 >= rdata[i - 2])
315             {
316                 rdata[i - 2] = std::min(rdata[i - 2], r1);
317                 rdata[i - 1] = std::max(rdata[i - 1], r2);
318             }
319             else
320             {
321                 rdata[i++] = r1;
322                 rdata[i++] = r2;
323             }
324         }
325     }
326     n = i / 2;
327     /* Sort the ranges and merge consequent ones */
328     if (param->val.type == INT_VALUE)
329     {
330         const auto range_data = reinterpret_cast<std::array<int, 2>*>(idata);
331         sort(range_data, range_data + n, cmp_range<int>);
332         for (i = j = 2; i < 2 * n; i += 2)
333         {
334             if (idata[j - 1] + 1 >= idata[i])
335             {
336                 if (idata[i + 1] > idata[j - 1])
337                 {
338                     idata[j - 1] = idata[i + 1];
339                 }
340             }
341             else
342             {
343                 idata[j]     = idata[i];
344                 idata[j + 1] = idata[i + 1];
345                 j += 2;
346             }
347         }
348     }
349     else
350     {
351         const auto range_data = reinterpret_cast<std::array<real, 2>*>(rdata);
352         sort(range_data, range_data + n, cmp_range<real>);
353         for (i = j = 2; i < 2 * n; i += 2)
354         {
355             if (rdata[j - 1] >= rdata[i])
356             {
357                 if (rdata[i + 1] > rdata[j - 1])
358                 {
359                     rdata[j - 1] = rdata[i + 1];
360                 }
361             }
362             else
363             {
364                 rdata[j]     = rdata[i];
365                 rdata[j + 1] = rdata[i + 1];
366                 j += 2;
367             }
368         }
369     }
370     n = j / 2;
371     /* Store the values */
372     if (param->flags & SPAR_VARNUM)
373     {
374         (void)dataGuard.release();
375         param->val.nr = n;
376         if (param->val.type == INT_VALUE)
377         {
378             srenew(idata, j);
379             _gmx_selvalue_setstore_alloc(&param->val, idata, j);
380         }
381         else
382         {
383             srenew(rdata, j);
384             _gmx_selvalue_setstore_alloc(&param->val, rdata, j);
385         }
386     }
387     else
388     {
389         if (n != param->val.nr)
390         {
391             GMX_ASSERT(n == 1, "Range parameters with a fixed count > 1 do not make sense");
392             GMX_THROW(
393                     InvalidInputError("Only one value or 'A to B' range is "
394                                       "supported in this context"));
395         }
396         if (param->val.type == INT_VALUE)
397         {
398             memcpy(param->val.u.i, idata, 2 * n * sizeof(int));
399         }
400         else
401         {
402             memcpy(param->val.u.r, rdata, 2 * n * sizeof(real));
403         }
404     }
405     if (param->nvalptr)
406     {
407         *param->nvalptr = param->val.nr;
408     }
409     param->nvalptr = nullptr;
410 }
411
412 /*! \brief
413  * Parses the values for a parameter that takes a variable number of values.
414  *
415  * \param[in] values List of values.
416  * \param     param  Parameter to parse.
417  * \param     root   Selection element to which child expressions are added.
418  * \param[in] scanner Scanner data structure.
419  *
420  * For integer ranges, the sequence of numbers from the first to second value
421  * is stored, each as a separate value.
422  */
423 static void parse_values_varnum(const SelectionParserValueList&    values,
424                                 gmx_ana_selparam_t*                param,
425                                 const SelectionTreeElementPointer& root,
426                                 void*                              scanner)
427 {
428     int i, j;
429
430     param->flags &= ~SPAR_DYNAMIC;
431     /* Compute number of values, considering also integer ranges. */
432     int valueCount = ssize(values);
433     if (param->val.type == INT_VALUE)
434     {
435         SelectionParserValueList::const_iterator value;
436         for (value = values.begin(); value != values.end(); ++value)
437         {
438             if (value->type == INT_VALUE && !value->hasExpressionValue())
439             {
440                 valueCount += abs(value->u.i.i2 - value->u.i.i1);
441             }
442         }
443     }
444
445     /* Check that the value type is actually implemented */
446     if (param->val.type != INT_VALUE && param->val.type != REAL_VALUE
447         && param->val.type != STR_VALUE && param->val.type != POS_VALUE)
448     {
449         GMX_THROW(InternalError("Variable-count value type not implemented"));
450     }
451
452     /* Reserve appropriate amount of memory */
453     if (param->val.type == POS_VALUE)
454     {
455         gmx_ana_pos_reserve(param->val.u.p, valueCount, 0);
456         gmx_ana_indexmap_init(&param->val.u.p->m, nullptr, nullptr, INDEX_UNKNOWN);
457         gmx_ana_pos_set_nr(param->val.u.p, valueCount);
458     }
459     else
460     {
461         _gmx_selvalue_reserve(&param->val, valueCount);
462     }
463     /* Create a dummy child element to store the string values.
464      * This element is responsible for freeing the values, but carries no
465      * other function. */
466     if (param->val.type == STR_VALUE)
467     {
468         SelectionTreeElementPointer child(
469                 new SelectionTreeElement(SEL_CONST, SelectionLocation::createEmpty()));
470         _gmx_selelem_set_vtype(child, STR_VALUE);
471         child->setName(param->name);
472         child->flags &= ~SEL_ALLOCVAL;
473         child->flags |= SEL_FLAGSSET | SEL_VARNUMVAL | SEL_ALLOCDATA;
474         child->v.nr = valueCount;
475         _gmx_selvalue_setstore(&child->v, param->val.u.s);
476         /* Because the child is not group-valued, the u union is not used
477          * for anything, so we can abuse it by storing the parameter value
478          * as place_child() expects, but this is really ugly... */
479         child->u.param = param;
480         place_child(root, child, param);
481     }
482     param->val.nr = valueCount;
483
484     i = 0;
485     SelectionParserValueList::const_iterator value;
486     for (value = values.begin(); value != values.end(); ++value)
487     {
488         GMX_RELEASE_ASSERT(value->type == param->val.type,
489                            "Invalid value type (should have been caught earlier)");
490         if (value->hasExpressionValue())
491         {
492             std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
493             std::string message(
494                     "Selection expressions are not supported in this "
495                     "context when multiple values are provided");
496             InvalidInputError ex(message);
497             ex.prependContext(formatString("Invalid expression '%s'", text.c_str()));
498             GMX_THROW(ex);
499         }
500         switch (param->val.type)
501         {
502             case INT_VALUE:
503                 if (value->u.i.i1 <= value->u.i.i2)
504                 {
505                     for (j = value->u.i.i1; j <= value->u.i.i2; ++j)
506                     {
507                         param->val.u.i[i++] = j;
508                     }
509                 }
510                 else
511                 {
512                     for (j = value->u.i.i1; j >= value->u.i.i2; --j)
513                     {
514                         param->val.u.i[i++] = j;
515                     }
516                 }
517                 break;
518             case REAL_VALUE:
519                 if (value->u.r.r1 != value->u.r.r2)
520                 {
521                     std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
522                     std::string message = formatString(
523                             "Real range ('%s') is not supported in this context", text.c_str());
524                     InvalidInputError ex(message);
525                     GMX_THROW(ex);
526                 }
527                 param->val.u.r[i++] = value->u.r.r1;
528                 break;
529             case STR_VALUE: param->val.u.s[i++] = gmx_strdup(value->stringValue().c_str()); break;
530             case POS_VALUE: copy_rvec(value->u.x, param->val.u.p->x[i++]); break;
531             default: /* Should not be reached */
532                 GMX_RELEASE_ASSERT(false, "Variable-count value type not implemented");
533         }
534     }
535     GMX_RELEASE_ASSERT(i == valueCount, "Inconsistent value count wrt. the actual value population");
536     if (param->nvalptr)
537     {
538         *param->nvalptr = param->val.nr;
539     }
540     param->nvalptr = nullptr;
541 }
542
543 /*! \brief
544  * Adds a new subexpression reference to a selection element.
545  *
546  * \param[in,out] root  Root element to which the subexpression is added.
547  * \param[in]     param Parameter for which this expression is a value.
548  * \param[in]     expr  Expression to add.
549  * \param[in]     scanner Scanner data structure.
550  * \returns       The created child element.
551  *
552  * Creates a new \ref SEL_SUBEXPRREF element and adds it into the child
553  * list of \p root.
554  * If \p expr is already a \ref SEL_SUBEXPRREF, it is used as it is.
555  * \ref SEL_ALLOCVAL is cleared for the returned element.
556  */
557 static SelectionTreeElementPointer add_child(const SelectionTreeElementPointer& root,
558                                              gmx_ana_selparam_t*                param,
559                                              const SelectionTreeElementPointer& expr,
560                                              void*                              scanner)
561 {
562     GMX_RELEASE_ASSERT(root->type == SEL_EXPRESSION || root->type == SEL_MODIFIER,
563                        "Unsupported root element for selection parameter parser");
564     SelectionTreeElementPointer child;
565     /* Create a subexpression reference element if necessary */
566     if (expr->type == SEL_SUBEXPRREF)
567     {
568         child = expr;
569     }
570     else
571     {
572         // TODO: Initialize such that it includes the parameter.
573         child = std::make_shared<SelectionTreeElement>(SEL_SUBEXPRREF, expr->location());
574         _gmx_selelem_set_vtype(child, expr->v.type);
575         child->child = expr;
576     }
577     /* Setup the child element */
578     child->flags &= ~SEL_ALLOCVAL;
579     child->u.param = param;
580     if (child->v.type != param->val.type)
581     {
582         // TODO: It would be nice to say what is the expected type.
583         std::string text(_gmx_sel_lexer_get_text(scanner, expr->location()));
584         std::string message = formatString(
585                 "Expression '%s' is not valid in this context "
586                 "(produces the wrong type of values)",
587                 text.c_str());
588         GMX_THROW(InvalidInputError(message));
589     }
590     _gmx_selelem_update_flags(child);
591     if ((child->flags & SEL_DYNAMIC) && !(param->flags & SPAR_DYNAMIC))
592     {
593         std::string text(_gmx_sel_lexer_get_text(scanner, expr->location()));
594         std::string message = formatString(
595                 "Expression '%s' is dynamic, which is not "
596                 "valid in this context",
597                 text.c_str());
598         GMX_THROW(InvalidInputError(message));
599     }
600     if (!(child->flags & SEL_DYNAMIC))
601     {
602         param->flags &= ~SPAR_DYNAMIC;
603     }
604     /* Put the child element in the correct place */
605     place_child(root, child, param);
606     return child;
607 }
608
609 /*! \brief
610  * Parses an expression value for a parameter that takes a variable number of values.
611  *
612  * \param[in] values List of values.
613  * \param     param  Parameter to parse.
614  * \param     root   Selection element to which child expressions are added.
615  * \param[in] scanner Scanner data structure.
616  */
617 static void parse_values_varnum_expr(const SelectionParserValueList&    values,
618                                      gmx_ana_selparam_t*                param,
619                                      const SelectionTreeElementPointer& root,
620                                      void*                              scanner)
621 {
622     GMX_RELEASE_ASSERT(values.size() == 1 && values.front().hasExpressionValue(),
623                        "Called with an invalid type of value");
624
625     SelectionTreeElementPointer child = add_child(root, param, values.front().expr, scanner);
626
627     /* Process single-valued expressions */
628     /* TODO: We should also handle SEL_SINGLEVAL expressions here */
629     if (child->v.type == POS_VALUE || child->v.type == GROUP_VALUE)
630     {
631         /* Set the value storage */
632         _gmx_selvalue_setstore(&child->v, param->val.u.ptr);
633         param->val.nr = 1;
634         if (param->nvalptr)
635         {
636             *param->nvalptr = param->val.nr;
637         }
638         param->nvalptr = nullptr;
639         return;
640     }
641
642     if (!(child->flags & SEL_VARNUMVAL))
643     {
644         std::string text(_gmx_sel_lexer_get_text(scanner, values.front().location()));
645         std::string message = formatString("Expression '%s' is invalid in this context", text.c_str());
646         GMX_THROW(InvalidInputError(message));
647     }
648
649     child->flags |= SEL_ALLOCVAL;
650     param->val.nr   = -1;
651     *param->nvalptr = param->val.nr;
652     /* Rest of the initialization is done during compilation in
653      * init_method(). */
654 }
655
656 /*! \brief
657  * Initializes the storage of an expression value.
658  *
659  * \param[in,out] sel   Selection element that evaluates the value.
660  * \param[in]     param Parameter to receive the value.
661  * \param[in]     i     The value of \p sel evaluates the value \p i for
662  *   \p param.
663  * \param[in]     scanner Scanner data structure.
664  *
665  * Initializes the data pointer of \p sel such that the result is stored
666  * as the value \p i of \p param.
667  * This function is used internally by parse_values_std().
668  */
669 static void set_expr_value_store(const SelectionTreeElementPointer& sel,
670                                  gmx_ana_selparam_t*                param,
671                                  int                                i,
672                                  void*                              scanner)
673 {
674     if (sel->v.type != GROUP_VALUE && !(sel->flags & SEL_SINGLEVAL))
675     {
676         std::string text(_gmx_sel_lexer_get_text(scanner, sel->location()));
677         std::string message = formatString("Expression '%s' is invalid in this context", text.c_str());
678         GMX_THROW(InvalidInputError(message));
679     }
680     switch (sel->v.type)
681     {
682         case INT_VALUE: sel->v.u.i = &param->val.u.i[i]; break;
683         case REAL_VALUE: sel->v.u.r = &param->val.u.r[i]; break;
684         case STR_VALUE: sel->v.u.s = &param->val.u.s[i]; break;
685         case POS_VALUE: sel->v.u.p = &param->val.u.p[i]; break;
686         case GROUP_VALUE: sel->v.u.g = &param->val.u.g[i]; break;
687         default: /* Error */ GMX_THROW(InternalError("Invalid value type"));
688     }
689     sel->v.nr     = 1;
690     sel->v.nalloc = -1;
691 }
692
693 /*! \brief
694  * Parses the values for a parameter that takes a constant number of values.
695  *
696  * \param[in] values List of values.
697  * \param     param  Parameter to parse.
698  * \param     root   Selection element to which child expressions are added.
699  * \param[in] scanner Scanner data structure.
700  *
701  * For integer ranges, the sequence of numbers from the first to second value
702  * is stored, each as a separate value.
703  */
704 static void parse_values_std(const SelectionParserValueList&    values,
705                              gmx_ana_selparam_t*                param,
706                              const SelectionTreeElementPointer& root,
707                              void*                              scanner)
708 {
709     int  i, j;
710     bool bDynamic;
711
712     /* Handle atom-valued parameters */
713     if (param->flags & SPAR_ATOMVAL)
714     {
715         if (values.size() > 1)
716         {
717             GMX_THROW(
718                     InvalidInputError("Only a single value or a single expression is "
719                                       "supported in this context"));
720         }
721         if (values.front().hasExpressionValue())
722         {
723             SelectionTreeElementPointer child = add_child(root, param, values.front().expr, scanner);
724             child->flags |= SEL_ALLOCVAL;
725             if (child->v.type != GROUP_VALUE && (child->flags & SEL_ATOMVAL))
726             {
727                 /* Rest of the initialization is done during compilation in
728                  * init_method(). */
729                 /* TODO: Positions are not correctly handled */
730                 param->val.nr = -1;
731                 if (param->nvalptr)
732                 {
733                     *param->nvalptr = -1;
734                 }
735                 return;
736             }
737             param->flags &= ~SPAR_ATOMVAL;
738             param->val.nr = 1;
739             if (param->nvalptr)
740             {
741                 *param->nvalptr = 1;
742             }
743             param->nvalptr = nullptr;
744             if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE || param->val.type == STR_VALUE)
745             {
746                 _gmx_selvalue_reserve(&param->val, 1);
747             }
748             set_expr_value_store(child, param, 0, scanner);
749             return;
750         }
751         /* If we reach here, proceed with normal parameter handling */
752         param->val.nr = 1;
753         if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE || param->val.type == STR_VALUE)
754         {
755             _gmx_selvalue_reserve(&param->val, 1);
756         }
757         param->flags &= ~SPAR_ATOMVAL;
758         param->flags &= ~SPAR_DYNAMIC;
759     }
760
761     i        = 0;
762     bDynamic = false;
763     SelectionParserValueList::const_iterator value;
764     for (value = values.begin(); value != values.end() && i < param->val.nr; ++value)
765     {
766         GMX_RELEASE_ASSERT(value->type == param->val.type,
767                            "Invalid value type (should have been caught earlier)");
768         if (value->hasExpressionValue())
769         {
770             SelectionTreeElementPointer child = add_child(root, param, value->expr, scanner);
771             set_expr_value_store(child, param, i, scanner);
772             if (child->flags & SEL_DYNAMIC)
773             {
774                 bDynamic = true;
775             }
776         }
777         else
778         {
779             /* Value is not an expression */
780             switch (value->type)
781             {
782                 case INT_VALUE:
783                 {
784                     bool bTooManyValues;
785                     if (value->u.i.i1 <= value->u.i.i2)
786                     {
787                         for (j = value->u.i.i1; j <= value->u.i.i2 && i < param->val.nr; ++j)
788                         {
789                             param->val.u.i[i++] = j;
790                         }
791                         bTooManyValues = (j != value->u.i.i2 + 1);
792                     }
793                     else
794                     {
795                         for (j = value->u.i.i1; j >= value->u.i.i2 && i < param->val.nr; --j)
796                         {
797                             param->val.u.i[i++] = j;
798                         }
799                         bTooManyValues = (j != value->u.i.i2 - 1);
800                     }
801                     if (bTooManyValues)
802                     {
803                         std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
804                         std::string message = formatString(
805                                 "Range ('%s') produces more values than is "
806                                 "accepted in this context",
807                                 text.c_str());
808                         GMX_THROW(InvalidInputError(message));
809                     }
810                     --i;
811                     break;
812                 }
813                 case REAL_VALUE:
814                     if (value->u.r.r1 != value->u.r.r2)
815                     {
816                         std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
817                         std::string message = formatString(
818                                 "Real range ('%s') is not supported in this context", text.c_str());
819                         GMX_THROW(InvalidInputError(message));
820                     }
821                     param->val.u.r[i] = value->u.r.r1;
822                     break;
823                 case STR_VALUE: param->val.u.s[i] = gmx_strdup(value->stringValue().c_str()); break;
824                 case POS_VALUE: gmx_ana_pos_init_const(&param->val.u.p[i], value->u.x); break;
825                 case NO_VALUE:
826                 case GROUP_VALUE: GMX_THROW(InternalError("Invalid non-expression value type"));
827             }
828         }
829         ++i;
830     }
831     if (value != values.end())
832     {
833         std::string message = formatString("Too many values provided, expected %d", param->val.nr);
834         GMX_THROW(InvalidInputError(message));
835     }
836     if (i < param->val.nr)
837     {
838         std::string message = formatString("Too few values provided, expected %d", param->val.nr);
839         GMX_THROW(InvalidInputError(message));
840     }
841     if (!bDynamic)
842     {
843         param->flags &= ~SPAR_DYNAMIC;
844     }
845     if (param->nvalptr)
846     {
847         *param->nvalptr = param->val.nr;
848     }
849     param->nvalptr = nullptr;
850 }
851
852 /*! \brief
853  * Parses the values for a boolean parameter.
854  *
855  * \param[in] name   Name by which the parameter was given.
856  * \param[in] values List of values.
857  * \param     param  Parameter to parse.
858  * \param[in] scanner Scanner data structure.
859  */
860 static void parse_values_bool(const std::string&              name,
861                               const SelectionParserValueList& values,
862                               gmx_ana_selparam_t*             param,
863                               void*                           scanner)
864 {
865     GMX_UNUSED_VALUE(scanner);
866     GMX_ASSERT(param->val.type == NO_VALUE, "Boolean parser called for non-boolean parameter");
867     if (values.size() > 1 || (!values.empty() && values.front().type != INT_VALUE))
868     {
869         std::string message =
870                 formatString("'%s' only accepts yes/no/on/off/0/1 (and empty) as a value", param->name);
871         GMX_THROW(InvalidInputError(message));
872     }
873
874     bool bSetNo = false;
875     /* Check if the parameter name is given with a 'no' prefix */
876     if (name.length() > 2 && name[0] == 'n' && name[1] == 'o'
877         && name.compare(2, name.length() - 2, param->name) == 0)
878     {
879         bSetNo = true;
880     }
881     if (bSetNo && !values.empty())
882     {
883         std::string message = formatString("'no%s' cannot be followed by any value", param->name);
884         GMX_THROW(InvalidInputError(message));
885     }
886     if (!values.empty() && values.front().u.i.i1 == 0)
887     {
888         bSetNo = true;
889     }
890
891     *param->val.u.b = !bSetNo;
892 }
893
894 /*! \brief
895  * Parses the values for an enumeration parameter.
896  *
897  * \param[in] values List of values.
898  * \param     param  Parameter to parse.
899  * \param[in] scanner Scanner data structure.
900  * \returns   true if the values were parsed successfully, false otherwise.
901  */
902 static void parse_values_enum(const SelectionParserValueList& values, gmx_ana_selparam_t* param, void* scanner)
903 {
904     GMX_ASSERT(param->val.type == STR_VALUE, "Enum parser called for non-string parameter");
905     if (values.size() != 1)
906     {
907         GMX_THROW(InvalidInputError("Only a single string value is supported in this context"));
908     }
909     const SelectionParserValue& value = values.front();
910     GMX_RELEASE_ASSERT(value.type == param->val.type,
911                        "Invalid value type (should have been caught earlier)");
912     if (value.hasExpressionValue())
913     {
914         std::string text(_gmx_sel_lexer_get_text(scanner, value.location()));
915         std::string message =
916                 formatString("Expression ('%s') is not supported in this context", text.c_str());
917         GMX_THROW(InvalidInputError(message));
918     }
919
920     const std::string& svalue = value.stringValue();
921     int                i      = 1;
922     int                match  = 0;
923     while (param->val.u.s[i] != nullptr)
924     {
925         if (startsWith(param->val.u.s[i], svalue))
926         {
927             /* Check if there is a duplicate match */
928             if (match > 0)
929             {
930                 std::string message = formatString("Value '%s' is ambiguous", svalue.c_str());
931                 GMX_THROW(InvalidInputError(message));
932             }
933             match = i;
934         }
935         ++i;
936     }
937     if (match == 0)
938     {
939         std::string message = formatString("Value '%s' is not recognized", svalue.c_str());
940         GMX_THROW(InvalidInputError(message));
941     }
942     param->val.u.s[0] = param->val.u.s[match];
943 }
944
945 /*! \brief
946  * Replaces constant expressions with their values.
947  *
948  * \param[in,out] values First element in the value list to process.
949  */
950 static void convert_const_values(SelectionParserValueList* values)
951 {
952     SelectionParserValueList::iterator value;
953     for (value = values->begin(); value != values->end(); ++value)
954     {
955         if (value->hasExpressionValue() && value->expr->v.type != GROUP_VALUE && value->expr->type == SEL_CONST)
956         {
957             SelectionTreeElementPointer expr     = value->expr;
958             const SelectionLocation&    location = value->location();
959             switch (expr->v.type)
960             {
961                 case INT_VALUE:
962                     *value = SelectionParserValue::createInteger(expr->v.u.i[0], location);
963                     break;
964                 case REAL_VALUE:
965                     *value = SelectionParserValue::createReal(expr->v.u.r[0], location);
966                     break;
967                 case STR_VALUE:
968                     *value = SelectionParserValue::createString(expr->v.u.s[0], location);
969                     break;
970                 case POS_VALUE:
971                     *value = SelectionParserValue::createPosition(expr->v.u.p->x[0], location);
972                     break;
973                 default: GMX_RELEASE_ASSERT(false, "Unsupported constant expression value type");
974             }
975         }
976     }
977 }
978
979 /*!
980  * \param     pparams List of parameters from the selection parser.
981  * \param[in] nparam  Number of parameters in \p params.
982  * \param     params  Array of parameters to parse.
983  * \param     root    Selection element to which child expressions are added.
984  * \param[in] scanner Scanner data structure.
985  *
986  * Initializes the \p params array based on the parameters in \p pparams.
987  * See the documentation of \c gmx_ana_selparam_t for different options
988  * available for parsing.
989  *
990  * The list \p pparams and any associated values are freed after the parameters
991  * have been processed, no matter is there was an error or not.
992  */
993 void _gmx_sel_parse_params(const gmx::SelectionParserParameterList& pparams,
994                            int                                      nparam,
995                            gmx_ana_selparam_t*                      params,
996                            const gmx::SelectionTreeElementPointer&  root,
997                            void*                                    scanner)
998 {
999     ExceptionInitializer errors("");
1000     /* Check that the value pointers of SPAR_VARNUM parameters are NULL and
1001      * that they are not NULL for other parameters */
1002     for (int i = 0; i < nparam; ++i)
1003     {
1004         if (params[i].val.type != POS_VALUE && (params[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
1005         {
1006             GMX_RELEASE_ASSERT(params[i].val.u.ptr == nullptr,
1007                                "value pointer is not NULL "
1008                                "although it should be for SPAR_VARNUM "
1009                                "and SPAR_ATOMVAL parameters");
1010             GMX_RELEASE_ASSERT(!((params[i].flags & SPAR_VARNUM) && (params[i].flags & SPAR_DYNAMIC))
1011                                        || params[i].nvalptr != nullptr,
1012                                "nvalptr is NULL but both "
1013                                "SPAR_VARNUM and SPAR_DYNAMIC are specified");
1014         }
1015         else
1016         {
1017             GMX_RELEASE_ASSERT(params[i].val.u.ptr != nullptr, "value pointer is NULL");
1018         }
1019     }
1020     /* Parse the parameters */
1021     int                                          nullParamIndex = 0;
1022     SelectionParserParameterList::const_iterator pparam;
1023     for (pparam = pparams.begin(); pparam != pparams.end(); ++pparam)
1024     {
1025         try
1026         {
1027             // Always assigned afterwards, but clang does not see that.
1028             gmx_ana_selparam_t* oparam = nullptr;
1029             /* Find the parameter and make some checks */
1030             if (!pparam->name().empty())
1031             {
1032                 nullParamIndex = -1;
1033                 oparam         = gmx_ana_selparam_find(pparam->name().c_str(), nparam, params);
1034                 GMX_RELEASE_ASSERT(oparam != nullptr, "Inconsistent selection parameter");
1035             }
1036             else if (nullParamIndex >= 0)
1037             {
1038                 oparam = &params[nullParamIndex];
1039                 if (oparam->name != nullptr)
1040                 {
1041                     std::string text(_gmx_sel_lexer_get_text(scanner, pparam->location()));
1042                     std::string message = formatString("Unexpected '%s'", text.c_str());
1043                     GMX_THROW(InvalidInputError(message));
1044                 }
1045                 ++nullParamIndex;
1046             }
1047             else
1048             {
1049                 GMX_RELEASE_ASSERT(false,
1050                                    "All NULL parameters should appear in "
1051                                    "the beginning of the list");
1052             }
1053             if (oparam->flags & SPAR_SET)
1054             {
1055                 std::string message =
1056                         formatString("'%s' appears multiple times", pparam->name().c_str());
1057                 GMX_THROW(InvalidInputError(message));
1058             }
1059             oparam->flags |= SPAR_SET;
1060             if (oparam->val.type != NO_VALUE && pparam->values().empty())
1061             {
1062                 std::string text;
1063                 if (pparam->name().empty())
1064                 {
1065                     text = root->name();
1066                 }
1067                 else
1068                 {
1069                     text = _gmx_sel_lexer_get_text(scanner, pparam->location());
1070                 }
1071                 std::string message =
1072                         formatString("'%s' should be followed by a value/expression", text.c_str());
1073                 GMX_THROW(InvalidInputError(message));
1074             }
1075             /* Process the values for the parameter */
1076             convert_const_values(pparam->values_.get());
1077             convert_values(pparam->values_.get(), oparam->val.type, scanner);
1078             if (oparam->val.type == NO_VALUE)
1079             {
1080                 parse_values_bool(pparam->name(), pparam->values(), oparam, scanner);
1081             }
1082             else if (oparam->flags & SPAR_RANGES)
1083             {
1084                 parse_values_range(pparam->values(), oparam, scanner);
1085             }
1086             else if (oparam->flags & SPAR_VARNUM)
1087             {
1088                 if (pparam->values().size() == 1 && pparam->values().front().hasExpressionValue())
1089                 {
1090                     parse_values_varnum_expr(pparam->values(), oparam, root, scanner);
1091                 }
1092                 else
1093                 {
1094                     parse_values_varnum(pparam->values(), oparam, root, scanner);
1095                 }
1096             }
1097             else if (oparam->flags & SPAR_ENUMVAL)
1098             {
1099                 parse_values_enum(pparam->values(), oparam, scanner);
1100             }
1101             else
1102             {
1103                 parse_values_std(pparam->values(), oparam, root, scanner);
1104             }
1105         }
1106         catch (UserInputError& ex)
1107         {
1108             if (!pparam->name().empty())
1109             {
1110                 std::string text(_gmx_sel_lexer_get_text(scanner, pparam->location()));
1111                 ex.prependContext(formatString("In '%s'", text.c_str()));
1112             }
1113             errors.addCurrentExceptionAsNested();
1114         }
1115     }
1116     /* Check that all required parameters are present */
1117     for (int i = 0; i < nparam; ++i)
1118     {
1119         if (!(params[i].flags & SPAR_OPTIONAL) && !(params[i].flags & SPAR_SET))
1120         {
1121             std::string message;
1122             if (params[i].name == nullptr)
1123             {
1124                 message = formatString("'%s' should be followed by a value/expression",
1125                                        root->name().c_str());
1126             }
1127             else
1128             {
1129                 message = formatString("'%s' is missing", params[i].name);
1130             }
1131             InvalidInputError ex(message);
1132             errors.addNested(ex);
1133         }
1134     }
1135     if (errors.hasNestedExceptions())
1136     {
1137         GMX_THROW(InvalidInputError(errors));
1138     }
1139 }