Merge branch release-4-6
[alexxy/gromacs.git] / src / gromacs / selection / symrec.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2009,2010,2011,2012,2013, by the GROMACS development team, led by
5  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6  * others, as listed in the AUTHORS file in the top-level source
7  * 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
37  * Implements classes in symrec.h.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_selection
41  */
42 #include <map>
43 #include <string>
44 #include <utility>
45
46 #include "gromacs/legacyheaders/macros.h"
47
48 #include "gromacs/utility/exceptions.h"
49 #include "gromacs/utility/gmxassert.h"
50 #include "gromacs/utility/stringutil.h"
51 #include "gromacs/utility/uniqueptr.h"
52
53 #include "poscalc.h"
54 #include "selelem.h"
55 #include "symrec.h"
56
57 namespace gmx
58 {
59
60 /********************************************************************
61  * SelectionParserSymbol
62  */
63
64 /*! \internal \brief
65  * Private implementation class for SelectionParserSymbol.
66  *
67  * \ingroup module_selection
68  */
69 class SelectionParserSymbol::Impl
70 {
71     public:
72         /*! \brief
73          * Initializes a symbol.
74          *
75          * \param[in] type  Type for the symbol.
76          * \param[in] name  Name for the symbol.
77          *
78          * The symbol table is responsible for initializing the \a meth_ and
79          * \a var_ members as appropriate.
80          */
81         Impl(SymbolType type, const char *name)
82             : name_(name), type_(type), meth_(NULL)
83         {
84         }
85
86         //! Name of the symbol.
87         std::string                     name_;
88         //! Type of the symbol.
89         SymbolType                      type_;
90         //! Pointer to the method structure (\ref MethodSymbol).
91         gmx_ana_selmethod_t            *meth_;
92         //! Pointer to the variable value (\ref VariableSymbol).
93         SelectionTreeElementPointer     var_;
94 };
95
96 SelectionParserSymbol::SelectionParserSymbol(Impl *impl)
97     : impl_(impl)
98 {
99 }
100
101 SelectionParserSymbol::~SelectionParserSymbol()
102 {
103 }
104
105 const std::string &
106 SelectionParserSymbol::name() const
107 {
108     return impl_->name_;
109 }
110
111 SelectionParserSymbol::SymbolType
112 SelectionParserSymbol::type() const
113 {
114     return impl_->type_;
115 }
116
117 gmx_ana_selmethod_t *
118 SelectionParserSymbol::methodValue() const
119 {
120     GMX_RELEASE_ASSERT(type() == MethodSymbol,
121                        "Attempting to get method handle for a non-method symbol");
122     return impl_->meth_;
123 }
124
125 const gmx::SelectionTreeElementPointer &
126 SelectionParserSymbol::variableValue() const
127 {
128     GMX_RELEASE_ASSERT(type() == VariableSymbol,
129                        "Attempting to get variable value for a non-variable symbol");
130     return impl_->var_;
131 }
132
133 /********************************************************************
134  * SelectionParserSymbolTable::Impl
135  */
136
137 /*! \internal
138  * \brief
139  * Private implementation class for SelectionParserSymbolTable.
140  *
141  * All methods in this class may throw std::bad_alloc if out of memory.
142  *
143  * \ingroup module_selection
144  */
145 class SelectionParserSymbolTable::Impl
146 {
147     public:
148         //! Smart pointer type for managing a SelectionParserSymbol.
149         typedef gmx::gmx_unique_ptr<SelectionParserSymbol>::type
150             SymbolPointer;
151         //! Container type for the list of symbols.
152         typedef std::map<std::string, SymbolPointer> SymbolMap;
153
154         /*! \brief
155          * Adds a symbol to the symbol list.
156          *
157          * \param[in] symbol  Symbol to add.
158          */
159         void addSymbol(SymbolPointer symbol);
160         //! Adds the reserved symbols to this symbol table.
161         void addReservedSymbols();
162         //! Adds the position symbols to this symbol table.
163         void addPositionSymbols();
164
165         //! Symbols in this symbol table.
166         SymbolMap               symbols_;
167 };
168
169 void
170 SelectionParserSymbolTable::Impl::addSymbol(SymbolPointer symbol)
171 {
172     symbols_.insert(std::make_pair(symbol->name(), move(symbol)));
173 }
174
175 void
176 SelectionParserSymbolTable::Impl::addReservedSymbols()
177 {
178     const char *const sym_reserved[] = {
179         "group",
180         "to",
181         "not",
182         "and",
183         "or",
184         "xor",
185         "yes",
186         "no",
187         "on",
188         "off",
189         "help",
190     };
191
192     for (size_t i = 0; i < asize(sym_reserved); ++i)
193     {
194         SymbolPointer sym(new SelectionParserSymbol(
195                                   new SelectionParserSymbol::Impl(
196                                           SelectionParserSymbol::ReservedSymbol, sym_reserved[i])));
197         addSymbol(move(sym));
198     }
199 }
200
201 void
202 SelectionParserSymbolTable::Impl::addPositionSymbols()
203 {
204     const char *const *postypes
205         = gmx::PositionCalculationCollection::typeEnumValues;
206     for (int i = 0; postypes[i] != NULL; ++i)
207     {
208         SymbolPointer sym(new SelectionParserSymbol(
209                                   new SelectionParserSymbol::Impl(
210                                           SelectionParserSymbol::PositionSymbol, postypes[i])));
211         addSymbol(move(sym));
212     }
213 }
214
215 /********************************************************************
216  * SelectionParserSymbolIterator
217  */
218
219 /*! \internal \brief
220  * Private implementation class for SelectionParserSymbolIterator.
221  *
222  * \ingroup module_selection
223  */
224 class SelectionParserSymbolIterator::Impl
225 {
226     public:
227         //! Shorthand for the underlying iterator type.
228         typedef SelectionParserSymbolTable::Impl::SymbolMap::const_iterator
229             IteratorType;
230
231         /*! \brief
232          * Constructs an end iterator.
233          *
234          * \param[in] end  Iterator to the end of the iterated container.
235          */
236         explicit Impl(IteratorType end)
237             : iter_(end), end_(end)
238         {
239         }
240         /*! \brief
241          * Constructs an iterator.
242          *
243          * \param[in] iter Iterator to the current symbol.
244          * \param[in] end  Iterator to the end of the iterated container.
245          */
246         Impl(IteratorType iter, IteratorType end)
247             : iter_(iter), end_(end)
248         {
249         }
250
251         //! Underlying iterator to the symbol container.
252         IteratorType            iter_;
253         //! End of the symbol container being iterated.
254         IteratorType            end_;
255 };
256
257 SelectionParserSymbolIterator::SelectionParserSymbolIterator(Impl *impl)
258     : impl_(impl)
259 {
260 }
261
262 SelectionParserSymbolIterator::SelectionParserSymbolIterator(
263         const SelectionParserSymbolIterator &other)
264     : impl_(new Impl(*other.impl_))
265 {
266 }
267
268 SelectionParserSymbolIterator::~SelectionParserSymbolIterator()
269 {
270 }
271
272 SelectionParserSymbolIterator &SelectionParserSymbolIterator::operator=(
273         const SelectionParserSymbolIterator &other)
274 {
275     impl_.reset(new Impl(*other.impl_));
276     return *this;
277 }
278
279 bool SelectionParserSymbolIterator::operator==(
280         const SelectionParserSymbolIterator &other) const
281 {
282     return impl_->iter_ == other.impl_->iter_;
283 }
284
285 const SelectionParserSymbol &SelectionParserSymbolIterator::operator*() const
286 {
287     return *impl_->iter_->second;
288 }
289
290 SelectionParserSymbolIterator &SelectionParserSymbolIterator::operator++()
291 {
292     SelectionParserSymbol::SymbolType type = impl_->iter_->second->type();
293     do
294     {
295         ++impl_->iter_;
296     }
297     while (impl_->iter_ != impl_->end_ && impl_->iter_->second->type() != type);
298     return *this;
299 }
300
301 /********************************************************************
302  * SelectionParserSymbolTable
303  */
304
305 SelectionParserSymbolTable::SelectionParserSymbolTable()
306     : impl_(new Impl)
307 {
308     impl_->addReservedSymbols();
309     impl_->addPositionSymbols();
310 }
311
312 SelectionParserSymbolTable::~SelectionParserSymbolTable()
313 {
314 }
315
316 const SelectionParserSymbol *
317 SelectionParserSymbolTable::findSymbol(const std::string &name,
318                                        bool               bExact) const
319 {
320     Impl::SymbolMap::const_iterator sym = impl_->symbols_.lower_bound(name);
321     if (sym == impl_->symbols_.end())
322     {
323         return NULL;
324     }
325     if (sym->second->name() == name)
326     {
327         return sym->second.get();
328     }
329     if (!bExact && startsWith(sym->second->name(), name))
330     {
331         Impl::SymbolMap::const_iterator next = sym;
332         ++next;
333         if (next != impl_->symbols_.end()
334             && startsWith(next->second->name(), name))
335         {
336             GMX_THROW(InvalidInputError("'" + name + "' is ambiguous"));
337         }
338         if (sym->second->type() == SelectionParserSymbol::MethodSymbol)
339         {
340             return sym->second.get();
341         }
342     }
343     return NULL;
344 }
345
346 SelectionParserSymbolIterator
347 SelectionParserSymbolTable::beginIterator(SelectionParserSymbol::SymbolType type) const
348 {
349     Impl::SymbolMap::const_iterator sym;
350     Impl::SymbolMap::const_iterator end = impl_->symbols_.end();
351     for (sym = impl_->symbols_.begin(); sym != end; ++sym)
352     {
353         if (sym->second->type() == type)
354         {
355             return SelectionParserSymbolIterator(
356                     new SelectionParserSymbolIterator::Impl(sym, end));
357         }
358     }
359     return endIterator();
360 }
361
362 SelectionParserSymbolIterator
363 SelectionParserSymbolTable::endIterator() const
364 {
365     return SelectionParserSymbolIterator(
366             new SelectionParserSymbolIterator::Impl(impl_->symbols_.end()));
367 }
368
369 void
370 SelectionParserSymbolTable::addVariable(const char                             *name,
371                                         const gmx::SelectionTreeElementPointer &sel)
372 {
373     // In the current parser implementation, a syntax error is produced before
374     // this point is reached, but the check is here for robustness.
375     Impl::SymbolMap::const_iterator other = impl_->symbols_.find(name);
376     if (other != impl_->symbols_.end())
377     {
378         if (other->second->type() == SelectionParserSymbol::VariableSymbol)
379         {
380             GMX_THROW(InvalidInputError(
381                               formatString("Reassigning variable '%s' is not supported",
382                                            name)));
383         }
384         else
385         {
386             GMX_THROW(InvalidInputError(
387                               formatString("Variable name '%s' conflicts with a reserved keyword",
388                                            name)));
389         }
390     }
391     Impl::SymbolPointer sym(new SelectionParserSymbol(
392                                     new SelectionParserSymbol::Impl(
393                                             SelectionParserSymbol::VariableSymbol, name)));
394     sym->impl_->var_ = sel;
395     impl_->addSymbol(move(sym));
396 }
397
398 void
399 SelectionParserSymbolTable::addMethod(const char          *name,
400                                       gmx_ana_selmethod_t *method)
401 {
402     if (impl_->symbols_.find(name) != impl_->symbols_.end())
403     {
404         GMX_THROW(APIError(
405                           formatString("Method name '%s' conflicts with another symbol",
406                                        name)));
407     }
408     Impl::SymbolPointer sym(new SelectionParserSymbol(
409                                     new SelectionParserSymbol::Impl(
410                                             SelectionParserSymbol::MethodSymbol, name)));
411     sym->impl_->meth_ = method;
412     impl_->addSymbol(move(sym));
413 }
414
415 } // namespace gmx