[2558] | 1 | import sys |
---|
| 2 | import pablo |
---|
| 3 | |
---|
| 4 | class Basis_bits(): |
---|
| 5 | bit_0 = 0 |
---|
| 6 | bit_1 = 0 |
---|
| 7 | bit_2 = 0 |
---|
| 8 | bit_3 = 0 |
---|
| 9 | bit_4 = 0 |
---|
| 10 | bit_5 = 0 |
---|
| 11 | bit_6 = 0 |
---|
| 12 | bit_7 = 0 |
---|
| 13 | |
---|
[2560] | 14 | class Parity(): |
---|
| 15 | odd = 0 |
---|
| 16 | even = 0 |
---|
| 17 | |
---|
[2558] | 18 | class Lex (): |
---|
| 19 | LParen = 0 |
---|
| 20 | RParen = 0 |
---|
| 21 | Backslash = 0 |
---|
| 22 | Percent = 0 |
---|
[2568] | 23 | Slash=0 |
---|
| 24 | Regular=0 |
---|
| 25 | Delimiter=0 |
---|
| 26 | LAngle=0 |
---|
| 27 | RAngle=0 |
---|
| 28 | Hex=0 |
---|
| 29 | WS=0 |
---|
| 30 | Digit=0 |
---|
| 31 | Sign=0 |
---|
| 32 | Period=0 |
---|
| 33 | EOL=0 |
---|
| 34 | RBracket=0 |
---|
| 35 | LBracket=0 |
---|
[2558] | 36 | |
---|
| 37 | class Escaped_Callouts (): |
---|
| 38 | UELParen = 0 |
---|
| 39 | UERParen = 0 |
---|
| 40 | |
---|
| 41 | class Comments_Callouts() : |
---|
[2568] | 42 | mask=0 |
---|
[2558] | 43 | class LiteralStrings_Callouts() : |
---|
[2568] | 44 | starts=0 |
---|
| 45 | ends=0 |
---|
| 46 | mask=0 |
---|
[2558] | 47 | error = 0 |
---|
| 48 | |
---|
| 49 | class Marker() : |
---|
| 50 | mask=0 |
---|
| 51 | |
---|
| 52 | class Names_Callouts() : |
---|
| 53 | starts = 0 |
---|
[2560] | 54 | follows = 0 |
---|
[2558] | 55 | |
---|
| 56 | class HexStrings_Callouts() : |
---|
| 57 | starts = 0 |
---|
[2568] | 58 | ends = 0 |
---|
[2558] | 59 | error = 0 |
---|
| 60 | |
---|
| 61 | class Numeric_Callouts() : |
---|
| 62 | starts = 0 |
---|
| 63 | follows = 0 |
---|
| 64 | error = 0 |
---|
| 65 | |
---|
| 66 | class Keywords_Callouts() : |
---|
| 67 | starts = 0 |
---|
[2568] | 68 | ends = 0 |
---|
[2558] | 69 | |
---|
| 70 | class Arrays_Callouts() : |
---|
| 71 | starts = 0 |
---|
[2568] | 72 | ends = 0 |
---|
[2558] | 73 | |
---|
[2568] | 74 | |
---|
[2558] | 75 | def Classify_bytes(basis_bits, lex): |
---|
| 76 | temp1 = (basis_bits.bit_0 | basis_bits.bit_1) |
---|
| 77 | temp2 = (basis_bits.bit_2 &~ basis_bits.bit_3) |
---|
| 78 | temp3 = (temp2 &~ temp1) |
---|
| 79 | temp4 = (basis_bits.bit_4 &~ basis_bits.bit_5) |
---|
| 80 | temp5 = (basis_bits.bit_6 | basis_bits.bit_7) |
---|
| 81 | temp6 = (temp4 &~ temp5) |
---|
| 82 | lex.LParen = (temp3 & temp6) |
---|
| 83 | temp7 = (basis_bits.bit_7 &~ basis_bits.bit_6) |
---|
| 84 | temp8 = (temp4 & temp7) |
---|
| 85 | lex.RParen = (temp3 & temp8) |
---|
| 86 | temp9 = (basis_bits.bit_1 &~ basis_bits.bit_0) |
---|
| 87 | temp10 = (basis_bits.bit_3 &~ basis_bits.bit_2) |
---|
| 88 | temp11 = (temp9 & temp10) |
---|
| 89 | temp12 = (basis_bits.bit_4 & basis_bits.bit_5) |
---|
| 90 | temp13 = (temp12 &~ temp5) |
---|
| 91 | lex.Backslash = (temp11 & temp13) |
---|
| 92 | temp14 = (temp5 &~ temp7) |
---|
| 93 | temp15 = (temp4 &~ temp14) |
---|
| 94 | temp16 = (temp3 & temp15) |
---|
| 95 | temp17 = (basis_bits.bit_2 & basis_bits.bit_3) |
---|
| 96 | temp18 = (temp17 &~ temp1) |
---|
| 97 | temp19 = (temp18 & temp13) |
---|
| 98 | temp20 = (temp16 | temp19) |
---|
| 99 | temp21 = (basis_bits.bit_6 &~ basis_bits.bit_7) |
---|
| 100 | temp22 = (temp12 & temp21) |
---|
| 101 | temp23 = (temp18 & temp22) |
---|
| 102 | temp24 = (temp20 | temp23) |
---|
| 103 | temp25 = (basis_bits.bit_6 & basis_bits.bit_7) |
---|
| 104 | temp26 = (temp4 & temp25) |
---|
| 105 | temp27 = (temp11 & temp26) |
---|
| 106 | temp28 = (temp24 | temp27) |
---|
| 107 | temp29 = (temp12 & temp7) |
---|
| 108 | temp30 = (temp11 & temp29) |
---|
| 109 | temp31 = (temp28 | temp30) |
---|
| 110 | temp32 = (temp9 & temp17) |
---|
| 111 | temp33 = (temp32 & temp26) |
---|
| 112 | temp34 = (temp31 | temp33) |
---|
| 113 | temp35 = (temp32 & temp29) |
---|
| 114 | temp36 = (temp34 | temp35) |
---|
| 115 | temp37 = (basis_bits.bit_5 &~ basis_bits.bit_4) |
---|
| 116 | temp38 = (temp37 & temp7) |
---|
| 117 | temp39 = (temp3 & temp38) |
---|
| 118 | temp40 = (temp36 | temp39) |
---|
| 119 | temp41 = (temp12 & temp25) |
---|
| 120 | temp42 = (temp3 & temp41) |
---|
| 121 | lex.Delimiter = (temp40 | temp42) |
---|
| 122 | lex.Percent = (temp3 & temp38) |
---|
| 123 | lex.Period = (temp3 & temp22) |
---|
| 124 | lex.LAngle = (temp18 & temp13) |
---|
| 125 | lex.RAngle = (temp18 & temp22) |
---|
| 126 | lex.LBracket = (temp11 & temp26) |
---|
| 127 | lex.RBracket = (temp11 & temp29) |
---|
| 128 | lex.Slash = (temp3 & temp41) |
---|
| 129 | temp43 = (basis_bits.bit_2 | basis_bits.bit_3) |
---|
| 130 | temp44 = (temp1 | temp43) |
---|
| 131 | temp45 = (temp4 & temp21) |
---|
| 132 | temp46 = (temp29 | temp45) |
---|
| 133 | lex.EOL = (temp46 &~ temp44) |
---|
| 134 | temp47 = (basis_bits.bit_4 | basis_bits.bit_5) |
---|
| 135 | temp48 = (temp47 | temp5) |
---|
| 136 | temp49 = (temp44 | temp48) |
---|
| 137 | temp50 = (temp13 &~ temp44) |
---|
| 138 | temp51 = (temp49 &~ temp50) |
---|
| 139 | temp52 = (temp29 &~ temp44) |
---|
| 140 | temp53 = (temp51 &~ temp52) |
---|
| 141 | temp54 = (temp45 &~ temp44) |
---|
| 142 | temp55 = (temp53 &~ temp54) |
---|
| 143 | temp56 = (temp8 &~ temp44) |
---|
| 144 | temp57 = (temp55 &~ temp56) |
---|
| 145 | temp58 = (temp3 &~ temp48) |
---|
| 146 | temp59 = (temp57 &~ temp58) |
---|
| 147 | lex.WS = (~temp59) |
---|
| 148 | temp60 = (basis_bits.bit_5 | basis_bits.bit_6) |
---|
| 149 | temp61 = (basis_bits.bit_4 & temp60) |
---|
| 150 | lex.Digit = (temp18 &~ temp61) |
---|
| 151 | temp62 = (temp29 | temp26) |
---|
| 152 | lex.Sign = (temp3 & temp62) |
---|
| 153 | temp63 = (temp9 &~ temp43) |
---|
| 154 | temp64 = (temp63 &~ basis_bits.bit_4) |
---|
| 155 | temp65 = (~temp25) |
---|
| 156 | temp66 = ((basis_bits.bit_5 & temp65)|(~(basis_bits.bit_5) & temp5)) |
---|
| 157 | temp67 = (temp64 & temp66) |
---|
| 158 | temp68 = (lex.Digit | temp67) |
---|
| 159 | temp69 = (temp9 & temp2) |
---|
| 160 | temp70 = (temp69 &~ basis_bits.bit_4) |
---|
| 161 | temp71 = (temp70 & temp66) |
---|
| 162 | lex.Hex = (temp68 | temp71) |
---|
| 163 | lex.Regular = ~lex.Delimiter&~lex.WS |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | |
---|
[2569] | 167 | #def simd_const_4(hexdigit, EOF_mask): |
---|
| 168 | # lgth = pablo.count_leading_zeroes(~EOF_mask)/4 |
---|
| 169 | # return int(hexdigit*(lgth+1),16)&EOF_mask |
---|
[2558] | 170 | |
---|
| 171 | |
---|
[2568] | 172 | def Parse_Escaped(lex, escaped_Callouts): |
---|
| 173 | |
---|
[2569] | 174 | #odd = simd_const_4('a',pablo.EOF_mask) |
---|
| 175 | #even = simd_const_4('5',pablo.EOF_mask) |
---|
[2568] | 176 | |
---|
[2558] | 177 | start = lex.Backslash &~ pablo.Advance(lex.Backslash) |
---|
| 178 | even_start = start & even |
---|
| 179 | |
---|
| 180 | even_final = pablo.ScanThru(even_start, lex.Backslash) |
---|
| 181 | escape = even_final & odd |
---|
| 182 | |
---|
| 183 | odd_start = start & odd |
---|
| 184 | odd_final = pablo.ScanThru(odd_start, lex.Backslash) |
---|
| 185 | escape = escape | (odd_final & even) |
---|
| 186 | |
---|
[2560] | 187 | escaped_Callouts.UELParen = lex.LParen &~ escape |
---|
| 188 | escaped_Callouts.UERParen = lex.RParen &~ escape |
---|
[2568] | 189 | |
---|
[2558] | 190 | # Parallel version |
---|
[2568] | 191 | # def Parse_LiteralStrings(LParen, RParen, LiteralStrings_Callouts): |
---|
[2558] | 192 | # unmatched = RParen |
---|
| 193 | # pscan = {} |
---|
| 194 | # qscan = {} |
---|
[2568] | 195 | # LiteralStrings_Callouts.closed={} |
---|
[2558] | 196 | # i = 0 |
---|
| 197 | # pscan[0] = pablo.ScanTo(pablo.Advance(LParen), LParen | RParen) |
---|
| 198 | # qscan[0] = pablo.ScanTo(pablo.Advance(RParen), LParen | RParen) |
---|
[2568] | 199 | # LiteralStrings_Callouts.mask = (pscan[0] - (LParen)) | pscan[0] |
---|
[2558] | 200 | |
---|
[2568] | 201 | # LiteralStrings_Callouts.closed[i] = pscan[i] & RParen |
---|
[2558] | 202 | # unclosed = pscan[i] & LParen | qscan[i] & RParen |
---|
[2568] | 203 | # LiteralStrings_Callouts.error = pscan[i] &~ pablo.EOF_mask |
---|
| 204 | # all_closed = LiteralStrings_Callouts.closed[i] |
---|
[2558] | 205 | # while unclosed: |
---|
| 206 | # i += 1 |
---|
| 207 | # pscan[i] = pablo.ScanTo(pablo.Advance(unclosed & LParen), unclosed) |
---|
| 208 | # qscan[i] = pablo.ScanTo(pablo.Advance(unclosed & RParen), unclosed) |
---|
[2568] | 209 | # LiteralStrings_Callouts.mask|=(pscan[i]-(unclosed & LParen)) | pscan[i] |
---|
| 210 | # LiteralStrings_Callouts.closed[i] = pscan[i] & RParen #| qscan[i] & LParen |
---|
[2558] | 211 | # unclosed = pscan[i] & LParen | qscan[i] & RParen |
---|
[2568] | 212 | # all_closed |= LiteralStrings_Callouts.closed[i] |
---|
| 213 | # LiteralStrings_Callouts.error |= pscan[i] &~ pablo.EOF_mask #| ~pablo.atEOF(qscan[i]) |
---|
[2558] | 214 | |
---|
[2568] | 215 | # LiteralStrings_Callouts.error |= RParen &~ all_closed |
---|
| 216 | # def Parse_Comments(Percent, lex, Comments_Callouts) : |
---|
[2558] | 217 | # end = Pablo.ScanTo(Percent, lex.EOL) |
---|
[2568] | 218 | # Comments_Callouts.mask = end-Percent |
---|
| 219 | # def Parse_CommentsLiteralStrings(lex, Escaped_Callouts, Marker, Comments_Callouts, LiteralStrings_Callouts) : |
---|
| 220 | # Parse_LiteralStrings(Escaped_Callouts.UELParen, Escaped_Callouts.UERParen, LiteralStrings_Callouts) |
---|
| 221 | # while (lex.Percent &~ (LiteralStrings_Callouts.mask | Comments_Callouts.mask)) : |
---|
| 222 | # Parse_Comments(lex.Percent&~LiteralStrings_Callouts.mask, lex, Comments_Callouts) |
---|
| 223 | # if (LiteralStrings_Callouts.mask & Comments_Callouts.mask) : |
---|
| 224 | # Parse_LiteralStrings(Escaped_Callouts.UELP &~ Comments_Callouts.mask, Escaped_Callouts.UERP &~ Comments_Callouts.mask, LiteralStrings_Callouts) |
---|
[2558] | 225 | # else : |
---|
| 226 | # break |
---|
[2568] | 227 | # Marker.mask = Comments_Callouts.mask | LiteralStrings_Callouts.mask |
---|
[2558] | 228 | |
---|
| 229 | |
---|
| 230 | |
---|
[2560] | 231 | def Parse_CommentsLiteralStrings(lex, escaped_Callouts, marker, comments_Callouts, literalStrings_Callouts) : |
---|
[2558] | 232 | #not entirely sure this code is correct once compiled |
---|
| 233 | depth = 0 |
---|
| 234 | comment_starts=0 |
---|
[2568] | 235 | comment_ends=0 |
---|
[2560] | 236 | scan_characters = escaped_Callouts.UELParen |escaped_Callouts.UERParen | lex.Percent |
---|
[2558] | 237 | cursor = pablo.ScanToFirst(scan_characters) |
---|
[2568] | 238 | while pablo.inFile(cursor) : |
---|
[2558] | 239 | comment_cursor = cursor & lex.Percent |
---|
[2560] | 240 | string_cursor = cursor & (escaped_Callouts.UELParen|escaped_Callouts.UERParen) |
---|
[2558] | 241 | if comment_cursor : |
---|
| 242 | comment_starts |=comment_cursor |
---|
| 243 | comment_cursor = pablo.ScanTo(comment_cursor, lex.EOL) |
---|
[2568] | 244 | comment_ends |=comment_cursor |
---|
[2558] | 245 | cursor = pablo.ScanTo(pablo.Advance(comment_cursor), scan_characters) |
---|
[2568] | 246 | |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | if string_cursor : |
---|
| 250 | if (string_cursor & escaped_Callouts.UELParen) : |
---|
| 251 | if is_zero(): |
---|
| 252 | literalStrings_Callouts.starts|=string_cursor |
---|
| 253 | |
---|
| 254 | inc() |
---|
| 255 | |
---|
| 256 | cursor = pablo.ScanTo(pablo.Advance(string_cursor), escaped_Callouts.UELParen | escaped_Callouts.UERParen) |
---|
| 257 | if (string_cursor & escaped_Callouts.UERParen) : |
---|
| 258 | |
---|
| 259 | dec() |
---|
| 260 | |
---|
| 261 | if is_zero(): |
---|
| 262 | literalStrings_Callouts.ends|=string_cursor |
---|
| 263 | cursor = pablo.ScanTo(pablo.Advance(string_cursor), scan_characters) |
---|
| 264 | elif lt_zero(): |
---|
| 265 | literalStrings_Callouts.error|=string_cursor |
---|
| 266 | cursor = pablo.ScanTo(pablo.Advance(string_cursor), scan_characters) |
---|
| 267 | else: |
---|
| 268 | cursor = pablo.ScanTo(pablo.Advance(string_cursor), escaped_Callouts.UELParen | escaped_Callouts.UERParen) |
---|
[2558] | 269 | |
---|
| 270 | |
---|
| 271 | |
---|
| 272 | #if (depth>0): |
---|
| 273 | #error |
---|
[2568] | 274 | comments_Callouts.mask = pablo.SpanUpTo(comment_starts, comment_ends) |
---|
| 275 | literalStrings_Callouts.mask = pablo.SpanUpTo(literalStrings_Callouts.starts,literalStrings_Callouts.ends) |
---|
[2560] | 276 | marker.mask = comments_Callouts.mask | literalStrings_Callouts.mask |
---|
[2558] | 277 | |
---|
| 278 | |
---|
[2560] | 279 | def Parse_Names(lex, marker, names_Callouts) : |
---|
| 280 | names_Callouts.starts = pablo.Advance(lex.Slash&~marker.mask) |
---|
| 281 | names_Callouts.follows = pablo.ScanThru(names_Callouts.starts, lex.Regular) |
---|
| 282 | marker.mask |=names_Callouts.starts |
---|
[2558] | 283 | |
---|
[2560] | 284 | def Parse_HexStrings(lex, marker, hexStrings_Callouts) : |
---|
[2558] | 285 | #Haven't confirmed that this is the correct behaviour with bad input |
---|
[2560] | 286 | hexStrings_Callouts.starts = lex.LAngle &~marker.mask |
---|
[2568] | 287 | hexStrings_Callouts.ends = pablo.ScanThru(pablo.Advance(hexStrings_Callouts.starts),lex.Hex|lex.WS) |
---|
| 288 | hexStrings_Callouts.error = hexStrings_Callouts.ends &~ lex.RAngle |
---|
| 289 | marker.mask |= pablo.SpanUpTo(hexStrings_Callouts.starts,hexStrings_Callouts.ends) |
---|
[2558] | 290 | |
---|
[2560] | 291 | def Parse_Numeric(lex, marker, numeric_Callouts) : |
---|
[2558] | 292 | numeric_characters = (lex.Digit | lex.Period | lex.Sign) |
---|
[2560] | 293 | numeric_Callouts.starts =(numeric_characters &~ pablo.Advance(lex.Regular)) &~ marker.mask |
---|
| 294 | numeric_Callouts.follows =pablo.ScanThru(numeric_Callouts.starts, lex.Regular) |
---|
[2568] | 295 | marker.mask |= numeric_Callouts.starts |
---|
| 296 | |
---|
[2560] | 297 | # aftersign = pablo.Advance(numeric_Callouts.starts & lex.Sign) | (numeric_Callouts.starts &~ lex.Sign) |
---|
[2558] | 298 | # temp = pablo.ScanThru(aftersign, lex.Digit) |
---|
| 299 | # afterperiod = pablo.Advance(temp & lex.Period) | (temp &~ lex.Period) |
---|
[2560] | 300 | # numeric_Callouts.follows = pablo.ScanThru(afterperiod, lex.Digit) |
---|
| 301 | # numeric_Callouts.error = numeric_Callouts.follows &~ (lex.WS | lex.Delimiter) |
---|
| 302 | # marker.mask |= numeric_Callouts.starts |
---|
[2558] | 303 | |
---|
[2560] | 304 | def Parse_Keywords(lex, marker, keywords_Callouts) : |
---|
| 305 | keywords_Callouts.starts = (lex.Regular &~ pablo.Advance(lex.Regular)) &~ marker.mask |
---|
| 306 | keywords_Callouts.follows = pablo.ScanThru(keywords_Callouts.starts, lex.Regular) |
---|
[2558] | 307 | |
---|
[2560] | 308 | def Parse_Arrays(lex, marker, arrays_Callouts) : |
---|
[2568] | 309 | arrays_Callouts.starts = lex.LBracket &~ marker.mask |
---|
| 310 | arrays_Callouts.ends = lex.RBracket &~ marker.mask |
---|
| 311 | |
---|
[2558] | 312 | |
---|
[2560] | 313 | def Main(basis_bits, lex, parity, escaped_Callouts): |
---|
[2558] | 314 | Classify_bytes(basis_bits, lex) |
---|
[2560] | 315 | # Generate parity odd/even streams |
---|
| 316 | Parse_Escaped(lex, parity, escaped_Callouts) |
---|
| 317 | # Parse_CommentsLiteralStrings(lex, escaped_Callouts, marker, comments_Callouts, literalStrings_Callouts) |
---|
| 318 | # Parse_Names(lex, marker, names_Callouts) |
---|
| 319 | # Parse_HexStrings(lex, marker, hexStrings_Callouts) |
---|
| 320 | # Parse_Numeric(lex, marker, numeric_Callouts) |
---|
| 321 | # Parse_Keywords(lex, marker, keywords_Callouts) |
---|
| 322 | # Parse_Arrays(lex, marker, arrays_Callouts) |
---|
[2558] | 323 | |
---|