| 1 |
package parser |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
const ( |
| 8 |
TransitionActionShift = 4 |
| 9 |
TransitionStateMask = 15 |
| 10 |
IndexStateShift = 8 |
| 11 |
|
| 12 |
|
| 13 |
DefaultTableSize = 4096 |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
var Table = GenerateTransitionTable() |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
type TransitionTable []byte |
| 22 |
|
| 23 |
|
| 24 |
func NewTransitionTable(size int) TransitionTable { |
| 25 |
if size <= 0 { |
| 26 |
size = DefaultTableSize |
| 27 |
} |
| 28 |
return TransitionTable(make([]byte, size)) |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
func (t TransitionTable) SetDefault(action Action, state State) { |
| 33 |
for i := range t { |
| 34 |
t[i] = action<<TransitionActionShift | state |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
func (t TransitionTable) AddOne(code byte, state State, action Action, next State) { |
| 40 |
idx := int(state)<<IndexStateShift | int(code) |
| 41 |
value := action<<TransitionActionShift | next |
| 42 |
t[idx] = value |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
func (t TransitionTable) AddMany(codes []byte, state State, action Action, next State) { |
| 47 |
for _, code := range codes { |
| 48 |
t.AddOne(code, state, action, next) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
func (t TransitionTable) AddRange(start, end byte, state State, action Action, next State) { |
| 54 |
for i := int(start); i <= int(end); i++ { |
| 55 |
t.AddOne(byte(i), state, action, next) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
func (t TransitionTable) Transition(state State, code byte) (State, Action) { |
| 61 |
index := int(state)<<IndexStateShift | int(code) |
| 62 |
value := t[index] |
| 63 |
return value & TransitionStateMask, value >> TransitionActionShift |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
func r(start, end byte) []byte { |
| 68 |
var a []byte |
| 69 |
for i := int(start); i <= int(end); i++ { |
| 70 |
a = append(a, byte(i)) |
| 71 |
} |
| 72 |
return a |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
func GenerateTransitionTable() TransitionTable { |
| 88 |
table := NewTransitionTable(DefaultTableSize) |
| 89 |
table.SetDefault(NoneAction, GroundState) |
| 90 |
|
| 91 |
|
| 92 |
for _, state := range r(GroundState, Utf8State) { |
| 93 |
|
| 94 |
table.AddMany([]byte{0x18, 0x1a, 0x99, 0x9a}, state, ExecuteAction, GroundState) |
| 95 |
table.AddRange(0x80, 0x8F, state, ExecuteAction, GroundState) |
| 96 |
table.AddRange(0x90, 0x97, state, ExecuteAction, GroundState) |
| 97 |
table.AddOne(0x9C, state, ExecuteAction, GroundState) |
| 98 |
|
| 99 |
table.AddOne(0x1B, state, ClearAction, EscapeState) |
| 100 |
|
| 101 |
table.AddOne(0x98, state, StartAction, SosStringState) |
| 102 |
|
| 103 |
table.AddOne(0x9E, state, StartAction, PmStringState) |
| 104 |
|
| 105 |
table.AddOne(0x9F, state, StartAction, ApcStringState) |
| 106 |
|
| 107 |
table.AddOne(0x9B, state, ClearAction, CsiEntryState) |
| 108 |
|
| 109 |
table.AddOne(0x90, state, ClearAction, DcsEntryState) |
| 110 |
|
| 111 |
table.AddOne(0x9D, state, StartAction, OscStringState) |
| 112 |
|
| 113 |
table.AddRange(0xC2, 0xDF, state, CollectAction, Utf8State) |
| 114 |
table.AddRange(0xE0, 0xEF, state, CollectAction, Utf8State) |
| 115 |
table.AddRange(0xF0, 0xF4, state, CollectAction, Utf8State) |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
table.AddRange(0x00, 0x17, GroundState, ExecuteAction, GroundState) |
| 120 |
table.AddOne(0x19, GroundState, ExecuteAction, GroundState) |
| 121 |
table.AddRange(0x1C, 0x1F, GroundState, ExecuteAction, GroundState) |
| 122 |
table.AddRange(0x20, 0x7E, GroundState, PrintAction, GroundState) |
| 123 |
table.AddOne(0x7F, GroundState, ExecuteAction, GroundState) |
| 124 |
|
| 125 |
|
| 126 |
table.AddRange(0x00, 0x17, EscapeIntermediateState, ExecuteAction, EscapeIntermediateState) |
| 127 |
table.AddOne(0x19, EscapeIntermediateState, ExecuteAction, EscapeIntermediateState) |
| 128 |
table.AddRange(0x1C, 0x1F, EscapeIntermediateState, ExecuteAction, EscapeIntermediateState) |
| 129 |
table.AddRange(0x20, 0x2F, EscapeIntermediateState, CollectAction, EscapeIntermediateState) |
| 130 |
table.AddOne(0x7F, EscapeIntermediateState, IgnoreAction, EscapeIntermediateState) |
| 131 |
|
| 132 |
table.AddRange(0x30, 0x7E, EscapeIntermediateState, DispatchAction, GroundState) |
| 133 |
|
| 134 |
|
| 135 |
table.AddRange(0x00, 0x17, EscapeState, ExecuteAction, EscapeState) |
| 136 |
table.AddOne(0x19, EscapeState, ExecuteAction, EscapeState) |
| 137 |
table.AddRange(0x1C, 0x1F, EscapeState, ExecuteAction, EscapeState) |
| 138 |
table.AddOne(0x7F, EscapeState, IgnoreAction, EscapeState) |
| 139 |
|
| 140 |
table.AddRange(0x30, 0x4F, EscapeState, DispatchAction, GroundState) |
| 141 |
table.AddRange(0x51, 0x57, EscapeState, DispatchAction, GroundState) |
| 142 |
table.AddOne(0x59, EscapeState, DispatchAction, GroundState) |
| 143 |
table.AddOne(0x5A, EscapeState, DispatchAction, GroundState) |
| 144 |
table.AddOne(0x5C, EscapeState, DispatchAction, GroundState) |
| 145 |
table.AddRange(0x60, 0x7E, EscapeState, DispatchAction, GroundState) |
| 146 |
|
| 147 |
table.AddRange(0x20, 0x2F, EscapeState, CollectAction, EscapeIntermediateState) |
| 148 |
|
| 149 |
table.AddOne('X', EscapeState, StartAction, SosStringState) |
| 150 |
table.AddOne('^', EscapeState, StartAction, PmStringState) |
| 151 |
table.AddOne('_', EscapeState, StartAction, ApcStringState) |
| 152 |
|
| 153 |
table.AddOne('P', EscapeState, ClearAction, DcsEntryState) |
| 154 |
|
| 155 |
table.AddOne('[', EscapeState, ClearAction, CsiEntryState) |
| 156 |
|
| 157 |
table.AddOne(']', EscapeState, StartAction, OscStringState) |
| 158 |
|
| 159 |
|
| 160 |
for _, state := range r(SosStringState, ApcStringState) { |
| 161 |
table.AddRange(0x00, 0x17, state, PutAction, state) |
| 162 |
table.AddOne(0x19, state, PutAction, state) |
| 163 |
table.AddRange(0x1C, 0x1F, state, PutAction, state) |
| 164 |
table.AddRange(0x20, 0x7F, state, PutAction, state) |
| 165 |
|
| 166 |
table.AddOne(0x1B, state, DispatchAction, EscapeState) |
| 167 |
table.AddOne(0x9C, state, DispatchAction, GroundState) |
| 168 |
table.AddMany([]byte{0x18, 0x1A}, state, IgnoreAction, GroundState) |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
table.AddRange(0x00, 0x07, DcsEntryState, IgnoreAction, DcsEntryState) |
| 173 |
table.AddRange(0x0E, 0x17, DcsEntryState, IgnoreAction, DcsEntryState) |
| 174 |
table.AddOne(0x19, DcsEntryState, IgnoreAction, DcsEntryState) |
| 175 |
table.AddRange(0x1C, 0x1F, DcsEntryState, IgnoreAction, DcsEntryState) |
| 176 |
table.AddOne(0x7F, DcsEntryState, IgnoreAction, DcsEntryState) |
| 177 |
|
| 178 |
table.AddRange(0x20, 0x2F, DcsEntryState, CollectAction, DcsIntermediateState) |
| 179 |
|
| 180 |
table.AddRange(0x30, 0x3B, DcsEntryState, ParamAction, DcsParamState) |
| 181 |
table.AddRange(0x3C, 0x3F, DcsEntryState, PrefixAction, DcsParamState) |
| 182 |
|
| 183 |
table.AddRange(0x08, 0x0D, DcsEntryState, PutAction, DcsStringState) 7 |
| 184 |
|
| 185 |
|
| 186 |
table.AddOne(0x1B, DcsEntryState, PutAction, DcsStringState) |
| 187 |
table.AddRange(0x40, 0x7E, DcsEntryState, StartAction, DcsStringState) |
| 188 |
|
| 189 |
|
| 190 |
table.AddRange(0x00, 0x17, DcsIntermediateState, IgnoreAction, DcsIntermediateState) |
| 191 |
table.AddOne(0x19, DcsIntermediateState, IgnoreAction, DcsIntermediateState) |
| 192 |
table.AddRange(0x1C, 0x1F, DcsIntermediateState, IgnoreAction, DcsIntermediateState) |
| 193 |
table.AddRange(0x20, 0x2F, DcsIntermediateState, CollectAction, DcsIntermediateState) |
| 194 |
table.AddOne(0x7F, DcsIntermediateState, IgnoreAction, DcsIntermediateState) |
| 195 |
|
| 196 |
table.AddRange(0x30, 0x3F, DcsIntermediateState, StartAction, DcsStringState) |
| 197 |
table.AddRange(0x40, 0x7E, DcsIntermediateState, StartAction, DcsStringState) |
| 198 |
|
| 199 |
|
| 200 |
table.AddRange(0x00, 0x17, DcsParamState, IgnoreAction, DcsParamState) |
| 201 |
table.AddOne(0x19, DcsParamState, IgnoreAction, DcsParamState) |
| 202 |
table.AddRange(0x1C, 0x1F, DcsParamState, IgnoreAction, DcsParamState) |
| 203 |
table.AddRange(0x30, 0x3B, DcsParamState, ParamAction, DcsParamState) |
| 204 |
table.AddOne(0x7F, DcsParamState, IgnoreAction, DcsParamState) |
| 205 |
table.AddRange(0x3C, 0x3F, DcsParamState, IgnoreAction, DcsParamState) |
| 206 |
|
| 207 |
table.AddRange(0x20, 0x2F, DcsParamState, CollectAction, DcsIntermediateState) |
| 208 |
|
| 209 |
table.AddRange(0x40, 0x7E, DcsParamState, StartAction, DcsStringState) |
| 210 |
|
| 211 |
|
| 212 |
table.AddRange(0x00, 0x17, DcsStringState, PutAction, DcsStringState) |
| 213 |
table.AddOne(0x19, DcsStringState, PutAction, DcsStringState) |
| 214 |
table.AddRange(0x1C, 0x1F, DcsStringState, PutAction, DcsStringState) |
| 215 |
table.AddRange(0x20, 0x7E, DcsStringState, PutAction, DcsStringState) |
| 216 |
table.AddOne(0x7F, DcsStringState, PutAction, DcsStringState) |
| 217 |
table.AddRange(0x80, 0xFF, DcsStringState, PutAction, DcsStringState) |
| 218 |
|
| 219 |
table.AddOne(0x1B, DcsStringState, DispatchAction, EscapeState) |
| 220 |
table.AddOne(0x9C, DcsStringState, DispatchAction, GroundState) |
| 221 |
table.AddMany([]byte{0x18, 0x1A}, DcsStringState, IgnoreAction, GroundState) |
| 222 |
|
| 223 |
|
| 224 |
table.AddRange(0x00, 0x17, CsiParamState, ExecuteAction, CsiParamState) |
| 225 |
table.AddOne(0x19, CsiParamState, ExecuteAction, CsiParamState) |
| 226 |
table.AddRange(0x1C, 0x1F, CsiParamState, ExecuteAction, CsiParamState) |
| 227 |
table.AddRange(0x30, 0x3B, CsiParamState, ParamAction, CsiParamState) |
| 228 |
table.AddOne(0x7F, CsiParamState, IgnoreAction, CsiParamState) |
| 229 |
table.AddRange(0x3C, 0x3F, CsiParamState, IgnoreAction, CsiParamState) |
| 230 |
|
| 231 |
table.AddRange(0x40, 0x7E, CsiParamState, DispatchAction, GroundState) |
| 232 |
|
| 233 |
table.AddRange(0x20, 0x2F, CsiParamState, CollectAction, CsiIntermediateState) |
| 234 |
|
| 235 |
|
| 236 |
table.AddRange(0x00, 0x17, CsiIntermediateState, ExecuteAction, CsiIntermediateState) |
| 237 |
table.AddOne(0x19, CsiIntermediateState, ExecuteAction, CsiIntermediateState) |
| 238 |
table.AddRange(0x1C, 0x1F, CsiIntermediateState, ExecuteAction, CsiIntermediateState) |
| 239 |
table.AddRange(0x20, 0x2F, CsiIntermediateState, CollectAction, CsiIntermediateState) |
| 240 |
table.AddOne(0x7F, CsiIntermediateState, IgnoreAction, CsiIntermediateState) |
| 241 |
|
| 242 |
table.AddRange(0x40, 0x7E, CsiIntermediateState, DispatchAction, GroundState) |
| 243 |
|
| 244 |
table.AddRange(0x30, 0x3F, CsiIntermediateState, IgnoreAction, GroundState) |
| 245 |
|
| 246 |
|
| 247 |
table.AddRange(0x00, 0x17, CsiEntryState, ExecuteAction, CsiEntryState) |
| 248 |
table.AddOne(0x19, CsiEntryState, ExecuteAction, CsiEntryState) |
| 249 |
table.AddRange(0x1C, 0x1F, CsiEntryState, ExecuteAction, CsiEntryState) |
| 250 |
table.AddOne(0x7F, CsiEntryState, IgnoreAction, CsiEntryState) |
| 251 |
|
| 252 |
table.AddRange(0x40, 0x7E, CsiEntryState, DispatchAction, GroundState) |
| 253 |
|
| 254 |
table.AddRange(0x20, 0x2F, CsiEntryState, CollectAction, CsiIntermediateState) |
| 255 |
|
| 256 |
table.AddRange(0x30, 0x3B, CsiEntryState, ParamAction, CsiParamState) |
| 257 |
table.AddRange(0x3C, 0x3F, CsiEntryState, PrefixAction, CsiParamState) |
| 258 |
|
| 259 |
|
| 260 |
table.AddRange(0x00, 0x06, OscStringState, IgnoreAction, OscStringState) |
| 261 |
table.AddRange(0x08, 0x17, OscStringState, IgnoreAction, OscStringState) |
| 262 |
table.AddOne(0x19, OscStringState, IgnoreAction, OscStringState) |
| 263 |
table.AddRange(0x1C, 0x1F, OscStringState, IgnoreAction, OscStringState) |
| 264 |
table.AddRange(0x20, 0xFF, OscStringState, PutAction, OscStringState) |
| 265 |
|
| 266 |
|
| 267 |
table.AddOne(0x1B, OscStringState, DispatchAction, EscapeState) |
| 268 |
table.AddOne(0x07, OscStringState, DispatchAction, GroundState) |
| 269 |
table.AddOne(0x9C, OscStringState, DispatchAction, GroundState) |
| 270 |
table.AddMany([]byte{0x18, 0x1A}, OscStringState, IgnoreAction, GroundState) |
| 271 |
|
| 272 |
return table |
| 273 |
} |