Changeset 3064


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

Simplify parenthesis matching; fix paren match with comments.

Location:
proto/matchparens
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • proto/matchparens/parenmatch.py

    r3031 r3064  
    4141       
    4242def Match_Parens(lex, matches):
     43        parens = lex.LParen | lex.RParen
     44        i = 0
     45        pscan = pablo.AdvanceThenScanTo(lex.LParen, parens)
     46        matches.closed[0] = pscan & lex.RParen
     47        all_closed = matches.closed[0]
     48        matches.error = pablo.atEOF(pscan)
     49        # Not matched, still pending.
     50        pending_LParen = pscan & lex.LParen
     51        RParen_unmatched = lex.RParen &~ matches.closed[0]
     52        inPlay = pending_LParen | RParen_unmatched
     53        while pending_LParen:
     54                i += 1
     55                pscan = pablo.AdvanceThenScanTo(pending_LParen, inPlay)
     56                matches.closed[i] = pscan & lex.RParen
     57                all_closed |= matches.closed[i]
     58                matches.error |= pablo.atEOF(pscan)
     59                pending_LParen = pscan & lex.LParen
     60                RParen_unmatched = lex.RParen &~ all_closed
     61                inPlay = pending_LParen | RParen_unmatched
     62        #
     63        # Any closing paren that was not actually used to close
     64        # an opener is in error.
     65        matches.error |= lex.RParen &~ all_closed
     66
     67
     68def Match_Parens0(lex, matches):
    4369        parens = lex.LParen | lex.RParen
    4470        Lscan = {}
  • proto/matchparens/parenmatch2.py

    r3031 r3064  
    2424       
    2525class Matches() :
    26         closed = {}
     26        closed = 0
    2727        error = 0
    2828
     
    4242def Match_Parens(lex, matches):
    4343        parens = lex.LParen | lex.RParen
    44         Lscan = pablo.AdvanceThenScanTo(lex.LParen, parens)
    45         Rscan = pablo.AdvanceThenScanTo(lex.RParen, parens)
    46         matches.closed = Lscan & lex.RParen
    47         matches.error = pablo.atEOF(Lscan)
    48         unclosed = Lscan & lex.LParen | Rscan & lex.RParen
    49         while unclosed:
    50                 unclosedLParen = unclosed & lex.LParen
    51                 unclosedRParen = unclosed & lex.RParen
    52                 Lscan = pablo.AdvanceThenScanTo(unclosedLParen, unclosed)
    53                 Rscan = pablo.AdvanceThenScanTo(unclosedRParen, unclosed)
    54                 matches.closed |= Lscan & lex.RParen
    55                 matches.error |= pablo.atEOF(Lscan)
    56                 unclosed = Lscan & lex.LParen | Rscan & lex.RParen
     44        pscan = pablo.AdvanceThenScanTo(lex.LParen, parens)
     45        matches.closed = pscan & lex.RParen
     46        matches.error = pablo.atEOF(pscan)
     47        # Not matched, still pending.
     48        pending_LParen = pscan & lex.LParen
     49        RParen_unmatched = lex.RParen &~ matches.closed
     50        inPlay = pending_LParen | RParen_unmatched
     51        while pending_LParen:
     52                pscan = pablo.AdvanceThenScanTo(pending_LParen, inPlay)
     53                matches.closed |= pscan & lex.RParen
     54                matches.error |= pablo.atEOF(pscan)
     55                pending_LParen = pscan & lex.LParen
     56                RParen_unmatched = lex.RParen &~ matches.closed
     57                inPlay = pending_LParen | RParen_unmatched
    5758        #
    5859        # Any closing paren that was not actually used to close
    5960        # an opener is in error.
    6061        matches.error |= lex.RParen &~ matches.closed
    61 
    6262
    6363basis_bits = Basis_bits()
  • proto/matchparens/pdfparenmatch.pablo

    r3030 r3064  
    4141def Match_Parens(lex, matches):
    4242        parens = lex.LParen | lex.RParen
    43         Lscan = pablo.AdvanceThenScanTo(lex.LParen, parens)
    44         Rscan = pablo.AdvanceThenScanTo(lex.RParen, parens)
    45         matches.instring = pablo.ExclusiveSpan(lex.LParen, Lscan)
    46         matches.closed = Lscan & lex.RParen
    47         matches.error = pablo.atEOF(Lscan)
    48         unclosed = Lscan & lex.LParen | Rscan & lex.RParen
    49         while unclosed:
    50                 unclosedLParen = unclosed & lex.LParen
    51                 unclosedRParen = unclosed & lex.RParen
    52                 Lscan = pablo.AdvanceThenScanTo(unclosedLParen, unclosed)
    53                 Rscan = pablo.AdvanceThenScanTo(unclosedRParen, unclosed)
    54                 matches.instring |= pablo.SpanUpTo(unclosedLParen, Lscan)
    55                 matches.closed |= Lscan & lex.RParen
    56                 matches.error |= pablo.atEOF(Lscan)
    57                 unclosed = Lscan & lex.LParen | Rscan & lex.RParen
     43        pscan = pablo.AdvanceThenScanTo(lex.LParen, parens)
     44        matches.closed = pscan & lex.RParen
     45        matches.instring = pablo.ExclusiveSpan(lex.LParen, pscan)
     46        matches.error = pablo.atEOF(pscan)
     47        # Not matched, still pending.
     48        pending_LParen = pscan & lex.LParen
     49        RParen_unmatched = lex.RParen &~ matches.closed
     50        inPlay = pending_LParen | RParen_unmatched
     51        while pending_LParen:
     52                pscan = pablo.AdvanceThenScanTo(pending_LParen, inPlay)
     53                matches.instring |= pablo.SpanUpTo(pending_LParen, pscan)
     54                matches.closed |= pscan & lex.RParen
     55                matches.error |= pablo.atEOF(pscan)
     56                pending_LParen = pscan & lex.LParen
     57                RParen_unmatched = lex.RParen &~ matches.closed
     58                inPlay = pending_LParen | RParen_unmatched
    5859        #
    5960        # Any closing paren that was not actually used to close
    6061        # an opener is in error.
    61         matches.error = matches.error | (lex.RParen &~ matches.closed)
    62 
     62        matches.error |= lex.RParen &~ matches.closed
    6363
    6464def Main(basis_bits, lex, matches):
  • proto/matchparens/pdfparenmatch.py

    r3030 r3064  
    4343def Match_Parens(lex, matches):
    4444        parens = lex.LParen | lex.RParen
    45         Lscan = pablo.AdvanceThenScanTo(lex.LParen, parens)
    46         Rscan = pablo.AdvanceThenScanTo(lex.RParen, parens)
    47         matches.instring = pablo.ExclusiveSpan(lex.LParen, Lscan)
    48         matches.closed = Lscan & lex.RParen
    49         matches.error = pablo.atEOF(Lscan)
    50         unclosed = Lscan & lex.LParen | Rscan & lex.RParen
    51         while unclosed:
    52                 unclosedLParen = unclosed & lex.LParen
    53                 unclosedRParen = unclosed & lex.RParen
    54                 Lscan = pablo.AdvanceThenScanTo(unclosedLParen, unclosed)
    55                 Rscan = pablo.AdvanceThenScanTo(unclosedRParen, unclosed)
    56                 matches.instring |= pablo.SpanUpTo(unclosedLParen, Lscan)
    57                 matches.closed |= Lscan & lex.RParen
    58                 matches.error |= pablo.atEOF(Lscan)
    59                 unclosed = Lscan & lex.LParen | Rscan & lex.RParen
     45        pscan = pablo.AdvanceThenScanTo(lex.LParen, parens)
     46        matches.closed = pscan & lex.RParen
     47        matches.instring = pablo.ExclusiveSpan(lex.LParen, pscan)
     48        matches.error = pablo.atEOF(pscan)
     49        # Not matched, still pending.
     50        pending_LParen = pscan & lex.LParen
     51        RParen_unmatched = lex.RParen &~ matches.closed
     52        inPlay = pending_LParen | RParen_unmatched
     53        while pending_LParen:
     54                pscan = pablo.AdvanceThenScanTo(pending_LParen, inPlay)
     55                matches.instring |= pablo.SpanUpTo(pending_LParen, pscan)
     56                matches.closed |= pscan & lex.RParen
     57                matches.error |= pablo.atEOF(pscan)
     58                pending_LParen = pscan & lex.LParen
     59                RParen_unmatched = lex.RParen &~ matches.closed
     60                inPlay = pending_LParen | RParen_unmatched
    6061        #
    6162        # Any closing paren that was not actually used to close
    6263        # an opener is in error.
    63         matches.error = matches.error | (lex.RParen &~ matches.closed)
    64 
     64        matches.error |= lex.RParen &~ matches.closed
    6565
    6666
  • 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.