Changeset 2560 for proto/PDF/pdf.py


Ignore:
Timestamp:
Oct 25, 2012, 4:57:33 PM (7 years ago)
Author:
ksherdy
Message:

Updated PDF demo to the point of g++ compile without compiler errors.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • proto/PDF/pdf.py

    r2558 r2560  
    1212        bit_7 = 0
    1313
     14class Parity():
     15        odd = 0
     16        even = 0
     17
    1418class Lex ():
    1519        LParen = 0
     
    1721        Backslash = 0
    1822        Percent = 0
    19         Slash=0
    20         Regular=0
    21         Delimiter=0
    22         LAngle=0
    23         RAngle=0
    24         Hex=0
    25         WS=0
    26         Digit=0
    27         Sign=0
    28         Period=0
    29         EOL=0
    30         RBracket=0
    31         LBracket=0
     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
    3236       
    3337class Escaped_Callouts ():
     
    3640
    3741class Comments_Callouts() :
    38         mask=0
     42        mask = 0
     43
    3944class LiteralStrings_Callouts() :
    40         starts=0
    41         ends=0
    42         mask=0
     45        starts = 0
     46        follows = 0
     47        mask = 0
    4348        error = 0
    4449
     
    4853class Names_Callouts() :
    4954        starts = 0
    50         ends = 0
     55        follows = 0
    5156
    5257class HexStrings_Callouts() :
    5358        starts = 0
    54         ends = 0
     59        follows = 0
    5560        error = 0
    5661
     
    6267class Keywords_Callouts() :
    6368        starts = 0
    64         ends = 0
     69        follows = 0
    6570       
    6671class Arrays_Callouts() :
    6772        starts = 0
    68         ends = 0
    69 
     73        follows = 0
    7074
    7175def Classify_bytes(basis_bits, lex): 
     
    159163        lex.Regular = ~lex.Delimiter&~lex.WS
    160164
    161 
    162 def GetEven(input):
    163         #ugly temporary version
    164         even = 2
    165         while (even <=input*2):
    166                         even = even * 4 + 2
    167         return even
    168 
    169 def GetOdd(input):
    170         #ugly temporary version
    171         odd = 1
    172         while (odd <=input*2):
    173                         odd = odd * 4 + 1
    174         return odd
    175 
    176 
    177 def Parse_Escaped(lex, Escaped_Callouts):
    178         odd = GetOdd(lex.Backslash)
    179         even = GetEven(lex.Backslash)
    180                    
     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
     170
     171
     172def Parse_Escaped(lex, parity, escaped_Callouts):
     173
     174  # Demo Only
     175  #
     176        # odd = simd_const_4('a',EOF_mask) 
     177        # even = simd_const_4('5',EOF_mask)
     178
     179        odd = parity.odd
     180        even = parity.even
     181   
    181182        start = lex.Backslash &~ pablo.Advance(lex.Backslash)
    182183        even_start = start & even
     
    189190        escape = escape | (odd_final & even)
    190191                   
    191         Escaped_Callouts.UELParen = lex.LParen &~ escape
    192         Escaped_Callouts.UERParen = lex.RParen &~ escape
    193 
    194                
     192        escaped_Callouts.UELParen = lex.LParen &~ escape
     193        escaped_Callouts.UERParen = lex.RParen &~ escape
     194       
    195195# Parallel version
    196 # def Parse_LiteralStrings(LParen, RParen, LiteralStrings_Callouts):
     196# def Parse_LiteralStrings(LParen, RParen, literalStrings_Callouts):
    197197        # unmatched = RParen
    198198        # pscan = {}
    199199        # qscan = {}
    200         # LiteralStrings_Callouts.closed={}
     200        # literalStrings_Callouts.closed={}
    201201        # i = 0
    202202        # pscan[0] = pablo.ScanTo(pablo.Advance(LParen), LParen | RParen)
    203203        # qscan[0] = pablo.ScanTo(pablo.Advance(RParen), LParen | RParen)
    204         # LiteralStrings_Callouts.mask = (pscan[0] - (LParen)) | pscan[0]
    205        
    206         # LiteralStrings_Callouts.closed[i] = pscan[i] & RParen
     204        # literalStrings_Callouts.mask = (pscan[0] - (LParen)) | pscan[0]
     205       
     206        # literalStrings_Callouts.closed[i] = pscan[i] & RParen
    207207        # unclosed = pscan[i] & LParen | qscan[i] & RParen
    208         # LiteralStrings_Callouts.error = pscan[i] &~ pablo.EOF_mask
    209         # all_closed = LiteralStrings_Callouts.closed[i]
     208        # literalStrings_Callouts.error = pscan[i] &~ EOF_mask
     209        # all_closed = literalStrings_Callouts.closed[i]
    210210        # while unclosed:
    211211                # i += 1
    212212                # pscan[i] = pablo.ScanTo(pablo.Advance(unclosed & LParen), unclosed)
    213213                # qscan[i] = pablo.ScanTo(pablo.Advance(unclosed & RParen), unclosed)
    214                 # LiteralStrings_Callouts.mask|=(pscan[i]-(unclosed & LParen)) | pscan[i]
    215                 # LiteralStrings_Callouts.closed[i] = pscan[i] & RParen #| qscan[i] & LParen
     214                # literalStrings_Callouts.mask|=(pscan[i]-(unclosed & LParen)) | pscan[i]
     215                # literalStrings_Callouts.closed[i] = pscan[i] & RParen #| qscan[i] & LParen
    216216                # unclosed = pscan[i] & LParen | qscan[i] & RParen
    217                 # all_closed |= LiteralStrings_Callouts.closed[i]
    218                 # LiteralStrings_Callouts.error |= pscan[i] &~ pablo.EOF_mask #| ~pablo.atEOF(qscan[i])
    219        
    220         # LiteralStrings_Callouts.error |= RParen &~ all_closed
    221 # def Parse_Comments(Percent, lex, Comments_Callouts) :
     217                # all_closed |= literalStrings_Callouts.closed[i]
     218                # literalStrings_Callouts.error |= pscan[i] &~ EOF_mask #| ~pablo.atEOF(qscan[i])
     219       
     220        # literalStrings_Callouts.error |= RParen &~ all_closed
     221# def Parse_Comments(Percent, lex, comments_Callouts) :
    222222        # end = Pablo.ScanTo(Percent, lex.EOL)
    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)
     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)
    230230                # else :
    231231                        # break
    232         # Marker.mask = Comments_Callouts.mask | LiteralStrings_Callouts.mask
    233 
    234 
    235 
    236 def Parse_CommentsLiteralStrings(lex, Escaped_Callouts, Marker, Comments_Callouts, LiteralStrings_Callouts) :
     232        # marker.mask = comments_Callouts.mask | literalStrings_Callouts.mask
     233
     234
     235
     236def Parse_CommentsLiteralStrings(lex, escaped_Callouts, marker, comments_Callouts, literalStrings_Callouts) :
    237237        #not entirely sure this code is correct once compiled
    238238        depth = 0
    239239        comment_starts=0
    240         comment_ends=0
    241         scan_characters = Escaped_Callouts.UELParen |Escaped_Callouts.UERParen | lex.Percent
     240        comment_follows=0
     241        scan_characters = escaped_Callouts.UELParen |escaped_Callouts.UERParen | lex.Percent
    242242        cursor = pablo.ScanToFirst(scan_characters)
    243         while cursor & pablo.EOF_mask :
     243        while cursor & EOF_mask :
    244244                comment_cursor = cursor & lex.Percent
    245                 string_cursor = cursor & (Escaped_Callouts.UELParen|Escaped_Callouts.UERParen)
     245                string_cursor = cursor & (escaped_Callouts.UELParen|escaped_Callouts.UERParen)
    246246                if comment_cursor :
    247247                        comment_starts |=comment_cursor
    248248                        comment_cursor = pablo.ScanTo(comment_cursor, lex.EOL)
    249                         comment_ends |=comment_cursor
     249                        comment_follows |=comment_cursor
    250250                        cursor = pablo.ScanTo(pablo.Advance(comment_cursor), scan_characters)
    251                
    252                 if string_cursor :
    253                         if (string_cursor & Escaped_Callouts.UELParen) :
    254                                 if depth==0:
    255                                         LiteralStrings_Callouts.starts|=string_cursor
    256                                 depth+=1
    257                                 cursor = pablo.ScanTo(pablo.Advance(string_cursor), Escaped_Callouts.UELParen | Escaped_Callouts.UERParen)
    258                         if (string_cursor & Escaped_Callouts.UERParen) :
    259                                 depth-=1
    260                                 if depth==0:
    261                                         LiteralStrings_Callouts.ends|=string_cursor
    262                                         cursor = pablo.ScanTo(pablo.Advance(string_cursor), scan_characters)
    263                                 elif depth==-1:
    264                                         LiteralStrings_Callouts.error|=string_cursor
    265                                         cursor = pablo.ScanTo(pablo.Advance(string_cursor), scan_characters)
    266                                 else:
    267                                         cursor = pablo.ScanTo(pablo.Advance(string_cursor), Escaped_Callouts.UELParen | Escaped_Callouts.UERParen)
     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)
    268274                                       
    269275               
     
    271277        #if (depth>0):
    272278                #error
    273         Comments_Callouts.mask = (comment_ends - comment_starts) | comment_ends
    274         LiteralStrings_Callouts.mask = (LiteralStrings_Callouts.ends-LiteralStrings_Callouts.starts) | LiteralStrings_Callouts.ends
    275         Marker.mask = Comments_Callouts.mask | LiteralStrings_Callouts.mask
    276 
    277 
    278 def Parse_Names(lex, Marker, Names_Callouts) :
    279         Names_Callouts.starts = pablo.Advance(lex.Slash&~Marker.mask)
    280         Names_Callouts.follows = pablo.ScanThru(Names_Callouts.starts, lex.Regular)
    281         Marker.mask |=Names_Callouts.starts
    282 
    283 def Parse_HexStrings(lex, Marker, HexStrings_Callouts) :
     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
     282
     283
     284def 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
     288
     289def Parse_HexStrings(lex, marker, hexStrings_Callouts) :
    284290        #Haven't confirmed that this is the correct behaviour with bad input
    285         HexStrings_Callouts.starts = lex.LAngle &~Marker.mask
    286         HexStrings_Callouts.ends = pablo.ScanThru(pablo.Advance(HexStrings_Callouts.starts),lex.Hex|lex.WS)
    287         HexStrings_Callouts.error = HexStrings_Callouts.ends &~ lex.RAngle
    288         Marker.mask |= HexStrings_Callouts.ends - HexStrings_Callouts.starts
    289 
    290 def Parse_Numeric(lex, Marker, Numeric_Callouts) :
     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
     295
     296def Parse_Numeric(lex, marker, numeric_Callouts) :
    291297        numeric_characters = (lex.Digit | lex.Period | lex.Sign)
    292         Numeric_Callouts.starts =(numeric_characters &~ pablo.Advance(lex.Regular)) &~ Marker.mask
    293         Numeric_Callouts.follows =pablo.ScanThru(Numeric_Callouts.starts, lex.Regular)
    294        
    295         # aftersign = pablo.Advance(Numeric_Callouts.starts & lex.Sign) | (Numeric_Callouts.starts &~ lex.Sign)
     298        numeric_Callouts.starts =(numeric_characters &~ pablo.Advance(lex.Regular)) &~ marker.mask
     299        numeric_Callouts.follows =pablo.ScanThru(numeric_Callouts.starts, lex.Regular)
     300       
     301        # aftersign = pablo.Advance(numeric_Callouts.starts & lex.Sign) | (numeric_Callouts.starts &~ lex.Sign)
    296302        # temp = pablo.ScanThru(aftersign, lex.Digit)
    297303        # afterperiod = pablo.Advance(temp & lex.Period) | (temp &~ lex.Period)
    298         # Numeric_Callouts.ends = pablo.ScanThru(afterperiod, lex.Digit)
    299         # Numeric_Callouts.error = Numeric_Callouts.ends &~ (lex.WS | lex.Delimiter)
    300         # Marker.mask |= Numeric_Callouts.starts
    301 
    302 def Parse_Keywords(lex, Marker, Keywords_Callouts) :
    303         Keywords_Callouts.starts = (lex.Regular &~ pablo.Advance(lex.Regular)) &~ Marker.mask
    304         Keywords_Callouts.ends = pablo.ScanThru(Keywords_Callouts.starts, lex.Regular)
    305        
    306 def Parse_Arrays(lex, Marker, Arrays_Callouts) :
    307         Arrays_Callouts.starts = lex.LBracket &~ Marker.mask
    308         Arrays_Callouts.ends = lex.RBracket &~ Marker.mask
     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
     307
     308def 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)
     311       
     312def Parse_Arrays(lex, marker, arrays_Callouts) :
     313        arrays_Callouts.starts = lex.LBracket &~ marker.mask
     314        arrays_Callouts.follows = lex.RBracket &~ marker.mask
    309315       
    310 basis_bits = Basis_bits()
    311 lex = Lex()
    312 LiteralStrings_Callouts = LiteralStrings_Callouts()
    313 Escaped_Callouts = Escaped_Callouts()
    314 Comments_Callouts = Comments_Callouts()
    315 Names_Callouts = Names_Callouts()
    316 HexStrings_Callouts = HexStrings_Callouts()
    317 Numeric_Callouts = Numeric_Callouts()
    318 Keywords_Callouts = Keywords_Callouts()
    319 Arrays_Callouts = Arrays_Callouts()
    320 Marker = Marker()
    321 
    322 if __name__ == "__main__":
    323         if len(sys.argv) > 1:
    324                 u8data = pablo.readfile(sys.argv[1])
    325                 lgth = len(u8data)
    326                 pablo.EOF_mask = pablo.transpose_streams(u8data, basis_bits)
     316def Main(basis_bits, lex, parity, escaped_Callouts):
    327317                Classify_bytes(basis_bits, lex)
    328                 Parse_Escaped(lex, Escaped_Callouts)
    329                 Parse_CommentsLiteralStrings(lex, Escaped_Callouts, Marker, Comments_Callouts, LiteralStrings_Callouts)
    330                 Parse_Names(lex, Marker, Names_Callouts)
    331                 Parse_HexStrings(lex, Marker, HexStrings_Callouts)
    332                 Parse_Numeric(lex, Marker, Numeric_Callouts)
    333                 Parse_Keywords(lex, Marker, Keywords_Callouts)
    334                 Parse_Arrays(lex, Marker, Arrays_Callouts)
    335                
    336                 print "data:" + " "*(16-5) + u8data
    337                 print "literal mask" + " "*(16-12) + pablo.bitstream2string(LiteralStrings_Callouts.mask, lgth+1)
    338                 print "literal starts" + " "*(16-14) + pablo.bitstream2string(LiteralStrings_Callouts.starts, lgth+1)
    339                 print "comment mask"+ " "*(16-12) + pablo.bitstream2string(Comments_Callouts.mask, lgth+1)
    340                 print "names starts"+ " "*(16-12) + pablo.bitstream2string(Names_Callouts.starts, lgth+1)
    341                 print "names follows"+ " "*(16-13) + pablo.bitstream2string(Names_Callouts.follows, lgth+1)
    342                 print "hex starts"+ " "*(16-10) + pablo.bitstream2string(HexStrings_Callouts.starts, lgth+1)
    343                 print "hex ends"+ " "*(16-8) + pablo.bitstream2string(HexStrings_Callouts.ends, lgth+1)
    344                 print "num starts"+ " "*(16-10) + pablo.bitstream2string(Numeric_Callouts.starts, lgth+1)
    345                 print "num follows"+ " "*(16-11) + pablo.bitstream2string(Numeric_Callouts.follows, lgth+1)
    346                 print "key starts"+ " "*(16-10) + pablo.bitstream2string(Keywords_Callouts.starts, lgth+1)
    347                 print "key ends"+ " "*(16-8) + pablo.bitstream2string(Keywords_Callouts.ends, lgth+1)
    348                 print "array starts"+ " "*(16-12) + pablo.bitstream2string(Arrays_Callouts.starts, lgth+1)
    349                 print "array ends"+ " "*(16-10) + pablo.bitstream2string(Arrays_Callouts.ends, lgth+1)
    350                 print "regular"+ " "*(16-7) + pablo.bitstream2string(lex.Regular, lgth+1)
    351                
    352 
    353                
    354                 print "errors" + " "*(16-6) + pablo.bitstream2string(LiteralStrings_Callouts.error|HexStrings_Callouts.error | Numeric_Callouts.error, lgth+1)
    355                
    356         else:
    357                 print("Usage: python pdf.py <file>")
    358        
    359 
     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)
     326       
Note: See TracChangeset for help on using the changeset viewer.