Nathan Skrzypczak | 5c2f964 | 2019-09-09 16:45:06 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 2 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 3 | import sys |
| 4 | import argparse |
Paul Vinciguerra | ff47fb6 | 2019-08-06 19:58:24 -0400 | [diff] [blame] | 5 | import keyword |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 6 | import logging |
| 7 | import binascii |
| 8 | import os |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 9 | from subprocess import Popen, PIPE |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 10 | import ply.lex as lex |
| 11 | import ply.yacc as yacc |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 12 | |
BenoƮt Ganne | 3f0ae66 | 2020-09-09 12:50:07 +0200 | [diff] [blame] | 13 | assert sys.version_info >= (3, 5), \ |
Ole Troan | 14a6c0e | 2020-05-13 11:47:43 +0200 | [diff] [blame] | 14 | "Not supported Python version: {}".format(sys.version) |
Paul Vinciguerra | 2cd3cc8 | 2019-08-06 22:02:45 -0400 | [diff] [blame] | 15 | log = logging.getLogger('vppapigen') |
| 16 | |
Ole Troan | d6743b1 | 2018-03-07 08:40:58 +0100 | [diff] [blame] | 17 | # Ensure we don't leave temporary files around |
| 18 | sys.dont_write_bytecode = True |
| 19 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 20 | # |
| 21 | # VPP API language |
| 22 | # |
| 23 | |
| 24 | # Global dictionary of new types (including enums) |
| 25 | global_types = {} |
| 26 | |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 27 | seen_imports = {} |
| 28 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 29 | |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 30 | def global_type_add(name, obj): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 31 | '''Add new type to the dictionary of types ''' |
| 32 | type_name = 'vl_api_' + name + '_t' |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 33 | if type_name in global_types: |
| 34 | raise KeyError("Attempted redefinition of {!r} with {!r}.".format( |
| 35 | name, obj)) |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 36 | global_types[type_name] = obj |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 37 | |
| 38 | |
| 39 | # All your trace are belong to us! |
| 40 | def exception_handler(exception_type, exception, traceback): |
Ole Troan | 17225df | 2018-04-11 09:50:03 +0200 | [diff] [blame] | 41 | print("%s: %s" % (exception_type.__name__, exception)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 42 | |
| 43 | |
| 44 | # |
| 45 | # Lexer |
| 46 | # |
Paul Vinciguerra | b00c49c | 2020-12-04 15:01:53 -0500 | [diff] [blame] | 47 | class VPPAPILexer: |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 48 | def __init__(self, filename): |
| 49 | self.filename = filename |
| 50 | |
| 51 | reserved = { |
| 52 | 'service': 'SERVICE', |
| 53 | 'rpc': 'RPC', |
| 54 | 'returns': 'RETURNS', |
Marek Gradzki | 51e5968 | 2018-03-06 10:05:44 +0100 | [diff] [blame] | 55 | 'null': 'NULL', |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 56 | 'stream': 'STREAM', |
| 57 | 'events': 'EVENTS', |
| 58 | 'define': 'DEFINE', |
| 59 | 'typedef': 'TYPEDEF', |
| 60 | 'enum': 'ENUM', |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 61 | 'enumflag': 'ENUMFLAG', |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 62 | 'typeonly': 'TYPEONLY', |
| 63 | 'manual_print': 'MANUAL_PRINT', |
| 64 | 'manual_endian': 'MANUAL_ENDIAN', |
| 65 | 'dont_trace': 'DONT_TRACE', |
| 66 | 'autoreply': 'AUTOREPLY', |
Neale Ranns | 9302cfe | 2021-02-02 09:21:52 +0000 | [diff] [blame] | 67 | 'autoendian': 'AUTOENDIAN', |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 68 | 'option': 'OPTION', |
| 69 | 'u8': 'U8', |
| 70 | 'u16': 'U16', |
| 71 | 'u32': 'U32', |
| 72 | 'u64': 'U64', |
| 73 | 'i8': 'I8', |
| 74 | 'i16': 'I16', |
| 75 | 'i32': 'I32', |
| 76 | 'i64': 'I64', |
| 77 | 'f64': 'F64', |
| 78 | 'bool': 'BOOL', |
| 79 | 'string': 'STRING', |
| 80 | 'import': 'IMPORT', |
| 81 | 'true': 'TRUE', |
| 82 | 'false': 'FALSE', |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 83 | 'union': 'UNION', |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 84 | 'counters': 'COUNTERS', |
| 85 | 'paths': 'PATHS', |
| 86 | 'units': 'UNITS', |
| 87 | 'severity': 'SEVERITY', |
| 88 | 'type': 'TYPE', |
| 89 | 'description': 'DESCRIPTION', |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | tokens = ['STRING_LITERAL', |
| 93 | 'ID', 'NUM'] + list(reserved.values()) |
| 94 | |
| 95 | t_ignore_LINE_COMMENT = '//.*' |
| 96 | |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 97 | def t_FALSE(self, t): |
| 98 | r'false' |
| 99 | t.value = False |
| 100 | return t |
| 101 | |
| 102 | def t_TRUE(self, t): |
| 103 | r'false' |
| 104 | t.value = True |
| 105 | return t |
| 106 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 107 | def t_NUM(self, t): |
Paul Vinciguerra | 063f374 | 2019-07-02 13:00:58 -0400 | [diff] [blame] | 108 | r'0[xX][0-9a-fA-F]+|-?\d+\.?\d*' |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 109 | base = 16 if t.value.startswith('0x') else 10 |
Paul Vinciguerra | 063f374 | 2019-07-02 13:00:58 -0400 | [diff] [blame] | 110 | if '.' in t.value: |
| 111 | t.value = float(t.value) |
| 112 | else: |
| 113 | t.value = int(t.value, base) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 114 | return t |
| 115 | |
| 116 | def t_ID(self, t): |
| 117 | r'[a-zA-Z_][a-zA-Z_0-9]*' |
| 118 | # Check for reserved words |
| 119 | t.type = VPPAPILexer.reserved.get(t.value, 'ID') |
| 120 | return t |
| 121 | |
| 122 | # C string |
| 123 | def t_STRING_LITERAL(self, t): |
| 124 | r'\"([^\\\n]|(\\.))*?\"' |
| 125 | t.value = str(t.value).replace("\"", "") |
| 126 | return t |
| 127 | |
| 128 | # C or C++ comment (ignore) |
| 129 | def t_comment(self, t): |
| 130 | r'(/\*(.|\n)*?\*/)|(//.*)' |
| 131 | t.lexer.lineno += t.value.count('\n') |
| 132 | |
| 133 | # Error handling rule |
| 134 | def t_error(self, t): |
| 135 | raise ParseError("Illegal character '{}' ({})" |
| 136 | "in {}: line {}".format(t.value[0], |
| 137 | hex(ord(t.value[0])), |
| 138 | self.filename, |
| 139 | t.lexer.lineno)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 140 | |
| 141 | # Define a rule so we can track line numbers |
| 142 | def t_newline(self, t): |
| 143 | r'\n+' |
| 144 | t.lexer.lineno += len(t.value) |
| 145 | |
| 146 | literals = ":{}[];=.," |
| 147 | |
| 148 | # A string containing ignored characters (spaces and tabs) |
| 149 | t_ignore = ' \t' |
| 150 | |
Ole Troan | 17225df | 2018-04-11 09:50:03 +0200 | [diff] [blame] | 151 | |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 152 | def vla_mark_length_field(block): |
| 153 | if isinstance(block[-1], Array): |
| 154 | lengthfield = block[-1].lengthfield |
| 155 | for b in block: |
| 156 | if b.fieldname == lengthfield: |
| 157 | b.is_lengthfield = True |
| 158 | |
| 159 | |
Ole Troan | d5a78a5 | 2019-09-18 12:12:47 +0200 | [diff] [blame] | 160 | def vla_is_last_check(name, block): |
| 161 | vla = False |
| 162 | for i, b in enumerate(block): |
| 163 | if isinstance(b, Array) and b.vla: |
| 164 | vla = True |
| 165 | if i + 1 < len(block): |
| 166 | raise ValueError( |
| 167 | 'VLA field "{}" must be the last field in message "{}"' |
| 168 | .format(b.fieldname, name)) |
| 169 | elif b.fieldtype.startswith('vl_api_'): |
| 170 | if global_types[b.fieldtype].vla: |
| 171 | vla = True |
| 172 | if i + 1 < len(block): |
| 173 | raise ValueError( |
| 174 | 'VLA field "{}" must be the last ' |
| 175 | 'field in message "{}"' |
| 176 | .format(b.fieldname, name)) |
| 177 | elif b.fieldtype == 'string' and b.length == 0: |
| 178 | vla = True |
| 179 | if i + 1 < len(block): |
| 180 | raise ValueError( |
| 181 | 'VLA field "{}" must be the last ' |
| 182 | 'field in message "{}"' |
| 183 | .format(b.fieldname, name)) |
| 184 | return vla |
| 185 | |
| 186 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 187 | class Processable: |
| 188 | type = "<Invalid>" |
| 189 | |
| 190 | def process(self, result): # -> Dict |
| 191 | result[self.type].append(self) |
| 192 | |
| 193 | |
| 194 | class Service(Processable): |
| 195 | type = 'Service' |
| 196 | |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 197 | def __init__(self, caller, reply, events=None, stream_message=None, |
| 198 | stream=False): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 199 | self.caller = caller |
| 200 | self.reply = reply |
| 201 | self.stream = stream |
Ole Troan | f5db371 | 2020-05-20 15:47:06 +0200 | [diff] [blame] | 202 | self.stream_message = stream_message |
Paul Vinciguerra | 7e0c48e | 2019-02-01 19:37:45 -0800 | [diff] [blame] | 203 | self.events = [] if events is None else events |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 204 | |
| 205 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 206 | class Typedef(Processable): |
| 207 | type = 'Typedef' |
| 208 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 209 | def __init__(self, name, flags, block): |
| 210 | self.name = name |
| 211 | self.flags = flags |
| 212 | self.block = block |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 213 | self.crc = str(block).encode() |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 214 | self.manual_print = False |
| 215 | self.manual_endian = False |
| 216 | for f in flags: |
| 217 | if f == 'manual_print': |
| 218 | self.manual_print = True |
| 219 | elif f == 'manual_endian': |
| 220 | self.manual_endian = True |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 221 | global_type_add(name, self) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 222 | |
Ole Troan | d5a78a5 | 2019-09-18 12:12:47 +0200 | [diff] [blame] | 223 | self.vla = vla_is_last_check(name, block) |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 224 | vla_mark_length_field(self.block) |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 225 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 226 | def process(self, result): |
| 227 | result['types'].append(self) |
| 228 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 229 | def __repr__(self): |
Vratko Polak | 7520e17 | 2019-08-01 10:31:49 +0200 | [diff] [blame] | 230 | return self.name + str(self.flags) + str(self.block) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 231 | |
| 232 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 233 | class Using(Processable): |
| 234 | type = 'Using' |
| 235 | |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 236 | def __init__(self, name, flags, alias): |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 237 | self.name = name |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 238 | self.vla = False |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 239 | self.block = [] |
| 240 | self.manual_print = True |
| 241 | self.manual_endian = True |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 242 | |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 243 | self.manual_print = False |
| 244 | self.manual_endian = False |
| 245 | for f in flags: |
| 246 | if f == 'manual_print': |
| 247 | self.manual_print = True |
| 248 | elif f == 'manual_endian': |
| 249 | self.manual_endian = True |
| 250 | |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 251 | if isinstance(alias, Array): |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 252 | a = {'type': alias.fieldtype, |
| 253 | 'length': alias.length} |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 254 | else: |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 255 | a = {'type': alias.fieldtype} |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 256 | self.alias = a |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 257 | self.using = alias |
| 258 | |
Ole Troan | 6006ca8 | 2020-08-31 13:54:47 +0200 | [diff] [blame] | 259 | # |
| 260 | # Should have been: |
| 261 | # self.crc = str(alias).encode() |
| 262 | # but to be backwards compatible use the block ([]) |
| 263 | # |
| 264 | self.crc = str(self.block).encode() |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 265 | global_type_add(name, self) |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 266 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 267 | def process(self, result): # -> Dict |
| 268 | result['types'].append(self) |
| 269 | |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 270 | def __repr__(self): |
Vratko Polak | 7520e17 | 2019-08-01 10:31:49 +0200 | [diff] [blame] | 271 | return self.name + str(self.alias) |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 272 | |
| 273 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 274 | class Union(Processable): |
| 275 | type = 'Union' |
| 276 | |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 277 | def __init__(self, name, flags, block): |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 278 | self.manual_print = False |
| 279 | self.manual_endian = False |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 280 | self.name = name |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 281 | |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 282 | for f in flags: |
| 283 | if f == 'manual_print': |
| 284 | self.manual_print = True |
| 285 | elif f == 'manual_endian': |
| 286 | self.manual_endian = True |
| 287 | |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 288 | self.block = block |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 289 | self.crc = str(block).encode() |
Ole Troan | d5a78a5 | 2019-09-18 12:12:47 +0200 | [diff] [blame] | 290 | self.vla = vla_is_last_check(name, block) |
| 291 | |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 292 | global_type_add(name, self) |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 293 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 294 | def process(self, result): |
| 295 | result['types'].append(self) |
| 296 | |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 297 | def __repr__(self): |
Vratko Polak | 7520e17 | 2019-08-01 10:31:49 +0200 | [diff] [blame] | 298 | return str(self.block) |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 299 | |
| 300 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 301 | class Define(Processable): |
| 302 | type = 'Define' |
| 303 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 304 | def __init__(self, name, flags, block): |
| 305 | self.name = name |
| 306 | self.flags = flags |
| 307 | self.block = block |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 308 | self.dont_trace = False |
| 309 | self.manual_print = False |
| 310 | self.manual_endian = False |
| 311 | self.autoreply = False |
Neale Ranns | 9302cfe | 2021-02-02 09:21:52 +0000 | [diff] [blame] | 312 | self.autoendian = 0 |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 313 | self.options = {} |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 314 | for f in flags: |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 315 | if f == 'dont_trace': |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 316 | self.dont_trace = True |
| 317 | elif f == 'manual_print': |
| 318 | self.manual_print = True |
| 319 | elif f == 'manual_endian': |
| 320 | self.manual_endian = True |
| 321 | elif f == 'autoreply': |
| 322 | self.autoreply = True |
Neale Ranns | 9302cfe | 2021-02-02 09:21:52 +0000 | [diff] [blame] | 323 | elif f == 'autoendian': |
| 324 | self.autoendian = 1 |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 325 | |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 326 | remove = [] |
Ole Troan | d5a78a5 | 2019-09-18 12:12:47 +0200 | [diff] [blame] | 327 | for b in block: |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 328 | if isinstance(b, Option): |
Ole Troan | 791c206 | 2020-12-08 20:35:32 +0100 | [diff] [blame] | 329 | self.options[b.option] = b.value |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 330 | remove.append(b) |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 331 | |
Ole Troan | 14a6c0e | 2020-05-13 11:47:43 +0200 | [diff] [blame] | 332 | block = [x for x in block if x not in remove] |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 333 | self.block = block |
Ole Troan | d5a78a5 | 2019-09-18 12:12:47 +0200 | [diff] [blame] | 334 | self.vla = vla_is_last_check(name, block) |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 335 | vla_mark_length_field(self.block) |
| 336 | |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 337 | self.crc = str(block).encode() |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 338 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 339 | def autoreply_block(self, name, parent): |
| 340 | block = [Field('u32', 'context'), |
| 341 | Field('i32', 'retval')] |
| 342 | # inherit the parent's options |
| 343 | for k, v in parent.options.items(): |
| 344 | block.append(Option(k, v)) |
| 345 | return Define(name + '_reply', [], block) |
| 346 | |
| 347 | def process(self, result): # -> Dict |
| 348 | tname = self.__class__.__name__ |
| 349 | result[tname].append(self) |
| 350 | if self.autoreply: |
| 351 | result[tname].append(self.autoreply_block(self.name, self)) |
| 352 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 353 | def __repr__(self): |
Vratko Polak | 7520e17 | 2019-08-01 10:31:49 +0200 | [diff] [blame] | 354 | return self.name + str(self.flags) + str(self.block) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 355 | |
| 356 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 357 | class Enum(Processable): |
| 358 | type = 'Enum' |
| 359 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 360 | def __init__(self, name, block, enumtype='u32'): |
| 361 | self.name = name |
| 362 | self.enumtype = enumtype |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 363 | self.vla = False |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 364 | self.manual_print = False |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 365 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 366 | count = 0 |
Ole Troan | 6006ca8 | 2020-08-31 13:54:47 +0200 | [diff] [blame] | 367 | block2 = [] |
| 368 | block3 = [] |
| 369 | bc_set = False |
| 370 | |
| 371 | for b in block: |
| 372 | if 'value' in b: |
| 373 | count = b['value'] |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 374 | else: |
| 375 | count += 1 |
Ole Troan | 6006ca8 | 2020-08-31 13:54:47 +0200 | [diff] [blame] | 376 | block2.append([b['id'], count]) |
| 377 | try: |
| 378 | if b['option']['backwards_compatible']: |
| 379 | pass |
| 380 | bc_set = True |
| 381 | except KeyError: |
| 382 | block3.append([b['id'], count]) |
| 383 | if bc_set: |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 384 | raise ValueError("Backward compatible enum must " |
| 385 | "be last {!r} {!r}" |
Ole Troan | 6006ca8 | 2020-08-31 13:54:47 +0200 | [diff] [blame] | 386 | .format(name, b['id'])) |
| 387 | self.block = block2 |
| 388 | self.crc = str(block3).encode() |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 389 | global_type_add(name, self) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 390 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 391 | def process(self, result): |
| 392 | result['types'].append(self) |
| 393 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 394 | def __repr__(self): |
Vratko Polak | 7520e17 | 2019-08-01 10:31:49 +0200 | [diff] [blame] | 395 | return self.name + str(self.block) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 396 | |
| 397 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 398 | class EnumFlag(Enum): |
| 399 | type = 'EnumFlag' |
| 400 | |
| 401 | def __init__(self, name, block, enumtype='u32'): |
| 402 | super(EnumFlag, self).__init__(name, block, enumtype) |
| 403 | |
| 404 | for b in self.block: |
| 405 | if bin(b[1])[2:].count("1") > 1: |
| 406 | raise TypeError("%s is not a flag enum. No element in a " |
| 407 | "flag enum may have more than a " |
| 408 | "single bit set." % self.name) |
| 409 | |
| 410 | |
| 411 | class Import(Processable): |
| 412 | type = 'Import' |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 413 | _initialized = False |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 414 | |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 415 | def __new__(cls, *args, **kwargs): |
| 416 | if args[0] not in seen_imports: |
| 417 | instance = super().__new__(cls) |
| 418 | instance._initialized = False |
| 419 | seen_imports[args[0]] = instance |
| 420 | |
| 421 | return seen_imports[args[0]] |
| 422 | |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 423 | def __init__(self, filename, revision): |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 424 | if self._initialized: |
| 425 | return |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 426 | self.filename = filename |
| 427 | # Deal with imports |
| 428 | parser = VPPAPI(filename=filename, revision=revision) |
| 429 | dirlist = dirlist_get() |
| 430 | f = filename |
| 431 | for dir in dirlist: |
| 432 | f = os.path.join(dir, filename) |
| 433 | if os.path.exists(f): |
| 434 | break |
| 435 | self.result = parser.parse_filename(f, None) |
| 436 | self._initialized = True |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 437 | |
| 438 | def __repr__(self): |
Vratko Polak | 7520e17 | 2019-08-01 10:31:49 +0200 | [diff] [blame] | 439 | return self.filename |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 440 | |
| 441 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 442 | class Option(Processable): |
| 443 | type = 'Option' |
| 444 | |
Ole Troan | 68ebcd5 | 2020-08-10 17:06:44 +0200 | [diff] [blame] | 445 | def __init__(self, option, value=None): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 446 | self.option = option |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 447 | self.value = value |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 448 | self.crc = str(option).encode() |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 449 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 450 | def process(self, result): # -> Dict |
| 451 | result[self.type][self.option] = self.value |
| 452 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 453 | def __repr__(self): |
Vratko Polak | 7520e17 | 2019-08-01 10:31:49 +0200 | [diff] [blame] | 454 | return str(self.option) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 455 | |
| 456 | def __getitem__(self, index): |
| 457 | return self.option[index] |
| 458 | |
| 459 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 460 | class Array(Processable): |
| 461 | type = 'Array' |
| 462 | |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 463 | def __init__(self, fieldtype, name, length, modern_vla=False): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 464 | self.fieldtype = fieldtype |
| 465 | self.fieldname = name |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 466 | self.modern_vla = modern_vla |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 467 | if type(length) is str: |
| 468 | self.lengthfield = length |
| 469 | self.length = 0 |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 470 | self.vla = True |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 471 | else: |
| 472 | self.length = length |
| 473 | self.lengthfield = None |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 474 | self.vla = False |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 475 | |
| 476 | def __repr__(self): |
Vratko Polak | 7520e17 | 2019-08-01 10:31:49 +0200 | [diff] [blame] | 477 | return str([self.fieldtype, self.fieldname, self.length, |
| 478 | self.lengthfield]) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 479 | |
| 480 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 481 | class Field(Processable): |
| 482 | type = 'Field' |
| 483 | |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 484 | def __init__(self, fieldtype, name, limit=None): |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 485 | # limit field has been expanded to an options dict. |
| 486 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 487 | self.fieldtype = fieldtype |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 488 | self.is_lengthfield = False |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 489 | |
| 490 | if self.fieldtype == 'string': |
| 491 | raise ValueError("The string type {!r} is an " |
| 492 | "array type ".format(name)) |
| 493 | |
Paul Vinciguerra | ff47fb6 | 2019-08-06 19:58:24 -0400 | [diff] [blame] | 494 | if name in keyword.kwlist: |
| 495 | raise ValueError("Fieldname {!r} is a python keyword and is not " |
| 496 | "accessible via the python API. ".format(name)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 497 | self.fieldname = name |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 498 | self.limit = limit |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 499 | |
| 500 | def __repr__(self): |
Vratko Polak | 7520e17 | 2019-08-01 10:31:49 +0200 | [diff] [blame] | 501 | return str([self.fieldtype, self.fieldname]) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 502 | |
| 503 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 504 | class Counter(Processable): |
| 505 | type = 'Counter' |
| 506 | |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 507 | def __init__(self, path, counter): |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 508 | self.name = path |
| 509 | self.block = counter |
| 510 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 511 | def process(self, result): # -> Dict |
| 512 | result['Counters'].append(self) |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 513 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 514 | |
| 515 | class Paths(Processable): |
| 516 | type = 'Paths' |
| 517 | |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 518 | def __init__(self, pathset): |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 519 | self.paths = pathset |
| 520 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 521 | def __repr__(self): |
| 522 | return "%s(paths=%s)" % ( |
| 523 | self.__class__.__name__, self.paths |
| 524 | ) |
| 525 | |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 526 | |
Paul Vinciguerra | b00c49c | 2020-12-04 15:01:53 -0500 | [diff] [blame] | 527 | class Coord: |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 528 | """ Coordinates of a syntactic element. Consists of: |
| 529 | - File name |
| 530 | - Line number |
| 531 | - (optional) column number, for the Lexer |
| 532 | """ |
| 533 | __slots__ = ('file', 'line', 'column', '__weakref__') |
| 534 | |
| 535 | def __init__(self, file, line, column=None): |
| 536 | self.file = file |
| 537 | self.line = line |
| 538 | self.column = column |
| 539 | |
| 540 | def __str__(self): |
| 541 | str = "%s:%s" % (self.file, self.line) |
| 542 | if self.column: |
| 543 | str += ":%s" % self.column |
| 544 | return str |
| 545 | |
| 546 | |
| 547 | class ParseError(Exception): |
| 548 | pass |
| 549 | |
| 550 | |
| 551 | # |
| 552 | # Grammar rules |
| 553 | # |
Paul Vinciguerra | b00c49c | 2020-12-04 15:01:53 -0500 | [diff] [blame] | 554 | class VPPAPIParser: |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 555 | tokens = VPPAPILexer.tokens |
| 556 | |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 557 | def __init__(self, filename, logger, revision=None): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 558 | self.filename = filename |
| 559 | self.logger = logger |
| 560 | self.fields = [] |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 561 | self.revision = revision |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 562 | |
| 563 | def _parse_error(self, msg, coord): |
| 564 | raise ParseError("%s: %s" % (coord, msg)) |
| 565 | |
| 566 | def _parse_warning(self, msg, coord): |
| 567 | if self.logger: |
| 568 | self.logger.warning("%s: %s" % (coord, msg)) |
| 569 | |
| 570 | def _coord(self, lineno, column=None): |
| 571 | return Coord( |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 572 | file=self.filename, |
| 573 | line=lineno, column=column) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 574 | |
| 575 | def _token_coord(self, p, token_idx): |
| 576 | """ Returns the coordinates for the YaccProduction object 'p' indexed |
| 577 | with 'token_idx'. The coordinate includes the 'lineno' and |
| 578 | 'column'. Both follow the lex semantic, starting from 1. |
| 579 | """ |
| 580 | last_cr = p.lexer.lexdata.rfind('\n', 0, p.lexpos(token_idx)) |
| 581 | if last_cr < 0: |
| 582 | last_cr = -1 |
| 583 | column = (p.lexpos(token_idx) - (last_cr)) |
| 584 | return self._coord(p.lineno(token_idx), column) |
| 585 | |
| 586 | def p_slist(self, p): |
| 587 | '''slist : stmt |
| 588 | | slist stmt''' |
| 589 | if len(p) == 2: |
| 590 | p[0] = [p[1]] |
| 591 | else: |
| 592 | p[0] = p[1] + [p[2]] |
| 593 | |
| 594 | def p_stmt(self, p): |
| 595 | '''stmt : define |
| 596 | | typedef |
| 597 | | option |
| 598 | | import |
| 599 | | enum |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 600 | | enumflag |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 601 | | union |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 602 | | service |
| 603 | | paths |
| 604 | | counters''' |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 605 | p[0] = p[1] |
| 606 | |
| 607 | def p_import(self, p): |
| 608 | '''import : IMPORT STRING_LITERAL ';' ''' |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 609 | p[0] = Import(p[2], revision=self.revision) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 610 | |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 611 | def p_path_elements(self, p): |
| 612 | '''path_elements : path_element |
| 613 | | path_elements path_element''' |
| 614 | if len(p) == 2: |
| 615 | p[0] = p[1] |
| 616 | else: |
| 617 | if type(p[1]) is dict: |
| 618 | p[0] = [p[1], p[2]] |
| 619 | else: |
| 620 | p[0] = p[1] + [p[2]] |
| 621 | |
| 622 | def p_path_element(self, p): |
| 623 | '''path_element : STRING_LITERAL STRING_LITERAL ';' ''' |
| 624 | p[0] = {'path': p[1], 'counter': p[2]} |
| 625 | |
| 626 | def p_paths(self, p): |
| 627 | '''paths : PATHS '{' path_elements '}' ';' ''' |
| 628 | p[0] = Paths(p[3]) |
| 629 | |
| 630 | def p_counters(self, p): |
| 631 | '''counters : COUNTERS ID '{' counter_elements '}' ';' ''' |
| 632 | p[0] = Counter(p[2], p[4]) |
| 633 | |
| 634 | def p_counter_elements(self, p): |
| 635 | '''counter_elements : counter_element |
| 636 | | counter_elements counter_element''' |
| 637 | if len(p) == 2: |
Ole Troan | 18327be | 2021-01-12 21:49:38 +0100 | [diff] [blame] | 638 | p[0] = [p[1]] |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 639 | else: |
| 640 | if type(p[1]) is dict: |
| 641 | p[0] = [p[1], p[2]] |
| 642 | else: |
| 643 | p[0] = p[1] + [p[2]] |
| 644 | |
| 645 | def p_counter_element(self, p): |
| 646 | '''counter_element : ID '{' counter_statements '}' ';' ''' |
| 647 | p[0] = {**{'name': p[1]}, **p[3]} |
| 648 | |
| 649 | def p_counter_statements(self, p): |
| 650 | '''counter_statements : counter_statement |
| 651 | | counter_statements counter_statement''' |
| 652 | if len(p) == 2: |
| 653 | p[0] = p[1] |
| 654 | else: |
| 655 | p[0] = {**p[1], **p[2]} |
| 656 | |
| 657 | def p_counter_statement(self, p): |
| 658 | '''counter_statement : SEVERITY ID ';' |
| 659 | | UNITS STRING_LITERAL ';' |
| 660 | | DESCRIPTION STRING_LITERAL ';' |
| 661 | | TYPE ID ';' ''' |
| 662 | p[0] = {p[1]: p[2]} |
| 663 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 664 | def p_service(self, p): |
| 665 | '''service : SERVICE '{' service_statements '}' ';' ''' |
| 666 | p[0] = p[3] |
| 667 | |
| 668 | def p_service_statements(self, p): |
| 669 | '''service_statements : service_statement |
| 670 | | service_statements service_statement''' |
| 671 | if len(p) == 2: |
| 672 | p[0] = [p[1]] |
| 673 | else: |
| 674 | p[0] = p[1] + [p[2]] |
| 675 | |
| 676 | def p_service_statement(self, p): |
Marek Gradzki | 51e5968 | 2018-03-06 10:05:44 +0100 | [diff] [blame] | 677 | '''service_statement : RPC ID RETURNS NULL ';' |
| 678 | | RPC ID RETURNS ID ';' |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 679 | | RPC ID RETURNS STREAM ID ';' |
| 680 | | RPC ID RETURNS ID EVENTS event_list ';' ''' |
Marek Gradzki | fc70e3a | 2018-03-06 10:56:26 +0100 | [diff] [blame] | 681 | if p[2] == p[4]: |
| 682 | # Verify that caller and reply differ |
Ole Troan | 17225df | 2018-04-11 09:50:03 +0200 | [diff] [blame] | 683 | self._parse_error( |
| 684 | 'Reply ID ({}) should not be equal to Caller ID'.format(p[2]), |
| 685 | self._token_coord(p, 1)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 686 | if len(p) == 8: |
| 687 | p[0] = Service(p[2], p[4], p[6]) |
| 688 | elif len(p) == 7: |
| 689 | p[0] = Service(p[2], p[5], stream=True) |
| 690 | else: |
| 691 | p[0] = Service(p[2], p[4]) |
| 692 | |
Ole Troan | f5db371 | 2020-05-20 15:47:06 +0200 | [diff] [blame] | 693 | def p_service_statement2(self, p): |
| 694 | '''service_statement : RPC ID RETURNS ID STREAM ID ';' ''' |
| 695 | p[0] = Service(p[2], p[4], stream_message=p[6], stream=True) |
| 696 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 697 | def p_event_list(self, p): |
| 698 | '''event_list : events |
| 699 | | event_list events ''' |
| 700 | if len(p) == 2: |
| 701 | p[0] = [p[1]] |
| 702 | else: |
| 703 | p[0] = p[1] + [p[2]] |
| 704 | |
| 705 | def p_event(self, p): |
| 706 | '''events : ID |
| 707 | | ID ',' ''' |
| 708 | p[0] = p[1] |
| 709 | |
| 710 | def p_enum(self, p): |
| 711 | '''enum : ENUM ID '{' enum_statements '}' ';' ''' |
| 712 | p[0] = Enum(p[2], p[4]) |
| 713 | |
| 714 | def p_enum_type(self, p): |
| 715 | ''' enum : ENUM ID ':' enum_size '{' enum_statements '}' ';' ''' |
| 716 | if len(p) == 9: |
| 717 | p[0] = Enum(p[2], p[6], enumtype=p[4]) |
| 718 | else: |
| 719 | p[0] = Enum(p[2], p[4]) |
| 720 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 721 | def p_enumflag(self, p): |
| 722 | '''enumflag : ENUMFLAG ID '{' enum_statements '}' ';' ''' |
| 723 | p[0] = EnumFlag(p[2], p[4]) |
| 724 | |
| 725 | def p_enumflag_type(self, p): |
Paul Vinciguerra | 6d467b3 | 2020-12-13 04:12:55 +0000 | [diff] [blame] | 726 | ''' enumflag : ENUMFLAG ID ':' enumflag_size '{' enum_statements '}' ';' ''' # noqa : E502 |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 727 | if len(p) == 9: |
| 728 | p[0] = EnumFlag(p[2], p[6], enumtype=p[4]) |
| 729 | else: |
| 730 | p[0] = EnumFlag(p[2], p[4]) |
| 731 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 732 | def p_enum_size(self, p): |
| 733 | ''' enum_size : U8 |
| 734 | | U16 |
Paul Vinciguerra | 6d467b3 | 2020-12-13 04:12:55 +0000 | [diff] [blame] | 735 | | U32 |
| 736 | | I8 |
| 737 | | I16 |
| 738 | | I32 ''' |
| 739 | p[0] = p[1] |
| 740 | |
| 741 | def p_enumflag_size(self, p): |
| 742 | ''' enumflag_size : U8 |
| 743 | | U16 |
| 744 | | U32 ''' |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 745 | p[0] = p[1] |
| 746 | |
| 747 | def p_define(self, p): |
| 748 | '''define : DEFINE ID '{' block_statements_opt '}' ';' ''' |
| 749 | self.fields = [] |
| 750 | p[0] = Define(p[2], [], p[4]) |
| 751 | |
| 752 | def p_define_flist(self, p): |
| 753 | '''define : flist DEFINE ID '{' block_statements_opt '}' ';' ''' |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 754 | # Legacy typedef |
| 755 | if 'typeonly' in p[1]: |
Paul Vinciguerra | e717482 | 2019-08-07 00:05:59 -0400 | [diff] [blame] | 756 | self._parse_error('legacy typedef. use typedef: {} {}[{}];' |
| 757 | .format(p[1], p[2], p[4]), |
| 758 | self._token_coord(p, 1)) |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 759 | else: |
| 760 | p[0] = Define(p[3], p[1], p[5]) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 761 | |
| 762 | def p_flist(self, p): |
| 763 | '''flist : flag |
| 764 | | flist flag''' |
| 765 | if len(p) == 2: |
| 766 | p[0] = [p[1]] |
| 767 | else: |
| 768 | p[0] = p[1] + [p[2]] |
| 769 | |
| 770 | def p_flag(self, p): |
| 771 | '''flag : MANUAL_PRINT |
| 772 | | MANUAL_ENDIAN |
| 773 | | DONT_TRACE |
| 774 | | TYPEONLY |
Neale Ranns | 9302cfe | 2021-02-02 09:21:52 +0000 | [diff] [blame] | 775 | | AUTOENDIAN |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 776 | | AUTOREPLY''' |
| 777 | if len(p) == 1: |
| 778 | return |
| 779 | p[0] = p[1] |
| 780 | |
| 781 | def p_typedef(self, p): |
| 782 | '''typedef : TYPEDEF ID '{' block_statements_opt '}' ';' ''' |
| 783 | p[0] = Typedef(p[2], [], p[4]) |
| 784 | |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 785 | def p_typedef_flist(self, p): |
| 786 | '''typedef : flist TYPEDEF ID '{' block_statements_opt '}' ';' ''' |
| 787 | p[0] = Typedef(p[3], p[1], p[5]) |
| 788 | |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 789 | def p_typedef_alias(self, p): |
| 790 | '''typedef : TYPEDEF declaration ''' |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 791 | p[0] = Using(p[2].fieldname, [], p[2]) |
| 792 | |
| 793 | def p_typedef_alias_flist(self, p): |
| 794 | '''typedef : flist TYPEDEF declaration ''' |
| 795 | p[0] = Using(p[3].fieldname, p[1], p[3]) |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 796 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 797 | def p_block_statements_opt(self, p): |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 798 | '''block_statements_opt : block_statements ''' |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 799 | p[0] = p[1] |
| 800 | |
| 801 | def p_block_statements(self, p): |
| 802 | '''block_statements : block_statement |
| 803 | | block_statements block_statement''' |
| 804 | if len(p) == 2: |
| 805 | p[0] = [p[1]] |
| 806 | else: |
| 807 | p[0] = p[1] + [p[2]] |
| 808 | |
| 809 | def p_block_statement(self, p): |
| 810 | '''block_statement : declaration |
| 811 | | option ''' |
| 812 | p[0] = p[1] |
| 813 | |
| 814 | def p_enum_statements(self, p): |
| 815 | '''enum_statements : enum_statement |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 816 | | enum_statements enum_statement''' |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 817 | if len(p) == 2: |
| 818 | p[0] = [p[1]] |
| 819 | else: |
| 820 | p[0] = p[1] + [p[2]] |
| 821 | |
| 822 | def p_enum_statement(self, p): |
| 823 | '''enum_statement : ID '=' NUM ',' |
Ole Troan | 6006ca8 | 2020-08-31 13:54:47 +0200 | [diff] [blame] | 824 | | ID ',' |
| 825 | | ID '[' field_options ']' ',' |
| 826 | | ID '=' NUM '[' field_options ']' ',' ''' |
| 827 | if len(p) == 3: |
| 828 | p[0] = {'id': p[1]} |
| 829 | elif len(p) == 5: |
| 830 | p[0] = {'id': p[1], 'value': p[3]} |
| 831 | elif len(p) == 6: |
| 832 | p[0] = {'id': p[1], 'option': p[3]} |
| 833 | elif len(p) == 8: |
| 834 | p[0] = {'id': p[1], 'value': p[3], 'option': p[5]} |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 835 | else: |
Ole Troan | 6006ca8 | 2020-08-31 13:54:47 +0200 | [diff] [blame] | 836 | self._parse_error('ERROR', self._token_coord(p, 1)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 837 | |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 838 | def p_field_options(self, p): |
| 839 | '''field_options : field_option |
| 840 | | field_options field_option''' |
| 841 | if len(p) == 2: |
| 842 | p[0] = p[1] |
| 843 | else: |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 844 | p[0] = {**p[1], **p[2]} |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 845 | |
| 846 | def p_field_option(self, p): |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 847 | '''field_option : ID |
| 848 | | ID '=' assignee ',' |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 849 | | ID '=' assignee |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 850 | |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 851 | ''' |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 852 | if len(p) == 2: |
| 853 | p[0] = {p[1]: None} |
| 854 | else: |
| 855 | p[0] = {p[1]: p[3]} |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 856 | |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 857 | def p_variable_name(self, p): |
| 858 | '''variable_name : ID |
| 859 | | TYPE |
| 860 | | SEVERITY |
| 861 | | DESCRIPTION |
| 862 | | COUNTERS |
| 863 | | PATHS |
| 864 | ''' |
| 865 | p[0] = p[1] |
| 866 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 867 | def p_declaration(self, p): |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 868 | '''declaration : type_specifier variable_name ';' |
| 869 | | type_specifier variable_name '[' field_options ']' ';' |
| 870 | ''' |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 871 | if len(p) == 7: |
| 872 | p[0] = Field(p[1], p[2], p[4]) |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 873 | elif len(p) == 4: |
| 874 | p[0] = Field(p[1], p[2]) |
| 875 | else: |
Paul Vinciguerra | 582eac5 | 2020-04-03 12:18:40 -0400 | [diff] [blame] | 876 | self._parse_error('ERROR', self._token_coord(p, 1)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 877 | self.fields.append(p[2]) |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 878 | |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 879 | def p_declaration_array_vla(self, p): |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 880 | '''declaration : type_specifier variable_name '[' ']' ';' ''' |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 881 | p[0] = Array(p[1], p[2], 0, modern_vla=True) |
| 882 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 883 | def p_declaration_array(self, p): |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 884 | '''declaration : type_specifier variable_name '[' NUM ']' ';' |
| 885 | | type_specifier variable_name '[' ID ']' ';' ''' |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 886 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 887 | if len(p) != 7: |
| 888 | return self._parse_error( |
| 889 | 'array: %s' % p.value, |
| 890 | self._coord(lineno=p.lineno)) |
| 891 | |
| 892 | # Make this error later |
| 893 | if type(p[4]) is int and p[4] == 0: |
| 894 | # XXX: Line number is wrong |
| 895 | self._parse_warning('Old Style VLA: {} {}[{}];' |
| 896 | .format(p[1], p[2], p[4]), |
| 897 | self._token_coord(p, 1)) |
| 898 | |
| 899 | if type(p[4]) is str and p[4] not in self.fields: |
| 900 | # Verify that length field exists |
| 901 | self._parse_error('Missing length field: {} {}[{}];' |
| 902 | .format(p[1], p[2], p[4]), |
| 903 | self._token_coord(p, 1)) |
| 904 | p[0] = Array(p[1], p[2], p[4]) |
| 905 | |
| 906 | def p_option(self, p): |
Ole Troan | 68ebcd5 | 2020-08-10 17:06:44 +0200 | [diff] [blame] | 907 | '''option : OPTION ID '=' assignee ';' |
| 908 | | OPTION ID ';' ''' |
| 909 | if len(p) == 4: |
| 910 | p[0] = Option(p[2]) |
| 911 | else: |
| 912 | p[0] = Option(p[2], p[4]) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 913 | |
| 914 | def p_assignee(self, p): |
| 915 | '''assignee : NUM |
| 916 | | TRUE |
| 917 | | FALSE |
| 918 | | STRING_LITERAL ''' |
| 919 | p[0] = p[1] |
| 920 | |
| 921 | def p_type_specifier(self, p): |
| 922 | '''type_specifier : U8 |
| 923 | | U16 |
| 924 | | U32 |
| 925 | | U64 |
| 926 | | I8 |
| 927 | | I16 |
| 928 | | I32 |
| 929 | | I64 |
| 930 | | F64 |
| 931 | | BOOL |
| 932 | | STRING''' |
| 933 | p[0] = p[1] |
| 934 | |
| 935 | # Do a second pass later to verify that user defined types are defined |
| 936 | def p_typedef_specifier(self, p): |
| 937 | '''type_specifier : ID ''' |
| 938 | if p[1] not in global_types: |
| 939 | self._parse_error('Undefined type: {}'.format(p[1]), |
| 940 | self._token_coord(p, 1)) |
| 941 | p[0] = p[1] |
| 942 | |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 943 | def p_union(self, p): |
| 944 | '''union : UNION ID '{' block_statements_opt '}' ';' ''' |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 945 | p[0] = Union(p[2], [], p[4]) |
| 946 | |
| 947 | def p_union_flist(self, p): |
| 948 | '''union : flist UNION ID '{' block_statements_opt '}' ';' ''' |
| 949 | p[0] = Union(p[3], p[1], p[5]) |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 950 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 951 | # Error rule for syntax errors |
| 952 | def p_error(self, p): |
| 953 | if p: |
| 954 | self._parse_error( |
| 955 | 'before: %s' % p.value, |
| 956 | self._coord(lineno=p.lineno)) |
| 957 | else: |
| 958 | self._parse_error('At end of input', self.filename) |
| 959 | |
| 960 | |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 961 | class VPPAPI(): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 962 | |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 963 | def __init__(self, debug=False, filename='', logger=None, revision=None): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 964 | self.lexer = lex.lex(module=VPPAPILexer(filename), debug=debug) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 965 | self.parser = yacc.yacc(module=VPPAPIParser(filename, logger, |
| 966 | revision=revision), |
Ole Troan | d6743b1 | 2018-03-07 08:40:58 +0100 | [diff] [blame] | 967 | write_tables=False, debug=debug) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 968 | self.logger = logger |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 969 | self.revision = revision |
| 970 | self.filename = filename |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 971 | |
| 972 | def parse_string(self, code, debug=0, lineno=1): |
| 973 | self.lexer.lineno = lineno |
| 974 | return self.parser.parse(code, lexer=self.lexer, debug=debug) |
| 975 | |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 976 | def parse_fd(self, fd, debug=0): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 977 | data = fd.read() |
| 978 | return self.parse_string(data, debug=debug) |
| 979 | |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 980 | def parse_filename(self, filename, debug=0): |
| 981 | if self.revision: |
BenoƮt Ganne | 3f0ae66 | 2020-09-09 12:50:07 +0200 | [diff] [blame] | 982 | git_show = 'git show {}:{}'.format(self.revision, filename) |
Ole Troan | deecc93 | 2020-05-19 12:33:00 +0200 | [diff] [blame] | 983 | proc = Popen(git_show.split(), stdout=PIPE, encoding='utf-8') |
| 984 | try: |
| 985 | data, errs = proc.communicate() |
| 986 | if proc.returncode != 0: |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 987 | print('File not found: {}:{}' |
| 988 | .format(self.revision, filename), file=sys.stderr) |
Ole Troan | deecc93 | 2020-05-19 12:33:00 +0200 | [diff] [blame] | 989 | sys.exit(2) |
| 990 | return self.parse_string(data, debug=debug) |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 991 | except Exception: |
Ole Troan | deecc93 | 2020-05-19 12:33:00 +0200 | [diff] [blame] | 992 | sys.exit(3) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 993 | else: |
| 994 | try: |
| 995 | with open(filename, encoding='utf-8') as fd: |
| 996 | return self.parse_fd(fd, None) |
| 997 | except FileNotFoundError: |
BenoƮt Ganne | 3f0ae66 | 2020-09-09 12:50:07 +0200 | [diff] [blame] | 998 | print('File not found: {}'.format(filename), file=sys.stderr) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 999 | sys.exit(2) |
| 1000 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1001 | def process(self, objs): |
| 1002 | s = {} |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 1003 | s['Option'] = {} |
| 1004 | s['Define'] = [] |
| 1005 | s['Service'] = [] |
| 1006 | s['types'] = [] |
| 1007 | s['Import'] = [] |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 1008 | s['Counters'] = [] |
| 1009 | s['Paths'] = [] |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 1010 | crc = 0 |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1011 | for o in objs: |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 1012 | try: |
Mark Nelson | ea2abba | 2020-03-04 15:32:09 -0500 | [diff] [blame] | 1013 | crc = binascii.crc32(o.crc, crc) & 0xffffffff |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 1014 | except AttributeError: |
| 1015 | pass |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 1016 | |
| 1017 | if type(o) is list: |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1018 | for o2 in o: |
| 1019 | if isinstance(o2, Service): |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 1020 | o2.process(s) |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 1021 | else: |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 1022 | o.process(s) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1023 | |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 1024 | msgs = {d.name: d for d in s['Define']} |
| 1025 | svcs = {s.caller: s for s in s['Service']} |
| 1026 | replies = {s.reply: s for s in s['Service']} |
Marek Gradzki | 51e5968 | 2018-03-06 10:05:44 +0100 | [diff] [blame] | 1027 | seen_services = {} |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1028 | |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 1029 | s['file_crc'] = crc |
| 1030 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1031 | for service in svcs: |
| 1032 | if service not in msgs: |
Ole Troan | 17225df | 2018-04-11 09:50:03 +0200 | [diff] [blame] | 1033 | raise ValueError( |
| 1034 | 'Service definition refers to unknown message' |
| 1035 | ' definition: {}'.format(service)) |
| 1036 | if svcs[service].reply != 'null' and \ |
| 1037 | svcs[service].reply not in msgs: |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1038 | raise ValueError('Service definition refers to unknown message' |
| 1039 | ' definition in reply: {}' |
| 1040 | .format(svcs[service].reply)) |
Marek Gradzki | b533f3f | 2018-03-06 11:10:56 +0100 | [diff] [blame] | 1041 | if service in replies: |
| 1042 | raise ValueError('Service definition refers to message' |
| 1043 | ' marked as reply: {}'.format(service)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1044 | for event in svcs[service].events: |
| 1045 | if event not in msgs: |
| 1046 | raise ValueError('Service definition refers to unknown ' |
| 1047 | 'event: {} in message: {}' |
| 1048 | .format(event, service)) |
Marek Gradzki | 51e5968 | 2018-03-06 10:05:44 +0100 | [diff] [blame] | 1049 | seen_services[event] = True |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1050 | |
Marek Gradzki | 51e5968 | 2018-03-06 10:05:44 +0100 | [diff] [blame] | 1051 | # Create services implicitly |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1052 | for d in msgs: |
Marek Gradzki | 51e5968 | 2018-03-06 10:05:44 +0100 | [diff] [blame] | 1053 | if d in seen_services: |
| 1054 | continue |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1055 | if d.endswith('_reply'): |
| 1056 | if d[:-6] in svcs: |
| 1057 | continue |
| 1058 | if d[:-6] not in msgs: |
Marek Gradzki | cc13471 | 2018-03-06 12:25:02 +0100 | [diff] [blame] | 1059 | raise ValueError('{} missing calling message' |
| 1060 | .format(d)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1061 | continue |
| 1062 | if d.endswith('_dump'): |
| 1063 | if d in svcs: |
| 1064 | continue |
| 1065 | if d[:-5]+'_details' in msgs: |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 1066 | s['Service'].append(Service(d, d[:-5]+'_details', |
Ole Troan | 5891425 | 2018-10-23 10:50:07 +0200 | [diff] [blame] | 1067 | stream=True)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1068 | else: |
Marek Gradzki | cc13471 | 2018-03-06 12:25:02 +0100 | [diff] [blame] | 1069 | raise ValueError('{} missing details message' |
| 1070 | .format(d)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1071 | continue |
| 1072 | |
| 1073 | if d.endswith('_details'): |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 1074 | if d[:-8]+'_get' in msgs: |
| 1075 | if d[:-8]+'_get' in svcs: |
| 1076 | continue |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 1077 | raise ValueError('{} should be in a stream service' |
| 1078 | .format(d[:-8]+'_get')) |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 1079 | if d[:-8]+'_dump' in msgs: |
| 1080 | continue |
| 1081 | raise ValueError('{} missing dump or get message' |
| 1082 | .format(d)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1083 | |
| 1084 | if d in svcs: |
| 1085 | continue |
| 1086 | if d+'_reply' in msgs: |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 1087 | s['Service'].append(Service(d, d+'_reply')) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1088 | else: |
Ole Troan | 17225df | 2018-04-11 09:50:03 +0200 | [diff] [blame] | 1089 | raise ValueError( |
| 1090 | '{} missing reply message ({}) or service definition' |
| 1091 | .format(d, d+'_reply')) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1092 | |
| 1093 | return s |
| 1094 | |
Paul Vinciguerra | a51f9b3 | 2020-11-24 23:26:06 -0500 | [diff] [blame] | 1095 | def process_imports(self, objs, in_import, result): # -> List |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1096 | for o in objs: |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 1097 | # Only allow the following object types from imported file |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 1098 | if in_import and not isinstance(o, (Enum, Import, Typedef, |
| 1099 | Union, Using)): |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 1100 | continue |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 1101 | if isinstance(o, Import): |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 1102 | result.append(o) |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 1103 | result = self.process_imports(o.result, True, result) |
Ole Troan | 10a0989 | 2018-06-29 11:32:33 +0200 | [diff] [blame] | 1104 | else: |
| 1105 | result.append(o) |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 1106 | return result |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1107 | |
Ole Troan | 5891425 | 2018-10-23 10:50:07 +0200 | [diff] [blame] | 1108 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1109 | # Add message ids to each message. |
| 1110 | def add_msg_id(s): |
| 1111 | for o in s: |
| 1112 | o.block.insert(0, Field('u16', '_vl_msg_id')) |
| 1113 | return s |
| 1114 | |
| 1115 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1116 | dirlist = [] |
| 1117 | |
| 1118 | |
| 1119 | def dirlist_add(dirs): |
| 1120 | global dirlist |
| 1121 | if dirs: |
| 1122 | dirlist = dirlist + dirs |
| 1123 | |
| 1124 | |
| 1125 | def dirlist_get(): |
| 1126 | return dirlist |
| 1127 | |
Paul Vinciguerra | 2cd3cc8 | 2019-08-06 22:02:45 -0400 | [diff] [blame] | 1128 | |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 1129 | def foldup_blocks(block, crc): |
| 1130 | for b in block: |
| 1131 | # Look up CRC in user defined types |
| 1132 | if b.fieldtype.startswith('vl_api_'): |
| 1133 | # Recursively |
| 1134 | t = global_types[b.fieldtype] |
| 1135 | try: |
Ole Troan | 6006ca8 | 2020-08-31 13:54:47 +0200 | [diff] [blame] | 1136 | crc = binascii.crc32(t.crc, crc) & 0xffffffff |
Ole Troan | 9f84e70 | 2020-06-25 14:27:46 +0200 | [diff] [blame] | 1137 | crc = foldup_blocks(t.block, crc) |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 1138 | except AttributeError: |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 1139 | pass |
| 1140 | return crc |
| 1141 | |
Paul Vinciguerra | 2cd3cc8 | 2019-08-06 22:02:45 -0400 | [diff] [blame] | 1142 | |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 1143 | def foldup_crcs(s): |
| 1144 | for f in s: |
| 1145 | f.crc = foldup_blocks(f.block, |
Mark Nelson | ea2abba | 2020-03-04 15:32:09 -0500 | [diff] [blame] | 1146 | binascii.crc32(f.crc) & 0xffffffff) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1147 | |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 1148 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1149 | # |
| 1150 | # Main |
| 1151 | # |
| 1152 | def main(): |
Paul Vinciguerra | 2cd3cc8 | 2019-08-06 22:02:45 -0400 | [diff] [blame] | 1153 | if sys.version_info < (3, 5,): |
| 1154 | log.exception('vppapigen requires a supported version of python. ' |
| 1155 | 'Please use version 3.5 or greater. ' |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 1156 | 'Using %s', sys.version) |
Paul Vinciguerra | 2cd3cc8 | 2019-08-06 22:02:45 -0400 | [diff] [blame] | 1157 | return 1 |
| 1158 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1159 | cliparser = argparse.ArgumentParser(description='VPP API generator') |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 1160 | cliparser.add_argument('--pluginpath', default="") |
| 1161 | cliparser.add_argument('--includedir', action='append') |
| 1162 | cliparser.add_argument('--outputdir', action='store') |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 1163 | cliparser.add_argument('--input') |
Paul Vinciguerra | 2cd3cc8 | 2019-08-06 22:02:45 -0400 | [diff] [blame] | 1164 | cliparser.add_argument('--output', nargs='?', |
| 1165 | type=argparse.FileType('w', encoding='UTF-8'), |
| 1166 | default=sys.stdout) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1167 | |
| 1168 | cliparser.add_argument('output_module', nargs='?', default='C') |
| 1169 | cliparser.add_argument('--debug', action='store_true') |
| 1170 | cliparser.add_argument('--show-name', nargs=1) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 1171 | cliparser.add_argument('--git-revision', |
| 1172 | help="Git revision to use for opening files") |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1173 | args = cliparser.parse_args() |
| 1174 | |
| 1175 | dirlist_add(args.includedir) |
| 1176 | if not args.debug: |
| 1177 | sys.excepthook = exception_handler |
| 1178 | |
| 1179 | # Filename |
| 1180 | if args.show_name: |
| 1181 | filename = args.show_name[0] |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 1182 | elif args.input: |
| 1183 | filename = args.input |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1184 | else: |
| 1185 | filename = '' |
| 1186 | |
Marek Gradzki | 51e5968 | 2018-03-06 10:05:44 +0100 | [diff] [blame] | 1187 | if args.debug: |
| 1188 | logging.basicConfig(stream=sys.stdout, level=logging.WARNING) |
| 1189 | else: |
| 1190 | logging.basicConfig() |
Marek Gradzki | 51e5968 | 2018-03-06 10:05:44 +0100 | [diff] [blame] | 1191 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1192 | # |
| 1193 | # Generate representation |
| 1194 | # |
Paul Vinciguerra | f4647ed | 2019-02-12 12:21:01 -0800 | [diff] [blame] | 1195 | from importlib.machinery import SourceFileLoader |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1196 | |
| 1197 | # Default path |
Ole Troan | 3078737 | 2018-03-01 13:33:39 +0100 | [diff] [blame] | 1198 | pluginpath = '' |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1199 | if not args.pluginpath: |
Ole Troan | 3078737 | 2018-03-01 13:33:39 +0100 | [diff] [blame] | 1200 | cand = [] |
| 1201 | cand.append(os.path.dirname(os.path.realpath(__file__))) |
Ole Troan | 17225df | 2018-04-11 09:50:03 +0200 | [diff] [blame] | 1202 | cand.append(os.path.dirname(os.path.realpath(__file__)) + |
Ole Troan | 3078737 | 2018-03-01 13:33:39 +0100 | [diff] [blame] | 1203 | '/../share/vpp/') |
| 1204 | for c in cand: |
| 1205 | c += '/' |
Ole Troan | 5891425 | 2018-10-23 10:50:07 +0200 | [diff] [blame] | 1206 | if os.path.isfile('{}vppapigen_{}.py' |
| 1207 | .format(c, args.output_module.lower())): |
Ole Troan | 3078737 | 2018-03-01 13:33:39 +0100 | [diff] [blame] | 1208 | pluginpath = c |
| 1209 | break |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1210 | else: |
| 1211 | pluginpath = args.pluginpath + '/' |
Ole Troan | 3078737 | 2018-03-01 13:33:39 +0100 | [diff] [blame] | 1212 | if pluginpath == '': |
Paul Vinciguerra | 2cd3cc8 | 2019-08-06 22:02:45 -0400 | [diff] [blame] | 1213 | log.exception('Output plugin not found') |
| 1214 | return 1 |
Ole Troan | 5891425 | 2018-10-23 10:50:07 +0200 | [diff] [blame] | 1215 | module_path = '{}vppapigen_{}.py'.format(pluginpath, |
| 1216 | args.output_module.lower()) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1217 | |
| 1218 | try: |
Paul Vinciguerra | f4647ed | 2019-02-12 12:21:01 -0800 | [diff] [blame] | 1219 | plugin = SourceFileLoader(args.output_module, |
| 1220 | module_path).load_module() |
Ole Troan | 5891425 | 2018-10-23 10:50:07 +0200 | [diff] [blame] | 1221 | except Exception as err: |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 1222 | log.exception('Error importing output plugin: %s, %s', |
| 1223 | module_path, err) |
Paul Vinciguerra | 2cd3cc8 | 2019-08-06 22:02:45 -0400 | [diff] [blame] | 1224 | return 1 |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1225 | |
Paul Vinciguerra | 9046e44 | 2020-11-20 23:10:09 -0500 | [diff] [blame] | 1226 | parser = VPPAPI(debug=args.debug, filename=filename, logger=log, |
| 1227 | revision=args.git_revision) |
| 1228 | |
| 1229 | try: |
| 1230 | if not args.input: |
| 1231 | parsed_objects = parser.parse_fd(sys.stdin, log) |
| 1232 | else: |
| 1233 | parsed_objects = parser.parse_filename(args.input, log) |
| 1234 | except ParseError as e: |
| 1235 | print('Parse error: ', e, file=sys.stderr) |
| 1236 | sys.exit(1) |
| 1237 | |
| 1238 | # Build a list of objects. Hash of lists. |
| 1239 | result = [] |
| 1240 | |
| 1241 | # if the variable is not set in the plugin, assume it to be false. |
| 1242 | try: |
| 1243 | plugin.process_imports |
| 1244 | except AttributeError: |
| 1245 | plugin.process_imports = False |
| 1246 | |
| 1247 | if plugin.process_imports: |
| 1248 | result = parser.process_imports(parsed_objects, False, result) |
| 1249 | s = parser.process(result) |
| 1250 | else: |
| 1251 | s = parser.process(parsed_objects) |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 1252 | imports = parser.process_imports(parsed_objects, False, result) |
| 1253 | s['imported'] = parser.process(imports) |
Paul Vinciguerra | 9046e44 | 2020-11-20 23:10:09 -0500 | [diff] [blame] | 1254 | |
| 1255 | # Add msg_id field |
| 1256 | s['Define'] = add_msg_id(s['Define']) |
| 1257 | |
| 1258 | # Fold up CRCs |
| 1259 | foldup_crcs(s['Define']) |
| 1260 | |
| 1261 | # |
| 1262 | # Debug |
| 1263 | if args.debug: |
| 1264 | import pprint |
| 1265 | pp = pprint.PrettyPrinter(indent=4, stream=sys.stderr) |
| 1266 | for t in s['Define']: |
| 1267 | pp.pprint([t.name, t.flags, t.block]) |
| 1268 | for t in s['types']: |
| 1269 | pp.pprint([t.name, t.block]) |
| 1270 | |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 1271 | result = plugin.run(args, filename, s) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1272 | if result: |
Ole Troan | 17225df | 2018-04-11 09:50:03 +0200 | [diff] [blame] | 1273 | print(result, file=args.output) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1274 | else: |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 1275 | log.exception('Running plugin failed: %s %s', filename, result) |
Paul Vinciguerra | 2cd3cc8 | 2019-08-06 22:02:45 -0400 | [diff] [blame] | 1276 | return 1 |
| 1277 | return 0 |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1278 | |
| 1279 | |
| 1280 | if __name__ == '__main__': |
Paul Vinciguerra | 2cd3cc8 | 2019-08-06 22:02:45 -0400 | [diff] [blame] | 1281 | sys.exit(main()) |