Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

Erlang module's syntax tree with epp:parse_file is not full and erl_parse:parse_form gives an error

I'm trying to build a script to extract some pieces of the erlang code from the source files, but my approaches don't work. Here is what I tried:

{ok, Forms} = epp:parse_file("src/day_tasks.erl", [{includes, "include"}]), 
file:write_file("/projects/result.txt",
io_lib:format("~p", [Forms])).

This kind of worked, the AST was built, but some functions are missing in the output. The attributes for them are in place though. Instead of them I see a lot of tuples like this inside of the built syntax tree:

{error,{10,epp,{include,file,"cards.hrl"}}},
{error,{320,epp,{undefined,'LONG_TASK',none}}}

And I know that it where include attribute and where is a macro defined in the module and used in the function's body. What I don't know though is what can I do about it.

I tried another approach to get the tree, it looks like this:

{ok, Data} = file:read_file("src/day_tasks.erl"), 
{ok, Tokens, _Lines} = erl_scan:string(binary_to_list(Data)), 
{ok, Abs} = erl_parse:parse_form(Tokens), 
file:write_file("/projects/result1.txt", io_lib:format("~p", [Abs])).

But erl_parse:parse_form(Tokens) fails with the error:

** exception error: no match of right hand side value {error,{6,erl_parse,["syntax error before: ","'-'"]}}

The 6-th line is the line where the

-compile(export_all).

module attribute resides, right after module name attribute. So here I am, I'm stuck and don't know what else can I do.

What is the right way to get fully built syntax tree for the module?

question from:https://stackoverflow.com/questions/65852473/erlang-modules-syntax-tree-with-eppparse-file-is-not-full-and-erl-parseparse

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
epp:parse_file("src/day_tasks.erl", [{includes, "include"}]),

The second element of the includes tuple is defined to be the type IncludePath, which is a list:

Options = 
    [{includes, IncludePath :: [DirectoryName :: file:name()]} |
    ...

file:name() = string() | atom() | deep_list()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...