kong.parser — Tofu parser¶
- kong.parser.parse_expression(tokens)¶
Parses an expression.
>>> from kong.lexer import tokenize >>> p = lambda s: parse_expression(tokenize(s)) >>> p('f()') kong.ast.Application(kong.ast.Identifier(u'f'), []) >>> p('f(a, f2())') kong.ast.Application(kong.ast.Identifier(u'f'), [kong.ast.Identifier(u'a'), kong.ast.Application(kong.ast.Identifier(u'f2'), [])]) >>> p('b <- 1 + (a <- 2) * 3') kong.ast.IdentifierLocalDefinition(lvalue=kong.ast.Identifier(u'b'), rvalue=kong.ast.Operator(operator=kong.ast.Identifier(u'*'), operands=[kong.ast.Operator(operator=kong.ast.Identifier(u'+'), operands=[kong.ast.StringLiteral(1), kong.ast.IdentifierLocalDefinition(lvalue=kong.ast.Identifier(u'a'), rvalue=kong.ast.StringLiteral(2))]), kong.ast.StringLiteral(3)])) >>> p('(1 + 2).*') kong.ast.Attribute(kong.ast.Operator(operator=kong.ast.Identifier(u'+'), operands=[kong.ast.StringLiteral(1), kong.ast.StringLiteral(2)]), attribute=kong.ast.Identifier(u'*')) >>> p('[]') kong.ast.ListLiteral([]) >>> p('[a, 1]') kong.ast.ListLiteral([kong.ast.Identifier(u'a'), kong.ast.StringLiteral(1)]) >>> p('[1, [2, 3], +]') kong.ast.ListLiteral([kong.ast.StringLiteral(1), kong.ast.ListLiteral([kong.ast.StringLiteral(2), kong.ast.StringLiteral(3)]), kong.ast.Identifier(u'+')])
Parameters: tokens (collections.Iterable) – tokens to parse Returns: parsed expression Return type: kong.ast.Expression Raises: kong.lexer.SyntaxError for invalid syntax