View Source Sourceror.Identifier (Sourceror v1.5.0)

Functions to identify an classify forms and quoted expressions.

Summary

Functions

Checks if the given quoted form is an atomic literal in the AST.

Checks if the given identifier is a binary op.

Checks if the given quoted form is a call.

Checks if the given quoted form is an identifier, such as a variable.

Checks if the given identifier is a pipeline operator.

Checks if the given quoted form is a qualified call.

Checks if the given quoted form is a reserved block name.

Checks if the given identifier is an unary op.

Checks if the given quoted form is an unqualified call.

Checks if the given atom is a valid module alias.

Functions

Link to this macro

is_atomic_literal(quoted)

View Source (macro)
@spec is_atomic_literal(Macro.t()) :: Macro.t()

Checks if the given quoted form is an atomic literal in the AST.

This set includes numbers, atoms, and strings, but not collections like tuples, lists, or maps.

This guard returns true for literals that are the only elements inside of a :__block__, such as {:__block__, [], [:literal]}.

Examples

iex> is_atomic_literal(1)
true

iex> is_atomic_literal(1.0)
true

iex> is_atomic_literal(:foo)
true

iex> is_atomic_literal("foo")
true

iex> is_atomic_literal({:__block__, [], [1]})
true

iex> is_atomic_literal({:__block__, [], [1, 2]})
false

iex> is_atomic_literal({:__block__, [], [{:node, [], nil}]})
false

iex> is_atomic_literal('foo')
false
Link to this macro

is_binary_op(op)

View Source (macro)
@spec is_binary_op(Macro.t()) :: Macro.t()

Checks if the given identifier is a binary op.

Examples

iex> is_binary_op(:+)
true
Link to this macro

is_call(quoted)

View Source (macro)
@spec is_call(Macro.t()) :: Macro.t()

Checks if the given quoted form is a call.

Calls are any form of the shape {form, metadata, args} where args is a list, with the exception of blocks and aliases, which are identified by the forms :__block__ and :__aliases__.

Examples

iex> "node()" |> Sourceror.parse_string!() |> is_call()
true

iex> "Kernel.node()" |> Sourceror.parse_string!() |> is_call()
true

iex> "%{}" |> Sourceror.parse_string!() |> is_call()
true

iex> "@attr" |> Sourceror.parse_string!() |> is_call()
true

iex> "node" |> Sourceror.parse_string!() |> is_call()
false

iex> "1" |> Sourceror.parse_string!() |> is_call()
false

iex> "(1; 2)" |> Sourceror.parse_string!() |> is_call()
false

iex> "Macro.Env" |> Sourceror.parse_string!() |> is_call()
false
Link to this macro

is_identifier(quoted)

View Source (macro)
@spec is_identifier(Macro.t()) :: Macro.t()

Checks if the given quoted form is an identifier, such as a variable.

Examples

iex> "node" |> Sourceror.parse_string!() |> is_identifier()
true

iex> "node()" |> Sourceror.parse_string!() |> is_identifier()
false

iex> "1" |> Sourceror.parse_string!() |> is_identifier()
false
Link to this macro

is_pipeline_op(op)

View Source (macro)
@spec is_pipeline_op(Macro.t()) :: Macro.t()

Checks if the given identifier is a pipeline operator.

Examples

iex> is_pipeline_op(:|>)
true
Link to this macro

is_qualified_call(quoted)

View Source (macro)
@spec is_qualified_call(Macro.t()) :: Macro.t()

Checks if the given quoted form is a qualified call.

All unqualified calls would also return true if passed to is_call/1, but they have the shape {{:., dot_metadata, dot_args}, metadata, args}.

Examples

iex> "Kernel.node()" |> Sourceror.parse_string!() |> is_qualified_call()
true

iex> "__MODULE__.node()" |> Sourceror.parse_string!() |> is_qualified_call()
true

iex> "foo.()" |> Sourceror.parse_string!() |> is_qualified_call()
true

iex> "foo.bar()" |> Sourceror.parse_string!() |> is_qualified_call()
true

iex> "node()" |> Sourceror.parse_string!() |> is_qualified_call()
false

iex> "%{}" |> Sourceror.parse_string!() |> is_qualified_call()
false

iex> "@attr" |> Sourceror.parse_string!() |> is_qualified_call()
false

iex> "1" |> Sourceror.parse_string!() |> is_qualified_call()
false

iex> "(1; 2)" |> Sourceror.parse_string!() |> is_qualified_call()
false

iex> "Macro.Env" |> Sourceror.parse_string!() |> is_qualified_call()
false
Link to this macro

is_reserved_block_name(name)

View Source (macro)
@spec is_reserved_block_name(Macro.t()) :: Macro.t()

Checks if the given quoted form is a reserved block name.

Examples

iex> is_reserved_block_name(:do)
true

iex> is_reserved_block_name(:else)
true

iex> is_reserved_block_name(:catch)
true

iex> is_reserved_block_name(:after)
true

iex> is_reserved_block_name(:rescue)
true

iex> is_reserved_block_name(:foo)
false
Link to this macro

is_unary_op(op)

View Source (macro)
@spec is_unary_op(Macro.t()) :: Macro.t()

Checks if the given identifier is an unary op.

Examples

iex> is_unary_op(:+)
true
Link to this macro

is_unqualified_call(quoted)

View Source (macro)
@spec is_unqualified_call(Macro.t()) :: Macro.t()

Checks if the given quoted form is an unqualified call.

All unqualified calls would also return true if passed to is_call/1, but they have the shape {atom, metadata, args}.

Examples

iex> "node()" |> Sourceror.parse_string!() |> is_unqualified_call()
true

iex> "%{}" |> Sourceror.parse_string!() |> is_unqualified_call()
true

iex> "@attr" |> Sourceror.parse_string!() |> is_unqualified_call()
true

iex> "node" |> Sourceror.parse_string!() |> is_unqualified_call()
false

iex> "1" |> Sourceror.parse_string!() |> is_unqualified_call()
false

iex> "(1; 2)" |> Sourceror.parse_string!() |> is_unqualified_call()
false

iex> "Macro.Env" |> Sourceror.parse_string!() |> is_unqualified_call()
false

Checks if the given atom is a valid module alias.

Examples

iex> valid_alias?(Foo)
true
iex> valid_alias?(:foo)
false