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 | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 287 | if inSym != outSym { |
| 288 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 289 | if tok.sigmaRev[outSym] == NEWLINE { |
| 290 | tokenend = true |
| 291 | } else if outSym == tok.epsilon { |
| 292 | nontoken = true |
| 293 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 294 | log.Error().Msg( |
| 295 | "Unsupported transition: " + |
| 296 | strconv.Itoa(state) + |
| 297 | " -> " + strconv.Itoa(end) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 298 | " (" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 299 | strconv.Itoa(inSym) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 300 | ":" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 301 | strconv.Itoa(outSym) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 302 | ") (" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 303 | string(tok.sigmaRev[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 | string(tok.sigmaRev[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 | os.Exit(1) |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 308 | } |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 309 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 310 | } else if inSym == tok.epsilon { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 311 | log.Error().Msg("Epsilon transitions not supported") |
| 312 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 315 | // This collects all edges until arrstate changes |
| 316 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 317 | // TODO: |
| 318 | // if arrin == EPSILON && arrout == TOKENEND, mark state as newline |
| 319 | // if the next transition is the same, remove TOKENEND and add SENTENCEEND |
| 320 | // This requires to remove the transition alltogether and marks the state instead. |
| 321 | |
| 322 | // TODO: |
| 323 | // if arrout == EPSILON, mark the transition as NOTOKEN |
| 324 | |
| 325 | targetObj := &edge{ |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 326 | inSym: inSym, |
| 327 | outSym: outSym, |
| 328 | end: end + 1, |
| 329 | tokenend: tokenend, |
| 330 | nontoken: nontoken, |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 331 | } |
| 332 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 333 | // Initialize outgoing states |
| 334 | if tok.transitions[state+1] == nil { |
| 335 | tok.transitions[state+1] = make(map[int]*edge) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 336 | } |
| 337 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 338 | // Ignore transitions with invalid symbols |
| 339 | if inSym >= 0 { |
| 340 | tok.transitions[state+1][inSym] = targetObj |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 341 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 342 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 343 | // Add final transition |
| 344 | if final == 1 { |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 345 | tok.transitions[state+1][tok.final] = &edge{} |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 346 | } |
| 347 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 348 | if DEBUG { |
| 349 | fmt.Println("Add", |
| 350 | state+1, "->", end+1, |
| 351 | "(", |
| 352 | inSym, |
| 353 | ":", |
| 354 | outSym, |
| 355 | ") (", |
| 356 | string(tok.sigmaRev[inSym]), |
| 357 | ":", |
| 358 | string(tok.sigmaRev[outSym]), |
| 359 | ")") |
| 360 | } |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 361 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 362 | continue |
| 363 | } |
| 364 | case SIGMA: |
| 365 | { |
| 366 | elem = strings.SplitN(line[0:len(line)-1], " ", 2) |
| 367 | |
| 368 | // Turn string into sigma id |
| 369 | number, err := strconv.Atoi(elem[0]) |
| 370 | |
| 371 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 372 | log.Error().Err(err) |
| 373 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 374 | } |
| 375 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 376 | tok.sigmaCount = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 377 | |
| 378 | var symbol rune |
| 379 | |
| 380 | // Read rune |
| 381 | if utf8.RuneCountInString(elem[1]) == 1 { |
| 382 | symbol = []rune(elem[1])[0] |
| 383 | |
| 384 | // Probably a MCS |
| 385 | } else if utf8.RuneCountInString(elem[1]) > 1 { |
| 386 | switch elem[1] { |
| 387 | case "@_EPSILON_SYMBOL_@": |
| 388 | { |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 389 | tok.epsilon = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 390 | continue |
| 391 | } |
| 392 | case "@_UNKNOWN_SYMBOL_@": |
| 393 | { |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 394 | tok.unknown = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 395 | continue |
| 396 | } |
| 397 | |
| 398 | case "@_IDENTITY_SYMBOL_@": |
| 399 | { |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 400 | tok.identity = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 401 | continue |
| 402 | } |
| 403 | default: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 404 | { |
| 405 | log.Error().Msg("MCS not supported: " + line) |
| 406 | os.Exit(1) |
| 407 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 408 | } |
| 409 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 410 | } else { // Probably a new line symbol |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 411 | line, err = r.ReadString('\n') |
| 412 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 413 | log.Error().Err(err) |
| 414 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 415 | } |
| 416 | if len(line) != 1 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 417 | log.Error().Msg("MCS not supported:" + line) |
| 418 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 419 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 420 | symbol = rune(NEWLINE) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 421 | } |
| 422 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 423 | tok.sigmaRev[number] = symbol |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | return tok |
| 429 | } |
| 430 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 431 | // Set alphabet A to the list of all symbols |
| 432 | // outgoing from s |
| 433 | func (tok *Tokenizer) get_set(s int, A *[]int) { |
| 434 | for a := range tok.transitions[s] { |
| 435 | *A = append(*A, a) |
| 436 | } |
| 437 | |
| 438 | // Not required, but simplifies bug hunting |
| 439 | sort.Ints(*A) |
| 440 | } |
| 441 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 442 | // Implementation of Mizobuchi et al (2000), p.128 |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 443 | func (tok *Tokenizer) ToDoubleArray() *DaTokenizer { |
| 444 | |
| 445 | dat := &DaTokenizer{ |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 446 | sigma: make(map[rune]int), |
| 447 | loadFactor: -1, |
| 448 | final: tok.final, |
| 449 | unknown: tok.unknown, |
| 450 | identity: tok.identity, |
| 451 | epsilon: tok.epsilon, |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 452 | // lastFilledBase: 1, |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | for num, sym := range tok.sigmaRev { |
| 456 | dat.sigma[sym] = num |
| 457 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 458 | |
| 459 | mark := 0 |
| 460 | size := 0 |
| 461 | |
| 462 | // Create a mapping from s to t |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 463 | table := make([]*mapping, tok.arcCount+1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 464 | |
| 465 | table[size] = &mapping{source: 1, target: 1} |
| 466 | size++ |
| 467 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 468 | // Allocate space for the outgoing symbol range |
| 469 | A := make([]int, 0, tok.sigmaCount) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 470 | |
| 471 | for mark < size { |
| 472 | s := table[mark].source // This is a state in Ms |
| 473 | t := table[mark].target // This is a state in Mt |
| 474 | mark++ |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 475 | |
| 476 | // Following the paper, here the state t can be remembered |
| 477 | // in the set of states St |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 478 | A = A[:0] |
| 479 | tok.get_set(s, &A) |
| 480 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 481 | // Set base to the first free slot in the double array |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 482 | dat.setBase(t, dat.xCheck(A)) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 483 | |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 484 | // TODO: |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 485 | // Sort the outgoing transitions based on the |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 486 | // outdegree of .end |
| 487 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 488 | // Iterate over all outgoing symbols |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 489 | for _, a := range A { |
| 490 | |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 491 | if a != tok.final { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 492 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 493 | // Aka g(s, a) |
| 494 | s1 := tok.transitions[s][a].end |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 495 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 496 | // Store the transition |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 497 | t1 := dat.getBase(t) + uint32(a) |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 498 | dat.setCheck(t1, t) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 499 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 500 | // Mark the state as being the target of a nontoken transition |
| 501 | if tok.transitions[s][a].nontoken { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 502 | dat.setNonToken(t1, true) |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 503 | } |
| 504 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 505 | // Mark the state as being the target of a tokenend transition |
| 506 | if tok.transitions[s][a].tokenend { |
| 507 | dat.setTokenEnd(t1, true) |
| 508 | } |
| 509 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 510 | // Check for representative states |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 511 | r := in_table(s1, table, size) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 512 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 513 | if r == 0 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 514 | // Remember the mapping |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 515 | table[size] = &mapping{source: s1, target: t1} |
| 516 | size++ |
| 517 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 518 | // Overwrite with the representative state |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 519 | dat.setBase(t1, r) |
| 520 | dat.setSeparate(t1, true) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 521 | } |
| 522 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 523 | // Store a final transition |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 524 | dat.setCheck(dat.getBase(t)+uint32(dat.final), t) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | // Following Mizobuchi et al (2000) the size of the |
| 530 | // FSA should be stored in check(1). |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 531 | dat.setSize(dat.maxSize + 1) |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 532 | dat.array = dat.array[:dat.maxSize+1] |
| 533 | return dat |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 534 | } |
| 535 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 536 | // Check the table if a mapping of s |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 537 | // exists and return this as a representative. |
| 538 | // Currently iterates through the whole table |
| 539 | // in a bruteforce manner. |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 540 | func in_table(s int, table []*mapping, size int) uint32 { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 541 | for x := 0; x < size; x++ { |
| 542 | if table[x].source == s { |
| 543 | return table[x].target |
| 544 | } |
| 545 | } |
| 546 | return 0 |
| 547 | } |
| 548 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 549 | // Resize double array when necessary |
| 550 | func (dat *DaTokenizer) resize(l int) { |
| 551 | // TODO: |
| 552 | // This is a bit too aggressive atm and should be calmed down. |
| 553 | if len(dat.array) <= l { |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 554 | dat.array = append(dat.array, make([]uint32, l)...) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 555 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 556 | } |
Akron | c9d84a6 | 2021-08-03 15:56:03 +0200 | [diff] [blame] | 557 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 558 | // Set base value in double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 559 | func (dat *DaTokenizer) setBase(p uint32, v uint32) { |
| 560 | l := int(p*2 + 1) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 561 | dat.resize(l) |
| 562 | if dat.maxSize < l { |
| 563 | dat.maxSize = l |
| 564 | } |
| 565 | dat.array[p*2] = v |
| 566 | } |
| 567 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 568 | // Returns true if a state is separate pointing to a representative |
| 569 | func (dat *DaTokenizer) isSeparate(p uint32) bool { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 570 | return dat.array[p*2]&firstBit != 0 |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | // Mark a state as separate pointing to a representative |
| 574 | func (dat *DaTokenizer) setSeparate(p uint32, sep bool) { |
| 575 | if sep { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 576 | dat.array[p*2] |= firstBit |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 577 | } else { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 578 | dat.array[p*2] &= (restBit | secondBit) |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 582 | // Returns true if a state is the target of a nontoken transition |
| 583 | func (dat *DaTokenizer) isNonToken(p uint32) bool { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 584 | return dat.array[p*2+1]&firstBit != 0 |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | // Mark a state as being the target of a nontoken transition |
| 588 | func (dat *DaTokenizer) setNonToken(p uint32, sep bool) { |
| 589 | if sep { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 590 | dat.array[p*2+1] |= firstBit |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 591 | } else { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 592 | dat.array[p*2+1] &= (restBit | secondBit) |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 596 | // Returns true if a state is the target of a tokenend transition |
| 597 | func (dat *DaTokenizer) isTokenEnd(p uint32) bool { |
| 598 | return dat.array[p*2+1]&secondBit != 0 |
| 599 | } |
| 600 | |
| 601 | // Mark a state as being the target of a tokenend transition |
| 602 | func (dat *DaTokenizer) setTokenEnd(p uint32, sep bool) { |
| 603 | if sep { |
| 604 | dat.array[p*2+1] |= secondBit |
| 605 | } else { |
| 606 | dat.array[p*2+1] &= (restBit | firstBit) |
| 607 | } |
| 608 | } |
| 609 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 610 | // Get base value in double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 611 | func (dat *DaTokenizer) getBase(p uint32) uint32 { |
| 612 | if int(p*2) >= len(dat.array) { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 613 | return 0 |
| 614 | } |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 615 | return dat.array[p*2] & restBit |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | // Set check value in double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 619 | func (dat *DaTokenizer) setCheck(p uint32, v uint32) { |
| 620 | l := int(p*2 + 1) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 621 | dat.resize(l) |
| 622 | if dat.maxSize < l { |
| 623 | dat.maxSize = l |
| 624 | } |
| 625 | dat.array[(p*2)+1] = v |
| 626 | } |
| 627 | |
| 628 | // Get check value in double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 629 | func (dat *DaTokenizer) getCheck(p uint32) uint32 { |
| 630 | if int((p*2)+1) >= len(dat.array) { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 631 | return 0 |
| 632 | } |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 633 | return dat.array[(p*2)+1] & restBit |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | // Set size of double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 637 | func (dat *DaTokenizer) setSize(v int) { |
| 638 | dat.setCheck(1, uint32(v)) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | // Get size of double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 642 | func (dat *DaTokenizer) GetSize() int { |
| 643 | return int(dat.getCheck(1)) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | // Based on Mizobuchi et al (2000), p. 124 |
| 647 | // This iterates for every state through the complete double array |
| 648 | // structure until it finds a gap that fits all outgoing transitions |
| 649 | // of the state. This is extremely slow, but is only necessary in the |
| 650 | // construction phase of the tokenizer. |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 651 | func (dat *DaTokenizer) xCheck(symbols []int) uint32 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 652 | |
| 653 | // Start at the first entry of the double array list |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 654 | base := uint32(1) // dat.lastFilledBase |
| 655 | // skip := false |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 656 | OVERLAP: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 657 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 658 | /* |
| 659 | if !skip { |
| 660 | if dat.getCheck(base) != 0 { |
| 661 | dat.lastFilledBase = base |
| 662 | } else { |
| 663 | skip = true |
| 664 | } |
| 665 | } |
| 666 | */ |
| 667 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 668 | // Resize the array if necessary |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 669 | dat.resize((int(base) + dat.final) * 2) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 670 | for _, a := range symbols { |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 671 | if dat.getCheck(base+uint32(a)) != 0 { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 672 | base++ |
| 673 | goto OVERLAP |
| 674 | } |
| 675 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 676 | return base |
| 677 | } |
| 678 | |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 679 | // LoadFactor as defined in Kanda et al (2018), |
| 680 | // i.e. the proportion of non-empty elements to all elements. |
| 681 | func (dat *DaTokenizer) LoadFactor() float64 { |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 682 | |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 683 | // Cache the loadfactor |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 684 | if dat.loadFactor > 0 { |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 685 | return dat.loadFactor |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 686 | } |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 687 | nonEmpty := 0 |
| 688 | all := len(dat.array) / 2 |
| 689 | for x := 1; x <= len(dat.array); x = x + 2 { |
| 690 | if dat.array[x] != 0 { |
| 691 | nonEmpty++ |
| 692 | } |
| 693 | } |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 694 | dat.loadFactor = float64(nonEmpty) / float64(all) * 100 |
| 695 | return dat.loadFactor |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 696 | } |
| 697 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 698 | // WriteTo stores the double array data in an io.Writer. |
| 699 | func (dat *DaTokenizer) WriteTo(w io.Writer) (n int64, err error) { |
| 700 | |
| 701 | // Store magical header |
| 702 | all, err := w.Write([]byte(MAGIC)) |
| 703 | if err != nil { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 704 | log.Error().Err(err) |
| 705 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | // Get sigma as a list |
| 709 | sigmalist := make([]rune, len(dat.sigma)+16) |
| 710 | max := 0 |
| 711 | for sym, num := range dat.sigma { |
| 712 | sigmalist[num] = sym |
| 713 | if num > max { |
| 714 | max = num |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | sigmalist = sigmalist[:max+1] |
| 719 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 720 | buf := make([]byte, 0, 16) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 721 | bo.PutUint16(buf[0:2], VERSION) |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 722 | bo.PutUint16(buf[2:4], uint16(dat.epsilon)) |
| 723 | bo.PutUint16(buf[4:6], uint16(dat.unknown)) |
| 724 | bo.PutUint16(buf[6:8], uint16(dat.identity)) |
| 725 | bo.PutUint16(buf[8:10], uint16(dat.final)) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 726 | bo.PutUint16(buf[10:12], uint16(len(sigmalist))) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 727 | bo.PutUint32(buf[12:16], uint32(len(dat.array))) |
| 728 | more, err := w.Write(buf[0:16]) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 729 | if err != nil { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 730 | log.Error().Err(err) |
| 731 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | all += more |
| 735 | |
| 736 | wbuf := bytes.NewBuffer(nil) |
| 737 | wbufWrap := bufio.NewWriter(wbuf) |
| 738 | |
| 739 | // Write sigma |
| 740 | for _, sym := range sigmalist { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 741 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 742 | more, err = wbufWrap.WriteRune(sym) |
| 743 | if err != nil { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 744 | log.Error().Err(err) |
| 745 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 746 | } |
| 747 | all += more |
| 748 | } |
| 749 | wbufWrap.Flush() |
| 750 | more, err = w.Write(wbuf.Bytes()) |
| 751 | if err != nil { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 752 | log.Error().Err(err) |
| 753 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 754 | } |
| 755 | all += more |
| 756 | |
| 757 | // Test marker - could be checksum |
| 758 | more, err = w.Write([]byte("T")) |
| 759 | if err != nil { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 760 | log.Error().Err(err) |
| 761 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 762 | } |
| 763 | all += more |
| 764 | |
| 765 | wbuf.Reset() |
| 766 | |
| 767 | for _, d := range dat.array { |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 768 | bo.PutUint32(buf[0:4], d) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 769 | more, err := w.Write(buf[0:4]) |
| 770 | if err != nil { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 771 | log.Error().Err(err) |
| 772 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 773 | } |
| 774 | all += more |
| 775 | } |
| 776 | |
| 777 | return int64(all), err |
| 778 | } |
| 779 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 780 | func LoadDatokFile(file string) *DaTokenizer { |
| 781 | f, err := os.Open(file) |
| 782 | if err != nil { |
| 783 | log.Error().Err(err) |
| 784 | os.Exit(0) |
| 785 | } |
| 786 | defer f.Close() |
| 787 | |
| 788 | gz, err := gzip.NewReader(f) |
| 789 | if err != nil { |
| 790 | log.Error().Err(err) |
| 791 | os.Exit(0) |
| 792 | } |
| 793 | defer gz.Close() |
| 794 | |
| 795 | return ParseDatok(gz) |
| 796 | } |
| 797 | |
| 798 | func ParseDatok(ior io.Reader) *DaTokenizer { |
| 799 | |
| 800 | dat := &DaTokenizer{ |
| 801 | sigma: make(map[rune]int), |
| 802 | epsilon: 0, |
| 803 | unknown: 0, |
| 804 | identity: 0, |
| 805 | final: 0, |
| 806 | loadFactor: 0, |
| 807 | } |
| 808 | |
| 809 | r := bufio.NewReader(ior) |
| 810 | |
| 811 | all := 0 |
| 812 | |
| 813 | buf := make([]byte, 1024) |
| 814 | buf = buf[0:len(MAGIC)] |
| 815 | |
| 816 | more, err := r.Read(buf) |
| 817 | |
| 818 | if err != nil { |
| 819 | log.Error().Err(err) |
| 820 | return nil |
| 821 | } |
| 822 | |
| 823 | all += more |
| 824 | |
| 825 | if string(MAGIC) != string(buf) { |
| 826 | log.Error().Msg("Not a datok file") |
| 827 | return nil |
| 828 | } |
| 829 | |
| 830 | more, err = r.Read(buf[0:16]) |
| 831 | if err != nil { |
| 832 | log.Error().Err(err) |
| 833 | return nil |
| 834 | } |
| 835 | |
| 836 | all += more |
| 837 | |
| 838 | // version := bo.Uint16(buf[0:2]) |
| 839 | dat.epsilon = int(bo.Uint16(buf[2:4])) |
| 840 | dat.unknown = int(bo.Uint16(buf[4:6])) |
| 841 | dat.identity = int(bo.Uint16(buf[6:8])) |
| 842 | dat.final = int(bo.Uint16(buf[8:10])) |
| 843 | |
| 844 | sigmaCount := int(bo.Uint16(buf[10:12])) |
| 845 | arraySize := int(bo.Uint32(buf[12:16])) |
| 846 | |
| 847 | for x := 0; x < sigmaCount; x++ { |
| 848 | sym, more, err := r.ReadRune() |
| 849 | if err == nil && sym != 0 { |
| 850 | dat.sigma[sym] = x |
| 851 | } |
| 852 | all += more |
| 853 | } |
| 854 | |
| 855 | more, err = r.Read(buf[0:1]) |
| 856 | |
| 857 | if err != nil { |
| 858 | log.Error().Err(err) |
| 859 | return nil |
| 860 | } |
| 861 | |
| 862 | all += more |
| 863 | |
| 864 | if string("T") != string(buf[0:1]) { |
| 865 | log.Error().Msg("Not a datok file") |
| 866 | return nil |
| 867 | } |
| 868 | |
| 869 | // Read based on length |
| 870 | dat.array = make([]uint32, arraySize) |
| 871 | |
| 872 | for x := 0; x < arraySize; x++ { |
| 873 | more, err = r.Read(buf[0:4]) |
| 874 | if err != nil { |
| 875 | log.Error().Err(err) |
| 876 | return nil |
| 877 | } |
| 878 | all += more |
| 879 | dat.array[x] = bo.Uint32(buf[0:4]) |
| 880 | } |
| 881 | |
| 882 | return dat |
| 883 | } |
| 884 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 885 | // Match an input string against the double array |
| 886 | // FSA. |
| 887 | // |
| 888 | // Based on Mizobuchi et al (2000), p. 129, |
| 889 | // with additional support for IDENTITY, UNKNOWN |
| 890 | // and EPSILON transitions. |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 891 | func (dat *DaTokenizer) Match(input string) bool { |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 892 | var a int |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 893 | var tu uint32 |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 894 | var ok bool |
| 895 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 896 | t := uint32(1) // Initial state |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 897 | chars := []rune(input) |
| 898 | i := 0 |
| 899 | |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 900 | for i < len(chars) { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 901 | a, ok = dat.sigma[chars[i]] |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 902 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 903 | // Support identity symbol if character is not in sigma |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 904 | if !ok && dat.identity != -1 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 905 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 906 | fmt.Println("IDENTITY symbol", string(chars[i]), "->", dat.identity) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 907 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 908 | a = dat.identity |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 909 | } else if DEBUG { |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 910 | fmt.Println("Sigma transition is okay for [", string(chars[i]), "]") |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 911 | } |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 912 | tu = t |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 913 | CHECK: |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 914 | t = dat.getBase(tu) + uint32(a) |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 915 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 916 | // Check if the transition is valid according to the double array |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 917 | if t > dat.getCheck(1) || dat.getCheck(t) != tu { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 918 | |
| 919 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 920 | fmt.Println("Match is not fine!", t, "and", dat.getCheck(t), "vs", tu) |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 921 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 922 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 923 | if !ok && a == dat.identity { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 924 | // Try again with unknown symbol, in case identity failed |
| 925 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 926 | fmt.Println("UNKNOWN symbol", string(chars[i]), "->", dat.unknown) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 927 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 928 | a = dat.unknown |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 929 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 930 | } else if a != dat.epsilon { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 931 | // Try again with epsilon symbol, in case everything else failed |
| 932 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 933 | fmt.Println("EPSILON symbol", string(chars[i]), "->", dat.epsilon) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 934 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 935 | a = dat.epsilon |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 936 | } else { |
| 937 | break |
| 938 | } |
| 939 | goto CHECK |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 940 | } else if dat.isSeparate(t) { |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 941 | // Move to representative state |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 942 | t = dat.getBase(t) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 943 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 944 | |
| 945 | // Transition is fine |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 946 | if a != dat.epsilon { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 947 | // Character consumed |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 948 | i++ |
| 949 | } |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 950 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 951 | // TODO: |
| 952 | // Prevent endless epsilon loops! |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 953 | } |
| 954 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 955 | if i != len(chars) { |
| 956 | if DEBUG { |
| 957 | fmt.Println("Not at the end") |
| 958 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 959 | return false |
| 960 | } |
| 961 | |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 962 | FINALCHECK: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 963 | |
| 964 | // Automaton is in a final state |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 965 | if dat.getCheck(dat.getBase(t)+uint32(dat.final)) == t { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 966 | return true |
| 967 | } |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 968 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 969 | // Check epsilon transitions until a final state is reached |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 970 | tu = t |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 971 | t = dat.getBase(tu) + uint32(dat.epsilon) |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 972 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 973 | // Epsilon transition failed |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 974 | if t > dat.getCheck(1) || dat.getCheck(t) != tu { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 975 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 976 | fmt.Println("Match is not fine!", t, "and", dat.getCheck(t), "vs", tu) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 977 | } |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 978 | return false |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 979 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 980 | } else if dat.isSeparate(t) { |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 981 | // Move to representative state |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 982 | t = dat.getBase(t) |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 983 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 984 | |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 985 | goto FINALCHECK |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 986 | } |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 987 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 988 | // Transduce an input string against the double array |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 989 | // FSA. |
| 990 | // |
| 991 | // Based on Match with additional support |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 992 | // for NONTOKEN and TOKENEND handling |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 993 | func (dat *DaTokenizer) Transduce(r io.Reader, w io.Writer) bool { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 994 | var a int |
| 995 | var tu uint32 |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 996 | var ok, nontoken, tokenend bool |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 997 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 998 | reader := bufio.NewReader(r) |
| 999 | writer := bufio.NewWriter(w) |
| 1000 | defer writer.Flush() |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1001 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 1002 | t := uint32(1) // Initial state |
| 1003 | // chars := []rune(input) |
| 1004 | skip := false |
| 1005 | |
| 1006 | var char rune |
| 1007 | var err error |
| 1008 | eof := false |
| 1009 | |
| 1010 | for { |
| 1011 | |
| 1012 | if !skip { |
| 1013 | char, _, err = reader.ReadRune() |
| 1014 | if err != nil { |
| 1015 | eof = true |
| 1016 | break |
| 1017 | } |
| 1018 | } |
| 1019 | skip = false |
| 1020 | |
| 1021 | a, ok = dat.sigma[char] |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1022 | |
| 1023 | // Support identity symbol if character is not in sigma |
| 1024 | if !ok && dat.identity != -1 { |
| 1025 | if DEBUG { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 1026 | fmt.Println("IDENTITY symbol", string(char), "->", dat.identity) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1027 | } |
| 1028 | a = dat.identity |
| 1029 | } else if DEBUG { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 1030 | fmt.Println("Sigma transition is okay for [", string(char), "]") |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1031 | } |
| 1032 | tu = t |
| 1033 | CHECK: |
| 1034 | nontoken = false |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1035 | tokenend = false |
| 1036 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1037 | t = dat.getBase(tu) + uint32(a) |
| 1038 | |
| 1039 | // Check if the transition is valid according to the double array |
| 1040 | if t > dat.getCheck(1) || dat.getCheck(t) != tu { |
| 1041 | |
| 1042 | if DEBUG { |
| 1043 | fmt.Println("Match is not fine!", t, "and", dat.getCheck(t), "vs", tu) |
| 1044 | } |
| 1045 | |
| 1046 | if !ok && a == dat.identity { |
| 1047 | // Try again with unknown symbol, in case identity failed |
| 1048 | if DEBUG { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 1049 | fmt.Println("UNKNOWN symbol", string(char), "->", dat.unknown) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1050 | } |
| 1051 | a = dat.unknown |
| 1052 | |
| 1053 | } else if a != dat.epsilon { |
| 1054 | // Try again with epsilon symbol, in case everything else failed |
| 1055 | if DEBUG { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 1056 | fmt.Println("EPSILON symbol", string(char), "->", dat.epsilon) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1057 | } |
| 1058 | a = dat.epsilon |
| 1059 | } else { |
| 1060 | break |
| 1061 | } |
| 1062 | goto CHECK |
| 1063 | } else if dat.isSeparate(t) { |
| 1064 | // Move to representative state |
| 1065 | nontoken = dat.isNonToken(t) |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1066 | tokenend = dat.isTokenEnd(t) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1067 | |
| 1068 | t = dat.getBase(t) |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1069 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1070 | } else { |
| 1071 | nontoken = dat.isNonToken(t) |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1072 | tokenend = dat.isTokenEnd(t) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | // Transition is fine |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 1076 | if a == dat.epsilon { |
| 1077 | skip = true |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1078 | // Character consumed |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 1079 | } else if !nontoken { |
| 1080 | writer.WriteRune(char) |
| 1081 | } |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1082 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 1083 | /* |
| 1084 | if nontoken { |
| 1085 | writer.WriteRune(("<|>") |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1086 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 1087 | */ |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1088 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1089 | if tokenend { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 1090 | writer.WriteRune('\n') |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1091 | } |
| 1092 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1093 | // TODO: |
| 1094 | // Prevent endless epsilon loops! |
| 1095 | } |
| 1096 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 1097 | if !eof { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1098 | if DEBUG { |
| 1099 | fmt.Println("Not at the end") |
| 1100 | } |
| 1101 | return false |
| 1102 | } |
| 1103 | |
| 1104 | FINALCHECK: |
| 1105 | |
| 1106 | // Automaton is in a final state |
| 1107 | if dat.getCheck(dat.getBase(t)+uint32(dat.final)) == t { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 1108 | /* |
| 1109 | if dat.isNonToken(t) { |
| 1110 | fmt.Print("<|>") |
| 1111 | } |
| 1112 | */ |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1113 | if dat.isTokenEnd(t) { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 1114 | writer.WriteRune('\n') |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | // 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] | 1118 | return true |
| 1119 | } |
| 1120 | |
| 1121 | // Check epsilon transitions until a final state is reached |
| 1122 | tu = t |
| 1123 | t = dat.getBase(tu) + uint32(dat.epsilon) |
| 1124 | |
| 1125 | // Epsilon transition failed |
| 1126 | if t > dat.getCheck(1) || dat.getCheck(t) != tu { |
| 1127 | if DEBUG { |
| 1128 | fmt.Println("Match is not fine!", t, "and", dat.getCheck(t), "vs", tu) |
| 1129 | } |
| 1130 | return false |
| 1131 | |
| 1132 | } else if dat.isSeparate(t) { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 1133 | // nontoken = dat.isNonToken(t) |
| 1134 | tokenend = dat.isTokenEnd(t) |
| 1135 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1136 | // Move to representative state |
| 1137 | t = dat.getBase(t) |
| 1138 | } else { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 1139 | tokenend = dat.isTokenEnd(t) |
| 1140 | // nontoken = dat.isNonToken(t) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1141 | } |
| 1142 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame^] | 1143 | /* |
| 1144 | if nontoken { |
| 1145 | fmt.Print("<|>") |
| 1146 | } |
| 1147 | */ |
| 1148 | |
| 1149 | if tokenend { |
| 1150 | writer.WriteRune('\n') |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | goto FINALCHECK |
| 1154 | } |