diff options
author | Lucas Neves <lcneves@gmail.com> | 2017-11-06 22:51:22 +0000 |
---|---|---|
committer | Lucas Neves <lcneves@gmail.com> | 2017-11-06 22:51:22 +0000 |
commit | 467261fe7df209b4b329176051f71e8a546f9829 (patch) | |
tree | 5cebe84239f4ca5aec76ba7b110b3fba2000a1c2 | |
parent | 99e5168ecb0f7203ed0d4d6ce2f08f092760029b (diff) | |
download | libcss-467261fe7df209b4b329176051f71e8a546f9829.tar.gz |
WIP: Select: autogen for selection properties.
-rw-r--r-- | src/select/__pycache__/assets.cpython-36.pyc | bin | 1913 -> 1911 bytes | |||
-rw-r--r-- | src/select/__pycache__/select_config.cpython-36.pyc | bin | 6543 -> 6514 bytes | |||
-rw-r--r-- | src/select/select_config.py | 11 | ||||
-rw-r--r-- | src/select/select_generator.py | 84 |
4 files changed, 68 insertions, 27 deletions
diff --git a/src/select/__pycache__/assets.cpython-36.pyc b/src/select/__pycache__/assets.cpython-36.pyc Binary files differindex b5bf9ca..f13ccad 100644 --- a/src/select/__pycache__/assets.cpython-36.pyc +++ b/src/select/__pycache__/assets.cpython-36.pyc diff --git a/src/select/__pycache__/select_config.cpython-36.pyc b/src/select/__pycache__/select_config.cpython-36.pyc Binary files differindex 3fdda2a..27faa8b 100644 --- a/src/select/__pycache__/select_config.cpython-36.pyc +++ b/src/select/__pycache__/select_config.cpython-36.pyc diff --git a/src/select/select_config.py b/src/select/select_config.py index 2f0899b..748dc43 100644 --- a/src/select/select_config.py +++ b/src/select/select_config.py @@ -6,7 +6,7 @@ # Configuration of CSS values values = { ('length', 'css_fixed', 4, '0', - 'unit', 'css_unit', 5, 'CSS_UNIT_PX'), + 'unit', 'css_unit', 5, ('0', 'CSS_UNIT_PX')), ('integer', 'int32_t', 4, '0'), ('fixed', 'css_fixed', 4, '0'), ('color', 'css_color', 4, '0'), @@ -111,14 +111,14 @@ page = { ('page_break_after', 3, None, None, 'CSS_PAGE_BREAK_AFTER_AUTO'), ('page_break_before', 3, None, None, 'CSS_PAGE_BREAK_BEFORE_AUTO'), ('page_break_inside', 2, None, None, 'CSS_PAGE_BREAK_INSIDE_AUTO'), - ('widows', 1, 'integer', None, ('CSS_WIDOWS_SET', '2 << CSS_RADIX_POINT')), - ('orphans', 1, 'integer', None, ('CSS_ORPHANS_SET', '2 << CSS_RADIX_POINT')) + ('widows', 1, ('integer', '2 << CSS_RADIX_POINT'), None, 'CSS_WIDOWS_SET'), + ('orphans', 1, ('integer', '2 << CSS_RADIX_POINT'), None, 'CSS_ORPHANS_SET') } uncommon = { # Uncommon group ('border_spacing', 1, ('length', 'length'), 'CSS_BORDER_SPACING_SET', - ('CSS_BORDER_SPACING_SET', '0', 'CSS_UNIT_PX', '0', 'CSS_UNIT_PX')), + 'CSS_BORDER_SPACING_SET'), ('break_after', 4, None, None, 'CSS_BREAK_AFTER_AUTO'), ('break_before', 4, None, None, 'CSS_BREAK_BEFORE_AUTO'), ('break_inside', 4, None, None, 'CSS_BREAK_INSIDE_AUTO'), @@ -128,8 +128,7 @@ uncommon = { ('column_fill', 2, None, None, 'CSS_COLUMN_FILL_BALANCE'), ('column_gap', 2, 'length', 'CSS_COLUMN_GAP_SET', 'CSS_COLUMN_GAP_NORMAL'), - ('column_rule_color', 2, 'color', None, - ('CSS_COLUMN_RULE_CURRENT_COLOR', '0')), + ('column_rule_color', 2, 'color', None, 'CSS_COLUMN_RULE_CURRENT_COLOR'), ('column_rule_style', 4, None, None, 'CSS_COLUMN_RULE_STYLE_NONE'), ('column_rule_width', 3, 'length', 'CSS_COLUMN_RULE_WIDTH_WIDTH', 'CSS_COLUMN_RULE_WIDTH_MEDIUM'), diff --git a/src/select/select_generator.py b/src/select/select_generator.py index d605f0b..b913efe 100644 --- a/src/select/select_generator.py +++ b/src/select/select_generator.py @@ -8,6 +8,18 @@ import string from select_config import values, groups from assets import assets +def get_tuple(self, from_var): + 'Accepts tuples, strings and None; returns a tuple.' + if type(from_var) is tuple: + return from_var + elif type(from_var) is str: + return (from_var,) + elif from_var is None: + return () + else: + raise TypeError('Value should be either tuple, string or None, ' + + 'received: ' + type(from_var).__name__) + def shift_star(value_type, prop_name): '''Shifts the asterisks from a pointer type to its name. i.e. `lwc_string** str_array` would become `lwc_string **str_array`''' @@ -103,13 +115,13 @@ class Text: class CSSValue: 'Values to be associated with properties.' - def __init__(self, name, css_type, size=None, default='NULL', + def __init__(self, name, css_type, size=None, defaults='NULL', bits_name=None, bits_type=None, bits_size=None, bits_default='0'): self.name = name self.type = css_type self.size = size # `None` means sizeof(ptr) - self.default = default + self.defaults = get_tuple(defaults) self.bits = None if bits_size is None else { 'name': bits_name, 'type': bits_type, @@ -122,27 +134,47 @@ class CSSProperty: defaults=None, comments=None, override=None): self.name = name self.type_size = type_size - self.values = [ x for v in self.get_tuple(values) for x in self.__vals - if x.name is v ] - self.defaults = self.get_tuple(defaults) + self.values = self.make_values(values) + self.defaults = get_tuple(defaults) self.condition = condition - self.override = self.get_tuple(override) + self.override = get_tuple(override) self.comments = comments self.__shift = None - __vals = [ CSSValue(*x) for x in values ] + def make_values(self, vals, for_list=False): + if type(vals) is None: + return [] + elif type(vals) is str: + val_list = [ CSSValue(*x) for x in values if x[0] == vals ] + return val_list[0] if for_list else val_list + elif type(vals) is tuple: + if len(vals) is 2 and type(vals[0]) is type(vals[1]) is str: + for v in values: + if v[0] == val[0]: + value = CSSValue(v) + value.defaults = get_tuple(vals[1]) + return value if for_list else [ value ] + else: + raise ValueError('Value ' + val[0] + ' not found!') + else: + return [ self.make_values(x, True) for x in vals ] + else: + raise TypeError('Expected None, str or tuple, got ' + + type(vals).__name__) - def get_tuple(self, from_var): - 'Accepts tuples, strings and None; returns a tuple.' - if type(from_var) is tuple: - return from_var - elif type(from_var) is str: - return (from_var,) - elif from_var is None: - return () + @property + def default_op(self): + if len(self.defaults) < 1: + raise ValueError('Property has empty defaults!') + return self.defaults[0] + + @property + def default_value(self, index): + # First default of property is the opcode + if len(self.defaults) < index + 2: + return self.values[index].defaults else: - raise TypeError('Value should be either tuple, string or None, ' + - 'received: ' + type(from_var).__name__) + return self.defaults[index + 1] @property def bits_size(self): @@ -341,23 +373,29 @@ class CSSGroup: for b in self.bits_array: or_ops = [] for p in b.contents: - or_ops.append('({} << {})'.format(p.defaults[0], - str(p.shift)) if p.shift else p.defaults[0]) + or_ops.append('({} << {})'.format(p.default_op, + str(p.shift)) + if p.shift else p.default_op) bits_ops.append(' | '.join(or_ops)) t.append(',\n'.join(bits_ops).split('\n')) t.indent(-1) t.append('},') + for p in self.props: + t.append(make_value_declaration(False, True)) + if self.name is not 'page': t.indent(-1) t.append('},') + for p in self.props: + t.append(make_value_declaration(True, True)) t.indent(-1) t.append('};') return t.to_string() - def make_value_declaration(self, for_commented): + def make_value_declaration(self, for_commented, defaults=False): r = [] for p in sorted(self.props, key=(lambda x: x.name)): if bool(p.comments) == for_commented: @@ -365,7 +403,11 @@ class CSSGroup: v_suffix = ('' if len(p.values) is 1 else '_' + string.ascii_lowercase[i]) v_type, v_name = shift_star(v.type, p.name) - r.append('{} {}{}{}'.format(v_type, v_name, v_suffix, ';')) + if defaults: + r.append('.{} = {},'.format(v_name, + p.default_value(i)[0])) + else: + r.append('{} {}{};'.format(v_type, v_name, v_suffix)) return r def make_text(self, filename): |