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