解析和验证ast

This commit is contained in:
anlicheng 2025-06-11 23:52:58 +08:00
parent 27a87c29d8
commit 39f05e2796
2 changed files with 43 additions and 10 deletions

View File

@ -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]);

View 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.