[erlang-questions] erl_sytanx help
Richard Carlsson
carlsson.richard@REDACTED
Wed Jul 15 00:02:37 CEST 2015
This is a good opportunity to demonstrate the function erl_syntax:meta/1
(using merl in OTP 18 to simplify building the AST):
1> AST=merl:quote("-import(mymdule, [test1/0, test2/0])").
2> io:put_chars(erl_prettypr:format(erl_syntax:meta(AST))).
erl_syntax:make_tree(prefix_expr,
[[erl_syntax:operator('-')],
[erl_syntax:make_tree(application,
[[erl_syntax:atom(import)],
[erl_syntax:atom(mymdule),
erl_syntax:list([erl_syntax:make_tree(infix_expr,
[[erl_syntax:atom(test1)],
[erl_syntax:operator('/')],
[erl_syntax:integer(0)]]),
erl_syntax:make_tree(infix_expr,
[[erl_syntax:atom(test2)],
[erl_syntax:operator('/')],
[erl_syntax:integer(0)]])])]])]])
There's also the useful function merl:show/1 for inspecting the basic
structure of an AST:
3> merl:show(AST).
prefix_expr: -import(mymdule, [...])
operator: -
+
application: import(mymdule, [test1 / 0, test2 / 0])
atom: import
+
atom: mymdule
list: [test1 / 0, test2 / 0]
infix_expr: test1 / 0
atom: test1
+
operator: /
+
integer: 0
infix_expr: test2 / 0
atom: test2
+
operator: /
+
integer: 0
/Richard
On Tue, Jul 14, 2015 at 3:08 PM, Humberto Rodríguez Avila <
h87rguez@REDACTED> wrote:
> Hello guys!
>
> I am a new programming in Erlang and I interested in doing things with
> meta-programming. I have been working with merl and I did a couple of
> examples (module generation). Nevertheless, I have been trying to do the
> same thing using erl_syntax, but I can’t found how to build the syntax
> tree of the directive -import :(
>
> For example: -import(mymdule, [test1/0, test2/0])
>
> Any suggestion?
>
> Thanks in advance!
>
> --- Humberto
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150715/6b95d725/attachment.htm>
More information about the erlang-questions
mailing list