Esempio n. 1
0
    global FooNode, BarNode, MyStruct

    print('== {} =='.format(name))

    class MyStruct(Struct):
        a = UserField(type=Int)
        b = UserField(type=Int)

    class FooNode(ASTNode):
        pass

    class BarNode(FooNode):
        @langkit_property(public=True)
        def public_prop():
            struct = Var(Self.prop)
            return struct.a + struct.b

        prop = Property(expr)

    emit_and_print_errors(lkt_file='foo.lkt')
    print('')


run("Correct code", lambda: New(MyStruct, a=Literal(12), b=Literal(15)))
run("Incorrect new 1", lambda: New(MyStruct, a=Literal(12)))
run("Incorrect new 2", lambda: New(MyStruct, a=Literal(12), b=No(FooNode)))
run("Incorrect new 1",
    lambda: New(MyStruct, a=Literal(12), b=Literal(15), c=Literal(19)))
print('Done')
Esempio n. 2
0
 class FooNode(ASTNode):
     prop = Property(Literal(0), public=True)
Esempio n. 3
0
 class ConcreteNode(AbstractNode):
     prop = Property(Literal(True), dynamic_vars=concrete_dyn_vars)
Esempio n. 4
0
# Test proper validation for argument passing with default arguments
run('Missing argument (1)',
    lambda a=Bool, b=(Bool, True): a.and_then(b),
    Self.p())
run('Missing argument (2)',
    lambda a=Bool, b=(Bool, True): a.and_then(b),
    Self.p(b=True))

run('Bad keyword argument',
    lambda a=Bool, b=(Bool, True): a.and_then(b),
    Self.p(a=1, b=True))

run('Too many arguments (1)',
    lambda a=Bool, b=(Bool, True): a.and_then(b),
    Self.p(True, False, b=False))
run('Too many arguments (2)',
    lambda a=Bool, b=(Bool, True): a.and_then(b),
    Self.p(True, a=False, b=False))
run('Too many arguments (3)',
    lambda a=Bool, b=(Bool, True): a.and_then(b),
    Self.p(True, False, c=False))
run('Too many arguments (4)',
    lambda a=Bool, b=(Bool, True): a.and_then(b),
    Self.p(True, False, 1, a=False))

run('Non-literal argument',
    lambda a=(Bool, Literal(True).equals(False)): a,
    Self.p)

print('Done')
Esempio n. 5
0
    'Invalid type', lambda: Self.body.match(
        lambda e=NullNode: e.prop,
        lambda e=Expression: e.prop,
        lambda e=Bool: e.prop,
    ))
run(
    'Irrelevant AST node', lambda: Self.body.match(
        lambda e=NullNode: e.prop,
        lambda e=Expression: e.prop,
        lambda e=FooNode: e.prop,
    ))

# Unreachable matchers
run('Default case after full coverage', lambda: Self.body.match(
    lambda e=BodyNode: e.prop,
    lambda _: Literal(1),
))

run('Node after default case (1)', lambda: Self.body.match(
    lambda _: Literal(0),
    lambda e=BodyNode: e.prop,
))
run('Node after default case (2)', lambda: Self.body.match(
    lambda _: Literal(0),
    lambda e=Number: e.prop,
))

run(
    'Node after full coverage (1)', lambda: Self.body.match(
        lambda e=BodyNode: e.prop,
        lambda e=Number: e.prop,
Esempio n. 6
0
 class FooNode(ASTNode):
     prop = Property(Literal(0))
Esempio n. 7
0
from langkit.expressions import Let, Literal, Property

from utils import emit_and_print_errors


def run(name, expr):
    """
    Emit and print the errors we get for the below grammar with "expr" as
    a property in ExampleNode.
    """

    global Compound, Expression, FooNode, NullNode, Number

    print('== {} =='.format(name))

    class FooNode(ASTNode):
        pass

    class BarNode(FooNode):
        prop = Property(expr, public=True)

    emit_and_print_errors(lkt_file='foo.lkt')
    print('')


run("Correct code", lambda: Let(lambda a=Literal(1): a))
run("Missing var value", lambda: Let(lambda a: a))
run("Invalid args", lambda: Let(lambda a=Literal(1), *b, **c: a))

print('Done')
Esempio n. 8
0
def int_expr():
    return Literal(0)
Esempio n. 9
0
def bool_expr():
    return Literal(True)
Esempio n. 10
0
 class ConcreteNode(AbstractNode):
     prop = Property(Literal(True),
                     has_implicit_env=concrete_has_implicit_env)
Esempio n. 11
0
    class ConcreteNode(AbstractNode):
        prop = Property(Literal(True), public=concrete_public)

        public_prop = Property(Self.prop, public=True)
Esempio n. 12
0
 class ConcreteNode(AbstractNode):
     prop = Property(Literal(True), private=concrete_private)