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