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