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