Ejemplo n.º 1
0
def ExtendParser(input):

    simpleExtend = Parser.allOf(
        Parser.terminal(TokenType.EXTEND),
        TypeParser,
        Parser.terminal(TokenType.AS),
        IdentifierLiteralParser,
        Parser.terminal(TokenType.WITH),
        Parser.doAssert(
            Parser.wrap(
                FunctionDefinitionParser,
                lambda method: Block.withChildren([method], method.pos)),
            "method definition"),
        createNode=lambda extend, type, _, variable, __, methods: Extend.
        withValues(extend.pos, type, variable, methods),
        name="simple extend")

    multiExtend = Parser.allOf(
        Parser.terminal(TokenType.EXTEND),
        Parser.doAssert(TypeParser, "type being extended"),
        Parser.terminal(TokenType.AS, doAssert=True),
        Parser.doAssert(IdentifierLiteralParser, "variable name"),
        Parser.doAssert(
            MethodsDeclarationParser,
            f"block with methods definitions or '{TokenType.WITH.key}' keyword"
        ),
        createNode=lambda extend, type, _, variable, methods: Extend.
        withValues(extend.pos, type, variable, methods),
        name="multiple extend")

    return Parser.oneOf(simpleExtend, multiExtend, name="extend")(input)
Ejemplo n.º 2
0
def LoopParser(input):
    from smnp.ast.node.identifier import IdentifierLiteralParser
    from smnp.ast.node.iterable import abstractIterableParser
    from smnp.ast.node.statement import StatementParser

    loopParameters = Parser.allOf(
        Parser.terminal(TokenType.AS),
        Parser.oneOf(
            Parser.wrap(IdentifierLiteralParser,
                        lambda id: LoopParameters.withChildren([id], id.pos)),
            abstractIterableParser(LoopParameters, TokenType.OPEN_PAREN,
                                   TokenType.CLOSE_PAREN,
                                   IdentifierLiteralParser)),
        createNode=lambda asKeyword, parameters: parameters,
        name="loop parameters")

    loopFilter = Parser.allOf(Parser.terminal(TokenType.PERCENT),
                              Parser.doAssert(ExpressionWithoutLoopParser,
                                              "filter as bool expression"),
                              createNode=lambda percent, expr: expr,
                              name="loop filter")

    return Parser.allOf(ExpressionWithoutLoopParser,
                        Parser.optional(loopParameters),
                        Parser.terminal(TokenType.CARET,
                                        createNode=Operator.withValue),
                        StatementParser,
                        Parser.optional(loopFilter),
                        createNode=Loop.loop,
                        name="caret-loop")(input)