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