这篇续前一篇,来介绍erl_parse这个重头模块。
先看看这个模块的三个重头方法:
parse_form(Tokens) -> {ok, AbsForm} | {error, ErrorInfo} Types: Tokens = [token()] AbsForm = abstract_form() ErrorInfo = error_info() This function parses Tokens as if it were a form. It returns: //针对form {ok, AbsForm} The parsing was successful. AbsForm is the abstract form of the parsed form. {error, ErrorInfo} An error occurred. parse_exprs(Tokens) -> {ok, ExprList} | {error, ErrorInfo} Types: Tokens = [token()] ExprList = [abstract_expr()] ErrorInfo = error_info() This function parses Tokens as if it were a list of expressions. It returns: //针对list {ok, ExprList} The parsing was successful. ExprList is a list of the abstract forms of the parsed expressions. {error, ErrorInfo} An error occurred. parse_term(Tokens) -> {ok, Term} | {error, ErrorInfo} Types: Tokens = [token()] Term = term() ErrorInfo = error_info() This function parses Tokens as if it were a term. It returns: //针对term {ok, Term} The parsing was successful. Term is the Erlang term corresponding to the token list. {error, ErrorInfo} An error occurred.
分别注意这三个方法是针对不同的tokens进行parse的。
下面给出两个例子
{ok, Tokens, EndLine} = erl_scan:string("test() -> ok."). {ok,[{atom,1,test}, {'(',1}, {')',1}, {'->',1}, {atom,1,ok}, {dot,1}], 1} erl_parse:parse_form(Tokens). {ok,{function,1,test,0,[{clause,1,[],[],[{atom,1,ok}]}]}}
{ok, Tokens2, EndLine2} = erl_scan:string("[a,b,c]."). {ok,[{'[',1}, {atom,1,a}, {',',1}, {atom,1,b}, {',',1}, {atom,1,c}, {']',1}, {dot,1}], 1} erl_parse:parse_exprs(Tokens2). {ok,[{cons,1, {atom,1,a}, {cons,1,{atom,1,b},{cons,1,{atom,1,c},{nil,1}}}}]} erl_parse:parse_term(Tokens2). {ok,[a,b,c]}
再看看下面这个不常用的方法
tokens(AbsTerm) -> Tokens tokens(AbsTerm, MoreTokens) -> Tokens Types: AbsTerm = abstract_expr() MoreTokens = Tokens = [token()] This function generates a list of tokens representing the abstract form AbsTerm of an expression. Optionally, it appends MoreTokens.
例子
Expr. {cons,1, {atom,1,a}, {cons,1,{atom,1,b},{cons,1,{atom,1,c},{nil,1}}}} erl_parse:tokens(Expr). [{'[',1}, {atom,1,a}, {',',1}, {atom,1,b}, {',',1}, {atom,1,c}, {']',1}]
其实就是把表达式重新解析成tokens格式
给出别人写的一个demo
-module(file_2). -export([compile/1]). -spec file_2:compile(Code) -> {ok, Module} | {error} when Code:: string(), Module :: module(). compile(Code) -> {ok, M, Bin} = compile:forms(scan_tokens(Code)), case code:load_binary(M, "nofile", Bin) of {module, Module} -> {ok, Module}; {error, _} -> {error} end. scan_tokens(Code) -> case erl_scan:tokens([], Code, 1) of {done, {ok, Token, _}, Remain} -> {ok,Form} = erl_parse:parse_form(Token), [Form | scan_tokens(Remain)]; {more, _} -> [] end.
相关推荐
erl_nif 扩展erlang的另外一种方法
erlang-erl_interface-19.3.6.4-1.el7.x86_64.rpm
Erlang otp_src_20.3.tar.gz linux centos otp-OTP-20.0.tar.gz 可兼容rabbitmq3.7.0
erlang-erl_docgen-19.3.6.4-1.el7.x86_64.rpm
erlang提供了binary_to_term 函数,用于把二进制数据转为原始的erlang数据。这个函数都是c实现的,这里用erlang语言实现了,很有参考价值,其他语言可以参考这个解析erlang二进制协议数据。配套文章地址...
otp_win64_22.1的官方地址下载太慢,共享一下otp_Erlang_win64_22.1百度云下载地址
esl-erlang_23.0和rabbitmq-3.8.4windows版本 直接下载安装就行,可以直接下载就可安装,非常的方便 ,欢迎大家下载 注意事项: 1. Erlang版本和RabbitMQ版本要配套 (Erlang23.0, RabbitMQ3.8.4) 2. amd芯片请乖乖...
erlang_otp_win64_25.0
erl_to_html 使用解析变换和erl_id_trans.erl Erlang的代码转换为HTML,跨度的一切。...我不知道我需要怎么做许可证,但是由于我是从Erlang示例代码中复制erl_id_trans.erl,因此我正在使用Erlang公共许可证。
这个erlang版本的protobuf,做了改进,效率有很大提升,配套文章地址http://blog.csdn.net/mycwq/article/details/42122439
这个erlang23.0版本,根据rabbitMQ官网的介绍,可以和下面这几个版本的rabbitMQ配合使用: 3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 其他版本的rabbit,请移步其他资源下载
Erlang_x64_20.1,Erlang安装工具,window系统X64,RabbitMQ环境配置必需
Erlang_win64_24.2 64位 rabbitmq 必要环境
erlang_23.0.2-1版本 centos7 64bit esl-erlang_23.0.2-1_centos_7_amd64.rpm
本资源是 适用于 linux centos 的 最新版 otp_src_21.3.tar.gz
RabbitMQ为了搭建运行环境,erlang的官网下载实在太慢,当前最新版本提交到这里提供给大家和自己下载,Erlang_win64_22.2
是erlang_otp_20.3 的win64安装包,内为exe文件,一路next即可安装完成 是适用于多线程、分布式开发的语言,也是如rabbitmq等重要工具的必须品 使用前需要配置环境变量:1.变量名为ERLANG_HOME,变量值为安装Erlang...
实现了Erlang B/C功能,支持大容量计算
实现爱尔兰B公式和爱尔兰C公式,功能齐全,适用于通信网课程中的实验。