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