#
# Recursive Parenthesis Matching
#
#
# Robert D. Cameron
# October 14, 2012
#
class Basis_bits():
bit_0 = 0
bit_1 = 0
bit_2 = 0
bit_3 = 0
bit_4 = 0
bit_5 = 0
bit_6 = 0
bit_7 = 0
class Lex ():
LParen = 0
RParen = 0
class Matches() :
closed = 0
instring = 0
error = 0
def Classify_bytes(basis_bits, lex):
temp1 = (basis_bits.bit_0 | basis_bits.bit_1)
temp2 = (basis_bits.bit_2 &~ basis_bits.bit_3)
temp3 = (temp2 &~ temp1)
temp4 = (basis_bits.bit_4 &~ basis_bits.bit_5)
temp5 = (basis_bits.bit_6 | basis_bits.bit_7)
temp6 = (temp4 &~ temp5)
lex.LParen = (temp3 & temp6)
temp7 = (basis_bits.bit_7 &~ basis_bits.bit_6)
temp8 = (temp4 & temp7)
lex.RParen = (temp3 & temp8)
def Match_Parens(lex, matches):
parens = lex.LParen | lex.RParen
Lscan = pablo.AdvanceThenScanTo(lex.LParen, parens)
Rscan = pablo.AdvanceThenScanTo(lex.RParen, parens)
matches.instring = pablo.ExclusiveSpan(lex.LParen, Lscan)
matches.closed = Lscan & lex.RParen
matches.error = pablo.atEOF(Lscan)
unclosed = Lscan & lex.LParen | Rscan & lex.RParen
while unclosed:
unclosedLParen = unclosed & lex.LParen
unclosedRParen = unclosed & lex.RParen
Lscan = pablo.AdvanceThenScanTo(unclosedLParen, unclosed)
Rscan = pablo.AdvanceThenScanTo(unclosedRParen, unclosed)
matches.instring |= pablo.SpanUpTo(unclosedLParen, Lscan)
matches.closed |= Lscan & lex.RParen
matches.error |= pablo.atEOF(Lscan)
unclosed = Lscan & lex.LParen | Rscan & lex.RParen
#
# Any closing paren that was not actually used to close
# an opener is in error.
matches.error = matches.error | (lex.RParen &~ matches.closed)
def Main(basis_bits, lex, matches):
Classify_bytes(basis_bits, lex)
Match_Parens(lex, matches)