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