120 lines
3.9 KiB
Python
Raw Permalink Normal View History

2012-03-30 19:32:11 +00:00
# -*- coding: utf8 -*-
2012-03-18 14:21:58 +00:00
"""
2012-03-30 19:32:11 +00:00
.. module:: lesscpy.plib.deferred
:synopsis: Deferred mixin call.
2013-07-19 11:21:51 +02:00
2012-03-30 19:32:11 +00:00
Copyright (c)
See LICENSE for details.
2012-07-18 17:28:04 -04:00
.. moduleauthor:: Johann T. Mariusson <jtm@robot.is>
2012-03-18 14:21:58 +00:00
"""
from .node import Node
2013-07-19 11:21:51 +02:00
2012-03-18 14:21:58 +00:00
class Deferred(Node):
2013-07-19 11:21:51 +02:00
2012-06-04 07:27:53 +00:00
def __init__(self, mixin, args, lineno=0):
2012-04-09 11:57:09 +00:00
"""This node represents mixin calls. The calls
2013-07-19 11:21:51 +02:00
to these mixins are deferred until the second
parse cycle. lessc.js allows calls to mixins not
2012-05-07 13:10:28 +00:00
yet defined or known.
2012-03-30 19:32:11 +00:00
args:
mixin (Mixin): Mixin object
args (list): Call arguments
2012-03-18 14:21:58 +00:00
"""
2012-04-08 13:17:22 +00:00
self.tokens = [mixin, args]
2012-04-07 11:33:07 +00:00
self.lineno = lineno
2013-07-19 11:21:51 +02:00
def parse(self, scope, error=False, depth=0):
2012-04-09 11:57:09 +00:00
""" Parse function. We search for mixins
first within current scope then fallback
to global scope. The special scope.deferred
2013-07-19 11:21:51 +02:00
is used when local scope mixins are called
within parent mixins.
2012-05-07 13:10:28 +00:00
If nothing is found we fallback to block-mixin
as lessc.js allows calls to blocks and mixins to
be inter-changable.
2013-07-19 11:21:51 +02:00
clx: This method is a HACK that stems from
poor design elsewhere. I will fix it
2012-05-07 13:10:28 +00:00
when I have more time.
2012-03-30 19:32:11 +00:00
args:
2012-04-03 13:36:08 +00:00
scope (Scope): Current scope
2012-03-30 19:32:11 +00:00
returns:
mixed
2012-03-18 14:21:58 +00:00
"""
2012-04-08 15:44:56 +00:00
res = False
2012-04-09 09:52:46 +00:00
ident, args = self.tokens
ident.parse(scope)
mixins = scope.mixins(ident.raw())
2013-07-19 11:21:51 +02:00
2012-04-09 09:52:46 +00:00
if not mixins:
ident.parse(None)
mixins = scope.mixins(ident.raw())
2013-07-19 11:21:51 +02:00
if depth > 64:
raise SyntaxError('NameError `%s`' % ident.raw(True))
2012-04-09 09:52:46 +00:00
if not mixins:
2012-04-23 19:55:23 +00:00
if scope.deferred:
store = [t for t in scope.deferred.parsed[-1]]
i = 0
2012-04-23 19:55:23 +00:00
while scope.deferred.parsed[-1]:
scope.current = scope.deferred
ident.parse(scope)
mixins = scope.mixins(ident.raw())
scope.current = None
if mixins or i > 64:
2012-04-23 19:55:23 +00:00
break
scope.deferred.parsed[-1].pop()
i += 1
2012-04-23 19:55:23 +00:00
scope.deferred.parsed[-1] = store
2013-07-19 11:21:51 +02:00
2012-05-07 09:49:31 +00:00
if not mixins:
# Fallback to blocks
block = scope.blocks(ident.raw())
if not block:
ident.parse(None)
block = scope.blocks(ident.raw())
2012-05-07 09:49:31 +00:00
if block:
2012-06-04 07:27:53 +00:00
scope.current = scope.real[-1] if scope.real else None
2012-05-07 09:49:31 +00:00
res = block.copy_inner(scope)
scope.current = None
2013-07-19 11:21:51 +02:00
2012-04-07 11:33:07 +00:00
if mixins:
for mixin in mixins:
scope.current = scope.real[-1] if scope.real else None
2012-04-08 13:17:22 +00:00
res = mixin.call(scope, args)
2013-07-19 11:21:51 +02:00
if res:
2012-05-07 13:10:28 +00:00
# Add variables to scope to support
# closures
2012-05-07 09:49:31 +00:00
[scope.add_variable(v) for v in mixin.vars]
scope.deferred = ident
break
2013-07-19 11:21:51 +02:00
2012-05-07 09:49:31 +00:00
if res:
2013-07-19 11:21:51 +02:00
store = [t for t in scope.deferred.parsed[
-1]] if scope.deferred else False
tmp_res = []
for p in res:
if p:
if isinstance(p, Deferred):
tmp_res.append(p.parse(scope, depth=depth + 1))
else:
tmp_res.append(p.parse(scope))
res = tmp_res
#res = [p.parse(scope, depth=depth+1) for p in res if p]
2013-07-19 11:21:51 +02:00
while(any(t for t in res if isinstance(t, Deferred))):
2012-04-09 09:52:46 +00:00
res = [p.parse(scope) for p in res if p]
2013-07-19 11:21:51 +02:00
if store:
scope.deferred.parsed[-1] = store
2012-04-08 15:44:56 +00:00
if error and not res:
2012-06-25 09:37:26 +00:00
raise SyntaxError('NameError `%s`' % ident.raw(True))
2012-04-07 14:38:52 +00:00
return res
2013-07-19 11:21:51 +02:00
def copy(self):
2012-05-07 13:10:28 +00:00
""" Returns self (used when Block objects are copy'd)
2012-05-05 17:20:03 +00:00
returns:
self
"""
return self