ExPanda.EnvManager
(ExPanda v0.2.1)
View Source
Manages Macro.Env structs for macro expansion.
Handles environment construction, updates for compilation directives
(alias, import, require), module/function scoping, and variable
registration for :elixir_expand compatibility.
Summary
Functions
Apply an alias directive to the environment.
Apply an import directive to the environment.
Apply a require directive to the environment.
Create a child environment for a function scope.
Create a child environment for a module scope.
Extract parameter names from a function signature AST.
Create an environment from the caller's current context.
Create a fresh compilation environment using Elixir's internal env factory.
Set the file path in the environment.
Set the line number in the environment.
Register multiple variable names extracted from a pattern AST.
Register a variable name in the environment's versioned_vars.
Extract the module name from an alias AST node.
Functions
@spec apply_alias(Macro.Env.t(), module(), module() | nil) :: Macro.Env.t()
Apply an alias directive to the environment.
Handles both alias Foo.Bar (aliasing Bar) and alias Foo.Bar, as: Baz.
Examples
iex> env = ExPanda.EnvManager.new_env()
iex> env = ExPanda.EnvManager.apply_alias(env, Foo.Bar, nil)
iex> {Foo.Bar, Bar} in env.aliases or Keyword.get(env.aliases, :"Elixir.Bar") == Foo.Bar
true
@spec apply_import(Macro.Env.t(), module(), keyword()) :: Macro.Env.t()
Apply an import directive to the environment.
Loads the target module's functions and macros into the env. Returns the env unchanged if the module is not available.
@spec apply_require(Macro.Env.t(), module()) :: Macro.Env.t()
Apply a require directive to the environment.
Adds the module to the requires list if not already present.
@spec enter_function(Macro.Env.t(), atom(), non_neg_integer(), list()) :: Macro.Env.t()
Create a child environment for a function scope.
Sets the function name/arity and registers parameters as variables.
@spec enter_module(Macro.Env.t(), module()) :: Macro.Env.t()
Create a child environment for a module scope.
Sets the module name and resets function context.
Extract parameter names from a function signature AST.
Handles simple params, pattern params, default values, and guards.
Examples
iex> ExPanda.EnvManager.extract_param_names([{:x, [], nil}, {:y, [], nil}])
[:x, :y]
@spec from_caller(Macro.Env.t()) :: Macro.Env.t()
Create an environment from the caller's current context.
Useful when expanding code within a running application where all dependencies are already loaded.
@spec new_env() :: Macro.Env.t()
Create a fresh compilation environment using Elixir's internal env factory.
This produces a Macro.Env with Kernel functions and macros pre-loaded,
matching what a new module compilation would start with.
Examples
iex> env = ExPanda.EnvManager.new_env()
iex> env.__struct__
Macro.Env
iex> Kernel in env.requires
true
@spec put_file(Macro.Env.t(), String.t()) :: Macro.Env.t()
Set the file path in the environment.
@spec put_line(Macro.Env.t(), non_neg_integer()) :: Macro.Env.t()
Set the line number in the environment.
@spec register_pattern_vars(Macro.Env.t(), Macro.t()) :: Macro.Env.t()
Register multiple variable names extracted from a pattern AST.
Walks the pattern to find all variable bindings and registers them.
@spec register_var(Macro.Env.t(), atom()) :: Macro.Env.t()
Register a variable name in the environment's versioned_vars.
This is needed for :elixir_expand.expand/3 which raises on undefined variables.
@spec resolve_module_name(Macro.t(), Macro.Env.t()) :: module()
Extract the module name from an alias AST node.
Examples
iex> ExPanda.EnvManager.resolve_module_name({:__aliases__, [], [:Foo, :Bar]}, ExPanda.EnvManager.new_env())
Foo.Bar