diff --git a/proto/0_test.msg b/proto/0_test.mpdf similarity index 90% rename from proto/0_test.msg rename to proto/0_test.mpdf index f355ce9..4895018 100644 --- a/proto/0_test.msg +++ b/proto/0_test.mpdf @@ -10,10 +10,12 @@ phoneNumber{ } %% person info +//注释 $errcode[err1:辅导费, err2 : 444 ] tttttt33 辅导费 +%%1111 $errcode[err3:辅导费, err4:dfsf ] tttttt33 辅导费 person{ string name; %% 名字 - int32 id; %% id - string email; //邮箱 + int32 id; //错误码 $errcode[err5:其他注释辅导费, err6:dfff] 其他注释辅导费 + string email; //fdfd $errcode[err7:def, err8:其他注释辅导费 ] tttttt33 辅导费 list[phoneNumber] phone; // list of phoneNumber } diff --git a/proto/1_error.mpdf b/proto/1_error.mpdf new file mode 100644 index 0000000..bced008 --- /dev/null +++ b/proto/1_error.mpdf @@ -0,0 +1,10 @@ +%% person info +//注释 $errcode[err1:辅导费, err2 : 444 ] tttttt33 辅导费 +%%1111 $errcode[err3:辅导费, err4:dfsf ] tttttt33 辅导费 +person1{ + string name; %% 名字 + int32 id; //错误码 $errcode[err5:其他注释辅导费, err6:dfff] 其他注释辅导费 + string email; //$errcode[err7:def, err8:其他注释辅导费 ] tttttt33 辅导费 + list[phoneNumber] phone; // list of phoneNumber +} + diff --git a/src/protoCode.erl b/src/protoCode.erl index a2203fa..76b0794 100644 --- a/src/protoCode.erl +++ b/src/protoCode.erl @@ -20,45 +20,45 @@ -define(int8(V), <>). -define(uint8(V), <>). --define(int16(V), <>). --define(uint16(V), <>). --define(int32(V), <>). --define(uint32(V), <>). --define(int64(V), <>). --define(uint64(V), <>). +-define(int16(V), <>). +-define(uint16(V), <>). +-define(int32(V), <>). +-define(uint32(V), <>). +-define(int64(V), <>). +-define(uint64(V), <>). -define(integer(V), (integer(V))). -define(number(V), (number(V))). -define(string(V), (string(V))). --define(float(V), <>). --define(double(V), <>). +-define(float(V), <>). +-define(double(V), <>). -define(bool(V), (case V of true -> <<1:8>>; _ -> <<0:8>> end)). -define(record(V), (case V of undefined -> [<<0:8>>]; V -> [<<1:8>>, encodeRec(V)] end)). --define(list_bool(List), [<<(length(List)):16/little>>, [?bool(V) || V <- List]]). --define(list_int8(List), [<<(length(List)):16/little>>, [?int8(V) || V <- List]]). --define(list_uint8(List), [<<(length(List)):16/little>>, [?uint8(V) || V <- List]]). --define(list_int16(List), [<<(length(List)):16/little>>, [?int16(V) || V <- List]]). --define(list_uint16(List), [<<(length(List)):16/little>>, [?uint16(V) || V <- List]]). --define(list_int32(List), [<<(length(List)):16/little>>, [?int32(V) || V <- List]]). --define(list_uint32(List), [<<(length(List)):16/little>>, [?uint32(V) || V <- List]]). --define(list_int64(List), [<<(length(List)):16/little>>, [?int64(V) || V <- List]]). --define(list_uint64(List), [<<(length(List)):16/little>>, [?uint64(V) || V <- List]]). --define(list_float(List), [<<(length(List)):16/little>>, [?float(V) || V <- List]]). --define(list_double(List), [<<(length(List)):16/little>>, [?double(V) || V <- List]]). --define(list_integer(List), [<<(length(List)):16/little>>, [integer(V) || V <- List]]). --define(list_number(List), [<<(length(List)):16/little>>, [number(V) || V <- List]]). --define(list_string(List), [<<(length(List)):16/little>>, [string(V) || V <- List]]). --define(list_record(List), [<<(length(List)):16/little>>, [encodeRec(V) || V <- List]]). +-define(list_bool(List), [<<(length(List)):16/big>>, [?bool(V) || V <- List]]). +-define(list_int8(List), [<<(length(List)):16/big>>, [?int8(V) || V <- List]]). +-define(list_uint8(List), [<<(length(List)):16/big>>, [?uint8(V) || V <- List]]). +-define(list_int16(List), [<<(length(List)):16/big>>, [?int16(V) || V <- List]]). +-define(list_uint16(List), [<<(length(List)):16/big>>, [?uint16(V) || V <- List]]). +-define(list_int32(List), [<<(length(List)):16/big>>, [?int32(V) || V <- List]]). +-define(list_uint32(List), [<<(length(List)):16/big>>, [?uint32(V) || V <- List]]). +-define(list_int64(List), [<<(length(List)):16/big>>, [?int64(V) || V <- List]]). +-define(list_uint64(List), [<<(length(List)):16/big>>, [?uint64(V) || V <- List]]). +-define(list_float(List), [<<(length(List)):16/big>>, [?float(V) || V <- List]]). +-define(list_double(List), [<<(length(List)):16/big>>, [?double(V) || V <- List]]). +-define(list_integer(List), [<<(length(List)):16/big>>, [integer(V) || V <- List]]). +-define(list_number(List), [<<(length(List)):16/big>>, [number(V) || V <- List]]). +-define(list_string(List), [<<(length(List)):16/big>>, [string(V) || V <- List]]). +-define(list_record(List), [<<(length(List)):16/big>>, [encodeRec(V) || V <- List]]). integer(V) -> if V >= ?min8 andalso V =< ?max8 -> <<8:8, <>/binary>>; V >= ?min16 andalso V =< ?max16 -> - <<16:8, <>/binary>>; + <<16:8, <>/binary>>; V >= ?min32 andalso V =< ?max32 -> - <<32:8, <>/binary>>; + <<32:8, <>/binary>>; V >= ?min64 andalso V =< ?max64 -> - <<64:8, <>/binary>>; + <<64:8, <>/binary>>; true -> throw(exceeded_the_integer) end. @@ -68,11 +68,11 @@ numInteger(V) -> V >= ?min8 andalso V =< ?max8 -> <<8:8, <>/binary>>; V >= ?min16 andalso V =< ?max16 -> - <<16:8, <>/binary>>; + <<16:8, <>/binary>>; V >= ?min32 andalso V =< ?max32 -> - <<32:8, <>/binary>>; + <<32:8, <>/binary>>; V >= ?min64 andalso V =< ?max64 -> - <<64:8, <>/binary>>; + <<64:8, <>/binary>>; true -> throw(exceeded_the_integer) end. @@ -80,9 +80,9 @@ numInteger(V) -> numFloat(V) -> if V >= ?minF32 andalso V =< ?maxF32 -> - <<33:8, <>/binary>>; + <<33:8, <>/binary>>; V >= ?minF64 andalso V =< ?maxF64 -> - <<65:8, <>/binary>>; + <<65:8, <>/binary>>; true -> throw(exceeded_the_float) end. @@ -98,18 +98,18 @@ number(V) -> end. string(Str) when is_binary(Str) -> - [<<(byte_size(Str)):16/little>>, Str]; + [<<(byte_size(Str)):16/big>>, Str]; string(Str) -> Str2 = unicode:characters_to_binary(Str, utf8), - [<<(byte_size(Str2)):16/little>>, Str2]. + [<<(byte_size(Str2)):16/big>>, Str2]. encode(Record) -> MsgBin = encodeRec(Record), MsgId = getMsgId(element(1, Record)), - [<>, MsgBin]. + [<>, MsgBin]. decode(Bin) -> - <> = Bin, + <> = Bin, SchList = getMsgSchema(MsgId), {<<>>, ResultList} = decodeField(SchList, MsgBin, [getMsgType(MsgId)]), list_to_tuple(ResultList). @@ -124,50 +124,50 @@ decodeField([], LeftBin, Result) -> decodeField([Type | SchList], MsgBin, Result) -> case Type of int32 -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Int | Result]); uint32 -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Int | Result]); string -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [StrBin | Result]); int16 -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Int | Result]); uint16 -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Int | Result]); int8 -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Int | Result]); uint8 -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Int | Result]); int64 -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Int | Result]); uint64 -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Int | Result]); integer -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Int | Result]); number -> <> = MsgBin, case NumBits of 33 -> - <> = NumBin, + <> = NumBin, decodeField(SchList, LeftBin, [Float | Result]); 65 -> - <> = NumBin, + <> = NumBin, decodeField(SchList, LeftBin, [Float | Result]); _ -> - <> = NumBin, + <> = NumBin, decodeField(SchList, LeftBin, [Int | Result]) end; bool -> - <> = MsgBin, + <> = MsgBin, case Bool =:= 1 of true -> decodeField(SchList, LeftBin, [true | Result]); @@ -175,69 +175,69 @@ decodeField([Type | SchList], MsgBin, Result) -> decodeField(SchList, LeftBin, [false | Result]) end; float -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Float | Result]); double -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Float | Result]); {list, int32} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deInt32List(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, uint32} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deUint32List(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, int16} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deInt16List(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, uint16} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deUint16List(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, int8} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deInt8List(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, uint8} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deUint8List(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, string} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deStringList(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, int64} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deInt64List(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, uint64} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deUint64List(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, integer} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deIntegerList(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, number} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deNumberList(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, bool} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deBoolList(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, float} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deFloatList(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, double} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deDoubleList(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, RecordName} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deRecordList(Len, RecordName, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); RecordName -> @@ -265,55 +265,55 @@ deBoolList(N, MsgBin, RetList) -> deInt8List(0, MsgBin, RetList) -> {MsgBin, RetList}; deInt8List(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deInt8List(N - 1, LeftBin, [Int | RetList]). deUint8List(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deUint8List(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deUint8List(N - 1, LeftBin, [Int | RetList]). deInt16List(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deInt16List(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deInt16List(N - 1, LeftBin, [Int | RetList]). deUint16List(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deUint16List(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deUint16List(N - 1, LeftBin, [Int | RetList]). deInt32List(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deInt32List(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deInt32List(N - 1, LeftBin, [Int | RetList]). deUint32List(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deUint32List(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deUint32List(N - 1, LeftBin, [Int | RetList]). deInt64List(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deInt64List(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deInt64List(N - 1, LeftBin, [Int | RetList]). deUint64List(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deUint64List(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deUint64List(N - 1, LeftBin, [Int | RetList]). deIntegerList(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deIntegerList(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deIntegerList(N - 1, LeftBin, [Int | RetList]). deNumberList(0, MsgBin, RetList) -> @@ -322,32 +322,32 @@ deNumberList(N, MsgBin, RetList) -> <> = MsgBin, case NumBits of 33 -> - <> = NumBin, + <> = NumBin, deNumberList(N - 1, LeftBin, [Float | RetList]); 65 -> - <> = NumBin, + <> = NumBin, deNumberList(N - 1, LeftBin, [Float | RetList]); _ -> - <> = NumBin, + <> = NumBin, deNumberList(N - 1, LeftBin, [Int | RetList]) end. deFloatList(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deFloatList(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deFloatList(N - 1, LeftBin, [Float | RetList]). deDoubleList(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deDoubleList(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deDoubleList(N - 1, LeftBin, [Float | RetList]). deStringList(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deStringList(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deStringList(N - 1, LeftBin, [StrBin | RetList]). deRecordList(0, _RecordName, MsgBin, RetList) -> diff --git a/src/protoGen.erl b/src/protoGen.erl index b28fe86..9a012c5 100644 --- a/src/protoGen.erl +++ b/src/protoGen.erl @@ -39,45 +39,45 @@ protoErlHeader() -> -define(int8(V), <>). -define(uint8(V), <>). --define(int16(V), <>). --define(uint16(V), <>). --define(int32(V), <>). --define(uint32(V), <>). --define(int64(V), <>). --define(uint64(V), <>). +-define(int16(V), <>). +-define(uint16(V), <>). +-define(int32(V), <>). +-define(uint32(V), <>). +-define(int64(V), <>). +-define(uint64(V), <>). -define(integer(V), (integer(V))). -define(number(V), (number(V))). -define(string(V), (string(V))). --define(float(V), <>). --define(double(V), <>). +-define(float(V), <>). +-define(double(V), <>). -define(bool(V), (case V of true -> <<1:8>>; _ -> <<0:8>> end)). -define(record(V), (case V of undefined -> [<<0:8>>]; V -> [<<1:8>>, encodeRec(V)] end)). --define(list_bool(List), [<<(length(List)):16/little>>, [?bool(V) || V <- List]]). --define(list_int8(List), [<<(length(List)):16/little>>, [?int8(V) || V <- List]]). --define(list_uint8(List), [<<(length(List)):16/little>>, [?uint8(V) || V <- List]]). --define(list_int16(List), [<<(length(List)):16/little>>, [?int16(V) || V <- List]]). --define(list_uint16(List), [<<(length(List)):16/little>>, [?uint16(V) || V <- List]]). --define(list_int32(List), [<<(length(List)):16/little>>, [?int32(V) || V <- List]]). --define(list_uint32(List), [<<(length(List)):16/little>>, [?uint32(V) || V <- List]]). --define(list_int64(List), [<<(length(List)):16/little>>, [?int64(V) || V <- List]]). --define(list_uint64(List), [<<(length(List)):16/little>>, [?uint64(V) || V <- List]]). --define(list_float(List), [<<(length(List)):16/little>>, [?float(V) || V <- List]]). --define(list_double(List), [<<(length(List)):16/little>>, [?double(V) || V <- List]]). --define(list_integer(List), [<<(length(List)):16/little>>, [integer(V) || V <- List]]). --define(list_number(List), [<<(length(List)):16/little>>, [number(V) || V <- List]]). --define(list_string(List), [<<(length(List)):16/little>>, [string(V) || V <- List]]). --define(list_record(List), [<<(length(List)):16/little>>, [encodeRec(V) || V <- List]]). +-define(list_bool(List), [<<(length(List)):16/big>>, [?bool(V) || V <- List]]). +-define(list_int8(List), [<<(length(List)):16/big>>, [?int8(V) || V <- List]]). +-define(list_uint8(List), [<<(length(List)):16/big>>, [?uint8(V) || V <- List]]). +-define(list_int16(List), [<<(length(List)):16/big>>, [?int16(V) || V <- List]]). +-define(list_uint16(List), [<<(length(List)):16/big>>, [?uint16(V) || V <- List]]). +-define(list_int32(List), [<<(length(List)):16/big>>, [?int32(V) || V <- List]]). +-define(list_uint32(List), [<<(length(List)):16/big>>, [?uint32(V) || V <- List]]). +-define(list_int64(List), [<<(length(List)):16/big>>, [?int64(V) || V <- List]]). +-define(list_uint64(List), [<<(length(List)):16/big>>, [?uint64(V) || V <- List]]). +-define(list_float(List), [<<(length(List)):16/big>>, [?float(V) || V <- List]]). +-define(list_double(List), [<<(length(List)):16/big>>, [?double(V) || V <- List]]). +-define(list_integer(List), [<<(length(List)):16/big>>, [integer(V) || V <- List]]). +-define(list_number(List), [<<(length(List)):16/big>>, [number(V) || V <- List]]). +-define(list_string(List), [<<(length(List)):16/big>>, [string(V) || V <- List]]). +-define(list_record(List), [<<(length(List)):16/big>>, [encodeRec(V) || V <- List]]). integer(V) -> if V >= ?min8 andalso V =< ?max8 -> <<8:8, <>/binary>>; V >= ?min16 andalso V =< ?max16 -> - <<16:8, <>/binary>>; + <<16:8, <>/binary>>; V >= ?min32 andalso V =< ?max32 -> - <<32:8, <>/binary>>; + <<32:8, <>/binary>>; V >= ?min64 andalso V =< ?max64 -> - <<64:8, <>/binary>>; + <<64:8, <>/binary>>; true -> throw(exceeded_the_integer) end. @@ -87,11 +87,11 @@ numInteger(V) -> V >= ?min8 andalso V =< ?max8 -> <<8:8, <>/binary>>; V >= ?min16 andalso V =< ?max16 -> - <<16:8, <>/binary>>; + <<16:8, <>/binary>>; V >= ?min32 andalso V =< ?max32 -> - <<32:8, <>/binary>>; + <<32:8, <>/binary>>; V >= ?min64 andalso V =< ?max64 -> - <<64:8, <>/binary>>; + <<64:8, <>/binary>>; true -> throw(exceeded_the_integer) end. @@ -99,9 +99,9 @@ numInteger(V) -> numFloat(V) -> if V >= ?minF32 andalso V =< ?maxF32 -> - <<33:8, <>/binary>>; + <<33:8, <>/binary>>; V >= ?minF64 andalso V =< ?maxF64 -> - <<65:8, <>/binary>>; + <<65:8, <>/binary>>; true -> throw(exceeded_the_float) end. @@ -117,18 +117,18 @@ number(V) -> end. string(Str) when is_binary(Str) -> - [<<(byte_size(Str)):16/little>>, Str]; + [<<(byte_size(Str)):16/big>>, Str]; string(Str) -> Str2 = unicode:characters_to_binary(Str, utf8), - [<<(byte_size(Str2)):16/little>>, Str2]. + [<<(byte_size(Str2)):16/big>>, Str2]. encode(Record) -> MsgBin = encodeRec(Record), MsgId = getMsgId(element(1, Record)), - [<>, MsgBin]. + [<>, MsgBin]. decode(Bin) -> - <> = Bin, + <> = Bin, SchList = getMsgSchema(MsgId), {<<>>, ResultList} = decodeField(SchList, MsgBin, [getMsgType(MsgId)]), list_to_tuple(ResultList). @@ -143,50 +143,50 @@ decodeField([], LeftBin, Result) -> decodeField([Type | SchList], MsgBin, Result) -> case Type of int32 -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Int | Result]); uint32 -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Int | Result]); string -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [StrBin | Result]); int16 -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Int | Result]); uint16 -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Int | Result]); int8 -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Int | Result]); uint8 -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Int | Result]); int64 -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Int | Result]); uint64 -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Int | Result]); integer -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Int | Result]); number -> <> = MsgBin, case NumBits of 33 -> - <> = NumBin, + <> = NumBin, decodeField(SchList, LeftBin, [Float | Result]); 65 -> - <> = NumBin, + <> = NumBin, decodeField(SchList, LeftBin, [Float | Result]); _ -> - <> = NumBin, + <> = NumBin, decodeField(SchList, LeftBin, [Int | Result]) end; bool -> - <> = MsgBin, + <> = MsgBin, case Bool =:= 1 of true -> decodeField(SchList, LeftBin, [true | Result]); @@ -194,69 +194,69 @@ decodeField([Type | SchList], MsgBin, Result) -> decodeField(SchList, LeftBin, [false | Result]) end; float -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Float | Result]); double -> - <> = MsgBin, + <> = MsgBin, decodeField(SchList, LeftBin, [Float | Result]); {list, int32} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deInt32List(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, uint32} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deUint32List(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, int16} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deInt16List(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, uint16} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deUint16List(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, int8} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deInt8List(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, uint8} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deUint8List(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, string} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deStringList(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, int64} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deInt64List(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, uint64} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deUint64List(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, integer} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deIntegerList(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, number} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deNumberList(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, bool} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deBoolList(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, float} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deFloatList(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, double} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deDoubleList(Len, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); {list, RecordName} -> - <> = MsgBin, + <> = MsgBin, {LeftBin, RetList} = deRecordList(Len, RecordName, LeftListBin, []), decodeField(SchList, LeftBin, [RetList | Result]); RecordName -> @@ -284,55 +284,55 @@ deBoolList(N, MsgBin, RetList) -> deInt8List(0, MsgBin, RetList) -> {MsgBin, RetList}; deInt8List(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deInt8List(N - 1, LeftBin, [Int | RetList]). deUint8List(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deUint8List(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deUint8List(N - 1, LeftBin, [Int | RetList]). deInt16List(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deInt16List(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deInt16List(N - 1, LeftBin, [Int | RetList]). deUint16List(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deUint16List(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deUint16List(N - 1, LeftBin, [Int | RetList]). deInt32List(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deInt32List(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deInt32List(N - 1, LeftBin, [Int | RetList]). deUint32List(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deUint32List(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deUint32List(N - 1, LeftBin, [Int | RetList]). deInt64List(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deInt64List(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deInt64List(N - 1, LeftBin, [Int | RetList]). deUint64List(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deUint64List(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deUint64List(N - 1, LeftBin, [Int | RetList]). deIntegerList(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deIntegerList(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deIntegerList(N - 1, LeftBin, [Int | RetList]). deNumberList(0, MsgBin, RetList) -> @@ -341,32 +341,32 @@ deNumberList(N, MsgBin, RetList) -> <> = MsgBin, case NumBits of 33 -> - <> = NumBin, + <> = NumBin, deNumberList(N - 1, LeftBin, [Float | RetList]); 65 -> - <> = NumBin, + <> = NumBin, deNumberList(N - 1, LeftBin, [Float | RetList]); _ -> - <> = NumBin, + <> = NumBin, deNumberList(N - 1, LeftBin, [Int | RetList]) end. deFloatList(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deFloatList(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deFloatList(N - 1, LeftBin, [Float | RetList]). deDoubleList(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deDoubleList(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deDoubleList(N - 1, LeftBin, [Float | RetList]). deStringList(0, MsgBin, RetList) -> {MsgBin, lists:reverse(RetList)}; deStringList(N, MsgBin, RetList) -> - <> = MsgBin, + <> = MsgBin, deStringList(N - 1, LeftBin, [StrBin | RetList]). deRecordList(0, _RecordName, MsgBin, RetList) -> @@ -390,6 +390,10 @@ genMsgHrl(FieldInfo, {Index, Len, AccList}) -> RecStr = TemStr ++ protoField:builtRecStr(FieldInfo) ++ (case Index == Len of true -> ""; _ -> "\t" end), {Index - 1, Len, [RecStr | AccList]}. +genErrCodeHrl({ErrName, ErrCodeId, ComDesc}, AccList) -> + Str = "-define(" ++ ErrName ++ ", " ++ integer_to_list(ErrCodeId) ++ ").\t\t%% " ++ ComDesc ++ "\n", + [Str | AccList]. + genEncodeRec({MsgName, _MsgId, FieldList}) -> FieldLen = length(FieldList), FunHead = @@ -431,24 +435,28 @@ convertDir(ProtoDir) -> convertDir(ProtoDir, "./", "./"). convertDir(ProtoDir, HrlDir, ErlDir) -> FunRead = - fun(File, Acc) -> - case filename:extension(File) == ".msg" of + fun(File, {ProAcc, ErrCodeAcc} = Acc) -> + case filename:extension(File) == ".mpdf" of true -> io:format("Convert proto msg file: ~s ~n", [File]), - BaseName = filename:basename(File, ".msg"), + BaseName = filename:basename(File, ".mpdf"), [ModIndex | _ModName] = re:split(BaseName, "_"), Index = binary_to_integer(ModIndex), - put(messageid, Index * 1000 + 1), + erlang:put(pd_messageid, Index * 1000 + 1), + erlang:put(pd_errcodeid, Index * 1000 + 1), + erlang:put(pd_errlist, []), SProto = protoParse:parseFile(File), - [SProto | Acc]; + ErrCode = erlang:get(pd_errlist), + erlang:erase(), + {[SProto | ProAcc], [ErrCode | ErrCodeAcc]}; _ -> Acc - end end, %% 下面文件帅选并不能准确的帅选出文件名为.msg结尾的文件 在FunRead函数中纠正处理一下 - SProtoListOfList = filelib:fold_files(ProtoDir, "\\.msg", true, FunRead, []), + {SProtoListOfList, ErrListOfList} = filelib:fold_files(ProtoDir, "\\.mpdf", true, FunRead, {[], []}), SProtoList = lists:append(SProtoListOfList), + ErrList = lists:append(ErrListOfList), SortedSProtoList = lists:sort(fun({_Name1, MessageId1, _FieldList1}, {_Name2, MessageId2, _FieldList2}) -> MessageId1 > MessageId2 end, SProtoList), FunSpell = @@ -465,10 +473,14 @@ convertDir(ProtoDir, HrlDir, ErlDir) -> {[HrlStr | MsgHrlAcc], [TypeStr | MsgTypeAcc], [IdStr | MsgIdAcc], [EncodeStr | MsgEndStr], [SchemaStr | MsgSchemaAcc]} end, {MsgHrlStr, MsgTypeStr, MsgIdStr, MsgEndStr, MsgSchemaStr} = lists:foldl(FunSpell, {[], ["getMsgType(_) -> undefined.\n\n"], ["getMsgId(_) -> 0.\n\n"], ["encodeRec(_) ->\n\t[].\n\n"], ["getMsgSchema(_) ->\n\t[].\n\n"]}, SortedSProtoList), - + + SortedErrList = lists:sort(fun({_ErrName1, ErrCodeId1, _Desc1}, {_ErrName2, ErrCodeId2, _Desc2}) -> + ErrCodeId1 > ErrCodeId2 end, ErrList), + ErrCodeStr = lists:foldl(fun genErrCodeHrl/2, [], SortedErrList) ++ "\n\n", + ModStr = protoErlHeader(), OutputStr = ModStr ++ MsgTypeStr ++ MsgIdStr ++ MsgEndStr ++ MsgSchemaStr, - HrlFilename = do_write_hrl(HrlDir, protoMsg, protoHrlHeader() ++ MsgHrlStr), + HrlFilename = do_write_hrl(HrlDir, protoMsg, protoHrlHeader() ++ ErrCodeStr ++ MsgHrlStr), ErlFilename = do_write_erl(ErlDir, protoMsg, OutputStr), io:format("protoConvert hrl dir : ~s ~n", [HrlDir]), diff --git a/src/protoParse.erl b/src/protoParse.erl index b9e409b..98255da 100644 --- a/src/protoParse.erl +++ b/src/protoParse.erl @@ -2,8 +2,8 @@ -compile(nowarn_unused_function). -export([ - parseParse/1 - , parseFile/1 + parseParse/1 + , parseFile/1 ]). -define(p_anything, true). @@ -20,180 +20,213 @@ -spec parseFile(file:name()) -> any(). parseFile(Filename) -> - case file:read_file(Filename) of - {ok, Bin} -> - parseParse(Bin); - Err -> Err - end. + case file:read_file(Filename) of + {ok, Bin} -> + parseParse(Bin); + Err -> Err + end. -spec parseParse(binary() | list()) -> any(). parseParse(List) when is_list(List) -> - parseParse(unicode:characters_to_binary(List)); + parseParse(unicode:characters_to_binary(List)); parseParse(Input) when is_binary(Input) -> - Result = - case 'all'(Input, {{line, 1}, {column, 1}}) of - {AST, <<>>, _Index} -> - AST; - Any -> - Any - end, - release_memo(), - Result. + Result = + case 'all'(Input, {{line, 1}, {column, 1}}) of + {AST, <<>>, _Index} -> + AST; + Any -> + Any + end, + Result. -spec 'all'(input(), index()) -> parse_result(). 'all'(Input, Index) -> - p(Input, Index, 'all', - fun(I, D) -> - (p_seq([fun 'blank0'/2, p_zero_or_more(p_seq([fun 'protocol'/2, fun 'blank0'/2]))]))(I, D) - end, - fun(Node, _Idx) -> - [_ | [T]] = Node, DataList = [H || [H | _] <- T], DataList - end). + p(Input, Index, 'all', + fun(I, D) -> + (p_seq([fun 'blank0'/2, p_zero_or_more(p_seq([fun 'protocol'/2, fun 'blank0'/2]))]))(I, D) + end, + fun(Node, _Idx) -> + [_ | [T]] = Node, DataList = [H || [H | _] <- T], DataList + end). -spec 'protocol'(input(), index()) -> parse_result(). 'protocol'(Input, Index) -> - p(Input, Index, 'protocol', - fun(I, D) -> - (p_seq([p_label('name', fun 'name'/2), fun 'blank0'/2, p_label('structural', fun 'structural'/2)]))(I, D) - end, - fun(Node, _Idx) -> - Name = binary_to_list(iolist_to_binary(proplists:get_value(name, Node))), - Structural = proplists:get_value(structural, Node), - MsgId = get(messageid), - put(messageid, MsgId + 1), - {Name, MsgId, Structural} - end). + p(Input, Index, 'protocol', + fun(I, D) -> + (p_seq([p_label('name', fun 'name'/2), fun 'blank0'/2, p_label('structural', fun 'structural'/2)]))(I, D) + end, + fun(Node, _Idx) -> + Name = binary_to_list(iolist_to_binary(proplists:get_value(name, Node))), + Structural = proplists:get_value(structural, Node), + MsgId = erlang:get(pd_messageid), + erlang:put(pd_messageid, MsgId + 1), + {Name, MsgId, Structural} + end). -spec 'structural'(input(), index()) -> parse_result(). 'structural'(Input, Index) -> - p(Input, Index, 'structural', - fun(I, D) -> - (p_seq([p_string(<<"{">>), fun 'blank0'/2, p_zero_or_more(p_seq([fun 'field'/2, fun 'blank0'/2])), p_string(<<"}">>)]))(I, D) - end, - fun(Node, _Idx) -> - [_, _, List, _] = Node, [H || [H | _] <- List] - end). + p(Input, Index, 'structural', + fun(I, D) -> + (p_seq([p_string(<<"{">>), fun 'blank0'/2, p_zero_or_more(p_seq([fun 'field'/2, fun 'blank0'/2])), p_string(<<"}">>)]))(I, D) + end, + fun(Node, _Idx) -> + [_, _, List, _] = Node, [H || [H | _] <- List] + end). -spec 'field'(input(), index()) -> parse_result(). 'field'(Input, Index) -> - p(Input, Index, 'field', - fun(I, D) -> - (p_seq([p_label('datatype', fun 'typename'/2), fun 'blanks'/2, p_label('name', fun 'name'/2), fun 'blank0'/2, p_string(<<";">>)]))(I, D) - end, - fun(Node, _Idx) -> - DataType = binary_to_list(iolist_to_binary(proplists:get_value(datatype, Node))), - Name = binary_to_list(iolist_to_binary(proplists:get_value(name, Node))), - {DataType, Name} - end). + p(Input, Index, 'field', + fun(I, D) -> + (p_seq([p_label('datatype', fun 'typename'/2), fun 'blanks'/2, p_label('name', fun 'name'/2), fun 'blank0'/2, p_string(<<";">>)]))(I, D) + end, + fun(Node, _Idx) -> + DataType = binary_to_list(iolist_to_binary(proplists:get_value(datatype, Node))), + Name = binary_to_list(iolist_to_binary(proplists:get_value(name, Node))), + {DataType, Name} + end). -spec 'eof'(input(), index()) -> parse_result(). 'eof'(Input, Index) -> - p(Input, Index, 'eof', - fun(I, D) -> - (p_not(p_string(<<".">>)))(I, D) - end, - fun(Node, _Idx) -> - Node - end). + p(Input, Index, 'eof', + fun(I, D) -> + (p_not(p_string(<<".">>)))(I, D) + end, + fun(Node, _Idx) -> + Node + end). -spec 'newline'(input(), index()) -> parse_result(). 'newline'(Input, Index) -> - p(Input, Index, 'newline', - fun(I, D) -> - (p_seq([p_optional(p_charclass(<<"[\r]">>)), p_charclass(<<"[\n]">>)]))(I, D) - end, - fun(Node, _Idx) -> - Node - end). + p(Input, Index, 'newline', + fun(I, D) -> + (p_seq([p_optional(p_charclass(<<"[\r]">>)), p_charclass(<<"[\n]">>)]))(I, D) + end, + fun(Node, _Idx) -> + Node + end). -spec 'line_comment'(input(), index()) -> parse_result(). 'line_comment'(Input, Index) -> - p(Input, Index, 'line_comment', - fun(I, D) -> - (p_seq([p_choose([p_string(<<"//">>), p_string(<<"%">>)]), p_zero_or_more(p_seq([p_not(fun 'newline'/2), p_anything()])), p_choose([fun 'newline'/2, fun 'eof'/2])]))(I, D) - end, - fun(Node, _Idx) -> - Node - end). + p(Input, Index, 'line_comment', + fun(I, D) -> + (p_seq([p_choose([p_string(<<"//">>), p_string(<<"%%">>)]), p_zero_or_more(p_seq([p_not(fun 'newline'/2), p_not(p_string(<<"$errcode[">>)), p_anything()])), p_zero_or_more(p_seq([p_string(<<"$errcode[">>), p_zero_or_more(fun 'errcode'/2), p_string(<<"]">>)])), p_zero_or_more(p_seq([p_not(fun 'newline'/2), p_anything()])), p_choose([fun 'newline'/2, fun 'eof'/2])]))(I, D) + end, + fun(Node, _Idx) -> + Node + end). + +-spec 'errcode'(input(), index()) -> parse_result(). +'errcode'(Input, Index) -> + p(Input, Index, 'errcode', + fun(I, D) -> + (p_seq([fun 'blank0'/2, p_label('errname', fun 'name'/2), fun 'blank0'/2, p_string(<<":">>), fun 'blank0'/2, p_label('errcode_str', fun 'errcode_str'/2), p_choose([p_charclass(<<",">>), fun 'blank0'/2])]))(I, D) + end, + fun(Node, _Idx) -> + ErrNameList = proplists:get_value('errname', Node), + ErrCodeStrList = proplists:get_value('errcode_str', Node), + ErrName = binary_to_list(iolist_to_binary(ErrNameList)), + Desc = binary_to_list(iolist_to_binary(ErrCodeStrList)), + ErrList = erlang:get(pd_errlist), + case ErrNameList =/= [] andalso lists:keyfind(ErrName, 1, ErrList) == false of + true -> + ErrCodeId = erlang:get(pd_errcodeid), + erlang:put(pd_errlist, [{ErrName, ErrCodeId, Desc} | ErrList]), + erlang:put(pd_errcodeid, ErrCodeId + 1); + _ -> + skip + end, + Node + end). -spec 'blank'(input(), index()) -> parse_result(). 'blank'(Input, Index) -> - p(Input, Index, 'blank', - fun(I, D) -> - (p_choose([p_charclass(<<"[\s\t]">>), fun 'newline'/2, fun 'line_comment'/2]))(I, D) - end, - fun(Node, _Idx) -> - Node - end). + p(Input, Index, 'blank', + fun(I, D) -> + (p_choose([p_charclass(<<"[\s\t]">>), fun 'newline'/2, fun 'line_comment'/2]))(I, D) + end, + fun(Node, _Idx) -> + Node + end). -spec 'blank0'(input(), index()) -> parse_result(). 'blank0'(Input, Index) -> - p(Input, Index, 'blank0', - fun(I, D) -> - (p_zero_or_more(fun 'blank'/2))(I, D) - end, - fun(Node, _Idx) -> - Node - end). + p(Input, Index, 'blank0', + fun(I, D) -> + (p_zero_or_more(fun 'blank'/2))(I, D) + end, + fun(Node, _Idx) -> + Node + end). -spec 'blanks'(input(), index()) -> parse_result(). 'blanks'(Input, Index) -> - p(Input, Index, 'blanks', - fun(I, D) -> (p_one_or_more(fun 'blank'/2))(I, D) - end, - fun(Node, _Idx) -> - Node - end). + p(Input, Index, 'blanks', + fun(I, D) -> + (p_one_or_more(fun 'blank'/2))(I, D) + end, + fun(Node, _Idx) -> + Node + end). -spec 'alpha'(input(), index()) -> parse_result(). 'alpha'(Input, Index) -> - p(Input, Index, 'alpha', - fun(I, D) -> - (p_choose([p_charclass(<<"[a-z]">>), p_charclass(<<"[A-Z]">>), p_string(<<"_">>), p_string(<<"[">>), p_string(<<"]">>)]))(I, D) - end, - fun(Node, _Idx) -> - Node - end). + p(Input, Index, 'alpha', + fun(I, D) -> + (p_choose([p_charclass(<<"[a-z]">>), p_charclass(<<"[A-Z]">>), p_string(<<"_">>), p_string(<<"[">>), p_string(<<"]">>)]))(I, D) + end, + fun(Node, _Idx) -> + Node + end). -spec 'alnum'(input(), index()) -> parse_result(). 'alnum'(Input, Index) -> - p(Input, Index, 'alnum', - fun(I, D) -> - (p_choose([fun 'alpha'/2, p_charclass(<<"[0-9]">>)]))(I, D) - end, - fun(Node, _Idx) -> - Node - end). + p(Input, Index, 'alnum', + fun(I, D) -> + (p_choose([fun 'alpha'/2, p_charclass(<<"[0-9]">>)]))(I, D) + end, + fun(Node, _Idx) -> + Node + end). -spec 'word'(input(), index()) -> parse_result(). 'word'(Input, Index) -> - p(Input, Index, 'word', - fun(I, D) -> - (p_seq([fun 'alpha'/2, p_zero_or_more(fun 'alnum'/2)]))(I, D) - end, - fun(Node, _Idx) -> - Node - end). + p(Input, Index, 'word', + fun(I, D) -> + (p_seq([fun 'alpha'/2, p_zero_or_more(fun 'alnum'/2)]))(I, D) + end, + fun(Node, _Idx) -> + Node + end). + +-spec 'errcode_str'(input(), index()) -> parse_result(). +'errcode_str'(Input, Index) -> + p(Input, Index, 'errcode_str', + fun(I, D) -> + (p_seq([p_zero_or_more(p_seq([p_not(fun 'newline'/2), p_not(p_string(<<",">>)), p_not(p_string(<<"]">>)), p_charclass(<<".">>)]))]))(I, D) + end, + fun(Node, _Idx) -> + Node + end). -spec 'name'(input(), index()) -> parse_result(). 'name'(Input, Index) -> - p(Input, Index, 'name', - fun(I, D) -> - (fun 'word'/2)(I, D) - end, - fun(Node, _Idx) -> - Node - end). + p(Input, Index, 'name', + fun(I, D) -> + (fun 'word'/2)(I, D) + end, + fun(Node, _Idx) -> + Node + end). -spec 'typename'(input(), index()) -> parse_result(). 'typename'(Input, Index) -> - p(Input, Index, 'typename', - fun(I, D) -> - (fun 'word'/2)(I, D) - end, - fun(Node, _Idx) -> - Node - end). + p(Input, Index, 'typename', + fun(I, D) -> + (fun 'word'/2)(I, D) + end, + fun(Node, _Idx) -> + Node + end). -file("peg_includes.hrl", 1). -type index() :: {{line, pos_integer()}, {column, pos_integer()}}. @@ -206,250 +239,242 @@ parseParse(Input) when is_binary(Input) -> -spec p(input(), index(), atom(), parse_fun(), xform_fun()) -> parse_result(). p(Inp, StartIndex, Name, ParseFun, TransformFun) -> - case get_memo(StartIndex, Name) of % See if the current reduction is memoized - {ok, Memo} -> % If it is, return the stored result - Memo; - _ -> % If not, attempt to parse - Result = - case ParseFun(Inp, StartIndex) of - {fail, _} = Failure -> % If it fails, memoize the failure - Failure; - {Match, InpRem, NewIndex} -> % If it passes, transform and memoize the result. - Transformed = TransformFun(Match, StartIndex), - {Transformed, InpRem, NewIndex} - end, - memoize(StartIndex, Name, Result), - Result - end. - --spec setup_memo() -> list(). -setup_memo() -> - erase(). - --spec release_memo() -> list(). -release_memo() -> - erase(). + case get_memo(StartIndex, Name) of % See if the current reduction is memoized + {ok, Memo} -> % If it is, return the stored result + Memo; + _ -> % If not, attempt to parse + Result = + case ParseFun(Inp, StartIndex) of + {fail, _} = Failure -> % If it fails, memoize the failure + Failure; + {Match, InpRem, NewIndex} -> % If it passes, transform and memoize the result. + Transformed = TransformFun(Match, StartIndex), + {Transformed, InpRem, NewIndex} + end, + memoize(StartIndex, Name, Result), + Result + end. -spec memoize(index(), atom(), parse_result()) -> any(). memoize(Index, Name, Result) -> - case get(Index) of - undefined -> - put(Index, [{Name, Result}]); - [] -> - put(Index, [{Name, Result}]); - Plist -> - put(Index, [{Name, Result} | Plist]) - end. + case erlang:get(Index) of + undefined -> + put(Index, [{Name, Result}]); + [] -> + put(Index, [{Name, Result}]); + Plist -> + put(Index, [{Name, Result} | Plist]) + end. -spec get_memo(index(), atom()) -> {ok, term()} | {error, not_found}. get_memo(Index, Name) -> - case get(Index) of - undefined -> - {error, not_found}; - [] -> - {error, not_found}; - Plist -> - case proplists:lookup(Name, Plist) of - {Name, Result} -> - {ok, Result}; - _ -> - {error, not_found} - end - end. + case erlang:get(Index) of + undefined -> + {error, not_found}; + [] -> + {error, not_found}; + Plist -> + case proplists:lookup(Name, Plist) of + {Name, Result} -> + {ok, Result}; + _ -> + {error, not_found} + end + end. -ifdef(p_eof). -spec p_eof() -> parse_fun(). p_eof() -> - fun(<<>>, Index) -> - {eof, [], Index}; - (_, Index) -> - {fail, {expected, eof, Index}} end. + fun(<<>>, Index) -> + {eof, [], Index}; + (_, Index) -> + {fail, {expected, eof, Index}} end. -endif. -ifdef(p_optional). -spec p_optional(parse_fun()) -> parse_fun(). p_optional(P) -> - fun(Input, Index) -> - case P(Input, Index) of - {fail, _} -> - {[], Input, Index}; - {_, _, _} = Success -> - Success - end - end. + fun(Input, Index) -> + case P(Input, Index) of + {fail, _} -> + {[], Input, Index}; + {_, _, _} = Success -> + Success + end + end. -endif. -ifdef(p_not). -spec p_not(parse_fun()) -> parse_fun(). p_not(P) -> - fun(Input, Index) -> - case P(Input, Index) of - {fail, _} -> - {[], Input, Index}; - {Result, _, _} -> - {fail, {expected, {no_match, Result}, Index}} - end - end. + fun(Input, Index) -> + case P(Input, Index) of + {fail, _} -> + {[], Input, Index}; + {Result, _, _} -> + {fail, {expected, {no_match, Result}, Index}} + end + end. -endif. -ifdef(p_assert). -spec p_assert(parse_fun()) -> parse_fun(). p_assert(P) -> - fun(Input, Index) -> - case P(Input, Index) of - {fail, _} = Failure -> - Failure; - _ -> - {[], Input, Index} - end - end. + fun(Input, Index) -> + case P(Input, Index) of + {fail, _} = Failure -> + Failure; + _ -> + {[], Input, Index} + end + end. -endif. -ifdef(p_seq). -spec p_seq([parse_fun()]) -> parse_fun(). p_seq(P) -> - fun(Input, Index) -> - p_all(P, Input, Index, []) - end. + fun(Input, Index) -> + p_all(P, Input, Index, []) + end. -spec p_all([parse_fun()], input(), index(), [term()]) -> parse_result(). p_all([], Inp, Index, Accum) -> - {lists:reverse(Accum), Inp, Index}; + {lists:reverse(Accum), Inp, Index}; p_all([P | Parsers], Inp, Index, Accum) -> - case P(Inp, Index) of - {fail, _} = Failure -> - Failure; - {Result, InpRem, NewIndex} -> - p_all(Parsers, InpRem, NewIndex, [Result | Accum]) - end. + case P(Inp, Index) of + {fail, _} = Failure -> + Failure; + {Result, InpRem, NewIndex} -> + p_all(Parsers, InpRem, NewIndex, [Result | Accum]) + end. -endif. -ifdef(p_choose). -spec p_choose([parse_fun()]) -> parse_fun(). p_choose(Parsers) -> - fun(Input, Index) -> - p_attempt(Parsers, Input, Index, none) - end. + fun(Input, Index) -> + p_attempt(Parsers, Input, Index, none) + end. -spec p_attempt([parse_fun()], input(), index(), none | parse_failure()) -> parse_result(). p_attempt([], _Input, _Index, Failure) -> Failure; p_attempt([P | Parsers], Input, Index, FirstFailure) -> - case P(Input, Index) of - {fail, _} = Failure -> - case FirstFailure of - none -> - p_attempt(Parsers, Input, Index, Failure); - _ -> - p_attempt(Parsers, Input, Index, FirstFailure) - end; - Result -> - Result - end. + case P(Input, Index) of + {fail, _} = Failure -> + case FirstFailure of + none -> + p_attempt(Parsers, Input, Index, Failure); + _ -> + p_attempt(Parsers, Input, Index, FirstFailure) + end; + Result -> + Result + end. -endif. -ifdef(p_zero_or_more). -spec p_zero_or_more(parse_fun()) -> parse_fun(). p_zero_or_more(P) -> - fun(Input, Index) -> - p_scan(P, Input, Index, []) - end. + fun(Input, Index) -> + p_scan(P, Input, Index, []) + end. -endif. -ifdef(p_one_or_more). -spec p_one_or_more(parse_fun()) -> parse_fun(). p_one_or_more(P) -> - fun(Input, Index) -> - Result = p_scan(P, Input, Index, []), - case Result of - {[_ | _], _, _} -> - Result; - _ -> - {fail, {expected, Failure, _}} = P(Input, Index), - {fail, {expected, {at_least_one, Failure}, Index}} - end - end. + fun(Input, Index) -> + Result = p_scan(P, Input, Index, []), + case Result of + {[_ | _], _, _} -> + Result; + _ -> + {fail, {expected, Failure, _}} = P(Input, Index), + {fail, {expected, {at_least_one, Failure}, Index}} + end + end. -endif. -ifdef(p_label). -spec p_label(atom(), parse_fun()) -> parse_fun(). p_label(Tag, P) -> - fun(Input, Index) -> - case P(Input, Index) of - {fail, _} = Failure -> - Failure; - {Result, InpRem, NewIndex} -> - {{Tag, Result}, InpRem, NewIndex} - end - end. + fun(Input, Index) -> + case P(Input, Index) of + {fail, _} = Failure -> + Failure; + {Result, InpRem, NewIndex} -> + {{Tag, Result}, InpRem, NewIndex} + end + end. -endif. -ifdef(p_scan). -spec p_scan(parse_fun(), input(), index(), [term()]) -> {[term()], input(), index()}. p_scan(_, <<>>, Index, Accum) -> - {lists:reverse(Accum), <<>>, Index}; + {lists:reverse(Accum), <<>>, Index}; p_scan(P, Inp, Index, Accum) -> - case P(Inp, Index) of - {fail, _} -> - {lists:reverse(Accum), Inp, Index}; - {Result, InpRem, NewIndex} -> - p_scan(P, InpRem, NewIndex, [Result | Accum]) - end. + case P(Inp, Index) of + {fail, _} -> + {lists:reverse(Accum), Inp, Index}; + {Result, InpRem, NewIndex} -> + p_scan(P, InpRem, NewIndex, [Result | Accum]) + end. -endif. -ifdef(p_string). -spec p_string(binary()) -> parse_fun(). p_string(S) -> - Length = erlang:byte_size(S), - fun(Input, Index) -> - try - <> = Input, - {S, Rest, p_advance_index(S, Index)} - catch - error:{badmatch, _} -> - {fail, {expected, {string, S}, Index}} - end - end. + Length = erlang:byte_size(S), + fun(Input, Index) -> + try + <> = Input, + {S, Rest, p_advance_index(S, Index)} + catch + error:{badmatch, _} -> + {fail, {expected, {string, S}, Index}} + end + end. -endif. -ifdef(p_anything). -spec p_anything() -> parse_fun(). p_anything() -> - fun(<<>>, Index) -> - {fail, {expected, any_character, Index}}; - (Input, Index) when is_binary(Input) -> - <> = Input, - {<>, Rest, p_advance_index(<>, Index)} - end. + fun(<<>>, Index) -> + {fail, {expected, any_character, Index}}; + (Input, Index) when is_binary(Input) -> + <> = Input, + {<>, Rest, p_advance_index(<>, Index)} + end. -endif. -ifdef(p_charclass). -spec p_charclass(string() | binary()) -> parse_fun(). p_charclass(Class) -> - {ok, RE} = re:compile(Class, [unicode, dotall]), - fun(Inp, Index) -> - case re:run(Inp, RE, [anchored]) of - {match, [{0, Length} | _]} -> - {Head, Tail} = erlang:split_binary(Inp, Length), - {Head, Tail, p_advance_index(Head, Index)}; - _ -> - {fail, {expected, {character_class, binary_to_list(Class)}, Index}} - end - end. + {ok, RE} = re:compile(Class, [unicode, dotall]), + fun(Inp, Index) -> + case re:run(Inp, RE, [anchored]) of + {match, [{0, Length} | _]} -> + {Head, Tail} = erlang:split_binary(Inp, Length), + {Head, Tail, p_advance_index(Head, Index)}; + _ -> + {fail, {expected, {character_class, binary_to_list(Class)}, Index}} + end + end. -endif. -ifdef(p_regexp). -spec p_regexp(binary()) -> parse_fun(). p_regexp(Regexp) -> - {ok, RE} = re:compile(Regexp, [unicode, dotall, anchored]), - fun(Inp, Index) -> - case re:run(Inp, RE) of - {match, [{0, Length} | _]} -> - {Head, Tail} = erlang:split_binary(Inp, Length), - {Head, Tail, p_advance_index(Head, Index)}; - _ -> - {fail, {expected, {regexp, binary_to_list(Regexp)}, Index}} - end - end. + {ok, RE} = re:compile(Regexp, [unicode, dotall, anchored]), + fun(Inp, Index) -> + case re:run(Inp, RE) of + {match, [{0, Length} | _]} -> + {Head, Tail} = erlang:split_binary(Inp, Length), + {Head, Tail, p_advance_index(Head, Index)}; + _ -> + {fail, {expected, {regexp, binary_to_list(Regexp)}, Index}} + end + end. -endif. -ifdef(line). @@ -466,10 +491,10 @@ column(_) -> undefined. -spec p_advance_index(input() | unicode:charlist() | pos_integer(), index()) -> index(). p_advance_index(MatchedInput, Index) when is_list(MatchedInput) orelse is_binary(MatchedInput) -> % strings - lists:foldl(fun p_advance_index/2, Index, unicode:characters_to_list(MatchedInput)); + lists:foldl(fun p_advance_index/2, Index, unicode:characters_to_list(MatchedInput)); p_advance_index(MatchedInput, Index) when is_integer(MatchedInput) -> % single characters - {{line, Line}, {column, Col}} = Index, - case MatchedInput of - $\n -> {{line, Line + 1}, {column, 1}}; - _ -> {{line, Line}, {column, Col + 1}} - end. + {{line, Line}, {column, Col}} = Index, + case MatchedInput of + $\n -> {{line, Line + 1}, {column, 1}}; + _ -> {{line, Line}, {column, Col + 1}} + end. diff --git a/test/protoMsg.erl b/test/protoMsg.erl index b1d7a89..61c0202 100644 --- a/test/protoMsg.erl +++ b/test/protoMsg.erl @@ -390,6 +390,7 @@ getMsgType(32)-> tlistdouble; getMsgType(33)-> tliststring; getMsgType(34)-> tlistunion; getMsgType(35)-> allType; +getMsgType(1001)-> person1; getMsgType(_) -> undefined. getMsgId(test)-> 1; @@ -427,6 +428,7 @@ getMsgId(tlistdouble)-> 32; getMsgId(tliststring)-> 33; getMsgId(tlistunion)-> 34; getMsgId(allType)-> 35; +getMsgId(person1)-> 1001; getMsgId(_) -> 0. encodeRec({test, V1}) -> @@ -499,6 +501,8 @@ encodeRec({tlistunion, V1}) -> [?list_record(V1)]; encodeRec({allType, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22, V23, V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36, V37, V38, V39, V40, V41, V42, V43, V44, V45, V46, V47, V48, V49, V50, V51, V52, V53, V54, V55}) -> [?bool(V1), ?int8(V2), ?uint8(V3), ?int16(V4), ?uint16(V5), ?int32(V6), ?uint32(V7), ?int64(V8), ?uint64(V9), ?integer(V10), ?integer(V11), ?integer(V12), ?integer(V13), ?integer(V14), ?integer(V15), ?integer(V16), ?integer(V17), ?number(V18), ?number(V19), ?number(V20), ?number(V21), ?number(V22), ?number(V23), ?number(V24), ?number(V25), ?number(V26), ?number(V27), ?float(V28), ?double(V29), ?string(V30), ?string(V31), ?record(V32), ?list_bool(V33), ?list_int8(V34), ?list_uint8(V35), ?list_int16(V36), ?list_uint16(V37), ?list_int32(V38), ?list_uint32(V39), ?list_int64(V40), ?list_uint64(V41), ?list_integer(V42), ?list_integer(V43), ?list_integer(V44), ?list_integer(V45), ?list_number(V46), ?list_number(V47), ?list_number(V48), ?list_number(V49), ?list_number(V50), ?list_number(V51), ?list_float(V52), ?list_double(V53), ?list_string(V54), ?list_record(V55)]; +encodeRec({person1, V1, V2, V3, V4}) -> + [?string(V1), ?int32(V2), ?string(V3), ?list_record(V4)]; encodeRec(_) -> []. @@ -667,6 +671,10 @@ getMsgSchema(35)-> {list,double}, {list,string}, {list,union}]; +getMsgSchema(person1)-> + getMsgSchema(1001); +getMsgSchema(1001)-> + [string,int32,string,{list,phoneNumber}]; getMsgSchema(_) -> []. diff --git a/test/protoMsg.hrl b/test/protoMsg.hrl index cc4b14f..194c3bd 100644 --- a/test/protoMsg.hrl +++ b/test/protoMsg.hrl @@ -8,6 +8,24 @@ -opaque uint64() :: 0..18446744073709551615. -opaque double() :: float(). +-define(err1, 1). %% 辅导费 +-define(err2, 2). %% 444 +-define(err3, 3). %% 辅导费 +-define(err4, 4). %% dfsf +-define(err5, 5). %% 其他注释辅导费 +-define(err6, 6). %% dfff +-define(err7, 7). %% def +-define(err8, 8). %% 其他注释辅导费 +-define(err1, 1001). %% 辅导费 +-define(err2, 1002). %% 444 +-define(err3, 1003). %% 辅导费 +-define(err4, 1004). %% dfsf +-define(err5, 1005). %% 其他注释辅导费 +-define(err6, 1006). %% dfff +-define(err7, 1007). %% def +-define(err8, 1008). %% 其他注释辅导费 + + -record(test ,{ aa = "" :: string() }). @@ -200,3 +218,9 @@ , lstring = [] :: [string()] , lunion = [] :: [#union{}] }). +-record(person1 ,{ + name = "" :: string() + , id = 0 :: int32() + , email = "" :: string() + , phone = [] :: [#phoneNumber{}] +}). diff --git a/test/test.erl b/test/test.erl index 4e3ec3d..04871fc 100644 --- a/test/test.erl +++ b/test/test.erl @@ -520,16 +520,6 @@ ttt(N, Bin) -> binary_to_term(Bin), ttt(N-1, Bin). - - - - - - - - - - print_bin(Bin) -> ByteList = lists:reverse(bin_to_hex(Bin, [])), Fun = fun(Byte, Acc) ->