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