fix parser
This commit is contained in:
parent
c965fcd17b
commit
b3536f31b7
@ -30,9 +30,10 @@ parse_file(Filename) ->
|
|||||||
lexer(Input) ->
|
lexer(Input) ->
|
||||||
lexer(Input, 1, [], []).
|
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(<<$\s, Rest/binary>>, Line, Current, Acc) ->
|
||||||
lexer(Rest, Line, Current, Acc);
|
lexer(Rest, Line, [" "|Current], Acc);
|
||||||
lexer(<<$\n, Rest/binary>>, 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(<<$\t, Rest/binary>>, Line, Current, Acc) ->
|
||||||
@ -50,16 +51,19 @@ lexer(<<Char/utf8, Rest/binary>>, Line, Current, Acc) ->
|
|||||||
case is_special(Char) of
|
case is_special(Char) of
|
||||||
true ->
|
true ->
|
||||||
NewAcc = case Current of
|
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,
|
end,
|
||||||
lexer(Rest, Line, [], [{special, Line, Char}|NewAcc]);
|
lexer(Rest, Line, [], [{special, Line, Char}|NewAcc]);
|
||||||
false ->
|
false ->
|
||||||
lexer(Rest, Line, [Char|Current], Acc)
|
lexer(Rest, Line, [Char|Current], Acc)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
read_until(Bin, Delim) ->
|
read_until(Bin, Delim) when is_binary(Bin), is_binary(Delim) ->
|
||||||
case binary:match(Bin, <<Delim>>) of
|
case binary:match(Bin, Delim) of
|
||||||
{Pos, _} ->
|
{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 ->
|
nomatch ->
|
||||||
@ -74,9 +78,12 @@ is_special(_) -> false.
|
|||||||
|
|
||||||
%% 语法分析:将标记序列转换为AST
|
%% 语法分析:将标记序列转换为AST
|
||||||
parser(Tokens) ->
|
parser(Tokens) ->
|
||||||
|
lists:foreach(fun(T) ->
|
||||||
|
lager:debug("token is: ~p", [T])
|
||||||
|
end, Tokens),
|
||||||
parser(Tokens, []).
|
parser(Tokens, []).
|
||||||
|
|
||||||
parser([], [Result]) -> {ok, Result};
|
parser([], Result) -> {ok, Result};
|
||||||
parser([{'{', _}|Tokens], Stack) ->
|
parser([{'{', _}|Tokens], Stack) ->
|
||||||
parser(Tokens, [open_block|Stack]);
|
parser(Tokens, [open_block|Stack]);
|
||||||
parser([{'}', _}|Tokens], [open_block, {block, Type, Props}|Stack]) ->
|
parser([{'}', _}|Tokens], [open_block, {block, Type, Props}|Stack]) ->
|
||||||
@ -86,7 +93,7 @@ parser([{ident, _, <<"modbus">>}|Tokens], Stack) ->
|
|||||||
parser(Tokens, [{block, modbus, []}|Stack]);
|
parser(Tokens, [{block, modbus, []}|Stack]);
|
||||||
parser([{ident, _, <<"device">>}, {ident, _, Name}|Tokens], Stack) ->
|
parser([{ident, _, <<"device">>}, {ident, _, Name}|Tokens], Stack) ->
|
||||||
parser(Tokens, [{block, {device, Name}, []}|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, []),
|
{Value, Rest} = parse_value(Tokens, []),
|
||||||
parser(Rest, [{block, Props#{binary_to_atom(Name, utf8) => Value}}|Stack]);
|
parser(Rest, [{block, Props#{binary_to_atom(Name, utf8) => Value}}|Stack]);
|
||||||
parser([{';', _}|Tokens], Stack) ->
|
parser([{';', _}|Tokens], Stack) ->
|
||||||
|
|||||||
@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
test() ->
|
test() ->
|
||||||
{ok, Config} = file:read_file("/usr/local/code/cloudkit/modbus/modbus.conf"),
|
{ok, Config} = file:read_file("/usr/local/code/cloudkit/modbus/modbus.conf"),
|
||||||
|
lager:debug("config is: ~ts", [Config]),
|
||||||
{ok, AST} = modbus_parser:parse(Config),
|
{ok, AST} = modbus_parser:parse(Config),
|
||||||
|
|
||||||
modbus_parser:validate(AST),
|
modbus_parser:validate(AST),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user