Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / selection / scanner_internal.cpp
index f3ddf929711c0fcce416ac6d97e742b2f2a8a2bb..f13031fd1a4e3abe373d96630217292775a97794 100644 (file)
@@ -1,32 +1,36 @@
 /*
+ * This file is part of the GROMACS molecular simulation package.
  *
- *                This source code is part of
+ * Copyright (c) 2009,2010,2011,2012,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 Helper functions for the selection tokenizer.
@@ -34,7 +38,7 @@
  * This file implements the functions in the headers scanner.h and
  * scanner_internal.h.
  *
- * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
  * \ingroup module_selection
  */
 /*! \cond
  * \ingroup module_selection
  * \endcond
  */
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
+#include "gmxpre.h"
+
+#include "scanner_internal.h"
 
 #include <stdlib.h>
 #include <string.h>
 
-#include "typedefs.h"
-#include "smalloc.h"
-#include "string2.h"
+#include <string>
 
-#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 "parser.h"
 #include "parsetree.h"
+#include "scanner.h"
 #include "selectioncollection-impl.h"
 #include "selelem.h"
 #include "selmethod.h"
 #include "symrec.h"
 
-#include "parser.h"
-#include "scanner.h"
-#include "scanner_internal.h"
-
-//! Step in which the allocated memory for pretty-printed input is incremented.
+/*! \brief
+ * Step in which the allocated memory for pretty-printed input is incremented.
+ */
 #define STRSTORE_ALLOCSTEP 1000
 
 /* These are defined as macros in the generated scanner_flex.h.
 #undef yytext
 #undef yyleng
 
+/*! \brief
+ * Handles initialization of method parameter token.
+ */
 static int
 init_param_token(YYSTYPE *yylval, gmx_ana_selparam_t *param, bool bBoolNo)
 {
     if (bBoolNo)
     {
         GMX_RELEASE_ASSERT(param->name != NULL,
-                "bBoolNo should only be set for a parameters with a name");
+                           "bBoolNo should only be set for a parameters with a name");
         snew(yylval->str, strlen(param->name) + 3);
         yylval->str[0] = 'n';
         yylval->str[1] = 'o';
@@ -97,11 +104,14 @@ init_param_token(YYSTYPE *yylval, gmx_ana_selparam_t *param, bool bBoolNo)
     }
     else
     {
-        yylval->str = param->name ? strdup(param->name) : NULL;
+        yylval->str = param->name ? gmx_strdup(param->name) : NULL;
     }
     return PARAM;
 }
 
+/*! \brief
+ * Processes a selection method token.
+ */
 static int
 init_method_token(YYSTYPE *yylval, gmx_ana_selmethod_t *method, bool bPosMod,
                   gmx_sel_lexer_t *state)
@@ -124,10 +134,11 @@ init_method_token(YYSTYPE *yylval, gmx_ana_selmethod_t *method, bool bPosMod,
             case STR_VALUE:   return KEYWORD_STR;
             case GROUP_VALUE: return KEYWORD_GROUP;
             default:
-                GMX_ERROR_NORET(gmx::eeInternalError, "Unsupported keyword type");
-                return INVALID;
+                GMX_THROW(gmx::InternalError("Unsupported keyword type"));
         }
-    } else {
+    }
+    else
+    {
         /* Method with parameters or a modifier */
         if (method->flags & SMETH_MODIFIER)
         {
@@ -164,8 +175,7 @@ init_method_token(YYSTYPE *yylval, gmx_ana_selmethod_t *method, bool bPosMod,
             case GROUP_VALUE: return METHOD_GROUP;
             default:
                 --state->msp;
-                GMX_ERROR_NORET(gmx::eeInternalError, "Unsupported method type");
-                return INVALID;
+                GMX_THROW(gmx::InternalError("Unsupported method type"));
         }
     }
     return INVALID; /* Should not be reached */
@@ -176,7 +186,7 @@ _gmx_sel_lexer_process_pending(YYSTYPE *yylval, gmx_sel_lexer_t *state)
 {
     if (state->nextparam)
     {
-        gmx_ana_selparam_t *param = state->nextparam;
+        gmx_ana_selparam_t *param   = state->nextparam;
         bool                bBoolNo = state->bBoolNo;
 
         if (state->neom > 0)
@@ -207,15 +217,12 @@ int
 _gmx_sel_lexer_process_identifier(YYSTYPE *yylval, char *yytext, size_t yyleng,
                                   gmx_sel_lexer_t *state)
 {
-    gmx_sel_symrec_t *symbol;
-    e_symbol_t        symtype;
-
     /* Check if the identifier matches with a parameter name */
     if (state->msp >= 0)
     {
-        gmx_ana_selparam_t *param = NULL;
+        gmx_ana_selparam_t *param   = NULL;
         bool                bBoolNo = false;
-        int                 sp = state->msp;
+        int                 sp      = state->msp;
         while (!param && sp >= 0)
         {
             int             i;
@@ -237,7 +244,7 @@ _gmx_sel_lexer_process_identifier(YYSTYPE *yylval, char *yytext, size_t yyleng,
                     && yyleng > 2 && yytext[0] == 'n' && yytext[1] == 'o'
                     && !strncmp(state->mstack[sp]->param[i].name, yytext+2, yyleng-2))
                 {
-                    param = &state->mstack[sp]->param[i];
+                    param   = &state->mstack[sp]->param[i];
                     bBoolNo = true;
                     break;
                 }
@@ -255,7 +262,7 @@ _gmx_sel_lexer_process_identifier(YYSTYPE *yylval, char *yytext, size_t yyleng,
             }
             if (sp < state->msp)
             {
-                state->neom = state->msp - sp - 1;
+                state->neom      = state->msp - sp - 1;
                 state->nextparam = param;
                 state->bBoolNo   = bBoolNo;
                 return END_OF_METHOD;
@@ -266,7 +273,8 @@ _gmx_sel_lexer_process_identifier(YYSTYPE *yylval, char *yytext, size_t yyleng,
     }
 
     /* Check if the identifier matches with a symbol */
-    symbol = _gmx_sel_find_symbol_len(state->sc->symtab, yytext, yyleng, false);
+    const gmx::SelectionParserSymbol *symbol
+        = state->sc->symtab->findSymbol(std::string(yytext, yyleng), false);
     /* If there is no match, return the token as a string */
     if (!symbol)
     {
@@ -274,21 +282,19 @@ _gmx_sel_lexer_process_identifier(YYSTYPE *yylval, char *yytext, size_t yyleng,
         _gmx_sel_lexer_add_token(yytext, yyleng, state);
         return IDENTIFIER;
     }
-    _gmx_sel_lexer_add_token(_gmx_sel_sym_name(symbol), -1, state);
-    symtype = _gmx_sel_sym_type(symbol);
+    _gmx_sel_lexer_add_token(symbol->name().c_str(), -1, state);
+    gmx::SelectionParserSymbol::SymbolType symtype = symbol->type();
     /* Reserved symbols should have been caught earlier */
-    if (symtype == SYMBOL_RESERVED)
+    if (symtype == gmx::SelectionParserSymbol::ReservedSymbol)
     {
-        GMX_ERROR_NORET(gmx::eeInternalError,
-                        "Mismatch between tokenizer and reserved symbol table");
-        return INVALID;
+        GMX_THROW(gmx::InternalError(gmx::formatString(
+                                             "Mismatch between tokenizer and reserved symbol table (for '%s')",
+                                             symbol->name().c_str())));
     }
     /* For variable symbols, return the type of the variable value */
-    if (symtype == SYMBOL_VARIABLE)
+    if (symtype == gmx::SelectionParserSymbol::VariableSymbol)
     {
-        t_selelem *var;
-
-        var = _gmx_sel_sym_value_var(symbol);
+        gmx::SelectionTreeElementPointer var = symbol->variableValue();
         /* Return simple tokens for constant variables */
         if (var->type == SEL_CONST)
         {
@@ -303,12 +309,10 @@ _gmx_sel_lexer_process_identifier(YYSTYPE *yylval, char *yytext, size_t yyleng,
                 case POS_VALUE:
                     break;
                 default:
-                    GMX_ERROR_NORET(gmx::eeInternalError,
-                                    "Unsupported variable type");
-                    return INVALID;
+                    GMX_THROW(gmx::InternalError("Unsupported variable type"));
             }
         }
-        yylval->sel = var;
+        yylval->sel = new gmx::SelectionTreeElementPointer(var);
         switch (var->v.type)
         {
             case INT_VALUE:   return VARIABLE_NUMERIC;
@@ -316,26 +320,24 @@ _gmx_sel_lexer_process_identifier(YYSTYPE *yylval, char *yytext, size_t yyleng,
             case POS_VALUE:   return VARIABLE_POS;
             case GROUP_VALUE: return VARIABLE_GROUP;
             default:
-                GMX_ERROR_NORET(gmx::eeInternalError,
-                                "Unsupported variable type");
+                delete yylval->sel;
+                GMX_THROW(gmx::InternalError("Unsupported variable type"));
                 return INVALID;
         }
-        return INVALID; /* Should not be reached. */
+        /* This position should not be reached. */
     }
     /* For method symbols, return the correct type */
-    if (symtype == SYMBOL_METHOD)
+    if (symtype == gmx::SelectionParserSymbol::MethodSymbol)
     {
-        gmx_ana_selmethod_t *method;
-
-        method = _gmx_sel_sym_value_method(symbol);
+        gmx_ana_selmethod_t *method = symbol->methodValue();
         return init_method_token(yylval, method, state->prev_pos_kw > 0, state);
     }
     /* For position symbols, we need to return KEYWORD_POS, but we also need
      * some additional handling. */
-    if (symtype == SYMBOL_POS)
+    if (symtype == gmx::SelectionParserSymbol::PositionSymbol)
     {
-        state->bMatchOf = true;
-        yylval->str = _gmx_sel_sym_name(symbol);
+        state->bMatchOf    = true;
+        yylval->str        = gmx_strdup(symbol->name().c_str());
         state->prev_pos_kw = 2;
         return KEYWORD_POS;
     }
@@ -367,7 +369,7 @@ _gmx_sel_lexer_add_token(const char *str, int len, gmx_sel_lexer_t *state)
     }
     /* Append the token to the stored string */
     strncpy(state->pselstr + state->pslen, str, len);
-    state->pslen += len;
+    state->pslen                += len;
     state->pselstr[state->pslen] = 0;
 }
 
@@ -430,7 +432,7 @@ _gmx_sel_free_lexer(yyscan_t scanner)
 }
 
 void
-_gmx_sel_set_lexer_error_reporter(yyscan_t scanner,
+_gmx_sel_set_lexer_error_reporter(yyscan_t                     scanner,
                                   gmx::MessageStringCollector *errors)
 {
     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
@@ -438,7 +440,7 @@ _gmx_sel_set_lexer_error_reporter(yyscan_t scanner,
 }
 
 void
-_gmx_sel_lexer_set_exception(yyscan_t scanner,
+_gmx_sel_lexer_set_exception(yyscan_t                    scanner,
                              const boost::exception_ptr &ex)
 {
     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);