Fixed interactive selection help.
[gromacs/qmmm-gamess-us.git] / src / gmxlib / selection / scanner_internal.c
blob57c5bc51ac54c5cb2e2240e2ea35c0f3ad679785
1 /*
3 * This source code is part of
5 * G R O M A C S
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
31 /*! \internal \file
32 * \brief Helper functions for the selection tokenizer.
34 * This file implements the functions in the headers scanner.h and
35 * scanner_internal.h.
37 /*! \internal file scanner_flex.h
38 * \brief Generated (from scanner.l) header file by Flex.
40 * This file contains definitions of functions that are needed in
41 * scanner_internal.c.
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif
47 #include <stdlib.h>
48 #include <typedefs.h>
49 #include <smalloc.h>
50 #include <string.h>
52 #include "string2.h"
54 #include <selmethod.h>
56 #include "parsetree.h"
57 #include "selcollection.h"
58 #include "selelem.h"
59 #include "symrec.h"
61 #include "parser.h"
62 #include "scanner.h"
63 #include "scanner_internal.h"
65 #define STRSTORE_ALLOCSTEP 1000
67 /* These are defined as macros in the generated scanner_flex.h.
68 * We undefine them here to have them as variable names in the subroutines.
69 * There are other ways of doing this, but this is probably the easiest. */
70 #undef yylval
71 #undef yytext
72 #undef yyleng
74 static bool
75 read_stdin_line(gmx_sel_lexer_t *state)
77 char *ptr = state->inputstr;
78 int max_len = state->nalloc_input;
79 int totlen = 0;
81 if (feof(stdin))
83 return FALSE;
85 if (state->bInteractive)
87 fprintf(stderr, "> ");
89 while (fgets(ptr, max_len, stdin))
91 int len = strlen(ptr);
93 totlen += len;
94 if (len >= 2 && ptr[len - 1] == '\n' && ptr[len - 2] == '\\')
96 if (state->bInteractive)
98 fprintf(stderr, "... ");
101 else if (len >= 1 && ptr[len - 1] == '\n')
103 break;
105 else if (len < max_len - 1)
107 if (state->bInteractive)
109 fprintf(stderr, "\n");
111 break;
113 ptr += len;
114 max_len -= len;
115 if (max_len <= 2)
117 max_len += state->nalloc_input;
118 state->nalloc_input *= 2;
119 len = ptr - state->inputstr;
120 srenew(state->inputstr, state->nalloc_input);
121 ptr = state->inputstr + len;
124 if (ferror(stdin))
126 gmx_input("selection reading failed");
128 state->bCmdStart = totlen > 0;
129 return totlen > 0;
133 _gmx_sel_yyblex(YYSTYPE *yylval, yyscan_t yyscanner)
135 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(yyscanner);
136 int token;
138 if (!state->bBuffer && !state->inputstr)
140 state->nalloc_input = 1024;
141 snew(state->inputstr, state->nalloc_input);
142 read_stdin_line(state);
143 _gmx_sel_set_lex_input_str(yyscanner, state->inputstr);
145 token = _gmx_sel_yylex(yylval, yyscanner);
146 while (state->inputstr && token == 0 && read_stdin_line(state))
148 _gmx_sel_set_lex_input_str(yyscanner, state->inputstr);
149 token = _gmx_sel_yylex(yylval, yyscanner);
151 return token;
154 static int
155 init_param_token(YYSTYPE *yylval, gmx_ana_selparam_t *param, bool bBoolNo)
157 if (bBoolNo)
159 snew(yylval->str, strlen(param->name) + 3);
160 yylval->str[0] = 'n';
161 yylval->str[1] = 'o';
162 strcpy(yylval->str+2, param->name);
164 else
166 yylval->str = param->name ? strdup(param->name) : NULL;
168 return PARAM;
171 static int
172 init_method_token(YYSTYPE *yylval, gmx_ana_selmethod_t *method, bool bPosMod,
173 gmx_sel_lexer_t *state)
175 /* If the previous token was not KEYWORD_POS, return EMPTY_POSMOD
176 * before the actual method to work around a limitation in Bison. */
177 if (!bPosMod && method->type != POS_VALUE)
179 state->nextmethod = method;
180 return EMPTY_POSMOD;
182 yylval->meth = method;
183 if (!(method->flags & SMETH_MODIFIER) && method->nparams == 0)
185 /* Keyword */
186 switch (method->type)
188 case INT_VALUE: return KEYWORD_NUMERIC;
189 case REAL_VALUE: return KEYWORD_NUMERIC;
190 case STR_VALUE: return KEYWORD_STR;
191 case GROUP_VALUE: return KEYWORD_GROUP;
192 default: return INVALID;
194 } else {
195 /* Method with parameters or a modifier */
196 if (method->flags & SMETH_MODIFIER)
198 /* Remove all methods from the stack */
199 state->msp = -1;
200 if (method->param[1].name == NULL)
202 state->nextparam = &method->param[1];
205 else
207 if (method->param[0].name == NULL)
209 state->nextparam = &method->param[0];
212 ++state->msp;
213 if (state->msp >= state->mstack_alloc)
215 state->mstack_alloc += 10;
216 srenew(state->mstack, state->mstack_alloc);
218 state->mstack[state->msp] = method;
219 if (method->flags & SMETH_MODIFIER)
221 return MODIFIER;
223 switch (method->type)
225 case INT_VALUE: return METHOD_NUMERIC;
226 case REAL_VALUE: return METHOD_NUMERIC;
227 case POS_VALUE: return METHOD_POS;
228 case GROUP_VALUE: return METHOD_GROUP;
229 default:
230 --state->msp;
231 return INVALID;
234 return INVALID; /* Should not be reached */
238 _gmx_sel_lexer_process_pending(YYSTYPE *yylval, gmx_sel_lexer_t *state)
240 if (state->nextparam)
242 gmx_ana_selparam_t *param = state->nextparam;
243 bool bBoolNo = state->bBoolNo;
245 if (state->neom > 0)
247 --state->neom;
248 return END_OF_METHOD;
250 state->nextparam = NULL;
251 state->bBoolNo = FALSE;
252 _gmx_sel_lexer_add_token(param->name, -1, state);
253 return init_param_token(yylval, param, bBoolNo);
255 if (state->prev_pos_kw > 0)
257 --state->prev_pos_kw;
259 if (state->nextmethod)
261 gmx_ana_selmethod_t *method = state->nextmethod;
263 state->nextmethod = NULL;
264 return init_method_token(yylval, method, TRUE, state);
266 return 0;
270 _gmx_sel_lexer_process_identifier(YYSTYPE *yylval, char *yytext, size_t yyleng,
271 gmx_sel_lexer_t *state)
273 gmx_sel_symrec_t *symbol;
274 e_symbol_t symtype;
276 /* Check if the identifier matches with a parameter name */
277 if (state->msp >= 0)
279 gmx_ana_selparam_t *param = NULL;
280 bool bBoolNo = FALSE;
281 int sp = state->msp;
282 while (!param && sp >= 0)
284 int i;
285 for (i = 0; i < state->mstack[sp]->nparams; ++i)
287 /* Skip NULL parameters and too long parameters */
288 if (state->mstack[sp]->param[i].name == NULL
289 || strlen(state->mstack[sp]->param[i].name) > yyleng)
291 continue;
293 if (!strncmp(state->mstack[sp]->param[i].name, yytext, yyleng))
295 param = &state->mstack[sp]->param[i];
296 break;
298 /* Check separately for a 'no' prefix on boolean parameters */
299 if (state->mstack[sp]->param[i].val.type == NO_VALUE
300 && yyleng > 2 && yytext[0] == 'n' && yytext[1] == 'o'
301 && !strncmp(state->mstack[sp]->param[i].name, yytext+2, yyleng-2))
303 param = &state->mstack[sp]->param[i];
304 bBoolNo = TRUE;
305 break;
308 if (!param)
310 --sp;
313 if (param)
315 if (param->val.type == NO_VALUE && !bBoolNo)
317 state->bMatchBool = TRUE;
319 if (sp < state->msp)
321 state->neom = state->msp - sp - 1;
322 state->nextparam = param;
323 state->bBoolNo = bBoolNo;
324 return END_OF_METHOD;
326 _gmx_sel_lexer_add_token(param->name, -1, state);
327 return init_param_token(yylval, param, bBoolNo);
331 /* Check if the identifier matches with a symbol */
332 symbol = _gmx_sel_find_symbol_len(state->sc->symtab, yytext, yyleng, FALSE);
333 /* If there is no match, return the token as a string */
334 if (!symbol)
336 yylval->str = strndup(yytext, yyleng);
337 _gmx_sel_lexer_add_token(yytext, yyleng, state);
338 return IDENTIFIER;
340 _gmx_sel_lexer_add_token(_gmx_sel_sym_name(symbol), -1, state);
341 symtype = _gmx_sel_sym_type(symbol);
342 /* Reserved symbols should have been caught earlier */
343 if (symtype == SYMBOL_RESERVED)
345 return INVALID;
347 /* For variable symbols, return the type of the variable value */
348 if (symtype == SYMBOL_VARIABLE)
350 t_selelem *var;
352 var = _gmx_sel_sym_value_var(symbol);
353 /* Return simple tokens for constant variables */
354 if (var->type == SEL_CONST)
356 switch (var->v.type)
358 case INT_VALUE:
359 yylval->i = var->v.u.i[0];
360 return TOK_INT;
361 case REAL_VALUE:
362 yylval->r = var->v.u.r[0];
363 return TOK_REAL;
364 case POS_VALUE:
365 break;
366 default:
367 return INVALID;
370 yylval->sel = var;
371 switch (var->v.type)
373 case INT_VALUE: return VARIABLE_NUMERIC;
374 case REAL_VALUE: return VARIABLE_NUMERIC;
375 case POS_VALUE: return VARIABLE_POS;
376 case GROUP_VALUE: return VARIABLE_GROUP;
377 default: return INVALID;
379 return INVALID;
381 /* For method symbols, return the correct type */
382 if (symtype == SYMBOL_METHOD)
384 gmx_ana_selmethod_t *method;
386 method = _gmx_sel_sym_value_method(symbol);
387 return init_method_token(yylval, method, state->prev_pos_kw > 0, state);
389 /* For position symbols, we need to return KEYWORD_POS, but we also need
390 * some additional handling. */
391 if (symtype == SYMBOL_POS)
393 state->bMatchOf = TRUE;
394 yylval->str = _gmx_sel_sym_name(symbol);
395 state->prev_pos_kw = 2;
396 return KEYWORD_POS;
398 /* Should not be reached */
399 return INVALID;
402 void
403 _gmx_sel_lexer_add_token(const char *str, int len, gmx_sel_lexer_t *state)
405 /* Do nothing if the string is empty, or if it is a space and there is
406 * no other text yet. */
407 if (!str || len == 0 || strlen(str) == 0
408 || (str[0] == ' ' && str[1] == 0 && state->pslen == 0))
410 return;
412 if (len < 0)
414 len = strlen(str);
416 /* Allocate more memory if necessary */
417 if (state->nalloc_psel - state->pslen < len)
419 int incr = STRSTORE_ALLOCSTEP < len ? len : STRSTORE_ALLOCSTEP;
420 state->nalloc_psel += incr;
421 srenew(state->pselstr, state->nalloc_psel);
423 /* Append the token to the stored string */
424 strncpy(state->pselstr + state->pslen, str, len);
425 state->pslen += len;
426 state->pselstr[state->pslen] = 0;
430 _gmx_sel_init_lexer(yyscan_t *scannerp, struct gmx_ana_selcollection_t *sc,
431 bool bInteractive, int maxnr,
432 struct gmx_ana_indexgrps_t *grps)
434 gmx_sel_lexer_t *state;
435 int rc;
437 rc = _gmx_sel_yylex_init(scannerp);
438 if (rc != 0)
440 return rc;
443 snew(state, 1);
444 state->sc = sc;
445 state->grps = grps;
446 state->nexpsel = (maxnr > 0 ? sc->nr + maxnr : -1);
448 state->bInteractive = bInteractive;
449 state->nalloc_input = 0;
450 state->inputstr = NULL;
452 snew(state->pselstr, STRSTORE_ALLOCSTEP);
453 state->pselstr[0] = 0;
454 state->pslen = 0;
455 state->nalloc_psel = STRSTORE_ALLOCSTEP;
457 snew(state->mstack, 20);
458 state->mstack_alloc = 20;
459 state->msp = -1;
460 state->neom = 0;
461 state->nextparam = NULL;
462 state->nextmethod = NULL;
463 state->prev_pos_kw = 0;
464 state->bBoolNo = FALSE;
465 state->bMatchOf = FALSE;
466 state->bMatchBool = FALSE;
467 state->bCmdStart = TRUE;
468 state->bBuffer = FALSE;
470 _gmx_sel_yyset_extra(state, *scannerp);
471 return 0;
474 void
475 _gmx_sel_free_lexer(yyscan_t scanner)
477 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
479 sfree(state->inputstr);
480 sfree(state->pselstr);
481 sfree(state->mstack);
482 if (state->bBuffer)
484 _gmx_sel_yy_delete_buffer(state->buffer, scanner);
486 sfree(state);
487 _gmx_sel_yylex_destroy(scanner);
490 bool
491 _gmx_sel_is_lexer_interactive(yyscan_t scanner)
493 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
494 return state->bInteractive;
497 struct gmx_ana_selcollection_t *
498 _gmx_sel_lexer_selcollection(yyscan_t scanner)
500 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
501 return state->sc;
504 struct gmx_ana_indexgrps_t *
505 _gmx_sel_lexer_indexgrps(yyscan_t scanner)
507 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
508 return state->grps;
512 _gmx_sel_lexer_exp_selcount(yyscan_t scanner)
514 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
515 return state->nexpsel;
518 const char *
519 _gmx_sel_lexer_pselstr(yyscan_t scanner)
521 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
522 return state->pselstr;
525 void
526 _gmx_sel_lexer_clear_pselstr(yyscan_t scanner)
528 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
529 state->pselstr[0] = 0;
530 state->pslen = 0;
533 void
534 _gmx_sel_lexer_clear_method_stack(yyscan_t scanner)
536 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
538 state->msp = -1;
541 void
542 _gmx_sel_finish_method(yyscan_t scanner)
544 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
546 if (state->msp >= 0)
548 --state->msp;
552 void
553 _gmx_sel_set_lex_input_file(yyscan_t scanner, FILE *fp)
555 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
557 state->bBuffer = TRUE;
558 state->buffer = _gmx_sel_yy_create_buffer(fp, YY_BUF_SIZE, scanner);
559 _gmx_sel_yy_switch_to_buffer(state->buffer, scanner);
562 void
563 _gmx_sel_set_lex_input_str(yyscan_t scanner, const char *str)
565 gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
567 if (state->bBuffer)
569 _gmx_sel_yy_delete_buffer(state->buffer, scanner);
571 state->bBuffer = TRUE;
572 state->buffer = _gmx_sel_yy_scan_string(str, scanner);