Merge release-5-0 into master
[alexxy/gromacs.git] / src / gromacs / selection / scanner_internal.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2009,2010,2011,2012,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 Internal header file used by the selection tokenizer.
37  *
38  * \author Teemu Murtola <teemu.murtola@gmail.com>
39  * \ingroup module_selection
40  */
41 #ifndef SELECTION_SCANNER_INTERNAL_H
42 #define SELECTION_SCANNER_INTERNAL_H
43
44 #include <boost/exception_ptr.hpp>
45
46 #include "parser.h"
47
48 namespace gmx
49 {
50 class MessageStringCollector;
51 class SelectionParserSymbol;
52 }
53
54 /* These need to be defined before including scanner_flex.h, because it
55  * uses YY_EXTRA_TYPE. But we also need to include it before defining
56  * gmx_sel_lexer_t; hence the forward declaration. */
57 struct gmx_sel_lexer_t;
58 #define YY_EXTRA_TYPE struct gmx_sel_lexer_t *
59
60 /* We cannot include scanner_flex.h from the scanner itself, because it
61  * seems to break everything. */
62 /* And we need to define YY_NO_UNISTD_H here as well, otherwise unistd.h
63  * gets included in other files than scanner.cpp... */
64 #ifndef FLEX_SCANNER
65 #define YY_NO_UNISTD_H
66 #include "scanner_flex.h"
67 #endif
68
69 /*! \internal \brief
70  * Internal data structure for the selection tokenizer state.
71  */
72 typedef struct gmx_sel_lexer_t
73 {
74     //! Selection collection to put parsed selections in.
75     struct gmx_ana_selcollection_t  *sc;
76     //! Error reporter object.
77     gmx::MessageStringCollector     *errors;
78     //! Stores an exception that occurred during parsing.
79     boost::exception_ptr             exception;
80     //! Whether external index groups have been set.
81     bool                             bGroups;
82     //! External index groups for resolving \c group keywords.
83     struct gmx_ana_indexgrps_t      *grps;
84     //! Number of selections at which the parser should stop.
85     int                              nexpsel;
86
87     //! Whether the parser is interactive.
88     bool                             bInteractive;
89
90     //! Pretty-printed version of the string parsed since last clear.
91     char                            *pselstr;
92     //! Length of the string in \a pselstr.
93     int                              pslen;
94     //! Number of bytes allocated for \a pselstr.
95     int                              nalloc_psel;
96     /*! \brief
97      * Position of the result of the current Bison action.
98      *
99      * This identifies the part of \a pselstr that corresponds to the
100      * subselection that is currently being reduced by Bison.
101      */
102     gmx::SelectionLocation           currentLocation;
103
104     //! Stack of methods in which parameters should be looked up.
105     struct gmx_ana_selmethod_t     **mstack;
106     //! Index of the top of the stack in \a mstack.
107     int                              msp;
108     //! Number of elements allocated for \a mstack.
109     int                              mstack_alloc;
110
111     //! Number of END_OF_METHOD tokens to return before \a nextparam.
112     int                              neom;
113     //! Parameter symbol to return before resuming scanning.
114     struct gmx_ana_selparam_t       *nextparam;
115     //! Whether \a nextparam was a boolean parameter with a 'no' prefix.
116     bool                             bBoolNo;
117     /*! \brief
118      * Method symbol to return before resuming scanning
119      *
120      * Only used when \p nextparam is NULL.
121      */
122     const gmx::SelectionParserSymbol *nextMethodSymbol;
123     //! Used to track whether the previous token was a position modifier.
124     int                               prev_pos_kw;
125
126     //! Whether the 'of' keyword is acceptable as the next token.
127     bool                             bMatchOf;
128     //! Whether boolean values (yes/no/on/off) are acceptable as the next token.
129     bool                             bMatchBool;
130     //! Whether the next token starts a new selection.
131     bool                             bCmdStart;
132
133     //! Whether an external buffer is set for the scanner.
134     bool                             bBuffer;
135     //! The current buffer for the scanner.
136     YY_BUFFER_STATE                  buffer;
137 } gmx_sel_lexer_t;
138
139 /* Because Flex defines yylval, yytext, and yyleng as macros,
140  * and this file is included from scanner.l,
141  * we cannot have them here as parameter names... */
142 /** Internal function for cases where several tokens need to be returned. */
143 int
144 _gmx_sel_lexer_process_pending(YYSTYPE *, YYLTYPE *, gmx_sel_lexer_t *state);
145 /** Internal function that processes identifier tokens. */
146 int
147     _gmx_sel_lexer_process_identifier(YYSTYPE *, YYLTYPE *, char *, size_t,
148                                       gmx_sel_lexer_t *state);
149 /** Internal function to add a token to the pretty-printed selection text. */
150 void
151 _gmx_sel_lexer_add_token(YYLTYPE *, const char *str, int len, gmx_sel_lexer_t *state);
152
153 #endif