2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
37 * Implements functions in parsetree.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_selection
43 * \page page_module_selection_parser Selection parsing
45 * The selection parser is implemented in the following files:
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.
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.
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().
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
88 * The rest of this page describes the resulting gmx::SelectionTreeElement
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
95 * \section selparser_tree Element tree constructed by the parser
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
104 * Fields that are not initialized are set to zero, NULL, or other similar
108 * \subsection selparser_tree_root Root elements
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.
117 * The \ref SEL_ROOT elements are linked together in a chain in the same order
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.
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.
137 * \subsection selparser_tree_const Constant elements
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
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.
154 * \subsection selparser_tree_method Method evaluation elements
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
161 * If a non-standard reference position type was set,
162 * gmx::SelectionTreeElement::pc has also been created, but only the type has
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.
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").
177 * \subsection selparser_tree_subexpr Subexpression elements
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
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
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
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.
203 * \subsection selparser_tree_bool Boolean elements
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
214 * \subsection selparser_tree_arith Arithmetic elements
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.
224 #include "parsetree.h"
229 #include <boost/exception_ptr.hpp>
230 #include <boost/shared_ptr.hpp>
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"
240 #include "keywords.h"
243 #include "selectioncollection-impl.h"
245 #include "selmethod.h"
248 using gmx::SelectionParserValue;
249 using gmx::SelectionParserValueList;
250 using gmx::SelectionParserValueListPointer;
251 using gmx::SelectionParserParameter;
252 using gmx::SelectionParserParameterList;
253 using gmx::SelectionParserParameterListPointer;
254 using gmx::SelectionParserValue;
255 using gmx::SelectionTreeElement;
256 using gmx::SelectionTreeElementPointer;
262 * Formats context string for errors.
264 * The returned string is used as the context for errors reported during
268 formatCurrentErrorContext(yyscan_t scanner)
270 return gmx::formatString(
271 "While parsing '%s'",
272 _gmx_sel_lexer_get_current_text(scanner).c_str());
278 _gmx_selparser_error(yyscan_t scanner, const char *fmt, ...)
280 gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
281 // FIXME: Use an arbitrary length buffer.
285 vsnprintf(buf, 1024, fmt, ap);
291 _gmx_selparser_handle_exception(yyscan_t scanner, std::exception *ex)
295 bool canContinue = false;
296 gmx::GromacsException *gromacsException
297 = dynamic_cast<gmx::GromacsException *>(ex);
298 if (gromacsException != NULL)
300 gromacsException->prependContext(formatCurrentErrorContext(scanner));
301 canContinue = (dynamic_cast<gmx::UserInputError *>(ex) != NULL);
303 _gmx_sel_lexer_set_exception(scanner, boost::current_exception());
306 catch (const std::exception &)
308 _gmx_sel_lexer_set_exception(scanner, boost::current_exception());
314 _gmx_selparser_handle_error(yyscan_t scanner)
316 std::string context(gmx::formatString("In selection '%s'",
317 _gmx_sel_lexer_pselstr(scanner)));
318 // The only way to prepend context to the exception is to rethrow it.
321 _gmx_sel_lexer_rethrow_exception_if_occurred(scanner);
323 catch (gmx::UserInputError &ex)
325 ex.prependContext(context);
326 if (_gmx_sel_is_lexer_interactive(scanner))
328 gmx::formatExceptionMessageToFile(stderr, ex);
333 catch (gmx::GromacsException &ex)
335 ex.prependContext(context);
338 // Do legacy processing for non-exception cases.
339 // This is only reached if there was no active exception.
340 _gmx_selparser_error(scanner, "Invalid selection '%s'",
341 _gmx_sel_lexer_pselstr(scanner));
342 return _gmx_sel_is_lexer_interactive(scanner);
348 /********************************************************************
349 * SelectionParserValue
352 SelectionParserValue::SelectionParserValue(e_selvalue_t type)
355 memset(&u, 0, sizeof(u));
358 SelectionParserValue::SelectionParserValue(
359 const SelectionTreeElementPointer &expr)
360 : type(expr->v.type), expr(expr)
362 memset(&u, 0, sizeof(u));
365 /********************************************************************
366 * SelectionParserParameter
369 SelectionParserParameter::SelectionParserParameter(
371 SelectionParserValueListPointer values)
372 : name_(name != NULL ? name : ""),
373 values_(values ? move(values)
374 : SelectionParserValueListPointer(new SelectionParserValueList))
381 * \param[in,out] sel Root of the selection element tree to initialize.
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
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.
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.
398 _gmx_selelem_update_flags(const gmx::SelectionTreeElementPointer &sel)
400 bool bUseChildType = false;
401 bool bOnlySingleChildren;
403 /* Return if the flags have already been set */
404 if (sel->flags & SEL_FLAGSSET)
408 /* Set the flags based on the current element type */
413 sel->flags |= SEL_SINGLEVAL;
414 bUseChildType = false;
418 if (sel->u.expr.method->flags & SMETH_DYNAMIC)
420 sel->flags |= SEL_DYNAMIC;
422 if (sel->u.expr.method->flags & SMETH_SINGLEVAL)
424 sel->flags |= SEL_SINGLEVAL;
426 else if (sel->u.expr.method->flags & SMETH_VARNUMVAL)
428 sel->flags |= SEL_VARNUMVAL;
432 sel->flags |= SEL_ATOMVAL;
434 bUseChildType = false;
438 sel->flags |= SEL_ATOMVAL;
439 bUseChildType = false;
443 if (sel->v.type != NO_VALUE)
445 sel->flags |= SEL_VARNUMVAL;
447 bUseChildType = false;
451 bUseChildType = false;
457 bUseChildType = true;
460 /* Loop through children to propagate their flags upwards */
461 bOnlySingleChildren = true;
462 SelectionTreeElementPointer child = sel->child;
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 */
472 if ((sel->flags & SEL_VALTYPEMASK)
473 && !(sel->flags & child->flags & SEL_VALTYPEMASK))
475 // TODO: Recollect when this is triggered, and whether the type
477 GMX_THROW(gmx::InvalidInputError("Invalid combination of selection expressions"));
479 sel->flags |= (child->flags & SEL_VALTYPEMASK);
481 if (!(child->flags & SEL_SINGLEVAL))
483 bOnlySingleChildren = false;
488 /* For arithmetic expressions consisting only of single values,
489 * the result is also a single value. */
490 if (sel->type == SEL_ARITHMETIC && bOnlySingleChildren)
492 sel->flags = (sel->flags & ~SEL_VALTYPEMASK) | SEL_SINGLEVAL;
494 /* For root elements, the type should be propagated here, after the
495 * children have been updated. */
496 if (sel->type == SEL_ROOT)
498 GMX_ASSERT(sel->child, "Root elements should always have a child");
499 sel->flags |= (sel->child->flags & SEL_VALTYPEMASK);
501 /* Mark that the flags are set */
502 sel->flags |= SEL_FLAGSSET;
506 * \param[in,out] sel Selection element to initialize.
507 * \param[in] scanner Scanner data structure.
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.
514 _gmx_selelem_init_method_params(const gmx::SelectionTreeElementPointer &sel,
518 gmx_ana_selparam_t *orgparam;
519 gmx_ana_selparam_t *param;
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)
529 param[i].flags &= ~SPAR_SET;
530 _gmx_selvalue_setstore(¶m[i].val, NULL);
531 if (param[i].flags & SPAR_VARNUM)
533 param[i].val.nr = -1;
535 /* Duplicate the enum value array if it is given statically */
536 if ((param[i].flags & SPAR_ENUMVAL) && orgparam[i].val.u.ptr != NULL)
540 /* Count the values */
542 while (orgparam[i].val.u.s[n] != NULL)
546 _gmx_selvalue_reserve(¶m[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]));
552 if (sel->u.expr.method->init_data)
554 mdata = sel->u.expr.method->init_data(nparams, param);
556 if (sel->u.expr.method->set_poscoll)
558 gmx_ana_selcollection_t *sc = _gmx_sel_lexer_selcollection(scanner);
560 sel->u.expr.method->set_poscoll(&sc->pcc, mdata);
562 /* Store the values */
563 sel->u.expr.method->param = param;
564 sel->u.expr.mdata = mdata;
568 * \param[in,out] sel Selection element to initialize.
569 * \param[in] method Selection method to set.
570 * \param[in] scanner Scanner data structure.
572 * Makes a copy of \p method and stores it in \p sel->u.expr.method,
573 * and calls _gmx_selelem_init_method_params();
576 _gmx_selelem_set_method(const gmx::SelectionTreeElementPointer &sel,
577 gmx_ana_selmethod_t *method,
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);
588 * Initializes the reference position calculation for a \ref SEL_EXPRESSION
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 * \param[in] scanner Scanner data structure.
595 * \returns 0 on success, a non-zero error code on error.
598 set_refpos_type(gmx::PositionCalculationCollection *pcc,
599 const SelectionTreeElementPointer &sel,
600 const char *rpost, yyscan_t scanner)
607 if (sel->u.expr.method->pupdate)
609 /* By default, use whole residues/molecules. */
611 = pcc->createCalculationFromEnum(rpost, POS_COMPLWHOLE);
615 // TODO: Should this be treated as a real error?
616 _gmx_selparser_error(scanner, "modifier '%s' is not applicable for '%s'",
617 rpost, sel->u.expr.method->name);
621 gmx::SelectionTreeElementPointer
622 _gmx_sel_init_arithmetic(const gmx::SelectionTreeElementPointer &left,
623 const gmx::SelectionTreeElementPointer &right,
624 char op, yyscan_t /* scanner */)
626 SelectionTreeElementPointer sel(new SelectionTreeElement(SEL_ARITHMETIC));
627 sel->v.type = REAL_VALUE;
630 case '+': sel->u.arith.type = ARITH_PLUS; break;
631 case '-': sel->u.arith.type = (right ? ARITH_MINUS : ARITH_NEG); break;
632 case '*': sel->u.arith.type = ARITH_MULT; break;
633 case '/': sel->u.arith.type = ARITH_DIV; break;
634 case '^': sel->u.arith.type = ARITH_EXP; break;
640 sel->u.arith.opstr = gmx_strdup(buf);
642 sel->child->next = right;
647 * \param[in] left Selection element for the left hand side.
648 * \param[in] right Selection element for the right hand side.
649 * \param[in] cmpop String representation of the comparison operator.
650 * \param[in] scanner Scanner data structure.
651 * \returns The created selection element.
653 * This function handles the creation of a gmx::SelectionTreeElement object for
654 * comparison expressions.
656 SelectionTreeElementPointer
657 _gmx_sel_init_comparison(const gmx::SelectionTreeElementPointer &left,
658 const gmx::SelectionTreeElementPointer &right,
659 const char *cmpop, yyscan_t scanner)
661 gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
662 gmx::MessageStringContext context(errors, formatCurrentErrorContext(scanner));
664 SelectionTreeElementPointer sel(new SelectionTreeElement(SEL_EXPRESSION));
665 _gmx_selelem_set_method(sel, &sm_compare, scanner);
667 SelectionParserParameterList params;
669 // Create the parameter for the left expression.
670 name = left->v.type == INT_VALUE ? "int1" : "real1";
671 params.push_back(SelectionParserParameter::createFromExpression(name, left));
672 // Create the parameter for the right expression.
673 name = right->v.type == INT_VALUE ? "int2" : "real2";
674 params.push_back(SelectionParserParameter::createFromExpression(name, right));
675 // Create the parameter for the operator.
677 SelectionParserParameter::create(
678 "op", SelectionParserValue::createString(cmpop)));
679 if (!_gmx_sel_parse_params(params, sel->u.expr.method->nparams,
680 sel->u.expr.method->param, sel, scanner))
682 return SelectionTreeElementPointer();
689 * Implementation method for keyword expression creation.
691 * \param[in] method Method to use.
692 * \param[in] matchType String matching type (only used if \p method is
693 * a string keyword and \p args is not empty.
694 * \param[in] args Pointer to the first argument.
695 * \param[in] rpost Reference position type to use (NULL = default).
696 * \param[in] scanner Scanner data structure.
697 * \returns The created selection element.
699 * This function handles the creation of a gmx::SelectionTreeElement object for
700 * selection methods that do not take parameters.
702 static SelectionTreeElementPointer
703 init_keyword_internal(gmx_ana_selmethod_t *method,
704 gmx::SelectionStringMatchType matchType,
705 SelectionParserValueListPointer args,
706 const char *rpost, yyscan_t scanner)
708 gmx_ana_selcollection_t *sc = _gmx_sel_lexer_selcollection(scanner);
710 gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
711 gmx::MessageStringContext context(errors, formatCurrentErrorContext(scanner));
713 if (method->nparams > 0)
715 // TODO: Would assert be better?
716 GMX_THROW(gmx::InternalError(
717 "Keyword initialization called with non-keyword method"));
720 SelectionTreeElementPointer root(new SelectionTreeElement(SEL_EXPRESSION));
721 SelectionTreeElementPointer child = root;
722 _gmx_selelem_set_method(child, method, scanner);
724 /* Initialize the evaluation of keyword matching if values are provided */
727 gmx_ana_selmethod_t *kwmethod;
728 switch (method->type)
730 case INT_VALUE: kwmethod = &sm_keyword_int; break;
731 case REAL_VALUE: kwmethod = &sm_keyword_real; break;
732 case STR_VALUE: kwmethod = &sm_keyword_str; break;
734 GMX_THROW(gmx::InternalError(
735 "Unknown type for keyword selection"));
737 /* Initialize the selection element */
738 root.reset(new SelectionTreeElement(SEL_EXPRESSION));
739 _gmx_selelem_set_method(root, kwmethod, scanner);
740 if (method->type == STR_VALUE)
742 _gmx_selelem_set_kwstr_match_type(root, matchType);
744 SelectionParserParameterList params;
746 SelectionParserParameter::createFromExpression(NULL, child));
747 params.push_back(SelectionParserParameter::create(NULL, move(args)));
748 if (!_gmx_sel_parse_params(params, root->u.expr.method->nparams,
749 root->u.expr.method->param, root, scanner))
751 return SelectionTreeElementPointer();
754 set_refpos_type(&sc->pcc, child, rpost, scanner);
760 * \param[in] method Method to use.
761 * \param[in] args Pointer to the first argument.
762 * \param[in] rpost Reference position type to use (NULL = default).
763 * \param[in] scanner Scanner data structure.
764 * \returns The created selection element.
766 * This function handles the creation of a gmx::SelectionTreeElement object for
767 * selection methods that do not take parameters.
769 SelectionTreeElementPointer
770 _gmx_sel_init_keyword(gmx_ana_selmethod_t *method,
771 gmx::SelectionParserValueListPointer args,
772 const char *rpost, yyscan_t scanner)
774 return init_keyword_internal(method, gmx::eStringMatchType_Auto, move(args),
779 * \param[in] method Method to use.
780 * \param[in] matchType String matching type.
781 * \param[in] args Pointer to the first argument.
782 * \param[in] rpost Reference position type to use (NULL = default).
783 * \param[in] scanner Scanner data structure.
784 * \returns The created selection element.
786 * This function handles the creation of a gmx::SelectionTreeElement object for
787 * keyword string matching.
789 SelectionTreeElementPointer
790 _gmx_sel_init_keyword_strmatch(gmx_ana_selmethod_t *method,
791 gmx::SelectionStringMatchType matchType,
792 gmx::SelectionParserValueListPointer args,
793 const char *rpost, yyscan_t scanner)
795 GMX_RELEASE_ASSERT(method->type == STR_VALUE,
796 "String keyword method called for a non-string-valued method");
797 GMX_RELEASE_ASSERT(args && !args->empty(),
798 "String keyword matching method called without any values");
799 return init_keyword_internal(method, matchType, move(args), rpost, scanner);
803 * \param[in] method Method to use for initialization.
804 * \param[in] group Selection in which the keyword should be evaluated.
805 * \param[in] rpost Reference position type to use (NULL = default).
806 * \param[in] scanner Scanner data structure.
807 * \returns The created selection element.
809 * This function handles the creation of a gmx::SelectionTreeElement object for
810 * expressions like "z of ...".
812 SelectionTreeElementPointer
813 _gmx_sel_init_keyword_of(gmx_ana_selmethod_t *method,
814 const gmx::SelectionTreeElementPointer &group,
815 const char *rpost, yyscan_t scanner)
817 gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
818 gmx::MessageStringContext context(errors, formatCurrentErrorContext(scanner));
820 GMX_UNUSED_VALUE(rpost);
821 return _gmx_sel_init_keyword_evaluator(method, group, scanner);
825 * \param[in] method Method to use for initialization.
826 * \param[in] params Pointer to the first parameter.
827 * \param[in] rpost Reference position type to use (NULL = default).
828 * \param[in] scanner Scanner data structure.
829 * \returns The created selection element.
831 * This function handles the creation of a gmx::SelectionTreeElement object for
832 * selection methods that take parameters.
834 * Part of the behavior of the \c same selection keyword is hardcoded into
835 * this function (or rather, into _gmx_selelem_custom_init_same()) to allow the
836 * use of any keyword in \c "same KEYWORD as" without requiring special
837 * handling somewhere else (or sacrificing the simple syntax).
839 SelectionTreeElementPointer
840 _gmx_sel_init_method(gmx_ana_selmethod_t *method,
841 gmx::SelectionParserParameterListPointer params,
842 const char *rpost, yyscan_t scanner)
844 gmx_ana_selcollection_t *sc = _gmx_sel_lexer_selcollection(scanner);
847 gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
848 gmx::MessageStringContext context(errors, formatCurrentErrorContext(scanner));
850 _gmx_sel_finish_method(scanner);
851 /* The "same" keyword needs some custom massaging of the parameters. */
852 rc = _gmx_selelem_custom_init_same(&method, params, scanner);
855 return SelectionTreeElementPointer();
857 SelectionTreeElementPointer root(new SelectionTreeElement(SEL_EXPRESSION));
858 _gmx_selelem_set_method(root, method, scanner);
859 /* Process the parameters */
860 if (!_gmx_sel_parse_params(*params, root->u.expr.method->nparams,
861 root->u.expr.method->param, root, scanner))
863 return SelectionTreeElementPointer();
865 set_refpos_type(&sc->pcc, root, rpost, scanner);
871 * \param[in] method Modifier to use for initialization.
872 * \param[in] params Pointer to the first parameter.
873 * \param[in] sel Selection element that the modifier should act on.
874 * \param[in] scanner Scanner data structure.
875 * \returns The created selection element.
877 * This function handles the creation of a gmx::SelectionTreeElement object for
878 * selection modifiers.
880 SelectionTreeElementPointer
881 _gmx_sel_init_modifier(gmx_ana_selmethod_t *method,
882 gmx::SelectionParserParameterListPointer params,
883 const gmx::SelectionTreeElementPointer &sel,
886 gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
887 gmx::MessageStringContext context(errors, formatCurrentErrorContext(scanner));
889 _gmx_sel_finish_method(scanner);
890 SelectionTreeElementPointer modifier(new SelectionTreeElement(SEL_MODIFIER));
891 _gmx_selelem_set_method(modifier, method, scanner);
892 SelectionTreeElementPointer root;
893 if (method->type == NO_VALUE)
895 SelectionTreeElementPointer child = sel;
900 child->next = modifier;
906 SelectionParserParameter::createFromExpression(NULL, sel));
909 /* Process the parameters */
910 if (!_gmx_sel_parse_params(*params, modifier->u.expr.method->nparams,
911 modifier->u.expr.method->param, modifier, scanner))
913 return SelectionTreeElementPointer();
920 * \param[in] expr Input selection element for the position calculation.
921 * \param[in] type Reference position type or NULL for default.
922 * \param[in] scanner Scanner data structure.
923 * \returns The created selection element.
925 * This function handles the creation of a gmx::SelectionTreeElement object for
926 * evaluation of reference positions.
928 SelectionTreeElementPointer
929 _gmx_sel_init_position(const gmx::SelectionTreeElementPointer &expr,
930 const char *type, yyscan_t scanner)
932 gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
933 gmx::MessageStringContext context(errors, formatCurrentErrorContext(scanner));
935 SelectionTreeElementPointer root(new SelectionTreeElement(SEL_EXPRESSION));
936 _gmx_selelem_set_method(root, &sm_keyword_pos, scanner);
937 _gmx_selelem_set_kwpos_type(root.get(), type);
938 /* Create the parameters for the parameter parser. */
939 SelectionParserParameterList params;
940 params.push_back(SelectionParserParameter::createFromExpression(NULL, expr));
941 /* Parse the parameters. */
942 if (!_gmx_sel_parse_params(params, root->u.expr.method->nparams,
943 root->u.expr.method->param, root, scanner))
945 return SelectionTreeElementPointer();
952 * \param[in] x,y,z Coordinates for the position.
953 * \returns The creates selection element.
955 SelectionTreeElementPointer
956 _gmx_sel_init_const_position(real x, real y, real z)
960 SelectionTreeElementPointer sel(new SelectionTreeElement(SEL_CONST));
961 _gmx_selelem_set_vtype(sel, POS_VALUE);
962 _gmx_selvalue_reserve(&sel->v, 1);
966 gmx_ana_pos_init_const(sel->v.u.p, pos);
971 * \param[in] name Name of an index group to search for.
972 * \param[in] scanner Scanner data structure.
973 * \returns The created selection element.
975 * See gmx_ana_indexgrps_find() for information on how \p name is matched
976 * against the index groups.
978 SelectionTreeElementPointer
979 _gmx_sel_init_group_by_name(const char *name, yyscan_t scanner)
982 SelectionTreeElementPointer sel(new SelectionTreeElement(SEL_GROUPREF));
983 _gmx_selelem_set_vtype(sel, GROUP_VALUE);
984 sel->setName(gmx::formatString("group \"%s\"", name));
985 sel->u.gref.name = gmx_strdup(name);
988 if (_gmx_sel_lexer_has_groups_set(scanner))
990 gmx_ana_indexgrps_t *grps = _gmx_sel_lexer_indexgrps(scanner);
991 gmx_ana_selcollection_t *sc = _gmx_sel_lexer_selcollection(scanner);
992 sel->resolveIndexGroupReference(grps, sc->gall.isize);
999 * \param[in] id Zero-based index number of the group to extract.
1000 * \param[in] scanner Scanner data structure.
1001 * \returns The created selection element.
1003 SelectionTreeElementPointer
1004 _gmx_sel_init_group_by_id(int id, yyscan_t scanner)
1006 SelectionTreeElementPointer sel(new SelectionTreeElement(SEL_GROUPREF));
1007 _gmx_selelem_set_vtype(sel, GROUP_VALUE);
1008 sel->setName(gmx::formatString("group %d", id));
1009 sel->u.gref.name = NULL;
1010 sel->u.gref.id = id;
1012 if (_gmx_sel_lexer_has_groups_set(scanner))
1014 gmx_ana_indexgrps_t *grps = _gmx_sel_lexer_indexgrps(scanner);
1015 gmx_ana_selcollection_t *sc = _gmx_sel_lexer_selcollection(scanner);
1016 sel->resolveIndexGroupReference(grps, sc->gall.isize);
1023 * \param[in,out] sel Value of the variable.
1024 * \returns The created selection element that references \p sel.
1026 * The reference count of \p sel is updated, but no other modifications are
1029 SelectionTreeElementPointer
1030 _gmx_sel_init_variable_ref(const gmx::SelectionTreeElementPointer &sel)
1032 SelectionTreeElementPointer ref;
1034 if (sel->v.type == POS_VALUE && sel->type == SEL_CONST)
1040 ref.reset(new SelectionTreeElement(SEL_SUBEXPRREF));
1041 _gmx_selelem_set_vtype(ref, sel->v.type);
1042 ref->setName(sel->name());
1049 * \param[in] name Name for the selection
1050 * (if NULL, a default name is constructed).
1051 * \param[in] sel The selection element that evaluates the selection.
1052 * \param scanner Scanner data structure.
1053 * \returns The created root selection element.
1055 * This function handles the creation of root (\ref SEL_ROOT)
1056 * gmx::SelectionTreeElement objects for selections.
1058 SelectionTreeElementPointer
1059 _gmx_sel_init_selection(const char *name,
1060 const gmx::SelectionTreeElementPointer &sel,
1063 if (sel->v.type != POS_VALUE)
1065 /* FIXME: Better handling of this error */
1066 GMX_THROW(gmx::InternalError(
1067 "Each selection must evaluate to a position"));
1070 SelectionTreeElementPointer root(new SelectionTreeElement(SEL_ROOT));
1074 root->setName(name);
1076 /* Update the flags */
1077 _gmx_selelem_update_flags(root);
1078 gmx::ExceptionInitializer errors("Invalid index group reference(s)");
1079 root->checkUnsortedAtoms(true, &errors);
1080 if (errors.hasNestedExceptions())
1082 GMX_THROW(gmx::InconsistentInputError(errors));
1085 root->fillNameIfMissing(_gmx_sel_lexer_pselstr(scanner));
1087 /* Print out some information if the parser is interactive */
1088 if (_gmx_sel_is_lexer_interactive(scanner))
1090 fprintf(stderr, "Selection '%s' parsed\n",
1091 _gmx_sel_lexer_pselstr(scanner));
1099 * \param[in] name Name of the variable.
1100 * \param[in] expr The selection element that evaluates the variable.
1101 * \param scanner Scanner data structure.
1102 * \returns The created root selection element.
1104 * This function handles the creation of root gmx::SelectionTreeElement objects
1105 * for variable assignments. A \ref SEL_ROOT element and a \ref SEL_SUBEXPR
1106 * element are both created.
1108 SelectionTreeElementPointer
1109 _gmx_sel_assign_variable(const char *name,
1110 const gmx::SelectionTreeElementPointer &expr,
1113 gmx_ana_selcollection_t *sc = _gmx_sel_lexer_selcollection(scanner);
1114 const char *pselstr = _gmx_sel_lexer_pselstr(scanner);
1115 SelectionTreeElementPointer root;
1117 _gmx_selelem_update_flags(expr);
1118 /* Check if this is a constant non-group value */
1119 if (expr->type == SEL_CONST && expr->v.type != GROUP_VALUE)
1121 /* If so, just assign the constant value to the variable */
1122 sc->symtab->addVariable(name, expr);
1124 /* Check if we are assigning a variable to another variable */
1125 else if (expr->type == SEL_SUBEXPRREF)
1127 /* If so, make a simple alias */
1128 sc->symtab->addVariable(name, expr->child);
1132 /* Create the root element */
1133 root.reset(new SelectionTreeElement(SEL_ROOT));
1134 root->setName(name);
1135 /* Create the subexpression element */
1136 root->child.reset(new SelectionTreeElement(SEL_SUBEXPR));
1137 root->child->setName(name);
1138 _gmx_selelem_set_vtype(root->child, expr->v.type);
1139 root->child->child = expr;
1141 _gmx_selelem_update_flags(root);
1142 gmx::ExceptionInitializer errors("Invalid index group reference(s)");
1143 root->checkUnsortedAtoms(true, &errors);
1144 if (errors.hasNestedExceptions())
1146 GMX_THROW(gmx::InconsistentInputError(errors));
1148 /* Add the variable to the symbol table */
1149 sc->symtab->addVariable(name, root->child);
1151 srenew(sc->varstrs, sc->nvars + 1);
1152 sc->varstrs[sc->nvars] = gmx_strdup(pselstr);
1154 if (_gmx_sel_is_lexer_interactive(scanner))
1156 fprintf(stderr, "Variable '%s' parsed\n", pselstr);
1162 * \param sel Selection to append (can be NULL, in which
1163 * case nothing is done).
1164 * \param last Last selection, or NULL if not present or not known.
1165 * \param scanner Scanner data structure.
1166 * \returns The last selection after the append.
1168 * Appends \p sel after the last root element, and returns either \p sel
1169 * (if it was non-NULL) or the last element (if \p sel was NULL).
1171 SelectionTreeElementPointer
1172 _gmx_sel_append_selection(const gmx::SelectionTreeElementPointer &sel,
1173 gmx::SelectionTreeElementPointer last,
1176 gmx_ana_selcollection_t *sc = _gmx_sel_lexer_selcollection(scanner);
1178 /* Append sel after last, or the last element of sc if last is NULL */
1199 /* Initialize a selection object if necessary */
1203 /* Add the new selection to the collection if it is not a variable. */
1204 if (sel->child->type != SEL_SUBEXPR)
1206 gmx::SelectionDataPointer selPtr(
1207 new gmx::internal::SelectionData(
1208 sel.get(), _gmx_sel_lexer_pselstr(scanner)));
1209 sc->sel.push_back(gmx::move(selPtr));
1212 /* Clear the selection string now that we've saved it */
1213 _gmx_sel_lexer_clear_pselstr(scanner);
1218 * \param[in] scanner Scanner data structure.
1219 * \returns true if the parser should finish, false if parsing should
1222 * This function is called always after _gmx_sel_append_selection() to
1223 * check whether a sufficient number of selections has already been provided.
1224 * This is used to terminate interactive parsers when the correct number of
1225 * selections has been provided.
1228 _gmx_sel_parser_should_finish(yyscan_t scanner)
1230 gmx_ana_selcollection_t *sc = _gmx_sel_lexer_selcollection(scanner);
1231 return (int)sc->sel.size() == _gmx_sel_lexer_exp_selcount(scanner);