2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009-2018, The GROMACS development team.
5 * Copyright (c) 2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
37 * \brief Helper functions for the selection tokenizer.
39 * This file implements the functions in the headers scanner.h and
42 * \author Teemu Murtola <teemu.murtola@gmail.com>
43 * \ingroup module_selection
46 * \internal \file scanner_flex.h
47 * \brief Generated (from scanner.l) header file by Flex.
49 * This file contains definitions of functions that are needed in
50 * scanner_internal.cpp.
52 * \ingroup module_selection
57 #include "scanner_internal.h"
64 #include "gromacs/utility/cstringutil.h"
65 #include "gromacs/utility/exceptions.h"
66 #include "gromacs/utility/gmxassert.h"
67 #include "gromacs/utility/smalloc.h"
68 #include "gromacs/utility/stringutil.h"
71 #include "parsetree.h"
73 #include "selectioncollection_impl.h"
75 #include "selmethod.h"
78 /* These are defined as macros in the generated scanner_flex.h.
79 * We undefine them here to have them as variable names in the subroutines.
80 * There are other ways of doing this, but this is probably the easiest. */
86 * Handles initialization of method parameter token.
88 static int init_param_token(YYSTYPE* yylval, gmx_ana_selparam_t* param, bool bBoolNo)
92 GMX_RELEASE_ASSERT(param->name != nullptr,
93 "bBoolNo should only be set for a parameters with a name");
94 snew(yylval->str, strlen(param->name) + 3);
97 strcpy(yylval->str + 2, param->name);
101 yylval->str = param->name ? gmx_strdup(param->name) : nullptr;
107 * Processes a selection method token.
109 static int init_method_token(YYSTYPE* yylval,
111 const gmx::SelectionParserSymbol* symbol,
113 gmx_sel_lexer_t* state)
115 gmx_ana_selmethod_t* method = symbol->methodValue();
116 /* If the previous token was not KEYWORD_POS, return EMPTY_POSMOD
117 * before the actual method to work around a limitation in Bison. */
118 if (!bPosMod && method->type != POS_VALUE)
120 state->nextMethodSymbol = symbol;
121 _gmx_sel_lexer_add_token(yylloc, nullptr, 0, state);
124 _gmx_sel_lexer_add_token(yylloc, symbol->name().c_str(), -1, state);
125 yylval->meth = method;
126 if (!(method->flags & SMETH_MODIFIER) && method->nparams == 0)
129 switch (method->type)
132 case REAL_VALUE: state->bMatchOf = true; return KEYWORD_NUMERIC;
133 case STR_VALUE: return KEYWORD_STR;
134 case GROUP_VALUE: return KEYWORD_GROUP;
135 default: GMX_THROW(gmx::InternalError("Unsupported keyword type"));
140 /* Method with parameters or a modifier */
141 if (method->flags & SMETH_MODIFIER)
143 /* Remove all methods from the stack */
145 if (method->param[1].name == nullptr)
147 state->nextparam = &method->param[1];
152 if (method->param[0].name == nullptr)
154 state->nextparam = &method->param[0];
158 if (state->msp >= state->mstack_alloc)
160 state->mstack_alloc += 10;
161 srenew(state->mstack, state->mstack_alloc);
163 state->mstack[state->msp] = method;
164 if (method->flags & SMETH_MODIFIER)
168 switch (method->type)
170 case INT_VALUE: // Intended fall through
171 case REAL_VALUE: return METHOD_NUMERIC;
172 case POS_VALUE: return METHOD_POS;
173 case GROUP_VALUE: return METHOD_GROUP;
174 default: --state->msp; GMX_THROW(gmx::InternalError("Unsupported method type"));
179 int _gmx_sel_lexer_process_pending(YYSTYPE* yylval, YYLTYPE* yylloc, gmx_sel_lexer_t* state)
181 if (state->nextparam)
183 gmx_ana_selparam_t* param = state->nextparam;
184 bool bBoolNo = state->bBoolNo;
189 _gmx_sel_lexer_add_token(yylloc, nullptr, 0, state);
190 return END_OF_METHOD;
192 state->nextparam = nullptr;
193 state->bBoolNo = false;
194 _gmx_sel_lexer_add_token(yylloc, param->name, -1, state);
195 return init_param_token(yylval, param, bBoolNo);
197 if (state->prev_pos_kw > 0)
199 --state->prev_pos_kw;
201 if (state->nextMethodSymbol)
203 const gmx::SelectionParserSymbol* symbol = state->nextMethodSymbol;
204 state->nextMethodSymbol = nullptr;
205 return init_method_token(yylval, yylloc, symbol, true, state);
210 int _gmx_sel_lexer_process_identifier(YYSTYPE* yylval, YYLTYPE* yylloc, char* yytext, size_t yyleng, gmx_sel_lexer_t* state)
212 /* Check if the identifier matches with a parameter name */
215 gmx_ana_selparam_t* param = nullptr;
216 bool bBoolNo = false;
218 while (!param && sp >= 0)
221 for (i = 0; i < state->mstack[sp]->nparams; ++i)
223 /* Skip NULL parameters and too long parameters */
224 if (state->mstack[sp]->param[i].name == nullptr
225 || strlen(state->mstack[sp]->param[i].name) > yyleng)
229 if (!strncmp(state->mstack[sp]->param[i].name, yytext, yyleng))
231 param = &state->mstack[sp]->param[i];
234 /* Check separately for a 'no' prefix on boolean parameters */
235 if (state->mstack[sp]->param[i].val.type == NO_VALUE && yyleng > 2
236 && yytext[0] == 'n' && yytext[1] == 'o'
237 && !strncmp(state->mstack[sp]->param[i].name, yytext + 2, yyleng - 2))
239 param = &state->mstack[sp]->param[i];
251 if (param->val.type == NO_VALUE && !bBoolNo)
253 state->bMatchBool = true;
257 state->neom = state->msp - sp - 1;
258 state->nextparam = param;
259 state->bBoolNo = bBoolNo;
260 return END_OF_METHOD;
262 _gmx_sel_lexer_add_token(yylloc, param->name, -1, state);
263 return init_param_token(yylval, param, bBoolNo);
267 /* Check if the identifier matches with a symbol */
268 const gmx::SelectionParserSymbol* symbol = state->sc->symtab->findSymbol(std::string(yytext, yyleng));
269 /* If there is no match, return the token as a string */
272 yylval->str = gmx_strndup(yytext, yyleng);
273 _gmx_sel_lexer_add_token(yylloc, yytext, yyleng, state);
276 gmx::SelectionParserSymbol::SymbolType symtype = symbol->type();
277 /* For method symbols, we need some extra processing. */
278 if (symtype == gmx::SelectionParserSymbol::MethodSymbol)
280 return init_method_token(yylval, yylloc, symbol, state->prev_pos_kw > 0, state);
282 _gmx_sel_lexer_add_token(yylloc, symbol->name().c_str(), -1, state);
283 /* Reserved symbols should have been caught earlier */
284 if (symtype == gmx::SelectionParserSymbol::ReservedSymbol)
286 GMX_THROW(gmx::InternalError(gmx::formatString(
287 "Mismatch between tokenizer and reserved symbol table (for '%s')", symbol->name().c_str())));
289 /* For variable symbols, return the type of the variable value */
290 if (symtype == gmx::SelectionParserSymbol::VariableSymbol)
292 const gmx::SelectionTreeElementPointer& var = symbol->variableValue();
293 /* Return simple tokens for constant variables */
294 if (var->type == SEL_CONST)
298 case INT_VALUE: yylval->i = var->v.u.i[0]; return TOK_INT;
299 case REAL_VALUE: yylval->r = var->v.u.r[0]; return TOK_REAL;
300 case POS_VALUE: break;
301 default: GMX_THROW(gmx::InternalError("Unsupported variable type"));
304 yylval->sel = new gmx::SelectionTreeElementPointer(var);
307 case INT_VALUE: // Intended fall through
308 case REAL_VALUE: return VARIABLE_NUMERIC;
309 case POS_VALUE: return VARIABLE_POS;
310 case GROUP_VALUE: return VARIABLE_GROUP;
311 default: delete yylval->sel; GMX_THROW(gmx::InternalError("Unsupported variable type"));
313 /* This position should not be reached. */
315 /* For position symbols, we need to return KEYWORD_POS, but we also need
316 * some additional handling. */
317 if (symtype == gmx::SelectionParserSymbol::PositionSymbol)
319 state->bMatchOf = true;
320 yylval->str = gmx_strdup(symbol->name().c_str());
321 state->prev_pos_kw = 2;
324 /* Should not be reached */
328 void _gmx_sel_lexer_add_token(YYLTYPE* yylloc, const char* str, int len, gmx_sel_lexer_t* state)
330 yylloc->startIndex = yylloc->endIndex = state->pselstr.size();
331 /* Do nothing if the string is empty, or if it is a space and there is
332 * no other text yet, or if there already is a space. */
333 if (!str || len == 0 || strlen(str) == 0
334 || (str[0] == ' ' && str[1] == 0 && (state->pselstr.empty() || state->pselstr.back() == ' ')))
342 /* Append the token to the stored string */
343 state->pselstr.append(str, len);
344 yylloc->endIndex = state->pselstr.size();
347 void _gmx_sel_init_lexer(yyscan_t* scannerp,
348 struct gmx_ana_selcollection_t* sc,
349 gmx::TextWriter* statusWriter,
352 struct gmx_ana_indexgrps_t* grps)
354 int rc = _gmx_sel_yylex_init(scannerp);
357 // TODO: Throw a more representative exception.
358 GMX_THROW(gmx::InternalError("Lexer initialization failed"));
361 gmx_sel_lexer_t* state = new gmx_sel_lexer_t;
364 state->bGroups = bGroups;
366 state->nexpsel = (maxnr > 0 ? gmx::ssize(sc->sel) + maxnr : -1);
368 state->statusWriter = statusWriter;
370 state->currentLocation.startIndex = 0;
371 state->currentLocation.endIndex = 0;
373 snew(state->mstack, 20);
374 state->mstack_alloc = 20;
377 state->nextparam = nullptr;
378 state->nextMethodSymbol = nullptr;
379 state->prev_pos_kw = 0;
380 state->bBoolNo = false;
381 state->bMatchOf = false;
382 state->bMatchBool = false;
383 state->bCmdStart = true;
384 state->bBuffer = false;
386 _gmx_sel_yyset_extra(state, *scannerp);
389 void _gmx_sel_free_lexer(yyscan_t scanner)
391 gmx_sel_lexer_t* state = _gmx_sel_yyget_extra(scanner);
393 sfree(state->mstack);
396 _gmx_sel_yy_delete_buffer(state->buffer, scanner);
399 _gmx_sel_yylex_destroy(scanner);
402 void _gmx_sel_lexer_set_exception(yyscan_t scanner, const std::exception_ptr& ex)
404 gmx_sel_lexer_t* state = _gmx_sel_yyget_extra(scanner);
405 state->exception = ex;
408 void _gmx_sel_lexer_rethrow_exception_if_occurred(yyscan_t scanner)
410 gmx_sel_lexer_t* state = _gmx_sel_yyget_extra(scanner);
411 if (state->exception)
413 std::exception_ptr ex = state->exception;
414 state->exception = std::exception_ptr();
415 std::rethrow_exception(ex);
419 gmx::TextWriter* _gmx_sel_lexer_get_status_writer(yyscan_t scanner)
421 gmx_sel_lexer_t* state = _gmx_sel_yyget_extra(scanner);
422 return state->statusWriter;
425 struct gmx_ana_selcollection_t* _gmx_sel_lexer_selcollection(yyscan_t scanner)
427 gmx_sel_lexer_t* state = _gmx_sel_yyget_extra(scanner);
431 bool _gmx_sel_lexer_has_groups_set(yyscan_t scanner)
433 gmx_sel_lexer_t* state = _gmx_sel_yyget_extra(scanner);
434 return state->bGroups;
437 struct gmx_ana_indexgrps_t* _gmx_sel_lexer_indexgrps(yyscan_t scanner)
439 gmx_sel_lexer_t* state = _gmx_sel_yyget_extra(scanner);
443 int _gmx_sel_lexer_exp_selcount(yyscan_t scanner)
445 gmx_sel_lexer_t* state = _gmx_sel_yyget_extra(scanner);
446 return state->nexpsel;
449 const char* _gmx_sel_lexer_pselstr(yyscan_t scanner)
451 gmx_sel_lexer_t* state = _gmx_sel_yyget_extra(scanner);
452 return state->pselstr.c_str();
455 void _gmx_sel_lexer_set_current_location(yyscan_t scanner, const gmx::SelectionLocation& location)
457 gmx_sel_lexer_t* state = _gmx_sel_yyget_extra(scanner);
458 state->currentLocation = location;
461 const gmx::SelectionLocation& _gmx_sel_lexer_get_current_location(yyscan_t scanner)
463 gmx_sel_lexer_t* state = _gmx_sel_yyget_extra(scanner);
464 return state->currentLocation;
467 std::string _gmx_sel_lexer_get_current_text(yyscan_t scanner)
469 gmx_sel_lexer_t* state = _gmx_sel_yyget_extra(scanner);
470 return _gmx_sel_lexer_get_text(scanner, state->currentLocation);
473 std::string _gmx_sel_lexer_get_text(yyscan_t scanner, const gmx::SelectionLocation& location)
475 gmx_sel_lexer_t* state = _gmx_sel_yyget_extra(scanner);
476 const int startIndex = location.startIndex;
477 const int endIndex = location.endIndex;
478 if (startIndex >= endIndex)
480 return std::string();
482 return state->pselstr.substr(startIndex, endIndex - startIndex);
485 void _gmx_sel_lexer_clear_pselstr(yyscan_t scanner)
487 gmx_sel_lexer_t* state = _gmx_sel_yyget_extra(scanner);
488 state->pselstr.clear();
491 void _gmx_sel_lexer_clear_method_stack(yyscan_t scanner)
493 gmx_sel_lexer_t* state = _gmx_sel_yyget_extra(scanner);
498 void _gmx_sel_finish_method(yyscan_t scanner)
500 gmx_sel_lexer_t* state = _gmx_sel_yyget_extra(scanner);
508 void _gmx_sel_set_lex_input_file(yyscan_t scanner, FILE* fp)
510 gmx_sel_lexer_t* state = _gmx_sel_yyget_extra(scanner);
512 state->bBuffer = true;
513 state->buffer = _gmx_sel_yy_create_buffer(fp, YY_BUF_SIZE, scanner);
514 _gmx_sel_yy_switch_to_buffer(state->buffer, scanner);
517 void _gmx_sel_set_lex_input_str(yyscan_t scanner, const char* str)
519 gmx_sel_lexer_t* state = _gmx_sel_yyget_extra(scanner);
523 _gmx_sel_yy_delete_buffer(state->buffer, scanner);
525 state->bBuffer = true;
526 state->buffer = _gmx_sel_yy_scan_string(str, scanner);