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('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, 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=Or( Row('example') ^ BarNode, Row('example') ^ BazNode, )) return foo_grammar
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(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 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 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 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.defining_name, A.aspect_spec, "is", PublicPart(A.basic_decls.dont_skip(Or("private", "end"))), Opt("private", PrivatePart(A.basic_decls.dont_skip("end"))), end_liblevel_block(), sc())
def construct_derived(): class FooNode(ASTNode): pass class Example(FooNode): annotations = Annotations(ple_unit_root=True) class DerivedExample(Example): pass grammar = Grammar('main_rule') grammar.add_rules( main_rule=Or(Example('example', ','), DerivedExample('example', ';'))) return grammar
def construct_multiple(): class FooNode(ASTNode): pass class Example1(FooNode): annotations = Annotations(ple_unit_root=True) class Example2(FooNode): annotations = Annotations(ple_unit_root=True) grammar = Grammar('main_rule') grammar.add_rules( main_rule=Or(Example1('example', ','), Example2('example', ';'))) return grammar
def run(abstract_public, concrete_public): """ Emit and print the errors we get for the below grammar for the given privacy levels. """ fmt_privacy = { None: 'default', True: 'public', False: 'private', } print('== abstract: {}, concrete: {} =='.format( fmt_privacy[abstract_public], fmt_privacy[concrete_public] )) class RootNode(ASTNode): pass @abstract class AbstractNode(RootNode): prop = AbstractProperty(Bool, public=abstract_public) public_prop = Property(Self.prop, public=True) class ConcreteNode(AbstractNode): prop = Property(Literal(True), public=concrete_public) public_prop = Property(Self.prop, public=True) class OtherConcreteNode(AbstractNode): prop = Property(False) grammar = Grammar('main_rule') grammar.add_rules( main_rule=Or(ConcreteNode('example'), OtherConcreteNode('null')), ) if emit_and_print_errors(grammar): for fld in (AbstractNode.prop, ConcreteNode.prop): print(' {}: {}'.format(fld.qualname, fmt_privacy[fld.original_is_public])) print('')
def run(name, runtime_check=False, abstract_root_prop=False): print('== {} =='.format(name)) if abstract_root_prop: class FooNode(ASTNode): root_prop = AbstractProperty(T.Bool, public=True) @abstract class BaseNode(FooNode): root_prop = AbstractProperty(T.Bool, runtime_check=True) class Number(BaseNode): pass class Identifier(BaseNode): pass else: class FooNode(ASTNode): pass @abstract class BaseNode(FooNode): prop = AbstractProperty(T.Bool, public=True) class Number(BaseNode): prop = AbstractProperty(T.Bool, runtime_check=runtime_check) class Identifier(BaseNode): prop = AbstractProperty(T.Bool, runtime_check=runtime_check) grammar = Grammar('main_rule') grammar.add_rules( main_rule=List(Or( Number(Token.Number), Identifier(Token.Identifier), ))) emit_and_print_errors(grammar) print('')
add_to_env(mappings=New(T.env_assoc, key=Self.name.symbol, val=Self)), add_env(), reference(Self.imports.map(lambda i: i.cast(T.FooNode)), # If PropertyDef rewriting omits the following references, # env lookup will never reach DerivedRef.referenced_env, so # resolution will sometimes fail to reach definition. T.MiddleRef.referenced_env) ) grammar = Grammar('main_rule') grammar.add_rules( main_rule=List(Or( Def('def', Tok(Token.Identifier, keep=True), grammar.imports, grammar.vars, grammar.expr), grammar.expr )), imports=Pick('(', List(grammar.derived_ref, empty_valid=True), ')'), var=Var(Tok(Token.Identifier, keep=True), '=', grammar.expr), vars=Pick('{', List(grammar.var, empty_valid=True), '}'), expr=Or(grammar.atom, grammar.plus), atom=Or(grammar.lit, grammar.ref), lit=Lit(Tok(Token.Number, keep=True)), ref=Ref(Tok(Token.Identifier, keep=True)), derived_ref=DerivedRef(Tok(Token.Identifier, keep=True)),
from lexer_example import Token from utils import build_and_run class FooNode(ASTNode): pass class BarNode(FooNode): pass class Literal(FooNode): token_node = True a = AbstractProperty(runtime_check=True, type=FooNode.entity) b = Property(Self.a.cast(BarNode.entity)) c = Property(Self.b, public=True) d = Property(Self.a.cast(BarNode), type=BarNode.entity, public=True) foo_grammar = Grammar('main_rule') foo_grammar.add_rules(main_rule=Or(Literal(Token.Number), BarNode('example')), ) build_and_run(foo_grammar, 'main.py') print('Done')
@langkit_property(external=True, uses_entity_info=False, uses_envs=False) def eval(): pass 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() g = Grammar('main_rule') g.add_rules( main_rule=List(g.var_decl), var_decl=VarDecl('var', g.name, '=', g.expr, ';'), expr=Or(Addition(g.expr, '+', g.expr), g.atom), atom=Or(g.number, g.ref), number=Number(Token.Number), ref=Ref(g.name), name=Name(Token.Identifier), ) build_and_run(g, ada_main=['main.adb']) print('Done')
pass @abstract class Expression(FooNode): result = AbstractProperty(type=LongType) class Literal(Expression): tok = Field() result = ExternalProperty() class Plus(Expression): left = Field() right = Field() result = Property(Self.left.result + Self.right.result) foo_grammar = Grammar('main_rule') foo_grammar.add_rules( main_rule=Or( Row(foo_grammar.atom, '+', foo_grammar.main_rule) ^ Plus, foo_grammar.atom), atom=Row(Tok(Token.Number, keep=True)) ^ Literal, ) build_and_run(foo_grammar, 'main.py') print 'Done'
class AdaAccessSubp(AdaPreludeNode): subp_kind = Field(type=AdaSubpKind) skips = Field(type=T.AdaSkip.list) class AdaPrelude(AdaPreludeNode): 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",
@abstract class Decl(FooNode): name = Field(Identifier) @langkit_property(public=True) def prop(arg=(Bool, False)): return arg class VarDecl(Decl): @langkit_property(public=True) def prop(arg=(Bool, False)): return Not(arg) class FunDecl(Decl): pass grammar = Grammar('main_rule') grammar.add_rules( main_rule=List(Or(grammar.var_decl, grammar.fun_decl)), var_decl=VarDecl('var', grammar.identifier), fun_decl=FunDecl('def', grammar.identifier), identifier=Identifier(Token.Identifier), ) build_and_run(grammar, 'main.py') print('') print('Done')
token_node = True @abstract class Expr(FooNode): pass class Number(Expr): token_node = True class Ref(Expr): name = Field() g = Grammar('main_rule') g.add_rules( main_rule=List(g.decl), decl=Or(g.var_decl, g.fun_decl), var_decl=VarDecl(VarKeyword('var'), g.name, '=', g.expr, ';'), fun_decl=FunDecl('def', g.name, ';'), expr=Or(g.number, g.ref), number=Number(Token.Number), ref=Ref(g.name), name=Name(Token.Identifier), ) build_and_run(g, ada_main=['main.adb']) print('Done')
return Self.arms.at(n - 1).expr() @langkit_property(return_type=BasePattern.entity.array, public=True) def patterns(): """ Return a list of the patterns that appear n the match expressions's arms. """ return Self.arms.map(lambda x: x.pattern.as_entity) 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("select", c(), G.pattern), pattern=Or(FilteredPattern(G.unfiltered_pattern_optional_chain, "when", G.expr), G.unfiltered_pattern_optional_chain), unfiltered_pattern_optional_chain=Or( ChainedNodePattern( G.unfiltered_pattern, List(Or( SelectorLink(G.selector_call, "is", G.unfiltered_pattern), FieldLink(".", G.identifier, "=", G.unfiltered_pattern),
class Ref(Atom): tok = Field() prop1 = Property(3) prop2 = Property(3) prop3 = Property(3) class Plus(Expr): lhs = Field() rhs = Field() prop1 = Property(4) prop2 = Property(4) prop3 = Property(4) grammar = Grammar('main_rule') grammar.add_rules( main_rule=List(grammar.expr), expr=Or(grammar.atom, grammar.plus), atom=Or(grammar.lit, grammar.ref), lit=Lit(Tok(Token.Number, keep=True)), ref=Ref(Tok(Token.Identifier, keep=True)), plus=Pick('(', Plus(grammar.expr, '+', grammar.expr), ')'), ) emit_and_print_errors(grammar) print('Done')
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')
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('') run('Token element', main_rule=lambda T, _: T.ListNode(List(Token.Number))) run('Non-list list_cls', num=lambda T, _: T.Num(Token.Number), main_rule=lambda T, g: List(g.num, list_cls=T.ListNode)) run('Invalid element type in list_cls', example=lambda T, _: T.Example(Token.Example), num=lambda T, _: T.Num(Token.Number), main_rule=lambda T, g: Or(List(g.num, list_cls=T.Example.list), T.ListNode(g.example))) 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 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.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 ),
match_env_element = Property( Self.env_element.match( lambda l=T.Literal.entity: l, lambda n=T.Name.entity: n, lambda others: others, ), public=True ) class Plus(Expression): left = Field() right = Field() foo_grammar = Grammar('main_rule') foo_grammar.add_rules( main_rule=foo_grammar.expression, expression=Or( Pick('(', foo_grammar.expression, ')'), Plus(foo_grammar.atom, '+', foo_grammar.main_rule), foo_grammar.atom, ), atom=Or( Literal(Token.Number), Name(Token.Identifier), ), ) build_and_run(foo_grammar, 'main.py') print('Done')
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')
@abstract class Expression(FooNode): pass class Literal(Expression): token_node = True class Name(Expression): token_node = True class Plus(Expression): left = Field() right = Field() g = Grammar('main_rule') g.add_rules( main_rule=List(Pick(g.decl, ';')), decl=Or( VarDecl('def', g.name, '=', g.expr), FuncDecl('def', g.name, '(', List(g.name, sep=','), ')', '=', g.expr)), expr=Or(Plus(g.atom, '+', g.expr), g.atom), atom=Or(Literal(Token.Number), g.name), name=Name(Token.Identifier), ) build_and_run(g, 'main.py') print('Done')
result = ExternalProperty(uses_entity_info=False, uses_envs=False) class Name(Expression): tok = Field() designated_unit = ExternalProperty(type=AnalysisUnitType, uses_entity_info=False, uses_envs=True) result = Property(Self.designated_unit.root.cast(Expression).result) class Plus(Expression): left = Field() right = Field() result = Property(Self.left.result + Self.right.result) foo_grammar = Grammar('main_rule') foo_grammar.add_rules( main_rule=Or(Plus(foo_grammar.atom, '+', foo_grammar.main_rule), foo_grammar.atom), atom=Or( Literal(Tok(Token.Number, keep=True)), Name(Tok(Token.Identifier, keep=True)), ), ) build_and_run(foo_grammar, 'main.py') print('Done')