This commit is contained in:
Waylon Walker 2022-03-31 20:20:07 -05:00
commit 38355d2442
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
9083 changed files with 1225834 additions and 0 deletions

View file

@ -0,0 +1,43 @@
from __future__ import unicode_literals
class Renderer(object):
def render(self, ast):
"""Walks the AST and calls member methods for each Node type.
@param ast {Node} The root of the abstract syntax tree.
"""
walker = ast.walker()
self.buf = ''
self.last_out = '\n'
event = walker.nxt()
while event is not None:
type_ = event['node'].t
if hasattr(self, type_):
getattr(self, type_)(event['node'], event['entering'])
event = walker.nxt()
return self.buf
def lit(self, s):
"""Concatenate a literal string to the buffer.
@param str {String} The string to concatenate.
"""
self.buf += s
self.last_out = s
def cr(self):
if self.last_out != '\n':
self.lit('\n')
def out(self, s):
"""Concatenate a string to the buffer possibly escaping the content.
Concrete renderer implementations should override this method.
@param str {String} The string to concatenate.
"""
self.lit(s)