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