Erlang提供的基本扫描器是erl_scan,里面提供了两个重头函数
string(String) -> Return string(String, StartLocation) -> Return string(String, StartLocation, Options) -> Return Types: String = string() Options = options() Return = {ok, Tokens :: tokens(), EndLocation} | {error, ErrorInfo :: error_info(), ErrorLocation} StartLocation = EndLocation = ErrorLocation = location()
tokens(Continuation, CharSpec, StartLocation) -> Return tokens(Continuation, CharSpec, StartLocation, Options) -> Return Types: Continuation = return_cont() | [] CharSpec = char_spec() StartLocation = location() Options = options() Return = {done, Result :: tokens_result(), LeftOverChars :: char_spec()} | {more, Continuation1 :: return_cont()} char_spec() = string() | eof return_cont() An opaque continuation
下面先来看看erl_scan:string这个方向
{ok,Data} = prim_file:read_file("sn.txt"). {ok,<<"{rssAPVg}.\r\nrssAPVg1.\r\nrssAPVg2.\r\nrssAPVg3.">>} Data1 = binary_to_list(Data). "{rssAPVg}.\r\nrssAPVg1.\r\nrssAPVg2.\r\nrssAPVg3."
这是我sn.txt文件放的内容
{ok, Tokens, _} = erl_scan:string(binary_to_list(Data)). {ok,[{'{',1}, {atom,1,rssAPVg}, {'}',1}, {dot,1}, {atom,2,rssAPVg1}, {dot,2}, {atom,3,rssAPVg2}, {dot,3}, {atom,4,rssAPVg3}, {dot,4}], 4}
erl_scan:string返回值的具体组成请看官方文档http://www.erlang.org/doc/man/erl_scan.html#type-token
erl_scan:tokens的用法差不多,下面也给一个例子
erl_scan:tokens([],binary_to_list(Data),0,text). {done,{ok,[{'{',[{line,0},{text,"{"}]}, {atom,[{line,0},{text,"rssAPVg"}],rssAPVg}, {'}',[{line,0},{text,"}"}]}, {dot,[{line,0},{text,".\r"}]}], 0}, "\nrssAPVg1.\r\nrssAPVg2.\r\nrssAPVg3."}
对于erl_scan:tokens来说,需要返回值中more进行尾递归进行调用erl_scan:tokens,如
scan_tokens(Code) -> case erl_scan:tokens([], Code, 1) of {done, {ok, Token, _}, Remain} -> scan_tokens(Remain); {more, _} -> [] end.
那这个模块有什么用呢?就是为后面erlang可以把从文件读取的text转成erlang特定的term或者是动态生成erlang模块做准备