Akron | 7f1097f | 2021-09-21 16:00:29 +0200 | [diff] [blame] | 1 | package datok |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 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 |
Akron | 7b1faa6 | 2021-09-02 16:10:21 +0200 | [diff] [blame] | 18 | // - Check if final states can be optional. |
| 19 | // - Introduce ELM (Morita et al. 2001) to speed |
| 20 | // up construction. Can be ignored on serialization |
| 21 | // to improve compression. |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 22 | // - Add checksum to serialization. |
Akron | e61380b | 2021-08-16 10:10:46 +0200 | [diff] [blame] | 23 | // - Replace/Enhance table with a map |
| 24 | // - Provide a bufio.Scanner compatible interface. |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 25 | // - Mark epsilon transitions in bytes |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 26 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 27 | import ( |
| 28 | "bufio" |
| 29 | "compress/gzip" |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 30 | "encoding/binary" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 31 | "fmt" |
| 32 | "io" |
Akron | 679b486 | 2021-09-02 16:59:26 +0200 | [diff] [blame] | 33 | "math" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 34 | "os" |
Akron | c9d84a6 | 2021-08-03 15:56:03 +0200 | [diff] [blame] | 35 | "sort" |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 36 | |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 37 | "log" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 38 | ) |
| 39 | |
| 40 | const ( |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 41 | DEBUG = false |
| 42 | MAGIC = "DATOK" |
| 43 | VERSION = uint16(1) |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 44 | FIRSTBIT uint32 = 1 << 31 |
| 45 | SECONDBIT uint32 = 1 << 30 |
| 46 | RESTBIT uint32 = ^uint32(0) &^ (FIRSTBIT | SECONDBIT) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 47 | ) |
| 48 | |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 49 | // Serialization is always little endian |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 50 | var bo binary.ByteOrder = binary.LittleEndian |
| 51 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 52 | type mapping struct { |
| 53 | source int |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 54 | target uint32 |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 57 | type bc struct { |
| 58 | base uint32 |
| 59 | check uint32 |
| 60 | } |
| 61 | |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 62 | // DaTokenizer represents a tokenizer implemented as a |
| 63 | // Double Array FSA. |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 64 | type DaTokenizer struct { |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 65 | sigma map[rune]int |
| 66 | sigmaASCII [256]int |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 67 | maxSize int |
Akron | 4fa28b3 | 2021-08-27 10:55:41 +0200 | [diff] [blame] | 68 | transCount int |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 69 | array []bc |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 70 | |
| 71 | // Special symbols in sigma |
| 72 | epsilon int |
| 73 | unknown int |
| 74 | identity int |
| 75 | final int |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 76 | tokenend int |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 77 | } |
| 78 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 79 | // ToDoubleArray turns the intermediate tokenizer representation |
| 80 | // into a double array representation. |
| 81 | // |
| 82 | // This is based on Mizobuchi et al (2000), p.128 |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame^] | 83 | func (auto *Automaton) ToDoubleArray() *DaTokenizer { |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 84 | |
| 85 | dat := &DaTokenizer{ |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 86 | sigma: make(map[rune]int), |
Akron | 4fa28b3 | 2021-08-27 10:55:41 +0200 | [diff] [blame] | 87 | transCount: -1, |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame^] | 88 | final: auto.final, |
| 89 | unknown: auto.unknown, |
| 90 | identity: auto.identity, |
| 91 | epsilon: auto.epsilon, |
| 92 | tokenend: auto.tokenend, |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 95 | dat.resize(dat.final) |
| 96 | |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame^] | 97 | for num, sym := range auto.sigmaRev { |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 98 | if int(sym) < 256 { |
| 99 | dat.sigmaASCII[int(sym)] = num |
| 100 | } |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 101 | dat.sigma[sym] = num |
| 102 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 103 | |
| 104 | mark := 0 |
| 105 | size := 0 |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame] | 106 | var base uint32 |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 107 | var atrans *edge |
| 108 | var s, s1 int |
| 109 | var t, t1 uint32 |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 110 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 111 | // Create a mapping from s (in Ms aka Intermediate FSA) |
| 112 | // to t (in Mt aka Double Array FSA) |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame^] | 113 | table := make([]*mapping, auto.arcCount+1) |
Akron | 7b1faa6 | 2021-09-02 16:10:21 +0200 | [diff] [blame] | 114 | // tableQueue := make([]int, tok.arcCount+1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 115 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 116 | // Initialize with the start state |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 117 | table[size] = &mapping{source: 1, target: 1} |
Akron | 7b1faa6 | 2021-09-02 16:10:21 +0200 | [diff] [blame] | 118 | // tableQueue[size] = 1 |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 119 | size++ |
| 120 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 121 | // Allocate space for the outgoing symbol range |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame^] | 122 | A := make([]int, 0, auto.sigmaCount) |
Akron | 7b1faa6 | 2021-09-02 16:10:21 +0200 | [diff] [blame] | 123 | // tableLookup := make([]uint32, tok.arcCount+2) // make(map[int]uint32) |
| 124 | // tableLookup[1] = 1 |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 125 | |
Akron | 7b1faa6 | 2021-09-02 16:10:21 +0200 | [diff] [blame] | 126 | // block_begin_pos := uint32(1) |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 127 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 128 | for mark < size { |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 129 | s = table[mark].source // This is a state in Ms |
| 130 | t = table[mark].target // This is a state in Mt |
Akron | 7b1faa6 | 2021-09-02 16:10:21 +0200 | [diff] [blame] | 131 | // s = tableQueue[mark] |
| 132 | // t = tableLookup[s] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 133 | mark++ |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 134 | |
| 135 | // Following the paper, here the state t can be remembered |
| 136 | // in the set of states St |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 137 | A = A[:0] |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame^] | 138 | auto.getSet(s, &A) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 139 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 140 | // Set base to the first free slot in the double array |
Akron | 29e306f | 2021-09-02 18:29:56 +0200 | [diff] [blame] | 141 | // base = dat.xCheck(A) |
Akron | 679b486 | 2021-09-02 16:59:26 +0200 | [diff] [blame] | 142 | // base = dat.xCheckSkip(A) |
Akron | 7b1faa6 | 2021-09-02 16:10:21 +0200 | [diff] [blame] | 143 | // base = dat.xCheckNiu(A, &block_begin_pos) |
Akron | 29e306f | 2021-09-02 18:29:56 +0200 | [diff] [blame] | 144 | base = dat.xCheckSkipNiu(A) |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame] | 145 | dat.array[t].setBase(base) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 146 | |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 147 | // TODO: |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 148 | // Sort the outgoing transitions based on the |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 149 | // outdegree of .end |
| 150 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 151 | // Iterate over all outgoing symbols |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 152 | for _, a := range A { |
| 153 | |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame^] | 154 | if a != auto.final { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 155 | |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame^] | 156 | atrans = auto.transitions[s][a] |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 157 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 158 | // Aka g(s, a) |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 159 | s1 = atrans.end |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 160 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 161 | // Store the transition |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 162 | t1 = base + uint32(a) |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 163 | dat.array[t1].setCheck(t) |
| 164 | |
| 165 | // Set maxSize |
| 166 | if dat.maxSize < int(t1) { |
| 167 | dat.maxSize = int(t1) |
| 168 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 169 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 170 | if DEBUG { |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 171 | fmt.Println("Translate transition", |
| 172 | s, "->", s1, "(", a, ")", "to", t, "->", t1) |
| 173 | } |
| 174 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 175 | // Mark the state as being the target of a nontoken transition |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 176 | if atrans.nontoken { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 177 | dat.array[t1].setNonToken(true) |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 178 | if DEBUG { |
| 179 | fmt.Println("Set", t1, "to nontoken") |
| 180 | } |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 183 | // Mark the state as being the target of a tokenend transition |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 184 | if atrans.tokenend { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 185 | dat.array[t1].setTokenEnd(true) |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 186 | if DEBUG { |
| 187 | fmt.Println("Set", t1, "to tokenend") |
| 188 | } |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 191 | // Check for representative states |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 192 | r := stateAlreadyInTable(s1, table, size) |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 193 | // r := tableLookup[s1] |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 194 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 195 | // No representative found |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 196 | if r == 0 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 197 | // Remember the mapping |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 198 | table[size] = &mapping{source: s1, target: t1} |
Akron | 7b1faa6 | 2021-09-02 16:10:21 +0200 | [diff] [blame] | 199 | // tableQueue[size] = s1 |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 200 | // tableLookup[s1] = t1 |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 201 | size++ |
| 202 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 203 | // Overwrite with the representative state |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 204 | dat.array[t1].setBase(r) |
| 205 | dat.array[t1].setSeparate(true) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 206 | } |
| 207 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 208 | // Store a final transition |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame] | 209 | dat.array[base+uint32(dat.final)].setCheck(t) |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 210 | |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 211 | if dat.maxSize < int(base)+dat.final { |
| 212 | dat.maxSize = int(base) + dat.final |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 213 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // Following Mizobuchi et al (2000) the size of the |
| 219 | // FSA should be stored in check(1). |
Akron | 29e306f | 2021-09-02 18:29:56 +0200 | [diff] [blame] | 220 | // We make the size a bit larger so we never have to check for boundaries. |
| 221 | dat.setSize(dat.maxSize + dat.final) |
| 222 | if len(dat.array) < dat.maxSize+dat.final { |
| 223 | dat.array = append(dat.array, make([]bc, dat.final)...) |
| 224 | } |
| 225 | dat.array = dat.array[:dat.maxSize+dat.final] |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 226 | return dat |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 229 | // Check the table if a mapping of s |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 230 | // exists and return this as a representative. |
| 231 | // Currently iterates through the whole table |
| 232 | // in a bruteforce manner. |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 233 | func stateAlreadyInTable(s int, table []*mapping, size int) uint32 { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 234 | for x := 0; x < size; x++ { |
| 235 | if table[x].source == s { |
| 236 | return table[x].target |
| 237 | } |
| 238 | } |
| 239 | return 0 |
| 240 | } |
| 241 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 242 | // Resize double array when necessary |
| 243 | func (dat *DaTokenizer) resize(l int) { |
| 244 | // TODO: |
| 245 | // This is a bit too aggressive atm and should be calmed down. |
| 246 | if len(dat.array) <= l { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 247 | dat.array = append(dat.array, make([]bc, l)...) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 248 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 249 | } |
Akron | c9d84a6 | 2021-08-03 15:56:03 +0200 | [diff] [blame] | 250 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 251 | // Set base value in double array |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 252 | func (bc *bc) setBase(v uint32) { |
| 253 | bc.base = v |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // Get base value in double array |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 257 | func (bc *bc) getBase() uint32 { |
| 258 | return bc.base & RESTBIT |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // Set check value in double array |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 262 | func (bc *bc) setCheck(v uint32) { |
| 263 | bc.check = v |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | // Get check value in double array |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 267 | func (bc *bc) getCheck() uint32 { |
| 268 | return bc.check & RESTBIT |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 269 | } |
| 270 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 271 | // Returns true if a state is separate pointing to a representative |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 272 | func (bc *bc) isSeparate() bool { |
| 273 | return bc.base&FIRSTBIT != 0 |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | // Mark a state as separate pointing to a representative |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 277 | func (bc *bc) setSeparate(sep bool) { |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 278 | if sep { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 279 | bc.base |= FIRSTBIT |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 280 | } else { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 281 | bc.base &= (RESTBIT | SECONDBIT) |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 285 | // Returns true if a state is the target of a nontoken transition |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 286 | func (bc *bc) isNonToken() bool { |
| 287 | return bc.check&FIRSTBIT != 0 |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | // Mark a state as being the target of a nontoken transition |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 291 | func (bc *bc) setNonToken(sep bool) { |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 292 | if sep { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 293 | bc.check |= FIRSTBIT |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 294 | } else { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 295 | bc.check &= (RESTBIT | SECONDBIT) |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 299 | // Returns true if a state is the target of a tokenend transition |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 300 | func (bc *bc) isTokenEnd() bool { |
| 301 | return bc.check&SECONDBIT != 0 |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | // Mark a state as being the target of a tokenend transition |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 305 | func (bc *bc) setTokenEnd(sep bool) { |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 306 | if sep { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 307 | bc.check |= SECONDBIT |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 308 | } else { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 309 | bc.check &= (RESTBIT | FIRSTBIT) |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 313 | // Set size of double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 314 | func (dat *DaTokenizer) setSize(v int) { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 315 | dat.array[1].setCheck(uint32(v)) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | // Get size of double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 319 | func (dat *DaTokenizer) GetSize() int { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 320 | return int(dat.array[1].getCheck()) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | // Based on Mizobuchi et al (2000), p. 124 |
| 324 | // This iterates for every state through the complete double array |
| 325 | // structure until it finds a gap that fits all outgoing transitions |
| 326 | // of the state. This is extremely slow, but is only necessary in the |
| 327 | // construction phase of the tokenizer. |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 328 | func (dat *DaTokenizer) xCheck(symbols []int) uint32 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 329 | |
| 330 | // Start at the first entry of the double array list |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame] | 331 | base := uint32(1) |
| 332 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 333 | OVERLAP: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 334 | // Resize the array if necessary |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 335 | dat.resize(int(base) + dat.final) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 336 | for _, a := range symbols { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 337 | if dat.array[int(base)+a].getCheck() != 0 { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 338 | base++ |
| 339 | goto OVERLAP |
| 340 | } |
| 341 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 342 | return base |
| 343 | } |
| 344 | |
Akron | 679b486 | 2021-09-02 16:59:26 +0200 | [diff] [blame] | 345 | // This is an implementation of xCheck with the skip-improvement |
| 346 | // proposed by Morita et al. (2001) |
| 347 | func (dat *DaTokenizer) xCheckSkip(symbols []int) uint32 { |
| 348 | |
| 349 | // Start at the first entry of the double array list |
| 350 | base := uint32(math.Abs(float64(dat.maxSize-1) * .9)) |
| 351 | |
| 352 | OVERLAP: |
| 353 | // Resize the array if necessary |
| 354 | dat.resize(int(base) + dat.final) |
| 355 | for _, a := range symbols { |
| 356 | if dat.array[int(base)+a].getCheck() != 0 { |
| 357 | base++ |
| 358 | goto OVERLAP |
| 359 | } |
| 360 | } |
| 361 | return base |
| 362 | } |
| 363 | |
Akron | 29e306f | 2021-09-02 18:29:56 +0200 | [diff] [blame] | 364 | // This is an implementation of xCheck with the skip-improvement |
| 365 | // proposed by Morita et al. (2001) for higher outdegrees as |
| 366 | // proposed by Niu et al. (2013) |
| 367 | func (dat *DaTokenizer) xCheckSkipNiu(symbols []int) uint32 { |
| 368 | |
| 369 | // Start at the first entry of the double array list |
| 370 | base := uint32(1) |
| 371 | |
| 372 | // Or skip the first few entries |
| 373 | if len(symbols) >= 3 { |
| 374 | base = uint32(math.Abs(float64(dat.maxSize-1)*.9)) + 1 |
| 375 | } |
| 376 | |
| 377 | OVERLAP: |
| 378 | // Resize the array if necessary |
| 379 | dat.resize(int(base) + dat.final + 1) |
| 380 | for _, a := range symbols { |
| 381 | if dat.array[int(base)+a].getCheck() != 0 { |
| 382 | base++ |
| 383 | goto OVERLAP |
| 384 | } |
| 385 | } |
| 386 | return base |
| 387 | } |
| 388 | |
Akron | 7b1faa6 | 2021-09-02 16:10:21 +0200 | [diff] [blame] | 389 | // This is an implementation of xCheck wit an improvement |
| 390 | // proposed by Niu et al. (2013) |
| 391 | func (dat *DaTokenizer) xCheckNiu(symbols []int, block_begin_pos *uint32) uint32 { |
| 392 | |
| 393 | // Start at the first entry of the double array list |
| 394 | base := uint32(1) |
| 395 | |
| 396 | if len(symbols) > 3 { |
| 397 | sort.Ints(symbols) |
| 398 | if *block_begin_pos > uint32(symbols[0]) { |
| 399 | dat.resize(int(*block_begin_pos) + dat.final) |
| 400 | *block_begin_pos += uint32(symbols[len(symbols)-1] + 1) |
| 401 | return *block_begin_pos - uint32(symbols[0]) |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | OVERLAP: |
| 406 | // Resize the array if necessary |
| 407 | dat.resize(int(base) + dat.final) |
| 408 | for _, a := range symbols { |
| 409 | if dat.array[int(base)+a].getCheck() != 0 { |
| 410 | base++ |
| 411 | goto OVERLAP |
| 412 | } |
| 413 | } |
| 414 | return base |
| 415 | } |
| 416 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 417 | // List all outgoing transitions for a state |
| 418 | // for testing purposes |
| 419 | func (dat *DaTokenizer) outgoing(t uint32) []int { |
| 420 | |
| 421 | valid := make([]int, 0, len(dat.sigma)) |
| 422 | |
| 423 | for _, a := range dat.sigma { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 424 | t1 := dat.array[t].getBase() + uint32(a) |
| 425 | if t1 <= dat.array[1].getCheck() && dat.array[t1].getCheck() == t { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 426 | valid = append(valid, a) |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | for _, a := range []int{dat.epsilon, dat.unknown, dat.identity, dat.final} { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 431 | t1 := dat.array[t].getBase() + uint32(a) |
| 432 | if t1 <= dat.array[1].getCheck() && dat.array[t1].getCheck() == t { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 433 | valid = append(valid, -1*a) |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | sort.Ints(valid) |
| 438 | |
| 439 | return valid |
| 440 | } |
| 441 | |
Akron | 4fa28b3 | 2021-08-27 10:55:41 +0200 | [diff] [blame] | 442 | // TransCount as the number of transitions aka arcs in the |
| 443 | // finite state automaton |
| 444 | func (dat *DaTokenizer) TransCount() int { |
| 445 | // Cache the transCount |
| 446 | if dat.transCount > 0 { |
| 447 | return dat.transCount |
| 448 | } |
| 449 | |
| 450 | dat.transCount = 0 |
| 451 | for x := 1; x < len(dat.array); x++ { |
| 452 | if dat.array[x].getBase() != 0 { |
| 453 | dat.transCount++ |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | return dat.transCount |
| 458 | } |
| 459 | |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 460 | // LoadFactor as defined in Kanda et al (2018), |
| 461 | // i.e. the proportion of non-empty elements to all elements. |
| 462 | func (dat *DaTokenizer) LoadFactor() float64 { |
Akron | 4fa28b3 | 2021-08-27 10:55:41 +0200 | [diff] [blame] | 463 | return float64(dat.TransCount()) / float64(len(dat.array)) * 100 |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 464 | } |
| 465 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 466 | // Save stores the double array data in a file |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 467 | func (dat *DaTokenizer) Save(file string) (n int64, err error) { |
| 468 | f, err := os.Create(file) |
| 469 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 470 | log.Println(err) |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 471 | return 0, err |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 472 | } |
| 473 | defer f.Close() |
| 474 | gz := gzip.NewWriter(f) |
| 475 | defer gz.Close() |
| 476 | n, err = dat.WriteTo(gz) |
| 477 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 478 | log.Println(err) |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 479 | return n, err |
| 480 | } |
| 481 | gz.Flush() |
| 482 | return n, nil |
| 483 | } |
| 484 | |
| 485 | // WriteTo stores the double array data in an io.Writer. |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 486 | func (dat *DaTokenizer) WriteTo(w io.Writer) (n int64, err error) { |
| 487 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 488 | wb := bufio.NewWriter(w) |
| 489 | defer wb.Flush() |
| 490 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 491 | // Store magical header |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 492 | all, err := wb.Write([]byte(MAGIC)) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 493 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 494 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 495 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | // Get sigma as a list |
| 499 | sigmalist := make([]rune, len(dat.sigma)+16) |
| 500 | max := 0 |
| 501 | for sym, num := range dat.sigma { |
| 502 | sigmalist[num] = sym |
| 503 | if num > max { |
| 504 | max = num |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | sigmalist = sigmalist[:max+1] |
| 509 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 510 | buf := make([]byte, 0, 16) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 511 | bo.PutUint16(buf[0:2], VERSION) |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 512 | bo.PutUint16(buf[2:4], uint16(dat.epsilon)) |
| 513 | bo.PutUint16(buf[4:6], uint16(dat.unknown)) |
| 514 | bo.PutUint16(buf[6:8], uint16(dat.identity)) |
| 515 | bo.PutUint16(buf[8:10], uint16(dat.final)) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 516 | bo.PutUint16(buf[10:12], uint16(len(sigmalist))) |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 517 | bo.PutUint32(buf[12:16], uint32(len(dat.array)*2)) // Legacy support |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 518 | more, err := wb.Write(buf[0:16]) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 519 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 520 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 521 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | all += more |
| 525 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 526 | // Write sigma |
| 527 | for _, sym := range sigmalist { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 528 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 529 | more, err = wb.WriteRune(sym) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 530 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 531 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 532 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 533 | } |
| 534 | all += more |
| 535 | } |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 536 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 537 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 538 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 539 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 540 | } |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 541 | |
| 542 | // Test marker - could be checksum |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 543 | more, err = wb.Write([]byte("T")) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 544 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 545 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 546 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 547 | } |
| 548 | all += more |
| 549 | |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 550 | // for x := 0; x < len(dat.array); x++ { |
| 551 | for _, bc := range dat.array { |
| 552 | bo.PutUint32(buf[0:4], bc.base) |
| 553 | more, err = wb.Write(buf[0:4]) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 554 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 555 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 556 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 557 | } |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 558 | all += more |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 559 | if more != 4 { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 560 | log.Println("Can not write base uint32") |
| 561 | return int64(all), err |
| 562 | } |
| 563 | bo.PutUint32(buf[0:4], bc.check) |
| 564 | more, err = wb.Write(buf[0:4]) |
| 565 | if err != nil { |
| 566 | log.Println(err) |
| 567 | return int64(all), err |
| 568 | } |
| 569 | all += more |
| 570 | if more != 4 { |
| 571 | log.Println("Can not write check uint32") |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 572 | return int64(all), err |
| 573 | } |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | return int64(all), err |
| 577 | } |
| 578 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 579 | // LoadDatokFile reads a double array represented tokenizer |
| 580 | // from a file. |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 581 | func LoadDatokFile(file string) *DaTokenizer { |
| 582 | f, err := os.Open(file) |
| 583 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 584 | log.Println(err) |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 585 | return nil |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 586 | } |
| 587 | defer f.Close() |
| 588 | |
| 589 | gz, err := gzip.NewReader(f) |
| 590 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 591 | log.Println(err) |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 592 | return nil |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 593 | } |
| 594 | defer gz.Close() |
| 595 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 596 | // Todo: Read the whole file! |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 597 | return ParseDatok(gz) |
| 598 | } |
| 599 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 600 | // LoadDatokFile reads a double array represented tokenizer |
| 601 | // from an io.Reader |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 602 | func ParseDatok(ior io.Reader) *DaTokenizer { |
| 603 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 604 | // Initialize tokenizer with default values |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 605 | dat := &DaTokenizer{ |
| 606 | sigma: make(map[rune]int), |
| 607 | epsilon: 0, |
| 608 | unknown: 0, |
| 609 | identity: 0, |
| 610 | final: 0, |
Akron | 4fa28b3 | 2021-08-27 10:55:41 +0200 | [diff] [blame] | 611 | transCount: 0, |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | r := bufio.NewReader(ior) |
| 615 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 616 | buf := make([]byte, 1024) |
| 617 | buf = buf[0:len(MAGIC)] |
| 618 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 619 | _, err := r.Read(buf) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 620 | |
| 621 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 622 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 623 | return nil |
| 624 | } |
| 625 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 626 | if string(MAGIC) != string(buf) { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 627 | log.Println("Not a datok file") |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 628 | return nil |
| 629 | } |
| 630 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 631 | more, err := io.ReadFull(r, buf[0:16]) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 632 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 633 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 634 | return nil |
| 635 | } |
| 636 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 637 | if more != 16 { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 638 | log.Println("Read bytes do not fit") |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 639 | return nil |
| 640 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 641 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 642 | version := bo.Uint16(buf[0:2]) |
| 643 | |
| 644 | if version != VERSION { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 645 | log.Println("Version not compatible") |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 646 | return nil |
| 647 | } |
| 648 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 649 | dat.epsilon = int(bo.Uint16(buf[2:4])) |
| 650 | dat.unknown = int(bo.Uint16(buf[4:6])) |
| 651 | dat.identity = int(bo.Uint16(buf[6:8])) |
| 652 | dat.final = int(bo.Uint16(buf[8:10])) |
| 653 | |
| 654 | sigmaCount := int(bo.Uint16(buf[10:12])) |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 655 | arraySize := int(bo.Uint32(buf[12:16])) / 2 // Legacy support |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 656 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 657 | // Shouldn't be relevant though |
| 658 | dat.maxSize = arraySize - 1 |
| 659 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 660 | for x := 0; x < sigmaCount; x++ { |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 661 | sym, _, err := r.ReadRune() |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 662 | if err == nil && sym != 0 { |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 663 | if int(sym) < 256 { |
| 664 | dat.sigmaASCII[int(sym)] = x |
| 665 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 666 | dat.sigma[sym] = x |
| 667 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 668 | } |
| 669 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 670 | _, err = io.ReadFull(r, buf[0:1]) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 671 | |
| 672 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 673 | log.Print(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 674 | return nil |
| 675 | } |
| 676 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 677 | if string("T") != string(buf[0:1]) { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 678 | log.Println("Not a datok file") |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 679 | return nil |
| 680 | } |
| 681 | |
| 682 | // Read based on length |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 683 | dat.array = make([]bc, arraySize) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 684 | |
Akron | bb4aac5 | 2021-08-13 00:52:27 +0200 | [diff] [blame] | 685 | dataArray, err := io.ReadAll(r) |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 686 | |
Akron | bb4aac5 | 2021-08-13 00:52:27 +0200 | [diff] [blame] | 687 | if err == io.EOF { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 688 | log.Println(err) |
Akron | bb4aac5 | 2021-08-13 00:52:27 +0200 | [diff] [blame] | 689 | return nil |
| 690 | } |
| 691 | |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 692 | if len(dataArray) < arraySize*8 { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 693 | log.Println("Not enough bytes read") |
Akron | bb4aac5 | 2021-08-13 00:52:27 +0200 | [diff] [blame] | 694 | return nil |
| 695 | } |
| 696 | |
| 697 | for x := 0; x < arraySize; x++ { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 698 | dat.array[x].base = bo.Uint32(dataArray[x*8 : (x*8)+4]) |
| 699 | dat.array[x].check = bo.Uint32(dataArray[(x*8)+4 : (x*8)+8]) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | return dat |
| 703 | } |
| 704 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 705 | // Show the current state of the buffer, |
| 706 | // for testing puroses |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 707 | func showBuffer(buffer []rune, buffo int, buffi int) string { |
| 708 | out := make([]rune, 0, 1024) |
| 709 | for x := 0; x < len(buffer); x++ { |
| 710 | if buffi == x { |
| 711 | out = append(out, '^') |
| 712 | } |
| 713 | if buffo == x { |
| 714 | out = append(out, '[', buffer[x], ']') |
| 715 | } else { |
| 716 | out = append(out, buffer[x]) |
| 717 | } |
| 718 | } |
| 719 | return string(out) |
| 720 | } |
| 721 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 722 | // Transduce an input string against the double array |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 723 | // FSA. The rules are always greedy. If the automaton fails, |
| 724 | // it takes the last possible token ending branch. |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 725 | // |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 726 | // Based on Mizobuchi et al (2000), p. 129, |
| 727 | // with additional support for IDENTITY, UNKNOWN |
| 728 | // and EPSILON transitions and NONTOKEN and TOKENEND handling. |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 729 | func (dat *DaTokenizer) Transduce(r io.Reader, w io.Writer) bool { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 730 | var a int |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 731 | var t0 uint32 |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 732 | t := uint32(1) // Initial state |
| 733 | var ok, rewindBuffer bool |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 734 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 735 | // Remember the last position of a possible tokenend, |
| 736 | // in case the automaton fails. |
| 737 | epsilonState := uint32(0) |
| 738 | epsilonOffset := 0 |
| 739 | |
| 740 | // Implement a low level buffer for full control, |
| 741 | // however - it is probably better to introduce |
| 742 | // this on a higher level with a io.Reader interface |
| 743 | // The buffer stores a single word and may have white |
| 744 | // space at the end (but not at the beginning). |
| 745 | // |
| 746 | // This is the only backtracking requirement because of |
| 747 | // epsilon transitions, to support tokenizations like: |
| 748 | // "this is an example|.| And it works." vs |
| 749 | // "this is an example.com| application." |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 750 | // |
| 751 | // TODO: |
| 752 | // Store a translation buffer as well, so characters don't |
| 753 | // have to be translated multiple times! |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 754 | buffer := make([]rune, 1024) |
| 755 | buffo := 0 // Buffer offset |
| 756 | buffi := 0 // Buffer length |
| 757 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 758 | reader := bufio.NewReader(r) |
| 759 | writer := bufio.NewWriter(w) |
| 760 | defer writer.Flush() |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 761 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 762 | var char rune |
| 763 | var err error |
| 764 | eof := false |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 765 | newchar := true |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 766 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 767 | PARSECHAR: |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 768 | for { |
| 769 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 770 | if newchar { |
| 771 | // Get from reader if buffer is empty |
| 772 | if buffo >= buffi { |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 773 | if eof { |
| 774 | break |
| 775 | } |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 776 | char, _, err = reader.ReadRune() |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 777 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 778 | // No more runes to read |
| 779 | if err != nil { |
| 780 | eof = true |
| 781 | break |
| 782 | } |
| 783 | buffer[buffi] = char |
| 784 | buffi++ |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 785 | } |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 786 | |
| 787 | char = buffer[buffo] |
| 788 | |
| 789 | if DEBUG { |
| 790 | fmt.Println("Current char", string(char), showBuffer(buffer, buffo, buffi)) |
| 791 | } |
| 792 | |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame] | 793 | // TODO: |
| 794 | // Better not repeatedly check for a! |
| 795 | // Possibly keep a buffer with a. |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 796 | if int(char) < 256 { |
| 797 | a = dat.sigmaASCII[int(char)] |
| 798 | } else { |
| 799 | a, ok = dat.sigma[char] |
| 800 | if !ok { |
| 801 | a = 0 |
| 802 | } |
| 803 | } |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 804 | |
| 805 | // Use identity symbol if character is not in sigma |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 806 | if a == 0 && dat.identity != -1 { |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 807 | a = dat.identity |
| 808 | } |
| 809 | |
| 810 | t0 = t |
| 811 | |
| 812 | // Check for epsilon transitions and remember |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 813 | if dat.array[dat.array[t0].getBase()+uint32(dat.epsilon)].getCheck() == t0 { |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 814 | // Remember state for backtracking to last tokenend state |
| 815 | epsilonState = t0 |
| 816 | epsilonOffset = buffo |
| 817 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 818 | } |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 819 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 820 | // Checks a transition based on t0, a and buffo |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 821 | t = dat.array[t0].getBase() + uint32(a) |
| 822 | ta := dat.array[t] |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 823 | |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 824 | if DEBUG { |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 825 | // Char is only relevant if set |
| 826 | fmt.Println("Check", t0, "-", a, "(", string(char), ")", "->", t) |
| 827 | if false { |
| 828 | fmt.Println(dat.outgoing(t0)) |
| 829 | } |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 830 | } |
| 831 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 832 | // Check if the transition is invalid according to the double array |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 833 | if t > dat.array[1].getCheck() || ta.getCheck() != t0 { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 834 | |
| 835 | if DEBUG { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 836 | fmt.Println("Match is not fine!", t, "and", ta.getCheck(), "vs", t0) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | if !ok && a == dat.identity { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 840 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 841 | // Try again with unknown symbol, in case identity failed |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 842 | // Char is only relevant when set |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 843 | if DEBUG { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 844 | fmt.Println("UNKNOWN symbol", string(char), "->", dat.unknown) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 845 | } |
| 846 | a = dat.unknown |
| 847 | |
| 848 | } else if a != dat.epsilon { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 849 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 850 | // Try again with epsilon symbol, in case everything else failed |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 851 | t0 = epsilonState |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 852 | epsilonState = 0 // reset |
| 853 | buffo = epsilonOffset |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 854 | a = dat.epsilon |
| 855 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 856 | if DEBUG { |
| 857 | fmt.Println("Get from epsilon stack and set buffo!", showBuffer(buffer, buffo, buffi)) |
| 858 | } |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 859 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 860 | } else { |
| 861 | break |
| 862 | } |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 863 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 864 | newchar = false |
| 865 | continue |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 866 | } |
| 867 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 868 | // Transition was successful |
| 869 | rewindBuffer = false |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 870 | |
| 871 | // Transition consumes a character |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 872 | if a != dat.epsilon { |
| 873 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 874 | buffo++ |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 875 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 876 | // Transition does not produce a character |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 877 | if buffo == 1 && ta.isNonToken() { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 878 | if DEBUG { |
| 879 | fmt.Println("Nontoken forward", showBuffer(buffer, buffo, buffi)) |
| 880 | } |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 881 | rewindBuffer = true |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 882 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 883 | } |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 884 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 885 | // Transition marks the end of a token - so flush the buffer |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 886 | if ta.isTokenEnd() { |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 887 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 888 | if buffi > 0 { |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 889 | if DEBUG { |
Akron | 01912fc | 2021-08-12 11:41:58 +0200 | [diff] [blame] | 890 | fmt.Println("-> Flush buffer: [", string(buffer[:buffo]), "]", showBuffer(buffer, buffo, buffi)) |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 891 | } |
Akron | 01912fc | 2021-08-12 11:41:58 +0200 | [diff] [blame] | 892 | writer.WriteString(string(buffer[:buffo])) |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 893 | rewindBuffer = true |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 894 | } |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 895 | if DEBUG { |
| 896 | fmt.Println("-> Newline") |
| 897 | } |
| 898 | writer.WriteRune('\n') |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 899 | } |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 900 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 901 | // Rewind the buffer if necessary |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 902 | if rewindBuffer { |
| 903 | |
| 904 | // TODO: Better as a ring buffer |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 905 | for x, i := range buffer[buffo:buffi] { |
| 906 | buffer[x] = i |
| 907 | } |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 908 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 909 | buffi -= buffo |
| 910 | epsilonOffset -= buffo |
| 911 | buffo = 0 |
| 912 | if DEBUG { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 913 | fmt.Println("Remaining:", showBuffer(buffer, buffo, buffi)) |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 914 | } |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 915 | } |
| 916 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 917 | // Move to representative state |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 918 | if ta.isSeparate() { |
| 919 | t = ta.getBase() |
| 920 | ta = dat.array[t] |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 921 | |
| 922 | if DEBUG { |
| 923 | fmt.Println("Representative pointing to", t) |
| 924 | } |
| 925 | } |
| 926 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 927 | newchar = true |
| 928 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 929 | // TODO: |
| 930 | // Prevent endless epsilon loops! |
| 931 | } |
| 932 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 933 | // Input reader is not yet finished |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 934 | if !eof { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 935 | if DEBUG { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 936 | fmt.Println("Not at the end - problem", t0, ":", dat.outgoing(t0)) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 937 | } |
| 938 | return false |
| 939 | } |
| 940 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 941 | if DEBUG { |
| 942 | fmt.Println("Entering final check") |
| 943 | } |
| 944 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 945 | // Automaton is in a final state, so flush the buffer and return |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 946 | x := dat.array[t].getBase() + uint32(dat.final) |
| 947 | |
| 948 | if x < dat.array[1].getCheck() && dat.array[x].getCheck() == t { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 949 | |
| 950 | if buffi > 0 { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 951 | if DEBUG { |
Akron | 01912fc | 2021-08-12 11:41:58 +0200 | [diff] [blame] | 952 | fmt.Println("-> Flush buffer: [", string(buffer[:buffi]), "]") |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 953 | } |
Akron | 01912fc | 2021-08-12 11:41:58 +0200 | [diff] [blame] | 954 | writer.WriteString(string(buffer[:buffi])) |
Akron | 6e70dc8 | 2021-08-11 11:33:18 +0200 | [diff] [blame] | 955 | |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 956 | if dat.array[t].isTokenEnd() { |
Akron | df0a3ef | 2021-08-09 15:53:45 +0200 | [diff] [blame] | 957 | writer.WriteRune('\n') |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 958 | if DEBUG { |
| 959 | fmt.Println("-> Newline") |
| 960 | } |
Akron | df0a3ef | 2021-08-09 15:53:45 +0200 | [diff] [blame] | 961 | } |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 962 | } |
| 963 | |
Akron | 6e70dc8 | 2021-08-11 11:33:18 +0200 | [diff] [blame] | 964 | // Add an additional sentence ending, if the file is over but no explicit |
| 965 | // sentence split was reached. This may be controversial and therefore |
| 966 | // optional via parameter. |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 967 | if !dat.array[t0].isTokenEnd() { |
Akron | 6e70dc8 | 2021-08-11 11:33:18 +0200 | [diff] [blame] | 968 | writer.WriteRune('\n') |
| 969 | if DEBUG { |
| 970 | fmt.Println("-> Newline") |
| 971 | } |
| 972 | } |
| 973 | |
Akron | e61380b | 2021-08-16 10:10:46 +0200 | [diff] [blame] | 974 | // TODO: |
| 975 | // There may be a new line at the end, from an epsilon, |
| 976 | // so we may need to go on! |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 977 | return true |
| 978 | } |
| 979 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 980 | // Check epsilon transitions until a final state is reached |
| 981 | t0 = t |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 982 | t = dat.array[t0].getBase() + uint32(dat.epsilon) |
Akron | 01912fc | 2021-08-12 11:41:58 +0200 | [diff] [blame] | 983 | a = dat.epsilon |
| 984 | newchar = false |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 985 | if dat.array[t].getCheck() == t0 { |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 986 | // Remember state for backtracking to last tokenend state |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 987 | goto PARSECHAR |
Akron | e61380b | 2021-08-16 10:10:46 +0200 | [diff] [blame] | 988 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 989 | } else if epsilonState != 0 { |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 990 | t0 = epsilonState |
| 991 | epsilonState = 0 // reset |
| 992 | buffo = epsilonOffset |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 993 | if DEBUG { |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 994 | fmt.Println("Get from epsilon stack and set buffo!", showBuffer(buffer, buffo, buffi)) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 995 | } |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 996 | goto PARSECHAR |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 997 | } |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 998 | return false |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 999 | } |