Fix internal error with selection "resname help".
[alexxy/gromacs.git] / src / gromacs / selection / scanner_internal.cpp
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
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.
13
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.
18  *
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.
25  *
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.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \internal \file
32  * \brief Helper functions for the selection tokenizer.
33  *
34  * This file implements the functions in the headers scanner.h and
35  * scanner_internal.h.
36  *
37  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
38  * \ingroup module_selection
39  */
40 /*! \cond
41  * \internal \file scanner_flex.h
42  * \brief Generated (from scanner.l) header file by Flex.
43  *
44  * This file contains definitions of functions that are needed in
45  * scanner_internal.cpp.
46  *
47  * \ingroup module_selection
48  * \endcond
49  */
50 #ifdef HAVE_CONFIG_H
51 #include <config.h>
52 #endif
53
54 #include <stdlib.h>
55 #include <string.h>
56
57 #include <string>
58
59 #include "gromacs/legacyheaders/typedefs.h"
60 #include "gromacs/legacyheaders/smalloc.h"
61 #include "gromacs/legacyheaders/string2.h"
62
63 #include "gromacs/utility/errorcodes.h"
64 #include "gromacs/utility/exceptions.h"
65 #include "gromacs/utility/gmxassert.h"
66 #include "gromacs/utility/messagestringcollector.h"
67 #include "gromacs/utility/stringutil.h"
68
69 #include "parsetree.h"
70 #include "selectioncollection-impl.h"
71 #include "selelem.h"
72 #include "selmethod.h"
73 #include "symrec.h"
74
75 #include "parser.h"
76 #include "scanner.h"
77 #include "scanner_internal.h"
78
79 //! Step in which the allocated memory for pretty-printed input is incremented.
80 #define STRSTORE_ALLOCSTEP 1000
81
82 /* These are defined as macros in the generated scanner_flex.h.
83  * We undefine them here to have them as variable names in the subroutines.
84  * There are other ways of doing this, but this is probably the easiest. */
85 #undef yylval
86 #undef yytext
87 #undef yyleng
88
89 /*! \brief
90  * Handles initialization of method parameter token.
91  */
92 static int
93 init_param_token(YYSTYPE *yylval, gmx_ana_selparam_t *param, bool bBoolNo)
94 {
95     if (bBoolNo)
96     {
97         GMX_RELEASE_ASSERT(param->name != NULL,
98                 "bBoolNo should only be set for a parameters with a name");
99         snew(yylval->str, strlen(param->name) + 3);
100         yylval->str[0] = 'n';
101         yylval->str[1] = 'o';
102         strcpy(yylval->str+2, param->name);
103     }
104     else
105     {
106         yylval->str = param->name ? strdup(param->name) : NULL;
107     }
108     return PARAM;
109 }
110
111 /*! \brief
112  * Processes a selection method token.
113  */
114 static int
115 init_method_token(YYSTYPE *yylval, gmx_ana_selmethod_t *method, bool bPosMod,
116                   gmx_sel_lexer_t *state)
117 {
118     /* If the previous token was not KEYWORD_POS, return EMPTY_POSMOD
119      * before the actual method to work around a limitation in Bison. */
120     if (!bPosMod && method->type != POS_VALUE)
121     {
122         state->nextmethod = method;
123         return EMPTY_POSMOD;
124     }
125     yylval->meth = method;
126     if (!(method->flags & SMETH_MODIFIER) && method->nparams == 0)
127     {
128         /* Keyword */
129         switch (method->type)
130         {
131             case INT_VALUE:   return KEYWORD_NUMERIC;
132             case REAL_VALUE:  return KEYWORD_NUMERIC;
133             case STR_VALUE:   return KEYWORD_STR;
134             case GROUP_VALUE: return KEYWORD_GROUP;
135             default:
136                 GMX_ERROR_NORET(gmx::eeInternalError, "Unsupported keyword type");
137                 return INVALID;
138         }
139     }
140     else
141     {
142         /* Method with parameters or a modifier */
143         if (method->flags & SMETH_MODIFIER)
144         {
145             /* Remove all methods from the stack */
146             state->msp = -1;
147             if (method->param[1].name == NULL)
148             {
149                 state->nextparam = &method->param[1];
150             }
151         }
152         else
153         {
154             if (method->param[0].name == NULL)
155             {
156                 state->nextparam = &method->param[0];
157             }
158         }
159         ++state->msp;
160         if (state->msp >= state->mstack_alloc)
161         {
162             state->mstack_alloc += 10;
163             srenew(state->mstack, state->mstack_alloc);
164         }
165         state->mstack[state->msp] = method;
166         if (method->flags & SMETH_MODIFIER)
167         {
168             return MODIFIER;
169         }
170         switch (method->type)
171         {
172             case INT_VALUE:   return METHOD_NUMERIC;
173             case REAL_VALUE:  return METHOD_NUMERIC;
174             case POS_VALUE:   return METHOD_POS;
175             case GROUP_VALUE: return METHOD_GROUP;
176             default:
177                 --state->msp;
178                 GMX_ERROR_NORET(gmx::eeInternalError, "Unsupported method type");
179                 return INVALID;
180         }
181     }
182     return INVALID; /* Should not be reached */
183 }
184
185 int
186 _gmx_sel_lexer_process_pending(YYSTYPE *yylval, gmx_sel_lexer_t *state)
187 {
188     if (state->nextparam)
189     {
190         gmx_ana_selparam_t *param = state->nextparam;
191         bool                bBoolNo = state->bBoolNo;
192
193         if (state->neom > 0)
194         {
195             --state->neom;
196             return END_OF_METHOD;
197         }
198         state->nextparam = NULL;
199         state->bBoolNo   = false;
200         _gmx_sel_lexer_add_token(param->name, -1, state);
201         return init_param_token(yylval, param, bBoolNo);
202     }
203     if (state->prev_pos_kw > 0)
204     {
205         --state->prev_pos_kw;
206     }
207     if (state->nextmethod)
208     {
209         gmx_ana_selmethod_t *method = state->nextmethod;
210
211         state->nextmethod = NULL;
212         return init_method_token(yylval, method, true, state);
213     }
214     return 0;
215 }
216
217 int
218 _gmx_sel_lexer_process_identifier(YYSTYPE *yylval, char *yytext, size_t yyleng,
219                                   gmx_sel_lexer_t *state)
220 {
221     /* Check if the identifier matches with a parameter name */
222     if (state->msp >= 0)
223     {
224         gmx_ana_selparam_t *param = NULL;
225         bool                bBoolNo = false;
226         int                 sp = state->msp;
227         while (!param && sp >= 0)
228         {
229             int             i;
230             for (i = 0; i < state->mstack[sp]->nparams; ++i)
231             {
232                 /* Skip NULL parameters and too long parameters */
233                 if (state->mstack[sp]->param[i].name == NULL
234                     || strlen(state->mstack[sp]->param[i].name) > yyleng)
235                 {
236                     continue;
237                 }
238                 if (!strncmp(state->mstack[sp]->param[i].name, yytext, yyleng))
239                 {
240                     param = &state->mstack[sp]->param[i];
241                     break;
242                 }
243                 /* Check separately for a 'no' prefix on boolean parameters */
244                 if (state->mstack[sp]->param[i].val.type == NO_VALUE
245                     && yyleng > 2 && yytext[0] == 'n' && yytext[1] == 'o'
246                     && !strncmp(state->mstack[sp]->param[i].name, yytext+2, yyleng-2))
247                 {
248                     param = &state->mstack[sp]->param[i];
249                     bBoolNo = true;
250                     break;
251                 }
252             }
253             if (!param)
254             {
255                 --sp;
256             }
257         }
258         if (param)
259         {
260             if (param->val.type == NO_VALUE && !bBoolNo)
261             {
262                 state->bMatchBool = true;
263             }
264             if (sp < state->msp)
265             {
266                 state->neom = state->msp - sp - 1;
267                 state->nextparam = param;
268                 state->bBoolNo   = bBoolNo;
269                 return END_OF_METHOD;
270             }
271             _gmx_sel_lexer_add_token(param->name, -1, state);
272             return init_param_token(yylval, param, bBoolNo);
273         }
274     }
275
276     /* Check if the identifier matches with a symbol */
277     const gmx::SelectionParserSymbol *symbol
278         = state->sc->symtab->findSymbol(std::string(yytext, yyleng), false);
279     /* If there is no match, return the token as a string */
280     if (!symbol)
281     {
282         yylval->str = gmx_strndup(yytext, yyleng);
283         _gmx_sel_lexer_add_token(yytext, yyleng, state);
284         return IDENTIFIER;
285     }
286     _gmx_sel_lexer_add_token(symbol->name().c_str(), -1, state);
287     gmx::SelectionParserSymbol::SymbolType symtype = symbol->type();
288     /* Reserved symbols should have been caught earlier */
289     if (symtype == gmx::SelectionParserSymbol::ReservedSymbol)
290     {
291         GMX_THROW(gmx::InternalError(gmx::formatString(
292                         "Mismatch between tokenizer and reserved symbol table (for '%s')",
293                         symbol->name().c_str())));
294     }
295     /* For variable symbols, return the type of the variable value */
296     if (symtype == gmx::SelectionParserSymbol::VariableSymbol)
297     {
298         gmx::SelectionTreeElementPointer var = symbol->variableValue();
299         /* Return simple tokens for constant variables */
300         if (var->type == SEL_CONST)
301         {
302             switch (var->v.type)
303             {
304                 case INT_VALUE:
305                     yylval->i = var->v.u.i[0];
306                     return TOK_INT;
307                 case REAL_VALUE:
308                     yylval->r = var->v.u.r[0];
309                     return TOK_REAL;
310                 case POS_VALUE:
311                     break;
312                 default:
313                     GMX_ERROR_NORET(gmx::eeInternalError,
314                                     "Unsupported variable type");
315                     return INVALID;
316             }
317         }
318         yylval->sel = new gmx::SelectionTreeElementPointer(var);
319         switch (var->v.type)
320         {
321             case INT_VALUE:   return VARIABLE_NUMERIC;
322             case REAL_VALUE:  return VARIABLE_NUMERIC;
323             case POS_VALUE:   return VARIABLE_POS;
324             case GROUP_VALUE: return VARIABLE_GROUP;
325             default:
326                 delete yylval->sel;
327                 GMX_ERROR_NORET(gmx::eeInternalError,
328                                 "Unsupported variable type");
329                 return INVALID;
330         }
331         delete yylval->sel;
332         return INVALID; /* Should not be reached. */
333     }
334     /* For method symbols, return the correct type */
335     if (symtype == gmx::SelectionParserSymbol::MethodSymbol)
336     {
337         gmx_ana_selmethod_t *method = symbol->methodValue();
338         return init_method_token(yylval, method, state->prev_pos_kw > 0, state);
339     }
340     /* For position symbols, we need to return KEYWORD_POS, but we also need
341      * some additional handling. */
342     if (symtype == gmx::SelectionParserSymbol::PositionSymbol)
343     {
344         state->bMatchOf = true;
345         yylval->str = strdup(symbol->name().c_str());
346         state->prev_pos_kw = 2;
347         return KEYWORD_POS;
348     }
349     /* Should not be reached */
350     return INVALID;
351 }
352
353 void
354 _gmx_sel_lexer_add_token(const char *str, int len, gmx_sel_lexer_t *state)
355 {
356     /* Do nothing if the string is empty, or if it is a space and there is
357      * no other text yet, or if there already is a space. */
358     if (!str || len == 0 || strlen(str) == 0
359         || (str[0] == ' ' && str[1] == 0
360             && (state->pslen == 0 || state->pselstr[state->pslen - 1] == ' ')))
361     {
362         return;
363     }
364     if (len < 0)
365     {
366         len = strlen(str);
367     }
368     /* Allocate more memory if necessary */
369     if (state->nalloc_psel - state->pslen < len)
370     {
371         int incr = STRSTORE_ALLOCSTEP < len ? len : STRSTORE_ALLOCSTEP;
372         state->nalloc_psel += incr;
373         srenew(state->pselstr, state->nalloc_psel);
374     }
375     /* Append the token to the stored string */
376     strncpy(state->pselstr + state->pslen, str, len);
377     state->pslen += len;
378     state->pselstr[state->pslen] = 0;
379 }
380
381 void
382 _gmx_sel_init_lexer(yyscan_t *scannerp, struct gmx_ana_selcollection_t *sc,
383                     bool bInteractive, int maxnr, bool bGroups,
384                     struct gmx_ana_indexgrps_t *grps)
385 {
386     int rc = _gmx_sel_yylex_init(scannerp);
387     if (rc != 0)
388     {
389         // TODO: Throw a more representative exception.
390         GMX_THROW(gmx::InternalError("Lexer initialization failed"));
391     }
392
393     gmx_sel_lexer_t *state = new gmx_sel_lexer_t;
394
395     state->sc        = sc;
396     state->errors    = NULL;
397     state->bGroups   = bGroups;
398     state->grps      = grps;
399     state->nexpsel   = (maxnr > 0 ? static_cast<int>(sc->sel.size()) + maxnr : -1);
400
401     state->bInteractive = bInteractive;
402
403     snew(state->pselstr, STRSTORE_ALLOCSTEP);
404     state->pselstr[0]   = 0;
405     state->pslen        = 0;
406     state->nalloc_psel  = STRSTORE_ALLOCSTEP;
407
408     snew(state->mstack, 20);
409     state->mstack_alloc = 20;
410     state->msp          = -1;
411     state->neom         = 0;
412     state->nextparam    = NULL;
413     state->nextmethod   = NULL;
414     state->prev_pos_kw  = 0;
415     state->bBoolNo      = false;
416     state->bMatchOf     = false;
417     state->bMatchBool   = false;
418     state->bCmdStart    = true;
419     state->bBuffer      = false;
420
421     _gmx_sel_yyset_extra(state, *scannerp);
422 }
423
424 void
425 _gmx_sel_free_lexer(yyscan_t scanner)
426 {
427     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
428
429     sfree(state->pselstr);
430     sfree(state->mstack);
431     if (state->bBuffer)
432     {
433         _gmx_sel_yy_delete_buffer(state->buffer, scanner);
434     }
435     delete state;
436     _gmx_sel_yylex_destroy(scanner);
437 }
438
439 void
440 _gmx_sel_set_lexer_error_reporter(yyscan_t scanner,
441                                   gmx::MessageStringCollector *errors)
442 {
443     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
444     state->errors = errors;
445 }
446
447 void
448 _gmx_sel_lexer_set_exception(yyscan_t scanner,
449                              const boost::exception_ptr &ex)
450 {
451     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
452     state->exception = ex;
453 }
454
455 void
456 _gmx_sel_lexer_rethrow_exception_if_occurred(yyscan_t scanner)
457 {
458     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
459     if (state->exception)
460     {
461         boost::exception_ptr ex = state->exception;
462         state->exception = boost::exception_ptr();
463         rethrow_exception(ex);
464     }
465 }
466
467 bool
468 _gmx_sel_is_lexer_interactive(yyscan_t scanner)
469 {
470     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
471     return state->bInteractive;
472 }
473
474 struct gmx_ana_selcollection_t *
475 _gmx_sel_lexer_selcollection(yyscan_t scanner)
476 {
477     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
478     return state->sc;
479 }
480
481 gmx::MessageStringCollector *
482 _gmx_sel_lexer_error_reporter(yyscan_t scanner)
483 {
484     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
485     GMX_RELEASE_ASSERT(state->errors != NULL, "Error reporter not set");
486     return state->errors;
487 }
488
489 bool
490 _gmx_sel_lexer_has_groups_set(yyscan_t scanner)
491 {
492     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
493     return state->bGroups;
494 }
495
496 struct gmx_ana_indexgrps_t *
497 _gmx_sel_lexer_indexgrps(yyscan_t scanner)
498 {
499     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
500     return state->grps;
501 }
502
503 int
504 _gmx_sel_lexer_exp_selcount(yyscan_t scanner)
505 {
506     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
507     return state->nexpsel;
508 }
509
510 const char *
511 _gmx_sel_lexer_pselstr(yyscan_t scanner)
512 {
513     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
514     return state->pselstr;
515 }
516
517 void
518 _gmx_sel_lexer_clear_pselstr(yyscan_t scanner)
519 {
520     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
521     state->pselstr[0] = 0;
522     state->pslen      = 0;
523 }
524
525 void
526 _gmx_sel_lexer_clear_method_stack(yyscan_t scanner)
527 {
528     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
529
530     state->msp = -1;
531 }
532
533 void
534 _gmx_sel_finish_method(yyscan_t scanner)
535 {
536     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
537
538     if (state->msp >= 0)
539     {
540         --state->msp;
541     }
542 }
543
544 void
545 _gmx_sel_set_lex_input_file(yyscan_t scanner, FILE *fp)
546 {
547     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
548
549     state->bBuffer = true;
550     state->buffer  = _gmx_sel_yy_create_buffer(fp, YY_BUF_SIZE, scanner);
551     _gmx_sel_yy_switch_to_buffer(state->buffer, scanner);
552 }
553
554 void
555 _gmx_sel_set_lex_input_str(yyscan_t scanner, const char *str)
556 {
557     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
558
559     if (state->bBuffer)
560     {
561         _gmx_sel_yy_delete_buffer(state->buffer, scanner);
562     }
563     state->bBuffer = true;
564     state->buffer  = _gmx_sel_yy_scan_string(str, scanner);
565 }