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