Skip to content

Commit

Permalink
Translated modifier statement.
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Aug 2, 2010
1 parent 10a6922 commit 174668a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 13 deletions.
7 changes: 6 additions & 1 deletion examples/sample.tpl
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
{$entry.Name[3].apple|escape:'foo':$bar|title:9}
{if $a eq 5 and $foo[3].bar[2] eq 2}
<html>
</html>
{$entry.Name[3].apple|escape:'foo':$bar|title:9:"banana"}
<br /><br />
{/if}
8 changes: 4 additions & 4 deletions smartytotwig/smarty_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ def dollar(): return '$'

def symbol(): return 0, dollar, re.compile(r'\w+')

def array(): return symbol, array_dereference

def array_dereference(): return "[", 0, expression, "]"
def array(): return symbol, "[", 0, expression, "]"

def expression(): return [object_dereference, array, symbol, string]

Expand All @@ -66,7 +64,9 @@ def if_statement(): return '{', keyword('if'), expression, -1, (operator

def elseif_statement(): return '{', keyword('elseif'), expression, -1, (operator, expression), '}', -1, smarty_language, 0, ('{/', keyword('if'), '}')

def modifier_statement(): return '{', expression, -2, ('|', symbol, -1, (':', expression), ), '}'
def modifier_right(): return ('|', symbol, -1, (':', expression), )

def modifier_statement(): return '{', expression, -2, modifier_right, '}'

"""
Finally, the actual language description.
Expand Down
97 changes: 89 additions & 8 deletions smartytotwig/tree_walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ def smarty_language(self, ast, code, tab_depth):
"""
"""

print ast

code = self.__walk_tree (
{
'if_statement': self.if_statement,
'content': self.content
'content': self.content,
'modifier_statement': self.modifier_statement
},
ast,
code,
Expand All @@ -56,13 +55,69 @@ def smarty_language(self, ast, code, tab_depth):

return code

def modifier_statement(self, ast, code, tab_depth):
"""
"""
code = "%s%s{{" % (
code,
self.__print_tabs(tab_depth)
)

# Walking the expression that starts a
# modifier statement.
code = self.__walk_tree (
{
'expression': self.expression,
'modifier_right': self.modifier_right
},
ast,
code,
tab_depth
)

code = "%s}}\n" % code
return code

def modifier_right(self, ast, code, tab_depth):
"""
"""
code = "%s|" % code

code = self.__walk_tree (
{
'symbol': self.symbol,
'string': self.string
},
ast,
code,
tab_depth
)

# We must have parameters being passed
# in to the modifier.
if len(ast) > 1:
code = "%s(" % code
i = 0
for k, v in ast[1:]:
code = self.expression(v, code, tab_depth)

# Put commas in if needed.
i += 1
if not i == len(ast) - 1:
code = "%s, " % code

code = "%s)" % code

return code

def content(self, ast, code, tab_depth):
"""
"""
code = "%s%s" % (
code,
ast
)

return code

def if_statement(self, ast, code, tab_depth):
Expand Down Expand Up @@ -163,9 +218,7 @@ def elseif_statement(self, ast, code, tab_depth):
def else_statement(self, ast, code, tab_depth):
"""
"""
"""
"""

code = "%s%s{%s else %s}\n" % (
code,
self.__print_tabs(tab_depth),
Expand Down Expand Up @@ -228,7 +281,9 @@ def expression(self, ast, code, tab_depth):
code = self.__walk_tree (
{
'symbol': self.symbol,
'object_dereference': self.object_dereference
'string': self.string,
'object_dereference': self.object_dereference,
'array': self.array,
},
ast,
code,
Expand All @@ -242,7 +297,9 @@ def object_dereference(self, ast, code, tab_depth):
"""
handlers = {
'expression': self.expression,
'symbol': self.symbol
'symbol': self.symbol,
'string': self.string,
'array': self.array
}

code = handlers[ast[0][0]](ast[0][1], code, tab_depth)
Expand All @@ -254,6 +311,30 @@ def object_dereference(self, ast, code, tab_depth):

return code

def array(self, ast, code, tab_depth):
"""
"""
handlers = {
'expression': self.expression,
'array': self.array,
'symbol': self.symbol,
'string': self.string
}

code = handlers[ast[0][0]](ast[0][1], code, tab_depth)

code = "%s[%s]" % (
code,
handlers[ast[1][0]](ast[1][1], "", tab_depth)
)

return code

def string(self, ast, code, tab_depth):
"""
"""
return "%s%s" % (code, ast[0])

def symbol(self, ast, code, tab_depth):
"""
Expand Down

0 comments on commit 174668a

Please sign in to comment.