Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 1 | package datokenizer |
| 2 | |
| 3 | /** |
| 4 | * The file reader is basically a port of foma2js, |
| 5 | * licensed under the Apache License, version 2, |
| 6 | * and written by Mans Hulden. |
| 7 | */ |
| 8 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 9 | // TODO: |
| 10 | // - replace maxSize with the check value |
| 11 | // - Strip first state and make everything start with 0! |
| 12 | // - Serialize! |
| 13 | // - Split Tokenizer and DATokenizer |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame^] | 14 | // - Make epsilon etc. properties |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 15 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 16 | import ( |
| 17 | "bufio" |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame^] | 18 | "bytes" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 19 | "compress/gzip" |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame^] | 20 | "encoding/binary" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 21 | "fmt" |
| 22 | "io" |
| 23 | "os" |
Akron | c9d84a6 | 2021-08-03 15:56:03 +0200 | [diff] [blame] | 24 | "sort" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 25 | "strconv" |
| 26 | "strings" |
| 27 | "unicode/utf8" |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 28 | |
| 29 | "github.com/rs/zerolog/log" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | const ( |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 33 | PROPS = 1 |
| 34 | SIGMA = 2 |
| 35 | STATES = 3 |
| 36 | NONE = 4 |
| 37 | NEWLINE = '\u000a' |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 38 | DEBUG = false |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame^] | 39 | MAGIC = "DATOK" |
| 40 | VERSION = uint16(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 41 | ) |
| 42 | |
| 43 | // Special symbols in sigma |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 44 | var EPSILON = -1 |
| 45 | var UNKNOWN = -1 |
| 46 | var IDENTITY = -1 |
| 47 | var FINAL = -1 |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 48 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame^] | 49 | var bo binary.ByteOrder = binary.LittleEndian |
| 50 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 51 | type mapping struct { |
| 52 | source int |
| 53 | target int |
| 54 | } |
| 55 | |
| 56 | type edge struct { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 57 | inSym int |
| 58 | outSym int |
| 59 | end int |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | type Tokenizer struct { |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 63 | // sigma map[rune]int |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 64 | sigmaRev map[int]rune |
| 65 | arcCount int |
| 66 | stateCount int |
| 67 | sigmaCount int |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 68 | transitions []map[int]*edge |
| 69 | } |
| 70 | |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 71 | type DaTokenizer struct { |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 72 | // sigmaRev map[int]rune |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 73 | sigma map[rune]int |
| 74 | maxSize int |
| 75 | loadLevel float64 |
| 76 | array []int |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 77 | } |
| 78 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 79 | func ParseFile(file string) *Tokenizer { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 80 | f, err := os.Open(file) |
| 81 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 82 | log.Error().Err(err) |
| 83 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 84 | } |
| 85 | defer f.Close() |
| 86 | |
| 87 | gz, err := gzip.NewReader(f) |
| 88 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 89 | log.Error().Err(err) |
| 90 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 91 | } |
| 92 | defer gz.Close() |
| 93 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 94 | return Parse(gz) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 97 | func Parse(ior io.Reader) *Tokenizer { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 98 | r := bufio.NewReader(ior) |
| 99 | |
| 100 | tok := &Tokenizer{ |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 101 | // sigma: make(map[rune]int), |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 102 | sigmaRev: make(map[int]rune), |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 105 | var state, inSym, outSym, end, final int |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 106 | |
| 107 | mode := 0 |
| 108 | var elem []string |
| 109 | var elemint [5]int |
| 110 | |
| 111 | for { |
| 112 | line, err := r.ReadString('\n') |
| 113 | if err != nil { |
| 114 | if err == io.EOF { |
| 115 | break |
| 116 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 117 | log.Error().Err(err) |
| 118 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 119 | } |
| 120 | if strings.HasPrefix(line, "##foma-net") { |
| 121 | continue |
| 122 | } |
| 123 | if strings.HasPrefix(line, "##props##") { |
| 124 | mode = PROPS |
| 125 | continue |
| 126 | } |
| 127 | if strings.HasPrefix(line, "##states##") { |
| 128 | mode = STATES |
| 129 | |
| 130 | // Adds a final transition symbol to sigma |
| 131 | // written as '#' in Mizobuchi et al (2000) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 132 | tok.sigmaCount++ |
| 133 | FINAL = tok.sigmaCount |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 134 | continue |
| 135 | } |
| 136 | if strings.HasPrefix(line, "##sigma##") { |
| 137 | mode = SIGMA |
| 138 | continue |
| 139 | } |
| 140 | if strings.HasPrefix(line, "##end##") { |
| 141 | mode = NONE |
| 142 | continue |
| 143 | } |
| 144 | |
| 145 | switch mode { |
| 146 | case PROPS: |
| 147 | { |
| 148 | elem = strings.Split(line, " ") |
| 149 | /* |
| 150 | fmt.Println("arity: " + elem[0]) |
| 151 | fmt.Println("arccount: " + elem[1]) |
| 152 | fmt.Println("statecount: " + elem[2]) |
| 153 | fmt.Println("linecount: " + elem[3]) |
| 154 | fmt.Println("finalcount: " + elem[4]) |
| 155 | fmt.Println("pathcount: " + elem[5]) |
| 156 | fmt.Println("is_deterministic: " + elem[6]) |
| 157 | fmt.Println("is_pruned: " + elem[7]) |
| 158 | fmt.Println("is_minimized: " + elem[8]) |
| 159 | fmt.Println("is_epsilon_free: " + elem[9]) |
| 160 | fmt.Println("is_loop_free: " + elem[10]) |
| 161 | fmt.Println("extras: " + elem[11]) |
| 162 | fmt.Println("name: " + elem[12]) |
| 163 | */ |
| 164 | if elem[6] != "1" { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 165 | log.Error().Msg("The FST needs to be deterministic") |
| 166 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 167 | } |
| 168 | if elem[9] != "1" { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 169 | log.Error().Msg("The FST needs to be epsilon free") |
| 170 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | elemint[0], err = strconv.Atoi(elem[1]) |
| 174 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 175 | log.Error().Msg("Can't read arccount") |
| 176 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 177 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 178 | tok.arcCount = elemint[0] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 179 | |
| 180 | // States start at 1 in Mizobuchi et al (2000), |
| 181 | // as the state 0 is associated with a fail. |
| 182 | // Initialize states and transitions |
| 183 | elemint[0], err = strconv.Atoi(elem[2]) |
| 184 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 185 | log.Error().Msg("Can't read statecount") |
| 186 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 187 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 188 | tok.stateCount = elemint[0] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 189 | tok.transitions = make([]map[int]*edge, elemint[0]+1) |
| 190 | continue |
| 191 | } |
| 192 | case STATES: |
| 193 | { |
| 194 | elem = strings.Split(line[0:len(line)-1], " ") |
| 195 | if elem[0] == "-1" { |
| 196 | continue |
| 197 | } |
| 198 | elemint[0], err = strconv.Atoi(elem[0]) |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 199 | if err != nil { |
| 200 | break |
| 201 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 202 | |
| 203 | if len(elem) > 1 { |
| 204 | elemint[1], err = strconv.Atoi(elem[1]) |
| 205 | if err != nil { |
| 206 | break |
| 207 | } |
| 208 | if len(elem) > 2 { |
| 209 | elemint[2], err = strconv.Atoi(elem[2]) |
| 210 | if err != nil { |
| 211 | break |
| 212 | } |
| 213 | if len(elem) > 3 { |
| 214 | elemint[3], err = strconv.Atoi(elem[3]) |
| 215 | if err != nil { |
| 216 | break |
| 217 | } |
| 218 | if len(elem) > 4 { |
| 219 | elemint[4], err = strconv.Atoi(elem[4]) |
| 220 | if err != nil { |
| 221 | break |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | switch len(elem) { |
| 229 | case 5: |
| 230 | { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 231 | state = elemint[0] |
| 232 | inSym = elemint[1] |
| 233 | outSym = elemint[2] |
| 234 | end = elemint[3] |
| 235 | final = elemint[4] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 236 | } |
| 237 | case 4: |
| 238 | { |
| 239 | if elemint[1] == -1 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 240 | state = elemint[0] |
| 241 | final = elemint[3] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 242 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 243 | state = elemint[0] |
| 244 | inSym = elemint[1] |
| 245 | end = elemint[2] |
| 246 | final = elemint[3] |
| 247 | outSym = inSym |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | case 3: |
| 251 | { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 252 | inSym = elemint[0] |
| 253 | outSym = elemint[1] |
| 254 | end = elemint[2] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 255 | } |
| 256 | case 2: |
| 257 | { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 258 | inSym = elemint[0] |
| 259 | end = elemint[1] |
| 260 | outSym = inSym |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 264 | // While the states in foma start with 0, the states in the |
| 265 | // Mizobuchi FSA start with one - so we increase every state by 1. |
| 266 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 267 | if inSym != outSym { |
| 268 | |
| 269 | // Allow any epsilon to become a newline |
| 270 | if !(inSym == EPSILON && tok.sigmaRev[outSym] == NEWLINE) && |
| 271 | |
| 272 | // Allow any whitespace to be ignored |
| 273 | !(inSym != EPSILON && outSym == EPSILON) && |
| 274 | |
| 275 | // Allow any whitespace to become a new line |
| 276 | !(tok.sigmaRev[outSym] == NEWLINE) { |
| 277 | |
| 278 | log.Error().Msg( |
| 279 | "Unsupported transition: " + |
| 280 | strconv.Itoa(state) + |
| 281 | " -> " + strconv.Itoa(end) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 282 | " (" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 283 | strconv.Itoa(inSym) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 284 | ":" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 285 | strconv.Itoa(outSym) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 286 | ") (" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 287 | string(tok.sigmaRev[inSym]) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 288 | ":" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 289 | string(tok.sigmaRev[outSym]) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 290 | ")") |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 291 | os.Exit(1) |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 292 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 293 | } |
| 294 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 295 | // This collects all edges until arrstate changes |
| 296 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 297 | // TODO: |
| 298 | // if arrin == EPSILON && arrout == TOKENEND, mark state as newline |
| 299 | // if the next transition is the same, remove TOKENEND and add SENTENCEEND |
| 300 | // This requires to remove the transition alltogether and marks the state instead. |
| 301 | |
| 302 | // TODO: |
| 303 | // if arrout == EPSILON, mark the transition as NOTOKEN |
| 304 | |
| 305 | targetObj := &edge{ |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 306 | inSym: inSym, |
| 307 | outSym: outSym, |
| 308 | end: end + 1, |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 311 | // Initialize outgoing states |
| 312 | if tok.transitions[state+1] == nil { |
| 313 | tok.transitions[state+1] = make(map[int]*edge) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 314 | } |
| 315 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 316 | // Ignore transitions with invalid symbols |
| 317 | if inSym >= 0 { |
| 318 | tok.transitions[state+1][inSym] = targetObj |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 319 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 320 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 321 | // Add final transition |
| 322 | if final == 1 { |
| 323 | tok.transitions[state+1][FINAL] = &edge{} |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 324 | } |
| 325 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 326 | if DEBUG { |
| 327 | fmt.Println("Add", |
| 328 | state+1, "->", end+1, |
| 329 | "(", |
| 330 | inSym, |
| 331 | ":", |
| 332 | outSym, |
| 333 | ") (", |
| 334 | string(tok.sigmaRev[inSym]), |
| 335 | ":", |
| 336 | string(tok.sigmaRev[outSym]), |
| 337 | ")") |
| 338 | } |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 339 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 340 | continue |
| 341 | } |
| 342 | case SIGMA: |
| 343 | { |
| 344 | elem = strings.SplitN(line[0:len(line)-1], " ", 2) |
| 345 | |
| 346 | // Turn string into sigma id |
| 347 | number, err := strconv.Atoi(elem[0]) |
| 348 | |
| 349 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 350 | log.Error().Err(err) |
| 351 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 354 | tok.sigmaCount = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 355 | |
| 356 | var symbol rune |
| 357 | |
| 358 | // Read rune |
| 359 | if utf8.RuneCountInString(elem[1]) == 1 { |
| 360 | symbol = []rune(elem[1])[0] |
| 361 | |
| 362 | // Probably a MCS |
| 363 | } else if utf8.RuneCountInString(elem[1]) > 1 { |
| 364 | switch elem[1] { |
| 365 | case "@_EPSILON_SYMBOL_@": |
| 366 | { |
| 367 | EPSILON = number |
| 368 | continue |
| 369 | } |
| 370 | case "@_UNKNOWN_SYMBOL_@": |
| 371 | { |
| 372 | UNKNOWN = number |
| 373 | continue |
| 374 | } |
| 375 | |
| 376 | case "@_IDENTITY_SYMBOL_@": |
| 377 | { |
| 378 | IDENTITY = number |
| 379 | continue |
| 380 | } |
| 381 | default: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 382 | { |
| 383 | log.Error().Msg("MCS not supported: " + line) |
| 384 | os.Exit(1) |
| 385 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 386 | } |
| 387 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 388 | } else { // Probably a new line symbol |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 389 | line, err = r.ReadString('\n') |
| 390 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 391 | log.Error().Err(err) |
| 392 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 393 | } |
| 394 | if len(line) != 1 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 395 | log.Error().Msg("MCS not supported:" + line) |
| 396 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 397 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 398 | symbol = rune(NEWLINE) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 399 | } |
| 400 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 401 | tok.sigmaRev[number] = symbol |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | return tok |
| 407 | } |
| 408 | |
| 409 | // Implementation of Mizobuchi et al (2000), p.128 |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 410 | func (tok *Tokenizer) ToDoubleArray() *DaTokenizer { |
| 411 | |
| 412 | dat := &DaTokenizer{ |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame^] | 413 | sigma: make(map[rune]int), |
| 414 | loadLevel: -1, |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | for num, sym := range tok.sigmaRev { |
| 418 | dat.sigma[sym] = num |
| 419 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 420 | |
| 421 | mark := 0 |
| 422 | size := 0 |
| 423 | |
| 424 | // Create a mapping from s to t |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 425 | table := make([]*mapping, tok.arcCount+1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 426 | |
| 427 | table[size] = &mapping{source: 1, target: 1} |
| 428 | size++ |
| 429 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 430 | // Allocate space for the outgoing symbol range |
| 431 | A := make([]int, 0, tok.sigmaCount) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 432 | |
| 433 | for mark < size { |
| 434 | s := table[mark].source // This is a state in Ms |
| 435 | t := table[mark].target // This is a state in Mt |
| 436 | mark++ |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 437 | |
| 438 | // Following the paper, here the state t can be remembered |
| 439 | // in the set of states St |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 440 | A = A[:0] |
| 441 | tok.get_set(s, &A) |
| 442 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 443 | // Set base to the first free slot in the double array |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 444 | dat.setBase(t, dat.xCheck(A)) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 445 | |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 446 | // TODO: |
| 447 | // Sort the outgoing transitions based onm the |
| 448 | // outdegree of .end |
| 449 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 450 | // Iterate over all outgoing symbols |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 451 | for _, a := range A { |
| 452 | |
| 453 | if a != FINAL { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 454 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 455 | // Aka g(s, a) |
| 456 | s1 := tok.transitions[s][a].end |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 457 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 458 | // Store the transition |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 459 | t1 := dat.getBase(t) + a |
| 460 | dat.setCheck(t1, t) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 461 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 462 | // Check for representative states |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 463 | r := in_table(s1, table, size) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 464 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 465 | if r == 0 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 466 | // Remember the mapping |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 467 | table[size] = &mapping{source: s1, target: t1} |
| 468 | size++ |
| 469 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 470 | // Overwrite with the representative state |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 471 | dat.setBase(t1, -1*r) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 472 | } |
| 473 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 474 | // Store a final transition |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 475 | dat.setCheck(dat.getBase(t)+FINAL, t) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | // Following Mizobuchi et al (2000) the size of the |
| 481 | // FSA should be stored in check(1). |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 482 | dat.setCheck(1, dat.maxSize+1) |
| 483 | dat.array = dat.array[:dat.maxSize+1] |
| 484 | return dat |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 485 | } |
| 486 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 487 | // Resize double array when necessary |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 488 | func (tok *DaTokenizer) resize(l int) { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 489 | // TODO: |
| 490 | // This is a bit too aggressive atm and should be calmed down. |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 491 | if len(tok.array) <= l { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 492 | tok.array = append(tok.array, make([]int, l)...) |
| 493 | } |
| 494 | } |
| 495 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 496 | // Set base value in double array |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 497 | func (tok *DaTokenizer) setBase(p int, v int) { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 498 | l := p*2 + 1 |
| 499 | tok.resize(l) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 500 | if tok.maxSize < l { |
| 501 | tok.maxSize = l |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 502 | } |
| 503 | tok.array[p*2] = v |
| 504 | } |
| 505 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 506 | // Get base value in double array |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 507 | func (tok *DaTokenizer) getBase(p int) int { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 508 | if p*2 >= len(tok.array) { |
| 509 | return 0 |
| 510 | } |
| 511 | return tok.array[p*2] |
| 512 | } |
| 513 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 514 | // Set check value in double array |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 515 | func (tok *DaTokenizer) setCheck(p int, v int) { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 516 | l := p*2 + 1 |
| 517 | tok.resize(l) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 518 | if tok.maxSize < l { |
| 519 | tok.maxSize = l |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 520 | } |
| 521 | tok.array[(p*2)+1] = v |
| 522 | } |
| 523 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 524 | // Get check value in double array |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 525 | func (tok *DaTokenizer) getCheck(p int) int { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 526 | if (p*2)+1 >= len(tok.array) { |
| 527 | return 0 |
| 528 | } |
| 529 | return tok.array[(p*2)+1] |
| 530 | } |
| 531 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 532 | // Set size of double array |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 533 | func (tok *DaTokenizer) setSize(p, v int) { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 534 | tok.setCheck(1, v) |
| 535 | } |
| 536 | |
| 537 | // Get size of double array |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 538 | func (tok *DaTokenizer) GetSize(p int) int { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 539 | return tok.getCheck(1) |
| 540 | } |
| 541 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 542 | // Check the table if a mapping of s |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 543 | // exists and return this as a representative. |
| 544 | // Currently iterates through the whole table |
| 545 | // in a bruteforce manner. |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 546 | func in_table(s int, table []*mapping, size int) int { |
| 547 | for x := 0; x < size; x++ { |
| 548 | if table[x].source == s { |
| 549 | return table[x].target |
| 550 | } |
| 551 | } |
| 552 | return 0 |
| 553 | } |
| 554 | |
| 555 | // Set alphabet A to the list of all symbols |
| 556 | // outgoing from s |
| 557 | func (tok *Tokenizer) get_set(s int, A *[]int) { |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 558 | for a := range tok.transitions[s] { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 559 | *A = append(*A, a) |
| 560 | } |
Akron | c9d84a6 | 2021-08-03 15:56:03 +0200 | [diff] [blame] | 561 | |
| 562 | // Not required, but simplifies bug hunting |
| 563 | sort.Ints(*A) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | // Based on Mizobuchi et al (2000), p. 124 |
| 567 | // This iterates for every state through the complete double array |
| 568 | // structure until it finds a gap that fits all outgoing transitions |
| 569 | // of the state. This is extremely slow, but is only necessary in the |
| 570 | // construction phase of the tokenizer. |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 571 | func (dat *DaTokenizer) xCheck(symbols []int) int { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 572 | |
| 573 | // Start at the first entry of the double array list |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 574 | base := 1 |
| 575 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 576 | OVERLAP: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 577 | |
| 578 | // Resize the array if necessary |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 579 | dat.resize((base + FINAL) * 2) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 580 | for _, a := range symbols { |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 581 | if dat.getCheck(base+a) != 0 { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 582 | base++ |
| 583 | goto OVERLAP |
| 584 | } |
| 585 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 586 | return base |
| 587 | } |
| 588 | |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 589 | func (dat *DaTokenizer) LoadLevel() float64 { |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 590 | |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 591 | if dat.loadLevel >= 0 { |
| 592 | return dat.loadLevel |
| 593 | } |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 594 | nonEmpty := 0 |
| 595 | all := len(dat.array) / 2 |
| 596 | for x := 1; x <= len(dat.array); x = x + 2 { |
| 597 | if dat.array[x] != 0 { |
| 598 | nonEmpty++ |
| 599 | } |
| 600 | } |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 601 | dat.loadLevel = float64(nonEmpty) / float64(all) * 100 |
| 602 | return dat.loadLevel |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 603 | } |
| 604 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame^] | 605 | // WriteTo stores the double array data in an io.Writer. |
| 606 | func (dat *DaTokenizer) WriteTo(w io.Writer) (n int64, err error) { |
| 607 | |
| 608 | // Store magical header |
| 609 | all, err := w.Write([]byte(MAGIC)) |
| 610 | if err != nil { |
| 611 | log.Error().Msg("Unable to write data") |
| 612 | } |
| 613 | |
| 614 | // Get sigma as a list |
| 615 | sigmalist := make([]rune, len(dat.sigma)+16) |
| 616 | max := 0 |
| 617 | for sym, num := range dat.sigma { |
| 618 | sigmalist[num] = sym |
| 619 | if num > max { |
| 620 | max = num |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | sigmalist = sigmalist[:max+1] |
| 625 | |
| 626 | buf := make([]byte, 0, 12) |
| 627 | bo.PutUint16(buf[0:2], VERSION) |
| 628 | bo.PutUint16(buf[2:4], uint16(EPSILON)) |
| 629 | bo.PutUint16(buf[4:6], uint16(UNKNOWN)) |
| 630 | bo.PutUint16(buf[6:8], uint16(IDENTITY)) |
| 631 | bo.PutUint16(buf[8:10], uint16(FINAL)) |
| 632 | bo.PutUint16(buf[10:12], uint16(len(sigmalist))) |
| 633 | more, err := w.Write(buf[0:12]) |
| 634 | if err != nil { |
| 635 | log.Error().Msg("Unable to write data") |
| 636 | } |
| 637 | |
| 638 | all += more |
| 639 | |
| 640 | wbuf := bytes.NewBuffer(nil) |
| 641 | wbufWrap := bufio.NewWriter(wbuf) |
| 642 | |
| 643 | // Write sigma |
| 644 | for _, sym := range sigmalist { |
| 645 | more, err = wbufWrap.WriteRune(sym) |
| 646 | if err != nil { |
| 647 | log.Error().Msg("Unable to write data") |
| 648 | } |
| 649 | all += more |
| 650 | } |
| 651 | wbufWrap.Flush() |
| 652 | more, err = w.Write(wbuf.Bytes()) |
| 653 | if err != nil { |
| 654 | log.Error().Msg("Unable to write data") |
| 655 | } |
| 656 | all += more |
| 657 | |
| 658 | // Test marker - could be checksum |
| 659 | more, err = w.Write([]byte("T")) |
| 660 | if err != nil { |
| 661 | log.Error().Msg("Unable to write data") |
| 662 | } |
| 663 | all += more |
| 664 | |
| 665 | wbuf.Reset() |
| 666 | |
| 667 | for _, d := range dat.array { |
| 668 | bo.PutUint32(buf[0:4], uint32(d)) |
| 669 | more, err := w.Write(buf[0:4]) |
| 670 | if err != nil { |
| 671 | log.Error().Msg("Unable to write data") |
| 672 | } |
| 673 | all += more |
| 674 | } |
| 675 | |
| 676 | return int64(all), err |
| 677 | } |
| 678 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 679 | // Match an input string against the double array |
| 680 | // FSA. |
| 681 | // |
| 682 | // Based on Mizobuchi et al (2000), p. 129, |
| 683 | // with additional support for IDENTITY, UNKNOWN |
| 684 | // and EPSILON transitions. |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 685 | func (tok *DaTokenizer) Match(input string) bool { |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 686 | var a int |
| 687 | var tu int |
| 688 | var ok bool |
| 689 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 690 | t := 1 // Initial state |
| 691 | chars := []rune(input) |
| 692 | i := 0 |
| 693 | |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 694 | for i < len(chars) { |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 695 | a, ok = tok.sigma[chars[i]] |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 696 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 697 | // Support identity symbol if character is not in sigma |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 698 | if !ok && IDENTITY != -1 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 699 | if DEBUG { |
| 700 | fmt.Println("IDENTITY symbol", string(chars[i]), "->", IDENTITY) |
| 701 | } |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 702 | a = IDENTITY |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 703 | } else if DEBUG { |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 704 | fmt.Println("Sigma transition is okay for [", string(chars[i]), "]") |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 705 | } |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 706 | tu = t |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 707 | CHECK: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 708 | t = tok.getBase(tu) + a |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 709 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 710 | // Check if the transition is valid according to the double array |
| 711 | if t > tok.getCheck(1) || tok.getCheck(t) != tu { |
| 712 | |
| 713 | if DEBUG { |
| 714 | fmt.Println("Match is not fine!", t, "and", tok.getCheck(t), "vs", tu) |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 715 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 716 | |
| 717 | if !ok && a == IDENTITY { |
| 718 | // Try again with unknown symbol, in case identity failed |
| 719 | if DEBUG { |
| 720 | fmt.Println("UNKNOWN symbol", string(chars[i]), "->", UNKNOWN) |
| 721 | } |
| 722 | a = UNKNOWN |
| 723 | |
| 724 | } else if a != EPSILON { |
| 725 | // Try again with epsilon symbol, in case everything else failed |
| 726 | if DEBUG { |
| 727 | fmt.Println("EPSILON symbol", string(chars[i]), "->", EPSILON) |
| 728 | } |
| 729 | a = EPSILON |
| 730 | } else { |
| 731 | break |
| 732 | } |
| 733 | goto CHECK |
| 734 | } else if tok.getBase(t) < 0 { |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 735 | // Move to representative state |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 736 | t = -1 * tok.getBase(t) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 737 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 738 | |
| 739 | // Transition is fine |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 740 | if a != EPSILON { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 741 | // Character consumed |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 742 | i++ |
| 743 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 744 | // TODO: |
| 745 | // Prevent endless epsilon loops! |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 746 | } |
| 747 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 748 | if i != len(chars) { |
| 749 | if DEBUG { |
| 750 | fmt.Println("Not at the end") |
| 751 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 752 | return false |
| 753 | } |
| 754 | |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 755 | FINALCHECK: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 756 | |
| 757 | // Automaton is in a final state |
| 758 | if tok.getCheck(tok.getBase(t)+FINAL) == t { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 759 | return true |
| 760 | } |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 761 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 762 | // Check epsilon transitions until a final state is reached |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 763 | tu = t |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 764 | t = tok.getBase(tu) + EPSILON |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 765 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 766 | // Epsilon transition failed |
| 767 | if t > tok.getCheck(1) || tok.getCheck(t) != tu { |
| 768 | if DEBUG { |
| 769 | fmt.Println("Match is not fine!", t, "and", tok.getCheck(t), "vs", tu) |
| 770 | } |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 771 | return false |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 772 | |
| 773 | } else if tok.getBase(t) < 0 { |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 774 | // Move to representative state |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 775 | t = -1 * tok.getBase(t) |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 776 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 777 | |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 778 | goto FINALCHECK |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 779 | } |