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