Skip to content

Commit

Permalink
tokenize/1 done
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderSLoburev committed Jan 3, 2024
1 parent 627c339 commit 496429d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
17 changes: 4 additions & 13 deletions common/utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,7 @@ try_parse_number(String) ->

is_digit(Char) -> lists:member(Char, lists:seq($0, $9)).

re_escape(Symbol) ->
case lists:member(Symbol, "[]\\^$.|?*+()") of
true ->
case is_integer(Symbol) of
true -> [$\ | [Symbol]];
false -> [$\ | Symbol]
end;
false ->
case is_integer(Symbol) of
true -> [Symbol];
false -> Symbol
end
end.
re_escape(String) ->
io:fwrite("Token:~p, is binary: ~p~n", [String, is_binary(String)]),
Escaped = re:replace(String, "[\\[\\]\\\\^$.|?*+()]", "\\\\&", [global]),
Escaped.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,18 @@ handle_token(Token, Stack, Position) ->


tokenize(Expression) ->
Tokens = re:split(Expression, "(\\d+\\.?\\d*|[-+*/%//**])"),
lists:filter(fun(X) -> X =/= "" end, Tokens).
Tokens = re:split(Expression, "\\s+"),
FilteredTokens = lists:filter(fun(X) -> (X =/= "") and not(lists:member(X, "\s\t\n")) end, Tokens),
io:fwrite("~p~n", [FilteredTokens]),
Positions = lists:flatmap(fun(Token) ->
case re:run(Expression, utils:re_escape(Token), [global]) of
nomatch -> [];
{match, Captured} ->
io:fwrite("~p:~p~n", [utils:re_escape(Token), Captured]),
lists:map(fun({Index, _Length}) -> {Token, Index + 1} end, lists:flatten(Captured))
end
end, FilteredTokens),
Positions.



Expand All @@ -41,4 +51,4 @@ tokenize(Expression) ->


start() ->
io:fwrite("~p~n", [tokenize(1 2 3 * + 4 -)]).
io:fwrite("~p~n", [tokenize("-11.4 2 3 / + * // ** % + 4 -")]).

0 comments on commit 496429d

Please sign in to comment.