Exemplo n.º 1
0
def evaluate_properties(lines):
    props = 'width', 'height', 'top', 'left', 'color', 'background-color', 'line-height', 'max-width', 'min-width', 'border-top-color'
    for f, n, name, level, behind, sel, _sel in lines:
        if not sel and name != 'pass':
            prop, expr = name
            if prop.startswith('margin-') or prop.startswith('padding-') or prop in props:
                expr = color_pattern(expr)
                expr = unit_pattern(expr)
                try:
                    expr = str(eval(expr, _globals, _locals))
                except SyntaxError as e:
                    raise SyntaxError(e.msg, (f, n, None, name))
                except (NameError, AttributeError, ValueError) as e:
                    raise SyntaxError(e, (f, n, None, name))
            elif prop in ('margin', 'padding', 'border', 'flex') or prop.startswith('border-'):
                expr = color_pattern(expr)
                expr = unit_pattern(expr)
                for i in range(4):
                    try:
                        expr = ' '.join([str(eval(v, _globals, _locals)) for v in expr.split(None, i)])
                    except (SyntaxError, NameError, ValueError) as e:
                        if i == 3:
                            raise SyntaxError(getattr(e, 'msg', e.message), (f, n, None, name))
                    else:
                        break
            name = prop, expr
        yield f, n, name, level, behind, sel, _sel
Exemplo n.º 2
0
def define_variables(lines, target):
    for f, n, line in lines:
        if '=' in line:
            variable, _, value = line.partition('=')
            variable, value = variable.rstrip(), value.lstrip()
            if not variable or not value:
                raise SyntaxError('variable or value not defined', (f, n, None, line))
            elif ',' in variable:
                raise SyntaxError('multiple assignments not allowed', (f, n, None, line))
            elif variable[:1].isspace():
                raise IndentationError('unresolved variable indent', (f, n, None, line))
            elif variable.replace('_', '').isalnum():
                if variable[0].isdigit():
                    raise SyntaxError('variable can\'t start with digit', (f, n, None, line))
                elif variable in _reserved:
                    raise SyntaxError('variable uses reserved word', (f, n, None, line))
                exec_line = variable + ' = ' + color_pattern(unit_pattern(value))
                try:
                    exec exec_line in _globals, _locals
                except (TypeError, SyntaxError, ValueError) as e:
                    raise SyntaxError(e.message + ': %s' % exec_line, (f, n, None, line))
                else:
                    target.send((f, n, line))
            else:
                yield f, n, line
        else:
            yield f, n, line