Fixing copyright issues and code contributors
[alexxy/gromacs.git] / src / gmxlib / selection / scanner_internal.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2009, The GROMACS development team,
6  * check out http://www.gromacs.org for more information.
7  * Copyright (c) 2012,2013, by the GROMACS development team, led by
8  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9  * others, as listed in the AUTHORS file in the top-level source
10  * directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 /*! \internal \file
39  * \brief Helper functions for the selection tokenizer.
40  *
41  * This file implements the functions in the headers scanner.h and
42  * scanner_internal.h.
43  */
44 /*! \internal file scanner_flex.h
45  * \brief Generated (from scanner.l) header file by Flex.
46  *
47  * This file contains definitions of functions that are needed in
48  * scanner_internal.c.
49  */
50 #ifdef HAVE_CONFIG_H
51 #include <config.h>
52 #endif
53
54 #include <stdlib.h>
55 #include <typedefs.h>
56 #include <smalloc.h>
57 #include <string.h>
58
59 #include "string2.h"
60 #include "gmx_fatal.h"
61
62 #include <selmethod.h>
63
64 #include "parsetree.h"
65 #include "selcollection.h"
66 #include "selelem.h"
67 #include "symrec.h"
68
69 #include "parser.h"
70 #include "scanner.h"
71 #include "scanner_internal.h"
72
73 #define STRSTORE_ALLOCSTEP 1000
74
75 /* These are defined as macros in the generated scanner_flex.h.
76  * We undefine them here to have them as variable names in the subroutines.
77  * There are other ways of doing this, but this is probably the easiest. */
78 #undef yylval
79 #undef yytext
80 #undef yyleng
81
82 static gmx_bool
83 read_stdin_line(gmx_sel_lexer_t *state)
84 {
85     char *ptr     = state->inputstr;
86     int   max_len = state->nalloc_input;
87     int   totlen = 0;
88
89     if (feof(stdin))
90     {
91         return FALSE;
92     }
93     if (state->bInteractive)
94     {
95         fprintf(stderr, "> ");
96     }
97     /* For some reason (at least on my Linux), fgets() doesn't return until
98      * the user presses Ctrl-D _twice_ at the end of a non-empty line.
99      * This can be a bit confusing for users, but there's not much we can
100      * do, and the chances of a normal user noticing this are not very big. */
101     while (fgets(ptr, max_len, stdin))
102     {
103         int len = strlen(ptr);
104
105         totlen += len;
106         if (len >= 2 && ptr[len - 1] == '\n' && ptr[len - 2] == '\\')
107         {
108             if (state->bInteractive)
109             {
110                 fprintf(stderr, "... ");
111             }
112         }
113         else if (len >= 1 && ptr[len - 1] == '\n')
114         {
115             break;
116         }
117         else if (len < max_len - 1)
118         {
119             if (state->bInteractive)
120             {
121                 fprintf(stderr, "\n");
122             }
123             break;
124         }
125         ptr     += len;
126         max_len -= len;
127         if (max_len <= 2)
128         {
129             max_len += state->nalloc_input;
130             state->nalloc_input *= 2;
131             len = ptr - state->inputstr;
132             srenew(state->inputstr, state->nalloc_input);
133             ptr = state->inputstr + len;
134         }
135     }
136     if (ferror(stdin))
137     {
138         gmx_input("selection reading failed");
139     }
140     return totlen > 0;
141 }
142
143 int
144 _gmx_sel_yyblex(YYSTYPE *yylval, yyscan_t yyscanner)
145 {
146     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(yyscanner);
147     gmx_bool bCmdStart;
148     int token;
149
150     if (!state->bBuffer && !state->inputstr)
151     {
152         state->nalloc_input = 1024;
153         snew(state->inputstr, state->nalloc_input);
154         read_stdin_line(state);
155         _gmx_sel_set_lex_input_str(yyscanner, state->inputstr);
156     }
157     bCmdStart = state->bCmdStart;
158     token = _gmx_sel_yylex(yylval, yyscanner);
159     while (state->inputstr && token == 0 && read_stdin_line(state))
160     {
161         _gmx_sel_set_lex_input_str(yyscanner, state->inputstr);
162         token = _gmx_sel_yylex(yylval, yyscanner);
163     }
164     if (token == 0 && !bCmdStart)
165     {
166         token = CMD_SEP;
167         rtrim(state->pselstr);
168     }
169     state->bCmdStart = (token == CMD_SEP);
170     return token;
171 }
172
173 static int
174 init_param_token(YYSTYPE *yylval, gmx_ana_selparam_t *param, gmx_bool bBoolNo)
175 {
176     if (bBoolNo)
177     {
178         snew(yylval->str, strlen(param->name) + 3);
179         yylval->str[0] = 'n';
180         yylval->str[1] = 'o';
181         strcpy(yylval->str+2, param->name);
182     }
183     else
184     {
185         yylval->str = param->name ? strdup(param->name) : NULL;
186     }
187     return PARAM;
188 }
189
190 static int
191 init_method_token(YYSTYPE *yylval, gmx_ana_selmethod_t *method, gmx_bool bPosMod,
192                   gmx_sel_lexer_t *state)
193 {
194     /* If the previous token was not KEYWORD_POS, return EMPTY_POSMOD
195      * before the actual method to work around a limitation in Bison. */
196     if (!bPosMod && method->type != POS_VALUE)
197     {
198         state->nextmethod = method;
199         return EMPTY_POSMOD;
200     }
201     yylval->meth = method;
202     if (!(method->flags & SMETH_MODIFIER) && method->nparams == 0)
203     {
204         /* Keyword */
205         switch (method->type)
206         {
207             case INT_VALUE:   return KEYWORD_NUMERIC;
208             case REAL_VALUE:  return KEYWORD_NUMERIC;
209             case STR_VALUE:   return KEYWORD_STR;
210             case GROUP_VALUE: return KEYWORD_GROUP;
211             default:          return INVALID;
212         }
213     } else {
214         /* Method with parameters or a modifier */
215         if (method->flags & SMETH_MODIFIER)
216         {
217             /* Remove all methods from the stack */
218             state->msp = -1;
219             if (method->param[1].name == NULL)
220             {
221                 state->nextparam = &method->param[1];
222             }
223         }
224         else
225         {
226             if (method->param[0].name == NULL)
227             {
228                 state->nextparam = &method->param[0];
229             }
230         }
231         ++state->msp;
232         if (state->msp >= state->mstack_alloc)
233         {
234             state->mstack_alloc += 10;
235             srenew(state->mstack, state->mstack_alloc);
236         }
237         state->mstack[state->msp] = method;
238         if (method->flags & SMETH_MODIFIER)
239         {
240             return MODIFIER;
241         }
242         switch (method->type)
243         {
244             case INT_VALUE:   return METHOD_NUMERIC;
245             case REAL_VALUE:  return METHOD_NUMERIC;
246             case POS_VALUE:   return METHOD_POS;
247             case GROUP_VALUE: return METHOD_GROUP;
248             default:
249                 --state->msp;
250                 return INVALID;
251         }
252     }
253     return INVALID; /* Should not be reached */
254 }
255
256 int
257 _gmx_sel_lexer_process_pending(YYSTYPE *yylval, gmx_sel_lexer_t *state)
258 {
259     if (state->nextparam)
260     {
261         gmx_ana_selparam_t *param = state->nextparam;
262         gmx_bool                bBoolNo = state->bBoolNo;
263
264         if (state->neom > 0)
265         {
266             --state->neom;
267             return END_OF_METHOD;
268         }
269         state->nextparam = NULL;
270         state->bBoolNo   = FALSE;
271         _gmx_sel_lexer_add_token(param->name, -1, state);
272         return init_param_token(yylval, param, bBoolNo);
273     }
274     if (state->prev_pos_kw > 0)
275     {
276         --state->prev_pos_kw;
277     }
278     if (state->nextmethod)
279     {
280         gmx_ana_selmethod_t *method = state->nextmethod;
281
282         state->nextmethod = NULL;
283         return init_method_token(yylval, method, TRUE, state);
284     }
285     return 0;
286 }
287
288 int
289 _gmx_sel_lexer_process_identifier(YYSTYPE *yylval, char *yytext, size_t yyleng,
290                                   gmx_sel_lexer_t *state)
291 {
292     gmx_sel_symrec_t *symbol;
293     e_symbol_t        symtype;
294
295     /* Check if the identifier matches with a parameter name */
296     if (state->msp >= 0)
297     {
298         gmx_ana_selparam_t *param = NULL;
299         gmx_bool                bBoolNo = FALSE;
300         int                 sp = state->msp;
301         while (!param && sp >= 0)
302         {
303             int             i;
304             for (i = 0; i < state->mstack[sp]->nparams; ++i)
305             {
306                 /* Skip NULL parameters and too long parameters */
307                 if (state->mstack[sp]->param[i].name == NULL
308                     || strlen(state->mstack[sp]->param[i].name) > yyleng)
309                 {
310                     continue;
311                 }
312                 if (!strncmp(state->mstack[sp]->param[i].name, yytext, yyleng))
313                 {
314                     param = &state->mstack[sp]->param[i];
315                     break;
316                 }
317                 /* Check separately for a 'no' prefix on gmx_boolean parameters */
318                 if (state->mstack[sp]->param[i].val.type == NO_VALUE
319                     && yyleng > 2 && yytext[0] == 'n' && yytext[1] == 'o'
320                     && !strncmp(state->mstack[sp]->param[i].name, yytext+2, yyleng-2))
321                 {
322                     param = &state->mstack[sp]->param[i];
323                     bBoolNo = TRUE;
324                     break;
325                 }
326             }
327             if (!param)
328             {
329                 --sp;
330             }
331         }
332         if (param)
333         {
334             if (param->val.type == NO_VALUE && !bBoolNo)
335             {
336                 state->bMatchBool = TRUE;
337             }
338             if (sp < state->msp)
339             {
340                 state->neom = state->msp - sp - 1;
341                 state->nextparam = param;
342                 state->bBoolNo   = bBoolNo;
343                 return END_OF_METHOD;
344             }
345             _gmx_sel_lexer_add_token(param->name, -1, state);
346             return init_param_token(yylval, param, bBoolNo);
347         }
348     }
349
350     /* Check if the identifier matches with a symbol */
351     symbol = _gmx_sel_find_symbol_len(state->sc->symtab, yytext, yyleng, FALSE);
352     /* If there is no match, return the token as a string */
353     if (!symbol)
354     {
355         yylval->str = gmx_strndup(yytext, yyleng);
356         _gmx_sel_lexer_add_token(yytext, yyleng, state);
357         return IDENTIFIER;
358     }
359     _gmx_sel_lexer_add_token(_gmx_sel_sym_name(symbol), -1, state);
360     symtype = _gmx_sel_sym_type(symbol);
361     /* Reserved symbols should have been caught earlier */
362     if (symtype == SYMBOL_RESERVED)
363     {
364         return INVALID;
365     }
366     /* For variable symbols, return the type of the variable value */
367     if (symtype == SYMBOL_VARIABLE)
368     {
369         t_selelem *var;
370
371         var = _gmx_sel_sym_value_var(symbol);
372         /* Return simple tokens for constant variables */
373         if (var->type == SEL_CONST)
374         {
375             switch (var->v.type)
376             {
377                 case INT_VALUE:
378                     yylval->i = var->v.u.i[0];
379                     return TOK_INT;
380                 case REAL_VALUE:
381                     yylval->r = var->v.u.r[0];
382                     return TOK_REAL;
383                 case POS_VALUE:
384                     break;
385                 default:
386                     return INVALID;
387             }
388         }
389         yylval->sel = var;
390         switch (var->v.type)
391         {
392             case INT_VALUE:   return VARIABLE_NUMERIC;
393             case REAL_VALUE:  return VARIABLE_NUMERIC;
394             case POS_VALUE:   return VARIABLE_POS;
395             case GROUP_VALUE: return VARIABLE_GROUP;
396             default:          return INVALID;
397         }
398         return INVALID;
399     }
400     /* For method symbols, return the correct type */
401     if (symtype == SYMBOL_METHOD)
402     {
403         gmx_ana_selmethod_t *method;
404
405         method = _gmx_sel_sym_value_method(symbol);
406         return init_method_token(yylval, method, state->prev_pos_kw > 0, state);
407     }
408     /* For position symbols, we need to return KEYWORD_POS, but we also need
409      * some additional handling. */
410     if (symtype == SYMBOL_POS)
411     {
412         state->bMatchOf = TRUE;
413         yylval->str = _gmx_sel_sym_name(symbol);
414         state->prev_pos_kw = 2;
415         return KEYWORD_POS;
416     }
417     /* Should not be reached */
418     return INVALID;
419 }
420
421 void
422 _gmx_sel_lexer_add_token(const char *str, int len, gmx_sel_lexer_t *state)
423 {
424     /* Do nothing if the string is empty, or if it is a space and there is
425      * no other text yet, or if there already is a space. */
426     if (!str || len == 0 || strlen(str) == 0
427         || (str[0] == ' ' && str[1] == 0
428             && (state->pslen == 0 || state->pselstr[state->pslen - 1] == ' ')))
429     {
430         return;
431     }
432     if (len < 0)
433     {
434         len = strlen(str);
435     }
436     /* Allocate more memory if necessary */
437     if (state->nalloc_psel - state->pslen < len)
438     {
439         int incr = STRSTORE_ALLOCSTEP < len ? len : STRSTORE_ALLOCSTEP;
440         state->nalloc_psel += incr;
441         srenew(state->pselstr, state->nalloc_psel);
442     }
443     /* Append the token to the stored string */
444     strncpy(state->pselstr + state->pslen, str, len);
445     state->pslen += len;
446     state->pselstr[state->pslen] = 0;
447 }
448
449 int
450 _gmx_sel_init_lexer(yyscan_t *scannerp, struct gmx_ana_selcollection_t *sc,
451                     gmx_bool bInteractive, int maxnr,
452                     struct gmx_ana_indexgrps_t *grps)
453 {
454     gmx_sel_lexer_t *state;
455     int              rc;
456
457     rc = _gmx_sel_yylex_init(scannerp);
458     if (rc != 0)
459     {
460         return rc;
461     }
462
463     snew(state, 1);
464     state->sc        = sc;
465     state->grps      = grps;
466     state->nexpsel   = (maxnr > 0 ? sc->nr + maxnr : -1);
467
468     state->bInteractive = bInteractive;
469     state->nalloc_input = 0;
470     state->inputstr     = NULL;
471
472     snew(state->pselstr, STRSTORE_ALLOCSTEP);
473     state->pselstr[0]   = 0;
474     state->pslen        = 0;
475     state->nalloc_psel  = STRSTORE_ALLOCSTEP;
476
477     snew(state->mstack, 20);
478     state->mstack_alloc = 20;
479     state->msp          = -1;
480     state->neom         = 0;
481     state->nextparam    = NULL;
482     state->nextmethod   = NULL;
483     state->prev_pos_kw  = 0;
484     state->bBoolNo      = FALSE;
485     state->bMatchOf     = FALSE;
486     state->bMatchBool   = FALSE;
487     state->bCmdStart    = TRUE;
488     state->bBuffer      = FALSE;
489
490     _gmx_sel_yyset_extra(state, *scannerp);
491     return 0;
492 }
493
494 void
495 _gmx_sel_free_lexer(yyscan_t scanner)
496 {
497     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
498
499     sfree(state->inputstr);
500     sfree(state->pselstr);
501     sfree(state->mstack);
502     if (state->bBuffer)
503     {
504         _gmx_sel_yy_delete_buffer(state->buffer, scanner);
505     }
506     sfree(state);
507     _gmx_sel_yylex_destroy(scanner);
508 }
509
510 gmx_bool
511 _gmx_sel_is_lexer_interactive(yyscan_t scanner)
512 {
513     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
514     return state->bInteractive;
515 }
516
517 struct gmx_ana_selcollection_t *
518 _gmx_sel_lexer_selcollection(yyscan_t scanner)
519 {
520     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
521     return state->sc;
522 }
523
524 struct gmx_ana_indexgrps_t *
525 _gmx_sel_lexer_indexgrps(yyscan_t scanner)
526 {
527     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
528     return state->grps;
529 }
530
531 int
532 _gmx_sel_lexer_exp_selcount(yyscan_t scanner)
533 {
534     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
535     return state->nexpsel;
536 }
537
538 const char *
539 _gmx_sel_lexer_pselstr(yyscan_t scanner)
540 {
541     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
542     return state->pselstr;
543 }
544
545 void
546 _gmx_sel_lexer_clear_pselstr(yyscan_t scanner)
547 {
548     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
549     state->pselstr[0] = 0;
550     state->pslen      = 0;
551 }
552
553 void
554 _gmx_sel_lexer_clear_method_stack(yyscan_t scanner)
555 {
556     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
557
558     state->msp = -1;
559 }
560
561 void
562 _gmx_sel_finish_method(yyscan_t scanner)
563 {
564     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
565
566     if (state->msp >= 0)
567     {
568         --state->msp;
569     }
570 }
571
572 void
573 _gmx_sel_set_lex_input_file(yyscan_t scanner, FILE *fp)
574 {
575     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
576
577     state->bBuffer = TRUE;
578     state->buffer  = _gmx_sel_yy_create_buffer(fp, YY_BUF_SIZE, scanner);
579     _gmx_sel_yy_switch_to_buffer(state->buffer, scanner);
580 }
581
582 void
583 _gmx_sel_set_lex_input_str(yyscan_t scanner, const char *str)
584 {
585     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
586
587     if (state->bBuffer)
588     {
589         _gmx_sel_yy_delete_buffer(state->buffer, scanner);
590     }
591     state->bBuffer = TRUE;
592     state->buffer  = _gmx_sel_yy_scan_string((yyconst char*) str, scanner);
593 }