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