Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 1 | package datok |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 5 | "compress/gzip" |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 6 | "io" |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 7 | "log" |
| 8 | "os" |
| 9 | ) |
| 10 | |
| 11 | const ( |
| 12 | MAMAGIC = "MATOK" |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 13 | EOT = 4 |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | type MatrixTokenizer struct { |
| 17 | sigma map[rune]int |
| 18 | sigmaASCII [256]int |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 19 | array []uint32 |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 20 | stateCount int |
| 21 | |
| 22 | // Special symbols in sigma |
| 23 | epsilon int |
| 24 | unknown int |
| 25 | identity int |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | // ToMatrix turns the intermediate tokenizer into a |
| 29 | // matrix representation. |
| 30 | func (auto *Automaton) ToMatrix() *MatrixTokenizer { |
| 31 | |
| 32 | mat := &MatrixTokenizer{ |
Akron | 28031b7 | 2021-10-02 13:07:25 +0200 | [diff] [blame] | 33 | sigma: make(map[rune]int), |
| 34 | unknown: auto.unknown, |
| 35 | identity: auto.identity, |
| 36 | epsilon: auto.epsilon, |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 37 | stateCount: auto.stateCount, |
| 38 | } |
| 39 | |
Akron | 00cecd1 | 2021-12-05 13:14:03 +0100 | [diff] [blame] | 40 | max := 0 |
| 41 | |
| 42 | // Init with identity |
| 43 | if mat.identity != -1 { |
| 44 | for i := 0; i < 256; i++ { |
| 45 | mat.sigmaASCII[i] = mat.identity |
| 46 | } |
| 47 | max = mat.identity |
Akron | 4880fb6 | 2021-12-05 12:03:05 +0100 | [diff] [blame] | 48 | } |
| 49 | |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 50 | for num, sym := range auto.sigmaRev { |
| 51 | if int(sym) < 256 { |
| 52 | mat.sigmaASCII[int(sym)] = num |
| 53 | } |
| 54 | mat.sigma[sym] = num |
| 55 | if num > auto.sigmaCount { |
| 56 | panic("sigmaCount is smaller") |
| 57 | } |
Akron | 8e80393 | 2023-04-18 10:19:19 +0200 | [diff] [blame] | 58 | |
| 59 | // Find max |
| 60 | // see https://dev.to/jobinrjohnson/branchless-programming-does-it-really-matter-20j4 |
| 61 | max -= ((max - num) & ((max - num) >> 31)) |
| 62 | // if num > max { |
| 63 | // max = num |
| 64 | // } |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 65 | } |
Akron | 28031b7 | 2021-10-02 13:07:25 +0200 | [diff] [blame] | 66 | // Add final entry to the list (maybe not necessary actually) |
| 67 | |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 68 | remember := make([]bool, auto.stateCount+2) |
| 69 | |
Akron | 28031b7 | 2021-10-02 13:07:25 +0200 | [diff] [blame] | 70 | // lower sigmaCount, as no final value exists |
| 71 | mat.array = make([]uint32, (auto.stateCount+1)*(max+1)) |
| 72 | |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 73 | // Store all transitions in matrix |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 74 | var toMatrix func([]uint32, int) |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 75 | |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 76 | toMatrix = func(matrix []uint32, start int) { |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 77 | if start > auto.stateCount { |
| 78 | panic("stateCount is smaller") |
| 79 | } |
| 80 | if remember[start] { |
| 81 | return |
| 82 | } |
| 83 | remember[start] = true |
| 84 | for alpha, t := range auto.transitions[start] { |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 85 | matrix[(alpha-1)*auto.stateCount+start] = uint32(t.end) |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 86 | |
| 87 | // Mark nontoken transitions |
| 88 | if t.nontoken { |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 89 | matrix[(alpha-1)*auto.stateCount+start] |= FIRSTBIT |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | toMatrix(matrix, t.end) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | toMatrix(mat.array, 1) |
| 97 | |
| 98 | return mat |
| 99 | } |
| 100 | |
Akron | 941f215 | 2021-09-26 15:14:25 +0200 | [diff] [blame] | 101 | // Type of tokenizer |
| 102 | func (MatrixTokenizer) Type() string { |
| 103 | return MAMAGIC |
| 104 | } |
| 105 | |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 106 | // Save stores the matrix data in a file |
| 107 | func (mat *MatrixTokenizer) Save(file string) (n int64, err error) { |
| 108 | f, err := os.Create(file) |
| 109 | if err != nil { |
| 110 | log.Println(err) |
| 111 | return 0, err |
| 112 | } |
| 113 | defer f.Close() |
| 114 | gz := gzip.NewWriter(f) |
| 115 | defer gz.Close() |
| 116 | n, err = mat.WriteTo(gz) |
| 117 | if err != nil { |
| 118 | log.Println(err) |
| 119 | return n, err |
| 120 | } |
| 121 | gz.Flush() |
| 122 | return n, nil |
| 123 | } |
| 124 | |
| 125 | // WriteTo stores the matrix data in an io.Writer. |
| 126 | func (mat *MatrixTokenizer) WriteTo(w io.Writer) (n int64, err error) { |
| 127 | |
| 128 | wb := bufio.NewWriter(w) |
| 129 | defer wb.Flush() |
| 130 | |
| 131 | // Store magical header |
| 132 | all, err := wb.Write([]byte(MAMAGIC)) |
| 133 | if err != nil { |
| 134 | log.Println(err) |
| 135 | return int64(all), err |
| 136 | } |
| 137 | |
| 138 | // Get sigma as a list |
Akron | 28031b7 | 2021-10-02 13:07:25 +0200 | [diff] [blame] | 139 | // In datok it's 16 - 4*4 |
| 140 | sigmalist := make([]rune, len(mat.sigma)+16) |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 141 | max := 0 |
| 142 | for sym, num := range mat.sigma { |
| 143 | sigmalist[num] = sym |
Akron | 8e80393 | 2023-04-18 10:19:19 +0200 | [diff] [blame] | 144 | |
| 145 | // Find max |
| 146 | // see https://dev.to/jobinrjohnson/branchless-programming-does-it-really-matter-20j4 |
| 147 | max -= ((max - num) & ((max - num) >> 31)) |
| 148 | // if num > max { |
| 149 | // max = num |
| 150 | // } |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Akron | 28031b7 | 2021-10-02 13:07:25 +0200 | [diff] [blame] | 153 | // Add final entry to the list (maybe not necessary actually) |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 154 | sigmalist = sigmalist[:max+1] |
| 155 | |
Akron | 28031b7 | 2021-10-02 13:07:25 +0200 | [diff] [blame] | 156 | buf := make([]byte, 0, 14) |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 157 | bo.PutUint16(buf[0:2], VERSION) |
| 158 | bo.PutUint16(buf[2:4], uint16(mat.epsilon)) |
| 159 | bo.PutUint16(buf[4:6], uint16(mat.unknown)) |
| 160 | bo.PutUint16(buf[6:8], uint16(mat.identity)) |
Akron | 28031b7 | 2021-10-02 13:07:25 +0200 | [diff] [blame] | 161 | bo.PutUint32(buf[8:12], uint32(mat.stateCount)) |
| 162 | bo.PutUint16(buf[12:14], uint16(len(sigmalist))) |
| 163 | more, err := wb.Write(buf[0:14]) |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 164 | if err != nil { |
| 165 | log.Println(err) |
| 166 | return int64(all), err |
| 167 | } |
| 168 | |
| 169 | all += more |
| 170 | |
| 171 | // Write sigma |
| 172 | for _, sym := range sigmalist { |
| 173 | |
| 174 | more, err = wb.WriteRune(sym) |
| 175 | if err != nil { |
| 176 | log.Println(err) |
| 177 | return int64(all), err |
| 178 | } |
| 179 | all += more |
| 180 | } |
| 181 | |
| 182 | if err != nil { |
| 183 | log.Println(err) |
| 184 | return int64(all), err |
| 185 | } |
| 186 | |
| 187 | // Test marker - could be checksum |
| 188 | more, err = wb.Write([]byte("M")) |
| 189 | if err != nil { |
| 190 | log.Println(err) |
| 191 | return int64(all), err |
| 192 | } |
| 193 | all += more |
| 194 | |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 195 | for _, x := range mat.array { |
| 196 | bo.PutUint32(buf[0:4], uint32(x)) |
| 197 | more, err = wb.Write(buf[0:4]) |
| 198 | if err != nil { |
| 199 | log.Println(err) |
| 200 | return int64(all), err |
| 201 | } |
| 202 | all += more |
| 203 | if more != 4 { |
| 204 | log.Println("Can not write base uint32") |
| 205 | return int64(all), err |
| 206 | } |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | return int64(all), err |
| 210 | } |
| 211 | |
Akron | b84d469 | 2024-08-20 14:33:00 +0200 | [diff] [blame^] | 212 | // LoadMatrixFile reads a matrix represented tokenizer |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 213 | // from a file. |
| 214 | func LoadMatrixFile(file string) *MatrixTokenizer { |
| 215 | f, err := os.Open(file) |
| 216 | if err != nil { |
| 217 | log.Println(err) |
| 218 | return nil |
| 219 | } |
| 220 | defer f.Close() |
| 221 | |
| 222 | gz, err := gzip.NewReader(f) |
| 223 | if err != nil { |
| 224 | log.Println(err) |
| 225 | return nil |
| 226 | } |
| 227 | defer gz.Close() |
| 228 | |
| 229 | // Todo: Read the whole file! |
| 230 | return ParseMatrix(gz) |
| 231 | } |
| 232 | |
Akron | b84d469 | 2024-08-20 14:33:00 +0200 | [diff] [blame^] | 233 | // ParseMatrix reads a matrix represented tokenizer |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 234 | // from an io.Reader |
| 235 | func ParseMatrix(ior io.Reader) *MatrixTokenizer { |
| 236 | |
| 237 | // Initialize tokenizer with default values |
| 238 | mat := &MatrixTokenizer{ |
Akron | 28031b7 | 2021-10-02 13:07:25 +0200 | [diff] [blame] | 239 | sigma: make(map[rune]int), |
| 240 | epsilon: 0, |
| 241 | unknown: 0, |
| 242 | identity: 0, |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 243 | stateCount: 0, |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | r := bufio.NewReader(ior) |
| 247 | |
| 248 | buf := make([]byte, 1024) |
| 249 | buf = buf[0:len(MAMAGIC)] |
| 250 | |
| 251 | _, err := r.Read(buf) |
| 252 | |
| 253 | if err != nil { |
| 254 | log.Println(err) |
| 255 | return nil |
| 256 | } |
| 257 | |
| 258 | if string(MAMAGIC) != string(buf) { |
| 259 | log.Println("Not a matok file") |
| 260 | return nil |
| 261 | } |
| 262 | |
Akron | 28031b7 | 2021-10-02 13:07:25 +0200 | [diff] [blame] | 263 | more, err := io.ReadFull(r, buf[0:14]) |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 264 | if err != nil { |
| 265 | log.Println(err) |
| 266 | return nil |
| 267 | } |
| 268 | |
Akron | 28031b7 | 2021-10-02 13:07:25 +0200 | [diff] [blame] | 269 | if more != 14 { |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 270 | log.Println("Read bytes do not fit") |
| 271 | return nil |
| 272 | } |
| 273 | |
| 274 | version := bo.Uint16(buf[0:2]) |
| 275 | |
| 276 | if version != VERSION { |
| 277 | log.Println("Version not compatible") |
| 278 | return nil |
| 279 | } |
| 280 | |
| 281 | mat.epsilon = int(bo.Uint16(buf[2:4])) |
| 282 | mat.unknown = int(bo.Uint16(buf[4:6])) |
| 283 | mat.identity = int(bo.Uint16(buf[6:8])) |
Akron | 28031b7 | 2021-10-02 13:07:25 +0200 | [diff] [blame] | 284 | mat.stateCount = int(bo.Uint32(buf[8:12])) |
| 285 | sigmaCount := int(bo.Uint16(buf[12:14])) |
| 286 | arraySize := (mat.stateCount + 1) * sigmaCount |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 287 | |
Akron | 00cecd1 | 2021-12-05 13:14:03 +0100 | [diff] [blame] | 288 | // Init with identity |
| 289 | if mat.identity != -1 { |
| 290 | for i := 0; i < 256; i++ { |
| 291 | mat.sigmaASCII[i] = mat.identity |
| 292 | } |
| 293 | } |
| 294 | |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 295 | for x := 0; x < sigmaCount; x++ { |
| 296 | sym, _, err := r.ReadRune() |
| 297 | if err == nil && sym != 0 { |
| 298 | if int(sym) < 256 { |
| 299 | mat.sigmaASCII[int(sym)] = x |
| 300 | } |
| 301 | mat.sigma[sym] = x |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | _, err = io.ReadFull(r, buf[0:1]) |
| 306 | |
| 307 | if err != nil { |
| 308 | log.Print(err) |
| 309 | return nil |
| 310 | } |
| 311 | |
| 312 | if string("M") != string(buf[0:1]) { |
| 313 | log.Println("Not a matok file") |
| 314 | return nil |
| 315 | } |
| 316 | |
| 317 | // Read based on length |
| 318 | mat.array = make([]uint32, arraySize) |
| 319 | |
| 320 | dataArray, err := io.ReadAll(r) |
| 321 | |
| 322 | if err == io.EOF { |
| 323 | log.Println(err) |
| 324 | return nil |
| 325 | } |
| 326 | |
| 327 | if len(dataArray) < arraySize*4 { |
Akron | 28031b7 | 2021-10-02 13:07:25 +0200 | [diff] [blame] | 328 | log.Println("Not enough bytes read", len(dataArray), arraySize*4) |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 329 | return nil |
| 330 | } |
| 331 | |
| 332 | for x := 0; x < arraySize; x++ { |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 333 | mat.array[x] = bo.Uint32(dataArray[x*4 : (x*4)+4]) |
| 334 | } |
| 335 | |
| 336 | return mat |
| 337 | } |
| 338 | |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 339 | // Transduce input to ouutput |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 340 | func (mat *MatrixTokenizer) Transduce(r io.Reader, w io.Writer) bool { |
Akron | 96fdc9b | 2021-10-27 21:11:17 +0200 | [diff] [blame] | 341 | return mat.TransduceTokenWriter(r, NewTokenWriter(w, SIMPLE)) |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 342 | } |
| 343 | |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 344 | // TransduceTokenWriter transduces an input string against |
| 345 | // the matrix FSA. The rules are always greedy. If the |
| 346 | // automaton fails, it takes the last possible token ending |
| 347 | // branch. |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 348 | func (mat *MatrixTokenizer) TransduceTokenWriter(r io.Reader, w *TokenWriter) bool { |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 349 | var a int |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 350 | var t0 uint32 |
| 351 | t := uint32(1) // Initial state |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 352 | var ok, rewindBuffer bool |
| 353 | |
| 354 | // Remember the last position of a possible tokenend, |
| 355 | // in case the automaton fails. |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 356 | epsilonState := uint32(0) |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 357 | epsilonOffset := 0 |
| 358 | |
Akron | 5c82a92 | 2021-09-24 19:11:29 +0200 | [diff] [blame] | 359 | // Remember if the last transition was epsilon |
| 360 | sentenceEnd := false |
| 361 | |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 362 | // Remember if a text end was already set |
| 363 | textEnd := false |
| 364 | |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 365 | buffer := make([]rune, 1024) |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 366 | bufft := 0 // Buffer token offset |
| 367 | buffc := 0 // Buffer current symbol |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 368 | buffi := 0 // Buffer length |
| 369 | |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 370 | // The buffer is organized as follows: |
| 371 | // [ t[....c..]..i] |
| 372 | |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 373 | reader := bufio.NewReader(r) |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 374 | defer w.Flush() |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 375 | |
| 376 | var char rune |
| 377 | |
| 378 | var err error |
| 379 | eof := false |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 380 | eot := false |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 381 | newchar := true |
| 382 | |
| 383 | PARSECHARM: |
| 384 | for { |
| 385 | |
| 386 | if newchar { |
| 387 | // Get from reader if buffer is empty |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 388 | if buffc >= buffi { |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 389 | if eof { |
| 390 | break |
| 391 | } |
| 392 | char, _, err = reader.ReadRune() |
| 393 | |
| 394 | // No more runes to read |
| 395 | if err != nil { |
Akron | 274600e | 2021-11-03 20:09:06 +0100 | [diff] [blame] | 396 | if err == io.EOF { |
| 397 | eof = true |
| 398 | break |
| 399 | } |
| 400 | |
| 401 | log.Fatalln(err) |
| 402 | os.Exit(1) |
| 403 | return false |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 404 | } |
Akron | 274600e | 2021-11-03 20:09:06 +0100 | [diff] [blame] | 405 | |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 406 | buffer[buffi] = char |
| 407 | buffi++ |
| 408 | } |
| 409 | |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 410 | char = buffer[buffc] |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 411 | |
| 412 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 413 | log.Println("Current char", string(char), int(char), showBufferNew(buffer, bufft, buffc, buffi)) |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 414 | } |
| 415 | |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 416 | eot = false |
| 417 | |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 418 | // TODO: |
| 419 | // Better not repeatedly check for a! |
| 420 | // Possibly keep a buffer with a. |
| 421 | if int(char) < 256 { |
Akron | 8e80393 | 2023-04-18 10:19:19 +0200 | [diff] [blame] | 422 | eot = int(char) == EOT |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 423 | |
| 424 | // mat.SigmaASCII[] is initialized with mat.identity |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 425 | a = mat.sigmaASCII[int(char)] |
| 426 | } else { |
| 427 | a, ok = mat.sigma[char] |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 428 | |
Akron | 4880fb6 | 2021-12-05 12:03:05 +0100 | [diff] [blame] | 429 | // Use identity symbol if character is not in sigma |
| 430 | if !ok && mat.identity != -1 { |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 431 | |
| 432 | // TODO: Maybe use unknown? |
Akron | 4880fb6 | 2021-12-05 12:03:05 +0100 | [diff] [blame] | 433 | a = mat.identity |
| 434 | } |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | t0 = t |
| 438 | |
| 439 | // Check for epsilon transitions and remember |
| 440 | |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 441 | // TODO: Can t0 be negative here? |
| 442 | if mat.array[(mat.epsilon-1)*mat.stateCount+int(t0)] != 0 { |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 443 | // Remember state for backtracking to last tokenend state |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 444 | |
| 445 | // Maybe not necessary - and should be simpler! |
| 446 | // Just Remove |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 447 | // t0 &= ^FIRSTBIT |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 448 | epsilonState = t0 |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 449 | epsilonOffset = buffc |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 450 | |
| 451 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 452 | log.Println("epsilonOffset is set to", buffc) |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 453 | } |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 457 | // can happen when no identity is defined. |
| 458 | // This shouldn't be tested in every loop |
| 459 | if a == 0 { |
| 460 | t = 0 |
| 461 | } else { |
| 462 | // Checks a transition based on t0, a and buffo |
| 463 | t = mat.array[(int(a)-1)*mat.stateCount+int(t0)] |
| 464 | } |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 465 | |
| 466 | if DEBUG { |
| 467 | // Char is only relevant if set |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 468 | log.Println("Check", t0, "-", a, "(", string(char), ")", "->", t) |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 469 | } |
| 470 | |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 471 | // Check if the transition is invalid according to the matrix |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 472 | if t == 0 { |
| 473 | |
| 474 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 475 | log.Println("Match is not fine!") |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | if !ok && a == mat.identity { |
| 479 | |
| 480 | // Try again with unknown symbol, in case identity failed |
| 481 | // Char is only relevant when set |
| 482 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 483 | log.Println("UNKNOWN symbol", string(char), "->", mat.unknown) |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 484 | } |
| 485 | a = mat.unknown |
| 486 | |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 487 | } else if a != mat.epsilon && epsilonState != 0 { |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 488 | |
| 489 | // Try again with epsilon symbol, in case everything else failed |
| 490 | t0 = epsilonState |
| 491 | epsilonState = 0 // reset |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 492 | buffc = epsilonOffset |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 493 | a = mat.epsilon |
| 494 | |
| 495 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 496 | log.Println("Get from epsilon stack and set buffo!", showBufferNew(buffer, bufft, buffc, buffi)) |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | } else { |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 500 | |
| 501 | if DEBUG { |
| 502 | log.Println("Fail!") |
| 503 | } |
| 504 | |
| 505 | // w.Fail(bufft) |
| 506 | |
| 507 | // The following procedure means the automaton fails to consume a certain character. |
| 508 | // In the tokenization scenario, this means, the tokenizer will drop the old or current data as a |
| 509 | // token and start blank at the root node of the automaton for the remaining data. |
| 510 | // It may be beneficial to have something like a "drop()" event to capture these cases, |
| 511 | // as they are likely the result of a bad automaton design. |
Akron | cae3911 | 2023-04-26 19:43:16 +0200 | [diff] [blame] | 512 | |
| 513 | // fmt.Println("Problem", len(buffer), buffc, bufft) |
| 514 | |
| 515 | if buffc-bufft <= 0 { |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 516 | buffc++ |
Akron | cae3911 | 2023-04-26 19:43:16 +0200 | [diff] [blame] | 517 | if buffc == 0 { |
| 518 | eof = true |
| 519 | break |
| 520 | } |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 521 | } |
Akron | 8e80393 | 2023-04-18 10:19:19 +0200 | [diff] [blame] | 522 | // This will hopefully be branchless by the compiler |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 523 | |
| 524 | if DEBUG { |
| 525 | log.Println("-> Flush buffer: [", string(buffer[bufft:buffc]), "]", showBufferNew(buffer, bufft, buffc, buffi)) |
| 526 | } |
Akron | cae3911 | 2023-04-26 19:43:16 +0200 | [diff] [blame] | 527 | |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 528 | w.Token(bufft, buffer[:buffc]) |
| 529 | |
| 530 | sentenceEnd = false |
| 531 | textEnd = false |
| 532 | |
| 533 | if DEBUG { |
| 534 | log.Println("-> Rewind buffer", bufft, buffc, buffi, epsilonOffset) |
| 535 | } |
| 536 | |
Akron | 8e80393 | 2023-04-18 10:19:19 +0200 | [diff] [blame] | 537 | copy(buffer[0:], buffer[buffc:buffi]) |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 538 | |
| 539 | buffi -= buffc |
| 540 | epsilonState = 0 |
| 541 | |
| 542 | buffc = 0 |
| 543 | bufft = 0 |
| 544 | |
| 545 | a = mat.epsilon |
| 546 | |
| 547 | // Restart from root state |
| 548 | t = uint32(1) |
| 549 | newchar = true |
| 550 | // goto PARSECHARM |
| 551 | continue |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | newchar = false |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 555 | eot = false |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 556 | continue |
| 557 | } |
| 558 | |
| 559 | // Transition was successful |
| 560 | rewindBuffer = false |
| 561 | |
Akron | 90aa45b | 2021-11-16 23:28:17 +0100 | [diff] [blame] | 562 | // Transition consumes no character |
| 563 | if a == mat.epsilon { |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 564 | // Transition marks the end of a token - so flush the buffer |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 565 | if buffc-bufft > 0 { |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 566 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 567 | log.Println("-> Flush buffer: [", string(buffer[bufft:buffc]), "]", showBufferNew(buffer, bufft, buffc, buffi)) |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 568 | } |
Akron | 32416ce | 2021-10-23 17:09:41 +0200 | [diff] [blame] | 569 | w.Token(bufft, buffer[:buffc]) |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 570 | rewindBuffer = true |
Akron | 5c82a92 | 2021-09-24 19:11:29 +0200 | [diff] [blame] | 571 | sentenceEnd = false |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 572 | textEnd = false |
Akron | 5c82a92 | 2021-09-24 19:11:29 +0200 | [diff] [blame] | 573 | } else { |
| 574 | sentenceEnd = true |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 575 | w.SentenceEnd(buffc) |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 576 | } |
Akron | 90aa45b | 2021-11-16 23:28:17 +0100 | [diff] [blame] | 577 | |
| 578 | // Transition consumes a character |
| 579 | } else { |
| 580 | buffc++ |
| 581 | |
| 582 | // Transition does not produce a character |
Akron | 8e80393 | 2023-04-18 10:19:19 +0200 | [diff] [blame] | 583 | // Hopefully generated branchless code |
Akron | 90aa45b | 2021-11-16 23:28:17 +0100 | [diff] [blame] | 584 | if buffc-bufft == 1 && (t&FIRSTBIT) != 0 { |
| 585 | if DEBUG { |
| 586 | log.Println("Nontoken forward", showBufferNew(buffer, bufft, buffc, buffi)) |
| 587 | } |
| 588 | bufft++ |
| 589 | // rewindBuffer = true |
| 590 | } |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 591 | } |
| 592 | |
Akron | 8cc2dd9 | 2021-10-25 19:49:41 +0200 | [diff] [blame] | 593 | if eot { |
| 594 | eot = false |
Akron | f66dc14 | 2023-09-06 20:00:47 +0200 | [diff] [blame] | 595 | if !sentenceEnd { |
| 596 | sentenceEnd = true |
| 597 | w.SentenceEnd(buffc) |
| 598 | } |
Akron | 8cc2dd9 | 2021-10-25 19:49:41 +0200 | [diff] [blame] | 599 | textEnd = true |
| 600 | w.TextEnd(buffc) |
| 601 | rewindBuffer = true |
| 602 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 603 | log.Println("END OF TEXT") |
Akron | 8cc2dd9 | 2021-10-25 19:49:41 +0200 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 607 | // Rewind the buffer if necessary |
| 608 | if rewindBuffer { |
| 609 | |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 610 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 611 | log.Println("-> Rewind buffer", bufft, buffc, buffi, epsilonOffset) |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 612 | } |
| 613 | |
Akron | 8e80393 | 2023-04-18 10:19:19 +0200 | [diff] [blame] | 614 | copy(buffer[0:], buffer[buffc:buffi]) |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 615 | |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 616 | buffi -= buffc |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 617 | // epsilonOffset -= buffo |
| 618 | epsilonOffset = 0 |
| 619 | epsilonState = 0 |
| 620 | |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 621 | buffc = 0 |
| 622 | bufft = 0 |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 623 | |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 624 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 625 | log.Println("Remaining:", showBufferNew(buffer, bufft, buffc, buffi)) |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 626 | } |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 627 | } |
| 628 | |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 629 | t &= ^FIRSTBIT |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 630 | |
| 631 | newchar = true |
| 632 | |
| 633 | // TODO: |
| 634 | // Prevent endless epsilon loops! |
| 635 | } |
| 636 | |
| 637 | // Input reader is not yet finished |
| 638 | if !eof { |
| 639 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 640 | log.Println("Not at the end") |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 641 | } |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 642 | // This should never happen |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 643 | return false |
| 644 | } |
| 645 | |
| 646 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 647 | log.Println("Entering final check") |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 648 | } |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 649 | |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 650 | // Check epsilon transitions as long as possible |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 651 | t0 = t |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 652 | t = mat.array[(int(mat.epsilon)-1)*mat.stateCount+int(t0)] |
| 653 | a = mat.epsilon |
| 654 | newchar = false |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 655 | // t can't be < 0 |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 656 | if t != 0 { |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 657 | // Remember state for backtracking to last tokenend state |
| 658 | goto PARSECHARM |
| 659 | |
| 660 | } else if epsilonState != 0 { |
| 661 | t0 = epsilonState |
| 662 | epsilonState = 0 // reset |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 663 | buffc = epsilonOffset |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 664 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 665 | log.Println("Get from epsilon stack and set buffo!", showBufferNew(buffer, bufft, buffc, buffi)) |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 666 | } |
| 667 | goto PARSECHARM |
| 668 | } |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 669 | |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 670 | // something left in buffer |
| 671 | if buffc-bufft > 0 { |
| 672 | if DEBUG { |
| 673 | log.Println("-> Flush buffer: [", string(buffer[bufft:buffc]), "]", showBufferNew(buffer, bufft, buffc, buffi)) |
| 674 | } |
| 675 | w.Token(bufft, buffer[:buffc]) |
| 676 | sentenceEnd = false |
| 677 | textEnd = false |
| 678 | } |
| 679 | |
Akron | 5c82a92 | 2021-09-24 19:11:29 +0200 | [diff] [blame] | 680 | // Add an additional sentence ending, if the file is over but no explicit |
| 681 | // sentence split was reached. This may be controversial and therefore |
| 682 | // optional via parameter. |
| 683 | if !sentenceEnd { |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 684 | w.SentenceEnd(buffc) |
Akron | 5c82a92 | 2021-09-24 19:11:29 +0200 | [diff] [blame] | 685 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 686 | log.Println("Sentence end") |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | |
| 690 | if !textEnd { |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 691 | w.TextEnd(buffc) |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 692 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 693 | log.Println("Text end") |
Akron | 5c82a92 | 2021-09-24 19:11:29 +0200 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
| 697 | return true |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 698 | } |