Initial commit of vpp code.
[vpp.git] / vlib / vlib / parse.h
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef included_vlib_parse_h
16 #define included_vlib_parse_h
17
18 #include <vlib/vlib.h>
19 #include <vlib/lex.h>
20 #include <vppinfra/mhash.h>
21
22 typedef struct {
23   /* Word aligned value. */
24   union {
25     u8 as_u8[32 - 1 * sizeof (u16)];
26     void * as_pointer;
27     uword as_uword;
28     word as_word;
29     u64 as_u64;
30   } value;
31
32   /* 16 bit type at end so that 30 bytes of value are aligned. */
33   u16 type;
34 } __attribute ((packed)) vlib_parse_value_t;
35
36 /* Instance of a type. */
37 typedef struct {
38   u32 type;
39
40   u32 origin;
41
42   u32 help_index;
43
44   union {
45     void * as_pointer;
46     uword as_uword;
47   } value;
48 } vlib_parse_item_t;
49
50 typedef struct {
51   /* Index of item for this node. */
52   u32 item;
53
54   /* Graph index of peer (sibling) node (linked list of peers). */
55   u32 peer;
56
57   /* Graph index of deeper (child) node (linked list of children). */
58   u32 deeper;
59 } vlib_parse_graph_t;
60
61 #define foreach_parse_match_type                \
62   _(MATCH_DONE)                                 \
63   _(MATCH_RULE)                                 \
64   _(MATCH_FAIL)                                 \
65   _(MATCH_FULL)                                 \
66   _(MATCH_VALUE)                                \
67   _(MATCH_PARTIAL)                              \
68   _(MATCH_AMBIGUOUS)                            \
69   _(MATCH_EVAL_FAIL)
70
71 typedef enum {
72 #define _(a) VLIB_PARSE_##a,
73   foreach_parse_match_type
74 #undef _
75 } vlib_parse_match_t;
76
77 struct vlib_parse_type;
78 struct vlib_parse_main;
79
80 typedef vlib_parse_match_t (vlib_parse_match_function_t)
81   (struct vlib_parse_main *, 
82    struct vlib_parse_type *,
83    vlib_lex_token_t *,
84    vlib_parse_value_t *);
85 typedef void (vlib_parse_value_cleanup_function_t) (vlib_parse_value_t *);
86
87 typedef struct vlib_parse_type {
88   /* Type name. */
89   char * name;
90
91   vlib_parse_match_function_t * match_function;
92
93   vlib_parse_value_cleanup_function_t * value_cleanup_function;
94
95   format_function_t * format_value;
96
97   u32 rule_index;
98 } vlib_parse_type_t;
99
100 typedef struct {
101   char *initializer;
102   void * eof_match;
103   int rule_length;
104 } parse_registration_t;
105
106 typedef struct vlib_parse_main {
107   /* (type, origin, help, value) tuples */
108   vlib_parse_item_t *parse_items;
109   mhash_t parse_item_hash;
110
111   /* (item, peer, deeper) tuples */
112   vlib_parse_graph_t *parse_graph;
113   u32 root_index;
114
115   u8 *register_input;
116
117   /* parser types */
118   vlib_parse_type_t * parse_types;
119   uword *parse_type_by_name_hash;
120
121   /* Vector of MATCH_VALUEs */
122   vlib_parse_value_t * parse_value;
123   u32 * match_items;
124
125   /* Parse registrations */
126   parse_registration_t **parse_registrations;
127
128   /* Token vector */
129   vlib_lex_token_t *tokens;
130   u32 current_token_index;
131     
132   vlib_lex_main_t *lex_main;
133   vlib_main_t *vlib_main;
134 } vlib_parse_main_t;
135
136 vlib_parse_main_t vlib_parse_main;
137
138 typedef vlib_parse_match_t (vlib_parse_eval_function_t)
139   (vlib_parse_main_t *,
140    vlib_parse_item_t *, 
141    vlib_parse_value_t *);
142
143 vlib_parse_match_t vlib_parse_eval (u8 * input);
144
145 format_function_t format_vlib_parse_value;
146
147 /* FIXME need these to be global? */
148 vlib_parse_match_function_t rule_match, eof_match, word_match, number_match;
149
150 #define _PARSE_REGISTRATION_DATA(x) \
151 VLIB_ELF_SECTION_DATA(x##_registration,parse_registration_t,parse_registrations)
152
153 #define PARSE_INIT(x, s, e)                     \
154 static _PARSE_REGISTRATION_DATA(x) = {          \
155     .initializer = s,                           \
156     .eof_match = e,                             \
157 };
158
159 #define _PARSE_TYPE_REGISTRATION_DATA(x) \
160 VLIB_ELF_SECTION_DATA(x##_type_registration,vlib_parse_type_t, \
161 parse_type_registrations)
162
163 #define PARSE_TYPE_INIT(n, m, c, f)             \
164 static _PARSE_TYPE_REGISTRATION_DATA(n) = {     \
165     .name = #n,                                 \
166     .match_function = m,                        \
167     .value_cleanup_function = c,                \
168     .format_value = f,                          \
169 };
170
171 #endif /* included_vlib_parse_h */