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/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()
Note: See TracChangeset for help on using the changeset viewer.