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