2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2009, The GROMACS development team,
6 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
39 * \brief Helper functions for the selection tokenizer.
41 * This file implements the functions in the headers scanner.h and
44 /*! \internal file scanner_flex.h
45 * \brief Generated (from scanner.l) header file by Flex.
47 * This file contains definitions of functions that are needed in
60 #include "gmx_fatal.h"
62 #include <selmethod.h>
64 #include "parsetree.h"
65 #include "selcollection.h"
71 #include "scanner_internal.h"
73 #define STRSTORE_ALLOCSTEP 1000
75 /* These are defined as macros in the generated scanner_flex.h.
76 * We undefine them here to have them as variable names in the subroutines.
77 * There are other ways of doing this, but this is probably the easiest. */
83 read_stdin_line(gmx_sel_lexer_t *state)
85 char *ptr = state->inputstr;
86 int max_len = state->nalloc_input;
93 if (state->bInteractive)
95 fprintf(stderr, "> ");
97 /* For some reason (at least on my Linux), fgets() doesn't return until
98 * the user presses Ctrl-D _twice_ at the end of a non-empty line.
99 * This can be a bit confusing for users, but there's not much we can
100 * do, and the chances of a normal user noticing this are not very big. */
101 while (fgets(ptr, max_len, stdin))
103 int len = strlen(ptr);
106 if (len >= 2 && ptr[len - 1] == '\n' && ptr[len - 2] == '\\')
108 if (state->bInteractive)
110 fprintf(stderr, "... ");
113 else if (len >= 1 && ptr[len - 1] == '\n')
117 else if (len < max_len - 1)
119 if (state->bInteractive)
121 fprintf(stderr, "\n");
129 max_len += state->nalloc_input;
130 state->nalloc_input *= 2;
131 len = ptr - state->inputstr;
132 srenew(state->inputstr, state->nalloc_input);
133 ptr = state->inputstr + len;
138 gmx_input("selection reading failed");
144 _gmx_sel_yyblex(YYSTYPE *yylval, yyscan_t yyscanner)
146 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(yyscanner);
150 if (!state->bBuffer && !state->inputstr)
152 state->nalloc_input = 1024;
153 snew(state->inputstr, state->nalloc_input);
154 read_stdin_line(state);
155 _gmx_sel_set_lex_input_str(yyscanner, state->inputstr);
157 bCmdStart = state->bCmdStart;
158 token = _gmx_sel_yylex(yylval, yyscanner);
159 while (state->inputstr && token == 0 && read_stdin_line(state))
161 _gmx_sel_set_lex_input_str(yyscanner, state->inputstr);
162 token = _gmx_sel_yylex(yylval, yyscanner);
164 if (token == 0 && !bCmdStart)
167 rtrim(state->pselstr);
169 state->bCmdStart = (token == CMD_SEP);
174 init_param_token(YYSTYPE *yylval, gmx_ana_selparam_t *param, gmx_bool bBoolNo)
178 snew(yylval->str, strlen(param->name) + 3);
179 yylval->str[0] = 'n';
180 yylval->str[1] = 'o';
181 strcpy(yylval->str+2, param->name);
185 yylval->str = param->name ? strdup(param->name) : NULL;
191 init_method_token(YYSTYPE *yylval, gmx_ana_selmethod_t *method, gmx_bool bPosMod,
192 gmx_sel_lexer_t *state)
194 /* If the previous token was not KEYWORD_POS, return EMPTY_POSMOD
195 * before the actual method to work around a limitation in Bison. */
196 if (!bPosMod && method->type != POS_VALUE)
198 state->nextmethod = method;
201 yylval->meth = method;
202 if (!(method->flags & SMETH_MODIFIER) && method->nparams == 0)
205 switch (method->type)
207 case INT_VALUE: return KEYWORD_NUMERIC;
208 case REAL_VALUE: return KEYWORD_NUMERIC;
209 case STR_VALUE: return KEYWORD_STR;
210 case GROUP_VALUE: return KEYWORD_GROUP;
211 default: return INVALID;
214 /* Method with parameters or a modifier */
215 if (method->flags & SMETH_MODIFIER)
217 /* Remove all methods from the stack */
219 if (method->param[1].name == NULL)
221 state->nextparam = &method->param[1];
226 if (method->param[0].name == NULL)
228 state->nextparam = &method->param[0];
232 if (state->msp >= state->mstack_alloc)
234 state->mstack_alloc += 10;
235 srenew(state->mstack, state->mstack_alloc);
237 state->mstack[state->msp] = method;
238 if (method->flags & SMETH_MODIFIER)
242 switch (method->type)
244 case INT_VALUE: return METHOD_NUMERIC;
245 case REAL_VALUE: return METHOD_NUMERIC;
246 case POS_VALUE: return METHOD_POS;
247 case GROUP_VALUE: return METHOD_GROUP;
253 return INVALID; /* Should not be reached */
257 _gmx_sel_lexer_process_pending(YYSTYPE *yylval, gmx_sel_lexer_t *state)
259 if (state->nextparam)
261 gmx_ana_selparam_t *param = state->nextparam;
262 gmx_bool bBoolNo = state->bBoolNo;
267 return END_OF_METHOD;
269 state->nextparam = NULL;
270 state->bBoolNo = FALSE;
271 _gmx_sel_lexer_add_token(param->name, -1, state);
272 return init_param_token(yylval, param, bBoolNo);
274 if (state->prev_pos_kw > 0)
276 --state->prev_pos_kw;
278 if (state->nextmethod)
280 gmx_ana_selmethod_t *method = state->nextmethod;
282 state->nextmethod = NULL;
283 return init_method_token(yylval, method, TRUE, state);
289 _gmx_sel_lexer_process_identifier(YYSTYPE *yylval, char *yytext, size_t yyleng,
290 gmx_sel_lexer_t *state)
292 gmx_sel_symrec_t *symbol;
295 /* Check if the identifier matches with a parameter name */
298 gmx_ana_selparam_t *param = NULL;
299 gmx_bool bBoolNo = FALSE;
301 while (!param && sp >= 0)
304 for (i = 0; i < state->mstack[sp]->nparams; ++i)
306 /* Skip NULL parameters and too long parameters */
307 if (state->mstack[sp]->param[i].name == NULL
308 || strlen(state->mstack[sp]->param[i].name) > yyleng)
312 if (!strncmp(state->mstack[sp]->param[i].name, yytext, yyleng))
314 param = &state->mstack[sp]->param[i];
317 /* Check separately for a 'no' prefix on gmx_boolean parameters */
318 if (state->mstack[sp]->param[i].val.type == NO_VALUE
319 && yyleng > 2 && yytext[0] == 'n' && yytext[1] == 'o'
320 && !strncmp(state->mstack[sp]->param[i].name, yytext+2, yyleng-2))
322 param = &state->mstack[sp]->param[i];
334 if (param->val.type == NO_VALUE && !bBoolNo)
336 state->bMatchBool = TRUE;
340 state->neom = state->msp - sp - 1;
341 state->nextparam = param;
342 state->bBoolNo = bBoolNo;
343 return END_OF_METHOD;
345 _gmx_sel_lexer_add_token(param->name, -1, state);
346 return init_param_token(yylval, param, bBoolNo);
350 /* Check if the identifier matches with a symbol */
351 symbol = _gmx_sel_find_symbol_len(state->sc->symtab, yytext, yyleng, FALSE);
352 /* If there is no match, return the token as a string */
355 yylval->str = gmx_strndup(yytext, yyleng);
356 _gmx_sel_lexer_add_token(yytext, yyleng, state);
359 _gmx_sel_lexer_add_token(_gmx_sel_sym_name(symbol), -1, state);
360 symtype = _gmx_sel_sym_type(symbol);
361 /* Reserved symbols should have been caught earlier */
362 if (symtype == SYMBOL_RESERVED)
366 /* For variable symbols, return the type of the variable value */
367 if (symtype == SYMBOL_VARIABLE)
371 var = _gmx_sel_sym_value_var(symbol);
372 /* Return simple tokens for constant variables */
373 if (var->type == SEL_CONST)
378 yylval->i = var->v.u.i[0];
381 yylval->r = var->v.u.r[0];
392 case INT_VALUE: return VARIABLE_NUMERIC;
393 case REAL_VALUE: return VARIABLE_NUMERIC;
394 case POS_VALUE: return VARIABLE_POS;
395 case GROUP_VALUE: return VARIABLE_GROUP;
396 default: return INVALID;
400 /* For method symbols, return the correct type */
401 if (symtype == SYMBOL_METHOD)
403 gmx_ana_selmethod_t *method;
405 method = _gmx_sel_sym_value_method(symbol);
406 return init_method_token(yylval, method, state->prev_pos_kw > 0, state);
408 /* For position symbols, we need to return KEYWORD_POS, but we also need
409 * some additional handling. */
410 if (symtype == SYMBOL_POS)
412 state->bMatchOf = TRUE;
413 yylval->str = _gmx_sel_sym_name(symbol);
414 state->prev_pos_kw = 2;
417 /* Should not be reached */
422 _gmx_sel_lexer_add_token(const char *str, int len, gmx_sel_lexer_t *state)
424 /* Do nothing if the string is empty, or if it is a space and there is
425 * no other text yet, or if there already is a space. */
426 if (!str || len == 0 || strlen(str) == 0
427 || (str[0] == ' ' && str[1] == 0
428 && (state->pslen == 0 || state->pselstr[state->pslen - 1] == ' ')))
436 /* Allocate more memory if necessary */
437 if (state->nalloc_psel - state->pslen < len)
439 int incr = STRSTORE_ALLOCSTEP < len ? len : STRSTORE_ALLOCSTEP;
440 state->nalloc_psel += incr;
441 srenew(state->pselstr, state->nalloc_psel);
443 /* Append the token to the stored string */
444 strncpy(state->pselstr + state->pslen, str, len);
446 state->pselstr[state->pslen] = 0;
450 _gmx_sel_init_lexer(yyscan_t *scannerp, struct gmx_ana_selcollection_t *sc,
451 gmx_bool bInteractive, int maxnr,
452 struct gmx_ana_indexgrps_t *grps)
454 gmx_sel_lexer_t *state;
457 rc = _gmx_sel_yylex_init(scannerp);
466 state->nexpsel = (maxnr > 0 ? sc->nr + maxnr : -1);
468 state->bInteractive = bInteractive;
469 state->nalloc_input = 0;
470 state->inputstr = NULL;
472 snew(state->pselstr, STRSTORE_ALLOCSTEP);
473 state->pselstr[0] = 0;
475 state->nalloc_psel = STRSTORE_ALLOCSTEP;
477 snew(state->mstack, 20);
478 state->mstack_alloc = 20;
481 state->nextparam = NULL;
482 state->nextmethod = NULL;
483 state->prev_pos_kw = 0;
484 state->bBoolNo = FALSE;
485 state->bMatchOf = FALSE;
486 state->bMatchBool = FALSE;
487 state->bCmdStart = TRUE;
488 state->bBuffer = FALSE;
490 _gmx_sel_yyset_extra(state, *scannerp);
495 _gmx_sel_free_lexer(yyscan_t scanner)
497 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
499 sfree(state->inputstr);
500 sfree(state->pselstr);
501 sfree(state->mstack);
504 _gmx_sel_yy_delete_buffer(state->buffer, scanner);
507 _gmx_sel_yylex_destroy(scanner);
511 _gmx_sel_is_lexer_interactive(yyscan_t scanner)
513 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
514 return state->bInteractive;
517 struct gmx_ana_selcollection_t *
518 _gmx_sel_lexer_selcollection(yyscan_t scanner)
520 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
524 struct gmx_ana_indexgrps_t *
525 _gmx_sel_lexer_indexgrps(yyscan_t scanner)
527 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
532 _gmx_sel_lexer_exp_selcount(yyscan_t scanner)
534 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
535 return state->nexpsel;
539 _gmx_sel_lexer_pselstr(yyscan_t scanner)
541 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
542 return state->pselstr;
546 _gmx_sel_lexer_clear_pselstr(yyscan_t scanner)
548 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
549 state->pselstr[0] = 0;
554 _gmx_sel_lexer_clear_method_stack(yyscan_t scanner)
556 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
562 _gmx_sel_finish_method(yyscan_t scanner)
564 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
573 _gmx_sel_set_lex_input_file(yyscan_t scanner, FILE *fp)
575 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
577 state->bBuffer = TRUE;
578 state->buffer = _gmx_sel_yy_create_buffer(fp, YY_BUF_SIZE, scanner);
579 _gmx_sel_yy_switch_to_buffer(state->buffer, scanner);
583 _gmx_sel_set_lex_input_str(yyscan_t scanner, const char *str)
585 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
589 _gmx_sel_yy_delete_buffer(state->buffer, scanner);
591 state->bBuffer = TRUE;
592 state->buffer = _gmx_sel_yy_scan_string((yyconst char*) str, scanner);