@@ -4208,6 +4208,16 @@ def testUpcaseDowncaseUnicode(self):
42084208 + td_end .suppress ()
42094209 )
42104210
4211+ def testRegexDeferredCompile (self ):
4212+ """test deferred compilation of Regex patterns"""
4213+ re_expr = pp .Regex (r"[A-Z]*" )
4214+ self .assertFalse (re_expr .mayReturnEmpty , "failed to initialize mayReturnEmpty flag to False" )
4215+ self .assertEqual (re_expr ._re , None )
4216+
4217+ compiled = re_expr .re
4218+ self .assertTrue (re_expr .mayReturnEmpty , "failed to set mayReturnEmpty flag to True" )
4219+ self .assertEqual (re_expr ._re , compiled )
4220+
42114221 def testParseUsingRegex (self ):
42124222 signedInt = pp .Regex (r"[-+][0-9]+" )
42134223 unsignedInt = pp .Regex (r"[0-9]+" )
@@ -4225,6 +4235,7 @@ def testMatch(expression, instring, shouldPass, expectedString=None):
42254235 print (
42264236 f"\t produced { repr (result [0 ])} instead of { repr (expectedString )} "
42274237 )
4238+ return False
42284239 return True
42294240 except pp .ParseException :
42304241 print (f"{ expression !r} incorrectly failed to match { instring !r} " )
@@ -4239,73 +4250,56 @@ def testMatch(expression, instring, shouldPass, expectedString=None):
42394250 return False
42404251
42414252 # These should fail
4242- self .assertTrue (
4243- testMatch (signedInt , "1234 foo" , False ), "Re: (1) passed, expected fail"
4244- )
4245- self .assertTrue (
4246- testMatch (signedInt , " +foo" , False ), "Re: (2) passed, expected fail"
4247- )
4248- self .assertTrue (
4249- testMatch (unsignedInt , "abc" , False ), "Re: (3) passed, expected fail"
4250- )
4251- self .assertTrue (
4252- testMatch (unsignedInt , "+123 foo" , False ), "Re: (4) passed, expected fail"
4253- )
4254- self .assertTrue (
4255- testMatch (simpleString , "foo" , False ), "Re: (5) passed, expected fail"
4256- )
4257- self .assertTrue (
4258- testMatch (simpleString , "\" foo bar'" , False ),
4259- "Re: (6) passed, expected fail" ,
4260- )
4261- self .assertTrue (
4262- testMatch (simpleString , "'foo bar\" " , False ),
4263- "Re: (7) passed, expected fail" ,
4264- )
4253+ for i , (test_expr , test_string ) in enumerate (
4254+ [
4255+ (signedInt , "1234 foo" ),
4256+ (signedInt , " +foo" ),
4257+ (unsignedInt , "abc" ),
4258+ (unsignedInt , "+123 foo" ),
4259+ (simpleString , "foo" ),
4260+ (simpleString , "\" foo bar'" ),
4261+ (simpleString , "'foo bar\" " ),
4262+ (compiledRE , "blah" ),
4263+ ],
4264+ start = 1
4265+ ):
4266+ with self .subTest (test_expr = test_expr , test_string = test_string ):
4267+ self .assertTrue (
4268+ testMatch (
4269+ test_expr ,
4270+ test_string ,
4271+ False ,
4272+ ),
4273+ f"Re: ({ i } ) passed, expected fail" ,
4274+ )
42654275
42664276 # These should pass
4267- self .assertTrue (
4268- testMatch (signedInt , " +123" , True , "+123" ),
4269- "Re: (8) failed, expected pass" ,
4270- )
4271- self .assertTrue (
4272- testMatch (signedInt , "+123" , True , "+123" ), "Re: (9) failed, expected pass"
4273- )
4274- self .assertTrue (
4275- testMatch (signedInt , "+123 foo" , True , "+123" ),
4276- "Re: (10) failed, expected pass" ,
4277- )
4278- self .assertTrue (
4279- testMatch (signedInt , "-0 foo" , True , "-0" ), "Re: (11) failed, expected pass"
4280- )
4281- self .assertTrue (
4282- testMatch (unsignedInt , "123 foo" , True , "123" ),
4283- "Re: (12) failed, expected pass" ,
4284- )
4285- self .assertTrue (
4286- testMatch (unsignedInt , "0 foo" , True , "0" ), "Re: (13) failed, expected pass"
4287- )
4288- self .assertTrue (
4289- testMatch (simpleString , '"foo"' , True , '"foo"' ),
4290- "Re: (14) failed, expected pass" ,
4291- )
4292- self .assertTrue (
4293- testMatch (simpleString , "'foo bar' baz" , True , "'foo bar'" ),
4294- "Re: (15) failed, expected pass" ,
4295- )
4296-
4297- self .assertTrue (
4298- testMatch (compiledRE , "blah" , False ), "Re: (16) passed, expected fail"
4299- )
4300- self .assertTrue (
4301- testMatch (compiledRE , "BLAH" , True , "BLAH" ),
4302- "Re: (17) failed, expected pass" ,
4303- )
4277+ for i , (test_expr , test_string , expected_match ) in enumerate (
4278+ [
4279+ (signedInt , " +123" , "+123" ),
4280+ (signedInt , "+123" , "+123" ),
4281+ (signedInt , "+123 foo" , "+123" ),
4282+ (signedInt , "-0 foo" , "-0" ),
4283+ (unsignedInt , "123 foo" , "123" ),
4284+ (unsignedInt , "0 foo" , "0" ),
4285+ (simpleString , '"foo"' , '"foo"' ),
4286+ (simpleString , "'foo bar' baz" , "'foo bar'" ),
4287+ (compiledRE , "BLAH" , "BLAH" ),
4288+ (namedGrouping , '"foo bar" baz' , '"foo bar"' ),
4289+ ],
4290+ start = i + 1
4291+ ):
4292+ with self .subTest (test_expr = test_expr , test_string = test_string ):
4293+ self .assertTrue (
4294+ testMatch (
4295+ test_expr ,
4296+ test_string ,
4297+ True ,
4298+ expected_match ,
4299+ ),
4300+ f"Re: ({ i } ) failed, expected pass" ,
4301+ )
43044302
4305- self .assertTrue (
4306- testMatch (namedGrouping , '"foo bar" baz' , True , '"foo bar"' ),
4307- "Re: (16) failed, expected pass" ,
4308- )
43094303 ret = namedGrouping .parseString ('"zork" blah' , parseAll = False )
43104304 print (ret )
43114305 print (list (ret .items ()))
@@ -4330,7 +4324,7 @@ def testMatch(expression, instring, shouldPass, expectedString=None):
43304324 with self .assertRaises (
43314325 ValueError , msg = "failed to warn empty string passed to Regex"
43324326 ):
4333- pp .Regex ("" ).re
4327+ pp .Regex ("" ).re # noqa
43344328
43354329 def testRegexAsType (self ):
43364330 test_str = "sldkjfj 123 456 lsdfkj"
@@ -4426,6 +4420,13 @@ def testRegexInvalidType(self):
44264420 with self .assertRaises (TypeError , msg = "issue with Regex of type int" ):
44274421 expr = pp .Regex (12 )
44284422
4423+ def testRegexLoopPastEndOfString (self ):
4424+ """test Regex matching after end of string"""
4425+ NL = pp .LineEnd ().suppress ()
4426+ empty_line = pp .rest_of_line () + NL
4427+ result = empty_line [1 , 10 ].parse_string ("\n \n " )
4428+ self .assertEqual (3 , len (result ))
4429+
44294430 def testPrecededBy (self ):
44304431 num = pp .Word (pp .nums ).setParseAction (lambda t : int (t [0 ]))
44314432 interesting_num = pp .PrecededBy (pp .Char ("abc" )("prefix*" )) + num
0 commit comments