Skip to content

Commit

Permalink
Merge pull request #68 from danc86/unary-negation
Browse files Browse the repository at this point in the history
Handle unary negation with a separate node type
  • Loading branch information
saschpe committed May 5, 2015
2 parents 8088fe5 + 8e0d494 commit aea430f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 24 deletions.
4 changes: 2 additions & 2 deletions lesscpy/lessc/parser.py
Expand Up @@ -25,7 +25,7 @@
from .scope import Scope
from .color import Color
from lesscpy.exceptions import CompilationError
from lesscpy.plib import Block, Call, Deferred, Expression, Identifier, Mixin, Property, Statement, Variable, Import
from lesscpy.plib import Block, Call, Deferred, Expression, NegatedExpression, Identifier, Mixin, Property, Statement, Variable, Import

class ErrorRegister(object):
"""
Expand Down Expand Up @@ -785,7 +785,7 @@ def p_expression_aux(self, p):
def p_expression_p_neg(self, p):
""" expression : '-' t_popen expression t_pclose
"""
p[0] = [p[1], p[3]]
p[0] = NegatedExpression([p[3]], 0)

def p_expression_p(self, p):
""" expression : t_popen expression t_pclose
Expand Down
2 changes: 2 additions & 0 deletions lesscpy/plib/__init__.py
Expand Up @@ -14,6 +14,7 @@
'Expression',
'Identifier',
'Mixin',
'NegatedExpression',
'Node',
'Property',
'Statement',
Expand All @@ -26,6 +27,7 @@
from .expression import Expression
from .identifier import Identifier
from .mixin import Mixin
from .negated_expression import NegatedExpression
from .node import Node
from .property import Property
from .statement import Statement
Expand Down
24 changes: 2 additions & 22 deletions lesscpy/plib/expression.py
Expand Up @@ -9,7 +9,6 @@
"""

import operator
import six

from .node import Node
from lesscpy.lessc import utility
Expand All @@ -19,7 +18,8 @@
class Expression(Node):

"""Expression node. Parses all expression except
color expressions, (handled in the color class)
color expressions (handled in the color class)
and unary negation (handled in the NegatedExpression class).
"""

def parse(self, scope):
Expand All @@ -33,7 +33,6 @@ def parse(self, scope):
"""
assert(len(self.tokens) == 3)
expr = self.process(self.tokens, scope)
expr = [self.neg(t, scope) for t in expr]
A, O, B = [e[0]
if isinstance(e, tuple)
else e
Expand All @@ -56,25 +55,6 @@ def parse(self, scope):
return out
return self.with_units(out, ua, ub)

def neg(self, value, scope):
"""Apply negativity.
args:
value (mixed): test value
scope (Scope): Current scope
raises:
SyntaxError
returns:
str
"""
if value and isinstance(value, list) and value[0] == '-':
val = value[1]
if len(value) > 1 and hasattr(value[1], 'parse'):
val = value[1].parse(scope)
if isinstance(val, six.string_types):
return '-' + val
return -val
return value

def with_units(self, val, ua, ub):
"""Return value with unit.
args:
Expand Down
23 changes: 23 additions & 0 deletions lesscpy/plib/negated_expression.py
@@ -0,0 +1,23 @@
# -*- coding: utf8 -*-
"""
.. module:: lesscpy.plib.negated_expression
:synopsis: Node for unary negated expressions.
Copyright (c)
See LICENSE for details.
"""

import six

from .node import Node


class NegatedExpression(Node):

"""Expressions preceded by unary negation."""

def parse(self, scope):
val, = self.process(self.tokens, scope)
if isinstance(val, six.string_types):
return '-' + val
return -val
3 changes: 3 additions & 0 deletions test/css/issues/issue67.css
@@ -0,0 +1,3 @@
.c {
left: 4;
}
1 change: 1 addition & 0 deletions test/less/issues/issue67.less
@@ -0,0 +1 @@
.c { left: -(1 + 1) * -(1 + 1); }

0 comments on commit aea430f

Please sign in to comment.