/*
+ * This file is part of the GROMACS molecular simulation package.
*
- * This source code is part of
+ * Copyright (c) 2009,2010,2011,2012,2013,2014, by the GROMACS development team, led by
+ * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+ * and including many others, as listed in the AUTHORS file in the
+ * top-level source directory and at http://www.gromacs.org.
*
- * G R O M A C S
+ * GROMACS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
*
- * GROningen MAchine for Chemical Simulations
+ * GROMACS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
*
- * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
- * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
- * Copyright (c) 2001-2009, The GROMACS development team,
- * check out http://www.gromacs.org for more information.
-
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
- * If you want to redistribute modifications, please consider that
- * scientific software is very special. Version control is crucial -
- * bugs must be traceable. We will be happy to consider code for
- * inclusion in the official distribution, but derived work must not
- * be called official GROMACS. Details are found in the README & COPYING
- * files - if they are missing, get the official version at www.gromacs.org.
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
*
* To help us fund GROMACS development, we humbly ask that you cite
- * the papers on the package - you can find them in the top README file.
- *
- * For more info, check our website at http://www.gromacs.org
+ * the research papers on the package. Check out http://www.gromacs.org.
*/
/*! \internal \file
* \brief
* Implements functions in selparam.h.
*
- * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
* \ingroup module_selection
*/
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
+#include "gmxpre.h"
#include <algorithm>
+#include <string>
-#include "smalloc.h"
-#include "string2.h"
-#include "vec.h"
-
+#include "gromacs/math/vec.h"
#include "gromacs/selection/position.h"
-#include "gromacs/selection/selmethod.h"
-#include "gromacs/selection/selparam.h"
-#include "gromacs/utility/errorcodes.h"
+#include "gromacs/utility/cstringutil.h"
+#include "gromacs/utility/exceptions.h"
#include "gromacs/utility/gmxassert.h"
#include "gromacs/utility/messagestringcollector.h"
+#include "gromacs/utility/smalloc.h"
#include "gromacs/utility/stringutil.h"
#include "parsetree.h"
-#include "position.h"
#include "scanner.h"
#include "selelem.h"
+#include "selmethod.h"
+#include "selparam.h"
-using std::min;
-using std::max;
+using gmx::SelectionParserValue;
+using gmx::SelectionParserValueList;
+using gmx::SelectionParserParameter;
+using gmx::SelectionParserParameterList;
+using gmx::SelectionTreeElement;
+using gmx::SelectionTreeElementPointer;
/*!
* \param[in] name Name of the parameter to search.
{
return (i == 0) ? NULL : ¶m[i-1];
}
- for ( ; i < nparam; ++i)
+ for (; i < nparam; ++i)
{
if (!strcmp(param[i].name, name))
{
}
/*! \brief
- * Does a type conversion on a \c t_selexpr_value.
+ * Does a type conversion on a SelectionParserValue.
*
* \param[in,out] value Value to convert.
* \param[in] type Type to convert to.
* \returns 0 on success, a non-zero value on error.
*/
static int
-convert_value(t_selexpr_value *value, e_selvalue_t type, void *scanner)
+convert_value(SelectionParserValue *value, e_selvalue_t type, void *scanner)
{
if (value->type == type || type == NO_VALUE)
{
return 0;
}
- if (value->bExpr)
+ if (value->hasExpressionValue())
{
/* Conversion from atom selection to position using default
* reference positions. */
if (value->type == GROUP_VALUE && type == POS_VALUE)
{
- value->u.expr =
- _gmx_sel_init_position(value->u.expr, NULL, scanner);
- if (value->u.expr == NULL)
+ SelectionTreeElementPointer expr =
+ _gmx_sel_init_position(value->expr, NULL, scanner);
+ // FIXME: Use exceptions
+ if (!expr)
{
return -1;
}
- value->type = type;
+ *value = SelectionParserValue::createExpr(expr);
return 0;
}
return -1;
/* Integers to floating point are easy */
if (value->type == INT_VALUE && type == REAL_VALUE)
{
- real r1 = (real)value->u.i.i1;
- real r2 = (real)value->u.i.i2;
- value->u.r.r1 = r1;
- value->u.r.r2 = r2;
- value->type = type;
+ *value = SelectionParserValue::createRealRange(value->u.i.i1,
+ value->u.i.i2);
return 0;
}
/* Reals that are integer-valued can also be converted */
- if (value->type == REAL_VALUE && type == INT_VALUE
- && gmx_within_tol(value->u.r.r1, (int)value->u.r.r1, GMX_REAL_EPS)
- && gmx_within_tol(value->u.r.r2, (int)value->u.r.r2, GMX_REAL_EPS))
+ if (value->type == REAL_VALUE && type == INT_VALUE)
{
- int i1 = (int)value->u.r.r1;
- int i2 = (int)value->u.r.r2;
- value->u.i.i1 = i1;
- value->u.i.i2 = i2;
- value->type = type;
- return 0;
+ int i1 = static_cast<int>(value->u.r.r1);
+ int i2 = static_cast<int>(value->u.r.r2);
+ if (gmx_within_tol(value->u.r.r1, i1, GMX_REAL_EPS)
+ && gmx_within_tol(value->u.r.r2, i2, GMX_REAL_EPS))
+ {
+ *value = SelectionParserValue::createIntegerRange(i1, i2);
+ return 0;
+ }
}
}
return -1;
* \returns 0 on success, a non-zero value on error.
*/
static int
-convert_values(t_selexpr_value *values, e_selvalue_t type, void *scanner)
+convert_values(SelectionParserValueList *values, e_selvalue_t type, void *scanner)
{
- t_selexpr_value *value;
- int rc, rc1;
-
- rc = 0;
- value = values;
- while (value)
+ int rc = 0;
+ SelectionParserValueList::iterator value;
+ for (value = values->begin(); value != values->end(); ++value)
{
- rc1 = convert_value(value, type, scanner);
+ int rc1 = convert_value(&*value, type, scanner);
if (rc1 != 0 && rc == 0)
{
rc = rc1;
}
- value = value->next;
}
/* FIXME: More informative error messages */
return rc;
* in the same order as the corresponding parameters.
*/
static void
-place_child(t_selelem *root, t_selelem *child, gmx_ana_selparam_t *param)
+place_child(const SelectionTreeElementPointer &root,
+ const SelectionTreeElementPointer &child,
+ gmx_ana_selparam_t *param)
{
gmx_ana_selparam_t *ps;
int n;
}
else
{
- t_selelem *prev;
-
- prev = root->child;
+ SelectionTreeElementPointer prev = root->child;
while (prev->next && prev->next->u.param - ps >= n)
{
prev = prev->next;
/*! \brief
* Comparison function for sorting integer ranges.
- *
+ *
* \param[in] a Pointer to the first range.
* \param[in] b Pointer to the second range.
* \returns -1, 0, or 1 depending on the relative order of \p a and \p b.
/*! \brief
* Parses the values for a parameter that takes integer or real ranges.
- *
- * \param[in] nval Number of values in \p values.
- * \param[in] values Pointer to the list of values.
+ *
+ * \param[in] values List of values.
* \param param Parameter to parse.
* \param[in] scanner Scanner data structure.
* \returns true if the values were parsed successfully, false otherwise.
*/
static bool
-parse_values_range(int nval, t_selexpr_value *values, gmx_ana_selparam_t *param,
- void *scanner)
+parse_values_range(const SelectionParserValueList &values,
+ gmx_ana_selparam_t *param, void *scanner)
{
- t_selexpr_value *value;
int *idata;
real *rdata;
int i, j, n;
param->flags &= ~SPAR_DYNAMIC;
- if (param->val.type != INT_VALUE && param->val.type != REAL_VALUE)
- {
- GMX_ERROR_NORET(gmx::eeInternalError, "Invalid range parameter type");
- return false;
- }
+ GMX_RELEASE_ASSERT(param->val.type == INT_VALUE || param->val.type == REAL_VALUE,
+ "Invalid range parameter type");
idata = NULL;
rdata = NULL;
if (param->val.type == INT_VALUE)
{
- snew(idata, nval*2);
+ snew(idata, values.size()*2);
}
else
{
- snew(rdata, nval*2);
+ snew(rdata, values.size()*2);
}
- value = values;
i = 0;
- while (value)
+ SelectionParserValueList::const_iterator value;
+ for (value = values.begin(); value != values.end(); ++value)
{
- if (value->bExpr)
+ if (value->hasExpressionValue())
{
_gmx_selparser_error(scanner, "expressions not supported within range parameters");
return false;
}
- if (value->type != param->val.type)
- {
- GMX_ERROR_NORET(gmx::eeInternalError, "Invalid range value type");
- return false;
- }
+ GMX_RELEASE_ASSERT(value->type == param->val.type,
+ "Invalid range value type (should have been caught earlier)");
if (param->val.type == INT_VALUE)
{
- /* Make sure the input range is in increasing order */
- if (value->u.i.i1 > value->u.i.i2)
- {
- int tmp = value->u.i.i1;
- value->u.i.i1 = value->u.i.i2;
- value->u.i.i2 = tmp;
- }
+ int i1 = std::min(value->u.i.i1, value->u.i.i2);
+ int i2 = std::max(value->u.i.i1, value->u.i.i2);
/* Check if the new range overlaps or extends the previous one */
- if (i > 0 && value->u.i.i1 <= idata[i-1]+1 && value->u.i.i2 >= idata[i-2]-1)
+ if (i > 0 && i1 <= idata[i-1]+1 && i2 >= idata[i-2]-1)
{
- idata[i-2] = min(idata[i-2], value->u.i.i1);
- idata[i-1] = max(idata[i-1], value->u.i.i2);
+ idata[i-2] = std::min(idata[i-2], i1);
+ idata[i-1] = std::max(idata[i-1], i2);
}
else
{
- idata[i++] = value->u.i.i1;
- idata[i++] = value->u.i.i2;
+ idata[i++] = i1;
+ idata[i++] = i2;
}
}
else
{
- /* Make sure the input range is in increasing order */
- if (value->u.r.r1 > value->u.r.r2)
- {
- real tmp = value->u.r.r1;
- value->u.r.r1 = value->u.r.r2;
- value->u.r.r2 = tmp;
- }
+ real r1 = std::min(value->u.r.r1, value->u.r.r2);
+ real r2 = std::max(value->u.r.r1, value->u.r.r2);
/* Check if the new range overlaps or extends the previous one */
- if (i > 0 && value->u.r.r1 <= rdata[i-1] && value->u.r.r2 >= rdata[i-2])
+ if (i > 0 && r1 <= rdata[i-1] && r2 >= rdata[i-2])
{
- rdata[i-2] = min(rdata[i-2], value->u.r.r1);
- rdata[i-1] = max(rdata[i-1], value->u.r.r2);
+ rdata[i-2] = std::min(rdata[i-2], r1);
+ rdata[i-1] = std::max(rdata[i-1], r2);
}
else
{
- rdata[i++] = value->u.r.r1;
- rdata[i++] = value->u.r.r2;
+ rdata[i++] = r1;
+ rdata[i++] = r2;
}
}
- value = value->next;
}
n = i/2;
/* Sort the ranges and merge consequent ones */
{
idata[j] = idata[i];
idata[j+1] = idata[i+1];
- j += 2;
+ j += 2;
}
}
}
{
rdata[j] = rdata[i];
rdata[j+1] = rdata[i+1];
- j += 2;
+ j += 2;
}
}
}
/*! \brief
* Parses the values for a parameter that takes a variable number of values.
- *
- * \param[in] nval Number of values in \p values.
- * \param[in] values Pointer to the list of values.
+ *
+ * \param[in] values List of values.
* \param param Parameter to parse.
* \param root Selection element to which child expressions are added.
* \param[in] scanner Scanner data structure.
* is stored, each as a separate value.
*/
static bool
-parse_values_varnum(int nval, t_selexpr_value *values,
- gmx_ana_selparam_t *param, t_selelem *root, void *scanner)
+parse_values_varnum(const SelectionParserValueList &values,
+ gmx_ana_selparam_t *param,
+ const SelectionTreeElementPointer &root,
+ void *scanner)
{
- t_selexpr_value *value;
int i, j;
param->flags &= ~SPAR_DYNAMIC;
- /* Update nval if there are integer ranges. */
+ /* Compute number of values, considering also integer ranges. */
+ size_t valueCount = values.size();
if (param->val.type == INT_VALUE)
{
- value = values;
- while (value)
+ SelectionParserValueList::const_iterator value;
+ for (value = values.begin(); value != values.end(); ++value)
{
- if (value->type == INT_VALUE && !value->bExpr)
+ if (value->type == INT_VALUE && !value->hasExpressionValue())
{
- nval += abs(value->u.i.i2 - value->u.i.i1);
+ valueCount += abs(value->u.i.i2 - value->u.i.i1);
}
- value = value->next;
}
}
if (param->val.type != INT_VALUE && param->val.type != REAL_VALUE
&& param->val.type != STR_VALUE && param->val.type != POS_VALUE)
{
- GMX_ERROR_NORET(gmx::eeInternalError,
- "Variable-count value type not implemented");
- return false;
+ GMX_THROW(gmx::InternalError("Variable-count value type not implemented"));
}
/* Reserve appropriate amount of memory */
if (param->val.type == POS_VALUE)
{
- gmx_ana_pos_reserve(param->val.u.p, nval, 0);
- gmx_ana_pos_set_nr(param->val.u.p, nval);
+ gmx_ana_pos_reserve(param->val.u.p, valueCount, 0);
gmx_ana_indexmap_init(¶m->val.u.p->m, NULL, NULL, INDEX_UNKNOWN);
+ gmx_ana_pos_set_nr(param->val.u.p, valueCount);
}
else
{
- _gmx_selvalue_reserve(¶m->val, nval);
+ _gmx_selvalue_reserve(¶m->val, valueCount);
}
- value = values;
i = 0;
- while (value)
+ SelectionParserValueList::const_iterator value;
+ for (value = values.begin(); value != values.end(); ++value)
{
- if (value->bExpr)
+ if (value->hasExpressionValue())
{
_gmx_selparser_error(scanner, "expressions not supported within value lists");
return false;
}
- if (value->type != param->val.type)
- {
- GMX_ERROR_NORET(gmx::eeInternalError, "Invalid value type");
- return false;
- }
+ GMX_RELEASE_ASSERT(value->type == param->val.type,
+ "Invalid value type (should have been caught earlier)");
switch (param->val.type)
{
case INT_VALUE:
}
param->val.u.r[i++] = value->u.r.r1;
break;
- case STR_VALUE: param->val.u.s[i++] = strdup(value->u.s); break;
+ case STR_VALUE:
+ param->val.u.s[i++] = gmx_strdup(value->stringValue().c_str());
+ break;
case POS_VALUE: copy_rvec(value->u.x, param->val.u.p->x[i++]); break;
default: /* Should not be reached */
- GMX_ERROR_NORET(gmx::eeInternalError, "Invalid value type");
- return false;
+ GMX_THROW(gmx::InternalError("Variable-count value type not implemented"));
}
- value = value->next;
}
param->val.nr = i;
if (param->nvalptr)
* other function. */
if (param->val.type == STR_VALUE)
{
- t_selelem *child;
-
- child = _gmx_selelem_create(SEL_CONST);
+ SelectionTreeElementPointer child(new SelectionTreeElement(SEL_CONST));
_gmx_selelem_set_vtype(child, STR_VALUE);
- child->name = param->name;
+ child->setName(param->name);
child->flags &= ~SEL_ALLOCVAL;
child->flags |= SEL_FLAGSSET | SEL_VARNUMVAL | SEL_ALLOCDATA;
- child->v.nr = param->val.nr;
+ child->v.nr = param->val.nr;
_gmx_selvalue_setstore(&child->v, param->val.u.s);
/* Because the child is not group-valued, the u union is not used
* for anything, so we can abuse it by storing the parameter value
* If \p expr is already a \ref SEL_SUBEXPRREF, it is used as it is.
* \ref SEL_ALLOCVAL is cleared for the returned element.
*/
-static t_selelem *
-add_child(t_selelem *root, gmx_ana_selparam_t *param, t_selelem *expr,
- void *scanner)
+static SelectionTreeElementPointer
+add_child(const SelectionTreeElementPointer &root, gmx_ana_selparam_t *param,
+ const SelectionTreeElementPointer &expr, void *scanner)
{
- t_selelem *child;
- int rc;
-
- if (root->type != SEL_EXPRESSION && root->type != SEL_MODIFIER)
- {
- GMX_ERROR_NORET(gmx::eeInternalError,
- "Unsupported root element for selection parameter parser");
- return NULL;
- }
+ GMX_RELEASE_ASSERT(root->type == SEL_EXPRESSION || root->type == SEL_MODIFIER,
+ "Unsupported root element for selection parameter parser");
+ SelectionTreeElementPointer child;
/* Create a subexpression reference element if necessary */
if (expr->type == SEL_SUBEXPRREF)
{
}
else
{
- child = _gmx_selelem_create(SEL_SUBEXPRREF);
- if (!child)
- {
- return NULL;
- }
+ child.reset(new SelectionTreeElement(SEL_SUBEXPRREF));
_gmx_selelem_set_vtype(child, expr->v.type);
child->child = expr;
}
/* Setup the child element */
- child->flags &= ~SEL_ALLOCVAL;
+ child->flags &= ~SEL_ALLOCVAL;
child->u.param = param;
if (child->v.type != param->val.type)
{
_gmx_selparser_error(scanner, "invalid expression value");
- goto on_error;
- }
- rc = _gmx_selelem_update_flags(child, scanner);
- if (rc != 0)
- {
- goto on_error;
+ // FIXME: Use exceptions.
+ return SelectionTreeElementPointer();
}
+ _gmx_selelem_update_flags(child);
if ((child->flags & SEL_DYNAMIC) && !(param->flags & SPAR_DYNAMIC))
{
_gmx_selparser_error(scanner, "dynamic values not supported");
- goto on_error;
+ // FIXME: Use exceptions.
+ return SelectionTreeElementPointer();
}
if (!(child->flags & SEL_DYNAMIC))
{
/* Put the child element in the correct place */
place_child(root, child, param);
return child;
-
-on_error:
- if (child != expr)
- {
- _gmx_selelem_free(child);
- }
- return NULL;
}
/*! \brief
* Parses an expression value for a parameter that takes a variable number of values.
- *
- * \param[in] nval Number of values in \p values.
- * \param[in] values Pointer to the list of values.
+ *
+ * \param[in] values List of values.
* \param param Parameter to parse.
* \param root Selection element to which child expressions are added.
* \param[in] scanner Scanner data structure.
* \returns true if the values were parsed successfully, false otherwise.
*/
static bool
-parse_values_varnum_expr(int nval, t_selexpr_value *values,
- gmx_ana_selparam_t *param, t_selelem *root,
- void *scanner)
+parse_values_varnum_expr(const SelectionParserValueList &values,
+ gmx_ana_selparam_t *param,
+ const SelectionTreeElementPointer &root,
+ void *scanner)
{
- t_selexpr_value *value;
- t_selelem *child;
-
- if (nval != 1 || !values->bExpr)
- {
- GMX_ERROR_NORET(gmx::eeInternalError, "Invalid expression value");
- return false;
- }
+ GMX_RELEASE_ASSERT(values.size() == 1 && values.front().hasExpressionValue(),
+ "Called with an invalid type of value");
- value = values;
- child = add_child(root, param, value->u.expr, scanner);
- value->u.expr = NULL;
+ SelectionTreeElementPointer child
+ = add_child(root, param, values.front().expr, scanner);
if (!child)
{
return false;
* This function is used internally by parse_values_std().
*/
static bool
-set_expr_value_store(t_selelem *sel, gmx_ana_selparam_t *param, int i,
- void *scanner)
+set_expr_value_store(const SelectionTreeElementPointer &sel,
+ gmx_ana_selparam_t *param, int i, void *scanner)
{
if (sel->v.type != GROUP_VALUE && !(sel->flags & SEL_SINGLEVAL))
{
case POS_VALUE: sel->v.u.p = ¶m->val.u.p[i]; break;
case GROUP_VALUE: sel->v.u.g = ¶m->val.u.g[i]; break;
default: /* Error */
- GMX_ERROR_NORET(gmx::eeInternalError, "Invalid value type");
- return false;
+ GMX_THROW(gmx::InternalError("Invalid value type"));
}
- sel->v.nr = 1;
+ sel->v.nr = 1;
sel->v.nalloc = -1;
return true;
}
/*! \brief
* Parses the values for a parameter that takes a constant number of values.
- *
- * \param[in] nval Number of values in \p values.
- * \param[in] values Pointer to the list of values.
+ *
+ * \param[in] values List of values.
* \param param Parameter to parse.
* \param root Selection element to which child expressions are added.
* \param[in] scanner Scanner data structure.
* is stored, each as a separate value.
*/
static bool
-parse_values_std(int nval, t_selexpr_value *values, gmx_ana_selparam_t *param,
- t_selelem *root, void *scanner)
+parse_values_std(const SelectionParserValueList &values,
+ gmx_ana_selparam_t *param,
+ const SelectionTreeElementPointer &root, void *scanner)
{
- t_selexpr_value *value;
- t_selelem *child;
int i, j;
bool bDynamic;
/* Handle atom-valued parameters */
if (param->flags & SPAR_ATOMVAL)
{
- if (nval > 1)
+ if (values.size() > 1)
{
_gmx_selparser_error(scanner, "more than one value not supported");
return false;
}
- value = values;
- if (value->bExpr)
+ if (values.front().hasExpressionValue())
{
- child = add_child(root, param, value->u.expr, scanner);
- value->u.expr = NULL;
+ SelectionTreeElementPointer child
+ = add_child(root, param, values.front().expr, scanner);
if (!child)
{
return false;
param->flags &= ~SPAR_DYNAMIC;
}
- value = values;
- i = 0;
+ i = 0;
bDynamic = false;
- while (value && i < param->val.nr)
+ SelectionParserValueList::const_iterator value;
+ for (value = values.begin(); value != values.end() && i < param->val.nr; ++value)
{
if (value->type != param->val.type)
{
_gmx_selparser_error(scanner, "incorrect value skipped");
- value = value->next;
continue;
}
- if (value->bExpr)
+ if (value->hasExpressionValue())
{
- child = add_child(root, param, value->u.expr, scanner);
- /* Clear the expression from the value once it is stored */
- value->u.expr = NULL;
+ SelectionTreeElementPointer child
+ = add_child(root, param, value->expr, scanner);
/* Check that the expression is valid */
if (!child)
{
param->val.u.r[i] = value->u.r.r1;
break;
case STR_VALUE:
- param->val.u.s[i] = strdup(value->u.s);
+ param->val.u.s[i] = gmx_strdup(value->stringValue().c_str());
break;
case POS_VALUE:
gmx_ana_pos_init_const(¶m->val.u.p[i], value->u.x);
break;
case NO_VALUE:
case GROUP_VALUE:
- GMX_ERROR_NORET(gmx::eeInternalError,
- "Invalid non-expression value");
- return false;
+ GMX_THROW(gmx::InternalError("Invalid non-expression value type"));
}
}
++i;
- value = value->next;
}
- if (value != NULL)
+ if (value != values.end())
{
- _gmx_selparser_error(scanner, "extra values'");
+ _gmx_selparser_error(scanner, "extra values");
return false;
}
if (i < param->val.nr)
* Parses the values for a boolean parameter.
*
* \param[in] name Name by which the parameter was given.
- * \param[in] nval Number of values in \p values.
- * \param[in] values Pointer to the list of values.
+ * \param[in] values List of values.
* \param param Parameter to parse.
* \param[in] scanner Scanner data structure.
* \returns true if the values were parsed successfully, false otherwise.
*/
static bool
-parse_values_bool(const char *name, int nval, t_selexpr_value *values,
+parse_values_bool(const std::string &name,
+ const SelectionParserValueList &values,
gmx_ana_selparam_t *param, void *scanner)
{
- bool bSetNo;
- int len;
-
- if (param->val.type != NO_VALUE)
- {
- GMX_ERROR_NORET(gmx::eeInternalError, "Invalid boolean parameter");
- return false;
- }
- if (nval > 1 || (values && values->type != INT_VALUE))
+ GMX_ASSERT(param->val.type == NO_VALUE,
+ "Boolean parser called for non-boolean parameter");
+ if (values.size() > 1 || (!values.empty() && values.front().type != INT_VALUE))
{
_gmx_selparser_error(scanner, "parameter takes only a yes/no/on/off/0/1 value");
return false;
}
- bSetNo = false;
+ bool bSetNo = false;
/* Check if the parameter name is given with a 'no' prefix */
- len = name != NULL ? strlen(name) : 0;
- if (len > 2 && name[0] == 'n' && name[1] == 'o'
- && strncmp(name+2, param->name, len-2) == 0)
+ if (name.length() > 2 && name[0] == 'n' && name[1] == 'o'
+ && name.compare(2, name.length() - 2, param->name) == 0)
{
bSetNo = true;
}
- if (bSetNo && nval > 0)
+ if (bSetNo && !values.empty())
{
_gmx_selparser_error(scanner, "parameter 'no%s' should not have a value",
param->name);
return false;
}
- if (values && values->u.i.i1 == 0)
+ if (!values.empty() && values.front().u.i.i1 == 0)
{
bSetNo = true;
}
/*! \brief
* Parses the values for an enumeration parameter.
*
- * \param[in] nval Number of values in \p values.
- * \param[in] values Pointer to the list of values.
+ * \param[in] values List of values.
* \param param Parameter to parse.
* \param[in] scanner Scanner data structure.
* \returns true if the values were parsed successfully, false otherwise.
*/
static bool
-parse_values_enum(int nval, t_selexpr_value *values, gmx_ana_selparam_t *param,
- void *scanner)
+parse_values_enum(const SelectionParserValueList &values,
+ gmx_ana_selparam_t *param,
+ void *scanner)
{
- int i, len, match;
-
- if (nval != 1)
+ GMX_ASSERT(param->val.type == STR_VALUE,
+ "Enum parser called for non-string parameter");
+ if (values.size() != 1)
{
_gmx_selparser_error(scanner, "a single value is required");
return false;
}
- if (values->type != STR_VALUE || param->val.type != STR_VALUE)
- {
- GMX_ERROR_NORET(gmx::eeInternalError, "Invalid enum parameter");
- return false;
- }
- if (values->bExpr)
+ const SelectionParserValue &value = values.front();
+ GMX_RELEASE_ASSERT(value.type == param->val.type,
+ "Invalid value type (should have been caught earlier)");
+ if (value.hasExpressionValue())
{
_gmx_selparser_error(scanner, "expression value for enumerated parameter not supported");
return false;
}
- len = strlen(values->u.s);
- i = 1;
- match = 0;
+ const std::string &svalue = value.stringValue();
+ int i = 1;
+ int match = 0;
while (param->val.u.s[i] != NULL)
{
- if (strncmp(values->u.s, param->val.u.s[i], len) == 0)
+ if (gmx::startsWith(param->val.u.s[i], svalue))
{
/* Check if there is a duplicate match */
if (match > 0)
* \param[in,out] values First element in the value list to process.
*/
static void
-convert_const_values(t_selexpr_value *values)
+convert_const_values(SelectionParserValueList *values)
{
- t_selexpr_value *val;
-
- val = values;
- while (val)
+ SelectionParserValueList::iterator value;
+ for (value = values->begin(); value != values->end(); ++value)
{
- if (val->bExpr && val->u.expr->v.type != GROUP_VALUE &&
- val->u.expr->type == SEL_CONST)
+ if (value->hasExpressionValue() && value->expr->v.type != GROUP_VALUE &&
+ value->expr->type == SEL_CONST)
{
- t_selelem *expr = val->u.expr;
- val->bExpr = false;
+ SelectionTreeElementPointer expr = value->expr;
switch (expr->v.type)
{
case INT_VALUE:
- val->u.i.i1 = val->u.i.i2 = expr->v.u.i[0];
+ *value = SelectionParserValue::createInteger(expr->v.u.i[0]);
break;
case REAL_VALUE:
- val->u.r.r1 = val->u.r.r2 = expr->v.u.r[0];
+ *value = SelectionParserValue::createReal(expr->v.u.r[0]);
break;
case STR_VALUE:
- val->u.s = expr->v.u.s[0];
+ *value = SelectionParserValue::createString(expr->v.u.s[0]);
break;
case POS_VALUE:
- copy_rvec(expr->v.u.p->x[0], val->u.x);
+ *value = SelectionParserValue::createPosition(expr->v.u.p->x[0]);
break;
default:
- GMX_ERROR_NORET(gmx::eeInternalError,
- "Unsupported value type");
- break;
+ GMX_THROW(gmx::InternalError(
+ "Unsupported constant expression value type"));
}
- _gmx_selelem_free(expr);
}
- val = val->next;
}
}
* have been processed, no matter is there was an error or not.
*/
bool
-_gmx_sel_parse_params(t_selexpr_param *pparams, int nparam, gmx_ana_selparam_t *params,
- t_selelem *root, void *scanner)
+_gmx_sel_parse_params(const gmx::SelectionParserParameterList &pparams,
+ int nparam, gmx_ana_selparam_t *params,
+ const gmx::SelectionTreeElementPointer &root,
+ void *scanner)
{
gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
- t_selexpr_param *pparam;
- gmx_ana_selparam_t *oparam;
- bool bOk, rc;
- int i;
+ gmx_ana_selparam_t *oparam;
+ bool bOk, rc;
+ int i;
/* Check that the value pointers of SPAR_VARNUM parameters are NULL and
* that they are not NULL for other parameters */
bOk = true;
for (i = 0; i < nparam; ++i)
{
- std::string contextStr = gmx::formatString("In parameter '%s'", params[i].name);
+ std::string contextStr = gmx::formatString("In parameter '%s'", params[i].name);
gmx::MessageStringContext context(errors, contextStr);
if (params[i].val.type != POS_VALUE && (params[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
{
}
if (!bOk)
{
- _gmx_selexpr_free_params(pparams);
return false;
}
/* Parse the parameters */
- pparam = pparams;
- i = 0;
- while (pparam)
+ i = 0;
+ SelectionParserParameterList::const_iterator pparam;
+ for (pparam = pparams.begin(); pparam != pparams.end(); ++pparam)
{
std::string contextStr;
/* Find the parameter and make some checks */
- if (pparam->name != NULL)
+ if (!pparam->name().empty())
{
- contextStr = gmx::formatString("In parameter '%s'", pparam->name);
- i = -1;
- oparam = gmx_ana_selparam_find(pparam->name, nparam, params);
+ contextStr = gmx::formatString("In parameter '%s'", pparam->name().c_str());
+ i = -1;
+ oparam = gmx_ana_selparam_find(pparam->name().c_str(), nparam, params);
}
else if (i >= 0)
{
contextStr = gmx::formatString("In value %d", i + 1);
- oparam = ¶ms[i];
+ oparam = ¶ms[i];
if (oparam->name != NULL)
{
oparam = NULL;
_gmx_selparser_error(scanner, "too many NULL parameters provided");
bOk = false;
- pparam = pparam->next;
continue;
}
++i;
{
_gmx_selparser_error(scanner, "all NULL parameters should appear in the beginning of the list");
bOk = false;
- pparam = pparam->next;
continue;
}
gmx::MessageStringContext context(errors, contextStr);
{
_gmx_selparser_error(scanner, "unknown parameter skipped");
bOk = false;
- goto next_param;
+ continue;
}
- if (oparam->val.type != NO_VALUE && pparam->value == NULL)
+ if (oparam->val.type != NO_VALUE && pparam->values().empty())
{
- GMX_ASSERT(pparam->nval == 0, "Inconsistent values and value count");
_gmx_selparser_error(scanner, "no value provided");
bOk = false;
- goto next_param;
+ continue;
}
if (oparam->flags & SPAR_SET)
{
_gmx_selparser_error(scanner, "parameter set multiple times, extra values skipped");
bOk = false;
- goto next_param;
+ continue;
}
oparam->flags |= SPAR_SET;
/* Process the values for the parameter */
- convert_const_values(pparam->value);
- if (convert_values(pparam->value, oparam->val.type, scanner) != 0)
+ convert_const_values(pparam->values_.get());
+ if (convert_values(pparam->values_.get(), oparam->val.type, scanner) != 0)
{
_gmx_selparser_error(scanner, "invalid value");
bOk = false;
- goto next_param;
+ continue;
}
if (oparam->val.type == NO_VALUE)
{
- rc = parse_values_bool(pparam->name, pparam->nval, pparam->value, oparam, scanner);
+ rc = parse_values_bool(pparam->name(), pparam->values(), oparam, scanner);
}
else if (oparam->flags & SPAR_RANGES)
{
- rc = parse_values_range(pparam->nval, pparam->value, oparam, scanner);
+ rc = parse_values_range(pparam->values(), oparam, scanner);
}
else if (oparam->flags & SPAR_VARNUM)
{
- if (pparam->nval == 1 && pparam->value->bExpr)
+ if (pparam->values().size() == 1
+ && pparam->values().front().hasExpressionValue())
{
- rc = parse_values_varnum_expr(pparam->nval, pparam->value, oparam, root, scanner);
+ rc = parse_values_varnum_expr(pparam->values(), oparam, root, scanner);
}
else
{
- rc = parse_values_varnum(pparam->nval, pparam->value, oparam, root, scanner);
+ rc = parse_values_varnum(pparam->values(), oparam, root, scanner);
}
}
else if (oparam->flags & SPAR_ENUMVAL)
{
- rc = parse_values_enum(pparam->nval, pparam->value, oparam, scanner);
+ rc = parse_values_enum(pparam->values(), oparam, scanner);
}
else
{
- rc = parse_values_std(pparam->nval, pparam->value, oparam, root, scanner);
+ rc = parse_values_std(pparam->values(), oparam, root, scanner);
}
if (!rc)
{
bOk = false;
}
- /* Advance to the next parameter */
-next_param:
- pparam = pparam->next;
}
/* Check that all required parameters are present */
for (i = 0; i < nparam; ++i)
}
}
- _gmx_selexpr_free_params(pparams);
return bOk;
}