Merge branch 'release-4-6'
[alexxy/gromacs.git] / src / gromacs / selection / parser_internal.h
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11  * Copyright (c) 2001-2009, The GROMACS development team,
12  * check out http://www.gromacs.org for more information.
13
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * If you want to redistribute modifications, please consider that
20  * scientific software is very special. Version control is crucial -
21  * bugs must be traceable. We will be happy to consider code for
22  * inclusion in the official distribution, but derived work must not
23  * be called official GROMACS. Details are found in the README & COPYING
24  * files - if they are missing, get the official version at www.gromacs.org.
25  *
26  * To help us fund GROMACS development, we humbly ask that you cite
27  * the papers on the package - you can find them in the top README file.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \internal \file
32  * \brief Helper functions for the selection parser.
33  *
34  * This header is includes only from parser.cpp (generated from parser.y), and
35  * it includes functions and macros used internally by the parser.
36  * They are in a separate file to make then easier to edit (no need to
37  * regenerate the parser), and to keep parser.y as simple as possible.
38  *
39  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
40  * \ingroup module_selection
41  */
42 #ifndef GMX_SELECTION_PARSER_INTERNAL_H
43 #define GMX_SELECTION_PARSER_INTERNAL_H
44
45 #include <exception>
46
47 #include <boost/scoped_ptr.hpp>
48
49 #include "gromacs/utility/gmxassert.h"
50
51 #include "parsetree.h"
52 #include "selelem.h"
53
54 #include "scanner.h"
55
56 //! Error handler needed by Bison.
57 static void
58 yyerror(yyscan_t scanner, char const *s)
59 {
60     _gmx_selparser_error(scanner, "%s", s);
61 }
62
63 /*! \name Exception handling macros for actions
64  *
65  * These macros should be used at the beginning and end of each semantic action
66  * that may throw an exception. For robustness, it's best to wrap all actions
67  * that call functions declared outside parser.y should be wrapped.
68  * These macros take care to catch any exceptions, store the exception (or
69  * handle it and allow the parser to continue), and terminate the parser
70  * cleanly if necessary.
71  * The code calling the parser should use
72  * _gmx_sel_lexer_rethrow_exception_if_occurred() to rethrow any exceptions.
73  * \{
74  */
75 //! Starts an action that may throw exceptions.
76 #define BEGIN_ACTION \
77     try {
78 //! Finishes an action that may throw exceptions.
79 #define END_ACTION \
80     } \
81     catch(const std::exception &ex) \
82     { \
83         if (_gmx_selparser_handle_exception(scanner, ex)) \
84             YYERROR; \
85         else \
86             YYABORT; \
87     }
88 //!\}
89
90 #ifndef GMX_CXX11
91 //! No-op to enable use of same get()/set() implementation as with C++11.
92 static gmx::SelectionParserValue &move(gmx::SelectionParserValue &src)
93 {
94     return src;
95 }
96 //! No-op to enable use of same get()/set() implementation as with C++11.
97 static gmx::SelectionParserParameter &move(gmx::SelectionParserParameter &src)
98 {
99     return src;
100 }
101 #endif
102
103 /*! \brief
104  * Retrieves a semantic value.
105  *
106  * \param[in] src  Semantic value to get the value from.
107  * \returns   Retrieved value.
108  * \throws    unspecified  Any exception thrown by the move constructor of
109  *      ValueType (copy constructor if GMX_CXX11 is not set).
110  *
111  * There should be no statements that may throw exceptions in actions before
112  * this function has been called for all semantic values that have a C++ object
113  * stored.  Together with set(), this function abstracts away exception
114  * safety issues that arise from the use of a plain pointer for storing the
115  * semantic values.
116  *
117  * Does not throw for smart pointer types.  If used with types that may throw,
118  * the order of operations should be such that it is exception-safe.
119  */
120 template <typename ValueType> static
121 ValueType get(ValueType *src)
122 {
123     GMX_RELEASE_ASSERT(src != NULL, "Semantic value pointers should be non-NULL");
124     boost::scoped_ptr<ValueType> srcGuard(src);
125     return ValueType(move(*src));
126 }
127 /*! \brief
128  * Sets a semantic value.
129  *
130  * \tparam     ValueType Type of value to set.
131  * \param[out] dest  Semantic value to set (typically $$).
132  * \param[in]  value Value to put into the semantic value.
133  * \throws     std::bad_alloc if out of memory.
134  * \throws     unspecified  Any exception thrown by the move constructor of
135  *      ValueType (copy constructor if GMX_CXX11 is not set).
136  *
137  * This should be the last statement before ::END_ACTION, except for a
138  * possible ::CHECK_SEL.
139  */
140 template <typename ValueType> static
141 void set(ValueType *&dest, ValueType value)
142 {
143     dest = new ValueType(move(value));
144 }
145 /*! \brief
146  * Sets an empty semantic value.
147  *
148  * \tparam     ValueType Type of value to set (must be default constructible).
149  * \param[out] dest  Semantic value to set (typically $$).
150  * \throws     std::bad_alloc if out of memory.
151  * \throws     unspecified  Any exception thrown by the default constructor of
152  *      ValueType.
153  *
154  * This should be the last statement before ::END_ACTION, except for a
155  * possible ::CHECK_SEL.
156  */
157 template <typename ValueType> static
158 void set_empty(ValueType *&dest)
159 {
160     dest = new ValueType;
161 }
162 /*! \brief
163  * Checks that a valid tree was set.
164  *
165  * Should be called after set() if it was used to set a value where NULL
166  * pointer indicates an error.
167  *
168  * \todo
169  * Get rid of this macro.  It should now be possible to handle all errors using
170  * exceptions.
171  */
172 #define CHECK_SEL(sel) \
173     if (!*(sel)) { \
174         delete sel; \
175         YYERROR; \
176     }
177
178 #endif