def lang_def(): foo_grammar = Grammar('main_rule') foo_grammar.add_rules( main_rule=Or(Row('example') ^ ExampleNode, Row('null') ^ NullNode) ) return foo_grammar
def run(name, abstract_prop, prop=None): """ Emit and print the errors we get for the below grammar with `abstract_prop` as a property in AbstractExample and `prop` (if provided) as a property in Example. """ print('== {} =='.format(name)) class FooNode(ASTNode): pass @abstract class AbstractExample(FooNode): p = abstract_prop() public_p = Property(Self.p, public=True) class Example(AbstractExample): if prop: p = prop() grammar = Grammar('main_rule') grammar.add_rules(main_rule=Example('example'), ) emit_and_print_errors(grammar) print('')
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): token_node = True grammar = Grammar('main_rule') grammar.add_rules(main_rule=Or(Example('example'), Lit(Token.Number)), ) emit_and_print_errors(grammar) Env.unfreeze() print('')
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)
def run(token_cls): print('== {} =='.format(token_cls.__name__)) class FooNode(ASTNode): pass class Identifier(FooNode): token_node = True class Number(FooNode): token_node = True foo_lexer = Lexer(token_cls) foo_lexer.add_rules( (Pattern('[0-9]+'), token_cls.Number), (Pattern('[a-zA-Z_][a-zA-Z0-9_]*'), token_cls.Identifier), ) g = Grammar('main_rule') g.add_rules(main_rule=Or(Identifier(token_cls.Identifier), Number(token_cls.Number))) emit_and_print_errors(g, foo_lexer, generate_unparser=True) BaseToken.Number.name = None BaseToken.Identifier.name = None print('')
def run(name, arg_parent, arg_overriding): """ Emit and print the errors we get for the below grammar with "arg_parent" and "arg_overriding" "arg" as the first argument in FooNode.prop and Example.prop. """ print('== {} =='.format(name)) class FooNode(ASTNode): @langkit_property(public=True, return_type=BoolType, kind=AbstractKind.abstract) def prop(a=arg_parent): pass class Example(FooNode): @langkit_property() def prop(a=arg_overriding): return a grammar = Grammar('main_rule') grammar.add_rules(main_rule=Example('example'), ) emit_and_print_errors(grammar) print('')
def run(name, *pred_args): print('== {} =='.format(name)) class FooNode(ASTNode): pass class Example(FooNode): var1 = UserField(LogicVarType, public=False) var2 = UserField(LogicVarType, public=False) @langkit_property(warn_on_unused=False) def pred1(n=T.FooNode.entity): return n.is_null @langkit_property(warn_on_unused=False) def pred2(n=T.FooNode.entity, b=(BoolType, False)): return n.is_null & b @langkit_property(public=True) def prop(): ignore(Var(Predicate(*pred_args))) return Self.as_bare_entity grammar = Grammar('main_rule') grammar.add_rules(main_rule=Example('example'), ) emit_and_print_errors(grammar) print('')
def lang_def(): foo_grammar = Grammar('main_rule') foo_grammar.add_rules(main_rule=Or( Row('example') ^ BarNode, Row('example') ^ BazNode, )) return foo_grammar
def run(name, expr): """ Emit and print the errors we get for the below grammar with "expr" as a property in BarNode. """ global FooNode, BarNode, ListNode print('== {} =='.format(name)) class FooNode(ASTNode): pass class BarNode(FooNode): list_node = Field() class ListNode(FooNode): nb_list = Field() bar_node_parent = Property(Self.parent.cast(BarNode)) prop = Property(expr) public_bar_node_parent = Property(Self.bar_node_parent.as_bare_entity, public=True) public_prop = Property(Self.prop.as_bare_entity, 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('')
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('')
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): token_node = True grammar = Grammar('main_rule') grammar.add_rules( main_rule=BarNode('example', grammar.list_rule), list_rule=ListNode(List(NumberNode(Token.Number))), ) emit_and_print_errors(grammar) print('')
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, 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): token_node = True @synthetic class EmptyNode(FooNode): pass @synthetic 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=','), ')'), list_item=Literal(Token.Number), ) emit_and_print_errors(grammar) print('')
def lang_def(): foo_grammar = Grammar('main_rule') foo_grammar.add_rules( main_rule=Row('example', foo_grammar.rule_2) ^ BarCode, rule_2=Row('example') ^ BarNode, ) return foo_grammar
def run(name, **kwargs): print('== {} =='.format(name)) class FooNode(ASTNode): pass class Root(FooNode): f = Field() class Identifier(FooNode): token_node = True class Number(FooNode): token_node = True class Nodes(object): pass Nodes.Root = Root Nodes.Identifier = Identifier Nodes.Number = Number g = Grammar('main_rule') g.add_rules(**{name: parser_fn(Nodes, g) for name, parser_fn in kwargs.items()}) emit_and_print_errors(g, generate_unparser=True) print('')
def run(name, astnode_fn): """ Emit and print the errors we get for the below grammar with "match_expr" as a property in ExampleNode. """ print('== {} =='.format(name)) astnode = astnode_fn(T) @abstract class FooNode(ASTNode): pass @abstract class MiddleNode(FooNode): get_random_node = AbstractProperty(type=T.MiddleNode) public_prop = Property(Self.get_random_node.as_bare_entity, public=True) class ExampleNode(MiddleNode): get_random_node = Property(No(astnode)) class NullNode(FooNode): pass grammar = Grammar('main_rule') grammar.add_rules(main_rule=Or(ExampleNode('example'), NullNode('null'))) emit_and_print_errors(grammar) print('')
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))) ^ ListNode, ) return foo_grammar
def run(label, **kwargs): print('== {} =='.format(label)) class FooNode(ASTNode): annotations = Annotations(generic_list_type='FooList') class ListNode(FooNode): items = Field() class Example(FooNode): token_node = True class Num(FooNode): token_node = True class T: pass T.FooNode = FooNode T.ListNode = ListNode T.Example = Example T.Num = Num g = Grammar('main_rule') g.add_rules(**{name: parser(T, g) for name, parser in kwargs.items()}) emit_and_print_errors(g) print('')
def run(name, expr): """ Emit and print the errors we get for the below grammar with "expr" as a property in BarNode. """ global FooNode, BarNode, MyStruct print('== {} =='.format(name)) class MyStruct(Struct): a = UserField(type=Int) b = UserField(type=Int) class FooNode(ASTNode): pass class BarNode(FooNode): @langkit_property(public=True) def public_prop(): struct = Var(Self.prop) return struct.a + struct.b prop = Property(expr) grammar = Grammar('main_rule') grammar.add_rules(main_rule=BarNode('example'), ) emit_and_print_errors(grammar) print('')
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(): @root_grammar_class class FooNode(ASTNode): b = Property(lit) foo_grammar = Grammar("main_rule") foo_grammar.add_rules(main_rule=Row("example") ^ FooNode) return foo_grammar
def lang_def(): @root_grammar_class() class FooNode(ASTNode): b = Property(lit) foo_grammar = Grammar('main_rule') foo_grammar.add_rules(main_rule=Row('example') ^ FooNode) return foo_grammar
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
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('')
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 run(lit): class FooNode(ASTNode): pass class ExampleNode(FooNode): b = Property(lit, public=True) grammar = Grammar('main_rule') grammar.add_rules(main_rule=ExampleNode('example')) emit_and_print_errors(grammar)
def lang_def(): foo_grammar = Grammar('item') foo_grammar.add_rules( item=Or(foo_grammar.example, foo_grammar.example_list), example=Row('example') ^ Example, example_list=ExampleList( '(', List(foo_grammar.item), ')' ) ) return foo_grammar
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
def run(name, eq_prop): """ Emit and print the errors we get for the below grammar with "expr" as a property in BarNode. """ env = DynamicVariable('env', LexicalEnv) dyn_node = DynamicVariable('dyn_node', T.BazNode) print('== {} =='.format(name)) eq_prop = eval(eq_prop) class FooNode(ASTNode): ref_var = UserField(LogicVar, public=False) type_var = UserField(LogicVar, public=False) class BarNode(FooNode): main_prop = Property( env.bind(Self.node_env, Bind(Self.type_var, Self.ref_var, eq_prop=eq_prop))) @langkit_property(public=True) def wrapper(): _ = Var(Self.main_prop) ignore(_) return Self.as_bare_entity class BazNode(FooNode): prop = Property(12, warn_on_unused=False) prop2 = Property(True, warn_on_unused=False) @langkit_property(warn_on_unused=False) def prop3(_=T.BarNode): return True @langkit_property(warn_on_unused=False, dynamic_vars=[dyn_node]) def prop4(other=T.BazNode.entity): return other.node == dyn_node @langkit_property(warn_on_unused=False) def prop_a(other=T.BazNode.entity): return Self.as_entity == other @langkit_property(warn_on_unused=False, dynamic_vars=[env]) def prop_b(other=T.BazNode.entity): return other.node_env == env grammar = Grammar('main_rule') grammar.add_rules(main_rule=Or( BarNode('example'), BazNode('example'), )) emit_and_print_errors(grammar) print('')
def construct_non_root_list(): class FooNode(ASTNode): pass class Subunit(FooNode): annotations = Annotations(ple_unit_root=True) fields = Field() grammar = Grammar('main_rule') grammar.add_rules(main_rule=List(Subunit('example', grammar.main_rule))) return grammar
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 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
def run(name, prop): print('== {} =='.format(name)) class FooNode(ASTNode): pass class Example(FooNode): result = Property(prop) grammar = Grammar('main_rule') grammar.add_rules(main_rule=Example('example')) emit_and_print_errors(grammar) print('')
def construct_bad_main_rule_2(): class FooNode(ASTNode): pass class ExampleWrapper(FooNode): example = Field() class Example(FooNode): annotations = Annotations(ple_unit_root=True) grammar = Grammar('main_rule') grammar.add_rules(main_rule=List(ExampleWrapper(Example('example')))) return grammar
def lang_def(): @abstract @root_grammar_class class FooNode(ASTNode): pass class ExampleNode(FooNode): pass foo_grammar = Grammar('main_rulezz') foo_grammar.add_rules( main_rule=Row('example') ^ ExampleNode ) return foo_grammar
def lang_def(): @abstract @root_grammar_class class FooNode(ASTNode): pass class ExampleNode(FooNode): pass foo_grammar = Grammar('main_rule') foo_grammar.add_rules( sec_rule=Row('example'), ) foo_grammar.add_rules( sec_rule=Row('example'), main_rule=Row(foo_grammar.sec_rule) ^ ExampleNode )
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 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) ^ Number, expression=Or( Row(foo_grammar.number, ',', foo_grammar.expression) ^ Compound, foo_grammar.number ), ) return foo_grammar
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
from langkit.compiled_types import ( abstract, ASTNode, root_grammar_class, NodeMacro ) from langkit.envs import EnvSpec from langkit.expressions import Property, Self from langkit.parsers import Grammar from language.parser.lexer import ada_lexer ada_grammar = Grammar() A = ada_grammar ada_grammar.main_rule_name = "compilation_unit" @abstract @root_grammar_class class AdaNode(ASTNode): """ Root node class for the Ada grammar. This is good and necessary for several reasons: 1. It will facilitate the sharing of langkit_support code if we ever have two libraries generated by LanguageKit in the same application. 2. It allows to insert code specific to the ada root node, without polluting every LanguageKit node, and without bringing back the root ASTNode in the code templates. """ pass
def lang_def(): foo_grammar = Grammar("main_rule") foo_grammar.add_rules(main_rule=Row("example") ^ BarNode) return foo_grammar
def lang_def(): foo_grammar = Grammar('main_rule') foo_grammar.add_rules( main_rule=Row('example') ^ ConcreteNode, ) return foo_grammar
def lang_def(): foo_grammar = Grammar('main_rule') foo_grammar.add_rules(main_rule=Row('example') ^ BarNode) return foo_grammar