예제 #1
0
    @langkit_property(external=True, uses_entity_info=False, uses_envs=False)
    def eval():
        pass

    # 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)


class Ref(Expr):
    name = Field()

    @langkit_property(public=True)
    def referenced_var_decl():
        return (Self.node_env.get_first(Self.name)
                .cast_or_raise(T.VarDecl))

    @langkit_property()
    def eval():
        return Self.referenced_var_decl.eval()


build_and_run(lkt_file='expected_concrete_syntax.lkt', ada_main=['main.adb'])
print('Done')
예제 #2
0

class FooNode(ASTNode):
    lvar = UserField(T.LogicVar, public=False)


class Examples(FooNode):
    e1 = Field(type=T.RegularExample)
    e2 = Field(type=T.PlusExample)

    @langkit_property(public=True)
    def do_solving():
        return (
            Bind(Self.e1.lvar, Self.e2.lvar, conv_prop=PlusExample.conv_prop)
            & Self.e1.lvar.domain([Self.e1])
            & Self.e2.lvar.domain([Self.e2])).solve


class RegularExample(FooNode):
    pass


class PlusExample(FooNode):
    @langkit_property()
    def conv_prop():
        return No(T.RegularExample.entity)


build_and_run(lkt_file='expected_concrete_syntax.lkt', py_script='main.py')
print('Done')
예제 #3
0
파일: test.py 프로젝트: nyulacska/langkit
"""
This tests that the Langkit based python parser can be compiled and can parse a
simple python program without errors.

TODO: This is hackish and minimalistic. Since Langkit's python parser is a
fully fledged Langkit generated lib, it would make sense to reuse Libadalang's
parser test driver.
"""

from __future__ import absolute_import, division, print_function

import os
from os import path as P
import sys
from utils import build_and_run

LK_PYTHON_LIB_DIR = P.join(os.environ['LANGKIT_ROOT_DIR'], 'contrib', 'python')
sys.path.append(LK_PYTHON_LIB_DIR)

from language import lexer, parser

# RA22-015: This will also dump the python grammar, which is not dumped as part
# of another test, so keep it.
build_and_run(
    parser.python_grammar,
    "main.py",
    lexer=lexer.python_lexer,
)
예제 #4
0
"""

from langkit.dsl import ASTNode, T
from langkit.expressions import ArrayLiteral, If, String, langkit_property

from utils import build_and_run


class FooNode(ASTNode):
    pass


class Example(FooNode):
    @langkit_property(public=True, memoized=True, return_type=T.Int.array)
    def get_array():
        return ArrayLiteral([1, 2])

    @langkit_property(public=True, memoized=True, return_type=T.Int)
    def test_prop(numbers=T.Int.array, c=T.String):
        return If(c == String("one"), numbers.at(0), numbers.at(1))

    @langkit_property(public=True, memoized=True, return_type=T.Bool)
    def test_prop2(numbers=T.FooNode.entity.array):
        return numbers.length == 0


build_and_run(lkt_file='expected_concrete_syntax.lkt',
              ada_main='main.adb',
              lkt_semantic_checks=True)
print('Done')
예제 #5
0
    rhs = Field()

    @langkit_property()
    def evaluate_abstract():
        return Self.lhs.evaluate_abstract + Self.rhs.evaluate_abstract

    @langkit_property()
    def evaluate_rtcheck():
        return Self.lhs.evaluate_rtcheck + Self.rhs.evaluate_rtcheck

    @langkit_property(public=True)
    def evaluate_concrete():
        return Self.lhs.evaluate_concrete + Self.rhs.evaluate_concrete

    @langkit_property(public=True)
    def evaluate_entity():
        return Entity.lhs.evaluate_entity + Entity.rhs.evaluate_entity


class Def(FooNode):
    name = Field()
    expr = Field()

    env_spec = EnvSpec(add_to_env_kv(key=Self.name.symbol, value=Self))


build_and_run(lkt_file='expected_concrete_syntax.lkt',
              py_script='main.py',
              lkt_semantic_checks=True)
print('Done')
예제 #6
0
"""
Test that the low-level mechanism to reject calls to C imported functions with
incorrect number of arguments works as expected.
"""

from __future__ import absolute_import, division, print_function

from langkit.dsl import ASTNode
from langkit.parsers import Grammar

from utils import build_and_run


class FooNode(ASTNode):
    pass


class Example(FooNode):
    token_node = True


g = Grammar('main_rule')
g.add_rules(main_rule=Example('example'))
build_and_run(g, 'main.py')
print('Done')
예제 #7
0
import os.path

from langkit.compiled_types import ASTNode, root_grammar_class
from langkit.diagnostics import Diagnostics
from langkit.envs import EnvSpec
from langkit.expressions import Self
from langkit.parsers import Grammar, Row

from utils import build_and_run

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


@root_grammar_class()
class FooNode(ASTNode):
    pass


class Example(FooNode):
    env_spec = EnvSpec(initial_env=Self.parent.parent.children_env)


foo_grammar = Grammar('main_rule')
foo_grammar.add_rules(main_rule=Row('example') ^ Example, )
build_and_run(foo_grammar, 'script.py')
예제 #8
0

class Example(FooNode):
    pass


class Null(FooNode):
    pass


class Var(FooNode):
    arg = Field(type=Sequence)


class Ident(FooNode):
    token_node = True

    @langkit_property(public=True, return_type=Symbol)
    def sym(sym=Symbol):
        return sym


class StringLiteral(FooNode):
    pass


build_and_run(lkt_file='expected_concrete_syntax.lkt',
              ocaml_main='main',
              symbol_canonicalizer=LibraryEntity('Pkg', 'Canonicalize'))
print('Done')
예제 #9
0
"""
Test that stack overflow migitations work as expected in parsing code.
"""

from langkit.dsl import ASTNode, Field

from utils import build_and_run


class FooNode(ASTNode):
    pass


class Example(FooNode):
    token_node = True


class Paren(FooNode):
    contents = Field(type=FooNode)


build_and_run(lkt_file="expected_concrete_syntax.lkt", py_script="main.py",
              types_from_lkt=True)
print("Done")
예제 #10
0
파일: test.py 프로젝트: shintakezou/langkit
class Expr(FooNode):
    pass


class Literal(Expr):
    token_node = True


class Ref(Expr):
    name = Field()


class ParentExpr(Expr):
    expr = Field()


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


g = Grammar('main_rule')
g.add_rules(main_rule=List(g.def_rule),
            name=Name(Token.Identifier),
            def_rule=Def('def', g.name, Opt('(', List(g.name, sep=','), ')'),
                         '=', g.expr),
            expr=Or(Plus(g.expr, '+', g.expr), ParentExpr('(', g.expr, ')'),
                    Ref(g.name), Literal(Token.Number)))
build_and_run(g, 'main.py', generate_unparser=True)
print('Done')
예제 #11
0
    env_spec = EnvSpec(
        add_to_env_kv(key=Self.name.symbol, value=Self),
        add_env(),
    )

    @langkit_property(public=True, return_type=T.Bool)
    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.Bool)
    def test_struct(other=T.FooNode.entity):
        return Self.env_struct == other.env_struct

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


class Ref(FooNode):
    name = Field()


build_and_run(
    lkt_file='expected_concrete_syntax.lkt',
    py_script='main.py',

    # FIXME: switch back to True, see U930-033
    lkt_semantic_checks=False)
print('Done')
예제 #12
0
class Var(FooNode):
    arg = Field(type=Sequence)


class Ident(FooNode):
    token_node = True

    @langkit_property(public=True, return_type=Symbol)
    def sym(sym=Symbol):
        return sym


class StringLiteral(FooNode):
    pass

foo_grammar = Grammar('main_rule')
foo_grammar.add_rules(
    main_rule=List(foo_grammar.node, list_cls=Sequence),
    node=Or(foo_grammar.example, foo_grammar.null, foo_grammar.var,
            foo_grammar.ident, foo_grammar.string),
    example=Example('example'),
    null=Null('null'),
    var=Var('var', '(', foo_grammar.main_rule, ')'),
    ident=Ident(Token.Identifier),
    string=StringLiteral(Token.String)
)

build_and_run(foo_grammar, ocaml_main='main',
              symbol_canonicalizer=LibraryEntity('Pkg', 'Canonicalize'))
print('Done')
예제 #13
0
Check that the Children_And_Trivia function works as expected.
"""

from langkit.dsl import ASTNode, Field

from utils import build_and_run


class FooNode(ASTNode):
    pass


class DeclError(FooNode):
    enum_node = True
    qualifier = True


class Identifier(FooNode):
    token_node = True


class Decl(FooNode):
    id = Field(type=Identifier)
    error = Field(type=DeclError)


build_and_run(lkt_file='expected_concrete_syntax.lkt',
              ada_main='main.adb',
              types_from_lkt=True)
print('Done')
예제 #14
0
    has_plus = Field()
    name = Field()
    items = Field()

    env_spec = EnvSpec(
        add_to_env_kv(key=Self.name.symbol,
                      val=Self,
                      metadata=New(Metadata, b=Self.has_plus.as_bool)))

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


class Ref(FooNode):
    name = Field()


fg = Grammar('main_rule')
fg.add_rules(
    main_rule=List(fg.decl),
    decl=Decl(Opt('+').as_bool(HasPlus), fg.name, '(', fg.ref_list, ')'),
    ref_list=List(fg.ref, empty_valid=True),
    ref=Ref(fg.name),
    name=Name(Token.Identifier),
)
build_and_run(fg, ada_main='main.adb')
print('Done')
예제 #15
0
파일: test.py 프로젝트: nyulacska/langkit
    inner = Field()


class Newline(FooNode):
    token_node = True


foo_grammar = Grammar('main_rule')
G = foo_grammar


def newlines():
    return _(List(G.newline, empty_valid=True))


foo_grammar.add_rules(newline=Newline(L.Newline),
                      expr=Or(G.call, G.identifier, G.indented),
                      identifier=Identifier(Token.Identifier),
                      call=Call(G.identifier, '(', newlines(),
                                List(G.call, sep=',', empty_valid=True),
                                newlines(), ')'),
                      indented=Indented(
                          newlines(), L.Indent,
                          List(newlines(), G.expr, newlines(), sep=L.Newline),
                          L.Dedent),
                      def_node=Def('def', G.identifier, L.Newline,
                                   List(G.indented)),
                      main_rule=List(newlines(), G.def_node, newlines()))
build_and_run(foo_grammar, 'main.py', lexer=foo_lexer)
print('Done')
예제 #16
0
파일: test.py 프로젝트: raph-amiard/langkit
from langkit.dsl import ASTNode, Field

from utils import build_and_run


class FooNode(ASTNode):
    pass


class DefNode(FooNode):
    name = Field()
    value = Field()


class Identifier(FooNode):
    token_node = True


class Number(FooNode):
    token_node = True


class ErrorDef(FooNode):
    pass


build_and_run(lkt_file='expected_concrete_syntax.lkt',
              ada_main='main.adb', generate_unparser=True)
print('Done')
예제 #17
0

@abstract
class Expr(FooNode):
    @langkit_property()
    def root1():
        return Self.super().concat([2])


class Name(Expr):
    token_node = True

    @langkit_property()
    def root1():
        return Self.super().concat([3])

    @langkit_property()
    def root2(a=T.String, b=T.String):
        return String("<").concat(
            Self.super(String("[").concat(a).concat(String("]")),
                       b=String("{").concat(b).concat(String("}")))).concat(
                           String(">"))

    @langkit_property()
    def root3():
        return Entity.super()


build_and_run(lkt_file="expected_concrete_syntax.lkt", py_script="main.py")
print("Done")
예제 #18
0
Test that assigning a default value to a dynamic variable generates the
expected public API.
"""

from __future__ import absolute_import, division, print_function

from langkit.dsl import ASTNode, Bool
from langkit.expressions import DynamicVariable, langkit_property
from langkit.parsers import Grammar

from utils import build_and_run

BoolVar = DynamicVariable('bool_var', Bool)


class RootNode(ASTNode):
    pass


class ExampleNode(RootNode):
    @langkit_property(public=True, dynamic_vars=[(BoolVar, True)])
    def prop():
        return BoolVar


g = Grammar('main_rule')
g.add_rules(main_rule=ExampleNode('example'))
build_and_run(g, py_script='main.py')

print('Done')
예제 #19
0
"""
Check that infinite recursion in memoized properties behaves as expected.
"""

from __future__ import absolute_import, division, print_function

from langkit.dsl import ASTNode, Bool
from langkit.expressions import Self, langkit_property
from langkit.parsers import Grammar

from utils import build_and_run


class FooNode(ASTNode):
    pass


class Example(FooNode):

    @langkit_property(public=True, memoized=True, return_type=Bool)
    def recurse():
        return Self.recurse


foo_grammar = Grammar('main_rule')
foo_grammar.add_rules(
    main_rule=Example('example'),
)
build_and_run(foo_grammar, ada_main='main.adb')
print('Done')
예제 #20
0
"""
Test that sloc-based token lookup works properly.
"""

from langkit.dsl import ASTNode

from utils import build_and_run, unparse_all_script


class FooNode(ASTNode):
    pass


class Example(FooNode):
    pass


build_and_run(lkt_file='expected_concrete_syntax.lkt',
              py_script='main.py',
              ada_main='main.adb',
              unparse_script=unparse_all_script)
print('Done')
예제 #21
0
class Decl(FooNode):
    has_plus = Field()
    name = Field()
    items = Field()

    env_spec = EnvSpec(
        add_to_env(mappings=New(T.env_assoc, key=Self.name.symbol, val=Self),
                   metadata=New(Metadata, b=Self.has_plus.as_bool)))

    @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()


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', properties_logging=True)
print('Done')
예제 #22
0
파일: test.py 프로젝트: briot/langkit
    sym = Property(Self.tok.symbol, type=T.SymbolType)
    resolve = Property(Self.parent.node_env.get(Self.sym).at(0),
                       type=T.FooNode.entity)


class Def(FooNode):
    name = Field(type=T.Name)
    ref = Field(type=T.Name)

    env_spec = EnvSpec(
        add_to_env(
            mappings=New(T.env_assoc, key=Self.name.sym, val=Self),
            metadata=New(Metadata, node=Self.ref.then(
                lambda r: r.resolve.el,
                default_val=No(T.FooNode)
            ))
        )
    )


grammar = Grammar('main_rule')
grammar.add_rules(
    main_rule=List(grammar.def_rule),
    def_rule=Def(grammar.name, Opt('+', grammar.name)),
    name=Name(Tok(Token.Identifier, keep=True))
)

build_and_run(grammar, 'main.py')
print('')
print('Done')
예제 #23
0
파일: test.py 프로젝트: pmderodat/langkit
the expected_concrete_syntax.lkt file.
"""

from langkit.dsl import ASTNode, Field

from utils import build_and_run


class FooNode(ASTNode):
    pass


class Def(FooNode):
    id = Field()


class VarDef(FooNode):
    id = Field()


class Block(FooNode):
    el = Field()


class Id(FooNode):
    token_node = True


build_and_run(lkt_file='foo.lkt', py_script='main.py')
print('Done')
예제 #24
0
파일: test.py 프로젝트: pmderodat/langkit
"""

from langkit.dsl import ASTNode, Field

from utils import build_and_run


class FooNode(ASTNode):
    pass


class Def(FooNode):
    name = Field()


class BasicVar(FooNode):
    name = Field()


class Var(FooNode):
    basic_var = Field()
    value = Field()


class Name(FooNode):
    token_node = True


build_and_run(lkt_file="foo.lkt", ada_main="main.adb", unparse_script=None)
print('Done')
예제 #25
0
파일: test.py 프로젝트: shintakezou/langkit
class MyStruct(Struct):
    entity_field = UserField(type=T.FooNode)
    array_field = UserField(type=T.FooNode.entity.array)
    bigint_field = UserField(type=T.BigInt)


class Example(FooNode):
    token_node = True

    @langkit_property(public=True)
    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))

    @langkit_property(public=True)
    def struct_identity(s=MyStruct):
        return s


grammar = Grammar('main_rule')
grammar.add_rules(main_rule=List(Example('example')))

build_and_run(grammar, py_script='main.py', ada_main='main.adb')
print('')
print('Done')
예제 #26
0
파일: test.py 프로젝트: eliericha/langkit
    defs = Field()

    env_spec = EnvSpec(
        add_to_env(New(T.env_assoc, key=Self.name.symbol, val=Self)),
        add_env(),
        do(If(Self.error.as_bool, PropertyError(T.FooNode), No(T.FooNode))),
    )


class Var(DefNode):
    name = Field()
    value = Field()

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


G = Grammar('main_rule')
G.add_rules(
    main_rule=G.defs,
    defs=List(G.def_rule, empty_valid=True),
    def_rule=Or(G.scope, G.var),
    scope=Scope(
        Opt('error').as_bool(HasError), Id(Token.Identifier), '{', G.defs,
        '}'),
    var=Var(Id(Token.Identifier), '=', G.name),
    name=Or(Prefix(G.name, '.', Id(Token.Identifier)), Id(Token.Identifier)),
)
build_and_run(G, 'main.py')
print('Done')
예제 #27
0
"""
Test that getting a unit using different filenames for the same file return the
same unit (i.e. that the filename is canonicalized).
"""

from langkit.dsl import ASTNode, abstract
from langkit.expressions import Property

from utils import build_and_run


class FooNode(ASTNode):
    pass


@abstract
class EnumNode(FooNode):
    prop = Property(True, public=True)


class HasExample(EnumNode):
    enum_node = True
    qualifier = True


build_and_run(lkt_file='expected_concrete_syntax.lkt',
              py_script='main.py',
              types_from_lkt=True)
print('Done')
예제 #28
0
파일: test.py 프로젝트: briot/langkit
    @langkit_property(public=True)
    def count(seq=T.Example.entity.array):
        return seq.length


class Sequence(FooNode.list):
    all_items = Property(Entity.map(lambda i: i), public=True)
    example_items = Property(Entity.filtermap(
        lambda i: i.cast_or_raise(T.Example), lambda i: i.is_a(T.Example)),
                             public=True)


class Example(FooNode):
    pass


class Null(FooNode):
    pass


foo_grammar = Grammar('main_rule')
foo_grammar.add_rules(
    main_rule=List(foo_grammar.node, list_cls=Sequence),
    node=Or(foo_grammar.example, foo_grammar.null),
    example=Example(Tok(Token.Example)),
    null=Null(Tok(Token.Null)),
)

build_and_run(foo_grammar, 'main.py')
print('Done')
예제 #29
0
    @langkit_property()
    def control_flow_helper(item=T.FooNode.array):
        return item.length + 1

    @langkit_property(public=True)
    def test_control_flow(i=T.Int):
        nodes = Var(Self.parent.children)
        arr = Var(
            nodes.map(lambda n: n.parents().length + Let(
                lambda item=n.children: Self.control_flow_helper(item))))
        return i + arr.length


class Example(FooNode):
    token_node = True

    env_spec = EnvSpec(add_env())


# Build the generated library and the Ada test program
build_and_run(lkt_file="expected_concrete_syntax.lkt", ada_main="main.adb")

# Run the test program under GDB to check the helpers. We keep this part in
# separate scripts to make it convenient, for debugging pruposes, to run these
# checks without re-building the library/program.
for script in ["check_printers.py", "check_control_flow.py"]:
    subprocess.check_call([sys.executable, script])

print("Done")
예제 #30
0
파일: test.py 프로젝트: AdaCore/langkit
    id = Field()
    body = Field()

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


class Block(Stmt):
    items = Field()

    env_spec = EnvSpec(add_env=True)


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)
)


build_and_run(foo_grammar, 'script.py', library_fields_all_public=True)
예제 #31
0
    name = Field()
    args = Field()
    expr = Field()


@abstract
class Expr(FooNode):
    pass


class Literal(Expr):
    token_node = True


class Ref(Expr):
    name = Field()


class ParentExpr(Expr):
    expr = Field()


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


build_and_run(lkt_file='expected_concrete_syntax.lkt', py_script='main.py',
              generate_unparser=True)
print('Done')