ExprTools
package haxe.macro
Available on all platforms
This class provides some utility methods to work with expressions. It is best used through 'using haxe.macro.ExprTools' syntax and then provides additional methods on haxe.macro.Expr instances.
While mainly intended to be used in macros, it works in non-macro code as well.
Static methods
staticgetValue(e:Expr):Dynamic
Returns the value e
represents.
Supported expressions are:
-
Int
,Float
andString
literals - identifiers
true
,false
andnull
- structure declarations if all their fields are values
- array declarations if all their elements are values
- unary operators
-
,!
and~
if the operand is a value - binary operators except
=>
,...
and assignments
Parentheses, metadata and the untyped
keyword are ignored.
If any non-value is encountered, an exception of type String
is thrown.
If e
is null, the result is unspecified.
staticiter(e:Expr, f:Expr ‑> Void):Void
Calls function f
on each sub-expression of e
.
If e
has no sub-expressions, this operation has no effect.
Otherwise f
is called once per sub-expression of e
, with the sub-expression as argument. These calls are done in order of the sub-expression declarations.
This method does not call itself recursively. It should instead be used in a recursive function which handles the expression nodes of interest.
Usage example:
function findStrings(e:Expr) {
switch(e.expr) {
case EConst(CString(s)):
// handle s
case _:
ExprTools.iter(e, findStrings);
}
}
staticmap(e:Expr, f:Expr ‑> Expr):Expr
Transforms the sub-expressions of e
by calling f
on each of them.
If e
has no sub-expressions, this operation returns e
unchanged.
Otherwise f
is called once per sub-expression of e
, with the sub-expression as argument. These calls are done in order of the sub-expression declarations.
This method does not call itself recursively. It should instead be used in a recursive function which handles the expression nodes of interest.
Usage example:
function capitalizeStrings(e:Expr) {
return switch(e.expr) {
case EConst(CString(s)):
{ expr: EConst(CString(s.toUpperCase())), pos: e.pos };
case _:
ExprTools.map(e, capitalizeStrings);
}
}
statictoString(e:Expr):String
Converts expression e
to a human-readable String representation.
The result is guaranteed to be valid Haxe code, but there may be differences from the original lexical syntax.
© 2005–2020 Haxe Foundation
Licensed under a MIT license.
https://api.haxe.org/haxe/macro/ExprTools.html