blob: 7facba5fe2519bc159798e6a6b0623e16a7cc6a5 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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#include <vlib/vlib.h>
16#include <vlib/lex.h>
17
18vlib_lex_main_t vlib_lex_main;
19
20#define LEX_DEBUG 0
21
Dave Barach9b8ffd92016-07-08 08:13:45 -040022u8 *
23format_vlib_lex_token (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070024{
25 vlib_lex_main_t *lm = va_arg (*args, vlib_lex_main_t *);
Dave Barach9b8ffd92016-07-08 08:13:45 -040026 vlib_lex_token_t *t = va_arg (*args, vlib_lex_token_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070027
28 if (t->token == VLIB_LEX_word)
29 s = format (s, "%s", t->value.as_pointer);
30 else
31 s = format (s, "%s", lm->lex_token_names[t->token]);
32 return s;
33}
34
Dave Barach9b8ffd92016-07-08 08:13:45 -040035void
36vlib_lex_get_token (vlib_lex_main_t * lm, vlib_lex_token_t * rv)
Ed Warnickecb9cada2015-12-08 15:45:58 -070037{
38 u8 c;
39 vlib_lex_table_t *t;
40 vlib_lex_table_entry_t *e;
41 uword tv;
42
43 if (PREDICT_FALSE (lm->pushback_sp >= 0))
44 {
Dave Barach9b8ffd92016-07-08 08:13:45 -040045 rv[0] = lm->pushback_vector[lm->pushback_sp--];
Ed Warnickecb9cada2015-12-08 15:45:58 -070046 return;
47 }
48
49 rv->value.as_uword = ~0;
50
51 while (1)
52 {
Dave Barach9b8ffd92016-07-08 08:13:45 -040053 if (PREDICT_FALSE (lm->current_index >= vec_len (lm->input_vector)))
Ed Warnickecb9cada2015-12-08 15:45:58 -070054 {
55 rv->token = VLIB_LEX_eof;
56 return;
57 }
58
59 t = vec_elt_at_index (lm->lex_tables, lm->current_table_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -040060 c = (lm->input_vector[lm->current_index++]) & 0x7f;
61 e = &t->entries[c];
Ed Warnickecb9cada2015-12-08 15:45:58 -070062 lm->current_table_index = e->next_table_index;
63
64 switch (e->action)
65 {
66 case VLIB_LEX_IGNORE:
67 continue;
68
69 case VLIB_LEX_START_NUMBER:
70 lm->current_token_value = 0;
71 /* fallthru */
72
73 case VLIB_LEX_ADD_TO_NUMBER:
74 lm->current_number_base = e->token;
75 lm->current_token_value *= lm->current_number_base;
76 tv = c - '0';
77 if (tv >= lm->current_number_base)
78 {
79 tv = 10 + c - 'A';
80 if (tv >= lm->current_number_base)
81 tv = 10 + c - 'a';
82 }
83 lm->current_token_value += tv;
84 continue;
85
86 case VLIB_LEX_ADD_TO_TOKEN:
Dave Barach9b8ffd92016-07-08 08:13:45 -040087 vec_add1 (lm->token_buffer, c);
Ed Warnickecb9cada2015-12-08 15:45:58 -070088 continue;
89
Dave Barach9b8ffd92016-07-08 08:13:45 -040090 case VLIB_LEX_KEYWORD_CHECK:
91 {
92 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -070093
Dave Barach9b8ffd92016-07-08 08:13:45 -040094 vec_add1 (lm->token_buffer, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070095
Dave Barach9b8ffd92016-07-08 08:13:45 -040096 /* It's either a keyword or just a word. */
97 p = hash_get_mem (lm->lex_keywords, lm->token_buffer);
98 if (p)
99 {
100 rv->token = p[0];
101 if (LEX_DEBUG > 0)
102 clib_warning ("keyword '%s' token %s",
103 lm->token_buffer,
104 lm->lex_token_names[rv->token]);
105 }
106 else
107 {
108 /* it's a WORD */
109 rv->token = VLIB_LEX_word;
110 rv->value.as_pointer = vec_dup (lm->token_buffer);
111 if (LEX_DEBUG > 0)
112 clib_warning ("%s, value '%s'",
113 lm->lex_token_names[VLIB_LEX_word],
114 rv->value.as_pointer);
115 }
Damjan Marion8bea5892022-04-04 22:40:45 +0200116 vec_set_len (lm->token_buffer, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117
Dave Barach9b8ffd92016-07-08 08:13:45 -0400118 /* Rescan the character which terminated the keyword/word. */
119 lm->current_index--;
120 return;
121 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122
123 case VLIB_LEX_RETURN_AND_RESCAN:
Dave Barach9b8ffd92016-07-08 08:13:45 -0400124 ASSERT (lm->current_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125 lm->current_index--;
126 /* note flow-through */
127
128 case VLIB_LEX_RETURN:
129 rv->token = e->token;
130 rv->value.as_uword = lm->current_token_value;
131 lm->current_token_value = ~0;
132 if (LEX_DEBUG > 0)
133 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400134 clib_warning
135 ("table %s char '%c'(0x%02x) next table %s return %s",
136 t->name, c, c, lm->lex_tables[e->next_table_index].name,
137 lm->lex_token_names[e->token]);
138 if (rv->token == VLIB_LEX_number)
139 clib_warning (" numeric value 0x%x (%d)", rv->value,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140 rv->value);
141 }
142 return;
143 }
144 }
145}
146
Dave Barach9b8ffd92016-07-08 08:13:45 -0400147u16
148vlib_lex_add_token (vlib_lex_main_t * lm, char *token_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149{
150 uword *p;
151 u16 rv;
152
153 p = hash_get_mem (lm->lex_tokens_by_name, token_name);
154
155 if (p)
156 return p[0];
157
158 rv = vec_len (lm->lex_token_names);
159 hash_set_mem (lm->lex_tokens_by_name, token_name, rv);
160 vec_add1 (lm->lex_token_names, token_name);
161
162 return rv;
163}
164
Dave Barach9b8ffd92016-07-08 08:13:45 -0400165static u16
166add_keyword (vlib_lex_main_t * lm, char *keyword, char *token_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167{
168 uword *p;
169 u16 token;
170
171 p = hash_get_mem (lm->lex_keywords, keyword);
172
173 ASSERT (p == 0);
174
175 token = vlib_lex_add_token (lm, token_name);
176
177 hash_set_mem (lm->lex_keywords, keyword, token);
178 return token;
179}
180
Dave Barach9b8ffd92016-07-08 08:13:45 -0400181u16
182vlib_lex_find_or_add_keyword (vlib_lex_main_t * lm, char *keyword,
183 char *token_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400185 uword *p = hash_get_mem (lm->lex_keywords, keyword);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186 return p ? p[0] : add_keyword (lm, keyword, token_name);
187}
188
Dave Barach9b8ffd92016-07-08 08:13:45 -0400189void
190vlib_lex_set_action_range (u32 table_index, u8 lo, u8 hi, u16 action,
191 u16 token, u32 next_table_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192{
193 int i;
194 vlib_lex_main_t *lm = &vlib_lex_main;
195 vlib_lex_table_t *t = pool_elt_at_index (lm->lex_tables, table_index);
196
197 for (i = lo; i <= hi; i++)
198 {
199 ASSERT (i < ARRAY_LEN (t->entries));
200 t->entries[i].action = action;
201 t->entries[i].token = token;
202 t->entries[i].next_table_index = next_table_index;
203 }
204}
205
Dave Barach9b8ffd92016-07-08 08:13:45 -0400206u16
207vlib_lex_add_table (char *name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208{
209 vlib_lex_main_t *lm = &vlib_lex_main;
210 vlib_lex_table_t *t;
211 uword *p;
212
213 p = hash_get_mem (lm->lex_tables_by_name, name);
214
Dave Barach9b8ffd92016-07-08 08:13:45 -0400215 ASSERT (p == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216
217 pool_get_aligned (lm->lex_tables, t, CLIB_CACHE_LINE_BYTES);
218
219 t->name = name;
220
221 hash_set_mem (lm->lex_tables_by_name, name, t - lm->lex_tables);
222
223 vlib_lex_set_action_range (t - lm->lex_tables, 1, 0x7F, VLIB_LEX_IGNORE, ~0,
224 t - lm->lex_tables);
225
Dave Barach9b8ffd92016-07-08 08:13:45 -0400226 vlib_lex_set_action_range (t - lm->lex_tables, 0, 0, VLIB_LEX_RETURN,
227 VLIB_LEX_eof, t - lm->lex_tables);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228
229 return t - lm->lex_tables;
230}
231
Dave Barach9b8ffd92016-07-08 08:13:45 -0400232void
233vlib_lex_reset (vlib_lex_main_t * lm, u8 * input_vector)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234{
235 if (lm->pushback_vector)
Damjan Marion8bea5892022-04-04 22:40:45 +0200236 vec_set_len (lm->pushback_vector, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237 lm->pushback_sp = -1;
238
239 lm->input_vector = input_vector;
240 lm->current_index = 0;
241}
242
Dave Barach9b8ffd92016-07-08 08:13:45 -0400243static clib_error_t *
244lex_onetime_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245{
246 vlib_lex_main_t *lm = &vlib_lex_main;
247
248 lm->lex_tables_by_name = hash_create_string (0, sizeof (uword));
249 lm->lex_tokens_by_name = hash_create_string (0, sizeof (uword));
250 lm->lex_keywords = hash_create_string (0, sizeof (uword));
251 lm->pushback_sp = -1;
252
253#define _(f) { u16 tmp = vlib_lex_add_token (lm, #f); ASSERT (tmp == VLIB_LEX_##f); }
254 foreach_vlib_lex_global_token;
255#undef _
256
257 vec_validate (lm->token_buffer, 127);
Damjan Marion8bea5892022-04-04 22:40:45 +0200258 vec_set_len (lm->token_buffer, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259
260 return 0;
261}
262
263VLIB_INIT_FUNCTION (lex_onetime_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400264
265/*
266 * fd.io coding-style-patch-verification: ON
267 *
268 * Local Variables:
269 * eval: (c-set-style "gnu")
270 * End:
271 */