[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 |
---|
[2560] | 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() : |
---|
[2560] | 42 | mask = 0 |
---|
| 43 | |
---|
[2558] | 44 | class LiteralStrings_Callouts() : |
---|
[2560] | 45 | starts = 0 |
---|
| 46 | follows = 0 |
---|
| 47 | mask = 0 |
---|
[2558] | 48 | error = 0 |
---|
| 49 | |
---|
| 50 | class Marker() : |
---|
| 51 | mask=0 |
---|
| 52 | |
---|
| 53 | class Names_Callouts() : |
---|
| 54 | starts = 0 |
---|
[2560] | 55 | follows = 0 |
---|
[2558] | 56 | |
---|
| 57 | class HexStrings_Callouts() : |
---|
| 58 | starts = 0 |
---|
[2560] | 59 | follows = 0 |
---|
[2558] | 60 | error = 0 |
---|
| 61 | |
---|
| 62 | class Numeric_Callouts() : |
---|
| 63 | starts = 0 |
---|
| 64 | follows = 0 |
---|
| 65 | error = 0 |
---|
| 66 | |
---|
| 67 | class Keywords_Callouts() : |
---|
| 68 | starts = 0 |
---|
[2560] | 69 | follows = 0 |
---|
[2558] | 70 | |
---|
| 71 | class Arrays_Callouts() : |
---|
| 72 | starts = 0 |
---|
[2560] | 73 | follows = 0 |
---|
[2558] | 74 | |
---|
| 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 | |
---|
[2560] | 165 | # Demo Only |
---|
| 166 | # |
---|
| 167 | # def simd_const_4(hexdigit, EOF_mask): |
---|
| 168 | # lgth = bitutil.count_leading_zeroes(~EOF_mask)/4 |
---|
| 169 | # return int(hexdigit*(lgth+1),16)&EOF_mask |
---|
[2558] | 170 | |
---|
| 171 | |
---|
[2560] | 172 | def Parse_Escaped(lex, parity, escaped_Callouts): |
---|
[2558] | 173 | |
---|
[2560] | 174 | # Demo Only |
---|
| 175 | # |
---|
| 176 | # odd = simd_const_4('a',EOF_mask) |
---|
| 177 | # even = simd_const_4('5',EOF_mask) |
---|
[2558] | 178 | |
---|
[2560] | 179 | odd = parity.odd |
---|
| 180 | even = parity.even |
---|
| 181 | |
---|
[2558] | 182 | start = lex.Backslash &~ pablo.Advance(lex.Backslash) |
---|
| 183 | even_start = start & even |
---|
| 184 | |
---|
| 185 | even_final = pablo.ScanThru(even_start, lex.Backslash) |
---|
| 186 | escape = even_final & odd |
---|
| 187 | |
---|
| 188 | odd_start = start & odd |
---|
| 189 | odd_final = pablo.ScanThru(odd_start, lex.Backslash) |
---|
| 190 | escape = escape | (odd_final & even) |
---|
| 191 | |
---|
[2560] | 192 | escaped_Callouts.UELParen = lex.LParen &~ escape |
---|
| 193 | escaped_Callouts.UERParen = lex.RParen &~ escape |
---|
| 194 | |
---|
[2558] | 195 | # Parallel version |
---|
[2560] | 196 | # def Parse_LiteralStrings(LParen, RParen, literalStrings_Callouts): |
---|
[2558] | 197 | # unmatched = RParen |
---|
| 198 | # pscan = {} |
---|
| 199 | # qscan = {} |
---|
[2560] | 200 | # literalStrings_Callouts.closed={} |
---|
[2558] | 201 | # i = 0 |
---|
| 202 | # pscan[0] = pablo.ScanTo(pablo.Advance(LParen), LParen | RParen) |
---|
| 203 | # qscan[0] = pablo.ScanTo(pablo.Advance(RParen), LParen | RParen) |
---|
[2560] | 204 | # literalStrings_Callouts.mask = (pscan[0] - (LParen)) | pscan[0] |
---|
[2558] | 205 | |
---|
[2560] | 206 | # literalStrings_Callouts.closed[i] = pscan[i] & RParen |
---|
[2558] | 207 | # unclosed = pscan[i] & LParen | qscan[i] & RParen |
---|
[2560] | 208 | # literalStrings_Callouts.error = pscan[i] &~ EOF_mask |
---|
| 209 | # all_closed = literalStrings_Callouts.closed[i] |
---|
[2558] | 210 | # while unclosed: |
---|
| 211 | # i += 1 |
---|
| 212 | # pscan[i] = pablo.ScanTo(pablo.Advance(unclosed & LParen), unclosed) |
---|
| 213 | # qscan[i] = pablo.ScanTo(pablo.Advance(unclosed & RParen), unclosed) |
---|
[2560] | 214 | # literalStrings_Callouts.mask|=(pscan[i]-(unclosed & LParen)) | pscan[i] |
---|
| 215 | # literalStrings_Callouts.closed[i] = pscan[i] & RParen #| qscan[i] & LParen |
---|
[2558] | 216 | # unclosed = pscan[i] & LParen | qscan[i] & RParen |
---|
[2560] | 217 | # all_closed |= literalStrings_Callouts.closed[i] |
---|
| 218 | # literalStrings_Callouts.error |= pscan[i] &~ EOF_mask #| ~pablo.atEOF(qscan[i]) |
---|
[2558] | 219 | |
---|
[2560] | 220 | # literalStrings_Callouts.error |= RParen &~ all_closed |
---|
| 221 | # def Parse_Comments(Percent, lex, comments_Callouts) : |
---|
[2558] | 222 | # end = Pablo.ScanTo(Percent, lex.EOL) |
---|
[2560] | 223 | # comments_Callouts.mask = end-Percent |
---|
| 224 | # def Parse_CommentsLiteralStrings(lex, escaped_Callouts, marker, comments_Callouts, literalStrings_Callouts) : |
---|
| 225 | # Parse_LiteralStrings(escaped_Callouts.UELParen, escaped_Callouts.UERParen, literalStrings_Callouts) |
---|
| 226 | # while (lex.Percent &~ (literalStrings_Callouts.mask | comments_Callouts.mask)) : |
---|
| 227 | # Parse_Comments(lex.Percent&~literalStrings_Callouts.mask, lex, comments_Callouts) |
---|
| 228 | # if (literalStrings_Callouts.mask & comments_Callouts.mask) : |
---|
| 229 | # Parse_LiteralStrings(escaped_Callouts.UELP &~ comments_Callouts.mask, escaped_Callouts.UERP &~ comments_Callouts.mask, literalStrings_Callouts) |
---|
[2558] | 230 | # else : |
---|
| 231 | # break |
---|
[2560] | 232 | # marker.mask = comments_Callouts.mask | literalStrings_Callouts.mask |
---|
[2558] | 233 | |
---|
| 234 | |
---|
| 235 | |
---|
[2560] | 236 | def Parse_CommentsLiteralStrings(lex, escaped_Callouts, marker, comments_Callouts, literalStrings_Callouts) : |
---|
[2558] | 237 | #not entirely sure this code is correct once compiled |
---|
| 238 | depth = 0 |
---|
| 239 | comment_starts=0 |
---|
[2560] | 240 | comment_follows=0 |
---|
| 241 | scan_characters = escaped_Callouts.UELParen |escaped_Callouts.UERParen | lex.Percent |
---|
[2558] | 242 | cursor = pablo.ScanToFirst(scan_characters) |
---|
[2560] | 243 | while cursor & EOF_mask : |
---|
[2558] | 244 | comment_cursor = cursor & lex.Percent |
---|
[2560] | 245 | string_cursor = cursor & (escaped_Callouts.UELParen|escaped_Callouts.UERParen) |
---|
[2558] | 246 | if comment_cursor : |
---|
| 247 | comment_starts |=comment_cursor |
---|
| 248 | comment_cursor = pablo.ScanTo(comment_cursor, lex.EOL) |
---|
[2560] | 249 | comment_follows |=comment_cursor |
---|
[2558] | 250 | cursor = pablo.ScanTo(pablo.Advance(comment_cursor), scan_characters) |
---|
[2560] | 251 | # |
---|
| 252 | # TODO - Add support for depth variable. |
---|
| 253 | # |
---|
| 254 | # if string_cursor : |
---|
| 255 | # if (string_cursor & escaped_Callouts.UELParen) : |
---|
| 256 | # if depth==0: |
---|
| 257 | # literalStrings_Callouts.starts|=string_cursor |
---|
| 258 | # |
---|
| 259 | # depth+=1 |
---|
| 260 | # |
---|
| 261 | # cursor = pablo.ScanTo(pablo.Advance(string_cursor), escaped_Callouts.UELParen | escaped_Callouts.UERParen) |
---|
| 262 | # if (string_cursor & escaped_Callouts.UERParen) : |
---|
| 263 | # |
---|
| 264 | # depth-=1 |
---|
| 265 | # |
---|
| 266 | # if depth==0: |
---|
| 267 | # literalStrings_Callouts.follows|=string_cursor |
---|
| 268 | # cursor = pablo.ScanTo(pablo.Advance(string_cursor), scan_characters) |
---|
| 269 | # elif depth==-1: |
---|
| 270 | # literalStrings_Callouts.error|=string_cursor |
---|
| 271 | # cursor = pablo.ScanTo(pablo.Advance(string_cursor), scan_characters) |
---|
| 272 | # else: |
---|
| 273 | # cursor = pablo.ScanTo(pablo.Advance(string_cursor), escaped_Callouts.UELParen | escaped_Callouts.UERParen) |
---|
[2558] | 274 | |
---|
| 275 | |
---|
| 276 | |
---|
| 277 | #if (depth>0): |
---|
| 278 | #error |
---|
[2560] | 279 | comments_Callouts.mask = (comment_follows - comment_starts) | comment_follows |
---|
| 280 | literalStrings_Callouts.mask = (literalStrings_Callouts.follows-literalStrings_Callouts.starts) | literalStrings_Callouts.follows |
---|
| 281 | marker.mask = comments_Callouts.mask | literalStrings_Callouts.mask |
---|
[2558] | 282 | |
---|
| 283 | |
---|
[2560] | 284 | def Parse_Names(lex, marker, names_Callouts) : |
---|
| 285 | names_Callouts.starts = pablo.Advance(lex.Slash&~marker.mask) |
---|
| 286 | names_Callouts.follows = pablo.ScanThru(names_Callouts.starts, lex.Regular) |
---|
| 287 | marker.mask |=names_Callouts.starts |
---|
[2558] | 288 | |
---|
[2560] | 289 | def Parse_HexStrings(lex, marker, hexStrings_Callouts) : |
---|
[2558] | 290 | #Haven't confirmed that this is the correct behaviour with bad input |
---|
[2560] | 291 | hexStrings_Callouts.starts = lex.LAngle &~marker.mask |
---|
| 292 | hexStrings_Callouts.follows = pablo.ScanThru(pablo.Advance(hexStrings_Callouts.starts),lex.Hex|lex.WS) |
---|
| 293 | hexStrings_Callouts.error = hexStrings_Callouts.follows &~ lex.RAngle |
---|
| 294 | marker.mask |= hexStrings_Callouts.follows - hexStrings_Callouts.starts |
---|
[2558] | 295 | |
---|
[2560] | 296 | def Parse_Numeric(lex, marker, numeric_Callouts) : |
---|
[2558] | 297 | numeric_characters = (lex.Digit | lex.Period | lex.Sign) |
---|
[2560] | 298 | numeric_Callouts.starts =(numeric_characters &~ pablo.Advance(lex.Regular)) &~ marker.mask |
---|
| 299 | numeric_Callouts.follows =pablo.ScanThru(numeric_Callouts.starts, lex.Regular) |
---|
[2558] | 300 | |
---|
[2560] | 301 | # aftersign = pablo.Advance(numeric_Callouts.starts & lex.Sign) | (numeric_Callouts.starts &~ lex.Sign) |
---|
[2558] | 302 | # temp = pablo.ScanThru(aftersign, lex.Digit) |
---|
| 303 | # afterperiod = pablo.Advance(temp & lex.Period) | (temp &~ lex.Period) |
---|
[2560] | 304 | # numeric_Callouts.follows = pablo.ScanThru(afterperiod, lex.Digit) |
---|
| 305 | # numeric_Callouts.error = numeric_Callouts.follows &~ (lex.WS | lex.Delimiter) |
---|
| 306 | # marker.mask |= numeric_Callouts.starts |
---|
[2558] | 307 | |
---|
[2560] | 308 | def Parse_Keywords(lex, marker, keywords_Callouts) : |
---|
| 309 | keywords_Callouts.starts = (lex.Regular &~ pablo.Advance(lex.Regular)) &~ marker.mask |
---|
| 310 | keywords_Callouts.follows = pablo.ScanThru(keywords_Callouts.starts, lex.Regular) |
---|
[2558] | 311 | |
---|
[2560] | 312 | def Parse_Arrays(lex, marker, arrays_Callouts) : |
---|
| 313 | arrays_Callouts.starts = lex.LBracket &~ marker.mask |
---|
| 314 | arrays_Callouts.follows = lex.RBracket &~ marker.mask |
---|
[2558] | 315 | |
---|
[2560] | 316 | def Main(basis_bits, lex, parity, escaped_Callouts): |
---|
[2558] | 317 | Classify_bytes(basis_bits, lex) |
---|
[2560] | 318 | # Generate parity odd/even streams |
---|
| 319 | Parse_Escaped(lex, parity, escaped_Callouts) |
---|
| 320 | # Parse_CommentsLiteralStrings(lex, escaped_Callouts, marker, comments_Callouts, literalStrings_Callouts) |
---|
| 321 | # Parse_Names(lex, marker, names_Callouts) |
---|
| 322 | # Parse_HexStrings(lex, marker, hexStrings_Callouts) |
---|
| 323 | # Parse_Numeric(lex, marker, numeric_Callouts) |
---|
| 324 | # Parse_Keywords(lex, marker, keywords_Callouts) |
---|
| 325 | # Parse_Arrays(lex, marker, arrays_Callouts) |
---|
[2558] | 326 | |
---|