FLAME  devel
 All Classes Functions Variables Typedefs Enumerations Pages
glps_parser.h
1 #ifndef GLPS_H
2 #define GLPS_H
3 
4 #include <stdarg.h>
5 
6 #ifdef __cplusplus
7 #include <string>
8 #include <vector>
9 #include <map>
10 #include <set>
11 
12 #include <boost/variant.hpp>
13 #define BOOST_FILESYSTEM_DYN_LINK
14 #include <boost/filesystem.hpp>
15 #include <boost/shared_ptr.hpp>
16 extern "C" {
17 #endif
18 
19 typedef enum {
20  glps_expr_number, // scalar value
21  glps_expr_vector, // vector value
22  glps_expr_string, // string value
23  glps_expr_config, // nested Config
24  glps_expr_var, // variable name
25  glps_expr_line, // beamline name
26  glps_expr_elem, // element label
27  glps_expr_invalid = -1
28 } glps_expr_type;
29 const char *glps_expr_type_name(glps_expr_type);
30 
31 typedef struct parse_context parse_context;
32 
33 typedef struct {
34  void *scanner;
35  parse_context *ctxt;
36 } yacc_arg;
37 
38 typedef struct string_t string_t;
39 
40 typedef struct expr_t expr_t;
41 
42 typedef struct vector_t vector_t;
43 typedef struct kvlist_t kvlist_t;
44 typedef struct strlist_t strlist_t;
45 
46 typedef struct {
47  string_t* key;
48  expr_t *value;
49 } kv_t;
50 
51 string_t* glps_string_alloc(const char *, size_t);
52 
53 void glps_string_cleanup(string_t*);
54 void glps_expr_cleanup(expr_t*);
55 void glps_vector_cleanup(vector_t*);
56 void glps_kvlist_cleanup(kvlist_t*);
57 void glps_strlist_cleanup(strlist_t*);
58 
59 void glps_expr_debug(FILE *, const expr_t *);
60 void glps_string_debug(FILE *, const string_t *);
61 
62 void glps_error(void *scan, parse_context *ctxt, const char*, ...) __attribute__((format(printf,3,4)));
63 void glps_verror(void *scan, parse_context *ctxt, const char*, va_list);
64 
65 void glps_assign(parse_context*, string_t *, expr_t*);
66 void glps_add_element(parse_context*, string_t *label, string_t *etype, kvlist_t*);
67 void glps_add_line(parse_context*, string_t *label, string_t *etype, strlist_t*);
68 void glps_command(parse_context*, string_t *kw);
69 void glps_call1(parse_context*, string_t*, expr_t*);
70 
71 kvlist_t* glps_append_kv(parse_context *ctxt, kvlist_t*, kv_t*);
72 strlist_t* glps_append_expr(parse_context *ctxt, strlist_t*, expr_t *);
73 vector_t* glps_append_vector(parse_context *ctxt, vector_t*, expr_t *);
74 
75 expr_t *glps_add_value(parse_context *ctxt, glps_expr_type t, ...);
76 expr_t *glps_add_op(parse_context *ctxt, string_t *, unsigned N, expr_t **);
77 
78 #ifdef __cplusplus
79 }
80 
81 class Config;
82 
83 typedef boost::variant<
84  double, // glps_expr_number
85  std::vector<double>, // glps_expr_vector
86  std::string, // glps_expr_string,
87  std::vector<std::string>, // glps_expr_line
88  boost::shared_ptr<Config> // glps_expr_config
89 > expr_value_t;
90 
91 struct string_t {
92  std::string str;
93  template<typename A>
94  string_t(A a) : str(a) {}
95  template<typename A, typename B>
96  string_t(A a, B b) : str(a,b) {}
97 };
98 
99 struct expr_t {
100  glps_expr_type etype;
101  expr_value_t value;
102 
103  expr_t(): etype(glps_expr_invalid), value(0.0) {}
104  expr_t(glps_expr_type e, expr_value_t v): etype(e), value(v) {}
105 };
106 
107 struct vector_t {
108  std::vector<double> value;
109 };
110 
111 struct kvlist_t {
112  typedef std::map<std::string, expr_t> map_t;
113  map_t map;
114 };
115 
116 struct strlist_t {
117  typedef std::vector<std::string> list_t;
118  list_t list;
119 };
120 
121 struct parse_var {
122  std::string name;
123  expr_t expr;
124 
125  parse_var() {}
126  parse_var(const char* n, expr_t E) :name(n), expr(E) {}
127 };
128 
129 struct parse_element {
130  std::string label, etype;
131  kvlist_t::map_t props;
132 
133  parse_element(std::string L, std::string E, kvlist_t::map_t& M);
134 };
135 
136 struct parse_line {
137  std::string label, etype;
138  strlist_t::list_t names;
139 
140  parse_line(std::string L, std::string E, strlist_t::list_t& N);
141 };
142 
143 struct operation_t {
144  typedef int (*eval_t)(parse_context*, expr_value_t *, const expr_t* const *);
145 
146  std::string name;
147  eval_t fn;
148  glps_expr_type result_type;
149  std::vector<glps_expr_type> arg_types;
150 
151  operation_t(const char *name, eval_t, glps_expr_type R, unsigned N, va_list args);
152 };
153 
154 struct parse_context {
155  typedef std::vector<parse_var> vars_t;
156  vars_t vars;
157 
158  typedef std::vector<parse_element> elements_t;
159  elements_t elements;
160 
161  typedef std::vector<parse_line> line_t;
162  line_t line;
163 
164  // to enforce uniquness of variable and label names
165  typedef std::map<std::string, size_t> map_idx_t;
166  map_idx_t var_idx, element_idx, line_idx;
167 
168  typedef std::multimap<std::string, operation_t> operations_t;
169  typedef std::pair<operations_t::const_iterator, operations_t::const_iterator> operations_iterator_pair;
170  operations_t operations;
171 
172  void addop(const char *name, operation_t::eval_t, glps_expr_type R, unsigned N, ...);
173 
174  std::string last_error;
175  unsigned last_line;
176  std::ostream *printer;
177 
178  std::vector<char> error_scratch;
179 
183  boost::filesystem::path cwd;
184 
186  parse_context(const char *path=NULL);
187  ~parse_context();
188 
189  void parse(FILE *fp);
190  void parse(const char* s, size_t len);
191  void parse(const std::string& s);
192 
193  void *scanner;
194 };
195 
196 #endif /* __cplusplus */
197 
198 #endif /* GLPS_H */
Associative configuration container.
Definition: config.h:66