Only accept exact matches for selection keywords
authorTeemu Murtola <teemu.murtola@gmail.com>
Mon, 29 Jun 2015 17:48:33 +0000 (20:48 +0300)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Mon, 29 Jun 2015 19:07:07 +0000 (21:07 +0200)
The selection parser tried to be nice to the user and also accept
unambiguous prefixes of keywords, but this also has a lot of side
effects that can be confusing (e.g., it was impossible to create
variables that had names that were prefixes to keywords or to other
variable names).  Additionally, this was the only case where user input
could cause an exception during tokenization of the input string, and
that wasn't handled very well during interactive input, either (it
caused the whole program to stop, instead of just reporting the error
like is done for other parsing errors).

Remove the logic, and only make the parser accept exact matches.
Add a few synonyms for keywords where there is a natural abbreviation.

Change-Id: I6041baa2f5a3b7dab87c3d5991e883d2d74ace66

src/gromacs/selection/scanner_internal.cpp
src/gromacs/selection/selmethod.cpp
src/gromacs/selection/symrec.cpp
src/gromacs/selection/symrec.h
src/gromacs/selection/tests/refdata/SelectionCollectionDataTest_HandlesBeta.xml

index fc20e46890510bb666bcaa2f5790bbfd04846861..1f3be505e5d192e406ce9824d30b916a1942ad54 100644 (file)
@@ -281,7 +281,7 @@ _gmx_sel_lexer_process_identifier(YYSTYPE *yylval, YYLTYPE *yylloc,
 
     /* Check if the identifier matches with a symbol */
     const gmx::SelectionParserSymbol *symbol
-        = state->sc->symtab->findSymbol(std::string(yytext, yyleng), false);
+        = state->sc->symtab->findSymbol(std::string(yytext, yyleng));
     /* If there is no match, return the token as a string */
     if (!symbol)
     {
index 6524f2fec92f83ed86e2b1fb37d06e5cd7a79f86..9c22df9e320b3fe4de83d4c382b03078f58c6fe1 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2009,2010,2011,2012,2014, by the GROMACS development team, led by
+ * Copyright (c) 2009,2010,2011,2012,2014,2015, 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.
@@ -154,12 +154,15 @@ static const t_register_method smtable_def[] = {
     {NULL,         &sm_altloc},
     {NULL,         &sm_occupancy},
     {NULL,         &sm_betafactor},
+    {"beta",       &sm_betafactor},
     {NULL,         &sm_x},
     {NULL,         &sm_y},
     {NULL,         &sm_z},
 
     {NULL,         &sm_distance},
+    {"dist",       &sm_distance},
     {NULL,         &sm_mindistance},
+    {"mindist",    &sm_mindistance},
     {NULL,         &sm_within},
     {NULL,         &sm_insolidangle},
     {NULL,         &sm_same},
@@ -386,7 +389,7 @@ check_params(FILE *fp, const char *name, int nparams, gmx_ana_selparam_t param[]
             continue;
         }
         /* Check that the name does not conflict with a method */
-        if (symtab.findSymbol(param[i].name, true))
+        if (symtab.findSymbol(param[i].name))
         {
             report_param_error(fp, name, param[i].name, "error: name conflicts with another method or a keyword");
             bOk = false;
index cd648f60efc0261e932ed93050c6a9b20a44774d..5c45cc33ccd738cf63356cec38f3bdb5bc2dee76 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2009,2010,2011,2012,2013,2014, by the GROMACS development team, led by
+ * Copyright (c) 2009,2010,2011,2012,2013,2014,2015, 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.
@@ -315,8 +315,7 @@ SelectionParserSymbolTable::~SelectionParserSymbolTable()
 }
 
 const SelectionParserSymbol *
-SelectionParserSymbolTable::findSymbol(const std::string &name,
-                                       bool               bExact) const
+SelectionParserSymbolTable::findSymbol(const std::string &name) const
 {
     Impl::SymbolMap::const_iterator sym = impl_->symbols_.lower_bound(name);
     if (sym == impl_->symbols_.end())
@@ -327,20 +326,6 @@ SelectionParserSymbolTable::findSymbol(const std::string &name,
     {
         return sym->second.get();
     }
-    if (!bExact && startsWith(sym->second->name(), name))
-    {
-        Impl::SymbolMap::const_iterator next = sym;
-        ++next;
-        if (next != impl_->symbols_.end()
-            && startsWith(next->second->name(), name))
-        {
-            GMX_THROW(InvalidInputError("'" + name + "' is ambiguous"));
-        }
-        if (sym->second->type() == SelectionParserSymbol::MethodSymbol)
-        {
-            return sym->second.get();
-        }
-    }
     return NULL;
 }
 
index d544bddad48ecdb13f223389aec2384a42539696..9c29aae6ba046df3021f5c5eb0eda9095048ac61 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2009,2010,2011,2012,2013,2014, by the GROMACS development team, led by
+ * Copyright (c) 2009,2010,2011,2012,2013,2014,2015, 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.
@@ -217,19 +217,13 @@ class SelectionParserSymbolTable
          * Finds a symbol by name.
          *
          * \param[in] name   Symbol name to find.
-         * \param[in] bExact If false, symbols that begin with \p name are also
-         *      considered.
          * \returns   Pointer to the symbol with name \p name, or
          *      NULL if not found.
-         * \throws    InvalidInputError if \p bExact is false and an ambiguous
-         *      symbol is provided.
          *
-         * If no exact match is found and \p bExact is false, returns a symbol
-         * that begins with \p name if a unique matching symbol is found.
-         * Only selection methods are considered for this inexact match.
+         * Does not throw.
          */
         const SelectionParserSymbol *
-        findSymbol(const std::string &name, bool bExact) const;
+        findSymbol(const std::string &name) const;
 
         /*! \brief
          * Returns the start iterator for iterating symbols of a given type.
index a03266b272c1b9884040c03f86e65cd73d7c28db..d670e98d8c5f5a6f0bf0ad0f3a2435fe7926164f 100644 (file)
@@ -4,12 +4,12 @@
   <ParsedSelections Name="Parsed">
     <ParsedSelection Name="Selection1">
       <String Name="Input">beta 0</String>
-      <String Name="Text">betafactor 0</String>
+      <String Name="Text">beta 0</String>
       <Bool Name="Dynamic">false</Bool>
     </ParsedSelection>
     <ParsedSelection Name="Selection2">
       <String Name="Input">beta &gt;= 0.3</String>
-      <String Name="Text">betafactor &gt;= 0.3</String>
+      <String Name="Text">beta &gt;= 0.3</String>
       <Bool Name="Dynamic">false</Bool>
     </ParsedSelection>
   </ParsedSelections>