2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012,2013,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.
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.
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.
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.
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.
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.
37 * Implements classes in symrec.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_selection
46 #include "gromacs/legacyheaders/macros.h"
48 #include "gromacs/utility/exceptions.h"
49 #include "gromacs/utility/gmxassert.h"
50 #include "gromacs/utility/stringutil.h"
51 #include "gromacs/utility/uniqueptr.h"
60 /********************************************************************
61 * SelectionParserSymbol
65 * Private implementation class for SelectionParserSymbol.
67 * \ingroup module_selection
69 class SelectionParserSymbol::Impl
73 * Initializes a symbol.
75 * \param[in] type Type for the symbol.
76 * \param[in] name Name for the symbol.
78 * The symbol table is responsible for initializing the \a meth_ and
79 * \a var_ members as appropriate.
81 Impl(SymbolType type, const char *name)
82 : name_(name), type_(type), meth_(NULL)
86 //! Name of the symbol.
88 //! Type of the symbol.
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_;
96 SelectionParserSymbol::SelectionParserSymbol(Impl *impl)
101 SelectionParserSymbol::~SelectionParserSymbol()
106 SelectionParserSymbol::name() const
111 SelectionParserSymbol::SymbolType
112 SelectionParserSymbol::type() const
117 gmx_ana_selmethod_t *
118 SelectionParserSymbol::methodValue() const
120 GMX_RELEASE_ASSERT(type() == MethodSymbol,
121 "Attempting to get method handle for a non-method symbol");
125 const gmx::SelectionTreeElementPointer &
126 SelectionParserSymbol::variableValue() const
128 GMX_RELEASE_ASSERT(type() == VariableSymbol,
129 "Attempting to get variable value for a non-variable symbol");
133 /********************************************************************
134 * SelectionParserSymbolTable::Impl
139 * Private implementation class for SelectionParserSymbolTable.
141 * All methods in this class may throw std::bad_alloc if out of memory.
143 * \ingroup module_selection
145 class SelectionParserSymbolTable::Impl
148 //! Smart pointer type for managing a SelectionParserSymbol.
149 typedef gmx::gmx_unique_ptr<SelectionParserSymbol>::type
151 //! Container type for the list of symbols.
152 typedef std::map<std::string, SymbolPointer> SymbolMap;
155 * Adds a symbol to the symbol list.
157 * \param[in] symbol Symbol to add.
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();
165 //! Symbols in this symbol table.
170 SelectionParserSymbolTable::Impl::addSymbol(SymbolPointer symbol)
172 symbols_.insert(std::make_pair(symbol->name(), move(symbol)));
176 SelectionParserSymbolTable::Impl::addReservedSymbols()
178 const char *const sym_reserved[] = {
191 for (size_t i = 0; i < asize(sym_reserved); ++i)
193 SymbolPointer sym(new SelectionParserSymbol(
194 new SelectionParserSymbol::Impl(
195 SelectionParserSymbol::ReservedSymbol, sym_reserved[i])));
196 addSymbol(move(sym));
201 SelectionParserSymbolTable::Impl::addPositionSymbols()
203 const char *const *postypes
204 = gmx::PositionCalculationCollection::typeEnumValues;
205 for (int i = 0; postypes[i] != NULL; ++i)
207 SymbolPointer sym(new SelectionParserSymbol(
208 new SelectionParserSymbol::Impl(
209 SelectionParserSymbol::PositionSymbol, postypes[i])));
210 addSymbol(move(sym));
214 /********************************************************************
215 * SelectionParserSymbolIterator
219 * Private implementation class for SelectionParserSymbolIterator.
221 * \ingroup module_selection
223 class SelectionParserSymbolIterator::Impl
226 //! Shorthand for the underlying iterator type.
227 typedef SelectionParserSymbolTable::Impl::SymbolMap::const_iterator
231 * Constructs an end iterator.
233 * \param[in] end Iterator to the end of the iterated container.
235 explicit Impl(IteratorType end)
236 : iter_(end), end_(end)
240 * Constructs an iterator.
242 * \param[in] iter Iterator to the current symbol.
243 * \param[in] end Iterator to the end of the iterated container.
245 Impl(IteratorType iter, IteratorType end)
246 : iter_(iter), end_(end)
250 //! Underlying iterator to the symbol container.
252 //! End of the symbol container being iterated.
256 SelectionParserSymbolIterator::SelectionParserSymbolIterator(Impl *impl)
261 SelectionParserSymbolIterator::SelectionParserSymbolIterator(
262 const SelectionParserSymbolIterator &other)
263 : impl_(new Impl(*other.impl_))
267 SelectionParserSymbolIterator::~SelectionParserSymbolIterator()
271 SelectionParserSymbolIterator &SelectionParserSymbolIterator::operator=(
272 const SelectionParserSymbolIterator &other)
274 impl_.reset(new Impl(*other.impl_));
278 bool SelectionParserSymbolIterator::operator==(
279 const SelectionParserSymbolIterator &other) const
281 return impl_->iter_ == other.impl_->iter_;
284 const SelectionParserSymbol &SelectionParserSymbolIterator::operator*() const
286 return *impl_->iter_->second;
289 SelectionParserSymbolIterator &SelectionParserSymbolIterator::operator++()
291 SelectionParserSymbol::SymbolType type = impl_->iter_->second->type();
296 while (impl_->iter_ != impl_->end_ && impl_->iter_->second->type() != type);
300 /********************************************************************
301 * SelectionParserSymbolTable
304 SelectionParserSymbolTable::SelectionParserSymbolTable()
307 impl_->addReservedSymbols();
308 impl_->addPositionSymbols();
311 SelectionParserSymbolTable::~SelectionParserSymbolTable()
315 const SelectionParserSymbol *
316 SelectionParserSymbolTable::findSymbol(const std::string &name,
319 Impl::SymbolMap::const_iterator sym = impl_->symbols_.lower_bound(name);
320 if (sym == impl_->symbols_.end())
324 if (sym->second->name() == name)
326 return sym->second.get();
328 if (!bExact && startsWith(sym->second->name(), name))
330 Impl::SymbolMap::const_iterator next = sym;
332 if (next != impl_->symbols_.end()
333 && startsWith(next->second->name(), name))
335 GMX_THROW(InvalidInputError("'" + name + "' is ambiguous"));
337 if (sym->second->type() == SelectionParserSymbol::MethodSymbol)
339 return sym->second.get();
345 SelectionParserSymbolIterator
346 SelectionParserSymbolTable::beginIterator(SelectionParserSymbol::SymbolType type) const
348 Impl::SymbolMap::const_iterator sym;
349 Impl::SymbolMap::const_iterator end = impl_->symbols_.end();
350 for (sym = impl_->symbols_.begin(); sym != end; ++sym)
352 if (sym->second->type() == type)
354 return SelectionParserSymbolIterator(
355 new SelectionParserSymbolIterator::Impl(sym, end));
358 return endIterator();
361 SelectionParserSymbolIterator
362 SelectionParserSymbolTable::endIterator() const
364 return SelectionParserSymbolIterator(
365 new SelectionParserSymbolIterator::Impl(impl_->symbols_.end()));
369 SelectionParserSymbolTable::addVariable(const char *name,
370 const gmx::SelectionTreeElementPointer &sel)
372 // In the current parser implementation, a syntax error is produced before
373 // this point is reached, but the check is here for robustness.
374 Impl::SymbolMap::const_iterator other = impl_->symbols_.find(name);
375 if (other != impl_->symbols_.end())
377 if (other->second->type() == SelectionParserSymbol::VariableSymbol)
379 GMX_THROW(InvalidInputError(
380 formatString("Reassigning variable '%s' is not supported",
385 GMX_THROW(InvalidInputError(
386 formatString("Variable name '%s' conflicts with a reserved keyword",
390 Impl::SymbolPointer sym(new SelectionParserSymbol(
391 new SelectionParserSymbol::Impl(
392 SelectionParserSymbol::VariableSymbol, name)));
393 sym->impl_->var_ = sel;
394 impl_->addSymbol(move(sym));
398 SelectionParserSymbolTable::addMethod(const char *name,
399 gmx_ana_selmethod_t *method)
401 if (impl_->symbols_.find(name) != impl_->symbols_.end())
404 formatString("Method name '%s' conflicts with another symbol",
407 Impl::SymbolPointer sym(new SelectionParserSymbol(
408 new SelectionParserSymbol::Impl(
409 SelectionParserSymbol::MethodSymbol, name)));
410 sym->impl_->meth_ = method;
411 impl_->addSymbol(move(sym));