From b3536f31b79d9b50bc0593ddac4f25af8b30a96f Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Tue, 10 Jun 2025 22:35:12 +0800 Subject: [PATCH] fix parser --- apps/modbus/src/modbus_parser.erl | 29 ++++++++++++++++++----------- apps/modbus/src/test.erl | 1 + 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/apps/modbus/src/modbus_parser.erl b/apps/modbus/src/modbus_parser.erl index aabd0e7..8aeb7ca 100644 --- a/apps/modbus/src/modbus_parser.erl +++ b/apps/modbus/src/modbus_parser.erl @@ -30,16 +30,17 @@ parse_file(Filename) -> lexer(Input) -> lexer(Input, 1, [], []). -lexer(<<>>, _Line, _Current, Acc) -> lists:reverse(Acc); +lexer(<<>>, _Line, _Current, Acc) -> + lists:reverse(Acc); lexer(<<$\s, Rest/binary>>, Line, Current, Acc) -> - lexer(Rest, Line, Current, Acc); + lexer(Rest, Line, [" "|Current], Acc); lexer(<<$\n, Rest/binary>>, Line, Current, Acc) -> - lexer(Rest, Line+1, Current, Acc); + lexer(Rest, Line + 1, Current, Acc); lexer(<<$\t, Rest/binary>>, Line, Current, Acc) -> lexer(Rest, Line, Current, Acc); lexer(<<$#, Rest/binary>>, Line, _Current, Acc) -> {Comment, NewRest} = read_until(Rest, <<$\n>>), - lexer(NewRest, Line+1, [], [{comment, Line, Comment}|Acc]); + lexer(NewRest, Line + 1, [], [{comment, Line, Comment}|Acc]); lexer(<<${, Rest/binary>>, Line, [], Acc) -> lexer(Rest, Line, [], [{'{', Line}|Acc]); lexer(<<$}, Rest/binary>>, Line, [], Acc) -> @@ -50,18 +51,21 @@ lexer(<>, Line, Current, Acc) -> case is_special(Char) of true -> NewAcc = case Current of - [] -> Acc; - _ -> [{ident, Line, list_to_binary(lists:reverse(Current))}|Acc] + [] -> + Acc; + _ -> + Ident = list_to_binary(string:trim(lists:reverse(Current))), + [{ident, Line, Ident}|Acc] end, lexer(Rest, Line, [], [{special, Line, Char}|NewAcc]); false -> lexer(Rest, Line, [Char|Current], Acc) end. -read_until(Bin, Delim) -> - case binary:match(Bin, <>) of +read_until(Bin, Delim) when is_binary(Bin), is_binary(Delim) -> + case binary:match(Bin, Delim) of {Pos, _} -> - {binary:part(Bin, 0, Pos), binary:part(Bin, Pos+1, byte_size(Bin)-Pos-1)}; + {binary:part(Bin, 0, Pos), binary:part(Bin, Pos + 1, byte_size(Bin) - Pos - 1)}; nomatch -> {Bin, <<>>} end. @@ -74,9 +78,12 @@ is_special(_) -> false. %% 语法分析:将标记序列转换为AST parser(Tokens) -> + lists:foreach(fun(T) -> + lager:debug("token is: ~p", [T]) + end, Tokens), parser(Tokens, []). -parser([], [Result]) -> {ok, Result}; +parser([], Result) -> {ok, Result}; parser([{'{', _}|Tokens], Stack) -> parser(Tokens, [open_block|Stack]); parser([{'}', _}|Tokens], [open_block, {block, Type, Props}|Stack]) -> @@ -86,7 +93,7 @@ parser([{ident, _, <<"modbus">>}|Tokens], Stack) -> parser(Tokens, [{block, modbus, []}|Stack]); parser([{ident, _, <<"device">>}, {ident, _, Name}|Tokens], Stack) -> parser(Tokens, [{block, {device, Name}, []}|Stack]); -parser([{ident, Line, Name}, {special, _, $=}|Tokens], [{block, _, Props}|Stack]) -> +parser([{ident, _Line, Name}, {special, _, $=}|Tokens], [{block, _, Props}|Stack]) -> {Value, Rest} = parse_value(Tokens, []), parser(Rest, [{block, Props#{binary_to_atom(Name, utf8) => Value}}|Stack]); parser([{';', _}|Tokens], Stack) -> diff --git a/apps/modbus/src/test.erl b/apps/modbus/src/test.erl index 3eb26b0..d7926b4 100644 --- a/apps/modbus/src/test.erl +++ b/apps/modbus/src/test.erl @@ -14,6 +14,7 @@ test() -> {ok, Config} = file:read_file("/usr/local/code/cloudkit/modbus/modbus.conf"), + lager:debug("config is: ~ts", [Config]), {ok, AST} = modbus_parser:parse(Config), modbus_parser:validate(AST),