Fix most doxygen warnings.
[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 "typedefs.h"
58 #include "smalloc.h"
59 #include "string2.h"
60
61 #include "gromacs/utility/errorcodes.h"
62 #include "gromacs/utility/exceptions.h"
63 #include "gromacs/utility/gmxassert.h"
64 #include "gromacs/utility/messagestringcollector.h"
65
66 #include "parsetree.h"
67 #include "selectioncollection-impl.h"
68 #include "selelem.h"
69 #include "selmethod.h"
70 #include "symrec.h"
71
72 #include "parser.h"
73 #include "scanner.h"
74 #include "scanner_internal.h"
75
76 //! Step in which the allocated memory for pretty-printed input is incremented.
77 #define STRSTORE_ALLOCSTEP 1000
78
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. */
82 #undef yylval
83 #undef yytext
84 #undef yyleng
85
86 static int
87 init_param_token(YYSTYPE *yylval, gmx_ana_selparam_t *param, bool bBoolNo)
88 {
89     if (bBoolNo)
90     {
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);
94         yylval->str[0] = 'n';
95         yylval->str[1] = 'o';
96         strcpy(yylval->str+2, param->name);
97     }
98     else
99     {
100         yylval->str = param->name ? strdup(param->name) : NULL;
101     }
102     return PARAM;
103 }
104
105 static int
106 init_method_token(YYSTYPE *yylval, gmx_ana_selmethod_t *method, bool bPosMod,
107                   gmx_sel_lexer_t *state)
108 {
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)
112     {
113         state->nextmethod = method;
114         return EMPTY_POSMOD;
115     }
116     yylval->meth = method;
117     if (!(method->flags & SMETH_MODIFIER) && method->nparams == 0)
118     {
119         /* Keyword */
120         switch (method->type)
121         {
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;
126             default:
127                 GMX_ERROR_NORET(gmx::eeInternalError, "Unsupported keyword type");
128                 return INVALID;
129         }
130     } else {
131         /* Method with parameters or a modifier */
132         if (method->flags & SMETH_MODIFIER)
133         {
134             /* Remove all methods from the stack */
135             state->msp = -1;
136             if (method->param[1].name == NULL)
137             {
138                 state->nextparam = &method->param[1];
139             }
140         }
141         else
142         {
143             if (method->param[0].name == NULL)
144             {
145                 state->nextparam = &method->param[0];
146             }
147         }
148         ++state->msp;
149         if (state->msp >= state->mstack_alloc)
150         {
151             state->mstack_alloc += 10;
152             srenew(state->mstack, state->mstack_alloc);
153         }
154         state->mstack[state->msp] = method;
155         if (method->flags & SMETH_MODIFIER)
156         {
157             return MODIFIER;
158         }
159         switch (method->type)
160         {
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;
165             default:
166                 --state->msp;
167                 GMX_ERROR_NORET(gmx::eeInternalError, "Unsupported method type");
168                 return INVALID;
169         }
170     }
171     return INVALID; /* Should not be reached */
172 }
173
174 int
175 _gmx_sel_lexer_process_pending(YYSTYPE *yylval, gmx_sel_lexer_t *state)
176 {
177     if (state->nextparam)
178     {
179         gmx_ana_selparam_t *param = state->nextparam;
180         bool                bBoolNo = state->bBoolNo;
181
182         if (state->neom > 0)
183         {
184             --state->neom;
185             return END_OF_METHOD;
186         }
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);
191     }
192     if (state->prev_pos_kw > 0)
193     {
194         --state->prev_pos_kw;
195     }
196     if (state->nextmethod)
197     {
198         gmx_ana_selmethod_t *method = state->nextmethod;
199
200         state->nextmethod = NULL;
201         return init_method_token(yylval, method, true, state);
202     }
203     return 0;
204 }
205
206 int
207 _gmx_sel_lexer_process_identifier(YYSTYPE *yylval, char *yytext, size_t yyleng,
208                                   gmx_sel_lexer_t *state)
209 {
210     gmx_sel_symrec_t *symbol;
211     e_symbol_t        symtype;
212
213     /* Check if the identifier matches with a parameter name */
214     if (state->msp >= 0)
215     {
216         gmx_ana_selparam_t *param = NULL;
217         bool                bBoolNo = false;
218         int                 sp = state->msp;
219         while (!param && sp >= 0)
220         {
221             int             i;
222             for (i = 0; i < state->mstack[sp]->nparams; ++i)
223             {
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)
227                 {
228                     continue;
229                 }
230                 if (!strncmp(state->mstack[sp]->param[i].name, yytext, yyleng))
231                 {
232                     param = &state->mstack[sp]->param[i];
233                     break;
234                 }
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))
239                 {
240                     param = &state->mstack[sp]->param[i];
241                     bBoolNo = true;
242                     break;
243                 }
244             }
245             if (!param)
246             {
247                 --sp;
248             }
249         }
250         if (param)
251         {
252             if (param->val.type == NO_VALUE && !bBoolNo)
253             {
254                 state->bMatchBool = true;
255             }
256             if (sp < state->msp)
257             {
258                 state->neom = state->msp - sp - 1;
259                 state->nextparam = param;
260                 state->bBoolNo   = bBoolNo;
261                 return END_OF_METHOD;
262             }
263             _gmx_sel_lexer_add_token(param->name, -1, state);
264             return init_param_token(yylval, param, bBoolNo);
265         }
266     }
267
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 */
271     if (!symbol)
272     {
273         yylval->str = gmx_strndup(yytext, yyleng);
274         _gmx_sel_lexer_add_token(yytext, yyleng, state);
275         return IDENTIFIER;
276     }
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)
281     {
282         GMX_ERROR_NORET(gmx::eeInternalError,
283                         "Mismatch between tokenizer and reserved symbol table");
284         return INVALID;
285     }
286     /* For variable symbols, return the type of the variable value */
287     if (symtype == SYMBOL_VARIABLE)
288     {
289         t_selelem *var;
290
291         var = _gmx_sel_sym_value_var(symbol);
292         /* Return simple tokens for constant variables */
293         if (var->type == SEL_CONST)
294         {
295             switch (var->v.type)
296             {
297                 case INT_VALUE:
298                     yylval->i = var->v.u.i[0];
299                     return TOK_INT;
300                 case REAL_VALUE:
301                     yylval->r = var->v.u.r[0];
302                     return TOK_REAL;
303                 case POS_VALUE:
304                     break;
305                 default:
306                     GMX_ERROR_NORET(gmx::eeInternalError,
307                                     "Unsupported variable type");
308                     return INVALID;
309             }
310         }
311         yylval->sel = var;
312         switch (var->v.type)
313         {
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;
318             default:
319                 GMX_ERROR_NORET(gmx::eeInternalError,
320                                 "Unsupported variable type");
321                 return INVALID;
322         }
323         return INVALID; /* Should not be reached. */
324     }
325     /* For method symbols, return the correct type */
326     if (symtype == SYMBOL_METHOD)
327     {
328         gmx_ana_selmethod_t *method;
329
330         method = _gmx_sel_sym_value_method(symbol);
331         return init_method_token(yylval, method, state->prev_pos_kw > 0, state);
332     }
333     /* For position symbols, we need to return KEYWORD_POS, but we also need
334      * some additional handling. */
335     if (symtype == SYMBOL_POS)
336     {
337         state->bMatchOf = true;
338         yylval->str = _gmx_sel_sym_name(symbol);
339         state->prev_pos_kw = 2;
340         return KEYWORD_POS;
341     }
342     /* Should not be reached */
343     return INVALID;
344 }
345
346 void
347 _gmx_sel_lexer_add_token(const char *str, int len, gmx_sel_lexer_t *state)
348 {
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] == ' ')))
354     {
355         return;
356     }
357     if (len < 0)
358     {
359         len = strlen(str);
360     }
361     /* Allocate more memory if necessary */
362     if (state->nalloc_psel - state->pslen < len)
363     {
364         int incr = STRSTORE_ALLOCSTEP < len ? len : STRSTORE_ALLOCSTEP;
365         state->nalloc_psel += incr;
366         srenew(state->pselstr, state->nalloc_psel);
367     }
368     /* Append the token to the stored string */
369     strncpy(state->pselstr + state->pslen, str, len);
370     state->pslen += len;
371     state->pselstr[state->pslen] = 0;
372 }
373
374 void
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)
378 {
379     int rc = _gmx_sel_yylex_init(scannerp);
380     if (rc != 0)
381     {
382         // TODO: Throw a more representative exception.
383         GMX_THROW(gmx::InternalError("Lexer initialization failed"));
384     }
385
386     gmx_sel_lexer_t *state = new gmx_sel_lexer_t;
387
388     state->sc        = sc;
389     state->errors    = NULL;
390     state->bGroups   = bGroups;
391     state->grps      = grps;
392     state->nexpsel   = (maxnr > 0 ? static_cast<int>(sc->sel.size()) + maxnr : -1);
393
394     state->bInteractive = bInteractive;
395
396     snew(state->pselstr, STRSTORE_ALLOCSTEP);
397     state->pselstr[0]   = 0;
398     state->pslen        = 0;
399     state->nalloc_psel  = STRSTORE_ALLOCSTEP;
400
401     snew(state->mstack, 20);
402     state->mstack_alloc = 20;
403     state->msp          = -1;
404     state->neom         = 0;
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;
413
414     _gmx_sel_yyset_extra(state, *scannerp);
415 }
416
417 void
418 _gmx_sel_free_lexer(yyscan_t scanner)
419 {
420     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
421
422     sfree(state->pselstr);
423     sfree(state->mstack);
424     if (state->bBuffer)
425     {
426         _gmx_sel_yy_delete_buffer(state->buffer, scanner);
427     }
428     delete state;
429     _gmx_sel_yylex_destroy(scanner);
430 }
431
432 void
433 _gmx_sel_set_lexer_error_reporter(yyscan_t scanner,
434                                   gmx::MessageStringCollector *errors)
435 {
436     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
437     state->errors = errors;
438 }
439
440 void
441 _gmx_sel_lexer_set_exception(yyscan_t scanner,
442                              const boost::exception_ptr &ex)
443 {
444     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
445     state->exception = ex;
446 }
447
448 void
449 _gmx_sel_lexer_rethrow_exception_if_occurred(yyscan_t scanner)
450 {
451     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
452     if (state->exception)
453     {
454         boost::exception_ptr ex = state->exception;
455         state->exception = boost::exception_ptr();
456         rethrow_exception(ex);
457     }
458 }
459
460 bool
461 _gmx_sel_is_lexer_interactive(yyscan_t scanner)
462 {
463     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
464     return state->bInteractive;
465 }
466
467 struct gmx_ana_selcollection_t *
468 _gmx_sel_lexer_selcollection(yyscan_t scanner)
469 {
470     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
471     return state->sc;
472 }
473
474 gmx::MessageStringCollector *
475 _gmx_sel_lexer_error_reporter(yyscan_t scanner)
476 {
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;
480 }
481
482 bool
483 _gmx_sel_lexer_has_groups_set(yyscan_t scanner)
484 {
485     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
486     return state->bGroups;
487 }
488
489 struct gmx_ana_indexgrps_t *
490 _gmx_sel_lexer_indexgrps(yyscan_t scanner)
491 {
492     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
493     return state->grps;
494 }
495
496 int
497 _gmx_sel_lexer_exp_selcount(yyscan_t scanner)
498 {
499     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
500     return state->nexpsel;
501 }
502
503 const char *
504 _gmx_sel_lexer_pselstr(yyscan_t scanner)
505 {
506     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
507     return state->pselstr;
508 }
509
510 void
511 _gmx_sel_lexer_clear_pselstr(yyscan_t scanner)
512 {
513     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
514     state->pselstr[0] = 0;
515     state->pslen      = 0;
516 }
517
518 void
519 _gmx_sel_lexer_clear_method_stack(yyscan_t scanner)
520 {
521     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
522
523     state->msp = -1;
524 }
525
526 void
527 _gmx_sel_finish_method(yyscan_t scanner)
528 {
529     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
530
531     if (state->msp >= 0)
532     {
533         --state->msp;
534     }
535 }
536
537 void
538 _gmx_sel_set_lex_input_file(yyscan_t scanner, FILE *fp)
539 {
540     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
541
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);
545 }
546
547 void
548 _gmx_sel_set_lex_input_str(yyscan_t scanner, const char *str)
549 {
550     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
551
552     if (state->bBuffer)
553     {
554         _gmx_sel_yy_delete_buffer(state->buffer, scanner);
555     }
556     state->bBuffer = true;
557     state->buffer  = _gmx_sel_yy_scan_string(str, scanner);
558 }