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