Initial commit of vpp code.
[vpp.git] / vlib / vlib / lex.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_lex_h
16 #define included_vlib_lex_h
17
18 #include <vppinfra/hash.h>
19 #include <vppinfra/bitmap.h>
20 #include <vppinfra/error.h>
21 #include <vppinfra/pool.h>
22
23 #define foreach_vlib_lex_global_token           \
24   _ (invalid)                                   \
25   _ (eof)                                       \
26   _ (word)                                      \
27   _ (number)                                    \
28   _ (lt)                                        \
29   _ (gt)                                        \
30   _ (dot)                                       \
31   _ (slash)                                     \
32   _ (qmark)                                     \
33   _ (equals)                                    \
34   _ (plus)                                      \
35   _ (minus)                                     \
36   _ (star)                                      \
37   _ (lpar)                                      \
38   _ (rpar)
39
40 typedef enum {
41 #define _(f) VLIB_LEX_##f,
42   foreach_vlib_lex_global_token
43 #undef _
44 } vlib_lex_global_token_t;
45
46 typedef enum {
47   VLIB_LEX_IGNORE,
48   VLIB_LEX_ADD_TO_TOKEN,
49   VLIB_LEX_RETURN,
50   VLIB_LEX_RETURN_AND_RESCAN,
51   VLIB_LEX_KEYWORD_CHECK,
52   VLIB_LEX_START_NUMBER, 
53   VLIB_LEX_ADD_TO_NUMBER,
54 } vlib_lex_action_t;
55
56 typedef struct {
57   u16 action;
58   u16 next_table_index;
59   u16 token;
60 } vlib_lex_table_entry_t;
61
62 typedef struct {
63   char *name;
64   vlib_lex_table_entry_t entries [128];
65 } vlib_lex_table_t;
66
67 typedef struct {
68   u32 token;
69
70   union {
71     uword as_uword;
72     void * as_pointer;
73     char * as_string;
74   } value;
75 } vlib_lex_token_t;
76
77 typedef struct {
78   vlib_lex_table_t * lex_tables;
79   uword * lex_tables_by_name;
80
81   /* Vector of token strings. */
82   char ** lex_token_names;
83
84   /* Hash mapping c string name to token index. */
85   uword * lex_tokens_by_name;
86
87   /* Hash mapping c string keyword name to token index. */
88   uword * lex_keywords;
89
90   vlib_lex_token_t * pushback_vector;
91
92   i32 pushback_sp;
93
94   u32 current_table_index;
95
96   uword current_token_value;
97
98   uword current_number_base;
99
100   /* Input string we are lex-ing. */
101   u8 *input_vector;
102
103   /* Current index into input vector. */
104   u32 current_index;
105
106   /* Re-used vector for forming token strings and hashing them. */
107   u8 * token_buffer;
108 } vlib_lex_main_t;
109
110 vlib_lex_main_t vlib_lex_main;
111
112 always_inline void
113 vlib_lex_cleanup_token (vlib_lex_token_t * t)
114 {
115   if (t->token == VLIB_LEX_word)
116     {
117       u8 * tv = t->value.as_pointer;
118       vec_free (tv);
119     }
120 }
121
122 u16 vlib_lex_add_table (char *name);
123 void vlib_lex_get_token (vlib_lex_main_t *lm, vlib_lex_token_t * result);
124 u16 vlib_lex_add_token (vlib_lex_main_t *lm, char *token_name);
125 void vlib_lex_set_action_range (u32 table_index, u8 lo, u8 hi, u16 action, 
126                                 u16 token, u32 next_table_index);
127 void vlib_lex_reset (vlib_lex_main_t *lm, u8 *input_vector);
128 format_function_t format_vlib_lex_token;
129
130 #endif /* included_vlib_lex_h */