3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
32 * \brief Helper functions for the selection tokenizer.
34 * This file implements the functions in the headers scanner.h and
37 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
38 * \ingroup module_selection
40 /*! \internal \file scanner_flex.h
41 * \brief Generated (from scanner.l) header file by Flex.
43 * This file contains definitions of functions that are needed in
44 * scanner_internal.cpp.
46 * \ingroup module_selection
59 #include "gromacs/utility/errorcodes.h"
60 #include "gromacs/utility/exceptions.h"
61 #include "gromacs/utility/gmxassert.h"
62 #include "gromacs/utility/messagestringcollector.h"
64 #include "parsetree.h"
65 #include "selectioncollection-impl.h"
67 #include "selmethod.h"
72 #include "scanner_internal.h"
74 //! Step in which the allocated memory for pretty-printed input is incremented.
75 #define STRSTORE_ALLOCSTEP 1000
77 /* These are defined as macros in the generated scanner_flex.h.
78 * We undefine them here to have them as variable names in the subroutines.
79 * There are other ways of doing this, but this is probably the easiest. */
85 init_param_token(YYSTYPE *yylval, gmx_ana_selparam_t *param, bool bBoolNo)
89 GMX_RELEASE_ASSERT(param->name != NULL,
90 "bBoolNo should only be set for a parameters with a name");
91 snew(yylval->str, strlen(param->name) + 3);
94 strcpy(yylval->str+2, param->name);
98 yylval->str = param->name ? strdup(param->name) : NULL;
104 init_method_token(YYSTYPE *yylval, gmx_ana_selmethod_t *method, bool bPosMod,
105 gmx_sel_lexer_t *state)
107 /* If the previous token was not KEYWORD_POS, return EMPTY_POSMOD
108 * before the actual method to work around a limitation in Bison. */
109 if (!bPosMod && method->type != POS_VALUE)
111 state->nextmethod = method;
114 yylval->meth = method;
115 if (!(method->flags & SMETH_MODIFIER) && method->nparams == 0)
118 switch (method->type)
120 case INT_VALUE: return KEYWORD_NUMERIC;
121 case REAL_VALUE: return KEYWORD_NUMERIC;
122 case STR_VALUE: return KEYWORD_STR;
123 case GROUP_VALUE: return KEYWORD_GROUP;
125 GMX_ERROR_NORET(gmx::eeInternalError, "Unsupported keyword type");
129 /* Method with parameters or a modifier */
130 if (method->flags & SMETH_MODIFIER)
132 /* Remove all methods from the stack */
134 if (method->param[1].name == NULL)
136 state->nextparam = &method->param[1];
141 if (method->param[0].name == NULL)
143 state->nextparam = &method->param[0];
147 if (state->msp >= state->mstack_alloc)
149 state->mstack_alloc += 10;
150 srenew(state->mstack, state->mstack_alloc);
152 state->mstack[state->msp] = method;
153 if (method->flags & SMETH_MODIFIER)
157 switch (method->type)
159 case INT_VALUE: return METHOD_NUMERIC;
160 case REAL_VALUE: return METHOD_NUMERIC;
161 case POS_VALUE: return METHOD_POS;
162 case GROUP_VALUE: return METHOD_GROUP;
165 GMX_ERROR_NORET(gmx::eeInternalError, "Unsupported method type");
169 return INVALID; /* Should not be reached */
173 _gmx_sel_lexer_process_pending(YYSTYPE *yylval, gmx_sel_lexer_t *state)
175 if (state->nextparam)
177 gmx_ana_selparam_t *param = state->nextparam;
178 bool bBoolNo = state->bBoolNo;
183 return END_OF_METHOD;
185 state->nextparam = NULL;
186 state->bBoolNo = false;
187 _gmx_sel_lexer_add_token(param->name, -1, state);
188 return init_param_token(yylval, param, bBoolNo);
190 if (state->prev_pos_kw > 0)
192 --state->prev_pos_kw;
194 if (state->nextmethod)
196 gmx_ana_selmethod_t *method = state->nextmethod;
198 state->nextmethod = NULL;
199 return init_method_token(yylval, method, true, state);
205 _gmx_sel_lexer_process_identifier(YYSTYPE *yylval, char *yytext, size_t yyleng,
206 gmx_sel_lexer_t *state)
208 gmx_sel_symrec_t *symbol;
211 /* Check if the identifier matches with a parameter name */
214 gmx_ana_selparam_t *param = NULL;
215 bool bBoolNo = false;
217 while (!param && sp >= 0)
220 for (i = 0; i < state->mstack[sp]->nparams; ++i)
222 /* Skip NULL parameters and too long parameters */
223 if (state->mstack[sp]->param[i].name == NULL
224 || strlen(state->mstack[sp]->param[i].name) > yyleng)
228 if (!strncmp(state->mstack[sp]->param[i].name, yytext, yyleng))
230 param = &state->mstack[sp]->param[i];
233 /* Check separately for a 'no' prefix on boolean parameters */
234 if (state->mstack[sp]->param[i].val.type == NO_VALUE
235 && yyleng > 2 && yytext[0] == 'n' && yytext[1] == 'o'
236 && !strncmp(state->mstack[sp]->param[i].name, yytext+2, yyleng-2))
238 param = &state->mstack[sp]->param[i];
250 if (param->val.type == NO_VALUE && !bBoolNo)
252 state->bMatchBool = true;
256 state->neom = state->msp - sp - 1;
257 state->nextparam = param;
258 state->bBoolNo = bBoolNo;
259 return END_OF_METHOD;
261 _gmx_sel_lexer_add_token(param->name, -1, state);
262 return init_param_token(yylval, param, bBoolNo);
266 /* Check if the identifier matches with a symbol */
267 symbol = _gmx_sel_find_symbol_len(state->sc->symtab, yytext, yyleng, false);
268 /* If there is no match, return the token as a string */
271 yylval->str = gmx_strndup(yytext, yyleng);
272 _gmx_sel_lexer_add_token(yytext, yyleng, state);
275 _gmx_sel_lexer_add_token(_gmx_sel_sym_name(symbol), -1, state);
276 symtype = _gmx_sel_sym_type(symbol);
277 /* Reserved symbols should have been caught earlier */
278 if (symtype == SYMBOL_RESERVED)
280 GMX_ERROR_NORET(gmx::eeInternalError,
281 "Mismatch between tokenizer and reserved symbol table");
284 /* For variable symbols, return the type of the variable value */
285 if (symtype == SYMBOL_VARIABLE)
289 var = _gmx_sel_sym_value_var(symbol);
290 /* Return simple tokens for constant variables */
291 if (var->type == SEL_CONST)
296 yylval->i = var->v.u.i[0];
299 yylval->r = var->v.u.r[0];
304 GMX_ERROR_NORET(gmx::eeInternalError,
305 "Unsupported variable type");
312 case INT_VALUE: return VARIABLE_NUMERIC;
313 case REAL_VALUE: return VARIABLE_NUMERIC;
314 case POS_VALUE: return VARIABLE_POS;
315 case GROUP_VALUE: return VARIABLE_GROUP;
317 GMX_ERROR_NORET(gmx::eeInternalError,
318 "Unsupported variable type");
321 return INVALID; /* Should not be reached. */
323 /* For method symbols, return the correct type */
324 if (symtype == SYMBOL_METHOD)
326 gmx_ana_selmethod_t *method;
328 method = _gmx_sel_sym_value_method(symbol);
329 return init_method_token(yylval, method, state->prev_pos_kw > 0, state);
331 /* For position symbols, we need to return KEYWORD_POS, but we also need
332 * some additional handling. */
333 if (symtype == SYMBOL_POS)
335 state->bMatchOf = true;
336 yylval->str = _gmx_sel_sym_name(symbol);
337 state->prev_pos_kw = 2;
340 /* Should not be reached */
345 _gmx_sel_lexer_add_token(const char *str, int len, gmx_sel_lexer_t *state)
347 /* Do nothing if the string is empty, or if it is a space and there is
348 * no other text yet, or if there already is a space. */
349 if (!str || len == 0 || strlen(str) == 0
350 || (str[0] == ' ' && str[1] == 0
351 && (state->pslen == 0 || state->pselstr[state->pslen - 1] == ' ')))
359 /* Allocate more memory if necessary */
360 if (state->nalloc_psel - state->pslen < len)
362 int incr = STRSTORE_ALLOCSTEP < len ? len : STRSTORE_ALLOCSTEP;
363 state->nalloc_psel += incr;
364 srenew(state->pselstr, state->nalloc_psel);
366 /* Append the token to the stored string */
367 strncpy(state->pselstr + state->pslen, str, len);
369 state->pselstr[state->pslen] = 0;
373 _gmx_sel_init_lexer(yyscan_t *scannerp, struct gmx_ana_selcollection_t *sc,
374 bool bInteractive, int maxnr, bool bGroups,
375 struct gmx_ana_indexgrps_t *grps)
377 int rc = _gmx_sel_yylex_init(scannerp);
380 // TODO: Throw a more representative exception.
381 GMX_THROW(gmx::InternalError("Lexer initialization failed"));
384 gmx_sel_lexer_t *state = new gmx_sel_lexer_t;
387 state->errors = NULL;
388 state->bGroups = bGroups;
390 state->nexpsel = (maxnr > 0 ? static_cast<int>(sc->sel.size()) + maxnr : -1);
392 state->bInteractive = bInteractive;
394 snew(state->pselstr, STRSTORE_ALLOCSTEP);
395 state->pselstr[0] = 0;
397 state->nalloc_psel = STRSTORE_ALLOCSTEP;
399 snew(state->mstack, 20);
400 state->mstack_alloc = 20;
403 state->nextparam = NULL;
404 state->nextmethod = NULL;
405 state->prev_pos_kw = 0;
406 state->bBoolNo = false;
407 state->bMatchOf = false;
408 state->bMatchBool = false;
409 state->bCmdStart = true;
410 state->bBuffer = false;
412 _gmx_sel_yyset_extra(state, *scannerp);
416 _gmx_sel_free_lexer(yyscan_t scanner)
418 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
420 sfree(state->pselstr);
421 sfree(state->mstack);
424 _gmx_sel_yy_delete_buffer(state->buffer, scanner);
427 _gmx_sel_yylex_destroy(scanner);
431 _gmx_sel_set_lexer_error_reporter(yyscan_t scanner,
432 gmx::MessageStringCollector *errors)
434 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
435 state->errors = errors;
439 _gmx_sel_lexer_set_exception(yyscan_t scanner,
440 const boost::exception_ptr &ex)
442 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
443 state->exception = ex;
447 _gmx_sel_lexer_rethrow_exception_if_occurred(yyscan_t scanner)
449 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
450 if (state->exception)
452 boost::exception_ptr ex = state->exception;
453 state->exception = boost::exception_ptr();
454 rethrow_exception(ex);
459 _gmx_sel_is_lexer_interactive(yyscan_t scanner)
461 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
462 return state->bInteractive;
465 struct gmx_ana_selcollection_t *
466 _gmx_sel_lexer_selcollection(yyscan_t scanner)
468 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
472 gmx::MessageStringCollector *
473 _gmx_sel_lexer_error_reporter(yyscan_t scanner)
475 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
476 GMX_RELEASE_ASSERT(state->errors != NULL, "Error reporter not set");
477 return state->errors;
481 _gmx_sel_lexer_has_groups_set(yyscan_t scanner)
483 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
484 return state->bGroups;
487 struct gmx_ana_indexgrps_t *
488 _gmx_sel_lexer_indexgrps(yyscan_t scanner)
490 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
495 _gmx_sel_lexer_exp_selcount(yyscan_t scanner)
497 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
498 return state->nexpsel;
502 _gmx_sel_lexer_pselstr(yyscan_t scanner)
504 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
505 return state->pselstr;
509 _gmx_sel_lexer_clear_pselstr(yyscan_t scanner)
511 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
512 state->pselstr[0] = 0;
517 _gmx_sel_lexer_clear_method_stack(yyscan_t scanner)
519 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
525 _gmx_sel_finish_method(yyscan_t scanner)
527 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
536 _gmx_sel_set_lex_input_file(yyscan_t scanner, FILE *fp)
538 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
540 state->bBuffer = true;
541 state->buffer = _gmx_sel_yy_create_buffer(fp, YY_BUF_SIZE, scanner);
542 _gmx_sel_yy_switch_to_buffer(state->buffer, scanner);
546 _gmx_sel_set_lex_input_str(yyscan_t scanner, const char *str)
548 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
552 _gmx_sel_yy_delete_buffer(state->buffer, scanner);
554 state->bBuffer = true;
555 state->buffer = _gmx_sel_yy_scan_string(str, scanner);