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