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
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('')
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
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())
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
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)
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')
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)
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')
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(
def end_liblevel_block(): return Pick("end", Opt(EndName(A.static_name)))
def recover(*rules): """ Helper to parse a sequence of rules, and ignore their result, but recover if they're absent. """ return Opt(*rules).error()
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,
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()
def end_named_block(): return recover("end", Opt(A.identifier))
def end_liblevel_block(): return recover("end", Opt(A.static_name))
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')
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',
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 ),
@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')
) @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')
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')
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,
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), ")") ),
""" 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')
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, ')')),
) @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')
def TrailList(el, sep, empty_valid=False): return Pick(List(el, sep=sep, empty_valid=empty_valid), Opt(sep))