diff --git a/rebar.config b/rebar.config index aaf5abc..01fa765 100644 --- a/rebar.config +++ b/rebar.config @@ -1,2 +1,4 @@ {erl_opts, [no_debug_info]}. -{deps, []}. \ No newline at end of file +{deps, [ + {eFmt, {git, "http://47.108.26.175:53000/SisMaker/eFmt.git", {branch, "master"}}} +]}. \ No newline at end of file diff --git a/src/genAcs.erl b/src/genAcs.erl index 6e364af..04cedb5 100644 --- a/src/genAcs.erl +++ b/src/genAcs.erl @@ -6,12 +6,12 @@ ]). main(Args) -> - [SNFile] = Args, + [SNFile, WriteDir] = Args, case file:open(SNFile, [read, raw, binary, {read_ahead, 65536}, {'encoding', 'utf8'}]) of {ok, IoDevice} -> {Goto, Output} = dealEveryLine(IoDevice, _Goto=#{0 => #{}}, _Output=#{}, _State=0), Failure = genFailure(Goto), - genErl(Goto, Failure, Output); + genErl(WriteDir, Goto, Failure, Output); _Err -> io:format("genAcs open the file:~p error ~p~n", [SNFile, _Err]) end. @@ -45,10 +45,8 @@ genGotoOutput([BinStr |Tail], Goto, Output, MaxState) -> genGotoOutput([], Goto, Output, _MaxState) -> {Goto, Output}. -addPattern(<> = BinStr, Goto, State, MaxState) -> +addPattern(<>, Goto, State, MaxState) -> #{State := Node} = Goto, - <> = BinStr, - io:format("IMY*********Word~p ~p ~p~n", [Word, Word1, BinStr]), case Node of #{Word := NextState} -> addPattern(Tail, Goto, NextState, MaxState); @@ -109,8 +107,56 @@ findFailureNode(Word, State, FailureState, Goto, Failure) -> end end. -genErl(Goto, Failure, Output) -> - ok. +genHead() -> + <<"-module(acsTrees).\n\n-export([goto/1, failure/1, output/1]).\n\n">>. + +genGoto(Goto, StrAcc) -> + Kvs = maps:to_list(Goto), + SortKvs = lists:sort(Kvs), + doGenGoto(SortKvs, StrAcc). + +doGenGoto([], StrAcc) -> + StrAcc; +doGenGoto([{K, V}], StrAcc) -> + < ", (eFmt:formatBin(<<"~w">>, [V]))/binary, ".\n\n">>; +doGenGoto([{K, V} | SortKvs], StrAcc) -> + NewStrAcc = < ", (eFmt:formatBin(<<"~w">>, [V]))/binary, ";\n">>, + doGenGoto(SortKvs, NewStrAcc). + +genFailure(Goto, StrAcc) -> + Kvs = maps:to_list(Goto), + SortKvs = lists:sort(Kvs), + doGenFailure(SortKvs, StrAcc). + +doGenFailure([], StrAcc) -> + StrAcc; +doGenFailure([{K, V}], StrAcc) -> + < ", (eFmt:formatBin(<<"~w">>, [V]))/binary, ".\n\n">>; +doGenFailure([{K, V} | SortKvs], StrAcc) -> + NewStrAcc = < ", (eFmt:formatBin(<<"~w">>, [V]))/binary, ";\n">>, + doGenFailure(SortKvs, NewStrAcc). + +genOutput(Goto, StrAcc) -> + Kvs = maps:to_list(Goto), + SortKvs = lists:sort(Kvs), + doGenOutput(SortKvs, StrAcc). + +doGenOutput([], StrAcc) -> + StrAcc; +doGenOutput([{K, V}], StrAcc) -> + < ", (eFmt:formatBin(<<"~w">>, [V]))/binary, ".\n\n">>; +doGenOutput([{K, V} | SortKvs], StrAcc) -> + NewStrAcc = < ", (eFmt:formatBin(<<"~w">>, [V]))/binary, ";\n">>, + doGenOutput(SortKvs, NewStrAcc). + + +genErl(WriteDir, Goto, Failure, Output) -> + HeadStr = genHead(), + GotoStr = genGoto(Goto, HeadStr), + FailureStr = genFailure(Failure, GotoStr), + OutputStr = genOutput(Output, FailureStr), + FileName = filename:join([WriteDir, "acsTrees.erl"]), + file:write_file(FileName, OutputStr).