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 | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 9 | // The maximum number of states is 107.3741.823 (30bit), |
| 10 | // with a loadfactor of ~70, this means roughly 70 million |
| 11 | // states in the FSA, which is sufficient for the current |
| 12 | // job. |
| 13 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 14 | // TODO: |
| 15 | // - replace maxSize with the check value |
| 16 | // - Strip first state and make everything start with 0! |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 17 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 18 | import ( |
| 19 | "bufio" |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 20 | "bytes" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 21 | "compress/gzip" |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 22 | "encoding/binary" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 23 | "fmt" |
| 24 | "io" |
| 25 | "os" |
Akron | c9d84a6 | 2021-08-03 15:56:03 +0200 | [diff] [blame] | 26 | "sort" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 27 | "strconv" |
| 28 | "strings" |
| 29 | "unicode/utf8" |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 30 | |
| 31 | "github.com/rs/zerolog/log" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | const ( |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 35 | PROPS = 1 |
| 36 | SIGMA = 2 |
| 37 | STATES = 3 |
| 38 | NONE = 4 |
| 39 | NEWLINE = '\u000a' |
| 40 | DEBUG = false |
| 41 | MAGIC = "DATOK" |
| 42 | VERSION = uint16(1) |
| 43 | firstBit uint32 = 1 << 31 |
| 44 | secondBit uint32 = 1 << 30 |
| 45 | restBit uint32 = ^uint32(0) &^ (firstBit | secondBit) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 46 | ) |
| 47 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 48 | var bo binary.ByteOrder = binary.LittleEndian |
| 49 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 50 | type mapping struct { |
| 51 | source int |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 52 | target uint32 |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | type edge struct { |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 56 | inSym int |
| 57 | outSym int |
| 58 | end int |
| 59 | nontoken bool |
| 60 | tokenend bool |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | type Tokenizer struct { |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 64 | // sigma map[rune]int |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 65 | sigmaRev map[int]rune |
| 66 | arcCount int |
| 67 | stateCount int |
| 68 | sigmaCount int |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 69 | transitions []map[int]*edge |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 70 | |
| 71 | // Special symbols in sigma |
| 72 | epsilon int |
| 73 | unknown int |
| 74 | identity int |
| 75 | final int |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 78 | type DaTokenizer struct { |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 79 | // sigmaRev map[int]rune |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 80 | sigma map[rune]int |
| 81 | maxSize int |
| 82 | loadFactor float64 |
| 83 | array []uint32 |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 84 | // lastFilledBase uint32 |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 85 | |
| 86 | // Special symbols in sigma |
| 87 | epsilon int |
| 88 | unknown int |
| 89 | identity int |
| 90 | final int |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 93 | func LoadFomaFile(file string) *Tokenizer { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 94 | f, err := os.Open(file) |
| 95 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 96 | log.Error().Err(err) |
| 97 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 98 | } |
| 99 | defer f.Close() |
| 100 | |
| 101 | gz, err := gzip.NewReader(f) |
| 102 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 103 | log.Error().Err(err) |
| 104 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 105 | } |
| 106 | defer gz.Close() |
| 107 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 108 | return ParseFoma(gz) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 111 | func ParseFoma(ior io.Reader) *Tokenizer { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 112 | r := bufio.NewReader(ior) |
| 113 | |
| 114 | tok := &Tokenizer{ |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 115 | sigmaRev: make(map[int]rune), |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 116 | epsilon: -1, |
| 117 | unknown: -1, |
| 118 | identity: -1, |
| 119 | final: -1, |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 122 | var state, inSym, outSym, end, final int |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 123 | |
| 124 | mode := 0 |
| 125 | var elem []string |
| 126 | var elemint [5]int |
| 127 | |
| 128 | for { |
| 129 | line, err := r.ReadString('\n') |
| 130 | if err != nil { |
| 131 | if err == io.EOF { |
| 132 | break |
| 133 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 134 | log.Error().Err(err) |
| 135 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 136 | } |
| 137 | if strings.HasPrefix(line, "##foma-net") { |
| 138 | continue |
| 139 | } |
| 140 | if strings.HasPrefix(line, "##props##") { |
| 141 | mode = PROPS |
| 142 | continue |
| 143 | } |
| 144 | if strings.HasPrefix(line, "##states##") { |
| 145 | mode = STATES |
| 146 | |
| 147 | // Adds a final transition symbol to sigma |
| 148 | // written as '#' in Mizobuchi et al (2000) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 149 | tok.sigmaCount++ |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 150 | tok.final = tok.sigmaCount |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 151 | continue |
| 152 | } |
| 153 | if strings.HasPrefix(line, "##sigma##") { |
| 154 | mode = SIGMA |
| 155 | continue |
| 156 | } |
| 157 | if strings.HasPrefix(line, "##end##") { |
| 158 | mode = NONE |
| 159 | continue |
| 160 | } |
| 161 | |
| 162 | switch mode { |
| 163 | case PROPS: |
| 164 | { |
| 165 | elem = strings.Split(line, " ") |
| 166 | /* |
| 167 | fmt.Println("arity: " + elem[0]) |
| 168 | fmt.Println("arccount: " + elem[1]) |
| 169 | fmt.Println("statecount: " + elem[2]) |
| 170 | fmt.Println("linecount: " + elem[3]) |
| 171 | fmt.Println("finalcount: " + elem[4]) |
| 172 | fmt.Println("pathcount: " + elem[5]) |
| 173 | fmt.Println("is_deterministic: " + elem[6]) |
| 174 | fmt.Println("is_pruned: " + elem[7]) |
| 175 | fmt.Println("is_minimized: " + elem[8]) |
| 176 | fmt.Println("is_epsilon_free: " + elem[9]) |
| 177 | fmt.Println("is_loop_free: " + elem[10]) |
| 178 | fmt.Println("extras: " + elem[11]) |
| 179 | fmt.Println("name: " + elem[12]) |
| 180 | */ |
| 181 | if elem[6] != "1" { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 182 | log.Error().Msg("The FST needs to be deterministic") |
| 183 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 184 | } |
| 185 | if elem[9] != "1" { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 186 | log.Error().Msg("The FST needs to be epsilon free") |
| 187 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | elemint[0], err = strconv.Atoi(elem[1]) |
| 191 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 192 | log.Error().Msg("Can't read arccount") |
| 193 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 194 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 195 | tok.arcCount = elemint[0] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 196 | |
| 197 | // States start at 1 in Mizobuchi et al (2000), |
| 198 | // as the state 0 is associated with a fail. |
| 199 | // Initialize states and transitions |
| 200 | elemint[0], err = strconv.Atoi(elem[2]) |
| 201 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 202 | log.Error().Msg("Can't read statecount") |
| 203 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 204 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 205 | tok.stateCount = elemint[0] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 206 | tok.transitions = make([]map[int]*edge, elemint[0]+1) |
| 207 | continue |
| 208 | } |
| 209 | case STATES: |
| 210 | { |
| 211 | elem = strings.Split(line[0:len(line)-1], " ") |
| 212 | if elem[0] == "-1" { |
| 213 | continue |
| 214 | } |
| 215 | elemint[0], err = strconv.Atoi(elem[0]) |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 216 | if err != nil { |
| 217 | break |
| 218 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 219 | |
| 220 | if len(elem) > 1 { |
| 221 | elemint[1], err = strconv.Atoi(elem[1]) |
| 222 | if err != nil { |
| 223 | break |
| 224 | } |
| 225 | if len(elem) > 2 { |
| 226 | elemint[2], err = strconv.Atoi(elem[2]) |
| 227 | if err != nil { |
| 228 | break |
| 229 | } |
| 230 | if len(elem) > 3 { |
| 231 | elemint[3], err = strconv.Atoi(elem[3]) |
| 232 | if err != nil { |
| 233 | break |
| 234 | } |
| 235 | if len(elem) > 4 { |
| 236 | elemint[4], err = strconv.Atoi(elem[4]) |
| 237 | if err != nil { |
| 238 | break |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | switch len(elem) { |
| 246 | case 5: |
| 247 | { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 248 | state = elemint[0] |
| 249 | inSym = elemint[1] |
| 250 | outSym = elemint[2] |
| 251 | end = elemint[3] |
| 252 | final = elemint[4] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 253 | } |
| 254 | case 4: |
| 255 | { |
| 256 | if elemint[1] == -1 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 257 | state = elemint[0] |
| 258 | final = elemint[3] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 259 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 260 | state = elemint[0] |
| 261 | inSym = elemint[1] |
| 262 | end = elemint[2] |
| 263 | final = elemint[3] |
| 264 | outSym = inSym |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | case 3: |
| 268 | { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 269 | inSym = elemint[0] |
| 270 | outSym = elemint[1] |
| 271 | end = elemint[2] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 272 | } |
| 273 | case 2: |
| 274 | { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 275 | inSym = elemint[0] |
| 276 | end = elemint[1] |
| 277 | outSym = inSym |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 281 | // While the states in foma start with 0, the states in the |
| 282 | // Mizobuchi FSA start with one - so we increase every state by 1. |
| 283 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 284 | nontoken := false |
| 285 | tokenend := false |
| 286 | |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame^] | 287 | // ID needs to be > 1 |
| 288 | inSym++ |
| 289 | outSym++ |
| 290 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 291 | if inSym != outSym { |
| 292 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 293 | if tok.sigmaRev[outSym] == NEWLINE { |
| 294 | tokenend = true |
| 295 | } else if outSym == tok.epsilon { |
| 296 | nontoken = true |
| 297 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 298 | log.Error().Msg( |
| 299 | "Unsupported transition: " + |
| 300 | strconv.Itoa(state) + |
| 301 | " -> " + strconv.Itoa(end) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 302 | " (" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 303 | strconv.Itoa(inSym) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 304 | ":" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 305 | strconv.Itoa(outSym) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 306 | ") (" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 307 | string(tok.sigmaRev[inSym]) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 308 | ":" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 309 | string(tok.sigmaRev[outSym]) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 310 | ")") |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 311 | os.Exit(1) |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 312 | } |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 313 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 314 | } else if inSym == tok.epsilon { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 315 | log.Error().Msg("Epsilon transitions not supported") |
| 316 | os.Exit(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 | // This collects all edges until arrstate changes |
| 320 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 321 | // TODO: |
| 322 | // if arrin == EPSILON && arrout == TOKENEND, mark state as newline |
| 323 | // if the next transition is the same, remove TOKENEND and add SENTENCEEND |
| 324 | // This requires to remove the transition alltogether and marks the state instead. |
| 325 | |
| 326 | // TODO: |
| 327 | // if arrout == EPSILON, mark the transition as NOTOKEN |
| 328 | |
| 329 | targetObj := &edge{ |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 330 | inSym: inSym, |
| 331 | outSym: outSym, |
| 332 | end: end + 1, |
| 333 | tokenend: tokenend, |
| 334 | nontoken: nontoken, |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 337 | // Initialize outgoing states |
| 338 | if tok.transitions[state+1] == nil { |
| 339 | tok.transitions[state+1] = make(map[int]*edge) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 340 | } |
| 341 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 342 | // Ignore transitions with invalid symbols |
| 343 | if inSym >= 0 { |
| 344 | tok.transitions[state+1][inSym] = targetObj |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 345 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 346 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 347 | // Add final transition |
| 348 | if final == 1 { |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 349 | tok.transitions[state+1][tok.final] = &edge{} |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 350 | } |
| 351 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 352 | if DEBUG { |
| 353 | fmt.Println("Add", |
| 354 | state+1, "->", end+1, |
| 355 | "(", |
| 356 | inSym, |
| 357 | ":", |
| 358 | outSym, |
| 359 | ") (", |
| 360 | string(tok.sigmaRev[inSym]), |
| 361 | ":", |
| 362 | string(tok.sigmaRev[outSym]), |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame^] | 363 | ")", |
| 364 | ";", |
| 365 | "TE:", tokenend, |
| 366 | "NT:", nontoken) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 367 | } |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 368 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 369 | continue |
| 370 | } |
| 371 | case SIGMA: |
| 372 | { |
| 373 | elem = strings.SplitN(line[0:len(line)-1], " ", 2) |
| 374 | |
| 375 | // Turn string into sigma id |
| 376 | number, err := strconv.Atoi(elem[0]) |
| 377 | |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame^] | 378 | // ID needs to be > 1 |
| 379 | number++ |
| 380 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 381 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 382 | log.Error().Err(err) |
| 383 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 384 | } |
| 385 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 386 | tok.sigmaCount = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 387 | |
| 388 | var symbol rune |
| 389 | |
| 390 | // Read rune |
| 391 | if utf8.RuneCountInString(elem[1]) == 1 { |
| 392 | symbol = []rune(elem[1])[0] |
| 393 | |
| 394 | // Probably a MCS |
| 395 | } else if utf8.RuneCountInString(elem[1]) > 1 { |
| 396 | switch elem[1] { |
| 397 | case "@_EPSILON_SYMBOL_@": |
| 398 | { |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 399 | tok.epsilon = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 400 | continue |
| 401 | } |
| 402 | case "@_UNKNOWN_SYMBOL_@": |
| 403 | { |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 404 | tok.unknown = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 405 | continue |
| 406 | } |
| 407 | |
| 408 | case "@_IDENTITY_SYMBOL_@": |
| 409 | { |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 410 | tok.identity = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 411 | continue |
| 412 | } |
| 413 | default: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 414 | { |
| 415 | log.Error().Msg("MCS not supported: " + line) |
| 416 | os.Exit(1) |
| 417 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 418 | } |
| 419 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 420 | } else { // Probably a new line symbol |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 421 | line, err = r.ReadString('\n') |
| 422 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 423 | log.Error().Err(err) |
| 424 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 425 | } |
| 426 | if len(line) != 1 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 427 | log.Error().Msg("MCS not supported:" + line) |
| 428 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 429 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 430 | symbol = rune(NEWLINE) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 431 | } |
| 432 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 433 | tok.sigmaRev[number] = symbol |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | return tok |
| 439 | } |
| 440 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 441 | // Set alphabet A to the list of all symbols |
| 442 | // outgoing from s |
| 443 | func (tok *Tokenizer) get_set(s int, A *[]int) { |
| 444 | for a := range tok.transitions[s] { |
| 445 | *A = append(*A, a) |
| 446 | } |
| 447 | |
| 448 | // Not required, but simplifies bug hunting |
| 449 | sort.Ints(*A) |
| 450 | } |
| 451 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 452 | // Implementation of Mizobuchi et al (2000), p.128 |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 453 | func (tok *Tokenizer) ToDoubleArray() *DaTokenizer { |
| 454 | |
| 455 | dat := &DaTokenizer{ |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 456 | sigma: make(map[rune]int), |
| 457 | loadFactor: -1, |
| 458 | final: tok.final, |
| 459 | unknown: tok.unknown, |
| 460 | identity: tok.identity, |
| 461 | epsilon: tok.epsilon, |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 462 | // lastFilledBase: 1, |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | for num, sym := range tok.sigmaRev { |
| 466 | dat.sigma[sym] = num |
| 467 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 468 | |
| 469 | mark := 0 |
| 470 | size := 0 |
| 471 | |
| 472 | // Create a mapping from s to t |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 473 | table := make([]*mapping, tok.arcCount+1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 474 | |
| 475 | table[size] = &mapping{source: 1, target: 1} |
| 476 | size++ |
| 477 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 478 | // Allocate space for the outgoing symbol range |
| 479 | A := make([]int, 0, tok.sigmaCount) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 480 | |
| 481 | for mark < size { |
| 482 | s := table[mark].source // This is a state in Ms |
| 483 | t := table[mark].target // This is a state in Mt |
| 484 | mark++ |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 485 | |
| 486 | // Following the paper, here the state t can be remembered |
| 487 | // in the set of states St |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 488 | A = A[:0] |
| 489 | tok.get_set(s, &A) |
| 490 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 491 | // Set base to the first free slot in the double array |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 492 | dat.setBase(t, dat.xCheck(A)) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 493 | |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 494 | // TODO: |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 495 | // Sort the outgoing transitions based on the |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 496 | // outdegree of .end |
| 497 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 498 | // Iterate over all outgoing symbols |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 499 | for _, a := range A { |
| 500 | |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 501 | if a != tok.final { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 502 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 503 | // Aka g(s, a) |
| 504 | s1 := tok.transitions[s][a].end |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 505 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 506 | // Store the transition |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 507 | t1 := dat.getBase(t) + uint32(a) |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 508 | dat.setCheck(t1, t) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 509 | |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame^] | 510 | if DEBUG { |
| 511 | fmt.Println("Translate transition", |
| 512 | s, "->", s1, "(", a, ")", "to", t, "->", t1) |
| 513 | } |
| 514 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 515 | // Mark the state as being the target of a nontoken transition |
| 516 | if tok.transitions[s][a].nontoken { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 517 | dat.setNonToken(t1, true) |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame^] | 518 | if DEBUG { |
| 519 | fmt.Println("Set", t1, "to nontoken") |
| 520 | } |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 521 | } |
| 522 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 523 | // Mark the state as being the target of a tokenend transition |
| 524 | if tok.transitions[s][a].tokenend { |
| 525 | dat.setTokenEnd(t1, true) |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame^] | 526 | if DEBUG { |
| 527 | fmt.Println("Set", t1, "to tokenend") |
| 528 | } |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 529 | } |
| 530 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 531 | // Check for representative states |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 532 | r := in_table(s1, table, size) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 533 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 534 | if r == 0 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 535 | // Remember the mapping |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 536 | table[size] = &mapping{source: s1, target: t1} |
| 537 | size++ |
| 538 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 539 | // Overwrite with the representative state |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 540 | dat.setBase(t1, r) |
| 541 | dat.setSeparate(t1, true) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 542 | } |
| 543 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 544 | // Store a final transition |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 545 | dat.setCheck(dat.getBase(t)+uint32(dat.final), t) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | // Following Mizobuchi et al (2000) the size of the |
| 551 | // FSA should be stored in check(1). |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 552 | dat.setSize(dat.maxSize + 1) |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 553 | dat.array = dat.array[:dat.maxSize+1] |
| 554 | return dat |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 555 | } |
| 556 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 557 | // Check the table if a mapping of s |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 558 | // exists and return this as a representative. |
| 559 | // Currently iterates through the whole table |
| 560 | // in a bruteforce manner. |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 561 | func in_table(s int, table []*mapping, size int) uint32 { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 562 | for x := 0; x < size; x++ { |
| 563 | if table[x].source == s { |
| 564 | return table[x].target |
| 565 | } |
| 566 | } |
| 567 | return 0 |
| 568 | } |
| 569 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 570 | // Resize double array when necessary |
| 571 | func (dat *DaTokenizer) resize(l int) { |
| 572 | // TODO: |
| 573 | // This is a bit too aggressive atm and should be calmed down. |
| 574 | if len(dat.array) <= l { |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 575 | dat.array = append(dat.array, make([]uint32, l)...) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 576 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 577 | } |
Akron | c9d84a6 | 2021-08-03 15:56:03 +0200 | [diff] [blame] | 578 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 579 | // Set base value in double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 580 | func (dat *DaTokenizer) setBase(p uint32, v uint32) { |
| 581 | l := int(p*2 + 1) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 582 | dat.resize(l) |
| 583 | if dat.maxSize < l { |
| 584 | dat.maxSize = l |
| 585 | } |
| 586 | dat.array[p*2] = v |
| 587 | } |
| 588 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 589 | // Returns true if a state is separate pointing to a representative |
| 590 | func (dat *DaTokenizer) isSeparate(p uint32) bool { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 591 | return dat.array[p*2]&firstBit != 0 |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | // Mark a state as separate pointing to a representative |
| 595 | func (dat *DaTokenizer) setSeparate(p uint32, sep bool) { |
| 596 | if sep { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 597 | dat.array[p*2] |= firstBit |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 598 | } else { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 599 | dat.array[p*2] &= (restBit | secondBit) |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 603 | // Returns true if a state is the target of a nontoken transition |
| 604 | func (dat *DaTokenizer) isNonToken(p uint32) bool { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 605 | return dat.array[p*2+1]&firstBit != 0 |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | // Mark a state as being the target of a nontoken transition |
| 609 | func (dat *DaTokenizer) setNonToken(p uint32, sep bool) { |
| 610 | if sep { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 611 | dat.array[p*2+1] |= firstBit |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 612 | } else { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 613 | dat.array[p*2+1] &= (restBit | secondBit) |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 617 | // Returns true if a state is the target of a tokenend transition |
| 618 | func (dat *DaTokenizer) isTokenEnd(p uint32) bool { |
| 619 | return dat.array[p*2+1]&secondBit != 0 |
| 620 | } |
| 621 | |
| 622 | // Mark a state as being the target of a tokenend transition |
| 623 | func (dat *DaTokenizer) setTokenEnd(p uint32, sep bool) { |
| 624 | if sep { |
| 625 | dat.array[p*2+1] |= secondBit |
| 626 | } else { |
| 627 | dat.array[p*2+1] &= (restBit | firstBit) |
| 628 | } |
| 629 | } |
| 630 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 631 | // Get base value in double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 632 | func (dat *DaTokenizer) getBase(p uint32) uint32 { |
| 633 | if int(p*2) >= len(dat.array) { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 634 | return 0 |
| 635 | } |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 636 | return dat.array[p*2] & restBit |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | // Set check value in double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 640 | func (dat *DaTokenizer) setCheck(p uint32, v uint32) { |
| 641 | l := int(p*2 + 1) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 642 | dat.resize(l) |
| 643 | if dat.maxSize < l { |
| 644 | dat.maxSize = l |
| 645 | } |
| 646 | dat.array[(p*2)+1] = v |
| 647 | } |
| 648 | |
| 649 | // Get check value in double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 650 | func (dat *DaTokenizer) getCheck(p uint32) uint32 { |
| 651 | if int((p*2)+1) >= len(dat.array) { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 652 | return 0 |
| 653 | } |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 654 | return dat.array[(p*2)+1] & restBit |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | // Set size of double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 658 | func (dat *DaTokenizer) setSize(v int) { |
| 659 | dat.setCheck(1, uint32(v)) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | // Get size of double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 663 | func (dat *DaTokenizer) GetSize() int { |
| 664 | return int(dat.getCheck(1)) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | // Based on Mizobuchi et al (2000), p. 124 |
| 668 | // This iterates for every state through the complete double array |
| 669 | // structure until it finds a gap that fits all outgoing transitions |
| 670 | // of the state. This is extremely slow, but is only necessary in the |
| 671 | // construction phase of the tokenizer. |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 672 | func (dat *DaTokenizer) xCheck(symbols []int) uint32 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 673 | |
| 674 | // Start at the first entry of the double array list |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 675 | base := uint32(1) // dat.lastFilledBase |
| 676 | // skip := false |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 677 | OVERLAP: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 678 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 679 | /* |
| 680 | if !skip { |
| 681 | if dat.getCheck(base) != 0 { |
| 682 | dat.lastFilledBase = base |
| 683 | } else { |
| 684 | skip = true |
| 685 | } |
| 686 | } |
| 687 | */ |
| 688 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 689 | // Resize the array if necessary |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 690 | dat.resize((int(base) + dat.final) * 2) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 691 | for _, a := range symbols { |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 692 | if dat.getCheck(base+uint32(a)) != 0 { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 693 | base++ |
| 694 | goto OVERLAP |
| 695 | } |
| 696 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 697 | return base |
| 698 | } |
| 699 | |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 700 | // LoadFactor as defined in Kanda et al (2018), |
| 701 | // i.e. the proportion of non-empty elements to all elements. |
| 702 | func (dat *DaTokenizer) LoadFactor() float64 { |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 703 | |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 704 | // Cache the loadfactor |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 705 | if dat.loadFactor > 0 { |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 706 | return dat.loadFactor |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 707 | } |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 708 | nonEmpty := 0 |
| 709 | all := len(dat.array) / 2 |
| 710 | for x := 1; x <= len(dat.array); x = x + 2 { |
| 711 | if dat.array[x] != 0 { |
| 712 | nonEmpty++ |
| 713 | } |
| 714 | } |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 715 | dat.loadFactor = float64(nonEmpty) / float64(all) * 100 |
| 716 | return dat.loadFactor |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 717 | } |
| 718 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 719 | // WriteTo stores the double array data in an io.Writer. |
| 720 | func (dat *DaTokenizer) WriteTo(w io.Writer) (n int64, err error) { |
| 721 | |
| 722 | // Store magical header |
| 723 | all, err := w.Write([]byte(MAGIC)) |
| 724 | if err != nil { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 725 | log.Error().Err(err) |
| 726 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | // Get sigma as a list |
| 730 | sigmalist := make([]rune, len(dat.sigma)+16) |
| 731 | max := 0 |
| 732 | for sym, num := range dat.sigma { |
| 733 | sigmalist[num] = sym |
| 734 | if num > max { |
| 735 | max = num |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | sigmalist = sigmalist[:max+1] |
| 740 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 741 | buf := make([]byte, 0, 16) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 742 | bo.PutUint16(buf[0:2], VERSION) |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 743 | bo.PutUint16(buf[2:4], uint16(dat.epsilon)) |
| 744 | bo.PutUint16(buf[4:6], uint16(dat.unknown)) |
| 745 | bo.PutUint16(buf[6:8], uint16(dat.identity)) |
| 746 | bo.PutUint16(buf[8:10], uint16(dat.final)) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 747 | bo.PutUint16(buf[10:12], uint16(len(sigmalist))) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 748 | bo.PutUint32(buf[12:16], uint32(len(dat.array))) |
| 749 | more, err := w.Write(buf[0:16]) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 750 | if err != nil { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 751 | log.Error().Err(err) |
| 752 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | all += more |
| 756 | |
| 757 | wbuf := bytes.NewBuffer(nil) |
| 758 | wbufWrap := bufio.NewWriter(wbuf) |
| 759 | |
| 760 | // Write sigma |
| 761 | for _, sym := range sigmalist { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 762 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 763 | more, err = wbufWrap.WriteRune(sym) |
| 764 | if err != nil { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 765 | log.Error().Err(err) |
| 766 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 767 | } |
| 768 | all += more |
| 769 | } |
| 770 | wbufWrap.Flush() |
| 771 | more, err = w.Write(wbuf.Bytes()) |
| 772 | if err != nil { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 773 | log.Error().Err(err) |
| 774 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 775 | } |
| 776 | all += more |
| 777 | |
| 778 | // Test marker - could be checksum |
| 779 | more, err = w.Write([]byte("T")) |
| 780 | if err != nil { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 781 | log.Error().Err(err) |
| 782 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 783 | } |
| 784 | all += more |
| 785 | |
| 786 | wbuf.Reset() |
| 787 | |
| 788 | for _, d := range dat.array { |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 789 | bo.PutUint32(buf[0:4], d) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 790 | more, err := w.Write(buf[0:4]) |
| 791 | if err != nil { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 792 | log.Error().Err(err) |
| 793 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 794 | } |
| 795 | all += more |
| 796 | } |
| 797 | |
| 798 | return int64(all), err |
| 799 | } |
| 800 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 801 | func LoadDatokFile(file string) *DaTokenizer { |
| 802 | f, err := os.Open(file) |
| 803 | if err != nil { |
| 804 | log.Error().Err(err) |
| 805 | os.Exit(0) |
| 806 | } |
| 807 | defer f.Close() |
| 808 | |
| 809 | gz, err := gzip.NewReader(f) |
| 810 | if err != nil { |
| 811 | log.Error().Err(err) |
| 812 | os.Exit(0) |
| 813 | } |
| 814 | defer gz.Close() |
| 815 | |
| 816 | return ParseDatok(gz) |
| 817 | } |
| 818 | |
| 819 | func ParseDatok(ior io.Reader) *DaTokenizer { |
| 820 | |
| 821 | dat := &DaTokenizer{ |
| 822 | sigma: make(map[rune]int), |
| 823 | epsilon: 0, |
| 824 | unknown: 0, |
| 825 | identity: 0, |
| 826 | final: 0, |
| 827 | loadFactor: 0, |
| 828 | } |
| 829 | |
| 830 | r := bufio.NewReader(ior) |
| 831 | |
| 832 | all := 0 |
| 833 | |
| 834 | buf := make([]byte, 1024) |
| 835 | buf = buf[0:len(MAGIC)] |
| 836 | |
| 837 | more, err := r.Read(buf) |
| 838 | |
| 839 | if err != nil { |
| 840 | log.Error().Err(err) |
| 841 | return nil |
| 842 | } |
| 843 | |
| 844 | all += more |
| 845 | |
| 846 | if string(MAGIC) != string(buf) { |
| 847 | log.Error().Msg("Not a datok file") |
| 848 | return nil |
| 849 | } |
| 850 | |
| 851 | more, err = r.Read(buf[0:16]) |
| 852 | if err != nil { |
| 853 | log.Error().Err(err) |
| 854 | return nil |
| 855 | } |
| 856 | |
| 857 | all += more |
| 858 | |
| 859 | // version := bo.Uint16(buf[0:2]) |
| 860 | dat.epsilon = int(bo.Uint16(buf[2:4])) |
| 861 | dat.unknown = int(bo.Uint16(buf[4:6])) |
| 862 | dat.identity = int(bo.Uint16(buf[6:8])) |
| 863 | dat.final = int(bo.Uint16(buf[8:10])) |
| 864 | |
| 865 | sigmaCount := int(bo.Uint16(buf[10:12])) |
| 866 | arraySize := int(bo.Uint32(buf[12:16])) |
| 867 | |
| 868 | for x := 0; x < sigmaCount; x++ { |
| 869 | sym, more, err := r.ReadRune() |
| 870 | if err == nil && sym != 0 { |
| 871 | dat.sigma[sym] = x |
| 872 | } |
| 873 | all += more |
| 874 | } |
| 875 | |
| 876 | more, err = r.Read(buf[0:1]) |
| 877 | |
| 878 | if err != nil { |
| 879 | log.Error().Err(err) |
| 880 | return nil |
| 881 | } |
| 882 | |
| 883 | all += more |
| 884 | |
| 885 | if string("T") != string(buf[0:1]) { |
| 886 | log.Error().Msg("Not a datok file") |
| 887 | return nil |
| 888 | } |
| 889 | |
| 890 | // Read based on length |
| 891 | dat.array = make([]uint32, arraySize) |
| 892 | |
| 893 | for x := 0; x < arraySize; x++ { |
| 894 | more, err = r.Read(buf[0:4]) |
| 895 | if err != nil { |
| 896 | log.Error().Err(err) |
| 897 | return nil |
| 898 | } |
| 899 | all += more |
| 900 | dat.array[x] = bo.Uint32(buf[0:4]) |
| 901 | } |
| 902 | |
| 903 | return dat |
| 904 | } |
| 905 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 906 | // Match an input string against the double array |
| 907 | // FSA. |
| 908 | // |
| 909 | // Based on Mizobuchi et al (2000), p. 129, |
| 910 | // with additional support for IDENTITY, UNKNOWN |
| 911 | // and EPSILON transitions. |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 912 | func (dat *DaTokenizer) Match(input string) bool { |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 913 | var a int |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 914 | var tu uint32 |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 915 | var ok bool |
| 916 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 917 | t := uint32(1) // Initial state |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 918 | chars := []rune(input) |
| 919 | i := 0 |
| 920 | |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 921 | for i < len(chars) { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 922 | a, ok = dat.sigma[chars[i]] |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 923 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 924 | // Support identity symbol if character is not in sigma |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 925 | if !ok && dat.identity != -1 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 926 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 927 | fmt.Println("IDENTITY symbol", string(chars[i]), "->", dat.identity) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 928 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 929 | a = dat.identity |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 930 | } else if DEBUG { |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 931 | fmt.Println("Sigma transition is okay for [", string(chars[i]), "]") |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 932 | } |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 933 | tu = t |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 934 | CHECK: |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 935 | t = dat.getBase(tu) + uint32(a) |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 936 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 937 | // Check if the transition is valid according to the double array |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 938 | if t > dat.getCheck(1) || dat.getCheck(t) != tu { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 939 | |
| 940 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 941 | fmt.Println("Match is not fine!", t, "and", dat.getCheck(t), "vs", tu) |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 942 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 943 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 944 | if !ok && a == dat.identity { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 945 | // Try again with unknown symbol, in case identity failed |
| 946 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 947 | fmt.Println("UNKNOWN symbol", string(chars[i]), "->", dat.unknown) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 948 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 949 | a = dat.unknown |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 950 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 951 | } else if a != dat.epsilon { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 952 | // Try again with epsilon symbol, in case everything else failed |
| 953 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 954 | fmt.Println("EPSILON symbol", string(chars[i]), "->", dat.epsilon) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 955 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 956 | a = dat.epsilon |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 957 | } else { |
| 958 | break |
| 959 | } |
| 960 | goto CHECK |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 961 | } else if dat.isSeparate(t) { |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 962 | // Move to representative state |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 963 | t = dat.getBase(t) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 964 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 965 | |
| 966 | // Transition is fine |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 967 | if a != dat.epsilon { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 968 | // Character consumed |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 969 | i++ |
| 970 | } |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 971 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 972 | // TODO: |
| 973 | // Prevent endless epsilon loops! |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 974 | } |
| 975 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 976 | if i != len(chars) { |
| 977 | if DEBUG { |
| 978 | fmt.Println("Not at the end") |
| 979 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 980 | return false |
| 981 | } |
| 982 | |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 983 | FINALCHECK: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 984 | |
| 985 | // Automaton is in a final state |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 986 | if dat.getCheck(dat.getBase(t)+uint32(dat.final)) == t { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 987 | return true |
| 988 | } |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 989 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 990 | // Check epsilon transitions until a final state is reached |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 991 | tu = t |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 992 | t = dat.getBase(tu) + uint32(dat.epsilon) |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 993 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 994 | // Epsilon transition failed |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 995 | if t > dat.getCheck(1) || dat.getCheck(t) != tu { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 996 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 997 | fmt.Println("Match is not fine!", t, "and", dat.getCheck(t), "vs", tu) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 998 | } |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 999 | return false |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 1000 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 1001 | } else if dat.isSeparate(t) { |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 1002 | // Move to representative state |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 1003 | t = dat.getBase(t) |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 1004 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 1005 | |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 1006 | goto FINALCHECK |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 1007 | } |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1008 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1009 | // Transduce an input string against the double array |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1010 | // FSA. |
| 1011 | // |
| 1012 | // Based on Match with additional support |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1013 | // for NONTOKEN and TOKENEND handling |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1014 | func (dat *DaTokenizer) Transduce(r io.Reader, w io.Writer) bool { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1015 | var a int |
| 1016 | var tu uint32 |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1017 | var ok, nontoken, tokenend bool |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1018 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1019 | reader := bufio.NewReader(r) |
| 1020 | writer := bufio.NewWriter(w) |
| 1021 | defer writer.Flush() |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1022 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1023 | t := uint32(1) // Initial state |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1024 | skip := false |
| 1025 | |
| 1026 | var char rune |
| 1027 | var err error |
| 1028 | eof := false |
| 1029 | |
| 1030 | for { |
| 1031 | |
| 1032 | if !skip { |
| 1033 | char, _, err = reader.ReadRune() |
| 1034 | if err != nil { |
| 1035 | eof = true |
| 1036 | break |
| 1037 | } |
| 1038 | } |
| 1039 | skip = false |
| 1040 | |
| 1041 | a, ok = dat.sigma[char] |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1042 | |
| 1043 | // Support identity symbol if character is not in sigma |
| 1044 | if !ok && dat.identity != -1 { |
| 1045 | if DEBUG { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1046 | fmt.Println("IDENTITY symbol", string(char), "->", dat.identity) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1047 | } |
| 1048 | a = dat.identity |
| 1049 | } else if DEBUG { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1050 | fmt.Println("Sigma transition is okay for [", string(char), "]") |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1051 | } |
| 1052 | tu = t |
| 1053 | CHECK: |
| 1054 | nontoken = false |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1055 | tokenend = false |
| 1056 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1057 | t = dat.getBase(tu) + uint32(a) |
| 1058 | |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame^] | 1059 | if DEBUG { |
| 1060 | fmt.Println("Check", tu, "-", a, "(", string(char), ")", "->", t) |
| 1061 | } |
| 1062 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1063 | // Check if the transition is valid according to the double array |
| 1064 | if t > dat.getCheck(1) || dat.getCheck(t) != tu { |
| 1065 | |
| 1066 | if DEBUG { |
| 1067 | fmt.Println("Match is not fine!", t, "and", dat.getCheck(t), "vs", tu) |
| 1068 | } |
| 1069 | |
| 1070 | if !ok && a == dat.identity { |
| 1071 | // Try again with unknown symbol, in case identity failed |
| 1072 | if DEBUG { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1073 | fmt.Println("UNKNOWN symbol", string(char), "->", dat.unknown) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1074 | } |
| 1075 | a = dat.unknown |
| 1076 | |
| 1077 | } else if a != dat.epsilon { |
| 1078 | // Try again with epsilon symbol, in case everything else failed |
| 1079 | if DEBUG { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1080 | fmt.Println("EPSILON symbol", string(char), "->", dat.epsilon) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1081 | } |
| 1082 | a = dat.epsilon |
| 1083 | } else { |
| 1084 | break |
| 1085 | } |
| 1086 | goto CHECK |
| 1087 | } else if dat.isSeparate(t) { |
| 1088 | // Move to representative state |
| 1089 | nontoken = dat.isNonToken(t) |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1090 | tokenend = dat.isTokenEnd(t) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1091 | |
| 1092 | t = dat.getBase(t) |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1093 | |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame^] | 1094 | if DEBUG { |
| 1095 | fmt.Println("Representative pointing to", t) |
| 1096 | } |
| 1097 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1098 | } else { |
| 1099 | nontoken = dat.isNonToken(t) |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1100 | tokenend = dat.isTokenEnd(t) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | // Transition is fine |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1104 | if a == dat.epsilon { |
| 1105 | skip = true |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1106 | // Character consumed |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1107 | } else if !nontoken { |
| 1108 | writer.WriteRune(char) |
| 1109 | } |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1110 | |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame^] | 1111 | if DEBUG { |
| 1112 | fmt.Println(" --> ok!") |
| 1113 | } |
| 1114 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1115 | /* |
| 1116 | if nontoken { |
| 1117 | writer.WriteRune(("<|>") |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1118 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1119 | */ |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1120 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1121 | if tokenend { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1122 | writer.WriteRune('\n') |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1123 | } |
| 1124 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1125 | // TODO: |
| 1126 | // Prevent endless epsilon loops! |
| 1127 | } |
| 1128 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1129 | if !eof { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1130 | if DEBUG { |
| 1131 | fmt.Println("Not at the end") |
| 1132 | } |
| 1133 | return false |
| 1134 | } |
| 1135 | |
| 1136 | FINALCHECK: |
| 1137 | |
| 1138 | // Automaton is in a final state |
| 1139 | if dat.getCheck(dat.getBase(t)+uint32(dat.final)) == t { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1140 | /* |
| 1141 | if dat.isNonToken(t) { |
| 1142 | fmt.Print("<|>") |
| 1143 | } |
| 1144 | */ |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1145 | if dat.isTokenEnd(t) { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1146 | writer.WriteRune('\n') |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | // There may be a new line at the end, from an epsilon, so we go on! |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1150 | return true |
| 1151 | } |
| 1152 | |
| 1153 | // Check epsilon transitions until a final state is reached |
| 1154 | tu = t |
| 1155 | t = dat.getBase(tu) + uint32(dat.epsilon) |
| 1156 | |
| 1157 | // Epsilon transition failed |
| 1158 | if t > dat.getCheck(1) || dat.getCheck(t) != tu { |
| 1159 | if DEBUG { |
| 1160 | fmt.Println("Match is not fine!", t, "and", dat.getCheck(t), "vs", tu) |
| 1161 | } |
| 1162 | return false |
| 1163 | |
| 1164 | } else if dat.isSeparate(t) { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1165 | // nontoken = dat.isNonToken(t) |
| 1166 | tokenend = dat.isTokenEnd(t) |
| 1167 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1168 | // Move to representative state |
| 1169 | t = dat.getBase(t) |
| 1170 | } else { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1171 | tokenend = dat.isTokenEnd(t) |
| 1172 | // nontoken = dat.isNonToken(t) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1173 | } |
| 1174 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1175 | /* |
| 1176 | if nontoken { |
| 1177 | fmt.Print("<|>") |
| 1178 | } |
| 1179 | */ |
| 1180 | |
| 1181 | if tokenend { |
| 1182 | writer.WriteRune('\n') |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | goto FINALCHECK |
| 1186 | } |