forked from danielgatis/go-utf8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_tablegen.rb
91 lines (73 loc) · 1.58 KB
/
_tablegen.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env ruby
require "erb"
action_names = [
:invalidSequence,
:emitByte,
:setByte1,
:setByte2,
:setByte2Top,
:setByte3,
:setByte3Top,
:setByte4,
]
state_names = [
:ground,
:tail3,
:tail2,
:tail1,
:u32e0,
:u32ed,
:utf843f0,
:utf843f4,
]
states = {}
states[:ground] = {
0x00..0x7f => [:ground, :emitByte],
0xc2..0xdf => [:tail1, :setByte2Top],
0xe0 => [:u32e0, :setByte3Top],
0xe1..0xec => [:tail2, :setByte3Top],
0xed => [:u32ed, :setByte3Top],
0xee..0xef => [:tail2, :setByte3Top],
0xf0 => [:utf843f0, :setByte4],
0xf1..0xf3 => [:tail3, :setByte4],
0xf4 => [:utf843f4, :setByte4],
}
states[:u32e0] = {
0xa0..0xbf => [:tail1, :setByte2],
}
states[:u32ed] = {
0x80..0x9f => [:tail1, :setByte2],
}
states[:utf843f0] = {
0x90..0xbf => [:tail2, :setByte3],
}
states[:utf843f4] = {
0x80..0x8f => [:tail2, :setByte3],
}
states[:tail3] = {
0x80..0xbf => [:tail2, :setByte3],
}
states[:tail2] = {
0x80..0xbf => [:tail1, :setByte2],
}
states[:tail1] = {
0x80..0xbf => [:ground, :setByte1],
}
table = state_names.map do |state|
actions = states[state]
state_actions = Array.new(256, [:ground, :invalidSequence])
actions.each do |range, act|
Array(range).each do |r|
state_actions[r] = act
end
end
state_actions
end
if __FILE__ == $0
template_path = File.read(File.expand_path("_table.go.erb", File.dirname(__FILE__)))
out_path = File.expand_path("table.go", File.dirname(__FILE__))
renderer = ERB.new(template_path, nil, "-")
output = renderer.result(binding)
File.write(out_path, output)
`go fmt #{out_path}`
end