Gromacs  2016.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Macros | Functions
#include <exception>
#include <memory>
#include "gromacs/utility/exceptions.h"
#include "gromacs/utility/gmxassert.h"
#include "gromacs/utility/stringutil.h"
#include "parsetree.h"
#include "scanner.h"
#include "selelem.h"
+ Include dependency graph for parser_internal.h:

Description

Helper functions for the selection parser.

This header is includes only from parser.cpp (generated from parser.y), and it includes functions and macros used internally by the parser. They are in a separate file to make then easier to edit (no need to regenerate the parser), and to keep parser.y as simple as possible.

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

Macros

#define GMX_YYFORCE_C_STACK_EXTENSION   1
 Custom macro to influence Bison behavior. More...
 
#define CHECK_SEL(sel)
 Checks that a valid tree was set. More...
 
Exception handling macros for actions

These macros should be used at the beginning and end of each semantic action that may throw an exception. For robustness, it's best to wrap all actions that call functions declared outside parser.y should be wrapped. These macros take care to catch any exceptions, store the exception (or handle it and allow the parser to continue), and terminate the parser cleanly if necessary. The code calling the parser should use _gmx_sel_lexer_rethrow_exception_if_occurred() to rethrow any exceptions.

#define BEGIN_ACTION   try {
 Starts an action that may throw exceptions.
 
#define END_ACTION
 Finishes an action that may throw exceptions. More...
 
#define END_ACTION_TOPLEVEL
 Finishes an action that may throw exceptions and does not support resuming. More...
 

Functions

template<typename ValueType >
static ValueType get (ValueType *src)
 Retrieves a semantic value. More...
 
template<typename ValueType >
static void set (ValueType *&dest, ValueType value)
 Sets a semantic value. More...
 
template<typename ValueType >
static void set_empty (ValueType *&dest)
 Sets an empty semantic value. More...
 

Macro Definition Documentation

#define CHECK_SEL (   sel)
Value:
if (!*(sel)) { \
delete sel; \
YYERROR; \
}

Checks that a valid tree was set.

Should be called after set() if it was used to set a value where NULL pointer indicates an error.

Todo:
Get rid of this macro. It should now be possible to handle all errors using exceptions.
#define END_ACTION
Value:
} \
catch (std::exception &ex) \
{ \
if (_gmx_selparser_handle_exception(scanner, &ex)) \
{ \
YYERROR; \
} \
else \
{ \
YYABORT; \
} \
}
bool _gmx_selparser_handle_exception(yyscan_t scanner, std::exception *ex)
Handles exceptions caught within the Bison code.
Definition: parsetree.cpp:278

Finishes an action that may throw exceptions.

#define END_ACTION_TOPLEVEL
Value:
} \
catch (const std::exception &) \
{ \
_gmx_sel_lexer_set_exception(scanner, std::current_exception()); \
YYABORT; \
}
void _gmx_sel_lexer_set_exception(yyscan_t scanner, const std::exception_ptr &ex)
Stores an exception that is caught during parsing.
Definition: scanner_internal.cpp:430

Finishes an action that may throw exceptions and does not support resuming.

#define GMX_YYFORCE_C_STACK_EXTENSION   1

Custom macro to influence Bison behavior.

This macro added to parser.cpp through our patch to force Bison to use C-style logic for stack reallocation even though we have provided YYLTYPE and are compiling the code in C++ (our YYLTYPE can safely be copied this way). An alternative would be to provide the whole reallocation logic through an undocumented yyoverflow() macro, but that is probably also more trouble than it is worth.

Function Documentation

template<typename ValueType >
static ValueType get ( ValueType *  src)
static

Retrieves a semantic value.

Parameters
[in]srcSemantic value to get the value from.
Returns
Retrieved value.
Exceptions
unspecifiedAny exception thrown by the move constructor of ValueType.

There should be no statements that may throw exceptions in actions before this function has been called for all semantic values that have a C++ object stored. Together with set(), this function abstracts away exception safety issues that arise from the use of a plain pointer for storing the semantic values.

Does not throw for smart pointer types. If used with types that may throw, the order of operations should be such that it is exception-safe.

template<typename ValueType >
static void set ( ValueType *&  dest,
ValueType  value 
)
static

Sets a semantic value.

Template Parameters
ValueTypeType of value to set.
Parameters
[out]destSemantic value to set (typically $$).
[in]valueValue to put into the semantic value.
Exceptions
std::bad_allocif out of memory.
unspecifiedAny exception thrown by the move constructor of ValueType.

This should be the last statement before END_ACTION, except for a possible CHECK_SEL.

template<typename ValueType >
static void set_empty ( ValueType *&  dest)
static

Sets an empty semantic value.

Template Parameters
ValueTypeType of value to set (must be default constructible).
Parameters
[out]destSemantic value to set (typically $$).
Exceptions
std::bad_allocif out of memory.
unspecifiedAny exception thrown by the default constructor of ValueType.

This should be the last statement before END_ACTION, except for a possible CHECK_SEL.