4f6e029095b7c2ff575e9be211467ca10e2f2385
[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,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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief
37  * Implements functions in selparam.h.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_selection
41  */
42 #include "gmxpre.h"
43
44 #include <algorithm>
45 #include <array>
46 #include <string>
47
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"
56
57 #include "parsetree.h"
58 #include "position.h"
59 #include "scanner.h"
60 #include "selelem.h"
61 #include "selmethod.h"
62 #include "selparam.h"
63
64 using namespace gmx;
65
66 /*!
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.
72  *
73  * The comparison is case-sensitive.
74  */
75 gmx_ana_selparam_t* gmx_ana_selparam_find(const char* name, int nparam, gmx_ana_selparam_t* param)
76 {
77     int i;
78
79     if (nparam == 0)
80     {
81         return nullptr;
82     }
83     /* Find the first non-null parameter */
84     i = 0;
85     while (i < nparam && param[i].name == nullptr)
86     {
87         ++i;
88     }
89     /* Process the special case of a NULL parameter */
90     if (name == nullptr)
91     {
92         return (i == 0) ? nullptr : &param[i - 1];
93     }
94     for (; i < nparam; ++i)
95     {
96         if (!strcmp(param[i].name, name))
97         {
98             return &param[i];
99         }
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))
103         {
104             return &param[i];
105         }
106     }
107     return nullptr;
108 }
109
110 /*! \brief
111  * Does a type conversion on a SelectionParserValue.
112  *
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.
117  */
118 static void convert_value(SelectionParserValue* value, e_selvalue_t type, ExceptionInitializer* errors, void* scanner)
119 {
120     if (value->type == type || type == NO_VALUE)
121     {
122         return;
123     }
124     if (value->hasExpressionValue())
125     {
126         /* Conversion from atom selection to position using default
127          * reference positions. */
128         if (value->type == GROUP_VALUE && type == POS_VALUE)
129         {
130             try
131             {
132                 SelectionTreeElementPointer expr = _gmx_sel_init_position(value->expr, nullptr, scanner);
133                 *value                           = SelectionParserValue::createExpr(expr);
134             }
135             catch (UserInputError& ex)
136             {
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();
141             }
142             return;
143         }
144     }
145     else
146     {
147         /* Integers to floating point are easy */
148         if (value->type == INT_VALUE && type == REAL_VALUE)
149         {
150             *value = SelectionParserValue::createRealRange(value->u.i.i1, value->u.i.i2,
151                                                            value->location());
152             return;
153         }
154         /* Reals that are integer-valued can also be converted */
155         if (value->type == REAL_VALUE && type == INT_VALUE)
156         {
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))
161             {
162                 *value = SelectionParserValue::createIntegerRange(i1, i2, value->location());
163                 return;
164             }
165         }
166     }
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);
172 }
173
174 /*! \brief
175  * Does a type conversion on a list of values.
176  *
177  * \param[in,out] values   Values to convert.
178  * \param[in]     type     Type to convert to.
179  * \param[in]     scanner  Scanner data structure.
180  */
181 static void convert_values(SelectionParserValueList* values, e_selvalue_t type, void* scanner)
182 {
183     ExceptionInitializer               errors("");
184     SelectionParserValueList::iterator value;
185     for (value = values->begin(); value != values->end(); ++value)
186     {
187         convert_value(&*value, type, &errors, scanner);
188     }
189     if (errors.hasNestedExceptions())
190     {
191         GMX_THROW(InvalidInputError(errors));
192     }
193 }
194
195 /*! \brief
196  * Adds a child element for a parameter, keeping the parameter order.
197  *
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.
201  *
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.
204  */
205 static void place_child(const SelectionTreeElementPointer& root,
206                         const SelectionTreeElementPointer& child,
207                         gmx_ana_selparam_t*                param)
208 {
209     gmx_ana_selparam_t* ps;
210     int                 n;
211
212     ps = root->u.expr.method->param;
213     n  = param - ps;
214     /* Put the child element in the correct place */
215     if (!root->child || n < root->child->u.param - ps)
216     {
217         child->next = root->child;
218         root->child = child;
219     }
220     else
221     {
222         SelectionTreeElementPointer prev = root->child;
223         while (prev->next && prev->next->u.param - ps >= n)
224         {
225             prev = prev->next;
226         }
227         child->next = prev->next;
228         prev->next  = child;
229     }
230 }
231
232 /*! \brief
233  * Comparison function for sorting ranges.
234  *
235  * \param[in] a First range.
236  * \param[in] b Second range.
237  * \returns   return true if a < b
238  *
239  * The ranges are primarily sorted based on their starting point, and
240  * secondarily based on length (longer ranges come first).
241  */
242 template<typename T>
243 static bool cmp_range(const std::array<T, 2>& a, const std::array<T, 2>& b)
244 {
245     return a[0] < b[0] || (a[0] == b[0] && a[1] > b[1]);
246 }
247
248 /*! \brief
249  * Parses the values for a parameter that takes integer or real ranges.
250  *
251  * \param[in] values List of values.
252  * \param     param  Parameter to parse.
253  * \param[in] scanner Scanner data structure.
254  */
255 static void parse_values_range(const SelectionParserValueList& values, gmx_ana_selparam_t* param, void* scanner)
256 {
257     int i, j, n;
258
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)
266     {
267         snew(idata, values.size() * 2);
268         dataGuard.reset(idata);
269     }
270     else
271     {
272         snew(rdata, values.size() * 2);
273         dataGuard.reset(rdata);
274     }
275     i = 0;
276     SelectionParserValueList::const_iterator value;
277     for (value = values.begin(); value != values.end(); ++value)
278     {
279         GMX_RELEASE_ASSERT(value->type == param->val.type,
280                            "Invalid range value type (should have been caught earlier)");
281         if (value->hasExpressionValue())
282         {
283             std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
284             std::string message(
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()));
289             GMX_THROW(ex);
290         }
291         if (param->val.type == INT_VALUE)
292         {
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)
297             {
298                 idata[i - 2] = std::min(idata[i - 2], i1);
299                 idata[i - 1] = std::max(idata[i - 1], i2);
300             }
301             else
302             {
303                 idata[i++] = i1;
304                 idata[i++] = i2;
305             }
306         }
307         else
308         {
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])
313             {
314                 rdata[i - 2] = std::min(rdata[i - 2], r1);
315                 rdata[i - 1] = std::max(rdata[i - 1], r2);
316             }
317             else
318             {
319                 rdata[i++] = r1;
320                 rdata[i++] = r2;
321             }
322         }
323     }
324     n = i / 2;
325     /* Sort the ranges and merge consequent ones */
326     if (param->val.type == INT_VALUE)
327     {
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)
331         {
332             if (idata[j - 1] + 1 >= idata[i])
333             {
334                 if (idata[i + 1] > idata[j - 1])
335                 {
336                     idata[j - 1] = idata[i + 1];
337                 }
338             }
339             else
340             {
341                 idata[j]     = idata[i];
342                 idata[j + 1] = idata[i + 1];
343                 j += 2;
344             }
345         }
346     }
347     else
348     {
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)
352         {
353             if (rdata[j - 1] >= rdata[i])
354             {
355                 if (rdata[i + 1] > rdata[j - 1])
356                 {
357                     rdata[j - 1] = rdata[i + 1];
358                 }
359             }
360             else
361             {
362                 rdata[j]     = rdata[i];
363                 rdata[j + 1] = rdata[i + 1];
364                 j += 2;
365             }
366         }
367     }
368     n = j / 2;
369     /* Store the values */
370     if (param->flags & SPAR_VARNUM)
371     {
372         (void)dataGuard.release();
373         param->val.nr = n;
374         if (param->val.type == INT_VALUE)
375         {
376             srenew(idata, j);
377             _gmx_selvalue_setstore_alloc(&param->val, idata, j);
378         }
379         else
380         {
381             srenew(rdata, j);
382             _gmx_selvalue_setstore_alloc(&param->val, rdata, j);
383         }
384     }
385     else
386     {
387         if (n != param->val.nr)
388         {
389             GMX_ASSERT(n == 1, "Range parameters with a fixed count > 1 do not make sense");
390             GMX_THROW(
391                     InvalidInputError("Only one value or 'A to B' range is "
392                                       "supported in this context"));
393         }
394         if (param->val.type == INT_VALUE)
395         {
396             memcpy(param->val.u.i, idata, 2 * n * sizeof(int));
397         }
398         else
399         {
400             memcpy(param->val.u.r, rdata, 2 * n * sizeof(real));
401         }
402     }
403     if (param->nvalptr)
404     {
405         *param->nvalptr = param->val.nr;
406     }
407     param->nvalptr = nullptr;
408 }
409
410 /*! \brief
411  * Parses the values for a parameter that takes a variable number of values.
412  *
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.
417  *
418  * For integer ranges, the sequence of numbers from the first to second value
419  * is stored, each as a separate value.
420  */
421 static void parse_values_varnum(const SelectionParserValueList&    values,
422                                 gmx_ana_selparam_t*                param,
423                                 const SelectionTreeElementPointer& root,
424                                 void*                              scanner)
425 {
426     int i, j;
427
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)
432     {
433         SelectionParserValueList::const_iterator value;
434         for (value = values.begin(); value != values.end(); ++value)
435         {
436             if (value->type == INT_VALUE && !value->hasExpressionValue())
437             {
438                 valueCount += abs(value->u.i.i2 - value->u.i.i1);
439             }
440         }
441     }
442
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)
446     {
447         GMX_THROW(InternalError("Variable-count value type not implemented"));
448     }
449
450     /* Reserve appropriate amount of memory */
451     if (param->val.type == POS_VALUE)
452     {
453         gmx_ana_pos_reserve(param->val.u.p, valueCount, 0);
454         gmx_ana_indexmap_init(&param->val.u.p->m, nullptr, nullptr, INDEX_UNKNOWN);
455         gmx_ana_pos_set_nr(param->val.u.p, valueCount);
456     }
457     else
458     {
459         _gmx_selvalue_reserve(&param->val, valueCount);
460     }
461     /* Create a dummy child element to store the string values.
462      * This element is responsible for freeing the values, but carries no
463      * other function. */
464     if (param->val.type == STR_VALUE)
465     {
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);
479     }
480     param->val.nr = valueCount;
481
482     i = 0;
483     SelectionParserValueList::const_iterator value;
484     for (value = values.begin(); value != values.end(); ++value)
485     {
486         GMX_RELEASE_ASSERT(value->type == param->val.type,
487                            "Invalid value type (should have been caught earlier)");
488         if (value->hasExpressionValue())
489         {
490             std::string text(_gmx_sel_lexer_get_text(scanner, value->location()));
491             std::string message(
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()));
496             GMX_THROW(ex);
497         }
498         switch (param->val.type)
499         {
500             case INT_VALUE:
501                 if (value->u.i.i1 <= value->u.i.i2)
502                 {
503                     for (j = value->u.i.i1; j <= value->u.i.i2; ++j)
504                     {
505                         param->val.u.i[i++] = j;
506                     }
507                 }
508                 else
509                 {
510                     for (j = value->u.i.i1; j >= value->u.i.i2; --j)
511                     {
512                         param->val.u.i[i++] = j;
513                     }
514                 }
515                 break;
516             case REAL_VALUE:
517                 if (value->u.r.r1 != value->u.r.r2)
518                 {
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);
523                     GMX_THROW(ex);
524                 }
525                 param->val.u.r[i++] = value->u.r.r1;
526                 break;
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");
531         }
532     }
533     GMX_RELEASE_ASSERT(i == valueCount,
534                        "Inconsistent value count wrt. the actual value population");
535     if (param->nvalptr)
536     {
537         *param->nvalptr = param->val.nr;
538     }
539     param->nvalptr = nullptr;
540 }
541
542 /*! \brief
543  * Adds a new subexpression reference to a selection element.
544  *
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.
550  *
551  * Creates a new \ref SEL_SUBEXPRREF element and adds it into the child
552  * list of \p root.
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.
555  */
556 static SelectionTreeElementPointer add_child(const SelectionTreeElementPointer& root,
557                                              gmx_ana_selparam_t*                param,
558                                              const SelectionTreeElementPointer& expr,
559                                              void*                              scanner)
560 {
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)
566     {
567         child = expr;
568     }
569     else
570     {
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);
574         child->child = expr;
575     }
576     /* Setup the child element */
577     child->flags &= ~SEL_ALLOCVAL;
578     child->u.param = param;
579     if (child->v.type != param->val.type)
580     {
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)",
586                 text.c_str());
587         GMX_THROW(InvalidInputError(message));
588     }
589     _gmx_selelem_update_flags(child);
590     if ((child->flags & SEL_DYNAMIC) && !(param->flags & SPAR_DYNAMIC))
591     {
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",
596                 text.c_str());
597         GMX_THROW(InvalidInputError(message));
598     }
599     if (!(child->flags & SEL_DYNAMIC))
600     {
601         param->flags &= ~SPAR_DYNAMIC;
602     }
603     /* Put the child element in the correct place */
604     place_child(root, child, param);
605     return child;
606 }
607
608 /*! \brief
609  * Parses an expression value for a parameter that takes a variable number of values.
610  *
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.
615  */
616 static void parse_values_varnum_expr(const SelectionParserValueList&    values,
617                                      gmx_ana_selparam_t*                param,
618                                      const SelectionTreeElementPointer& root,
619                                      void*                              scanner)
620 {
621     GMX_RELEASE_ASSERT(values.size() == 1 && values.front().hasExpressionValue(),
622                        "Called with an invalid type of value");
623
624     SelectionTreeElementPointer child = add_child(root, param, values.front().expr, scanner);
625
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)
629     {
630         /* Set the value storage */
631         _gmx_selvalue_setstore(&child->v, param->val.u.ptr);
632         param->val.nr = 1;
633         if (param->nvalptr)
634         {
635             *param->nvalptr = param->val.nr;
636         }
637         param->nvalptr = nullptr;
638         return;
639     }
640
641     if (!(child->flags & SEL_VARNUMVAL))
642     {
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));
646     }
647
648     child->flags |= SEL_ALLOCVAL;
649     param->val.nr   = -1;
650     *param->nvalptr = param->val.nr;
651     /* Rest of the initialization is done during compilation in
652      * init_method(). */
653 }
654
655 /*! \brief
656  * Initializes the storage of an expression value.
657  *
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
661  *   \p param.
662  * \param[in]     scanner Scanner data structure.
663  *
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().
667  */
668 static void set_expr_value_store(const SelectionTreeElementPointer& sel,
669                                  gmx_ana_selparam_t*                param,
670                                  int                                i,
671                                  void*                              scanner)
672 {
673     if (sel->v.type != GROUP_VALUE && !(sel->flags & SEL_SINGLEVAL))
674     {
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));
678     }
679     switch (sel->v.type)
680     {
681         case INT_VALUE: sel->v.u.i = &param->val.u.i[i]; break;
682         case REAL_VALUE: sel->v.u.r = &param->val.u.r[i]; break;
683         case STR_VALUE: sel->v.u.s = &param->val.u.s[i]; break;
684         case POS_VALUE: sel->v.u.p = &param->val.u.p[i]; break;
685         case GROUP_VALUE: sel->v.u.g = &param->val.u.g[i]; break;
686         default: /* Error */ GMX_THROW(InternalError("Invalid value type"));
687     }
688     sel->v.nr     = 1;
689     sel->v.nalloc = -1;
690 }
691
692 /*! \brief
693  * Parses the values for a parameter that takes a constant number of values.
694  *
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.
699  *
700  * For integer ranges, the sequence of numbers from the first to second value
701  * is stored, each as a separate value.
702  */
703 static void parse_values_std(const SelectionParserValueList&    values,
704                              gmx_ana_selparam_t*                param,
705                              const SelectionTreeElementPointer& root,
706                              void*                              scanner)
707 {
708     int  i, j;
709     bool bDynamic;
710
711     /* Handle atom-valued parameters */
712     if (param->flags & SPAR_ATOMVAL)
713     {
714         if (values.size() > 1)
715         {
716             GMX_THROW(
717                     InvalidInputError("Only a single value or a single expression is "
718                                       "supported in this context"));
719         }
720         if (values.front().hasExpressionValue())
721         {
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))
725             {
726                 /* Rest of the initialization is done during compilation in
727                  * init_method(). */
728                 /* TODO: Positions are not correctly handled */
729                 param->val.nr = -1;
730                 if (param->nvalptr)
731                 {
732                     *param->nvalptr = -1;
733                 }
734                 return;
735             }
736             param->flags &= ~SPAR_ATOMVAL;
737             param->val.nr = 1;
738             if (param->nvalptr)
739             {
740                 *param->nvalptr = 1;
741             }
742             param->nvalptr = nullptr;
743             if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE || param->val.type == STR_VALUE)
744             {
745                 _gmx_selvalue_reserve(&param->val, 1);
746             }
747             set_expr_value_store(child, param, 0, scanner);
748             return;
749         }
750         /* If we reach here, proceed with normal parameter handling */
751         param->val.nr = 1;
752         if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE || param->val.type == STR_VALUE)
753         {
754             _gmx_selvalue_reserve(&param->val, 1);
755         }
756         param->flags &= ~SPAR_ATOMVAL;
757         param->flags &= ~SPAR_DYNAMIC;
758     }
759
760     i        = 0;
761     bDynamic = false;
762     SelectionParserValueList::const_iterator value;
763     for (value = values.begin(); value != values.end() && i < param->val.nr; ++value)
764     {
765         GMX_RELEASE_ASSERT(value->type == param->val.type,
766                            "Invalid value type (should have been caught earlier)");
767         if (value->hasExpressionValue())
768         {
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)
772             {
773                 bDynamic = true;
774             }
775         }
776         else
777         {
778             /* Value is not an expression */
779             switch (value->type)
780             {
781                 case INT_VALUE:
782                 {
783                     bool bTooManyValues;
784                     if (value->u.i.i1 <= value->u.i.i2)
785                     {
786                         for (j = value->u.i.i1; j <= value->u.i.i2 && i < param->val.nr; ++j)
787                         {
788                             param->val.u.i[i++] = j;
789                         }
790                         bTooManyValues = (j != value->u.i.i2 + 1);
791                     }
792                     else
793                     {
794                         for (j = value->u.i.i1; j >= value->u.i.i2 && i < param->val.nr; --j)
795                         {
796                             param->val.u.i[i++] = j;
797                         }
798                         bTooManyValues = (j != value->u.i.i2 - 1);
799                     }
800                     if (bTooManyValues)
801                     {
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",
806                                 text.c_str());
807                         GMX_THROW(InvalidInputError(message));
808                     }
809                     --i;
810                     break;
811                 }
812                 case REAL_VALUE:
813                     if (value->u.r.r1 != value->u.r.r2)
814                     {
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));
819                     }
820                     param->val.u.r[i] = value->u.r.r1;
821                     break;
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(&param->val.u.p[i], value->u.x); break;
824                 case NO_VALUE:
825                 case GROUP_VALUE: GMX_THROW(InternalError("Invalid non-expression value type"));
826             }
827         }
828         ++i;
829     }
830     if (value != values.end())
831     {
832         std::string message = formatString("Too many values provided, expected %d", param->val.nr);
833         GMX_THROW(InvalidInputError(message));
834     }
835     if (i < param->val.nr)
836     {
837         std::string message = formatString("Too few values provided, expected %d", param->val.nr);
838         GMX_THROW(InvalidInputError(message));
839     }
840     if (!bDynamic)
841     {
842         param->flags &= ~SPAR_DYNAMIC;
843     }
844     if (param->nvalptr)
845     {
846         *param->nvalptr = param->val.nr;
847     }
848     param->nvalptr = nullptr;
849 }
850
851 /*! \brief
852  * Parses the values for a boolean parameter.
853  *
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.
858  */
859 static void parse_values_bool(const std::string&              name,
860                               const SelectionParserValueList& values,
861                               gmx_ana_selparam_t*             param,
862                               void*                           scanner)
863 {
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))
867     {
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));
871     }
872
873     bool bSetNo = false;
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)
877     {
878         bSetNo = true;
879     }
880     if (bSetNo && !values.empty())
881     {
882         std::string message = formatString("'no%s' cannot be followed by any value", param->name);
883         GMX_THROW(InvalidInputError(message));
884     }
885     if (!values.empty() && values.front().u.i.i1 == 0)
886     {
887         bSetNo = true;
888     }
889
890     *param->val.u.b = !bSetNo;
891 }
892
893 /*! \brief
894  * Parses the values for an enumeration parameter.
895  *
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.
900  */
901 static void parse_values_enum(const SelectionParserValueList& values, gmx_ana_selparam_t* param, void* scanner)
902 {
903     GMX_ASSERT(param->val.type == STR_VALUE, "Enum parser called for non-string parameter");
904     if (values.size() != 1)
905     {
906         GMX_THROW(InvalidInputError("Only a single string value is supported in this context"));
907     }
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())
912     {
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));
917     }
918
919     const std::string& svalue = value.stringValue();
920     int                i      = 1;
921     int                match  = 0;
922     while (param->val.u.s[i] != nullptr)
923     {
924         if (startsWith(param->val.u.s[i], svalue))
925         {
926             /* Check if there is a duplicate match */
927             if (match > 0)
928             {
929                 std::string message = formatString("Value '%s' is ambiguous", svalue.c_str());
930                 GMX_THROW(InvalidInputError(message));
931             }
932             match = i;
933         }
934         ++i;
935     }
936     if (match == 0)
937     {
938         std::string message = formatString("Value '%s' is not recognized", svalue.c_str());
939         GMX_THROW(InvalidInputError(message));
940     }
941     param->val.u.s[0] = param->val.u.s[match];
942 }
943
944 /*! \brief
945  * Replaces constant expressions with their values.
946  *
947  * \param[in,out] values First element in the value list to process.
948  */
949 static void convert_const_values(SelectionParserValueList* values)
950 {
951     SelectionParserValueList::iterator value;
952     for (value = values->begin(); value != values->end(); ++value)
953     {
954         if (value->hasExpressionValue() && value->expr->v.type != GROUP_VALUE && value->expr->type == SEL_CONST)
955         {
956             SelectionTreeElementPointer expr     = value->expr;
957             const SelectionLocation&    location = value->location();
958             switch (expr->v.type)
959             {
960                 case INT_VALUE:
961                     *value = SelectionParserValue::createInteger(expr->v.u.i[0], location);
962                     break;
963                 case REAL_VALUE:
964                     *value = SelectionParserValue::createReal(expr->v.u.r[0], location);
965                     break;
966                 case STR_VALUE:
967                     *value = SelectionParserValue::createString(expr->v.u.s[0], location);
968                     break;
969                 case POS_VALUE:
970                     *value = SelectionParserValue::createPosition(expr->v.u.p->x[0], location);
971                     break;
972                 default: GMX_RELEASE_ASSERT(false, "Unsupported constant expression value type");
973             }
974         }
975     }
976 }
977
978 /*!
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.
984  *
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.
988  *
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.
991  */
992 void _gmx_sel_parse_params(const gmx::SelectionParserParameterList& pparams,
993                            int                                      nparam,
994                            gmx_ana_selparam_t*                      params,
995                            const gmx::SelectionTreeElementPointer&  root,
996                            void*                                    scanner)
997 {
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)
1002     {
1003         if (params[i].val.type != POS_VALUE && (params[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
1004         {
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");
1013         }
1014         else
1015         {
1016             GMX_RELEASE_ASSERT(params[i].val.u.ptr != nullptr, "value pointer is NULL");
1017         }
1018     }
1019     /* Parse the parameters */
1020     int                                          nullParamIndex = 0;
1021     SelectionParserParameterList::const_iterator pparam;
1022     for (pparam = pparams.begin(); pparam != pparams.end(); ++pparam)
1023     {
1024         try
1025         {
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())
1030             {
1031                 nullParamIndex = -1;
1032                 oparam         = gmx_ana_selparam_find(pparam->name().c_str(), nparam, params);
1033                 GMX_RELEASE_ASSERT(oparam != nullptr, "Inconsistent selection parameter");
1034             }
1035             else if (nullParamIndex >= 0)
1036             {
1037                 oparam = &params[nullParamIndex];
1038                 if (oparam->name != nullptr)
1039                 {
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));
1043                 }
1044                 ++nullParamIndex;
1045             }
1046             else
1047             {
1048                 GMX_RELEASE_ASSERT(false,
1049                                    "All NULL parameters should appear in "
1050                                    "the beginning of the list");
1051             }
1052             if (oparam->flags & SPAR_SET)
1053             {
1054                 std::string message =
1055                         formatString("'%s' appears multiple times", pparam->name().c_str());
1056                 GMX_THROW(InvalidInputError(message));
1057             }
1058             oparam->flags |= SPAR_SET;
1059             if (oparam->val.type != NO_VALUE && pparam->values().empty())
1060             {
1061                 std::string text;
1062                 if (pparam->name().empty())
1063                 {
1064                     text = root->name();
1065                 }
1066                 else
1067                 {
1068                     text = _gmx_sel_lexer_get_text(scanner, pparam->location());
1069                 }
1070                 std::string message =
1071                         formatString("'%s' should be followed by a value/expression", text.c_str());
1072                 GMX_THROW(InvalidInputError(message));
1073             }
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)
1078             {
1079                 parse_values_bool(pparam->name(), pparam->values(), oparam, scanner);
1080             }
1081             else if (oparam->flags & SPAR_RANGES)
1082             {
1083                 parse_values_range(pparam->values(), oparam, scanner);
1084             }
1085             else if (oparam->flags & SPAR_VARNUM)
1086             {
1087                 if (pparam->values().size() == 1 && pparam->values().front().hasExpressionValue())
1088                 {
1089                     parse_values_varnum_expr(pparam->values(), oparam, root, scanner);
1090                 }
1091                 else
1092                 {
1093                     parse_values_varnum(pparam->values(), oparam, root, scanner);
1094                 }
1095             }
1096             else if (oparam->flags & SPAR_ENUMVAL)
1097             {
1098                 parse_values_enum(pparam->values(), oparam, scanner);
1099             }
1100             else
1101             {
1102                 parse_values_std(pparam->values(), oparam, root, scanner);
1103             }
1104         }
1105         catch (UserInputError& ex)
1106         {
1107             if (!pparam->name().empty())
1108             {
1109                 std::string text(_gmx_sel_lexer_get_text(scanner, pparam->location()));
1110                 ex.prependContext(formatString("In '%s'", text.c_str()));
1111             }
1112             errors.addCurrentExceptionAsNested();
1113         }
1114     }
1115     /* Check that all required parameters are present */
1116     for (int i = 0; i < nparam; ++i)
1117     {
1118         if (!(params[i].flags & SPAR_OPTIONAL) && !(params[i].flags & SPAR_SET))
1119         {
1120             std::string message;
1121             if (params[i].name == nullptr)
1122             {
1123                 message = formatString("'%s' should be followed by a value/expression",
1124                                        root->name().c_str());
1125             }
1126             else
1127             {
1128                 message = formatString("'%s' is missing", params[i].name);
1129             }
1130             InvalidInputError ex(message);
1131             errors.addNested(ex);
1132         }
1133     }
1134     if (errors.hasNestedExceptions())
1135     {
1136         GMX_THROW(InvalidInputError(errors));
1137     }
1138 }