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
41 * \internal \file scanner_flex.h
42 * \brief Generated (from scanner.l) header file by Flex.
44 * This file contains definitions of functions that are needed in
45 * scanner_internal.cpp.
47 * \ingroup module_selection
61 #include "gromacs/utility/errorcodes.h"
62 #include "gromacs/utility/exceptions.h"
63 #include "gromacs/utility/gmxassert.h"
64 #include "gromacs/utility/messagestringcollector.h"
66 #include "parsetree.h"
67 #include "selectioncollection-impl.h"
69 #include "selmethod.h"
74 #include "scanner_internal.h"
76 //! Step in which the allocated memory for pretty-printed input is incremented.
77 #define STRSTORE_ALLOCSTEP 1000
79 /* These are defined as macros in the generated scanner_flex.h.
80 * We undefine them here to have them as variable names in the subroutines.
81 * There are other ways of doing this, but this is probably the easiest. */
87 init_param_token(YYSTYPE *yylval, gmx_ana_selparam_t *param, bool bBoolNo)
91 GMX_RELEASE_ASSERT(param->name != NULL,
92 "bBoolNo should only be set for a parameters with a name");
93 snew(yylval->str, strlen(param->name) + 3);
96 strcpy(yylval->str+2, param->name);
100 yylval->str = param->name ? strdup(param->name) : NULL;
106 init_method_token(YYSTYPE *yylval, gmx_ana_selmethod_t *method, bool bPosMod,
107 gmx_sel_lexer_t *state)
109 /* If the previous token was not KEYWORD_POS, return EMPTY_POSMOD
110 * before the actual method to work around a limitation in Bison. */
111 if (!bPosMod && method->type != POS_VALUE)
113 state->nextmethod = method;
116 yylval->meth = method;
117 if (!(method->flags & SMETH_MODIFIER) && method->nparams == 0)
120 switch (method->type)
122 case INT_VALUE: return KEYWORD_NUMERIC;
123 case REAL_VALUE: return KEYWORD_NUMERIC;
124 case STR_VALUE: return KEYWORD_STR;
125 case GROUP_VALUE: return KEYWORD_GROUP;
127 GMX_ERROR_NORET(gmx::eeInternalError, "Unsupported keyword type");
131 /* Method with parameters or a modifier */
132 if (method->flags & SMETH_MODIFIER)
134 /* Remove all methods from the stack */
136 if (method->param[1].name == NULL)
138 state->nextparam = &method->param[1];
143 if (method->param[0].name == NULL)
145 state->nextparam = &method->param[0];
149 if (state->msp >= state->mstack_alloc)
151 state->mstack_alloc += 10;
152 srenew(state->mstack, state->mstack_alloc);
154 state->mstack[state->msp] = method;
155 if (method->flags & SMETH_MODIFIER)
159 switch (method->type)
161 case INT_VALUE: return METHOD_NUMERIC;
162 case REAL_VALUE: return METHOD_NUMERIC;
163 case POS_VALUE: return METHOD_POS;
164 case GROUP_VALUE: return METHOD_GROUP;
167 GMX_ERROR_NORET(gmx::eeInternalError, "Unsupported method type");
171 return INVALID; /* Should not be reached */
175 _gmx_sel_lexer_process_pending(YYSTYPE *yylval, gmx_sel_lexer_t *state)
177 if (state->nextparam)
179 gmx_ana_selparam_t *param = state->nextparam;
180 bool bBoolNo = state->bBoolNo;
185 return END_OF_METHOD;
187 state->nextparam = NULL;
188 state->bBoolNo = false;
189 _gmx_sel_lexer_add_token(param->name, -1, state);
190 return init_param_token(yylval, param, bBoolNo);
192 if (state->prev_pos_kw > 0)
194 --state->prev_pos_kw;
196 if (state->nextmethod)
198 gmx_ana_selmethod_t *method = state->nextmethod;
200 state->nextmethod = NULL;
201 return init_method_token(yylval, method, true, state);
207 _gmx_sel_lexer_process_identifier(YYSTYPE *yylval, char *yytext, size_t yyleng,
208 gmx_sel_lexer_t *state)
210 gmx_sel_symrec_t *symbol;
213 /* Check if the identifier matches with a parameter name */
216 gmx_ana_selparam_t *param = NULL;
217 bool bBoolNo = false;
219 while (!param && sp >= 0)
222 for (i = 0; i < state->mstack[sp]->nparams; ++i)
224 /* Skip NULL parameters and too long parameters */
225 if (state->mstack[sp]->param[i].name == NULL
226 || strlen(state->mstack[sp]->param[i].name) > yyleng)
230 if (!strncmp(state->mstack[sp]->param[i].name, yytext, yyleng))
232 param = &state->mstack[sp]->param[i];
235 /* Check separately for a 'no' prefix on boolean parameters */
236 if (state->mstack[sp]->param[i].val.type == NO_VALUE
237 && yyleng > 2 && yytext[0] == 'n' && yytext[1] == 'o'
238 && !strncmp(state->mstack[sp]->param[i].name, yytext+2, yyleng-2))
240 param = &state->mstack[sp]->param[i];
252 if (param->val.type == NO_VALUE && !bBoolNo)
254 state->bMatchBool = true;
258 state->neom = state->msp - sp - 1;
259 state->nextparam = param;
260 state->bBoolNo = bBoolNo;
261 return END_OF_METHOD;
263 _gmx_sel_lexer_add_token(param->name, -1, state);
264 return init_param_token(yylval, param, bBoolNo);
268 /* Check if the identifier matches with a symbol */
269 symbol = _gmx_sel_find_symbol_len(state->sc->symtab, yytext, yyleng, false);
270 /* If there is no match, return the token as a string */
273 yylval->str = gmx_strndup(yytext, yyleng);
274 _gmx_sel_lexer_add_token(yytext, yyleng, state);
277 _gmx_sel_lexer_add_token(_gmx_sel_sym_name(symbol), -1, state);
278 symtype = _gmx_sel_sym_type(symbol);
279 /* Reserved symbols should have been caught earlier */
280 if (symtype == SYMBOL_RESERVED)
282 GMX_ERROR_NORET(gmx::eeInternalError,
283 "Mismatch between tokenizer and reserved symbol table");
286 /* For variable symbols, return the type of the variable value */
287 if (symtype == SYMBOL_VARIABLE)
291 var = _gmx_sel_sym_value_var(symbol);
292 /* Return simple tokens for constant variables */
293 if (var->type == SEL_CONST)
298 yylval->i = var->v.u.i[0];
301 yylval->r = var->v.u.r[0];
306 GMX_ERROR_NORET(gmx::eeInternalError,
307 "Unsupported variable type");
314 case INT_VALUE: return VARIABLE_NUMERIC;
315 case REAL_VALUE: return VARIABLE_NUMERIC;
316 case POS_VALUE: return VARIABLE_POS;
317 case GROUP_VALUE: return VARIABLE_GROUP;
319 GMX_ERROR_NORET(gmx::eeInternalError,
320 "Unsupported variable type");
323 return INVALID; /* Should not be reached. */
325 /* For method symbols, return the correct type */
326 if (symtype == SYMBOL_METHOD)
328 gmx_ana_selmethod_t *method;
330 method = _gmx_sel_sym_value_method(symbol);
331 return init_method_token(yylval, method, state->prev_pos_kw > 0, state);
333 /* For position symbols, we need to return KEYWORD_POS, but we also need
334 * some additional handling. */
335 if (symtype == SYMBOL_POS)
337 state->bMatchOf = true;
338 yylval->str = _gmx_sel_sym_name(symbol);
339 state->prev_pos_kw = 2;
342 /* Should not be reached */
347 _gmx_sel_lexer_add_token(const char *str, int len, gmx_sel_lexer_t *state)
349 /* Do nothing if the string is empty, or if it is a space and there is
350 * no other text yet, or if there already is a space. */
351 if (!str || len == 0 || strlen(str) == 0
352 || (str[0] == ' ' && str[1] == 0
353 && (state->pslen == 0 || state->pselstr[state->pslen - 1] == ' ')))
361 /* Allocate more memory if necessary */
362 if (state->nalloc_psel - state->pslen < len)
364 int incr = STRSTORE_ALLOCSTEP < len ? len : STRSTORE_ALLOCSTEP;
365 state->nalloc_psel += incr;
366 srenew(state->pselstr, state->nalloc_psel);
368 /* Append the token to the stored string */
369 strncpy(state->pselstr + state->pslen, str, len);
371 state->pselstr[state->pslen] = 0;
375 _gmx_sel_init_lexer(yyscan_t *scannerp, struct gmx_ana_selcollection_t *sc,
376 bool bInteractive, int maxnr, bool bGroups,
377 struct gmx_ana_indexgrps_t *grps)
379 int rc = _gmx_sel_yylex_init(scannerp);
382 // TODO: Throw a more representative exception.
383 GMX_THROW(gmx::InternalError("Lexer initialization failed"));
386 gmx_sel_lexer_t *state = new gmx_sel_lexer_t;
389 state->errors = NULL;
390 state->bGroups = bGroups;
392 state->nexpsel = (maxnr > 0 ? static_cast<int>(sc->sel.size()) + maxnr : -1);
394 state->bInteractive = bInteractive;
396 snew(state->pselstr, STRSTORE_ALLOCSTEP);
397 state->pselstr[0] = 0;
399 state->nalloc_psel = STRSTORE_ALLOCSTEP;
401 snew(state->mstack, 20);
402 state->mstack_alloc = 20;
405 state->nextparam = NULL;
406 state->nextmethod = NULL;
407 state->prev_pos_kw = 0;
408 state->bBoolNo = false;
409 state->bMatchOf = false;
410 state->bMatchBool = false;
411 state->bCmdStart = true;
412 state->bBuffer = false;
414 _gmx_sel_yyset_extra(state, *scannerp);
418 _gmx_sel_free_lexer(yyscan_t scanner)
420 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
422 sfree(state->pselstr);
423 sfree(state->mstack);
426 _gmx_sel_yy_delete_buffer(state->buffer, scanner);
429 _gmx_sel_yylex_destroy(scanner);
433 _gmx_sel_set_lexer_error_reporter(yyscan_t scanner,
434 gmx::MessageStringCollector *errors)
436 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
437 state->errors = errors;
441 _gmx_sel_lexer_set_exception(yyscan_t scanner,
442 const boost::exception_ptr &ex)
444 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
445 state->exception = ex;
449 _gmx_sel_lexer_rethrow_exception_if_occurred(yyscan_t scanner)
451 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
452 if (state->exception)
454 boost::exception_ptr ex = state->exception;
455 state->exception = boost::exception_ptr();
456 rethrow_exception(ex);
461 _gmx_sel_is_lexer_interactive(yyscan_t scanner)
463 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
464 return state->bInteractive;
467 struct gmx_ana_selcollection_t *
468 _gmx_sel_lexer_selcollection(yyscan_t scanner)
470 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
474 gmx::MessageStringCollector *
475 _gmx_sel_lexer_error_reporter(yyscan_t scanner)
477 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
478 GMX_RELEASE_ASSERT(state->errors != NULL, "Error reporter not set");
479 return state->errors;
483 _gmx_sel_lexer_has_groups_set(yyscan_t scanner)
485 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
486 return state->bGroups;
489 struct gmx_ana_indexgrps_t *
490 _gmx_sel_lexer_indexgrps(yyscan_t scanner)
492 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
497 _gmx_sel_lexer_exp_selcount(yyscan_t scanner)
499 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
500 return state->nexpsel;
504 _gmx_sel_lexer_pselstr(yyscan_t scanner)
506 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
507 return state->pselstr;
511 _gmx_sel_lexer_clear_pselstr(yyscan_t scanner)
513 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
514 state->pselstr[0] = 0;
519 _gmx_sel_lexer_clear_method_stack(yyscan_t scanner)
521 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
527 _gmx_sel_finish_method(yyscan_t scanner)
529 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
538 _gmx_sel_set_lex_input_file(yyscan_t scanner, FILE *fp)
540 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
542 state->bBuffer = true;
543 state->buffer = _gmx_sel_yy_create_buffer(fp, YY_BUF_SIZE, scanner);
544 _gmx_sel_yy_switch_to_buffer(state->buffer, scanner);
548 _gmx_sel_set_lex_input_str(yyscan_t scanner, const char *str)
550 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
554 _gmx_sel_yy_delete_buffer(state->buffer, scanner);
556 state->bBuffer = true;
557 state->buffer = _gmx_sel_yy_scan_string(str, scanner);