示例#1
0
 def test_struct_iterator():
     val = Var(TestStruct.new(
         count=2,
         nodes=[No(Example.entity), Entity],
     ))
     itr = Var(
         ArrayLiteral([val, val], element_type=TestStruct).to_iterator)
     return ArrayLiteral([itr, itr],
                         element_type=T.TestStruct.iterator).to_iterator
示例#2
0
    def test_envs():
        null = Var(No(T.LexicalEnv))
        primary = Var(Self.children_env)
        orphan = Var(primary.env_orphan)
        group = Var(ArrayLiteral([primary, null]).env_group())
        rebound = Var(primary.rebind_env(Self.get_rebindings(False)))

        arr = Var(ArrayLiteral([
            null,
            primary,
            orphan,
            group,
            rebound,
        ]))
        return arr.length  # BREAK:test_envs
示例#3
0
    def test_arrays():
        empty = Var(No(T.Example.array))
        single = Var(ArrayLiteral([Self.cast(Example)]))
        complete = Var(Self.parent.cast(T.Example.list).as_array)

        arr = Var(empty.concat(single).concat(complete))
        return arr.length  # BREAK:test_arrays
示例#4
0
 def get_struct():
     return New(MyStruct,
                entity_field=Self,
                array_field=ArrayLiteral([
                    Self.cast(T.FooNode).as_bare_entity,
                    Self.parent.as_bare_entity]),
                bigint_field=BigIntLiteral(10**100))
示例#5
0
 def test_dotexpr_lhs():
     """
     Test various valid dotexpr's LHS.
     """
     a = Var(ArrayLiteral([1]).find(lambda v: v == 1))
     b = Var(Let(lambda b=[1, 2]: b).find(lambda v: v == 1))
     c = Var(String("hello").find(lambda c: c == Char('h')))
     ignore(b)
     ignore(c)
     return a
示例#6
0
文件: test.py 项目: pmderodat/langkit
class Example(BaseExample):

    fld_2 = NullField()
    fld_1 = Field()

    @langkit_property()
    def to_public(p=T.PrivatePoint):
        return Point.new(label=String("from private"), x=p.x, y=p.y)

    @langkit_property(public=True)
    def prop(p=T.Point):
        return Self.to_public(PrivatePoint.new(x=p.x, y=p.y))

    @langkit_property(public=True)
    def result():
        return T.NodeResult.new(n=Self, e=Entity)

    # Test for primitive types
    id_bool = Property(lambda id=T.Bool: id, public=True)
    id_int = Property(lambda id=T.Int: id, public=True)
    id_bigint = Property(lambda id=T.BigInt: id, public=True)
    id_char = Property(lambda id=T.Character: id, public=True)
    id_token = Property(lambda id=T.Token: id, public=True)
    id_sym = Property(lambda id=T.Symbol: id, public=True)
    id_unit = Property(lambda id=T.AnalysisUnit: id, public=True)
    id_root_node = Property(lambda id=T.FooNode: id, public=True)
    id_name = Property(lambda id=T.Name: id, public=True)

    # Test for enums
    id_unit_kind = Property(lambda id=T.AnalysisUnitKind: id, public=True)

    # Test for arrays
    id_node_array = Property(lambda id=T.FooNode.entity.array: id, public=True)
    id_expr_array = Property(lambda id=T.Expr.entity.array: id, public=True)
    id_bigint_array = Property(lambda id=T.BigInt.array: id, public=True)
    id_unit_array = Property(lambda id=T.AnalysisUnit.array: id, public=True)

    # Test for iterators
    create_bigint_iterator = Property(
        ArrayLiteral([BigIntLiteral(1), BigIntLiteral(2), BigIntLiteral(3)])
        .to_iterator,
        public=True
    )
    id_bigint_iterator = Property(lambda id=T.BigInt.iterator: id, public=True)

    # Test for default values
    id_dflt_bool = Property(lambda id=(T.Bool, True): id, public=True)
    id_dflt_int = Property(lambda id=(T.Int, 42): id, public=True)
    id_dflt_char = Property(
        lambda id=(T.Character, CharacterLiteral('\x00')): id,
        public=True)
    id_dflt_root_node = Property(lambda id=(T.FooNode, No(T.FooNode)): id,
                                 public=True)
示例#7
0
    def test_rebindings():
        null = Var(No(T.EnvRebindings))
        r1 = Var(Self.get_rebindings(False))
        r2 = Var(Self.get_rebindings(True))
        concat = Var(r1.concat_rebindings(r2))

        arr = Var(ArrayLiteral([
            null,
            r1,
            r2,
            concat,
        ]))
        return arr.length  # BREAK:test_rebindings
示例#8
0
    def construct(self):
        from langkit.expressions import If, IsNull, ArrayLiteral

        expr = construct(self.expr)
        expr_var = expr.create_result_var('To_Array_Prefix')

        # Use "expr" only for first evaluation, and then use expr_var to refer
        # to the result. We do this to avoid resolved expression sharing in the
        # expression tree.
        ret = CollectionSingleton.Expr(expr_var if self.coerce_null else expr)
        if self.coerce_null:
            return If.Expr(IsNull.construct_static(expr),
                           ArrayLiteral.construct_static([], expr.type.array),
                           ret, ret.type)
        else:
            return ret
示例#9
0
    def test_strings():
        empty = Var(String(""))
        foo = Var(String("foo"))
        s_quote = Var(String("'"))
        d_quote = Var(String('"'))
        quote_mix = Var(String("\"'"))
        lf = Var(String("\n"))
        nul = Var(String("\x00"))

        arr = Var(
            ArrayLiteral([
                empty,
                foo,
                s_quote,
                d_quote,
                quote_mix,
                lf,
                nul,
            ]))
        return arr.length  # BREAK:test_strings
示例#10
0
    def test_entities():
        null_root = Var(No(T.FooNode.entity))
        null_example = Var(No(T.Example.entity))
        ent_root = Var(Entity)  # BREAK:test_state
        ent_example = Var(ent_root.cast(T.Example))
        rebound = Var(
            T.FooNode.entity.new(
                node=Self,
                info=T.EntityInfo.new(
                    md=No(T.Metadata),
                    rebindings=Self.get_rebindings(False),
                    from_rebound=False,
                ),
            ))

        arr = Var(
            ArrayLiteral([
                null_root,
                null_example.cast(T.FooNode),
                ent_root,
                ent_example.cast(T.FooNode),
                rebound,
            ]))
        return arr.length  # BREAK:test_entities
示例#11
0
 def values_array():
     return ArrayLiteral([1, 2, 3], element_type=T.Int)
示例#12
0
 def double(c=T.Character):
     return ArrayLiteral([c, c], T.Character)
示例#13
0
 def iter_int():
     return ArrayLiteral([1, 2, 3]).to_iterator
示例#14
0
 def get_array():
     return ArrayLiteral([1, 2])
示例#15
0
    def test_tokens():
        null = Var(No(T.Token))
        first = Var(Self.token_start)

        arr = Var(ArrayLiteral([null, first]))
        return arr.length  # BREAK:test_tokens
示例#16
0
 def entities_iterator():
     return ArrayLiteral([Entity, Entity, Entity],
                         element_type=Example.entity).to_iterator
示例#17
0
from langkit.parsers import Grammar

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 Example.
    """

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

    class FooNode(ASTNode):
        pass

    class Example(FooNode):
        p = Property(expr)

    grammar = Grammar('main_rule')
    grammar.add_rules(main_rule=Example('example'), )
    emit_and_print_errors(grammar)
    print('')


run('Empty: missing type', [])
run('Empty: ok', ArrayLiteral([], T.Bool))
run('Single: ok', [True])
run('Multiple: heterogeneous types', [True, Self])
print('Done')
示例#18
0
文件: test.py 项目: setton/langkit
 def entities_array():
     return ArrayLiteral([Entity, Entity, Entity],
                         element_type=Example.entity)