@@ -437,6 +437,13 @@ def parseIntegratedTestScript(test, normalize_slashes=False,
437
437
('%/T' , tmpDir .replace ('\\ ' , '/' )),
438
438
])
439
439
440
+ # re for %if
441
+ re_cond_end = re .compile ('%{' )
442
+ re_if = re .compile ('(.*?)(?:%if)' )
443
+ re_nested_if = re .compile ('(.*?)(?:%if|%})' )
444
+ re_else = re .compile ('^\s*%else\s*(%{)?' )
445
+
446
+
440
447
# Collect the test lines from the script.
441
448
script = []
442
449
requires = []
@@ -475,11 +482,104 @@ def replace_line_number(match):
475
482
raise ValueError ("unknown script command type: %r" % (
476
483
command_type ,))
477
484
485
+
486
+ def substituteIfElse (ln ):
487
+ # early exit to avoid wasting time on lines without
488
+ # conditional substitutions
489
+ if ln .find ('%if ' ) == - 1 :
490
+ return ln
491
+
492
+ def tryParseIfCond (ln ):
493
+ # space is important to not conflict with other (possible)
494
+ # substitutions
495
+ if not ln .startswith ('%if ' ):
496
+ return None , ln
497
+ ln = ln [4 :]
498
+
499
+ # stop at '%{'
500
+ match = re_cond_end .search (ln )
501
+ if not match :
502
+ raise ValueError ("'%{' is missing for %if substitution" )
503
+ cond = ln [:match .start ()]
504
+
505
+ # eat '%{' as well
506
+ ln = ln [match .end ():]
507
+ return cond , ln
508
+
509
+ def tryParseElse (ln ):
510
+ match = re_else .search (ln )
511
+ if not match :
512
+ return False , ln
513
+ if not match .group (1 ):
514
+ raise ValueError ("'%{' is missing for %else substitution" )
515
+ return True , ln [match .end ():]
516
+
517
+ def tryParseEnd (ln ):
518
+ if ln .startswith ('%}' ):
519
+ return True , ln [2 :]
520
+ return False , ln
521
+
522
+ def parseText (ln , isNested ):
523
+ # parse everything until %if, or %} if we're parsing a
524
+ # nested expression.
525
+ re_pat = re_nested_if if isNested else re_if
526
+ match = re_pat .search (ln )
527
+ if not match :
528
+ # there is no terminating pattern, so treat the whole
529
+ # line as text
530
+ return ln , ''
531
+ text_end = match .end (1 )
532
+ return ln [:text_end ], ln [text_end :]
533
+
534
+ def parseRecursive (ln , isNested ):
535
+ result = ''
536
+ while len (ln ):
537
+ if isNested :
538
+ found_end , _ = tryParseEnd (ln )
539
+ if found_end :
540
+ break
541
+
542
+ # %if cond %{ branch_if %} %else %{ branch_else %}
543
+ cond , ln = tryParseIfCond (ln )
544
+ if cond :
545
+ branch_if , ln = parseRecursive (ln , isNested = True )
546
+ found_end , ln = tryParseEnd (ln )
547
+ if not found_end :
548
+ raise ValueError ("'%}' is missing for %if substitution" )
549
+
550
+ branch_else = ''
551
+ found_else , ln = tryParseElse (ln )
552
+ if found_else :
553
+ branch_else , ln = parseRecursive (ln , isNested = True )
554
+ found_end , ln = tryParseEnd (ln )
555
+ if not found_end :
556
+ raise ValueError ("'%}' is missing for %else substitution" )
557
+
558
+ cond = cond .strip ()
559
+
560
+ if cond in test .config .available_features :
561
+ result += branch_if
562
+ else :
563
+ result += branch_else
564
+ continue
565
+
566
+ # The rest is handled as plain text.
567
+ text , ln = parseText (ln , isNested )
568
+ result += text
569
+
570
+ return result , ln
571
+
572
+ result , ln = parseRecursive (ln , isNested = False )
573
+ assert len (ln ) == 0
574
+ return result
575
+
478
576
# Apply substitutions to the script. Allow full regular
479
577
# expression syntax. Replace each matching occurrence of regular
480
578
# expression pattern a with substitution b in line ln.
481
579
def processLine (ln ):
482
580
# Apply substitutions
581
+ ln = substituteIfElse (ln )
582
+
483
583
for a ,b in substitutions :
484
584
if kIsWindows :
485
585
b = b .replace ("\\ " ,"\\ \\ " )
0 commit comments