-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenize.rb
55 lines (45 loc) · 1.73 KB
/
tokenize.rb
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
require 'benchmark'
module Tokenize
def self.tokenize(str)
result = []
while(1)
match = str.match(/%{|\}|\[|\]|=>|,/)
if match
val = case match[0]
when "%{"
{ tk_start_hash: "%{"}
when "}"
{ tk_end_hash: "}" }
when "=>"
{ tk_kv: "=>" }
when "#"
{ tk_object_value: "#" }
when "["
{ tk_start_array: "[" }
when "]"
{ tk_end_array: "]" }
when ","
{ tk_comma: "," }
else
{ unknown: "xxx"}
end
result << val
str = str.split(match[0], 2)
str = str[1]
else
break
end
end
puts "result #{result}"
end
end
str = "%{ [ ] =>, , , , ,, ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ] ] ] ] ] ] ] [ [ [ [ [ ] ] ] ] ][ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ => } "
str2 = "%{ [ ] =>, , , , ,, ,, , , , => [ ], , , , , , , , , , , , , , , , , , , , , , , , , , ] ] ] ] ] ] ] [ [ [ [ [ ] ] ] ] ][ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ => } "
str3 = "%{ [ ] =>, , , , , => => [ , , ] , , , , , , , , , , , , , , , , , , , , , , , , , , , ] ] ] ] ] ] ] [ [ [ [ [ ] ] ] ] ][ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ => } "
[str, str2, str3].each do | str |
beginning_time = Time.now
Tokenize.tokenize(str)
end_time = Time.now
puts "Time elapsed #{(end_time - beginning_time)*1000} milliseconds"
end
#puts Benchmark.measure { Tokenize.tokenize(str) }