转载:http://agileprogrammer.blogspot.com/2006/08/erlang-abstract-form-moduleform.html
我们将具体考察Erlang Abstract Form的组成。方法很简单,参考Eralng文档的Abstract Form一节,用实际的例子加以验证。
模块声明由一系列Form组成,这些Form要么是函数声明,要么是属性(attribute)。A module declaration consists of a sequence of forms that are either function declarations or attributes.
- If D is a module declaration consisting of the forms
F_1
, ...,F_k
, then Rep(D) =[Rep(F_1), ..., Rep(F_k)]
.- If F is an attribute
-module(Mod)
, then Rep(F) ={attribute,LINE,module,Mod}
.- If F is an attribute
-export([Fun_1/A_1, ..., Fun_k/A_k])
, then Rep(F) ={attribute,LINE,export,[{Fun_1,A_1}, ..., {Fun_k,A_k}]}
.- If F is an attribute
-import(Mod,[Fun_1/A_1, ..., Fun_k/A_k])
, then Rep(F) ={attribute,LINE,import,{Mod,[{Fun_1,A_1}, ..., {Fun_k,A_k}]}}
.- If F is an attribute
-compile(Options)
, then Rep(F) ={attribute,LINE,compile,Options}
.- If F is an attribute
-file(File,Line)
, then Rep(F) ={attribute,LINE,file,{File,Line}}
.- If F is a record declaration
-record(Name,{V_1, ..., V_k})
, then Rep(F) ={attribute,LINE,record,{Name,[Rep(V_1), ..., Rep(V_k)]}}
. For Rep(V), see below.- If F is a wild attribute
-A(T)
, then Rep(F) ={attribute,LINE,A,T}
.
- If F is a function declaration
Name Fc_1 ; ... ; Name Fc_k
, where eachFc_i
is a function clause with a pattern sequence of the same lengthArity
, then Rep(F) ={function,LINE,Name,Arity,[Rep(Fc_1), ...,Rep(Fc_k)]}
.
-module(simplest).
Eshell V5.5 (abort with ^G)
1> c(simplest,[debug_info]).
{ok,simplest}
2> beam_lib:chunks(simplest, [abstract_code]).
{ok,{simplest,[{abstract_code,{raw_abstract_v1,
[{attribute,1,file,{"./simplest.erl",1}},
{attribute,1,module,simplest},
{eof,1}]}}]}}
beam_lib:chunks返回的abstract_code定义如下:
如果无法在beam文件中找到abstract form,那么将返回no_abstract_code。如果找到的话,则是一个tuple, tuple的第一项是版本,即我们上面例子中的raw_abstract_v1,tuple的第2项就是真正的form。因此,最简单的simplest beam文件中包含的Form如下:{ChunkName, DataT} = {abstract_code, AbstractCode} AbstractCode = {AbstVersion, Forms} | no_abstract_code AbstVersion = atom()
[{attribute,1,file,{"./simplest.erl",1}},
{attribute,1,module,simplest},
{eof,1}]
Abstract Form关于module声明Form的第一条说:
If D is a module declaration consisting of the forms这可以解释Form为什么是一个列表。F_1
, ...,F_k
, then Rep(D) =[Rep(F_1), ..., Rep(F_k)]
.
If F is an attribute-module(Mod)
, then Rep(F) ={attribute,LINE,module,Mod}
.
{attribute,1,module,simplest}
还有:
If F is an attribute这也说明了为什么会出现:-file(File,Line)
, then Rep(F) ={attribute,LINE,file,{File,Line}}
.
{attribute,1,file,{"./simplest.erl",1}}
尽管我们没有在源代码中编写-file属性,但是编译器还是在abstract code中加入了这个属性。
{eof,1}
4.1.2 Representation of parse errors and end of file
In addition to the representations of forms, the list that represents a module declaration (as returned by functions inerl_parse
andepp
) may contain tuples{error,E}
, denoting syntactically incorrect forms, and{eof,LINE}
, denoting an end of stream encountered before a complete form had been parsed.
-module(simplest). %1
-export([test/0]). %2
test() -> %3
ok. %4
5> c(simplest,[debug_info]).
{ok,simplest}
6> beam_lib:chunks(simplest, [abstract_code]).
{ok,{simplest,[{abstract_code,{raw_abstract_v1,
[{attribute,1,file,{"./simplest.erl",1}},
{attribute,1,module,simplest},
{attribute,2,export,[{test,0}]},
{function,
3,
test,
0,
[{clause,3,[],[],[{atom,4|...}]}]},
{eof,5}]}}]}}
If F is a function declarationfunction的Form共有5项,第一项是function这个atom,第二项是行号,第3项是函数的名字,第4项是函数参数的个数。最后一项是一个列表,包含每个子句的Rep。Name Fc_1 ; ... ; Name Fc_k
, where eachFc_i
is a function clause with a pattern sequence of the same lengthArity
, then Rep(F) ={function,LINE,Name,Arity,[Rep(Fc_1), ...,Rep(Fc_k)]}
.