FD.io VPP  v16.06
Vector Packet Processing
lex.c
Go to the documentation of this file.
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 #include <vlib/vlib.h>
16 #include <vlib/lex.h>
17 
19 
20 #define LEX_DEBUG 0
21 
22 u8 * format_vlib_lex_token (u8 * s, va_list * args)
23 {
24  vlib_lex_main_t *lm = va_arg (*args, vlib_lex_main_t *);
25  vlib_lex_token_t *t = va_arg (*args, vlib_lex_token_t *);
26 
27  if (t->token == VLIB_LEX_word)
28  s = format (s, "%s", t->value.as_pointer);
29  else
30  s = format (s, "%s", lm->lex_token_names[t->token]);
31  return s;
32 }
33 
35 {
36  u8 c;
39  uword tv;
40 
41  if (PREDICT_FALSE (lm->pushback_sp >= 0))
42  {
43  rv[0] = lm->pushback_vector [lm->pushback_sp--];
44  return;
45  }
46 
47  rv->value.as_uword = ~0;
48 
49  while (1)
50  {
52  {
53  rv->token = VLIB_LEX_eof;
54  return;
55  }
56 
58  c = (lm->input_vector [lm->current_index++]) & 0x7f;
59  e = &t->entries [c];
61 
62  switch (e->action)
63  {
64  case VLIB_LEX_IGNORE:
65  continue;
66 
68  lm->current_token_value = 0;
69  /* fallthru */
70 
72  lm->current_number_base = e->token;
74  tv = c - '0';
75  if (tv >= lm->current_number_base)
76  {
77  tv = 10 + c - 'A';
78  if (tv >= lm->current_number_base)
79  tv = 10 + c - 'a';
80  }
81  lm->current_token_value += tv;
82  continue;
83 
85  vec_add1(lm->token_buffer, c);
86  continue;
87 
89  uword * p;
90 
91  vec_add1 (lm->token_buffer, 0);
92 
93  /* It's either a keyword or just a word. */
94  p = hash_get_mem (lm->lex_keywords, lm->token_buffer);
95  if (p)
96  {
97  rv->token = p[0];
98  if (LEX_DEBUG > 0)
99  clib_warning ("keyword '%s' token %s",
100  lm->token_buffer,
101  lm->lex_token_names[rv->token]);
102  }
103  else
104  {
105  /* it's a WORD */
106  rv->token = VLIB_LEX_word;
107  rv->value.as_pointer = vec_dup (lm->token_buffer);
108  if (LEX_DEBUG > 0)
109  clib_warning ("%s, value '%s'",
110  lm->lex_token_names[VLIB_LEX_word],
111  rv->value.as_pointer);
112  }
113  _vec_len (lm->token_buffer) = 0;
114 
115  /* Rescan the character which terminated the keyword/word. */
116  lm->current_index--;
117  return;
118  }
119 
121  ASSERT(lm->current_index);
122  lm->current_index--;
123  /* note flow-through */
124 
125  case VLIB_LEX_RETURN:
126  rv->token = e->token;
128  lm->current_token_value = ~0;
129  if (LEX_DEBUG > 0)
130  {
131  clib_warning ("table %s char '%c'(0x%02x) next table %s return %s",
132  t->name, c, c, lm->lex_tables[e->next_table_index].name,
133  lm->lex_token_names[e->token]);
134  if (rv->token == VLIB_LEX_number)
135  clib_warning (" numeric value 0x%x (%d)", rv->value,
136  rv->value);
137  }
138  return;
139  }
140  }
141 }
142 
143 u16 vlib_lex_add_token (vlib_lex_main_t *lm, char *token_name)
144 {
145  uword *p;
146  u16 rv;
147 
148  p = hash_get_mem (lm->lex_tokens_by_name, token_name);
149 
150  if (p)
151  return p[0];
152 
153  rv = vec_len (lm->lex_token_names);
154  hash_set_mem (lm->lex_tokens_by_name, token_name, rv);
155  vec_add1 (lm->lex_token_names, token_name);
156 
157  return rv;
158 }
159 
160 static u16 add_keyword (vlib_lex_main_t *lm, char *keyword, char *token_name)
161 {
162  uword *p;
163  u16 token;
164 
165  p = hash_get_mem (lm->lex_keywords, keyword);
166 
167  ASSERT (p == 0);
168 
169  token = vlib_lex_add_token (lm, token_name);
170 
171  hash_set_mem (lm->lex_keywords, keyword, token);
172  return token;
173 }
174 
175 u16 vlib_lex_find_or_add_keyword (vlib_lex_main_t *lm, char *keyword, char *token_name)
176 {
177  uword * p = hash_get_mem (lm->lex_keywords, keyword);
178  return p ? p[0] : add_keyword (lm, keyword, token_name);
179 }
180 
181 void vlib_lex_set_action_range (u32 table_index, u8 lo, u8 hi, u16 action,
182  u16 token, u32 next_table_index)
183 {
184  int i;
186  vlib_lex_table_t *t = pool_elt_at_index (lm->lex_tables, table_index);
187 
188  for (i = lo; i <= hi; i++)
189  {
190  ASSERT (i < ARRAY_LEN (t->entries));
191  t->entries[i].action = action;
192  t->entries[i].token = token;
193  t->entries[i].next_table_index = next_table_index;
194  }
195 }
196 
198 {
200  vlib_lex_table_t *t;
201  uword *p;
202 
203  p = hash_get_mem (lm->lex_tables_by_name, name);
204 
205  ASSERT(p == 0);
206 
208 
209  t->name = name;
210 
211  hash_set_mem (lm->lex_tables_by_name, name, t - lm->lex_tables);
212 
214  t - lm->lex_tables);
215 
216  vlib_lex_set_action_range (t - lm->lex_tables, 0, 0, VLIB_LEX_RETURN, VLIB_LEX_eof,
217  t - lm->lex_tables);
218 
219  return t - lm->lex_tables;
220 }
221 
222 void vlib_lex_reset (vlib_lex_main_t *lm, u8 *input_vector)
223 {
224  if (lm->pushback_vector)
225  _vec_len (lm->pushback_vector) = 0;
226  lm->pushback_sp = -1;
227 
228  lm->input_vector = input_vector;
229  lm->current_index = 0;
230 }
231 
233 {
235 
236  lm->lex_tables_by_name = hash_create_string (0, sizeof (uword));
237  lm->lex_tokens_by_name = hash_create_string (0, sizeof (uword));
238  lm->lex_keywords = hash_create_string (0, sizeof (uword));
239  lm->pushback_sp = -1;
240 
241 #define _(f) { u16 tmp = vlib_lex_add_token (lm, #f); ASSERT (tmp == VLIB_LEX_##f); }
243 #undef _
244 
245  vec_validate (lm->token_buffer, 127);
246  _vec_len (lm->token_buffer) = 0;
247 
248  return 0;
249 }
250 
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:394
vmrglw vmrglh hi
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
u8 * input_vector
Definition: lex.h:101
Definition: lex.h:56
uword * lex_tables_by_name
Definition: lex.h:79
uword * lex_tokens_by_name
Definition: lex.h:85
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:480
#define hash_set_mem(h, key, value)
Definition: hash.h:257
void vlib_lex_set_action_range(u32 table_index, u8 lo, u8 hi, u16 action, u16 token, u32 next_table_index)
Definition: lex.c:181
static u16 add_keyword(vlib_lex_main_t *lm, char *keyword, char *token_name)
Definition: lex.c:160
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:109
char ** lex_token_names
Definition: lex.h:82
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define clib_warning(format, args...)
Definition: error.h:59
#define hash_create_string(elts, value_bytes)
Definition: hash.h:609
u16 vlib_lex_add_table(char *name)
Definition: lex.c:197
u32 current_index
Definition: lex.h:104
char * name
Definition: lex.h:63
vlib_lex_token_t * pushback_vector
Definition: lex.h:90
#define pool_elt_at_index(p, i)
Definition: pool.h:346
u16 action
Definition: lex.h:57
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:332
#define PREDICT_FALSE(x)
Definition: clib.h:97
vlib_lex_table_entry_t entries[128]
Definition: lex.h:64
i32 pushback_sp
Definition: lex.h:92
#define pool_get_aligned(P, E, A)
Definition: pool.h:155
vlib_lex_table_t * lex_tables
Definition: lex.h:78
u16 vlib_lex_find_or_add_keyword(vlib_lex_main_t *lm, char *keyword, char *token_name)
Definition: lex.c:175
#define ARRAY_LEN(x)
Definition: clib.h:59
u32 current_table_index
Definition: lex.h:94
u16 vlib_lex_add_token(vlib_lex_main_t *lm, char *token_name)
Definition: lex.c:143
#define LEX_DEBUG
Definition: lex.c:20
#define ASSERT(truth)
uword current_token_value
Definition: lex.h:96
unsigned int u32
Definition: types.h:88
void * as_pointer
Definition: lex.h:72
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
void vlib_lex_reset(vlib_lex_main_t *lm, u8 *input_vector)
Definition: lex.c:222
union vlib_lex_token_t::@31 value
u64 uword
Definition: types.h:112
u16 token
Definition: lex.h:59
uword current_number_base
Definition: lex.h:98
unsigned short u16
Definition: types.h:57
vlib_lex_main_t vlib_lex_main
Definition: lex.c:18
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
void vlib_lex_get_token(vlib_lex_main_t *lm, vlib_lex_token_t *rv)
Definition: lex.c:34
static clib_error_t * lex_onetime_init(vlib_main_t *vm)
Definition: lex.c:232
uword * lex_keywords
Definition: lex.h:88
#define hash_get_mem(h, key)
Definition: hash.h:251
u16 next_table_index
Definition: lex.h:58
uword as_uword
Definition: lex.h:71
u8 * token_buffer
Definition: lex.h:107
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 token
Definition: lex.h:68
u8 * format_vlib_lex_token(u8 *s, va_list *args)
Definition: lex.c:22