G.match, G.id, G.string_literal, G.block_string_literal, G.bool_literal, G.unit_literal, NullLiteral("null"), G.integer, G.anonymous_function, Pick("(", G.expr, ")"), G.if_then_else ), anonymous_function=AnonymousFunction( "(", List(G.param, empty_valid=True, sep=","), ")", "=>", Null(G.doc_node), G.expr ), block_expr=BlockExpr( "{", List(G.decl, sep=";", empty_valid=False), ";", c(), G.expr, "}" ), val_decl=ValDecl( Opt(G.decl_annotation), Opt(G.doc_node), "val", c(), G.id, "=", G.expr ), fun_decl=FunDecl( Opt(G.decl_annotation),
from __future__ import absolute_import, division, print_function from langkit.dsl import ASTNode, Field from langkit.parsers import Grammar, List, Null from lexer_example import Token from utils import build_and_run class FooNode(ASTNode): pass class DefNode(FooNode): name = Field() values = Field() class Identifier(FooNode): token_node = True g = Grammar('main_rule') g.add_rules(main_rule=List(g.def_node), def_node=DefNode('(', g.identifier, ',', Null(DefNode.list), ')'), identifier=Identifier(Token.Identifier)) build_and_run(g, ada_main='main.adb', generate_unparser=True) print('Done')
discriminant_spec=DiscriminantSpec(List(A.defining_id, sep=","), ":", A.type_expr, Opt(":=", A.expr)), discr_spec_list=List(A.discriminant_spec, sep=";"), discriminant_part=Or( KnownDiscriminantPart("(", A.discr_spec_list, ")"), UnknownDiscriminantPart("(", "<>", ")"), ), enum_literal_decl=EnumLiteralDecl(A.defining_id | DefiningName(A.char_literal)), formal_discrete_type_def=FormalDiscreteTypeDef("(", "<>", ")"), record_def=Or( RecordDef("record", A.component_list, "end", "record"), NullRecordDef( "null", "record", ComponentList(Null(List(A.component_item)), Null(A.variant_part)), ), ), range_spec=RangeSpec("range", A.discrete_range | A.name | A.box_expr), real_type_def=Or(A.floating_point_def, A.decimal_fixed_point_def, A.ordinary_fixed_point_def), sexpr_or_box=A.simple_expr | A.box_expr, ordinary_fixed_point_def=OrdinaryFixedPointDef( "delta", A.sexpr_or_box, Opt(A.range_spec), ), decimal_fixed_point_def=DecimalFixedPointDef("delta", A.sexpr_or_box, "digits", A.sexpr_or_box, Opt(A.range_spec)), floating_point_def=FloatingPointDef("digits", A.sexpr_or_box,
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')
Opt(A.range_spec)), range_constraint=RangeConstraint(A.range_spec), constraint=Or(A.digits_constraint, A.delta_constraint, A.range_constraint, A.index_constraint, A.discriminant_constraint), discriminant_spec=DiscriminantSpec(List(A.identifier, sep=","), ":", A.type_expr, Opt(":=", A.expr)), discr_spec_list=List(A.discriminant_spec, sep=";"), discriminant_part=Or( KnownDiscriminantPart("(", A.discr_spec_list, ")"), UnknownDiscriminantPart("(", "<>", ")"), ), enum_literal_decl=EnumLiteralDecl(A.identifier | A.char_literal), formal_discrete_type_def=FormalDiscreteTypeDef("(", "<>", ")"), record_def=Or( RecordDef("record", A.component_list, "end", "record"), NullRecordDef("null", "record", Null(ComponentList)), ), range_spec=RangeSpec("range", A.discrete_range | A.name | A.box_expr), real_type_def=Or(A.floating_point_def, A.decimal_fixed_point_def, A.ordinary_fixed_point_def), sexpr_or_box=A.simple_expr | A.box_expr, ordinary_fixed_point_def=OrdinaryFixedPointDef( "delta", A.sexpr_or_box, Opt(A.range_spec), ), decimal_fixed_point_def=DecimalFixedPointDef("delta", A.sexpr_or_box, "digits", A.sexpr_or_box, Opt(A.range_spec)), floating_point_def=FloatingPointDef("digits", A.sexpr_or_box, Opt(A.range_spec)),
KnownDiscriminantPart("(", A.discr_spec_list, ")"), UnknownDiscriminantPart("(", "<>", ")"), ), enum_literal_decl=EnumLiteralDecl( A.defining_id | DefiningName(A.char_literal) ), formal_discrete_type_def=FormalDiscreteTypeDef("(", "<>", ")"), record_def=Or( RecordDef("record", A.component_list, "end", "record"), NullRecordDef( "null", "record", ComponentList( Null(List(A.component_item)), Null(A.variant_part) ), ), ), range_spec=RangeSpec("range", A.discrete_range | A.name | A.box_expr), real_type_def=Or(A.floating_point_def, A.decimal_fixed_point_def, A.ordinary_fixed_point_def), sexpr_or_box=A.simple_expr | A.box_expr, ordinary_fixed_point_def=OrdinaryFixedPointDef( "delta", A.sexpr_or_box, Opt(A.range_spec), ),
G.string_literal, G.block_string_literal, G.bool_literal, G.unit_literal, NullLiteral("null"), G.integer, G.anonymous_function, Pick("(", G.expr, ")"), G.if_then_else, G.block_expr, G.tuple_expr, ), tuple_expr=Tuple("(", List(G.expr, sep=","), ")"), anonymous_function=AnonymousFunction( "(", List(G.param, empty_valid=True, sep=","), ")", "=>", Null(G.doc_node), G.expr), block_expr=BlockExpr("{", List(G.decl, sep=";", empty_valid=False), ";", c(), G.expr, "}"), val_decl=ValDecl(Opt(G.decl_annotation), Opt(G.doc_node), "val", c(), G.id, "=", G.expr), fun_decl=FunDecl( Opt(G.decl_annotation), "fun", c(), G.id, NamedFunction("(", List(G.param, empty_valid=True, sep=","), ")", "=", Opt(G.doc_node), G.expr)), fun_call=FunCall(G.id, Safe("?"), "(", c(), G.arg_list, ")"), arg_list=List(G.arg, empty_valid=True, sep=","), selector_decl=SelectorDecl(Opt(G.decl_annotation), "selector", c(), G.id, Opt(G.doc_node), List(G.selector_arm, empty_valid=False)), selector_arm=SelectorArm( "|", G.pattern, "=>", List(G.selector_expr,
from __future__ import absolute_import, division, print_function from langkit.parsers import Grammar, List, Or, Pick, Null from language.lexer import dependz_lexer as L from language.ast import (Program, Introduction, Definition, Abstraction, Apply, SourceId, Arrow, Term) dependz_grammar = Grammar('main_rule') D = dependz_grammar dependz_grammar.add_rules(main_rule=List(D.toplevel, empty_valid=True, list_cls=Program), toplevel=Or(D.intro, D.definition), intro=Introduction(D.ident, ':', D.defterm, L.Newlines), definition=Definition(D.ident, '=', D.defterm, L.Newlines), ident=SourceId(L.Ident), defterm=Or( Or(Arrow(Null(Term), D.term, '->', D.defterm), Arrow(D.term, ':', D.term, '->', D.defterm)), D.term), term=Or(Apply(D.term, D.term1), D.term1), term1=Or(D.ident, D.term2), term2=Or(Abstraction('\\', D.ident, '.', D.term), D.term3), term3=Pick('(', D.defterm, ')'))
def lower(rule): """ Helper to lower one parser. :param liblktlang.GrammarExpr rule: Grammar rule to lower. :rtype: Parser """ # For convenience, accept null input rules, as we generally want to # forward them as-is to the lower level parsing machinery. if rule is None: return None loc = ctx.lkt_loc(rule) with ctx.lkt_context(rule): if isinstance(rule, liblktlang.ParseNodeExpr): node = resolve_node_ref(rule.f_node_name) # Lower the subparsers subparsers = [ lower(subparser) for subparser in rule.f_sub_exprs ] # Qualifier nodes are a special case: we produce one subclass # or the other depending on whether the subparsers accept the # input. if node._type.is_bool_node: return Opt(*subparsers, location=loc).as_bool(node) # Likewise for enum nodes elif node._type.base and node._type.base.is_enum_node: return _Transform(_Row(*subparsers, location=loc), node.type_ref, location=loc) # For other nodes, always create the node when the subparsers # accept the input. else: return _Transform(parser=_Row(*subparsers), typ=node, location=loc) elif isinstance(rule, liblktlang.GrammarToken): token_name = rule.f_token_name.text try: val = tokens[token_name] except KeyError: check_source_language( False, 'Unknown token: {}'.format(token_name)) match_text = '' if rule.f_expr: # The grammar is supposed to mainain this invariant assert isinstance(rule.f_expr, liblktlang.TokenLit) match_text = denoted_string_literal(rule.f_expr) return _Token(val=val, match_text=match_text, location=loc) elif isinstance(rule, liblktlang.TokenLit): return _Token(denoted_string_literal(rule), location=loc) elif isinstance(rule, liblktlang.GrammarList): return List(lower(rule.f_expr), empty_valid=rule.f_kind.text == '*', list_cls=resolve_node_ref(rule.f_list_type), sep=lower(rule.f_sep), location=loc) elif isinstance( rule, (liblktlang.GrammarImplicitPick, liblktlang.GrammarPick)): return Pick(*[lower(subparser) for subparser in rule.f_exprs], location=loc) elif isinstance(rule, liblktlang.GrammarRuleRef): return getattr(grammar, rule.f_node_name.text) elif isinstance(rule, liblktlang.GrammarOrExpr): return Or( *[lower(subparser) for subparser in rule.f_sub_exprs], location=loc) elif isinstance(rule, liblktlang.GrammarOpt): return Opt(lower(rule.f_expr), location=loc) elif isinstance(rule, liblktlang.GrammarOptGroup): return Opt(*[lower(subparser) for subparser in rule.f_expr], location=loc) elif isinstance(rule, liblktlang.GrammarExprList): return Pick(*[lower(subparser) for subparser in rule], location=loc) elif isinstance(rule, liblktlang.GrammarDiscard): return Discard(lower(rule.f_expr), location=loc) elif isinstance(rule, liblktlang.GrammarNull): return Null(resolve_node_ref(rule.f_name), location=loc) elif isinstance(rule, liblktlang.GrammarSkip): return Skip(resolve_node_ref(rule.f_name), location=loc) elif isinstance(rule, liblktlang.GrammarDontSkip): return DontSkip(lower(rule.f_expr), lower(rule.f_dont_skip), location=loc) elif isinstance(rule, liblktlang.GrammarPredicate): check_source_language( isinstance(rule.f_prop_ref, liblktlang.DotExpr), 'Invalid property reference') node = resolve_node_ref(rule.f_prop_ref.f_prefix) prop_name = rule.f_prop_ref.f_suffix.text try: prop = getattr(node, prop_name) except AttributeError: check_source_language( False, '{} has no {} property'.format( node._name.camel_with_underscores, prop_name)) return Predicate(lower(rule.f_expr), prop, location=loc) else: raise NotImplementedError('unhandled parser: {}'.format(rule))
# Non optional fields field_opt_bool = Field() field_transform = Field() class HasExample(FooNode): enum_node = True qualifier = True foo_grammar = Grammar('main_rule') foo_grammar.add_rules(main_rule=Or(foo_grammar.rule_1, foo_grammar.rule_2), rule_1=ExampleWrapper( Opt(Example("example")), Or(Example("example"), Null(Example)), foo_grammar.sub_rule, Null(Example), DontSkip(Opt(Example("example"))), Opt(Example("example")).as_bool(HasExample), Example("example")), rule_2=ExampleWrapper(Example("example"), Example("example"), Example("example"), Example("example"), Example("example"), HasExample("example"), Example("example")), sub_rule=Opt(Example("example"))) emit_and_print_errors(foo_grammar)
print('== {} =='.format(label)) @has_abstract_list class FooNode(ASTNode): pass class Example(FooNode): f = Field(FooNode) @synthetic class SynthExample(FooNode): pass @synthetic class ListSynthExample(FooNode.list): pass foo_grammar = Grammar('main_rule') foo_grammar.add_rules(main_rule=Example( 'example', parser_constructor(SynthExample, ListSynthExample))) emit_and_print_errors(foo_grammar) print('') run('List', lambda _, cls: List('example', list_cls=cls)) run('Null', lambda cls, _: Null(cls)) run('Skip', lambda cls, _: Skip(cls)) run('Transform', lambda cls, _: cls()) print('Done')
), discr_spec_list=List(A.discriminant_spec, sep=";"), discriminant_part=Or( KnownDiscriminantPart("(", A.discr_spec_list, ")"), UnknownDiscriminantPart("(", "<>", ")"), ), enum_literal_decl=EnumLiteralDecl(A.identifier | A.char_literal), formal_discrete_type_def=FormalDiscreteTypeDef("(", "<>", ")"), record_def=Or( RecordDef("record", A.component_list, "end", "record"), NullRecordDef("null", "record", Null(ComponentList)), ), range_spec=RangeSpec("range", A.discrete_range | A.name | A.box_expr), real_type_def=Or(A.floating_point_def, A.decimal_fixed_point_def, A.ordinary_fixed_point_def), sexpr_or_box=A.simple_expr | A.box_expr, ordinary_fixed_point_def=OrdinaryFixedPointDef( "delta", A.sexpr_or_box, Opt(A.range_spec), ), decimal_fixed_point_def=DecimalFixedPointDef( "delta", A.sexpr_or_box, "digits",
field_opt_bool = Field() field_transform = Field() class HasExample(FooNode): enum_node = True qualifier = True foo_grammar = Grammar('main_rule') foo_grammar.add_rules( main_rule=Or(foo_grammar.rule_1, foo_grammar.rule_2), rule_1=ExampleWrapper( Opt(Example("example")), Or(Example("example"), Null(Example)), foo_grammar.sub_rule, Null(Example), DontSkip(Opt(Example("example"))), Opt(Example("example")).as_bool(HasExample), Example("example") ), rule_2=ExampleWrapper( Example("example"), Example("example"), Example("example"), Example("example"), Example("example"), HasExample("example"),