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