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