Gromacs  2024.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Functions
#include "gmxpre.h"
#include "parsetree.h"
#include <cstdarg>
#include <cstdio>
#include <exception>
#include <memory>
#include "gromacs/selection/selection.h"
#include "gromacs/utility/cstringutil.h"
#include "gromacs/utility/exceptions.h"
#include "gromacs/utility/smalloc.h"
#include "gromacs/utility/stringutil.h"
#include "gromacs/utility/textwriter.h"
#include "keywords.h"
#include "poscalc.h"
#include "scanner.h"
#include "selectioncollection_impl.h"
#include "selelem.h"
#include "selmethod.h"
#include "symrec.h"
+ Include dependency graph for parsetree.cpp:

Description

Implements functions in parsetree.h.

Author
Teemu Murtola teemu.nosp@m..mur.nosp@m.tola@.nosp@m.gmai.nosp@m.l.com

Functions

std::string anonymous_namespace{parsetree.cpp}::formatCurrentErrorContext (yyscan_t scanner)
 Formats context string for errors. More...
 
bool _gmx_selparser_handle_exception (yyscan_t scanner, std::exception *ex)
 Handles exceptions caught within the Bison code. More...
 
bool _gmx_selparser_handle_error (yyscan_t scanner)
 Handles errors in the selection parser. More...
 
void _gmx_selelem_update_flags (const gmx::SelectionTreeElementPointer &sel)
 Propagates the flags for selection elements. More...
 
void _gmx_selelem_init_method_params (const gmx::SelectionTreeElementPointer &sel, yyscan_t scanner)
 Initializes the method parameter data of SEL_EXPRESSION and SEL_MODIFIER elements. More...
 
void _gmx_selelem_set_method (const gmx::SelectionTreeElementPointer &sel, gmx_ana_selmethod_t *method, yyscan_t scanner)
 Initializes the method for a SEL_EXPRESSION selection element. More...
 
static void set_refpos_type (gmx::PositionCalculationCollection *pcc, const SelectionTreeElementPointer &sel, const char *rpost)
 Initializes the reference position calculation for a SEL_EXPRESSION element. More...
 
gmx::SelectionTreeElementPointer _gmx_sel_init_arithmetic (const gmx::SelectionTreeElementPointer &left, const gmx::SelectionTreeElementPointer &right, char op, yyscan_t scanner)
 Creates a gmx::SelectionTreeElement for arithmetic expression evaluation. More...
 
SelectionTreeElementPointer _gmx_sel_init_comparison (const gmx::SelectionTreeElementPointer &left, const gmx::SelectionTreeElementPointer &right, const char *cmpop, yyscan_t scanner)
 Creates a gmx::SelectionTreeElement for comparsion expression evaluation. More...
 
static SelectionTreeElementPointer init_keyword_internal (gmx_ana_selmethod_t *method, gmx::SelectionStringMatchType matchType, SelectionParserValueListPointer args, const char *rpost, yyscan_t scanner)
 Implementation method for keyword expression creation. More...
 
SelectionTreeElementPointer _gmx_sel_init_keyword (gmx_ana_selmethod_t *method, gmx::SelectionParserValueListPointer args, const char *rpost, yyscan_t scanner)
 Creates a gmx::SelectionTreeElement for a keyword expression from the parsed data. More...
 
SelectionTreeElementPointer _gmx_sel_init_keyword_strmatch (gmx_ana_selmethod_t *method, gmx::SelectionStringMatchType matchType, gmx::SelectionParserValueListPointer args, const char *rpost, yyscan_t scanner)
 Creates a gmx::SelectionTreeElement for string-matching keyword expression. More...
 
SelectionTreeElementPointer _gmx_sel_init_keyword_of (gmx_ana_selmethod_t *method, const gmx::SelectionTreeElementPointer &group, const char *rpost, yyscan_t scanner)
 Creates a gmx::SelectionTreeElement for "keyword of" expression. More...
 
SelectionTreeElementPointer _gmx_sel_init_method (gmx_ana_selmethod_t *method, gmx::SelectionParserParameterListPointer params, const char *rpost, yyscan_t scanner)
 Creates a gmx::SelectionTreeElement for a method expression from the parsed data. More...
 
SelectionTreeElementPointer _gmx_sel_init_modifier (gmx_ana_selmethod_t *method, gmx::SelectionParserParameterListPointer params, const gmx::SelectionTreeElementPointer &sel, yyscan_t scanner)
 Creates a gmx::SelectionTreeElement for a modifier expression from the parsed data. More...
 
SelectionTreeElementPointer _gmx_sel_init_position (const gmx::SelectionTreeElementPointer &expr, const char *type, yyscan_t scanner)
 Creates a gmx::SelectionTreeElement for evaluation of reference positions. More...
 
SelectionTreeElementPointer _gmx_sel_init_const_position (real x, real y, real z, yyscan_t scanner)
 Creates a gmx::SelectionTreeElement for a constant position. More...
 
SelectionTreeElementPointer _gmx_sel_init_group_by_name (const char *name, yyscan_t scanner)
 Creates a gmx::SelectionTreeElement for a index group expression using group name. More...
 
SelectionTreeElementPointer _gmx_sel_init_group_by_id (int id, yyscan_t scanner)
 Creates a gmx::SelectionTreeElement for a index group expression using group index. More...
 
SelectionTreeElementPointer _gmx_sel_init_variable_ref (const gmx::SelectionTreeElementPointer &sel, yyscan_t scanner)
 Creates a gmx::SelectionTreeElement for a variable reference. More...
 
SelectionTreeElementPointer _gmx_sel_init_selection (const char *name, const gmx::SelectionTreeElementPointer &sel, yyscan_t scanner)
 Creates a root gmx::SelectionTreeElement for a selection. More...
 
SelectionTreeElementPointer _gmx_sel_assign_variable (const char *name, const gmx::SelectionTreeElementPointer &expr, yyscan_t scanner)
 Creates a root gmx::SelectionTreeElement elements for a variable assignment. More...
 
SelectionTreeElementPointer _gmx_sel_append_selection (const gmx::SelectionTreeElementPointer &sel, gmx::SelectionTreeElementPointer last, yyscan_t scanner)
 Appends a root gmx::SelectionTreeElement to a selection collection. More...
 
bool _gmx_sel_parser_should_finish (yyscan_t scanner)
 Check whether the parser should finish. More...
 

Function Documentation

SelectionTreeElementPointer _gmx_sel_append_selection ( const gmx::SelectionTreeElementPointer sel,
gmx::SelectionTreeElementPointer  last,
yyscan_t  scanner 
)

Appends a root gmx::SelectionTreeElement to a selection collection.

Parameters
selSelection to append (can be NULL, in which case nothing is done).
lastLast selection, or NULL if not present or not known.
scannerScanner data structure.
Returns
The last selection after the append.

Appends sel after the last root element, and returns either sel (if it was non-NULL) or the last element (if sel was NULL).

SelectionTreeElementPointer _gmx_sel_assign_variable ( const char *  name,
const gmx::SelectionTreeElementPointer expr,
yyscan_t  scanner 
)

Creates a root gmx::SelectionTreeElement elements for a variable assignment.

Parameters
[in]nameName of the variable.
[in]exprThe selection element that evaluates the variable.
scannerScanner data structure.
Returns
The created root selection element.

This function handles the creation of root gmx::SelectionTreeElement objects for variable assignments. A SEL_ROOT element and a SEL_SUBEXPR element are both created.

gmx::SelectionTreeElementPointer _gmx_sel_init_arithmetic ( const gmx::SelectionTreeElementPointer left,
const gmx::SelectionTreeElementPointer right,
char  op,
yyscan_t  scanner 
)

Creates a gmx::SelectionTreeElement for arithmetic expression evaluation.

Parameters
[in]leftSelection element for the left hand side.
[in]rightSelection element for the right hand side.
[in]opString representation of the operator.
[in]scannerScanner data structure.
Returns
The created selection element.

This function handles the creation of a gmx::SelectionTreeElement object for arithmetic expressions.

SelectionTreeElementPointer _gmx_sel_init_comparison ( const gmx::SelectionTreeElementPointer left,
const gmx::SelectionTreeElementPointer right,
const char *  cmpop,
yyscan_t  scanner 
)

Creates a gmx::SelectionTreeElement for comparsion expression evaluation.

Parameters
[in]leftSelection element for the left hand side.
[in]rightSelection element for the right hand side.
[in]cmpopString representation of the comparison operator.
[in]scannerScanner data structure.
Returns
The created selection element.

This function handles the creation of a gmx::SelectionTreeElement object for comparison expressions.

SelectionTreeElementPointer _gmx_sel_init_const_position ( real  x,
real  y,
real  z,
yyscan_t  scanner 
)

Creates a gmx::SelectionTreeElement for a constant position.

Parameters
[in]x,y,zCoordinates for the position.
[in]scannerScanner data structure.
Returns
The creates selection element.
SelectionTreeElementPointer _gmx_sel_init_group_by_id ( int  id,
yyscan_t  scanner 
)

Creates a gmx::SelectionTreeElement for a index group expression using group index.

Parameters
[in]idZero-based index number of the group to extract.
[in]scannerScanner data structure.
Returns
The created selection element.
SelectionTreeElementPointer _gmx_sel_init_group_by_name ( const char *  name,
yyscan_t  scanner 
)

Creates a gmx::SelectionTreeElement for a index group expression using group name.

Parameters
[in]nameName of an index group to search for.
[in]scannerScanner data structure.
Returns
The created selection element.

See gmx_ana_indexgrps_find() for information on how name is matched against the index groups.

SelectionTreeElementPointer _gmx_sel_init_keyword ( gmx_ana_selmethod_t method,
gmx::SelectionParserValueListPointer  args,
const char *  rpost,
yyscan_t  scanner 
)

Creates a gmx::SelectionTreeElement for a keyword expression from the parsed data.

Parameters
[in]methodMethod to use.
[in]argsPointer to the first argument.
[in]rpostReference position type to use (NULL = default).
[in]scannerScanner data structure.
Returns
The created selection element.

This function handles the creation of a gmx::SelectionTreeElement object for selection methods that do not take parameters.

SelectionTreeElementPointer _gmx_sel_init_keyword_of ( gmx_ana_selmethod_t method,
const gmx::SelectionTreeElementPointer group,
const char *  rpost,
yyscan_t  scanner 
)

Creates a gmx::SelectionTreeElement for "keyword of" expression.

Parameters
[in]methodMethod to use for initialization.
[in]groupSelection in which the keyword should be evaluated.
[in]rpostReference position type to use (NULL = default).
[in]scannerScanner data structure.
Returns
The created selection element.

This function handles the creation of a gmx::SelectionTreeElement object for expressions like "z of ...".

SelectionTreeElementPointer _gmx_sel_init_keyword_strmatch ( gmx_ana_selmethod_t method,
gmx::SelectionStringMatchType  matchType,
gmx::SelectionParserValueListPointer  args,
const char *  rpost,
yyscan_t  scanner 
)

Creates a gmx::SelectionTreeElement for string-matching keyword expression.

Parameters
[in]methodMethod to use.
[in]matchTypeString matching type.
[in]argsPointer to the first argument.
[in]rpostReference position type to use (NULL = default).
[in]scannerScanner data structure.
Returns
The created selection element.

This function handles the creation of a gmx::SelectionTreeElement object for keyword string matching.

SelectionTreeElementPointer _gmx_sel_init_method ( gmx_ana_selmethod_t method,
gmx::SelectionParserParameterListPointer  params,
const char *  rpost,
yyscan_t  scanner 
)

Creates a gmx::SelectionTreeElement for a method expression from the parsed data.

Parameters
[in]methodMethod to use for initialization.
[in]paramsPointer to the first parameter.
[in]rpostReference position type to use (NULL = default).
[in]scannerScanner data structure.
Returns
The created selection element.

This function handles the creation of a gmx::SelectionTreeElement object for selection methods that take parameters.

Part of the behavior of the same selection keyword is hardcoded into this function (or rather, into _gmx_selelem_custom_init_same()) to allow the use of any keyword in "same KEYWORD as" without requiring special handling somewhere else (or sacrificing the simple syntax).

SelectionTreeElementPointer _gmx_sel_init_modifier ( gmx_ana_selmethod_t method,
gmx::SelectionParserParameterListPointer  params,
const gmx::SelectionTreeElementPointer sel,
yyscan_t  scanner 
)

Creates a gmx::SelectionTreeElement for a modifier expression from the parsed data.

Parameters
[in]methodModifier to use for initialization.
[in]paramsPointer to the first parameter.
[in]selSelection element that the modifier should act on.
[in]scannerScanner data structure.
Returns
The created selection element.

This function handles the creation of a gmx::SelectionTreeElement object for selection modifiers.

SelectionTreeElementPointer _gmx_sel_init_position ( const gmx::SelectionTreeElementPointer expr,
const char *  type,
yyscan_t  scanner 
)

Creates a gmx::SelectionTreeElement for evaluation of reference positions.

Parameters
[in]exprInput selection element for the position calculation.
[in]typeReference position type or NULL for default.
[in]scannerScanner data structure.
Returns
The created selection element.

This function handles the creation of a gmx::SelectionTreeElement object for evaluation of reference positions.

SelectionTreeElementPointer _gmx_sel_init_selection ( const char *  name,
const gmx::SelectionTreeElementPointer sel,
yyscan_t  scanner 
)

Creates a root gmx::SelectionTreeElement for a selection.

Parameters
[in]nameName for the selection (if NULL, a default name is constructed).
[in]selThe selection element that evaluates the selection.
scannerScanner data structure.
Returns
The created root selection element.

This function handles the creation of root (SEL_ROOT) gmx::SelectionTreeElement objects for selections.

SelectionTreeElementPointer _gmx_sel_init_variable_ref ( const gmx::SelectionTreeElementPointer sel,
yyscan_t  scanner 
)

Creates a gmx::SelectionTreeElement for a variable reference.

Parameters
[in,out]selValue of the variable.
scannerScanner data structure.
Returns
The created selection element that references sel.

The reference count of sel is updated, but no other modifications are made.

bool _gmx_sel_parser_should_finish ( yyscan_t  scanner)

Check whether the parser should finish.

Parameters
[in]scannerScanner data structure.
Returns
true if the parser should finish, false if parsing should continue.

This function is called always after _gmx_sel_append_selection() to check whether a sufficient number of selections has already been provided. This is used to terminate interactive parsers when the correct number of selections has been provided.

void _gmx_selelem_init_method_params ( const gmx::SelectionTreeElementPointer sel,
yyscan_t  scanner 
)

Initializes the method parameter data of SEL_EXPRESSION and SEL_MODIFIER elements.

Parameters
[in,out]selSelection element to initialize.
[in]scannerScanner data structure.

A deep copy of the parameters is made to allow several expressions with the same method to coexist peacefully. Calls sel_datafunc() if one is specified for the method.

void _gmx_selelem_set_method ( const gmx::SelectionTreeElementPointer sel,
gmx_ana_selmethod_t method,
yyscan_t  scanner 
)

Initializes the method for a SEL_EXPRESSION selection element.

Parameters
[in,out]selSelection element to initialize.
[in]methodSelection method to set.
[in]scannerScanner data structure.

Makes a copy of method and stores it in sel->u.expr.method, and calls _gmx_selelem_init_method_params();

void _gmx_selelem_update_flags ( const gmx::SelectionTreeElementPointer sel)

Propagates the flags for selection elements.

Parameters
[in,out]selRoot of the selection element tree to initialize.

Propagates the SEL_DYNAMIC flag from the children of sel to sel (if any child of sel is dynamic, sel is also marked as such). The SEL_DYNAMIC flag is also set for SEL_EXPRESSION elements with a dynamic method. Also, sets one of the SEL_SINGLEVAL, SEL_ATOMVAL, or SEL_VARNUMVAL flags, either based on the children or on the type of the selection method. If the types of the children conflict, an error is returned.

The flags of the children of sel are also updated if not done earlier. The flags are initialized only once for any element; if SEL_FLAGSSET is set for an element, the function returns immediately, and the recursive operation does not descend beyond such elements.

bool _gmx_selparser_handle_error ( void *  scanner)

Handles errors in the selection parser.

Returns
true if parsing can continue with the next selection.
Exceptions
std::bad_allocif out of memory during the error processing.
unspecifiedCan throw the stored exception if recovery from that exception is not possible.

This function is called during error recovery, after Bison has discarded all the symbols for the erroneous selection. At this point, the full selection that caused the error is known, and can be added to the error context.

For an interactive parser, this function returns true to let the parsing continue with the next selection, or to let the user enter the next selection, if it was possible to recover from the exception. For other cases, this will either rethrow the original exception with added context, or return false after adding the context to the error reporter. Any exceptions thrown from this method are again caught by Bison and result in termination of the parsing; the caller can then rethrow them.

bool _gmx_selparser_handle_exception ( void *  scanner,
std::exception *  ex 
)

Handles exceptions caught within the Bison code.

Return values
<tt>true</tt>if the parser should attempt error recovery.
<tt>false</tt>if the parser should immediately abort.

This function is called whenever an exception is caught within Bison actions. Since exceptions cannot propagate through Bison code, the exception is saved (potentially with some extra context information) so that the caller of the parser can rethrow the exception.

If it is possible to recover from the exception, then the function returns true, and Bison enters error recovery state. At the end of the recovery, _gmx_selparser_handle_error() is called. If this function returns false, then Bison immediately aborts the parsing so that the caller can rethrow the exception.

static SelectionTreeElementPointer init_keyword_internal ( gmx_ana_selmethod_t method,
gmx::SelectionStringMatchType  matchType,
SelectionParserValueListPointer  args,
const char *  rpost,
yyscan_t  scanner 
)
static

Implementation method for keyword expression creation.

Parameters
[in]methodMethod to use.
[in]matchTypeString matching type (only used if method is a string keyword and args is not empty.
[in]argsPointer to the first argument.
[in]rpostReference position type to use (NULL = default).
[in]scannerScanner data structure.
Returns
The created selection element.

This function handles the creation of a gmx::SelectionTreeElement object for selection methods that do not take parameters.

static void set_refpos_type ( gmx::PositionCalculationCollection pcc,
const SelectionTreeElementPointer &  sel,
const char *  rpost 
)
static

Initializes the reference position calculation for a SEL_EXPRESSION element.

Parameters
[in,out]pccPosition calculation collection to use.
[in,out]selSelection element to initialize.
[in]rpostReference position type to use (NULL = default).