Exemplo n.º 1
0
def run(name, prop):
    print('== {} =='.format(name))

    class FooNode(ASTNode):
        resolve_ref = prop

    class Decl(FooNode):
        name = Field()
        refs = Field()

        env_spec = EnvSpec(
            add_to_env(New(T.env_assoc, key=Self.name.symbol, val=Self)),
            add_env())

    class Ref(FooNode):
        name = Field()

        env_spec = EnvSpec(
            add_to_env(New(T.env_assoc, key=Self.name.symbol, val=Self),
                       resolver=FooNode.resolve_ref))

        @langkit_property(public=True)
        def resolve():
            return Self.node_env.get(Self.name.symbol).at(0)

    grammar = Grammar('main_rule')
    grammar.add_rules(
        main_rule=List(grammar.decl),
        decl=Decl(Tok(Token.Identifier, keep=True), Tok(Token.LPar),
                  List(grammar.ref, empty_valid=True), Tok(Token.RPar)),
        ref=Ref(Tok(Token.Identifier, keep=True)),
    )
    emit_and_print_errors(grammar)
Exemplo n.º 2
0
def run(name, lhs, rhs):
    """
    Emit and print the errors we get for the below grammar with "expr" as
    a property in Example.
    """

    global FooNode, BarNode, ListNode

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

    class FooNode(ASTNode):
        pass

    class Example(FooNode):
        prop = Property(lhs.equals(rhs), dynamic_vars=[Env])
        use_prop = Property(Env.bind(Self.node_env, Self.prop), public=True)

    class Lit(FooNode):
        tok = Field()

    grammar = Grammar('main_rule')
    grammar.add_rules(main_rule=Or(Example(Tok(Token.Example)),
                                   Lit(Tok(Token.Number, keep=True))), )
    emit_and_print_errors(grammar)
    Env.unfreeze()
    print('')
Exemplo n.º 3
0
 def lang_def():
     foo_grammar = Grammar('main_rule')
     foo_grammar.add_rules(
         main_rule=Example('example', Opt(foo_grammar.name)),
         name=Name(Tok(Token.Identifier, keep=True)),
     )
     return foo_grammar
Exemplo n.º 4
0
def run(name, prop_fn, prop_memoized):
    """
    Emit and print the errors we get for the below grammar with "expr" as
    a property in BarNode.
    """

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

    class FooNode(ASTNode):
        pass

    class Literal(FooNode):
        tok = Field()

    class EmptyNode(FooNode):
        pass

    class LiteralList(Literal.list):
        prop = Property(prop_fn(), memoized=prop_memoized)

    grammar = Grammar('main_rule')
    grammar.add_rules(
        main_rule=grammar.list_rule,
        list_rule=Pick('(', List(grammar.list_item, sep=',', cls=LiteralList),
                       ')'),
        list_item=Literal(Tok(Token.Number, keep=True)),
    )
    emit_and_print_errors(grammar)
    print('')
Exemplo n.º 5
0
def run(name, expr_fn):
    """
    Emit and print the errors we get for the below grammar with "expr_fn" as a
    property in Example.
    """
    print('== {} =='.format(name))

    @abstract
    class FooNode(ASTNode):
        pass

    class Example(FooNode):
        name = Field()

        prop = Property(expr_fn)

    class Name(FooNode):
        tok = Field()

    grammar = Grammar('main_rule')
    grammar.add_rules(
        main_rule=Example('example', Opt(grammar.name)),
        name=Name(Tok(Token.Identifier, keep=True)),
    )
    emit_and_print_errors(grammar)
    print('')
Exemplo n.º 6
0
def run(expr):
    """
    Emit and print the errors we get for the below grammar for the given
    "expr" property expression.
    """

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

    class FooNode(ASTNode):
        pass

    class ExampleNode(FooNode):
        tok = Field()

        implicit_prop = Property(Self.as_bare_entity, dynamic_vars=[Env])

        prop = Property(expr, public=True)
        use_implicit_prop = Property(
            Env.bind(Self.node_env, Self.implicit_prop),
            public=True
        )

    grammar = Grammar('main_rule')
    grammar.add_rules(
        main_rule=ExampleNode(Tok('example', keep=True)),
    )
    emit_and_print_errors(grammar)
    Env.unfreeze()
    print('')
Exemplo n.º 7
0
def run(name, expr):
    """
    Emit and print the errors we get for the below grammar with "expr" as
    a property in BarNode.
    """

    global FooNode

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

    class FooNode(ASTNode):
        pass

    class BarNode(FooNode):
        list_node = Field()

    class ListNode(FooNode):
        nb_list = Field()
        prop = Property(expr, public=True)

    class NumberNode(FooNode):
        tok = Field()

    grammar = Grammar('main_rule')
    grammar.add_rules(
        main_rule=BarNode('example', grammar.list_rule),
        list_rule=ListNode(List(NumberNode(Tok(Token.Number, keep=True)))),
    )
    emit_and_print_errors(grammar)
    print('')
Exemplo n.º 8
0
 def lang_def():
     foo_grammar = Grammar('main_rule')
     foo_grammar.add_rules(
         main_rule=Row('example', foo_grammar.list_rule) ^ BarNode,
         list_rule=Row(
             List(Tok(Token.Number, keep=True) ^ NumberNode)
         ) ^ ListNode,
     )
     return foo_grammar
Exemplo n.º 9
0
def run(name, match_expr):
    """
    Emit and print the errors we get for the below grammar with "match_expr" as
    a property in ExampleNode.
    """

    global BodyNode, Compound, Expression, FooNode, NullNode, Number

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

    @abstract
    class FooNode(ASTNode):
        prop = Property(Literal(0), public=True)

    @abstract
    class BodyNode(FooNode):
        pass

    class NullNode(BodyNode):
        pass

    @abstract
    class Expression(BodyNode):
        pass

    class Number(Expression):
        tok = Field()

    class Compound(Expression):
        prefix = Field()
        suffix = Field()

    class ExampleNode(FooNode):
        body = Field()

        prop = Property(match_expr)

    grammar = Grammar('main_rule')
    grammar.add_rules(
        main_rule=ExampleNode(
            'example',
            Or(
                grammar.expression,
                NullNode('null')
            )
        ),

        number=Number(Tok(Token.Number, keep=True)),

        expression=Or(
            Compound(grammar.number, ',', grammar.expression),
            grammar.number
        ),
    )
    emit_and_print_errors(grammar)
    print('')
Exemplo n.º 10
0
def lang_def():
    foo_grammar = Grammar('stmts_rule')
    foo_grammar.add_rules(
        def_rule=Row(Tok(Token.Identifier, keep=True),
                     Opt(Row('(', foo_grammar.stmts_rule, ')')[1])) ^ Def,
        stmt_rule=(foo_grammar.def_rule
                   | Row('{', List(foo_grammar.stmt_rule, empty_valid=True),
                         '}') ^ Block),
        stmts_rule=List(foo_grammar.stmt_rule))
    return foo_grammar
Exemplo n.º 11
0
 def lang_def():
     foo_grammar = Grammar('main_rule')
     foo_grammar.add_rules(
         main_rule=foo_grammar.list_rule,
         list_rule=Row(
             '(', List(foo_grammar.list_item, sep=',', cls=LiteralList),
             ')')[0],
         list_item=Row(Tok(Token.Number, keep=True)) ^ Literal,
     )
     return foo_grammar
Exemplo n.º 12
0
 def lang_def():
     foo_grammar = Grammar('main_rule')
     foo_grammar.add_rules(
         main_rule=Row('example',
                       Or(foo_grammar.expression,
                          Row('null') ^ NullNode)) ^ ExampleNode,
         number=Tok(Token.Number, keep=True) ^ Number,
         expression=Or(
             Row(foo_grammar.number, ',', foo_grammar.expression)
             ^ Compound, foo_grammar.number),
     )
     return foo_grammar
Exemplo n.º 13
0
def lang_def():
    @root_grammar_class()
    class FooNode(ASTNode):
        pass

    class Def(FooNode):
        name = Field()
        body = Field()
        env_spec = EnvSpec(add_env=True, add_to_env=(Self.name, Self))

    foo_grammar = Grammar('stmt_rule')
    foo_grammar.add_rules(
        def_rule=Row(
            Tok(Token.Identifier, keep=True),
            '(', foo_grammar.stmt_rule, ')'
        ) ^ Def,
        stmt_rule=List(
            foo_grammar.def_rule
            | Row('{', List(foo_grammar.stmt_rule, empty_valid=True), '}')[1],
            empty_valid=True
        )
    )
    return foo_grammar
Exemplo n.º 14
0
def run(md_constructor):
    """
    Emit and print he errors we get for the below grammar. `md_constructor` is
    called to create the lexical environment metadata.
    """

    print('== {} =='.format(md_constructor.__name__))

    class FooNode(ASTNode):
        pass

    class Example(FooNode):
        pass

    grammar = Grammar('main_rule')
    grammar.add_rules(main_rule=Example(Tok(Token.Example)))

    try:
        md_constructor()
    except DiagnosticError:
        reset_langkit()
    else:
        emit_and_print_errors(grammar)
    print('')
Exemplo n.º 15
0
from langkit.expressions import DynamicVariable, Property, Self
from langkit.parsers import Grammar, Tok

from lexer_example import Token
from utils import emit_and_print_errors


dynvar = DynamicVariable('dynvar', T.FooNode)


class FooNode(ASTNode):
    pass


class Example(FooNode):
    tok = Field()

    # The "construct" pass on p1 will require the type of p2 and thus trigger
    # the construction of p2. A bug used to propagate the binding of "dynvar"
    # from the construction of p1 to p2's.
    p1 = Property(dynvar.bind(Self, Self.p2).as_bare_entity, public=True)
    p2 = Property(dynvar)


grammar = Grammar('main_rule')
grammar.add_rules(
    main_rule=Example(Tok(Token.Example, keep=True)),
)
emit_and_print_errors(grammar)
print('Done')
Exemplo n.º 16
0

class Ref(FooNode):
    name = Field()

    env_spec = EnvSpec(
        add_to_env(
            New(T.env_assoc, key=Self.name.symbol, val=Self),
        )
    )

    @langkit_property(public=True)
    def resolve():
        return Env.bind(Self.parent.parent.node_env,
                        Env.get(Self.name.symbol).at(0))


foo_grammar = Grammar('main_rule')
foo_grammar.add_rules(
    main_rule=List(foo_grammar.decl),
    decl=Decl(
        Tok(Token.Identifier, keep=True),
        Tok(Token.LPar),
        List(foo_grammar.ref, empty_valid=True),
        Tok(Token.RPar)
    ),
    ref=Ref(Tok(Token.Identifier, keep=True)),
)
build_and_run(foo_grammar, 'main.py')
print('Done')
Exemplo n.º 17
0

class Ref(Atom):
    tok = Field()

    prop1 = Property(3)
    prop2 = Property(3)
    prop3 = Property(3)


class Plus(Expr):
    lhs = Field()
    rhs = Field()

    prop1 = Property(4)
    prop2 = Property(4)
    prop3 = Property(4)


grammar = Grammar('main_rule')
grammar.add_rules(
    main_rule=List(grammar.expr),
    expr=Or(grammar.atom, grammar.plus),
    atom=Or(grammar.lit, grammar.ref),
    lit=Lit(Tok(Token.Number, keep=True)),
    ref=Ref(Tok(Token.Identifier, keep=True)),
    plus=Pick('(', Plus(grammar.expr, '+', grammar.expr), ')'),
)
emit_and_print_errors(grammar)
print('Done')
Exemplo n.º 18
0
    env_spec = EnvSpec(
        add_to_env(mappings=New(T.env_assoc, key=Self.name.symbol, val=Self)),
        add_env(),
        reference(Self.imports.map(lambda i: i.cast(T.FooNode)),

                  # If PropertyDef rewriting omits the following references,
                  # env lookup will never reach DerivedRef.referenced_env, so
                  # resolution will sometimes fail to reach definition.
                  T.MiddleRef.referenced_env)
    )


grammar = Grammar('main_rule')
grammar.add_rules(
    main_rule=List(Or(
        Def('def', Tok(Token.Identifier, keep=True),
            grammar.imports, grammar.vars, grammar.expr),
        grammar.expr
    )),

    imports=Pick('(', List(grammar.derived_ref, empty_valid=True), ')'),

    var=Var(Tok(Token.Identifier, keep=True), '=', grammar.expr),
    vars=Pick('{', List(grammar.var, empty_valid=True), '}'),

    expr=Or(grammar.atom, grammar.plus),

    atom=Or(grammar.lit, grammar.ref),
    lit=Lit(Tok(Token.Number, keep=True)),
    ref=Ref(Tok(Token.Identifier, keep=True)),
    derived_ref=DerivedRef(Tok(Token.Identifier, keep=True)),
Exemplo n.º 19
0
    pass


@abstract
class Expression(FooNode):
    result = AbstractProperty(type=LongType)


class Literal(Expression):
    tok = Field()

    result = ExternalProperty()


class Plus(Expression):
    left = Field()
    right = Field()

    result = Property(Self.left.result + Self.right.result)


foo_grammar = Grammar('main_rule')
foo_grammar.add_rules(
    main_rule=Or(
        Row(foo_grammar.atom, '+', foo_grammar.main_rule) ^ Plus,
        foo_grammar.atom),
    atom=Row(Tok(Token.Number, keep=True)) ^ Literal,
)
build_and_run(foo_grammar, 'main.py')
print 'Done'
Exemplo n.º 20
0
    result = ExternalProperty(uses_entity_info=False, uses_envs=False)


class Name(Expression):
    tok = Field()

    designated_unit = ExternalProperty(type=AnalysisUnitType,
                                       uses_entity_info=False,
                                       uses_envs=True)
    result = Property(Self.designated_unit.root.cast(Expression).result)


class Plus(Expression):
    left = Field()
    right = Field()

    result = Property(Self.left.result + Self.right.result)


foo_grammar = Grammar('main_rule')
foo_grammar.add_rules(
    main_rule=Or(Plus(foo_grammar.atom, '+', foo_grammar.main_rule),
                 foo_grammar.atom),
    atom=Or(
        Literal(Tok(Token.Number, keep=True)),
        Name(Tok(Token.Identifier, keep=True)),
    ),
)
build_and_run(foo_grammar, 'main.py')
print('Done')
Exemplo n.º 21
0
from lexer_example import Token
from utils import build_and_run


class FooNode(ASTNode):
    prop = AbstractProperty(runtime_check=True, type=LongType, public=True)


class Literal(FooNode):
    tok = Field()

    a = AbstractProperty(runtime_check=True, type=FooNode.entity)
    var = UserField(LogicVarType, public=False)

    @langkit_property(return_type=T.Literal.entity)
    def node():
        return Self.as_entity

    b = Property(Bind(Self.var, Self.a, Self.node))

    @langkit_property(public=True)
    def public_pro():
        return Let(lambda _=Self.b: Self.as_bare_entity)


foo_grammar = Grammar('main_rule')
foo_grammar.add_rules(main_rule=Literal(Tok(Token.Number, keep=True)), )
build_and_run(foo_grammar, 'main.py')
print('Done')
Exemplo n.º 22
0
    env_spec = EnvSpec(
        add_to_env(mappings=New(T.env_assoc, key=Self.name.symbol, val=Self),
                   metadata=New(Metadata, b=Self.has_plus))
    )

    @langkit_property(public=True, return_type=T.Ref.entity.array)
    def entity_items():
        return Self.as_entity.items.map(lambda i: i)


class Ref(FooNode):
    name = Field()

    @langkit_property(public=True, return_type=Decl.entity)
    def decl():
        return Self.children_env.get(Self.name).at(0).cast_or_raise(Decl)


fg = Grammar('main_rule')
fg.add_rules(
    main_rule=List(fg.decl),
    decl=Decl(Opt('+').as_bool(),
              Tok(Token.Identifier, keep=True),
              '(', fg.ref_list, ')'),
    ref_list=List(fg.ref, empty_valid=True),
    ref=Ref(Tok(Token.Identifier, keep=True)),
)
build_and_run(fg, 'main.py')
print('Done')
Exemplo n.º 23
0
from utils import build_and_run


class FooNode(ASTNode):
    prop = AbstractProperty(runtime_check=True, type=LongType, public=True)


class Literal(FooNode):
    tok = Field()

    a = AbstractProperty(runtime_check=True, type=FooNode.entity)
    var = UserField(LogicVarType, public=False)

    @langkit_property(return_type=BoolType)
    def is_eq(other=T.Literal.entity):
        return (Self.as_entity == other)

    b = Property(Bind(Self.var, Self.a, eq_prop=Self.is_eq))

    @langkit_property(public=True)
    def public_prop():
        return Let(lambda _=Self.b: Self.as_bare_entity)


foo_grammar = Grammar('main_rule')
foo_grammar.add_rules(
    main_rule=Literal(Tok(Token.Number, keep=True)),
)
build_and_run(foo_grammar, 'main.py')
print('Done')
Exemplo n.º 24
0
class Decl(FooNode):
    name = Field()
    items = Field()

    env_spec = EnvSpec(
        add_to_env(mappings=New(T.env_assoc, key=Self.name.symbol, val=Self)))


class Ref(FooNode):
    name = Field()

    @langkit_property(public=True, return_type=Decl.entity)
    def decl_wrapper():
        return Entity.decl

    @langkit_property(public=True, return_type=Decl.entity)
    def decl():
        return Self.children_env.get_first(Self.name).cast_or_raise(Decl)


fg = Grammar('main_rule')
fg.add_rules(
    main_rule=List(fg.decl),
    decl=Decl(Tok(Token.Identifier, keep=True), '(', fg.ref_list, ')'),
    ref_list=List(fg.ref, empty_valid=True),
    ref=Ref(Tok(Token.Identifier, keep=True)),
)
build_and_run(fg, 'main.py')
print('Done')
Exemplo n.º 25
0
         PrivatePart(List(A.task_item, empty_valid=True,
                          list_cls=DeclList))), end_named_block()),
 task_type_decl=TaskTypeDecl("task", "type", A.identifier,
                             Opt(A.discriminant_part), A.aspect_spec,
                             Opt(A.task_def), sc()),
 subtype_decl=SubtypeDecl("subtype", A.identifier, "is",
                          A.subtype_indication, A.aspect_spec, sc()),
 interface_type_def=InterfaceTypeDef(
     Opt(
         Or(
             InterfaceKind.alt_limited("limited"),
             InterfaceKind.alt_task("task"),
             InterfaceKind.alt_protected(
                 L.Identifier(match_text="protected"), ),
             InterfaceKind.alt_synchronized(
                 Tok(L.Identifier, match_text="synchronized")))),
     L.Identifier(match_text="interface"), Opt("and", A.parent_list)),
 unconstrained_index=UnconstrainedArrayIndex(A.subtype_indication, "range",
                                             "<>"),
 array_type_def=ArrayTypeDef(
     "array", "(",
     Or(UnconstrainedArrayIndices(List(A.unconstrained_index, sep=",")),
        ConstrainedArrayIndices(A.constraint_list)), ")", "of",
     A.component_def),
 discrete_subtype_definition=A.discrete_range | A.subtype_indication,
 constraint_list=List(A.discrete_subtype_definition,
                      sep=",",
                      list_cls=ConstraintList),
 signed_int_type_def=SignedIntTypeDef(A.range_spec),
 mod_int_type_def=ModIntTypeDef("mod", A.sexpr_or_box),
 derived_type_def=DerivedTypeDef(
Exemplo n.º 26
0
from langkit.dsl import ASTNode, Field
from langkit.envs import EnvSpec
from langkit.expressions import Self
from langkit.parsers import Grammar, List, Pick, Tok

from lexer_example import Token
from utils import emit_and_print_errors


class FooNode(ASTNode):
    pass


class Def(FooNode):
    name = Field()
    body = Field()
    env_spec = EnvSpec(add_env=True, add_to_env=(Self.name, Self))


grammar = Grammar('stmt_rule')
grammar.add_rules(
    def_rule=Def(Tok(Token.Identifier, keep=True), '(', grammar.stmt_rule,
                 ')'),
    stmt_rule=List(grammar.def_rule
                   | Pick('{', List(grammar.stmt_rule, empty_valid=True), '}'),
                   empty_valid=True))
emit_and_print_errors(grammar)

print('Done')
Exemplo n.º 27
0
from langkit.parsers import Grammar, Row, Tok

from lexer_example import Token
from utils import build_and_run

Diagnostics.set_lang_source_dir(os.path.abspath(__file__))


@root_grammar_class()
class FooNode(ASTNode):
    prop = AbstractProperty(runtime_check=True, type=LongType)


class BarNode(FooNode):
    pass


class Literal(FooNode):
    tok = Field()

    a = AbstractProperty(runtime_check=True, type=FooNode.env_el())
    var = UserField(LogicVarType, is_private=True)

    b = Property(Bind(Self.var, Self.a), private=True)


foo_grammar = Grammar('main_rule')
foo_grammar.add_rules(main_rule=Row(Tok(Token.Number, keep=True)) ^ Literal, )
build_and_run(foo_grammar, 'main.py')
print 'Done'
Exemplo n.º 28
0
@abstract
class Stmt(FooNode):
    pass


class Def(Stmt):
    id = Field()
    body = Field()

    name = Property(Self.id)
    env_spec = EnvSpec(add_to_env(Self.id.symbol, Self), add_env())

    faulty_prop = Property(Self._env_mappings_0)


class Block(Stmt):
    items = Field()

    env_spec = EnvSpec(add_env())


grammar = Grammar('stmts_rule')
grammar.add_rules(
    def_rule=Def(Tok(Token.Identifier, keep=True),
                 Opt('(', grammar.stmts_rule, ')')),
    stmt_rule=(grammar.def_rule
               | Block('{', List(grammar.stmt_rule, empty_valid=True), '}')),
    stmts_rule=List(grammar.stmt_rule))
emit_and_print_errors(grammar)
print('Done')
Exemplo n.º 29
0
    )

    @langkit_property(public=True, return_type=T.BoolType)
    def test_env(other=T.FooNode.entity):
        return Self.children_env.env_orphan == other.children_env.env_orphan

    @langkit_property(public=True, return_type=T.BoolType)
    def test_struct(other=T.FooNode.entity):
        return Self.env_struct == other.env_struct

    @langkit_property(public=True, return_type=T.BoolType)
    def test_array(other=T.FooNode.entity):
        return Self.env_array == other.env_array


class Ref(FooNode):
    name = Field()


fg = Grammar('main_rule')
fg.add_rules(
    main_rule=List(fg.decl),
    decl=Decl(
        Opt('+').as_bool(HasPlus), Tok(Token.Identifier, keep=True), '(',
        fg.ref_list, ')'),
    ref_list=List(fg.ref, empty_valid=True),
    ref=Ref(Tok(Token.Identifier, keep=True)),
)
build_and_run(fg, 'main.py')
print('Done')
Exemplo n.º 30
0
from lexer_example import Token
from utils import build_and_run


class FooNode(ASTNode):
    root_node = Property(Self.unit.root.as_bare_entity, public=True)


@abstract
class Expression(FooNode):
    pass


class Literal(Expression):
    tok = Field()


class Plus(Expression):
    left = Field()
    right = Field()


foo_grammar = Grammar('main_rule')
foo_grammar.add_rules(
    main_rule=Or(Plus(foo_grammar.atom, '+', foo_grammar.main_rule),
                 foo_grammar.atom),
    atom=Literal(Tok(Token.Number, keep=True)),
)
build_and_run(foo_grammar, 'main.py')
print('Done')