fix
This commit is contained in:
parent
dd234af4d0
commit
2fac4de604
@ -12,6 +12,14 @@
|
|||||||
%% API
|
%% API
|
||||||
-export([parse/1, test/0]).
|
-export([parse/1, test/0]).
|
||||||
|
|
||||||
|
-record(or_expr, {
|
||||||
|
expr = []
|
||||||
|
}).
|
||||||
|
|
||||||
|
-record(and_expr, {
|
||||||
|
expr = []
|
||||||
|
}).
|
||||||
|
|
||||||
test() ->
|
test() ->
|
||||||
%Rule = <<"SELECT * FROM service.data WHERE id > 0 AND (name = 'anlicheng' OR name = 'test')">>,
|
%Rule = <<"SELECT * FROM service.data WHERE id > 0 AND (name = 'anlicheng' OR name = 'test')">>,
|
||||||
%parse(Rule),
|
%parse(Rule),
|
||||||
@ -22,14 +30,17 @@ test() ->
|
|||||||
|
|
||||||
scan(Tokens) ->
|
scan(Tokens) ->
|
||||||
ExprList = scan_or(Tokens),
|
ExprList = scan_or(Tokens),
|
||||||
lists:map(fun({_, Expr}) ->
|
ORChildExpr = lists:map(fun(E) ->
|
||||||
scan_and(Expr)
|
case E of
|
||||||
end, ExprList).
|
{simple, Expr} ->
|
||||||
|
AndChildExpr = scan_and(Expr),
|
||||||
scan_each([]) ->
|
#and_expr{expr = AndChildExpr};
|
||||||
[];
|
{complex, Expr} ->
|
||||||
scan_each([H|Tail]) when length(H) > 1 ->
|
AndChildExpr = scan_and(Expr),
|
||||||
[scan(H)|scan_each(Tail)].
|
#and_expr{expr = AndChildExpr}
|
||||||
|
end
|
||||||
|
end, ExprList),
|
||||||
|
#or_expr{expr = ORChildExpr}.
|
||||||
|
|
||||||
scan_or(Tokens) ->
|
scan_or(Tokens) ->
|
||||||
lager:debug("tokens: ~p", [Tokens]),
|
lager:debug("tokens: ~p", [Tokens]),
|
||||||
@ -87,7 +98,7 @@ parse(Rule) ->
|
|||||||
parse_condition(Condition) when is_binary(Condition) ->
|
parse_condition(Condition) when is_binary(Condition) ->
|
||||||
Tokens = unicode:characters_to_list(Condition),
|
Tokens = unicode:characters_to_list(Condition),
|
||||||
X = scan(Tokens),
|
X = scan(Tokens),
|
||||||
lists:foreach(fun(E) -> lager:debug("E is: ~p", [E]) end, X),
|
lager:debug("E is: ~p", [X]),
|
||||||
X.
|
X.
|
||||||
|
|
||||||
parse_token(Tokens) ->
|
parse_token(Tokens) ->
|
||||||
@ -114,6 +125,19 @@ parse_token([Token|Tokens], Expr, Acc, Level) when Token == $) ->
|
|||||||
parse_token([Token|Tokens], Expr, Acc, Level) ->
|
parse_token([Token|Tokens], Expr, Acc, Level) ->
|
||||||
parse_token(Tokens, [Token|Expr], Acc, Level).
|
parse_token(Tokens, [Token|Expr], Acc, Level).
|
||||||
|
|
||||||
|
trim([$(|Tokens]) ->
|
||||||
|
trim0(Tokens, [], 0);
|
||||||
|
trim(Tokens) ->
|
||||||
|
Tokens.
|
||||||
|
trim0([$)], Acc, 1) ->
|
||||||
|
{ok, Acc};
|
||||||
|
trim0([$(|Tokens], Acc, Level) ->
|
||||||
|
trim0(Tokens, [$(|Acc], Level + 1);
|
||||||
|
trim0([$)|Tokens], Acc, Level) ->
|
||||||
|
trim0(Tokens, [$)|Acc], Level - 1);
|
||||||
|
trim0([H|Tokens], Acc, Level) ->
|
||||||
|
trim0(Tokens, [H|Acc], Level).
|
||||||
|
|
||||||
%% 处理表达式的优先级关系
|
%% 处理表达式的优先级关系
|
||||||
generate([{simple, Expr}|ExprList], Data) ->
|
generate([{simple, Expr}|ExprList], Data) ->
|
||||||
Tokens = string:tokens(Expr, "OR"),
|
Tokens = string:tokens(Expr, "OR"),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user