Example #1
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
Example #2
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('')
Example #3
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
Example #4
0
def package_decl_factory(dest_class):
    """
    Factory for creating a grammar rule that parses package declarations. Used
    to be able to generate both PackageDecl and BasePackageDecl instances.

    :rtype: Parser
    """
    return dest_class("package", A.static_name, A.aspect_spec, "is",
                      PublicPart(A.basic_decls),
                      Opt("private", PrivatePart(A.basic_decls)),
                      end_liblevel_block(), sc())
Example #5
0
def construct_ple_unit_root_field():
    class FooNode(ASTNode):
        pass

    class Example(FooNode):
        annotations = Annotations(ple_unit_root=True)
        child = Field()

    grammar = Grammar('main_rule')
    grammar.add_rules(main_rule=List(grammar.example),
                      example=Example('example', Opt(grammar.example)))
    return grammar
Example #6
0
    def _create_parser(cls, typeref, *args):
        """
        Construct a parser for this EnumNode subclass.
        """
        from langkit.parsers import _Row, _Transform, Opt

        if cls._qualifier:
            # If the node is a boolean node, then we want to parse the
            # sub-parsers as an optional parser that will be booleanized.
            return Opt(*args).as_bool(cls)

        else:
            # Otherwise, we want to parse the sub-parsers as a row + transform
            return _Transform(_Row(*args), typeref)
Example #7
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')
Example #8
0
 def create_parser_bool_node(cls, *args):
     # If the node is a boolean node, then we want to parse the
     # sub-parsers as an optional parser that will be booleanized.
     return Opt(*args).as_bool(cls)
Example #9
0
    pass


class Name(FooNode):
    token_node = True


class Def(FooNode):
    name = Field()
    defs = Field()
    values = Field()


class Values(FooNode):
    items = Field()


class Number(FooNode):
    token_node = True


g = Grammar('main_rule')
g.add_rules(
    main_rule=List(g.def_rule),
    def_rule=Def('def', g.name, Opt(g.def_rule), g.values),
    values=Values(List(Number(Token.Number), empty_valid=True)),
    name=Name(Token.Identifier),
)
build_and_run(g, ada_main='main.adb')
print('Done')
Example #10
0
    context_clauses = Field(type=T.AdaContextClause.list)
    library_item = Field(type=T.AdaLibraryItem)


A.add_rules(
    project_qualifier=Or(
        ProjectQualifier.alt_abstract("abstract"),
        ProjectQualifier.alt_library(Lex.Identifier("library")),
        ProjectQualifier.alt_aggregate_library(Lex.Identifier("aggregate"),
                                               Lex.Identifier("library")),
        ProjectQualifier.alt_aggregate(Lex.Identifier("aggregate")),
        ProjectQualifier.alt_configuration(Lex.Identifier("configuration")),
        ProjectQualifier.alt_standard(Lex.Identifier("standard")),
    ),
    project_extension=ProjectExtension("extends",
                                       Opt("all").as_bool(AllQualifier),
                                       A.string_literal),
    project_declaration=ProjectDeclaration(
        Opt(A.project_qualifier),
        Lex.Identifier(match_text="project"),
        A.static_name,
        Opt(A.project_extension),
        "is",
        A.declarative_items,
        "end",
        A.static_name,
        ";",
    ),
    project=Project(
        A.context_clauses,
        A.project_declaration,

class Tuple(Expr):
    """
    Tuple expression.
    """
    exprs = Field(type=Expr.list)


lkql_grammar = Grammar('main_rule')
G = lkql_grammar

# noinspection PyTypeChecker
lkql_grammar.add_rules(
    main_rule=List(Or(G.decl, G.expr), list_cls=TopLevelList),
    query=Query(Opt("from", Or(G.expr, Unpack("*", G.expr))), "select", c(),
                G.pattern),
    pattern=Or(
        ChainedNodePattern(
            G.filtered_pattern,
            List(
                Or(SelectorLink(G.selector_call, "is", G.filtered_pattern),
                   FieldLink(".", G.id, "is", G.filtered_pattern),
                   PropertyLink(".", G.fun_call, "is", G.filtered_pattern)))),
        G.filtered_pattern),
    filtered_pattern=Or(FilteredPattern(G.binding_pattern, "when", G.expr),
                        G.binding_pattern),
    binding_pattern=Or(BindingPattern(G.id, "@", G.value_pattern),
                       G.value_pattern),
    value_pattern=Or(
        ExtendedNodePattern(
Example #12
0
def end_liblevel_block():
    return Pick("end", Opt(EndName(A.static_name)))
Example #13
0
def recover(*rules):
    """
    Helper to parse a sequence of rules, and ignore their result, but recover
    if they're absent.
    """
    return Opt(*rules).error()
Example #14
0
    return Opt(*rules).error()


def end_liblevel_block():
    return Pick("end", Opt(EndName(A.static_name)))


def end_named_block():
    return Pick("end", Opt(EndName(A.identifier)))


A.add_rules(
    parent_list=List(A.static_name, sep="and", list_cls=ParentList),
    protected_type_decl=ProtectedTypeDecl(res("protected"), "type",
                                          A.defining_id,
                                          Opt(A.discriminant_part),
                                          A.aspect_spec, "is",
                                          Opt("new", A.parent_list,
                                              "with"), A.protected_def, sc()),
    protected_op=Or(A.subp_decl, A.entry_decl, A.aspect_clause, A.pragma),
    protected_el=Or(A.protected_op, A.component_decl),
    protected_def=ProtectedDef(
        PublicPart(List(A.protected_op, empty_valid=True, list_cls=DeclList)),
        Opt(
            "private",
            PrivatePart(
                List(A.protected_el, empty_valid=True, list_cls=DeclList))),
        end_named_block()),
    protected_decl=SingleProtectedDecl(res("protected"), A.defining_id,
                                       A.aspect_spec, "is",
                                       Opt("new", A.parent_list,
Example #15
0

def end_liblevel_block():
    return Pick("end", Opt(EndName(A.static_name)))


def end_named_block():
    return Pick("end", Opt(EndName(A.identifier)))


A.add_rules(
    parent_list=List(A.static_name, sep="and", list_cls=ParentList),

    protected_type_decl=ProtectedTypeDecl(
        res("protected"),
        "type", A.defining_id, Opt(A.discriminant_part),
        A.aspect_spec,
        "is", Opt("new", A.parent_list, "with"),
        A.protected_def, sc()
    ),

    protected_op=Or(A.subp_decl, A.entry_decl, A.aspect_clause, A.pragma),
    protected_el=Or(A.protected_op, A.component_decl),

    protected_def=ProtectedDef(
        PublicPart(List(A.protected_op,
                        empty_valid=True, list_cls=DeclList)),
        Opt("private",
            PrivatePart(List(A.protected_el,
                             empty_valid=True, list_cls=DeclList))),
        end_named_block()
Example #16
0
def end_named_block():
    return recover("end", Opt(A.identifier))
Example #17
0
def end_liblevel_block():
    return recover("end", Opt(A.static_name))
Example #18
0

class FooNode(ASTNode):
    pass


class RootNode(FooNode):
    ident = Field()
    number = Field()


class Identifier(FooNode):
    token_node = True


class Number(FooNode):
    token_node = True


g = Grammar('main_rule')
g.add_rules(main_rule=List(
    Or(
        RootNode('def', Null(Identifier), Opt('{', Number(Token.Number), '}'),
                 ';'),
        RootNode('def', Opt('(', Identifier(Token.Identifier), ')'),
                 Null(Number), ';'),
    )))
build_and_run(g, ada_main='main.adb', generate_unparser=True)

print('Done')
Example #19
0
    expr = Field()


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


g = Grammar('main_rule')
g.add_rules(
    main_rule=List(g.def_rule, empty_valid=True),

    name=Name(Token.Identifier),

    def_rule=Def('def', g.name,
                 Opt('(', List(g.name, sep=','), ')'),
                 '=', g.expr),

    expr=Or(Plus(g.expr, '+', g.expr),
            ParenExpr('(', g.expr, ')'),
            Ref(g.name),
            Literal(Token.Number))
)
build_and_run(g, ada_main=['general_api.adb',
                           'revert.adb',
                           'rewrite.adb',
                           'rewrite_lists.adb',
                           'iter_units.adb',
                           'apply_error.adb',
                           'templates.adb',
                           'preserve_formatting.adb',
Example #20
0
def end_named_block():
    return Pick("end", Opt(EndName(A.identifier)))
lkql_grammar = Grammar('main_rule')
G = lkql_grammar

# noinspection PyTypeChecker
lkql_grammar.add_rules(
    main_rule=List(
        Or(G.import_clause, G.decl, G.expr),
        list_cls=TopLevelList, empty_valid=True
    ),

    import_clause=Import("import", G.id),

    query=Query(
        Opt(
            "from",
            Or(G.expr, Unpack("*", G.expr))
        ),
        "select", c(), Or(
            QueryKind.alt_first(L.Identifier(match_text="first")),
            QueryKind.alt_all(),
        ), G.pattern
    ),

    pattern=Or(
        OrPattern(
            G.chained_node_pattern,
            "or",
            G.pattern
        ),
        G.chained_node_pattern
    ),
Example #22
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')
Example #23
0
    )

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


fg = Grammar('main_rule')
fg.add_rules(
    main_rule=List(fg.decl),
    decl=Decl(
        Opt('+').as_bool(HasPlus), Name(Token.Identifier), '(', fg.ref_list,
        ')'),
    ref_list=List(fg.ref, empty_valid=True),
    ref=Ref(Name(Token.Identifier)),
)
build_and_run(fg, 'main.py')
print('Done')
Example #24
0

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,
              ada_main=[
                  'general_api.adb', 'revert.adb', 'rewrite.adb',
                  'rewrite_lists.adb', 'iter_units.adb', 'apply_error.adb',
                  'templates.adb', 'preserve_formatting.adb'
              ],
              generate_unparser=True)
print('Done')
Example #25
0
class AbstractPresent(GPRNode):
    pass


class OthersDesignator(GPRNode):
    pass


class Choices(GPRNode.list):
    pass


A.add_rules(
    context_clauses=List(A.with_decl, empty_valid=True),
    with_decl=WithDecl(
        Opt("limited").as_bool(Limited), "with", List(A.string_literal,
                                                      sep=","), ";"),
    abstract_present=AbstractPresent("abstract"),
    qualifier_names=QualifierNames(A.identifier, Opt(A.identifier)),
    project_qualifier=ProjectQualifier(
        Or(A.abstract_present, A.qualifier_names)),
    project_extension=ProjectExtension("extends",
                                       Opt("all").as_bool(AllQualifier),
                                       A.string_literal),
    project_declaration=ProjectDeclaration(Opt(A.project_qualifier), "project",
                                           A.static_name,
                                           Opt(A.project_extension), "is",
                                           A.declarative_items, "end",
                                           A.static_name, ";"),
    project=Project(
        A.context_clauses,
Example #26
0
    node_pattern=Or(G.extended_node_pattern, G.node_kind_pattern),

    node_kind_pattern=NodeKindPattern(G.kind_name),

    detail_value=Or(DetailPattern(G.pattern), DetailExpr(G.expr)),

    extended_node_pattern=ExtendedNodePattern(
        Or(G.universal_pattern, G.node_kind_pattern),
        Pick("(", c(), List(G.node_pattern_detail, sep=","), ")")
    ),

    node_pattern_detail=Or(
        NodePatternSelector(
            SelectorCall(
                G.identifier,
                Opt(Pick(G.identifier, "@")),
                G.identifier,
                Opt("(", c(),
                    List(G.named_arg, sep=",", empty_valid=False), ")")
            ), "is", G.pattern
        ),
        NodePatternField(G.identifier, "=", c(), G.detail_value),
        NodePatternProperty(G.fun_call, "is", c(), G.detail_value)
    ),

    selector_call=SelectorCall(
        G.identifier,
        Opt(Pick(G.identifier, "@")),
        G.identifier,
        Opt("(", c(), List(G.named_arg, sep=",", empty_valid=False), ")")
    ),
Example #27
0
"""
Test that the various wrappers that the Python binding instantiates (contexts,
units, nodes) are re-used whenever we want to wrap unique C values.
"""

from __future__ import absolute_import, division, print_function

from langkit.dsl import ASTNode, Field
from langkit.parsers import Grammar, List, Opt

from utils import build_and_run


class FooNode(ASTNode):
    pass


class Example(FooNode):
    examples = Field()


g = Grammar('main_rule')
g.add_rules(main_rule=g.example_list,
            example_list=List(g.example),
            example=Example('example', Opt('(', g.example_list, ')')))
build_and_run(g, 'main.py')
print('Done')
Example #28
0

python_grammar = Grammar('main_rule')
P = python_grammar

python_grammar.add_rules(
    name=Id(L.Identifier),
    number=NumberLit(L.Number),
    string=StringLit(L.String),
    cat_string=ConcatStringLit(P.string, List(P.string)),
    nl=NL(L.Newline),
    main_rule=FileNode(
        List(newlines(), P.stmt, newlines()), L.Termination
    ),
    decorator=Decorator(
        '@', P.dotted_name, Opt('(', P.arg_list, ')'), L.Newline
    ),
    decorators=List(P.decorator),
    decorated=Decorated(P.decorators, Or(P.class_def, P.func_def)),
    func_def=FuncDef('def', P.name, P.parameters, ':', P.suite),
    parameters=Pick('(', Opt(P.varargslist), ')'),
    varargslist=Params(
        List(
            SingleParam(
                Opt('*').as_bool(VarArgsFlag), Opt('**').as_bool(KwArgsFlag),
                P.fpdef, Opt('=', P.test)
            ),
            empty_valid=True, sep=","
        ),
    ),
    fpdef=Or(P.name, Pick('(', P.name_list, ')')),
Example #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')
Example #30
0
def TrailList(el, sep, empty_valid=False):
    return Pick(List(el, sep=sep, empty_valid=empty_valid), Opt(sep))