060fbc34817836a89d3819794605aba346c1d1b5
[alexxy/gromacs.git] / src / gmxlib / selection / parsetree.h
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 Handling of intermediate selection parser data.
33  *
34  * The data types declared in this header are used by the parser to store
35  * intermediate data when constructing method expressions.
36  * In particular, the parameters for the method are stored.
37  * The intermediate data is freed once a \c t_selelem object can be
38  * constructed.
39  * 
40  * This is an implementation header: there should be no need to use it outside
41  * this directory.
42  */
43 #ifndef SELECTION_PARSETREE_H
44 #define SELECTION_PARSETREE_H
45
46 /*#include <typedefs.h>*/
47 #include <types/simple.h>
48
49
50 #include <selvalue.h>
51
52 struct t_selelem;
53 struct gmx_ana_indexgrps_t;
54 struct gmx_ana_selmethod_t;
55 struct gmx_ana_selparam_t;
56
57 /*! \internal \brief
58  * Describes a parsed value, possibly resulting from expression evaluation.
59  */
60 typedef struct t_selexpr_value
61 {
62     /** Type of the value. */
63     e_selvalue_t            type;
64     /** TRUE if the value is the result of an expression. */
65     bool                    bExpr;
66     union {
67         /** The integer value/range (\p type INT_VALUE); */
68         struct {
69             /** Beginning of the range. */
70             int             i1;
71             /** End of the range; equals \p i1 for a single integer. */
72             int             i2;
73         }                   i;
74         /** The real value/range (\p type REAL_VALUE); */
75         struct {
76             /** Beginning of the range. */
77             real            r1;
78             /** End of the range; equals \p r1 for a single number. */
79             real            r2;
80         }                   r;
81         /** The string value (\p type STR_VALUE); */
82         char               *s;
83         /** The position value (\p type POS_VALUE); */
84         rvec                x;
85         /** The expression if \p bExpr is TRUE. */
86         struct t_selelem   *expr;
87     }                       u;
88     /** Pointer to the next value. */
89     struct t_selexpr_value *next;
90 } t_selexpr_value;
91
92 /*! \internal \brief
93  * Describes a parsed method parameter.
94  */
95 typedef struct t_selexpr_param
96 {
97     /** Name of the parameter. */
98     char                   *name;
99     /** Number of values given for this parameter. */
100     int                     nval;
101     /** Pointer to the first value. */
102     struct t_selexpr_value *value;
103     /** Pointer to the next parameter. */
104     struct t_selexpr_param *next;
105 } t_selexpr_param;
106
107 /** Error reporting function for the selection parser. */
108 void
109 _gmx_selparser_error(const char *fmt, ...);
110
111 /** Allocates and initializes a constant \c t_selexpr_value. */
112 t_selexpr_value *
113 _gmx_selexpr_create_value(e_selvalue_t type);
114 /** Allocates and initializes an expression \c t_selexpr_value. */
115 t_selexpr_value *
116 _gmx_selexpr_create_value_expr(struct t_selelem *expr);
117 /** Allocates and initializes a \c t_selexpr_param. */
118 t_selexpr_param *
119 _gmx_selexpr_create_param(char *name);
120
121 /** Frees the memory allocated for a chain of values. */
122 void
123 _gmx_selexpr_free_values(t_selexpr_value *value);
124 /** Frees the memory allocated for a chain of parameters. */
125 void
126 _gmx_selexpr_free_params(t_selexpr_param *param);
127
128 /** Propagates the flags for selection elements. */
129 int
130 _gmx_selelem_update_flags(struct t_selelem *sel);
131
132 /** Initializes the method parameter data of \ref SEL_EXPRESSION and
133  * \ref SEL_MODIFIER elements. */
134 void
135 _gmx_selelem_init_method_params(struct t_selelem *sel, void *scanner);
136 /** Initializes the method for a \ref SEL_EXPRESSION selection element. */
137 void
138 _gmx_selelem_set_method(struct t_selelem *sel,
139                         struct gmx_ana_selmethod_t *method, void *scanner);
140
141 /** Creates a \c t_selelem for arithmetic expression evaluation. */
142 struct t_selelem *
143 _gmx_sel_init_arithmetic(struct t_selelem *left, struct t_selelem *right,
144                          char op, void *scanner);
145 /** Creates a \c t_selelem for comparsion expression evaluation. */
146 struct t_selelem *
147 _gmx_sel_init_comparison(struct t_selelem *left, struct t_selelem *right,
148                          char *cmpop, void *scanner);
149 /** Creates a \c t_selelem for a keyword expression from the parsed data. */
150 struct t_selelem *
151 _gmx_sel_init_keyword(struct gmx_ana_selmethod_t *method,
152                       t_selexpr_value *args, const char *rpost, void *scanner);
153 /** Creates a \c t_selelem for a method expression from the parsed data. */
154 struct t_selelem *
155 _gmx_sel_init_method(struct gmx_ana_selmethod_t *method,
156                      t_selexpr_param *params, const char *rpost,
157                      void *scanner);
158 /** Creates a \c t_selelem for a modifier expression from the parsed data. */
159 struct t_selelem *
160 _gmx_sel_init_modifier(struct gmx_ana_selmethod_t *mod, t_selexpr_param *params,
161                        struct t_selelem *sel, void *scanner);
162 /** Creates a \c t_selelem for evaluation of reference positions. */
163 struct t_selelem *
164 _gmx_sel_init_position(struct t_selelem *expr, const char *type, void *scanner);
165
166 /** Creates a \c t_selelem for a constant position. */
167 struct t_selelem *
168 _gmx_sel_init_const_position(real x, real y, real z);
169 /** Creates a \c t_selelem for a index group expression using group name. */
170 struct t_selelem *
171 _gmx_sel_init_group_by_name(const char *name, void *scanner);
172 /** Creates a \c t_selelem for a index group expression using group index. */
173 struct t_selelem *
174 _gmx_sel_init_group_by_id(int id, void *scanner);
175 /** Creates a \c t_selelem for a variable reference */
176 struct t_selelem *
177 _gmx_sel_init_variable_ref(struct t_selelem *sel);
178
179 /** Creates a root \c t_selelem for a selection. */
180 struct t_selelem *
181 _gmx_sel_init_selection(char *name, struct t_selelem *sel, void *scanner);
182 /** Creates a root \c t_selelem elements for a variable assignment. */
183 struct t_selelem *
184 _gmx_sel_assign_variable(char *name, struct t_selelem *expr, void *scanner);
185 /** Appends a root \c t_selelem to a selection collection. */
186 struct t_selelem *
187 _gmx_sel_append_selection(struct t_selelem *sel, struct t_selelem *last,
188                           void *scanner);
189 /** Check whether the parser should finish. */
190 bool
191 _gmx_sel_parser_should_finish(void *scanner);
192
193 /** Handle empty commands. */
194 void
195 _gmx_sel_handle_empty_cmd(void *scanner);
196 /** Process help commands. */
197 void
198 _gmx_sel_handle_help_cmd(char *topic, void *scanner);
199
200 /* In params.c */
201 /** Initializes an array of parameters based on input from the selection parser. */
202 bool
203 _gmx_sel_parse_params(t_selexpr_param *pparams, int nparam,
204                       struct gmx_ana_selparam_t *param, struct t_selelem *root,
205                       void *scanner);
206
207 #endif