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