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