解析和验证ast
This commit is contained in:
parent
27a87c29d8
commit
39f05e2796
@ -48,7 +48,7 @@ lexer(<<$\n, Rest/binary>>, Line, 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>>),
|
||||
{Comment, NewRest} = modbus_util:read_until(Rest, <<$\n>>),
|
||||
lexer(NewRest, Line + 1, [], [{comment, Line, Comment}|Acc]);
|
||||
%% 处理特殊字符开头的行, ‘$=’ 不会是一行的开头
|
||||
lexer(<<${, Rest/binary>>, Line, [], Acc) ->
|
||||
@ -72,14 +72,6 @@ lexer(<<Char/utf8, Rest/binary>>, Line, Current, Acc) ->
|
||||
lexer(Rest, Line, [Char|Current], Acc)
|
||||
end.
|
||||
|
||||
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)};
|
||||
nomatch ->
|
||||
{Bin, <<>>}
|
||||
end.
|
||||
|
||||
is_special(${) -> true;
|
||||
is_special($}) -> true;
|
||||
is_special($;) -> true;
|
||||
@ -165,7 +157,7 @@ parse_ast1(Props) ->
|
||||
parse_ast1([], Acc) ->
|
||||
maps:from_list(lists:reverse(Acc));
|
||||
parse_ast1([Bin|T], Acc) when is_binary(Bin) ->
|
||||
[Name|Vars] = binary:split(Bin, <<" ">>, [global, trim]),
|
||||
[Name|Vars] = binary:split(Bin, <<" ">>, [trim]),
|
||||
parse_ast1(T, [{Name, Vars}|Acc]);
|
||||
parse_ast1([#block{ident = <<"actions">>, props = Props}|T], Acc) ->
|
||||
parse_ast1(T, [{<<"actions">>, Props}|Acc]);
|
||||
|
||||
41
apps/modbus/src/modbus_util.erl
Normal file
41
apps/modbus/src/modbus_util.erl
Normal file
@ -0,0 +1,41 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @author anlicheng
|
||||
%%% @copyright (C) 2025, <COMPANY>
|
||||
%%% @doc
|
||||
%%%
|
||||
%%% @end
|
||||
%%% Created : 11. 6月 2025 23:51
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(modbus_util).
|
||||
-author("anlicheng").
|
||||
|
||||
%% API
|
||||
-export([split/1, read_until/2]).
|
||||
|
||||
%% 按照空格分割字符串, 需要将引号里面的字符看成一个整体
|
||||
-spec split(Bin :: binary()) -> [binary()].
|
||||
split(Bin) when is_binary(Bin) ->
|
||||
split(Bin, [], []).
|
||||
split(<<>>, [], Acc) ->
|
||||
lists:reverse(Acc);
|
||||
split(<<>>, Target, Acc) ->
|
||||
lists:reverse([Target|Acc]);
|
||||
split(<<$", Bin/binary>>, [], Acc) ->
|
||||
{Item, ResetBin} = read_until(Bin, <<$">>),
|
||||
split(ResetBin, [], [Item|Acc]);
|
||||
split(<<$\s, Bin/binary>>, [], Acc) ->
|
||||
split(Bin, [], Acc);
|
||||
split(<<$\s, Bin/binary>>, Target, Acc) ->
|
||||
NTarget = iolist_to_binary(lists:reverse(Target)),
|
||||
lager:debug("ntarget: ~p", [NTarget]),
|
||||
split(Bin, [], [NTarget|Acc]);
|
||||
split(<<Char/utf8, Bin/binary>>, Target, Acc) ->
|
||||
split(Bin, [Char|Target], Acc).
|
||||
|
||||
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)};
|
||||
nomatch ->
|
||||
{Bin, <<>>}
|
||||
end.
|
||||
Loading…
x
Reference in New Issue
Block a user