Improve selection error messages (use exceptions)
[alexxy/gromacs.git] / src / gromacs / selection / parsetree.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 parsetree.h.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_selection
41  */
42 /*! \internal
43  * \page page_module_selection_parser Selection parsing
44  *
45  * The selection parser is implemented in the following files:
46  *  - scanner.l:
47  *    Tokenizer implemented using Flex, splits the input into tokens
48  *    (scanner.c and scanner_flex.h are generated from this file).
49  *  - scanner.h, scanner_internal.h, scanner_internal.cpp:
50  *    Helper functions for scanner.l and for interfacing between
51  *    scanner.l and parser.y. Functions in scanner_internal.h are only
52  *    used from scanner.l, while scanner.h is used from the parser.
53  *  - symrec.h, symrec.cpp:
54  *    Functions used by the tokenizer to handle the symbol table, i.e.,
55  *    the recognized keywords. Some basic keywords are hardcoded into
56  *    scanner.l, but all method and variable references go through the
57  *    symbol table, as do position evaluation keywords.
58  *  - parser.y:
59  *    Semantic rules for parsing the grammar
60  *    (parser.cpp and parser.h are generated from this file by Bison).
61  *  - parsetree.h, parsetree.cpp:
62  *    Functions called from actions in parser.y to construct the
63  *    evaluation elements corresponding to different grammar elements.
64  *  - params.c:
65  *    Defines a function that processes the parameters of selection
66  *    methods and initializes the children of the method element.
67  *  - selectioncollection.h, selectioncollection.cpp:
68  *    These files define the high-level public interface to the parser
69  *    through SelectionCollection::parseFromStdin(),
70  *    SelectionCollection::parseFromFile() and
71  *    SelectionCollection::parseFromString().
72  *
73  * The basic control flow in the parser is as follows: when a parser function
74  * in SelectionCollection gets called, it performs some
75  * initialization, and then calls the _gmx_sel_yyparse() function generated
76  * by Bison. This function then calls _gmx_sel_yylex() to repeatedly read
77  * tokens from the input (more complex tasks related to token recognition
78  * and bookkeeping are done by functions in scanner_internal.cpp) and uses the
79  * grammar rules to decide what to do with them. Whenever a grammar rule
80  * matches, a corresponding function in parsetree.cpp is called to construct
81  * either a temporary representation for the object or a
82  * gmx::SelectionTreeElement object
83  * (some simple rules are handled internally in parser.y).
84  * When a complete selection has been parsed, the functions in parsetree.cpp
85  * also take care of updating the ::gmx_ana_selcollection_t structure
86  * appropriately.
87  *
88  * The rest of this page describes the resulting gmx::SelectionTreeElement
89  * object tree.
90  * Before the selections can be evaluated, this tree needs to be passed to
91  * the selection compiler, which is described on a separate page:
92  * \ref page_module_selection_compiler
93  *
94  *
95  * \section selparser_tree Element tree constructed by the parser
96  *
97  * The parser initializes the following fields in all selection elements:
98  * gmx::SelectionTreeElement::name, gmx::SelectionTreeElement::type,
99  * gmx::SelectionTreeElement::v\c .type,
100  * gmx::SelectionTreeElement::flags, gmx::SelectionTreeElement::child, and
101  * gmx::SelectionTreeElement::next.
102  * Some other fields are also initialized for particular element types as
103  * discussed below.
104  * Fields that are not initialized are set to zero, NULL, or other similar
105  * value.
106  *
107  *
108  * \subsection selparser_tree_root Root elements
109  *
110  * The parser creates a \ref SEL_ROOT selection element for each variable
111  * assignment and each selection. However, there are two exceptions that do
112  * not result in a \ref SEL_ROOT element (in these cases, only the symbol
113  * table is modified):
114  *  - Variable assignments that assign a variable to another variable.
115  *  - Variable assignments that assign a non-group constant.
116  *  .
117  * The \ref SEL_ROOT elements are linked together in a chain in the same order
118  * as in the input.
119  *
120  * The children of the \ref SEL_ROOT elements can be used to distinguish
121  * the two types of root elements from each other:
122  *  - For variable assignments, the first and only child is always
123  *    a \ref SEL_SUBEXPR element.
124  *  - For selections, the first child is a \ref SEL_EXPRESSION or a
125  *    \ref SEL_MODIFIER element that evaluates the final positions (if the
126  *    selection defines a constant position, the child is a \ref SEL_CONST).
127  *    The rest of the children are \ref SEL_MODIFIER elements with
128  *    \ref NO_VALUE, in the order given by the user.
129  *  .
130  * The name of the selection/variable is stored in
131  * gmx::SelectionTreeElement::cgrp\c .name.
132  * It is set to either the name provided by the user or the selection string
133  * for selections not explicitly named by the user.
134  * \ref SEL_ROOT or \ref SEL_SUBEXPR elements do not appear anywhere else.
135  *
136  *
137  * \subsection selparser_tree_const Constant elements
138  *
139  * \ref SEL_CONST elements are created for every constant that is required
140  * for later evaluation.
141  * Currently, \ref SEL_CONST elements can be present for
142  *  - selections that consist of a constant position,
143  *  - \ref GROUP_VALUE method parameters if provided using external index
144  *    groups,
145  *  .
146  * For group-valued elements, the value is stored in
147  * gmx::SelectionTreeElement::cgrp; other types of values are stored in
148  * gmx::SelectionTreeElement::v.
149  * Constants that appear as parameters for selection methods are not present
150  * in the selection tree unless they have \ref GROUP_VALUE.
151  * \ref SEL_CONST elements have no children.
152  *
153  *
154  * \subsection selparser_tree_method Method evaluation elements
155  *
156  * \ref SEL_EXPRESSION and \ref SEL_MODIFIER elements are treated very
157  * similarly. The \c gmx_ana_selmethod_t structure corresponding to the
158  * evaluation method is in gmx::SelectionTreeElement::method, and the method
159  * data in gmx::SelectionTreeElement::mdata has been allocated using
160  * sel_datafunc().
161  * If a non-standard reference position type was set,
162  * gmx::SelectionTreeElement::pc has also been created, but only the type has
163  * been set.
164  * All children of these elements are of the type \ref SEL_SUBEXPRREF, and
165  * each describes a selection that needs to be evaluated to obtain a value
166  * for one parameter of the method.
167  * No children are present for parameters that were given a constant
168  * non-\ref GROUP_VALUE value.
169  * The children are sorted in the order in which the parameters appear in the
170  * \ref gmx_ana_selmethod_t structure.
171  *
172  * In addition to actual selection keywords, \ref SEL_EXPRESSION elements
173  * are used internally to implement numerical comparisons (e.g., "x < 5")
174  * and keyword matching (e.g., "resnr 1 to 3" or "name CA").
175  *
176  *
177  * \subsection selparser_tree_subexpr Subexpression elements
178  *
179  * \ref SEL_SUBEXPR elements only appear for variables, as described above.
180  * gmx::SelectionTreeElement::name points to the name of the variable (from the
181  * \ref SEL_ROOT element).
182  * The element always has exactly one child, which represents the value of
183  * the variable.
184  *
185  * \ref SEL_SUBEXPRREF elements are used for two purposes:
186  *  - Variable references that need to be evaluated (i.e., there is a
187  *    \ref SEL_SUBEXPR element for the variable) are represented using
188  *    \ref SEL_SUBEXPRREF elements.
189  *    In this case, gmx::SelectionTreeElement::param is NULL, and the first and
190  *    only child of the element is the \ref SEL_SUBEXPR element of the
191  *    variable.
192  *    Such references can appear anywhere where the variable value
193  *    (the child of the \ref SEL_SUBEXPR element) would be valid.
194  *  - Children of \ref SEL_EXPRESSION and \ref SEL_MODIFIER elements are
195  *    always of this type. For these elements, gmx::SelectionTreeElement::param
196  *    is initialized to point to the parameter that receives the value from
197  *    the expression.
198  *    Each such element has exactly one child, which can be of any type;
199  *    the \ref SEL_SUBEXPR element of a variable is used if the value comes
200  *    from a variable, otherwise the child type is not \ref SEL_SUBEXPR.
201  *
202  *
203  * \subsection selparser_tree_bool Boolean elements
204  *
205  * One \ref SEL_BOOLEAN element is created for each boolean keyword in the
206  * input, and the tree structure represents the evaluation order.
207  * The gmx::SelectionTreeElement::boolt type gives the type of the operation.
208  * Each element has exactly two children (one for \ref BOOL_NOT elements),
209  * which are in the order given in the input.
210  * The children always have \ref GROUP_VALUE, but different element types
211  * are possible.
212  *
213  *
214  * \subsection selparser_tree_arith Arithmetic elements
215  *
216  * One \ref SEL_ARITHMETIC element is created for each arithmetic operation in
217  * the input, and the tree structure represents the evaluation order.
218  * The gmx::SelectionTreeElement::optype type gives the name of the operation.
219  * Each element has exactly two children (one for unary negation elements),
220  * which are in the order given in the input.
221  */
222 #include "gmxpre.h"
223
224 #include "parsetree.h"
225
226 #include <stdarg.h>
227 #include <stdio.h>
228
229 #include <boost/exception_ptr.hpp>
230 #include <boost/shared_ptr.hpp>
231
232 #include "gromacs/selection/selection.h"
233 #include "gromacs/utility/cstringutil.h"
234 #include "gromacs/utility/exceptions.h"
235 #include "gromacs/utility/file.h"
236 #include "gromacs/utility/messagestringcollector.h"
237 #include "gromacs/utility/smalloc.h"
238 #include "gromacs/utility/stringutil.h"
239
240 #include "keywords.h"
241 #include "poscalc.h"
242 #include "scanner.h"
243 #include "selectioncollection-impl.h"
244 #include "selelem.h"
245 #include "selmethod.h"
246 #include "symrec.h"
247
248 using gmx::SelectionLocation;
249 using gmx::SelectionParserValue;
250 using gmx::SelectionParserValueList;
251 using gmx::SelectionParserValueListPointer;
252 using gmx::SelectionParserParameter;
253 using gmx::SelectionParserParameterList;
254 using gmx::SelectionParserParameterListPointer;
255 using gmx::SelectionParserValue;
256 using gmx::SelectionTreeElement;
257 using gmx::SelectionTreeElementPointer;
258
259 namespace
260 {
261
262 /*! \brief
263  * Formats context string for errors.
264  *
265  * The returned string is used as the context for errors reported during
266  * parsing.
267  */
268 std::string
269 formatCurrentErrorContext(yyscan_t scanner)
270 {
271     return gmx::formatString(
272             "While parsing '%s'",
273             _gmx_sel_lexer_get_current_text(scanner).c_str());
274 }
275
276 } // namespace
277
278 void
279 _gmx_selparser_error(yyscan_t scanner, const char *fmt, ...)
280 {
281     gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
282     // FIXME: Use an arbitrary length buffer.
283     char    buf[1024];
284     va_list ap;
285     va_start(ap, fmt);
286     vsnprintf(buf, 1024, fmt, ap);
287     va_end(ap);
288     errors->append(buf);
289 }
290
291 bool
292 _gmx_selparser_handle_exception(yyscan_t scanner, std::exception *ex)
293 {
294     try
295     {
296         bool                   canContinue = false;
297         gmx::GromacsException *gromacsException
298             = dynamic_cast<gmx::GromacsException *>(ex);
299         if (gromacsException != NULL)
300         {
301             gromacsException->prependContext(formatCurrentErrorContext(scanner));
302             canContinue = (dynamic_cast<gmx::UserInputError *>(ex) != NULL);
303         }
304         _gmx_sel_lexer_set_exception(scanner, boost::current_exception());
305         return canContinue;
306     }
307     catch (const std::exception &)
308     {
309         _gmx_sel_lexer_set_exception(scanner, boost::current_exception());
310         return false;
311     }
312 }
313
314 bool
315 _gmx_selparser_handle_error(yyscan_t scanner)
316 {
317     std::string context(gmx::formatString("Invalid selection '%s'",
318                                           _gmx_sel_lexer_pselstr(scanner)));
319     // The only way to prepend context to the exception is to rethrow it.
320     try
321     {
322         _gmx_sel_lexer_rethrow_exception_if_occurred(scanner);
323     }
324     catch (gmx::UserInputError &ex)
325     {
326         ex.prependContext(context);
327         if (_gmx_sel_is_lexer_interactive(scanner))
328         {
329             gmx::formatExceptionMessageToFile(stderr, ex);
330             return true;
331         }
332         throw;
333     }
334     catch (gmx::GromacsException &ex)
335     {
336         ex.prependContext(context);
337         throw;
338     }
339     GMX_RELEASE_ASSERT(false, "All parsing errors should result in a captured exception");
340     return false; // Some compilers will not believe that the above never returns.
341 }
342
343 namespace gmx
344 {
345
346 /********************************************************************
347  * SelectionParserValue
348  */
349
350 SelectionParserValue::SelectionParserValue(
351         e_selvalue_t type, const SelectionLocation &location)
352     : type(type), location_(location)
353 {
354     memset(&u, 0, sizeof(u));
355 }
356
357 SelectionParserValue::SelectionParserValue(
358         const SelectionTreeElementPointer &expr)
359     : type(expr->v.type), expr(expr), location_(expr->location())
360 {
361     memset(&u, 0, sizeof(u));
362 }
363
364 /********************************************************************
365  * SelectionParserParameter
366  */
367
368 SelectionParserParameter::SelectionParserParameter(
369         const char                      *name,
370         SelectionParserValueListPointer  values,
371         const SelectionLocation         &location)
372     : name_(name != NULL ? name : ""), location_(location),
373       values_(values ? move(values)
374               : SelectionParserValueListPointer(new SelectionParserValueList))
375 {
376 }
377
378 } // namespace gmx
379
380 /*!
381  * \param[in,out] sel  Root of the selection element tree to initialize.
382  *
383  * Propagates the \ref SEL_DYNAMIC flag from the children of \p sel to \p sel
384  * (if any child of \p sel is dynamic, \p sel is also marked as such).
385  * The \ref SEL_DYNAMIC flag is also set for \ref SEL_EXPRESSION elements with
386  * a dynamic method.
387  * Also, sets one of the \ref SEL_SINGLEVAL, \ref SEL_ATOMVAL, or
388  * \ref SEL_VARNUMVAL flags, either based on the children or on the type of
389  * the selection method.
390  * If the types of the children conflict, an error is returned.
391  *
392  * The flags of the children of \p sel are also updated if not done earlier.
393  * The flags are initialized only once for any element; if \ref SEL_FLAGSSET
394  * is set for an element, the function returns immediately, and the recursive
395  * operation does not descend beyond such elements.
396  */
397 void
398 _gmx_selelem_update_flags(const gmx::SelectionTreeElementPointer &sel)
399 {
400     bool                bUseChildType = false;
401     bool                bOnlySingleChildren;
402
403     /* Return if the flags have already been set */
404     if (sel->flags & SEL_FLAGSSET)
405     {
406         return;
407     }
408     /* Set the flags based on the current element type */
409     switch (sel->type)
410     {
411         case SEL_CONST:
412         case SEL_GROUPREF:
413             sel->flags   |= SEL_SINGLEVAL;
414             bUseChildType = false;
415             break;
416
417         case SEL_EXPRESSION:
418             if (sel->u.expr.method->flags & SMETH_DYNAMIC)
419             {
420                 sel->flags |= SEL_DYNAMIC;
421             }
422             if (sel->u.expr.method->flags & SMETH_SINGLEVAL)
423             {
424                 sel->flags |= SEL_SINGLEVAL;
425             }
426             else if (sel->u.expr.method->flags & SMETH_VARNUMVAL)
427             {
428                 sel->flags |= SEL_VARNUMVAL;
429             }
430             else
431             {
432                 sel->flags |= SEL_ATOMVAL;
433             }
434             bUseChildType = false;
435             break;
436
437         case SEL_ARITHMETIC:
438             sel->flags   |= SEL_ATOMVAL;
439             bUseChildType = false;
440             break;
441
442         case SEL_MODIFIER:
443             if (sel->v.type != NO_VALUE)
444             {
445                 sel->flags |= SEL_VARNUMVAL;
446             }
447             bUseChildType = false;
448             break;
449
450         case SEL_ROOT:
451             bUseChildType = false;
452             break;
453
454         case SEL_BOOLEAN:
455         case SEL_SUBEXPR:
456         case SEL_SUBEXPRREF:
457             bUseChildType = true;
458             break;
459     }
460     /* Loop through children to propagate their flags upwards */
461     bOnlySingleChildren = true;
462     SelectionTreeElementPointer child = sel->child;
463     while (child)
464     {
465         /* Update the child */
466         _gmx_selelem_update_flags(child);
467         /* Propagate the dynamic and unsorted flags */
468         sel->flags |= (child->flags & (SEL_DYNAMIC | SEL_UNSORTED));
469         /* Propagate the type flag if necessary and check for problems */
470         if (bUseChildType)
471         {
472             if ((sel->flags & SEL_VALTYPEMASK)
473                 && !(sel->flags & child->flags & SEL_VALTYPEMASK))
474             {
475                 // TODO: Recollect when this is triggered, and whether the type
476                 // is appropriate.
477                 GMX_THROW(gmx::InvalidInputError("Invalid combination of selection expressions"));
478             }
479             sel->flags |= (child->flags & SEL_VALTYPEMASK);
480         }
481         if (!(child->flags & SEL_SINGLEVAL))
482         {
483             bOnlySingleChildren = false;
484         }
485
486         child = child->next;
487     }
488     /* For arithmetic expressions consisting only of single values,
489      * the result is also a single value. */
490     if (sel->type == SEL_ARITHMETIC && bOnlySingleChildren)
491     {
492         sel->flags = (sel->flags & ~SEL_VALTYPEMASK) | SEL_SINGLEVAL;
493     }
494     /* For root elements, the type should be propagated here, after the
495      * children have been updated. */
496     if (sel->type == SEL_ROOT)
497     {
498         GMX_ASSERT(sel->child, "Root elements should always have a child");
499         sel->flags |= (sel->child->flags & SEL_VALTYPEMASK);
500     }
501     /* Mark that the flags are set */
502     sel->flags |= SEL_FLAGSSET;
503 }
504
505 /*!
506  * \param[in,out] sel    Selection element to initialize.
507  * \param[in]     scanner Scanner data structure.
508  *
509  * A deep copy of the parameters is made to allow several
510  * expressions with the same method to coexist peacefully.
511  * Calls sel_datafunc() if one is specified for the method.
512  */
513 void
514 _gmx_selelem_init_method_params(const gmx::SelectionTreeElementPointer &sel,
515                                 yyscan_t                                scanner)
516 {
517     int                 nparams;
518     gmx_ana_selparam_t *orgparam;
519     gmx_ana_selparam_t *param;
520     int                 i;
521     void               *mdata;
522
523     nparams   = sel->u.expr.method->nparams;
524     orgparam  = sel->u.expr.method->param;
525     snew(param, nparams);
526     memcpy(param, orgparam, nparams*sizeof(gmx_ana_selparam_t));
527     for (i = 0; i < nparams; ++i)
528     {
529         param[i].flags &= ~SPAR_SET;
530         _gmx_selvalue_setstore(&param[i].val, NULL);
531         if (param[i].flags & SPAR_VARNUM)
532         {
533             param[i].val.nr = -1;
534         }
535         /* Duplicate the enum value array if it is given statically */
536         if ((param[i].flags & SPAR_ENUMVAL) && orgparam[i].val.u.ptr != NULL)
537         {
538             int n;
539
540             /* Count the values */
541             n = 1;
542             while (orgparam[i].val.u.s[n] != NULL)
543             {
544                 ++n;
545             }
546             _gmx_selvalue_reserve(&param[i].val, n+1);
547             memcpy(param[i].val.u.s, orgparam[i].val.u.s,
548                    (n+1)*sizeof(param[i].val.u.s[0]));
549         }
550     }
551     mdata = NULL;
552     if (sel->u.expr.method->init_data)
553     {
554         mdata = sel->u.expr.method->init_data(nparams, param);
555     }
556     if (sel->u.expr.method->set_poscoll)
557     {
558         gmx_ana_selcollection_t *sc = _gmx_sel_lexer_selcollection(scanner);
559
560         sel->u.expr.method->set_poscoll(&sc->pcc, mdata);
561     }
562     /* Store the values */
563     sel->u.expr.method->param = param;
564     sel->u.expr.mdata         = mdata;
565 }
566
567 /*!
568  * \param[in,out] sel    Selection element to initialize.
569  * \param[in]     method Selection method to set.
570  * \param[in]     scanner Scanner data structure.
571  *
572  * Makes a copy of \p method and stores it in \p sel->u.expr.method,
573  * and calls _gmx_selelem_init_method_params();
574  */
575 void
576 _gmx_selelem_set_method(const gmx::SelectionTreeElementPointer &sel,
577                         gmx_ana_selmethod_t                    *method,
578                         yyscan_t                                scanner)
579 {
580     _gmx_selelem_set_vtype(sel, method->type);
581     sel->setName(method->name);
582     snew(sel->u.expr.method, 1);
583     memcpy(sel->u.expr.method, method, sizeof(gmx_ana_selmethod_t));
584     _gmx_selelem_init_method_params(sel, scanner);
585 }
586
587 /*! \brief
588  * Initializes the reference position calculation for a \ref SEL_EXPRESSION
589  * element.
590  *
591  * \param[in,out] pcc    Position calculation collection to use.
592  * \param[in,out] sel    Selection element to initialize.
593  * \param[in]     rpost  Reference position type to use (NULL = default).
594  */
595 static void
596 set_refpos_type(gmx::PositionCalculationCollection *pcc,
597                 const SelectionTreeElementPointer  &sel,
598                 const char                         *rpost)
599 {
600     if (!rpost)
601     {
602         return;
603     }
604
605     if (sel->u.expr.method->pupdate)
606     {
607         /* By default, use whole residues/molecules. */
608         sel->u.expr.pc
609             = pcc->createCalculationFromEnum(rpost, POS_COMPLWHOLE);
610     }
611     else
612     {
613         std::string message
614             = gmx::formatString("Position modifiers ('%s') is not applicable for '%s'",
615                                 rpost, sel->u.expr.method->name);
616         GMX_THROW(gmx::InvalidInputError(message));
617     }
618 }
619
620 gmx::SelectionTreeElementPointer
621 _gmx_sel_init_arithmetic(const gmx::SelectionTreeElementPointer &left,
622                          const gmx::SelectionTreeElementPointer &right,
623                          char op, yyscan_t scanner)
624 {
625     SelectionTreeElementPointer sel(
626             new SelectionTreeElement(
627                     SEL_ARITHMETIC, _gmx_sel_lexer_get_current_location(scanner)));
628     sel->v.type        = REAL_VALUE;
629     switch (op)
630     {
631         case '+': sel->u.arith.type = ARITH_PLUS; break;
632         case '-': sel->u.arith.type = (right ? ARITH_MINUS : ARITH_NEG); break;
633         case '*': sel->u.arith.type = ARITH_MULT; break;
634         case '/': sel->u.arith.type = ARITH_DIV;  break;
635         case '^': sel->u.arith.type = ARITH_EXP;  break;
636     }
637     char               buf[2];
638     buf[0] = op;
639     buf[1] = 0;
640     sel->setName(buf);
641     sel->u.arith.opstr = gmx_strdup(buf);
642     sel->child         = left;
643     sel->child->next   = right;
644     return sel;
645 }
646
647 /*!
648  * \param[in]  left   Selection element for the left hand side.
649  * \param[in]  right  Selection element for the right hand side.
650  * \param[in]  cmpop  String representation of the comparison operator.
651  * \param[in]  scanner Scanner data structure.
652  * \returns    The created selection element.
653  *
654  * This function handles the creation of a gmx::SelectionTreeElement object for
655  * comparison expressions.
656  */
657 SelectionTreeElementPointer
658 _gmx_sel_init_comparison(const gmx::SelectionTreeElementPointer &left,
659                          const gmx::SelectionTreeElementPointer &right,
660                          const char *cmpop, yyscan_t scanner)
661 {
662     gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
663     gmx::MessageStringContext    context(errors, formatCurrentErrorContext(scanner));
664
665     SelectionTreeElementPointer  sel(
666             new SelectionTreeElement(
667                     SEL_EXPRESSION, _gmx_sel_lexer_get_current_location(scanner)));
668     _gmx_selelem_set_method(sel, &sm_compare, scanner);
669
670     SelectionParserParameterList params;
671     const char                  *name;
672     // Create the parameter for the left expression.
673     name  = left->v.type == INT_VALUE ? "int1" : "real1";
674     params.push_back(SelectionParserParameter::createFromExpression(name, left));
675     // Create the parameter for the right expression.
676     name  = right->v.type == INT_VALUE ? "int2" : "real2";
677     params.push_back(SelectionParserParameter::createFromExpression(name, right));
678     // Create the parameter for the operator.
679     // TODO: Consider whether a proper location is needed.
680     SelectionLocation location(SelectionLocation::createEmpty());
681     params.push_back(
682             SelectionParserParameter::create(
683                     "op", SelectionParserValue::createString(cmpop, location),
684                     location));
685     _gmx_sel_parse_params(params, sel->u.expr.method->nparams,
686                           sel->u.expr.method->param, sel, scanner);
687
688     return sel;
689 }
690
691 /*! \brief
692  * Implementation method for keyword expression creation.
693  *
694  * \param[in]  method Method to use.
695  * \param[in]  matchType String matching type (only used if \p method is
696  *      a string keyword and \p args is not empty.
697  * \param[in]  args   Pointer to the first argument.
698  * \param[in]  rpost  Reference position type to use (NULL = default).
699  * \param[in]  scanner Scanner data structure.
700  * \returns    The created selection element.
701  *
702  * This function handles the creation of a gmx::SelectionTreeElement object for
703  * selection methods that do not take parameters.
704  */
705 static SelectionTreeElementPointer
706 init_keyword_internal(gmx_ana_selmethod_t *method,
707                       gmx::SelectionStringMatchType matchType,
708                       SelectionParserValueListPointer args,
709                       const char *rpost, yyscan_t scanner)
710 {
711     gmx_ana_selcollection_t     *sc = _gmx_sel_lexer_selcollection(scanner);
712
713     gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
714     gmx::MessageStringContext    context(errors, formatCurrentErrorContext(scanner));
715
716     if (method->nparams > 0)
717     {
718         // TODO: Would assert be better?
719         GMX_THROW(gmx::InternalError(
720                           "Keyword initialization called with non-keyword method"));
721     }
722
723     const SelectionLocation    &location = _gmx_sel_lexer_get_current_location(scanner);
724     // TODO: If there are arguments, the location would be better as just the
725     // location of the keyword itself.
726     SelectionTreeElementPointer root(new SelectionTreeElement(SEL_EXPRESSION, location));
727     SelectionTreeElementPointer child = root;
728     _gmx_selelem_set_method(child, method, scanner);
729
730     /* Initialize the evaluation of keyword matching if values are provided */
731     if (args)
732     {
733         gmx_ana_selmethod_t *kwmethod;
734         switch (method->type)
735         {
736             case INT_VALUE:  kwmethod = &sm_keyword_int;  break;
737             case REAL_VALUE: kwmethod = &sm_keyword_real; break;
738             case STR_VALUE:  kwmethod = &sm_keyword_str;  break;
739             default:
740                 GMX_THROW(gmx::InternalError(
741                                   "Unknown type for keyword selection"));
742         }
743         /* Initialize the selection element */
744         root.reset(new SelectionTreeElement(SEL_EXPRESSION, location));
745         _gmx_selelem_set_method(root, kwmethod, scanner);
746         if (method->type == STR_VALUE)
747         {
748             _gmx_selelem_set_kwstr_match_type(root, matchType);
749         }
750         SelectionParserParameterList params;
751         params.push_back(
752                 SelectionParserParameter::createFromExpression(NULL, child));
753         params.push_back(
754                 SelectionParserParameter::create(NULL, move(args), location));
755         _gmx_sel_parse_params(params, root->u.expr.method->nparams,
756                               root->u.expr.method->param, root, scanner);
757     }
758     set_refpos_type(&sc->pcc, child, rpost);
759
760     return root;
761 }
762
763 /*!
764  * \param[in]  method Method to use.
765  * \param[in]  args   Pointer to the first argument.
766  * \param[in]  rpost  Reference position type to use (NULL = default).
767  * \param[in]  scanner Scanner data structure.
768  * \returns    The created selection element.
769  *
770  * This function handles the creation of a gmx::SelectionTreeElement object for
771  * selection methods that do not take parameters.
772  */
773 SelectionTreeElementPointer
774 _gmx_sel_init_keyword(gmx_ana_selmethod_t *method,
775                       gmx::SelectionParserValueListPointer args,
776                       const char *rpost, yyscan_t scanner)
777 {
778     return init_keyword_internal(method, gmx::eStringMatchType_Auto, move(args),
779                                  rpost, scanner);
780 }
781
782 /*!
783  * \param[in]  method    Method to use.
784  * \param[in]  matchType String matching type.
785  * \param[in]  args      Pointer to the first argument.
786  * \param[in]  rpost     Reference position type to use (NULL = default).
787  * \param[in]  scanner   Scanner data structure.
788  * \returns    The created selection element.
789  *
790  * This function handles the creation of a gmx::SelectionTreeElement object for
791  * keyword string matching.
792  */
793 SelectionTreeElementPointer
794 _gmx_sel_init_keyword_strmatch(gmx_ana_selmethod_t *method,
795                                gmx::SelectionStringMatchType matchType,
796                                gmx::SelectionParserValueListPointer args,
797                                const char *rpost, yyscan_t scanner)
798 {
799     GMX_RELEASE_ASSERT(method->type == STR_VALUE,
800                        "String keyword method called for a non-string-valued method");
801     GMX_RELEASE_ASSERT(args && !args->empty(),
802                        "String keyword matching method called without any values");
803     return init_keyword_internal(method, matchType, move(args), rpost, scanner);
804 }
805
806 /*!
807  * \param[in]  method Method to use for initialization.
808  * \param[in]  group  Selection in which the keyword should be evaluated.
809  * \param[in]  rpost  Reference position type to use (NULL = default).
810  * \param[in]  scanner Scanner data structure.
811  * \returns    The created selection element.
812  *
813  * This function handles the creation of a gmx::SelectionTreeElement object for
814  * expressions like "z of ...".
815  */
816 SelectionTreeElementPointer
817 _gmx_sel_init_keyword_of(gmx_ana_selmethod_t                    *method,
818                          const gmx::SelectionTreeElementPointer &group,
819                          const char *rpost, yyscan_t scanner)
820 {
821     gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
822     gmx::MessageStringContext    context(errors, formatCurrentErrorContext(scanner));
823
824     GMX_UNUSED_VALUE(rpost);
825     return _gmx_sel_init_keyword_evaluator(method, group, scanner);
826 }
827
828 /*!
829  * \param[in]  method Method to use for initialization.
830  * \param[in]  params Pointer to the first parameter.
831  * \param[in]  rpost  Reference position type to use (NULL = default).
832  * \param[in]  scanner Scanner data structure.
833  * \returns    The created selection element.
834  *
835  * This function handles the creation of a gmx::SelectionTreeElement object for
836  * selection methods that take parameters.
837  *
838  * Part of the behavior of the \c same selection keyword is hardcoded into
839  * this function (or rather, into _gmx_selelem_custom_init_same()) to allow the
840  * use of any keyword in \c "same KEYWORD as" without requiring special
841  * handling somewhere else (or sacrificing the simple syntax).
842  */
843 SelectionTreeElementPointer
844 _gmx_sel_init_method(gmx_ana_selmethod_t                      *method,
845                      gmx::SelectionParserParameterListPointer  params,
846                      const char *rpost, yyscan_t scanner)
847 {
848     gmx_ana_selcollection_t     *sc = _gmx_sel_lexer_selcollection(scanner);
849
850     gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
851     gmx::MessageStringContext    context(errors, formatCurrentErrorContext(scanner));
852
853     _gmx_sel_finish_method(scanner);
854     /* The "same" keyword needs some custom massaging of the parameters. */
855     _gmx_selelem_custom_init_same(&method, params, scanner);
856     SelectionTreeElementPointer root(
857             new SelectionTreeElement(
858                     SEL_EXPRESSION, _gmx_sel_lexer_get_current_location(scanner)));
859     _gmx_selelem_set_method(root, method, scanner);
860     /* Process the parameters */
861     _gmx_sel_parse_params(*params, root->u.expr.method->nparams,
862                           root->u.expr.method->param, root, scanner);
863     set_refpos_type(&sc->pcc, root, rpost);
864
865     return root;
866 }
867
868 /*!
869  * \param[in]  method Modifier to use for initialization.
870  * \param[in]  params Pointer to the first parameter.
871  * \param[in]  sel    Selection element that the modifier should act on.
872  * \param[in]  scanner Scanner data structure.
873  * \returns    The created selection element.
874  *
875  * This function handles the creation of a gmx::SelectionTreeElement object for
876  * selection modifiers.
877  */
878 SelectionTreeElementPointer
879 _gmx_sel_init_modifier(gmx_ana_selmethod_t                      *method,
880                        gmx::SelectionParserParameterListPointer  params,
881                        const gmx::SelectionTreeElementPointer   &sel,
882                        yyscan_t                                  scanner)
883 {
884     gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
885     gmx::MessageStringContext    context(errors, formatCurrentErrorContext(scanner));
886
887     _gmx_sel_finish_method(scanner);
888     SelectionTreeElementPointer modifier(
889             new SelectionTreeElement(
890                     SEL_MODIFIER, _gmx_sel_lexer_get_current_location(scanner)));
891     _gmx_selelem_set_method(modifier, method, scanner);
892     SelectionTreeElementPointer root;
893     if (method->type == NO_VALUE)
894     {
895         SelectionTreeElementPointer child = sel;
896         while (child->next)
897         {
898             child = child->next;
899         }
900         child->next = modifier;
901         root        = sel;
902     }
903     else
904     {
905         params->push_front(
906                 SelectionParserParameter::createFromExpression(NULL, sel));
907         root = modifier;
908     }
909     /* Process the parameters */
910     _gmx_sel_parse_params(*params, modifier->u.expr.method->nparams,
911                           modifier->u.expr.method->param, modifier, scanner);
912
913     return root;
914 }
915
916 /*!
917  * \param[in]  expr    Input selection element for the position calculation.
918  * \param[in]  type    Reference position type or NULL for default.
919  * \param[in]  scanner Scanner data structure.
920  * \returns    The created selection element.
921  *
922  * This function handles the creation of a gmx::SelectionTreeElement object for
923  * evaluation of reference positions.
924  */
925 SelectionTreeElementPointer
926 _gmx_sel_init_position(const gmx::SelectionTreeElementPointer &expr,
927                        const char *type, yyscan_t scanner)
928 {
929     gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
930     gmx::MessageStringContext    context(errors, formatCurrentErrorContext(scanner));
931
932     SelectionTreeElementPointer  root(
933             new SelectionTreeElement(
934                     SEL_EXPRESSION, _gmx_sel_lexer_get_current_location(scanner)));
935     _gmx_selelem_set_method(root, &sm_keyword_pos, scanner);
936     _gmx_selelem_set_kwpos_type(root.get(), type);
937     /* Create the parameters for the parameter parser. */
938     SelectionParserParameterList params;
939     params.push_back(SelectionParserParameter::createFromExpression(NULL, expr));
940     /* Parse the parameters. */
941     _gmx_sel_parse_params(params, root->u.expr.method->nparams,
942                           root->u.expr.method->param, root, scanner);
943
944     return root;
945 }
946
947 /*!
948  * \param[in] x,y,z   Coordinates for the position.
949  * \param[in] scanner Scanner data structure.
950  * \returns   The creates selection element.
951  */
952 SelectionTreeElementPointer
953 _gmx_sel_init_const_position(real x, real y, real z, yyscan_t scanner)
954 {
955     rvec                        pos;
956
957     SelectionTreeElementPointer sel(
958             new SelectionTreeElement(
959                     SEL_CONST, _gmx_sel_lexer_get_current_location(scanner)));
960     _gmx_selelem_set_vtype(sel, POS_VALUE);
961     _gmx_selvalue_reserve(&sel->v, 1);
962     pos[XX] = x;
963     pos[YY] = y;
964     pos[ZZ] = z;
965     gmx_ana_pos_init_const(sel->v.u.p, pos);
966     return sel;
967 }
968
969 /*!
970  * \param[in] name  Name of an index group to search for.
971  * \param[in] scanner Scanner data structure.
972  * \returns   The created selection element.
973  *
974  * See gmx_ana_indexgrps_find() for information on how \p name is matched
975  * against the index groups.
976  */
977 SelectionTreeElementPointer
978 _gmx_sel_init_group_by_name(const char *name, yyscan_t scanner)
979 {
980
981     SelectionTreeElementPointer sel(
982             new SelectionTreeElement(
983                     SEL_GROUPREF, _gmx_sel_lexer_get_current_location(scanner)));
984     _gmx_selelem_set_vtype(sel, GROUP_VALUE);
985     sel->setName(gmx::formatString("group \"%s\"", name));
986     sel->u.gref.name = gmx_strdup(name);
987     sel->u.gref.id   = -1;
988
989     if (_gmx_sel_lexer_has_groups_set(scanner))
990     {
991         gmx_ana_indexgrps_t     *grps = _gmx_sel_lexer_indexgrps(scanner);
992         gmx_ana_selcollection_t *sc   = _gmx_sel_lexer_selcollection(scanner);
993         sel->resolveIndexGroupReference(grps, sc->gall.isize);
994     }
995
996     return sel;
997 }
998
999 /*!
1000  * \param[in] id    Zero-based index number of the group to extract.
1001  * \param[in] scanner Scanner data structure.
1002  * \returns   The created selection element.
1003  */
1004 SelectionTreeElementPointer
1005 _gmx_sel_init_group_by_id(int id, yyscan_t scanner)
1006 {
1007     SelectionTreeElementPointer sel(
1008             new SelectionTreeElement(
1009                     SEL_GROUPREF, _gmx_sel_lexer_get_current_location(scanner)));
1010     _gmx_selelem_set_vtype(sel, GROUP_VALUE);
1011     sel->setName(gmx::formatString("group %d", id));
1012     sel->u.gref.name = NULL;
1013     sel->u.gref.id   = id;
1014
1015     if (_gmx_sel_lexer_has_groups_set(scanner))
1016     {
1017         gmx_ana_indexgrps_t     *grps = _gmx_sel_lexer_indexgrps(scanner);
1018         gmx_ana_selcollection_t *sc   = _gmx_sel_lexer_selcollection(scanner);
1019         sel->resolveIndexGroupReference(grps, sc->gall.isize);
1020     }
1021
1022     return sel;
1023 }
1024
1025 /*!
1026  * \param[in,out] sel      Value of the variable.
1027  * \param         scanner  Scanner data structure.
1028  * \returns       The created selection element that references \p sel.
1029  *
1030  * The reference count of \p sel is updated, but no other modifications are
1031  * made.
1032  */
1033 SelectionTreeElementPointer
1034 _gmx_sel_init_variable_ref(const gmx::SelectionTreeElementPointer &sel,
1035                            yyscan_t                                scanner)
1036 {
1037     SelectionTreeElementPointer ref;
1038
1039     if (sel->v.type == POS_VALUE && sel->type == SEL_CONST)
1040     {
1041         ref = sel;
1042     }
1043     else
1044     {
1045         ref.reset(new SelectionTreeElement(
1046                           SEL_SUBEXPRREF, _gmx_sel_lexer_get_current_location(scanner)));
1047         _gmx_selelem_set_vtype(ref, sel->v.type);
1048         ref->setName(sel->name());
1049         ref->child = sel;
1050     }
1051     return ref;
1052 }
1053
1054 /*!
1055  * \param[in]  name     Name for the selection
1056  *     (if NULL, a default name is constructed).
1057  * \param[in]  sel      The selection element that evaluates the selection.
1058  * \param      scanner  Scanner data structure.
1059  * \returns    The created root selection element.
1060  *
1061  * This function handles the creation of root (\ref SEL_ROOT)
1062  * gmx::SelectionTreeElement objects for selections.
1063  */
1064 SelectionTreeElementPointer
1065 _gmx_sel_init_selection(const char                             *name,
1066                         const gmx::SelectionTreeElementPointer &sel,
1067                         yyscan_t                                scanner)
1068 {
1069     if (sel->v.type != POS_VALUE)
1070     {
1071         /* FIXME: Better handling of this error */
1072         GMX_THROW(gmx::InternalError(
1073                           "Each selection must evaluate to a position"));
1074     }
1075
1076     SelectionTreeElementPointer root(
1077             new SelectionTreeElement(
1078                     SEL_ROOT, _gmx_sel_lexer_get_current_location(scanner)));
1079     root->child = sel;
1080     if (name)
1081     {
1082         root->setName(name);
1083     }
1084     /* Update the flags */
1085     _gmx_selelem_update_flags(root);
1086     gmx::ExceptionInitializer errors("Invalid index group reference(s)");
1087     root->checkUnsortedAtoms(true, &errors);
1088     if (errors.hasNestedExceptions())
1089     {
1090         GMX_THROW(gmx::InconsistentInputError(errors));
1091     }
1092
1093     root->fillNameIfMissing(_gmx_sel_lexer_pselstr(scanner));
1094
1095     /* Print out some information if the parser is interactive */
1096     if (_gmx_sel_is_lexer_interactive(scanner))
1097     {
1098         fprintf(stderr, "Selection '%s' parsed\n",
1099                 _gmx_sel_lexer_pselstr(scanner));
1100     }
1101
1102     return root;
1103 }
1104
1105
1106 /*!
1107  * \param[in]  name     Name of the variable.
1108  * \param[in]  expr     The selection element that evaluates the variable.
1109  * \param      scanner  Scanner data structure.
1110  * \returns    The created root selection element.
1111  *
1112  * This function handles the creation of root gmx::SelectionTreeElement objects
1113  * for variable assignments. A \ref SEL_ROOT element and a \ref SEL_SUBEXPR
1114  * element are both created.
1115  */
1116 SelectionTreeElementPointer
1117 _gmx_sel_assign_variable(const char                             *name,
1118                          const gmx::SelectionTreeElementPointer &expr,
1119                          yyscan_t                                scanner)
1120 {
1121     gmx_ana_selcollection_t     *sc      = _gmx_sel_lexer_selcollection(scanner);
1122     const char                  *pselstr = _gmx_sel_lexer_pselstr(scanner);
1123     SelectionTreeElementPointer  root;
1124
1125     _gmx_selelem_update_flags(expr);
1126     /* Check if this is a constant non-group value */
1127     if (expr->type == SEL_CONST && expr->v.type != GROUP_VALUE)
1128     {
1129         /* If so, just assign the constant value to the variable */
1130         sc->symtab->addVariable(name, expr);
1131     }
1132     /* Check if we are assigning a variable to another variable */
1133     else if (expr->type == SEL_SUBEXPRREF)
1134     {
1135         /* If so, make a simple alias */
1136         sc->symtab->addVariable(name, expr->child);
1137     }
1138     else
1139     {
1140         SelectionLocation location(_gmx_sel_lexer_get_current_location(scanner));
1141         /* Create the root element */
1142         root.reset(new SelectionTreeElement(SEL_ROOT, location));
1143         root->setName(name);
1144         /* Create the subexpression element */
1145         root->child.reset(new SelectionTreeElement(SEL_SUBEXPR, location));
1146         root->child->setName(name);
1147         _gmx_selelem_set_vtype(root->child, expr->v.type);
1148         root->child->child  = expr;
1149         /* Update flags */
1150         _gmx_selelem_update_flags(root);
1151         gmx::ExceptionInitializer errors("Invalid index group reference(s)");
1152         root->checkUnsortedAtoms(true, &errors);
1153         if (errors.hasNestedExceptions())
1154         {
1155             GMX_THROW(gmx::InconsistentInputError(errors));
1156         }
1157         /* Add the variable to the symbol table */
1158         sc->symtab->addVariable(name, root->child);
1159     }
1160     srenew(sc->varstrs, sc->nvars + 1);
1161     sc->varstrs[sc->nvars] = gmx_strdup(pselstr);
1162     ++sc->nvars;
1163     if (_gmx_sel_is_lexer_interactive(scanner))
1164     {
1165         fprintf(stderr, "Variable '%s' parsed\n", pselstr);
1166     }
1167     return root;
1168 }
1169
1170 /*!
1171  * \param         sel   Selection to append (can be NULL, in which
1172  *   case nothing is done).
1173  * \param         last  Last selection, or NULL if not present or not known.
1174  * \param         scanner  Scanner data structure.
1175  * \returns       The last selection after the append.
1176  *
1177  * Appends \p sel after the last root element, and returns either \p sel
1178  * (if it was non-NULL) or the last element (if \p sel was NULL).
1179  */
1180 SelectionTreeElementPointer
1181 _gmx_sel_append_selection(const gmx::SelectionTreeElementPointer &sel,
1182                           gmx::SelectionTreeElementPointer        last,
1183                           yyscan_t                                scanner)
1184 {
1185     gmx_ana_selcollection_t *sc = _gmx_sel_lexer_selcollection(scanner);
1186
1187     /* Append sel after last, or the last element of sc if last is NULL */
1188     if (last)
1189     {
1190         last->next = sel;
1191     }
1192     else
1193     {
1194         if (sc->root)
1195         {
1196             last = sc->root;
1197             while (last->next)
1198             {
1199                 last = last->next;
1200             }
1201             last->next = sel;
1202         }
1203         else
1204         {
1205             sc->root = sel;
1206         }
1207     }
1208     /* Initialize a selection object if necessary */
1209     if (sel)
1210     {
1211         last = sel;
1212         /* Add the new selection to the collection if it is not a variable. */
1213         if (sel->child->type != SEL_SUBEXPR)
1214         {
1215             gmx::SelectionDataPointer selPtr(
1216                     new gmx::internal::SelectionData(
1217                             sel.get(), _gmx_sel_lexer_pselstr(scanner)));
1218             sc->sel.push_back(gmx::move(selPtr));
1219         }
1220     }
1221     /* Clear the selection string now that we've saved it */
1222     _gmx_sel_lexer_clear_pselstr(scanner);
1223     return last;
1224 }
1225
1226 /*!
1227  * \param[in] scanner Scanner data structure.
1228  * \returns   true if the parser should finish, false if parsing should
1229  *   continue.
1230  *
1231  * This function is called always after _gmx_sel_append_selection() to
1232  * check whether a sufficient number of selections has already been provided.
1233  * This is used to terminate interactive parsers when the correct number of
1234  * selections has been provided.
1235  */
1236 bool
1237 _gmx_sel_parser_should_finish(yyscan_t scanner)
1238 {
1239     gmx_ana_selcollection_t *sc = _gmx_sel_lexer_selcollection(scanner);
1240     return (int)sc->sel.size() == _gmx_sel_lexer_exp_selcount(scanner);
1241 }