Fixed mixin argument parsing

This commit is contained in:
jtm 2012-04-06 19:23:22 +00:00
parent e2f925fad8
commit 5f2ab7d9b1
7 changed files with 59 additions and 33 deletions

View File

@ -248,8 +248,8 @@ class LessParser(object):
p[0] = None
def p_open_mixin(self, p):
""" open_mixin : identifier t_popen mixin_args t_pclose brace_open
| identifier t_popen mixin_args t_pclose mixin_guard brace_open
""" open_mixin : identifier t_popen mixin_args_list t_pclose brace_open
| identifier t_popen mixin_args_list t_pclose mixin_guard brace_open
"""
p[1].parse(self.scope)
p[0] = [p[1], p[3]]
@ -301,7 +301,7 @@ class LessParser(object):
p[0] = ''.join(list(p)[1:])
def p_call_mixin(self, p):
""" call_mixin : identifier t_popen mixin_args t_pclose ';'
""" call_mixin : identifier t_popen mixin_args_list t_pclose ';'
"""
p[1].parse(None)
mixin = self.scope.mixins(p[1].raw())
@ -329,15 +329,23 @@ class LessParser(object):
p[0] = res
def p_mixin_args_arguments(self, p):
""" mixin_args : less_arguments
""" mixin_args_list : less_arguments
"""
p[0] = [p[1]]
def p_mixin_args_list_aux(self, p):
""" mixin_args_list : mixin_args_list ',' mixin_args
"""
p[1].extend([p[3]])
p[0] = p[1]
def p_mixin_args_list(self, p):
""" mixin_args_list : mixin_args
"""
p[0] = [p[1]]
def p_mixin_args_aux(self, p):
""" mixin_args : mixin_args ',' argument
| mixin_args ',' mixin_kwarg
| mixin_args argument
| mixin_args mixin_kwarg
""" mixin_args : mixin_args argument
"""
p[1].extend(list(p)[2:])
p[0] = p[1]
@ -359,13 +367,13 @@ class LessParser(object):
p[0] = Variable(list(p)[1:], p.lineno(2))
def p_margument_list_aux(self, p):
""" mixin_kwarg_arg_list : mixin_kwarg_arg_list argument
""" mixin_kwarg_arg_list : mixin_kwarg_arg_list argument
"""
p[1].extend(list(p)[2:])
p[0] = p[1]
def p_margument_list(self, p):
""" mixin_kwarg_arg_list : argument
""" mixin_kwarg_arg_list : argument
"""
p[0] = [p[1]]

View File

@ -33,6 +33,7 @@ class Call(Node):
if not self.parsed:
name = ''.join(self.tokens[0])
parsed = self.process(self.tokens[1:], scope)
if name == '%(':
name = 'sformat'
elif name in ('~', 'e'):
@ -45,6 +46,7 @@ class Call(Node):
return getattr(self, name)(*args)
except ValueError:
pass
if hasattr(color, name):
try:
return getattr(color, name)(*args)

View File

@ -29,19 +29,19 @@ class Deferred(Node):
returns:
mixed
"""
if self.args:
args = [p.parse(scope)
if hasattr(p, 'parse')
else p
for p in self.args]
else:
args = []
# if self.args:
# args = [p.parse(scope)
# if hasattr(p, 'parse')
# else p
# for p in self.args]
# else:
# args = []
if hasattr(self.mixin, 'call'):
return self.mixin.call(scope, args)
return self.mixin.call(scope, self.args)
mixins = scope.mixins(self.mixin.raw())
if not mixins: return self
for mixin in mixins:
res = mixin.call(scope, args)
res = mixin.call(scope, self.args)
if res: return res
return False

View File

@ -28,7 +28,7 @@ class Mixin(Node):
self
"""
self.name, args, self.guards = self.tokens[0]
self.args = [a for a in args if a != ','] if args else []
self.args = list(utility.flatten(args)) if args else []
self.body = Block([None, self.tokens[1]], 0)
self.scope = copy.deepcopy(scope[-1])
return self
@ -50,21 +50,20 @@ class Mixin(Node):
raises:
SyntaxError
"""
arguments = args if args else None
arguments = zip(args, [' '] * len(args)) if args and args[0] else None
if self.args:
parsed = [v.parse(scope)
if hasattr(v, 'parse') else v
for v in copy.deepcopy(self.args)]
args = args if type(args) is list else [args]
vars = [self._parse_arg(var, arg, scope)
for arg, var in itertools.zip_longest([a for a in args if a != ','], parsed)]
for arg, var in itertools.zip_longest([a for a in args],
parsed)]
for var in vars:
if var: scope.add_variable(var)
if not arguments:
arguments = [v.value for v in vars if v]
if arguments:
arguments = [' ' if a == ',' else a for a in arguments]
else:
if not arguments:
arguments = ''
scope.add_variable(Variable(['@arguments',
None,
@ -76,8 +75,8 @@ class Mixin(Node):
if type(var) is Variable:
# kwarg
if arg:
if utility.is_variable(arg):
tmp = scope.variables(arg)
if utility.is_variable(arg[0]):
tmp = scope.variables(arg[0])
if not tmp: return None
var.value = tmp.value
else:
@ -87,8 +86,8 @@ class Mixin(Node):
if utility.is_variable(var):
if arg is None:
raise SyntaxError('Missing argument to mixin')
elif utility.is_variable(arg):
tmp = scope.variables(arg)
elif utility.is_variable(arg[0]):
tmp = scope.variables(arg[0])
if not tmp: return None
var = Variable([var, None, tmp.value]).parse(scope)
else:
@ -133,9 +132,11 @@ class Mixin(Node):
ret = False
variables = copy.deepcopy(scope[-1]['__variables__'])
if args:
args = [arg.parse(scope)
if type(arg) is Expression
else arg for arg in args]
args = [[a.parse(scope)
if type(a) is Expression
else a for a in arg]
if arg else arg
for arg in args]
try:
self.parse_args(args, scope)
except SyntaxError:

View File

@ -67,3 +67,8 @@
-moz-box-shadow: 0 1px 3px rgba(0,0,0,.25);
box-shadow: 0 1px 3px rgba(0,0,0,.25);
}
.a {
-webkit-border-radius: 0 3px 3px 0;
-moz-border-radius: 0 3px 3px 0;
border-radius: 0 3px 3px 0;
}

View File

@ -17,3 +17,4 @@
.div :-moz-placeholder{color:#ffffff;}
.div::-webkit-input-placeholder{color:#ffffff;}
.arglist{-webkit-box-shadow:0 1px 3px rgba(0,0,0,.25);-moz-box-shadow:0 1px 3px rgba(0,0,0,.25);box-shadow:0 1px 3px rgba(0,0,0,.25);}
.a{-webkit-border-radius:0 3px 3px 0;-moz-border-radius:0 3px 3px 0;border-radius:0 3px 3px 0;}

View File

@ -130,3 +130,12 @@
.arglist {
.box-shadow();
}
// Reversed
.border-radius(@radius: 5px) {
-webkit-border-radius: @radius;
-moz-border-radius: @radius;
border-radius: @radius;
}
.a {
.border-radius(0 3px 3px 0);
}