|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +import re |
| 6 | + |
| 7 | + |
| 8 | +def to_rust_ident(name): |
| 9 | + name = name.replace("-", "_") |
| 10 | + if name in ["static", "super", "box", "move"]: |
| 11 | + name += "_" |
| 12 | + return name |
| 13 | + |
| 14 | + |
| 15 | +def to_camel_case(ident): |
| 16 | + return re.sub("_([a-z])", lambda m: m.group(1).upper(), ident.strip("_").capitalize()) |
| 17 | + |
| 18 | + |
| 19 | +class Keyword(object): |
| 20 | + def __init__(self, name, values, gecko_constant_prefix=None, |
| 21 | + extra_gecko_values=None, extra_servo_values=None): |
| 22 | + self.name = name |
| 23 | + self.values = values |
| 24 | + self.gecko_constant_prefix = gecko_constant_prefix or \ |
| 25 | + "NS_STYLE_" + self.name.upper().replace("-", "_") |
| 26 | + self.extra_gecko_values = (extra_gecko_values or "").split() |
| 27 | + self.extra_servo_values = (extra_servo_values or "").split() |
| 28 | + |
| 29 | + def gecko_values(self): |
| 30 | + return self.values + self.extra_gecko_values |
| 31 | + |
| 32 | + def servo_values(self): |
| 33 | + return self.values + self.extra_servo_values |
| 34 | + |
| 35 | + def values_for(self, product): |
| 36 | + if product == "gecko": |
| 37 | + return self.gecko_values() |
| 38 | + elif product == "servo": |
| 39 | + return self.servo_values() |
| 40 | + else: |
| 41 | + raise Exception("Bad product: " + product) |
| 42 | + |
| 43 | + def gecko_constant(self, value): |
| 44 | + return self.gecko_constant_prefix + "_" + value.upper().replace("-", "_") |
| 45 | + |
| 46 | + |
| 47 | +class Longhand(object): |
| 48 | + def __init__(self, style_struct, name, derived_from=None, keyword=None, |
| 49 | + custom_cascade=False, experimental=False, internal=False, |
| 50 | + gecko_ffi_name=None): |
| 51 | + self.name = name |
| 52 | + self.keyword = keyword |
| 53 | + self.ident = to_rust_ident(name) |
| 54 | + self.camel_case = to_camel_case(self.ident) |
| 55 | + self.style_struct = style_struct |
| 56 | + self.experimental = ("layout.%s.enabled" % name) if experimental else None |
| 57 | + self.custom_cascade = custom_cascade |
| 58 | + self.internal = internal |
| 59 | + self.gecko_ffi_name = gecko_ffi_name or "m" + self.camel_case |
| 60 | + self.derived_from = (derived_from or "").split() |
| 61 | + |
| 62 | + |
| 63 | +class Shorthand(object): |
| 64 | + def __init__(self, name, sub_properties, experimental=False, internal=False): |
| 65 | + self.name = name |
| 66 | + self.ident = to_rust_ident(name) |
| 67 | + self.camel_case = to_camel_case(self.ident) |
| 68 | + self.derived_from = None |
| 69 | + self.experimental = ("layout.%s.enabled" % name) if experimental else None |
| 70 | + self.sub_properties = sub_properties |
| 71 | + self.internal = internal |
| 72 | + |
| 73 | + |
| 74 | +class Method(object): |
| 75 | + def __init__(self, name, return_type=None, arg_types=None, is_mut=False): |
| 76 | + self.name = name |
| 77 | + self.return_type = return_type |
| 78 | + self.arg_types = arg_types or [] |
| 79 | + self.is_mut = is_mut |
| 80 | + |
| 81 | + def arg_list(self): |
| 82 | + args = ["_: " + x for x in self.arg_types] |
| 83 | + args = ["&mut self" if self.is_mut else "&self"] + args |
| 84 | + return ", ".join(args) |
| 85 | + |
| 86 | + def signature(self): |
| 87 | + sig = "fn %s(%s)" % (self.name, self.arg_list()) |
| 88 | + if self.return_type: |
| 89 | + sig = sig + " -> " + self.return_type |
| 90 | + return sig |
| 91 | + |
| 92 | + def declare(self): |
| 93 | + return self.signature() + ";" |
| 94 | + |
| 95 | + def stub(self): |
| 96 | + return self.signature() + "{ unimplemented!() }" |
| 97 | + |
| 98 | + |
| 99 | +class StyleStruct(object): |
| 100 | + def __init__(self, name, inherited, gecko_ffi_name=None, additional_methods=None): |
| 101 | + self.servo_struct_name = "Servo" + name |
| 102 | + self.gecko_struct_name = "Gecko" + name |
| 103 | + self.trait_name = name |
| 104 | + self.trait_name_lower = name.lower() |
| 105 | + self.ident = to_rust_ident(self.trait_name_lower) |
| 106 | + self.longhands = [] |
| 107 | + self.inherited = inherited |
| 108 | + self.gecko_ffi_name = gecko_ffi_name |
| 109 | + self.additional_methods = additional_methods or [] |
| 110 | + |
| 111 | + |
| 112 | +class PropertiesData(object): |
| 113 | + def __init__(self, product): |
| 114 | + self.product = product |
| 115 | + self.style_structs = [] |
| 116 | + self.current_style_struct = None |
| 117 | + self.longhands = [] |
| 118 | + self.longhands_by_name = {} |
| 119 | + self.derived_longhands = {} |
| 120 | + self.shorthands = [] |
| 121 | + |
| 122 | + def new_style_struct(self, *args, **kwargs): |
| 123 | + style_struct = StyleStruct(*args, **kwargs) |
| 124 | + self.style_structs.append(style_struct) |
| 125 | + self.current_style_struct = style_struct |
| 126 | + |
| 127 | + def active_style_structs(self): |
| 128 | + return [s for s in self.style_structs if s.additional_methods or s.longhands] |
| 129 | + |
| 130 | + def switch_to_style_struct(self, name): |
| 131 | + for style_struct in self.style_structs: |
| 132 | + if style_struct.trait_name == name: |
| 133 | + self.current_style_struct = style_struct |
| 134 | + return |
| 135 | + raise Exception("Failed to find the struct named " + name) |
| 136 | + |
| 137 | + def declare_longhand(self, name, products="gecko servo", **kwargs): |
| 138 | + products = products.split() |
| 139 | + if self.product not in products: |
| 140 | + return |
| 141 | + |
| 142 | + longand = Longhand(self.current_style_struct, name, **kwargs) |
| 143 | + self.current_style_struct.longhands.append(longand) |
| 144 | + self.longhands.append(longand) |
| 145 | + self.longhands_by_name[name] = longand |
| 146 | + |
| 147 | + for name in longand.derived_from: |
| 148 | + self.derived_longhands.setdefault(name, []).append(longand) |
| 149 | + |
| 150 | + return longand |
| 151 | + |
| 152 | + def declare_shorthand(self, name, sub_properties, *args, **kwargs): |
| 153 | + sub_properties = [self.longhands_by_name[s] for s in sub_properties] |
| 154 | + shorthand = Shorthand(name, sub_properties, *args, **kwargs) |
| 155 | + self.shorthands.append(shorthand) |
| 156 | + return shorthand |
0 commit comments