-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenize.ex
More file actions
61 lines (49 loc) · 1.9 KB
/
tokenize.ex
File metadata and controls
61 lines (49 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
defmodule Tokenize do
def main(args) do
str1 = "%{ [ ] =>, , , , ,, ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ] ] ] ] ] ] ] [ [ [ [ [ ] ] ] ] ][ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ => } "
str2 = "%{ [ ] =>, , , , ,, ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ] ] ] ] ] ] ] [ [ [ [ [ ] ] ] ] ][ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ => } "
str3 = "%{ [ ] =>, , , , ,, ,, => , , , , , , , , , , , , , , [][][][][] , , , , ] ] ] ] ] ] ] [ [ [ [ [ ] ] ] ] ][ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ => } "
list = [ str1, str2, str3 ]
result = Enum.map(list, fn(str) ->
:timer.tc(fn -> Tokenize.tokenize(str) end)
end)
IO.inspect(result)
end
def tokenize(str) do
result = []
final = _tokenize(str, result)
final = Enum.reverse(final)
end
# match the empty string (must be before)
def _tokenize("--end--", result) do
result
end
def _tokenize("", result) do
result
end
def _tokenize(str, result) do
lex = Regex.run(~r/%{|}|\[|\]|=>|\,/, str)
#IO.inspect(lex)
match = case List.first(lex) do
"%{" -> { :tk_start_hash, "%{"}
"}" -> { :tk_end_hash, "}" }
"=>" -> { :tk_kv, "=>" }
"\"" -> { :tk_quote_value, "\"" }
"," -> { :tk_comma, "," }
"#" -> { :tk_object_value, "#" }
"[" -> { :tk_start_array, "[" }
"]" -> { :tk_end_array, "]" }
_ -> { :tk_done, "_" }
end
{ token, matcher } = match
case token do
:tk_done ->
_tokenize("--end--", [ result | match ])
_ ->
base = String.length(matcher)
{_, str_out} = String.split_at(str, base)
#str_out = String.slice(str, base..-1)
_tokenize(String.lstrip(str_out), [ match | result ])
end
end
end