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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
class StateTransition
attr_accessor :to_state
def initialize(to_state)
@to_state = to_state
end
end
def transition_to(state)
StateTransition.new(state)
end
$states = {}
$states[:ANYWHERE] = {
0x18 => [:execute, transition_to(:GROUND)],
0x1a => [:execute, transition_to(:GROUND)],
0x80..0x8f => [:execute, transition_to(:GROUND)],
0x91..0x97 => [:execute, transition_to(:GROUND)],
0x99 => [:execute, transition_to(:GROUND)],
0x9a => [:execute, transition_to(:GROUND)],
0x9c => [:execute, transition_to(:GROUND)],
0x1b => transition_to(:ESCAPE),
0x98 => transition_to(:SOS_PM_APC_STRING),
0x9e => transition_to(:SOS_PM_APC_STRING),
0x9f => transition_to(:SOS_PM_APC_STRING),
0x90 => transition_to(:DCS_ENTRY),
0x9d => transition_to(:OSC_STRING),
0x9b => transition_to(:CSI_ENTRY),
0xc0..0xdf => [:print, transition_to(:UTF8)], # UTF-8 2 bytes sequence start
0xe0..0xef => [:print, transition_to(:UTF8)], # UTF-8 3 bytes sequence start
0xf0..0xf7 => [:print, transition_to(:UTF8)], # UTF-8 4 bytes sequence start
0xf8..0xfb => [:print, transition_to(:UTF8)], # UTF-8 5 bytes sequence start
0xfc..0xfd => [:print, transition_to(:UTF8)] # UTF-8 6 bytes sequence start
}
$states[:GROUND] = {
0x00..0x17 => :execute,
0x19 => :execute,
0x1c..0x1f => :execute,
0x20..0x7f => :print,
0x80..0x8f => :execute,
0x91..0x9a => :execute,
0x9c => :execute,
0xc0..0xdf => [:print, transition_to(:UTF8)], # UTF-8 2 bytes sequence start
0xe0..0xef => [:print, transition_to(:UTF8)], # UTF-8 3 bytes sequence start
0xf0..0xf7 => [:print, transition_to(:UTF8)], # UTF-8 4 bytes sequence start
0xf8..0xfb => [:print, transition_to(:UTF8)], # UTF-8 5 bytes sequence start
0xfc..0xfd => [:print, transition_to(:UTF8)] # UTF-8 6 bytes sequence start
}
$states[:UTF8] = {
0x00..0x17 => [:execute, transition_to(:GROUND)],
0x19 => [:execute, transition_to(:GROUND)],
0x1c..0x1f => [:execute, transition_to(:GROUND)],
0x20..0x7f => [:print, transition_to(:GROUND)],
0x80..0xbf => :print, # UTF-8 sequence
0xc0..0xdf => :print, # UTF-8 2 bytes sequence start
0xe0..0xef => :print, # UTF-8 3 bytes sequence start
0xf0..0xf7 => :print, # UTF-8 4 bytes sequence start
0xf8..0xfb => :print, # UTF-8 5 bytes sequence start
0xfc..0xfd => :print, # UTF-8 6 bytes sequence start
0xfe..0xff => transition_to(:GROUND)
}
$states[:ESCAPE] = {
:on_entry => :clear,
0x00..0x17 => :execute,
0x19 => :execute,
0x1c..0x1f => :execute,
0x7f => :ignore,
0x20..0x2f => [:collect, transition_to(:ESCAPE_INTERMEDIATE)],
0x30..0x4f => [:esc_dispatch, transition_to(:GROUND)],
0x51..0x57 => [:esc_dispatch, transition_to(:GROUND)],
0x59 => [:esc_dispatch, transition_to(:GROUND)],
0x5a => [:esc_dispatch, transition_to(:GROUND)],
0x5c => [:esc_dispatch, transition_to(:GROUND)],
0x60..0x7e => [:esc_dispatch, transition_to(:GROUND)],
0x5b => transition_to(:CSI_ENTRY),
0x5d => transition_to(:OSC_STRING),
0x50 => transition_to(:DCS_ENTRY),
0x58 => transition_to(:SOS_PM_APC_STRING),
0x5e => transition_to(:SOS_PM_APC_STRING),
0x5f => transition_to(:SOS_PM_APC_STRING),
}
$states[:ESCAPE_INTERMEDIATE] = {
0x00..0x17 => :execute,
0x19 => :execute,
0x1c..0x1f => :execute,
0x20..0x2f => :collect,
0x7f => :ignore,
0x30..0x7e => [:esc_dispatch, transition_to(:GROUND)]
}
$states[:CSI_ENTRY] = {
:on_entry => :clear,
0x00..0x17 => :execute,
0x19 => :execute,
0x1c..0x1f => :execute,
0x7f => :ignore,
0x20..0x2f => [:collect, transition_to(:CSI_INTERMEDIATE)],
0x3a => transition_to(:CSI_IGNORE),
0x30..0x39 => [:param, transition_to(:CSI_PARAM)],
0x3b => [:param, transition_to(:CSI_PARAM)],
0x3c..0x3f => [:collect, transition_to(:CSI_PARAM)],
0x40..0x7e => [:csi_dispatch, transition_to(:GROUND)]
}
$states[:CSI_IGNORE] = {
0x00..0x17 => :execute,
0x19 => :execute,
0x1c..0x1f => :execute,
0x20..0x3f => :ignore,
0x7f => :ignore,
0x40..0x7e => transition_to(:GROUND),
}
$states[:CSI_PARAM] = {
0x00..0x17 => :execute,
0x19 => :execute,
0x1c..0x1f => :execute,
0x30..0x39 => :param,
0x3b => :param,
0x7f => :ignore,
0x3a => transition_to(:CSI_IGNORE),
0x3c..0x3f => transition_to(:CSI_IGNORE),
0x20..0x2f => [:collect, transition_to(:CSI_INTERMEDIATE)],
0x40..0x7e => [:csi_dispatch, transition_to(:GROUND)]
}
$states[:CSI_INTERMEDIATE] = {
0x00..0x17 => :execute,
0x19 => :execute,
0x1c..0x1f => :execute,
0x20..0x2f => :collect,
0x7f => :ignore,
0x30..0x3f => transition_to(:CSI_IGNORE),
0x40..0x7e => [:csi_dispatch, transition_to(:GROUND)],
}
$states[:DCS_ENTRY] = {
:on_entry => :clear,
0x00..0x17 => :ignore,
0x19 => :ignore,
0x1c..0x1f => :ignore,
0x7f => :ignore,
0x3a => transition_to(:DCS_IGNORE),
0x20..0x2f => [:collect, transition_to(:DCS_INTERMEDIATE)],
0x30..0x39 => [:param, transition_to(:DCS_PARAM)],
0x3b => [:param, transition_to(:DCS_PARAM)],
0x3c..0x3f => [:collect, transition_to(:DCS_PARAM)],
0x40..0x7e => [transition_to(:DCS_PASSTHROUGH)]
}
$states[:DCS_INTERMEDIATE] = {
0x00..0x17 => :ignore,
0x19 => :ignore,
0x1c..0x1f => :ignore,
0x20..0x2f => :collect,
0x7f => :ignore,
0x30..0x3f => transition_to(:DCS_IGNORE),
0x40..0x7e => transition_to(:DCS_PASSTHROUGH)
}
$states[:DCS_IGNORE] = {
0x00..0x17 => :ignore,
0x19 => :ignore,
0x1c..0x1f => :ignore,
0x20..0x7f => :ignore,
0x9c => transition_to(:GROUND)
}
$states[:DCS_PARAM] = {
0x00..0x17 => :ignore,
0x19 => :ignore,
0x1c..0x1f => :ignore,
0x30..0x39 => :param,
0x3b => :param,
0x7f => :ignore,
0x3a => transition_to(:DCS_IGNORE),
0x3c..0x3f => transition_to(:DCS_IGNORE),
0x20..0x2f => [:collect, transition_to(:DCS_INTERMEDIATE)],
0x40..0x7e => transition_to(:DCS_PASSTHROUGH)
}
$states[:DCS_PASSTHROUGH] = {
:on_entry => :hook,
0x00..0x17 => :put,
0x19 => :put,
0x1c..0x1f => :put,
0x20..0x7e => :put,
0x7f => :ignore,
0x9c => transition_to(:GROUND),
:on_exit => :unhook
}
$states[:SOS_PM_APC_STRING] = {
0x00..0x17 => :ignore,
0x19 => :ignore,
0x1c..0x1f => :ignore,
0x20..0x7f => :ignore,
0x9c => transition_to(:GROUND)
}
$states[:OSC_STRING] = {
:on_entry => :osc_start,
0x00..0x17 => :ignore,
0x19 => :ignore,
0x1c..0x1f => :ignore,
0x20..0x7f => :osc_put,
0x9c => transition_to(:GROUND),
:on_exit => :osc_end
}
$states.each { |state, transitions|
transitions.each { |keys, actions|
if not actions.kind_of?(Array)
$states[state][keys] = [actions]
end
}
}
# get the list of actions implicit in the tables
action_names = {}
$states.each { |state, transitions|
transitions.each { |keys, actions|
actions.each { |action|
if action.kind_of?(Symbol)
action_names[action] = 1
end
}
}
}
# establish an ordering to the states and actions
$actions_in_order = action_names.keys.sort { |a1, a2| a1.to_s <=> a2.to_s }
$states_in_order = $states.keys.sort { |s1, s2| s1.to_s <=> s2.to_s }
#
# Expand the above range-based data structures (which are convenient
# to write) into fully expanded tables (which are easier to use).
#
$state_tables = {}
def expand_ranges(hash_with_ranges_as_keys)
array = []
hash_with_ranges_as_keys.each { |range, val|
if range.kind_of?(Range)
range.each { |i|
array[i] = val
}
elsif range.kind_of?(Fixnum)
array[range] = val
end
}
array
end
$states.each { |state, transitions|
$state_tables[state] = expand_ranges(transitions)
}
|