Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 40 | typedef enum |
| 41 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 42 | #define _(f) VLIB_LEX_##f, |
| 43 | foreach_vlib_lex_global_token |
| 44 | #undef _ |
| 45 | } vlib_lex_global_token_t; |
| 46 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 47 | typedef enum |
| 48 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | VLIB_LEX_IGNORE, |
| 50 | VLIB_LEX_ADD_TO_TOKEN, |
| 51 | VLIB_LEX_RETURN, |
| 52 | VLIB_LEX_RETURN_AND_RESCAN, |
| 53 | VLIB_LEX_KEYWORD_CHECK, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 54 | VLIB_LEX_START_NUMBER, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | VLIB_LEX_ADD_TO_NUMBER, |
| 56 | } vlib_lex_action_t; |
| 57 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 58 | typedef struct |
| 59 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | u16 action; |
| 61 | u16 next_table_index; |
| 62 | u16 token; |
| 63 | } vlib_lex_table_entry_t; |
| 64 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 65 | typedef struct |
| 66 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | char *name; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 68 | vlib_lex_table_entry_t entries[128]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | } vlib_lex_table_t; |
| 70 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 71 | typedef struct |
| 72 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | u32 token; |
| 74 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 75 | union |
| 76 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | uword as_uword; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 78 | void *as_pointer; |
| 79 | char *as_string; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | } value; |
| 81 | } vlib_lex_token_t; |
| 82 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 83 | typedef struct |
| 84 | { |
| 85 | vlib_lex_table_t *lex_tables; |
| 86 | uword *lex_tables_by_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | |
| 88 | /* Vector of token strings. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 89 | char **lex_token_names; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | |
| 91 | /* Hash mapping c string name to token index. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 92 | uword *lex_tokens_by_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | |
| 94 | /* Hash mapping c string keyword name to token index. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 95 | uword *lex_keywords; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 97 | vlib_lex_token_t *pushback_vector; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | |
| 99 | i32 pushback_sp; |
| 100 | |
| 101 | u32 current_table_index; |
| 102 | |
| 103 | uword current_token_value; |
| 104 | |
| 105 | uword current_number_base; |
| 106 | |
| 107 | /* Input string we are lex-ing. */ |
| 108 | u8 *input_vector; |
| 109 | |
| 110 | /* Current index into input vector. */ |
| 111 | u32 current_index; |
| 112 | |
| 113 | /* Re-used vector for forming token strings and hashing them. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 114 | u8 *token_buffer; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | } vlib_lex_main_t; |
| 116 | |
| 117 | vlib_lex_main_t vlib_lex_main; |
| 118 | |
| 119 | always_inline void |
| 120 | vlib_lex_cleanup_token (vlib_lex_token_t * t) |
| 121 | { |
| 122 | if (t->token == VLIB_LEX_word) |
| 123 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 124 | u8 *tv = t->value.as_pointer; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | vec_free (tv); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | u16 vlib_lex_add_table (char *name); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 130 | void vlib_lex_get_token (vlib_lex_main_t * lm, vlib_lex_token_t * result); |
| 131 | u16 vlib_lex_add_token (vlib_lex_main_t * lm, char *token_name); |
| 132 | void vlib_lex_set_action_range (u32 table_index, u8 lo, u8 hi, u16 action, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | u16 token, u32 next_table_index); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 134 | void vlib_lex_reset (vlib_lex_main_t * lm, u8 * input_vector); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | format_function_t format_vlib_lex_token; |
| 136 | |
| 137 | #endif /* included_vlib_lex_h */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 138 | |
| 139 | /* |
| 140 | * fd.io coding-style-patch-verification: ON |
| 141 | * |
| 142 | * Local Variables: |
| 143 | * eval: (c-set-style "gnu") |
| 144 | * End: |
| 145 | */ |