Ignore:
Timestamp:
Apr 22, 2013, 10:42:23 AM (6 years ago)
Author:
cameron
Message:

Simplify parenthesis matching; fix paren match with comments.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • proto/matchparens/pdfparenmatch2.py

    r3062 r3064  
    2626        LParen = 0
    2727        RParen = 0
    28         Pct = 0
    29         LF = 0
     28        Percent = 0
     29        EOL = 0
    3030       
    3131class Matches() :
     
    6161#
    6262# Let pending_LParen be left parentheses for which we have a pending
    63 #   obligation to find the corresponding right parentheses.
     63#   obligation to find the corresponding right parentheses. 
    6464#
    6565# Let pending_Pct be Percent marks that have not yet been
    6666#   ruled out as comment opening delimiters
     67#
     68#
    6769def Match_Parens_With_Comments(lex, matches):
     70        matches.instring = 0
     71        matches.closed = 0
     72        matches.error = 0
    6873        line_starts = ~pablo.Advance(~lex.EOL)
    6974        line_ends1 = pablo.ScanTo(line_starts, lex.EOL | lex.Percent)
    7075        pending_Pct = line_ends1 & lex.Percent
    71         outside_Ct = pablo.SpanUpTo(line_starts, line_ends1)
    72         pending_LParen = outside_Ct & lex.LParen
    73         RParen_unmatched = lex.RParen
     76        known_outside_Ct = pablo.SpanUpTo(line_starts, line_ends1)
     77        pending_LParen = known_outside_Ct & lex.LParen
     78        print "pending_LParen0" + " "*(16-15) + pablo.bitstream2string(pending_LParen,110)
     79        unmatched_RParen = lex.RParen
    7480
    75         inPlay = pending_LParen | RParen_unmatched | pending_Pct
     81        inPlay = pending_LParen | unmatched_RParen | pending_Pct
    7682
    7783        while pending_LParen:
     
    8288                matches.instring |= pablo.ExclusiveSpan(pending_LParen, pscan) | pscan &~ lex.RParen
    8389                matches.closed |= pscan & lex.RParen
     90                print "matches.closed" + " "*(16-14) + pablo.bitstream2string(matches.closed,110)
    8491                matches.error |= pablo.atEOF(pscan)
    85                 pending_LParen = pablo.inFile(pscan & ~lex.RParen)
     92                pending_LParen = pscan & lex.LParen
     93                print "pending_LParen1" + " "*(16-15) + pablo.bitstream2string(pending_LParen,110)
    8694                # Did we scan into a pending comment region?
    8795                pct_found = pscan & pending_Pct
    8896               
    8997                if pct_found:
     98                        # The scan from the "(" was terminated prematurely by the "%" mark.
     99                        # We include this position in pending_LParen so that the scan can
     100                        # continue next time around.
     101                        pending_LParen |= pct_found
     102                        print "pct_found:" + " "*(16-10) + pablo.bitstream2string(pct_found,110)
     103                        print "pending_LParen2" + " "*(16-15) + pablo.bitstream2string(pending_LParen,110)
     104                        # Clear this % position as a line terminator, and find the next.
     105                        # Determine the region that was previously identified as potentially
     106                        # inside a comment and mark it as outside.
    90107                        line_ends1 &= ~pct_found
    91108                        line_ends1 |= pablo.AdvanceThenScanTo(pct_found, lex.EOL | lex.Percent)
    92                         pending_Pct = line_ends1 & lex.Percent
    93                         new_outside = pablo.SpanUpTo(pct_found, line_ends1)
    94                         pending_LParen |= new_outside & lex.LParen
    95                         outside_Ct |= new_outside
    96 
    97                 RParen_unmatched = lex.RParen &~ matches.closed                         
    98                 inPlay = pending_LParen | RParen_unmatched | pending_Pct
     109                        # Add any new potential comment delimiter to the pending ones.
     110                        pending_Pct |= line_ends1 & lex.Percent
     111                        newly_outside = pablo.SpanUpTo(pct_found, line_ends1)
     112                        known_outside_Ct |= newly_outside
     113                        # If any LParens have been revealed, add scan obligations for them
     114                        pending_LParen |= newly_outside & lex.LParen
     115                        print "pending_LParen3" + " "*(16-15) + pablo.bitstream2string(pending_LParen,110)
     116               
     117                unmatched_RParen = lex.RParen &~ matches.closed                         
     118                inPlay = pending_LParen | unmatched_RParen | pending_Pct
    99119        #
    100120        # No more scans to do.  Any pending pct marks are now known
    101121        # as definite comment delimiters.
    102         matches.comment_start = pending_Pct
    103         matches.comment_end = pablo.ScanTo(pending_Pct, lex.EOL)
     122        matches.comment_start = line_ends1 & lex.Percent
     123        matches.comment_end = pablo.ScanTo(matches.comment_start, lex.EOL)
    104124        #
    105125        # Any closing paren that was not actually used to close
    106126        # an opener is in error.
    107127        matches.error |= lex.RParen &~ matches.closed &~ pablo.SpanUpTo(matches.comment_start, matches.comment_end)
    108 
    109 
    110128
    111129basis_bits = Basis_bits()
Note: See TracChangeset for help on using the changeset viewer.