Example #1
0
class Builder(object):
    def __init__(self, debug=False, optimize=False):
        self.debug, self.optimize = debug, optimize
        self.parser = parser(debug=debug, optimize=optimize)

    def parse(self, input):
        lexer = Lexer().build(debug=self.debug, optimize=self.optimize,
                lextab='cm.lexer.output.lextab')
        try:
            return self.parser.parse(input, lexer=lexer, debug=self.debug)
        except SyntaxError as e:
            raise convert(e, self.context)

    def preparse(self, string):
        string = comment.sub(subcomment, string)
        return string + ('' if string.endswith('\n') else '\n')
        
    def build(self, string, context=None):
        self.indent = 0
        self.context = context
        self.stream = Stream(self.parse(self.preparse(string)))
        return File(*list(self.statements()))

    def statements(self):
        while True:
            line = self.stream.next()
            line.context = self.context
            if isinstance(line, EmptyLine):
                line.indentation = self.indent
            if line.indentation > self.indent:
                raise ConversionError('Unexpected indent at %s' % repr(line))
            elif line.indentation < self.indent:
                self.stream.backstep()
                raise StopIteration
            line.subparse(self)
            yield line

    def parse_body(self):
        self.indent += 1
        statements = list(self.statements())
        self.indent -= 1
        return statements
Example #2
0
 def build(self, string, context=None):
     self.indent = 0
     self.context = context
     self.stream = Stream(self.parse(self.preparse(string)))
     return File(*list(self.statements()))