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