|
| 1 | +from dataclasses import dataclass, field |
| 2 | +from typing import List, ClassVar |
| 3 | + |
| 4 | +# taken from https://en.cppreference.com/w/cpp/keyword |
| 5 | +cpp_keywords = {"alignas", "alignof", "and", "and_eq", "asm", "atomic_cancel", "atomic_commit", "atomic_noexcept", |
| 6 | + "auto", "bitand", "bitor", "bool", "break", "case", "catch", "char", "char8_t", "char16_t", "char32_t", |
| 7 | + "class", "compl", "concept", "const", "consteval", "constexpr", "constinit", "const_cast", "continue", |
| 8 | + "co_await", "co_return", "co_yield", "decltype", "default", "delete", "do", "double", "dynamic_cast", |
| 9 | + "else", "enum", "explicit", "export", "extern", "false", "float", "for", "friend", "goto", "if", |
| 10 | + "inline", "int", "long", "mutable", "namespace", "new", "noexcept", "not", "not_eq", "nullptr", |
| 11 | + "operator", "or", "or_eq", "private", "protected", "public", "reflexpr", "register", "reinterpret_cast", |
| 12 | + "requires", "return", "short", "signed", "sizeof", "static", "static_assert", "static_cast", "struct", |
| 13 | + "switch", "synchronized", "template", "this", "thread_local", "throw", "true", "try", "typedef", |
| 14 | + "typeid", "typename", "union", "unsigned", "using", "virtual", "void", "volatile", "wchar_t", "while", |
| 15 | + "xor", "xor_eq"} |
| 16 | + |
| 17 | + |
| 18 | +@dataclass |
| 19 | +class Field: |
| 20 | + name: str |
| 21 | + type: str |
| 22 | + first: bool = False |
| 23 | + |
| 24 | + def cpp_name(self): |
| 25 | + if self.name in cpp_keywords: |
| 26 | + return self.name + "_" |
| 27 | + return self.name |
| 28 | + |
| 29 | + def stream(self): |
| 30 | + if self.type == "std::string": |
| 31 | + return lambda x: f"trapQuoted({x})" |
| 32 | + elif self.type == "bool": |
| 33 | + return lambda x: f'({x} ? "true" : "false")' |
| 34 | + else: |
| 35 | + return lambda x: x |
| 36 | + |
| 37 | + |
| 38 | +@dataclass |
| 39 | +class Trap: |
| 40 | + table_name: str |
| 41 | + name: str |
| 42 | + fields: List[Field] |
| 43 | + id: Field = None |
| 44 | + |
| 45 | + def __post_init__(self): |
| 46 | + assert self.fields |
| 47 | + self.fields[0].first = True |
| 48 | + |
| 49 | + |
| 50 | +@dataclass |
| 51 | +class TagBase: |
| 52 | + base: str |
| 53 | + first: bool = False |
| 54 | + |
| 55 | + |
| 56 | +@dataclass |
| 57 | +class Tag: |
| 58 | + name: str |
| 59 | + bases: List[TagBase] |
| 60 | + index: int |
| 61 | + id: str |
| 62 | + |
| 63 | + def __post_init__(self): |
| 64 | + if self.bases: |
| 65 | + self.bases = [TagBase(b) for b in self.bases] |
| 66 | + self.bases[0].first = True |
| 67 | + |
| 68 | + def has_bases(self): |
| 69 | + return bool(self.bases) |
| 70 | + |
| 71 | + |
| 72 | +@dataclass |
| 73 | +class TrapList: |
| 74 | + template: ClassVar = 'cpp_traps' |
| 75 | + |
| 76 | + traps: List[Trap] = field(default_factory=list) |
| 77 | + |
| 78 | + |
| 79 | +@dataclass |
| 80 | +class TagList: |
| 81 | + template: ClassVar = 'cpp_tags' |
| 82 | + |
| 83 | + tags: List[Tag] = field(default_factory=list) |
0 commit comments