Merge release-4-6 into release-5-0
[alexxy/gromacs.git] / src / gromacs / selection / scanner_internal.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2009,2010,2011,2012,2014, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \internal \file
36  * \brief Helper functions for the selection tokenizer.
37  *
38  * This file implements the functions in the headers scanner.h and
39  * scanner_internal.h.
40  *
41  * \author Teemu Murtola <teemu.murtola@gmail.com>
42  * \ingroup module_selection
43  */
44 /*! \cond
45  * \internal \file scanner_flex.h
46  * \brief Generated (from scanner.l) header file by Flex.
47  *
48  * This file contains definitions of functions that are needed in
49  * scanner_internal.cpp.
50  *
51  * \ingroup module_selection
52  * \endcond
53  */
54 #include <stdlib.h>
55 #include <string.h>
56
57 #include <string>
58
59 #include "gromacs/legacyheaders/typedefs.h"
60
61 #include "gromacs/utility/cstringutil.h"
62 #include "gromacs/utility/exceptions.h"
63 #include "gromacs/utility/gmxassert.h"
64 #include "gromacs/utility/messagestringcollector.h"
65 #include "gromacs/utility/smalloc.h"
66 #include "gromacs/utility/stringutil.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 /*! \brief
79  * Step in which the allocated memory for pretty-printed input is incremented.
80  */
81 #define STRSTORE_ALLOCSTEP 1000
82
83 /* These are defined as macros in the generated scanner_flex.h.
84  * We undefine them here to have them as variable names in the subroutines.
85  * There are other ways of doing this, but this is probably the easiest. */
86 #undef yylval
87 #undef yytext
88 #undef yyleng
89
90 /*! \brief
91  * Handles initialization of method parameter token.
92  */
93 static int
94 init_param_token(YYSTYPE *yylval, gmx_ana_selparam_t *param, bool bBoolNo)
95 {
96     if (bBoolNo)
97     {
98         GMX_RELEASE_ASSERT(param->name != NULL,
99                            "bBoolNo should only be set for a parameters with a name");
100         snew(yylval->str, strlen(param->name) + 3);
101         yylval->str[0] = 'n';
102         yylval->str[1] = 'o';
103         strcpy(yylval->str+2, param->name);
104     }
105     else
106     {
107         yylval->str = param->name ? strdup(param->name) : NULL;
108     }
109     return PARAM;
110 }
111
112 /*! \brief
113  * Processes a selection method token.
114  */
115 static int
116 init_method_token(YYSTYPE *yylval, gmx_ana_selmethod_t *method, bool bPosMod,
117                   gmx_sel_lexer_t *state)
118 {
119     /* If the previous token was not KEYWORD_POS, return EMPTY_POSMOD
120      * before the actual method to work around a limitation in Bison. */
121     if (!bPosMod && method->type != POS_VALUE)
122     {
123         state->nextmethod = method;
124         return EMPTY_POSMOD;
125     }
126     yylval->meth = method;
127     if (!(method->flags & SMETH_MODIFIER) && method->nparams == 0)
128     {
129         /* Keyword */
130         switch (method->type)
131         {
132             case INT_VALUE:   return KEYWORD_NUMERIC;
133             case REAL_VALUE:  return KEYWORD_NUMERIC;
134             case STR_VALUE:   return KEYWORD_STR;
135             case GROUP_VALUE: return KEYWORD_GROUP;
136             default:
137                 GMX_THROW(gmx::InternalError("Unsupported keyword type"));
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_THROW(gmx::InternalError("Unsupported method type"));
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_THROW(gmx::InternalError(gmx::formatString(
291                                              "Mismatch between tokenizer and reserved symbol table (for '%s')",
292                                              symbol->name().c_str())));
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_THROW(gmx::InternalError("Unsupported variable type"));
313             }
314         }
315         yylval->sel = new gmx::SelectionTreeElementPointer(var);
316         switch (var->v.type)
317         {
318             case INT_VALUE:   return VARIABLE_NUMERIC;
319             case REAL_VALUE:  return VARIABLE_NUMERIC;
320             case POS_VALUE:   return VARIABLE_POS;
321             case GROUP_VALUE: return VARIABLE_GROUP;
322             default:
323                 delete yylval->sel;
324                 GMX_THROW(gmx::InternalError("Unsupported variable type"));
325                 return INVALID;
326         }
327         delete yylval->sel;
328         return INVALID; /* Should not be reached. */
329     }
330     /* For method symbols, return the correct type */
331     if (symtype == gmx::SelectionParserSymbol::MethodSymbol)
332     {
333         gmx_ana_selmethod_t *method = symbol->methodValue();
334         return init_method_token(yylval, method, state->prev_pos_kw > 0, state);
335     }
336     /* For position symbols, we need to return KEYWORD_POS, but we also need
337      * some additional handling. */
338     if (symtype == gmx::SelectionParserSymbol::PositionSymbol)
339     {
340         state->bMatchOf    = true;
341         yylval->str        = strdup(symbol->name().c_str());
342         state->prev_pos_kw = 2;
343         return KEYWORD_POS;
344     }
345     /* Should not be reached */
346     return INVALID;
347 }
348
349 void
350 _gmx_sel_lexer_add_token(const char *str, int len, gmx_sel_lexer_t *state)
351 {
352     /* Do nothing if the string is empty, or if it is a space and there is
353      * no other text yet, or if there already is a space. */
354     if (!str || len == 0 || strlen(str) == 0
355         || (str[0] == ' ' && str[1] == 0
356             && (state->pslen == 0 || state->pselstr[state->pslen - 1] == ' ')))
357     {
358         return;
359     }
360     if (len < 0)
361     {
362         len = strlen(str);
363     }
364     /* Allocate more memory if necessary */
365     if (state->nalloc_psel - state->pslen < len)
366     {
367         int incr = STRSTORE_ALLOCSTEP < len ? len : STRSTORE_ALLOCSTEP;
368         state->nalloc_psel += incr;
369         srenew(state->pselstr, state->nalloc_psel);
370     }
371     /* Append the token to the stored string */
372     strncpy(state->pselstr + state->pslen, str, len);
373     state->pslen                += len;
374     state->pselstr[state->pslen] = 0;
375 }
376
377 void
378 _gmx_sel_init_lexer(yyscan_t *scannerp, struct gmx_ana_selcollection_t *sc,
379                     bool bInteractive, int maxnr, bool bGroups,
380                     struct gmx_ana_indexgrps_t *grps)
381 {
382     int rc = _gmx_sel_yylex_init(scannerp);
383     if (rc != 0)
384     {
385         // TODO: Throw a more representative exception.
386         GMX_THROW(gmx::InternalError("Lexer initialization failed"));
387     }
388
389     gmx_sel_lexer_t *state = new gmx_sel_lexer_t;
390
391     state->sc        = sc;
392     state->errors    = NULL;
393     state->bGroups   = bGroups;
394     state->grps      = grps;
395     state->nexpsel   = (maxnr > 0 ? static_cast<int>(sc->sel.size()) + maxnr : -1);
396
397     state->bInteractive = bInteractive;
398
399     snew(state->pselstr, STRSTORE_ALLOCSTEP);
400     state->pselstr[0]   = 0;
401     state->pslen        = 0;
402     state->nalloc_psel  = STRSTORE_ALLOCSTEP;
403
404     snew(state->mstack, 20);
405     state->mstack_alloc = 20;
406     state->msp          = -1;
407     state->neom         = 0;
408     state->nextparam    = NULL;
409     state->nextmethod   = NULL;
410     state->prev_pos_kw  = 0;
411     state->bBoolNo      = false;
412     state->bMatchOf     = false;
413     state->bMatchBool   = false;
414     state->bCmdStart    = true;
415     state->bBuffer      = false;
416
417     _gmx_sel_yyset_extra(state, *scannerp);
418 }
419
420 void
421 _gmx_sel_free_lexer(yyscan_t scanner)
422 {
423     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
424
425     sfree(state->pselstr);
426     sfree(state->mstack);
427     if (state->bBuffer)
428     {
429         _gmx_sel_yy_delete_buffer(state->buffer, scanner);
430     }
431     delete state;
432     _gmx_sel_yylex_destroy(scanner);
433 }
434
435 void
436 _gmx_sel_set_lexer_error_reporter(yyscan_t                     scanner,
437                                   gmx::MessageStringCollector *errors)
438 {
439     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
440     state->errors = errors;
441 }
442
443 void
444 _gmx_sel_lexer_set_exception(yyscan_t                    scanner,
445                              const boost::exception_ptr &ex)
446 {
447     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
448     state->exception = ex;
449 }
450
451 void
452 _gmx_sel_lexer_rethrow_exception_if_occurred(yyscan_t scanner)
453 {
454     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
455     if (state->exception)
456     {
457         boost::exception_ptr ex = state->exception;
458         state->exception = boost::exception_ptr();
459         rethrow_exception(ex);
460     }
461 }
462
463 bool
464 _gmx_sel_is_lexer_interactive(yyscan_t scanner)
465 {
466     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
467     return state->bInteractive;
468 }
469
470 struct gmx_ana_selcollection_t *
471 _gmx_sel_lexer_selcollection(yyscan_t scanner)
472 {
473     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
474     return state->sc;
475 }
476
477 gmx::MessageStringCollector *
478 _gmx_sel_lexer_error_reporter(yyscan_t scanner)
479 {
480     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
481     GMX_RELEASE_ASSERT(state->errors != NULL, "Error reporter not set");
482     return state->errors;
483 }
484
485 bool
486 _gmx_sel_lexer_has_groups_set(yyscan_t scanner)
487 {
488     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
489     return state->bGroups;
490 }
491
492 struct gmx_ana_indexgrps_t *
493 _gmx_sel_lexer_indexgrps(yyscan_t scanner)
494 {
495     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
496     return state->grps;
497 }
498
499 int
500 _gmx_sel_lexer_exp_selcount(yyscan_t scanner)
501 {
502     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
503     return state->nexpsel;
504 }
505
506 const char *
507 _gmx_sel_lexer_pselstr(yyscan_t scanner)
508 {
509     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
510     return state->pselstr;
511 }
512
513 void
514 _gmx_sel_lexer_clear_pselstr(yyscan_t scanner)
515 {
516     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
517     state->pselstr[0] = 0;
518     state->pslen      = 0;
519 }
520
521 void
522 _gmx_sel_lexer_clear_method_stack(yyscan_t scanner)
523 {
524     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
525
526     state->msp = -1;
527 }
528
529 void
530 _gmx_sel_finish_method(yyscan_t scanner)
531 {
532     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
533
534     if (state->msp >= 0)
535     {
536         --state->msp;
537     }
538 }
539
540 void
541 _gmx_sel_set_lex_input_file(yyscan_t scanner, FILE *fp)
542 {
543     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
544
545     state->bBuffer = true;
546     state->buffer  = _gmx_sel_yy_create_buffer(fp, YY_BUF_SIZE, scanner);
547     _gmx_sel_yy_switch_to_buffer(state->buffer, scanner);
548 }
549
550 void
551 _gmx_sel_set_lex_input_str(yyscan_t scanner, const char *str)
552 {
553     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
554
555     if (state->bBuffer)
556     {
557         _gmx_sel_yy_delete_buffer(state->buffer, scanner);
558     }
559     state->bBuffer = true;
560     state->buffer  = _gmx_sel_yy_scan_string(str, scanner);
561 }