class SingleParam(PythonNode): is_varargs = Field(type=T.VarArgsFlag) is_kwargs = Field(type=T.KwArgsFlag) name = Field(type=T.PythonNode) default_value = Field(type=T.Expr) env_spec = EnvSpec( add_to_env( Self.name.match( lambda i=T.Id: new_env_assoc(key=i.sym, val=Self).singleton, lambda l=T.Id.list: l.map(lambda i: new_env_assoc(key=i.sym, val=Self)), lambda _: No(T.env_assoc.array))))
def add_to_env_kv(key: AbstractExpression, val: AbstractExpression, dest_env: Optional[AbstractExpression] = None, metadata: Optional[AbstractExpression] = None, resolver: Optional[PropertyDef] = None, unsound: bool = False) -> AddToEnv: """ Specify a single element to add to the lexical environment. See langkit.expressions.envs.new_env_assoc for more precision about the first four arguments. :param resolver: Optional property that returns an AST node. If provided, the lexical environment lookup that will try to return the given mappings will first run this property on all nodes and return its result instead. :param unsound: Whether ``dest_env`` is allowed to return foreign environments. """ from langkit.expressions import new_env_assoc return add_to_env( mappings=new_env_assoc(key, val, dest_env, metadata), resolver=resolver, unsound=unsound, )
class Example(FooNode): token_node = True env_spec = EnvSpec( add_env(), add_to_env( new_env_assoc(key="foo", val=Self, dest_env=Self.children_env.env_orphan)))
class AssignStmt(Stmt): l_value = Field(type=T.Expr.list) r_values = Field(type=T.PythonNode.list) env_spec = EnvSpec( add_to_env(Self.l_value.filtermap( lambda e: new_env_assoc(key=e.cast_or_raise(T.Id).sym, value=Self), lambda e: e.is_a(T.Id), )) )
class Scope(FooNode): name = Field(type=T.Id) content = Field(type=T.FooNode.list) env_spec = EnvSpec( add_env(), handle_children(), add_to_env( Self.content.children.map(lambda r: new_env_assoc( key="Scope", val=Self, dest_env=r.match(lambda s=T.Scope: s.name.children_env, lambda _: r.children_env)))), )
def add_to_env_kv(key, val, dest_env=None, metadata=None, resolver=None): """ Specify a single element to add to the lexical environment. See langkit.expressions.envs.new_env_assoc for more precision about the first four arguments. :type key: AbstractExpression :type val: AbstractExpression :type dest_env: AbstractExpression :type metadata: AbstractExpression metadata :param PropertyDef|None resolver: Optional property that returns an AST node. If provided, the lexical environment lookup that will try to return the given mappings will first run this property on all nodes and return its result instead. """ from langkit.expressions import new_env_assoc return add_to_env(mappings=new_env_assoc(key, val, dest_env, metadata), resolver=resolver)
def add_to_env_by_name(key: AbstractExpression, val: AbstractExpression, name: AbstractExpression, fallback_env: AbstractExpression) -> AddToEnv: """Add an entry to a potentially foreign lexical env. The target lexical environment is: * If ``name`` returns a non-null env name, the environment that has this name (according to precedence rules). * Otherwise, the environment that the ``fallback_env`` expression returns. In that case, the environment cannot be foreign. """ from langkit.expressions import new_env_assoc return AddToEnv( mappings=new_env_assoc(key, val), resolver=None, name_expr=name, fallback_env_expr=fallback_env, unsound=False, )