@@ -240,9 +240,37 @@ def concat_multi_lines(f):
240240 print_err (lineno , line , 'Trailing backslash at the end of the file' )
241241
242242
243+ def get_known_directive_names ():
244+ def filter_line (line ):
245+ line = line .strip ()
246+ return line .startswith ('"' ) and (line .endswith ('",' ) or line .endswith ('"' ))
247+
248+ # Equivalent to `src/tools/compiletest/src/header.rs` constant of the same name.
249+ with open (
250+ os .path .join (
251+ # We go back to `src`.
252+ os .path .dirname (os .path .dirname (__file__ )),
253+ "tools/compiletest/src/command-list.rs" ,
254+ ),
255+ "r" ,
256+ encoding = "utf8"
257+ ) as fd :
258+ content = fd .read ()
259+ return [
260+ line .strip ().replace ('",' , '' ).replace ('"' , '' )
261+ for line in content .split ('\n ' )
262+ if filter_line (line )
263+ ]
264+
265+
266+ # To prevent duplicating the list of commmands between `compiletest` and `htmldocck`, we put
267+ # it into a common file which is included in rust code and parsed here.
268+ # FIXME: This setup is temporary until we figure out how to improve this situation.
269+ KNOWN_DIRECTIVE_NAMES = get_known_directive_names ()
270+
243271LINE_PATTERN = re .compile (r'''
244- (?<=(?<!\S))(?P<invalid>!?)@(?P<negated>!?)
245- (?P<cmd>[A-Za-z ]+(?:-[A-Za-z ]+)*)
272+ //@\s+
273+ (?P<negated>!?)(?P< cmd>[A-Za-z0-9 ]+(?:-[A-Za-z0-9 ]+)*)
246274 (?P<args>.*)$
247275''' , re .X | re .UNICODE )
248276
@@ -254,17 +282,9 @@ def get_commands(template):
254282 if not m :
255283 continue
256284
257- negated = (m .group ('negated' ) == '!' )
258285 cmd = m .group ('cmd' )
259- if m .group ('invalid' ) == '!' :
260- print_err (
261- lineno ,
262- line ,
263- 'Invalid command: `!@{0}{1}`, (help: try with `@!{1}`)' .format (
264- '!' if negated else '' ,
265- cmd ,
266- ),
267- )
286+ negated = (m .group ('negated' ) == '!' )
287+ if not negated and cmd in KNOWN_DIRECTIVE_NAMES :
268288 continue
269289 args = m .group ('args' )
270290 if args and not args [:1 ].isspace ():
@@ -549,7 +569,7 @@ def get_nb_matching_elements(cache, c, regexp, stop_at_first):
549569def check_files_in_folder (c , cache , folder , files ):
550570 files = files .strip ()
551571 if not files .startswith ('[' ) or not files .endswith (']' ):
552- raise InvalidCheck ("Expected list as second argument of @ {} (ie '[]')" .format (c .cmd ))
572+ raise InvalidCheck ("Expected list as second argument of {} (ie '[]')" .format (c .cmd ))
553573
554574 folder = cache .get_absolute_path (folder )
555575
@@ -558,7 +578,7 @@ def check_files_in_folder(c, cache, folder, files):
558578 files_set = set ()
559579 for file in files :
560580 if file in files_set :
561- raise InvalidCheck ("Duplicated file `{}` in @ {}" .format (file , c .cmd ))
581+ raise InvalidCheck ("Duplicated file `{}` in {}" .format (file , c .cmd ))
562582 files_set .add (file )
563583 folder_set = set ([f for f in os .listdir (folder ) if f != "." and f != ".." ])
564584
@@ -590,48 +610,48 @@ def check_command(c, cache):
590610 if c .cmd in ['has' , 'hasraw' , 'matches' , 'matchesraw' ]: # string test
591611 regexp = c .cmd .startswith ('matches' )
592612
593- # @ has <path> = file existence
613+ # has <path> = file existence
594614 if len (c .args ) == 1 and not regexp and 'raw' not in c .cmd :
595615 try :
596616 cache .get_file (c .args [0 ])
597617 ret = True
598618 except FailedCheck as err :
599619 cerr = str (err )
600620 ret = False
601- # @ hasraw/matchesraw <path> <pat> = string test
621+ # hasraw/matchesraw <path> <pat> = string test
602622 elif len (c .args ) == 2 and 'raw' in c .cmd :
603623 cerr = "`PATTERN` did not match"
604624 ret = check_string (cache .get_file (c .args [0 ]), c .args [1 ], regexp )
605- # @ has/matches <path> <pat> <match> = XML tree test
625+ # has/matches <path> <pat> <match> = XML tree test
606626 elif len (c .args ) == 3 and 'raw' not in c .cmd :
607627 cerr = "`XPATH PATTERN` did not match"
608628 ret = get_nb_matching_elements (cache , c , regexp , True ) != 0
609629 else :
610- raise InvalidCheck ('Invalid number of @ {} arguments' .format (c .cmd ))
630+ raise InvalidCheck ('Invalid number of {} arguments' .format (c .cmd ))
611631
612632 elif c .cmd == 'files' : # check files in given folder
613- if len (c .args ) != 2 : # @ files <folder path> <file list>
614- raise InvalidCheck ("Invalid number of @ {} arguments" .format (c .cmd ))
633+ if len (c .args ) != 2 : # files <folder path> <file list>
634+ raise InvalidCheck ("Invalid number of {} arguments" .format (c .cmd ))
615635 elif c .negated :
616- raise InvalidCheck ("@ {} doesn't support negative check" .format (c .cmd ))
636+ raise InvalidCheck ("{} doesn't support negative check" .format (c .cmd ))
617637 ret = check_files_in_folder (c , cache , c .args [0 ], c .args [1 ])
618638
619639 elif c .cmd == 'count' : # count test
620- if len (c .args ) == 3 : # @ count <path> <pat> <count> = count test
640+ if len (c .args ) == 3 : # count <path> <pat> <count> = count test
621641 expected = int (c .args [2 ])
622642 found = get_tree_count (cache .get_tree (c .args [0 ]), c .args [1 ])
623643 cerr = "Expected {} occurrences but found {}" .format (expected , found )
624644 ret = expected == found
625- elif len (c .args ) == 4 : # @ count <path> <pat> <text> <count> = count test
645+ elif len (c .args ) == 4 : # count <path> <pat> <text> <count> = count test
626646 expected = int (c .args [3 ])
627647 found = get_nb_matching_elements (cache , c , False , False )
628648 cerr = "Expected {} occurrences but found {}" .format (expected , found )
629649 ret = found == expected
630650 else :
631- raise InvalidCheck ('Invalid number of @ {} arguments' .format (c .cmd ))
651+ raise InvalidCheck ('Invalid number of {} arguments' .format (c .cmd ))
632652
633653 elif c .cmd == 'snapshot' : # snapshot test
634- if len (c .args ) == 3 : # @ snapshot <snapshot-name> <html-path> <xpath>
654+ if len (c .args ) == 3 : # snapshot <snapshot-name> <html-path> <xpath>
635655 [snapshot_name , html_path , pattern ] = c .args
636656 tree = cache .get_tree (html_path )
637657 xpath = normalize_xpath (pattern )
@@ -654,33 +674,33 @@ def check_command(c, cache):
654674 else :
655675 raise FailedCheck ('Expected 1 match, but found {}' .format (len (subtrees )))
656676 else :
657- raise InvalidCheck ('Invalid number of @ {} arguments' .format (c .cmd ))
677+ raise InvalidCheck ('Invalid number of {} arguments' .format (c .cmd ))
658678
659679 elif c .cmd == 'has-dir' : # has-dir test
660- if len (c .args ) == 1 : # @ has-dir <path> = has-dir test
680+ if len (c .args ) == 1 : # has-dir <path> = has-dir test
661681 try :
662682 cache .get_dir (c .args [0 ])
663683 ret = True
664684 except FailedCheck as err :
665685 cerr = str (err )
666686 ret = False
667687 else :
668- raise InvalidCheck ('Invalid number of @ {} arguments' .format (c .cmd ))
688+ raise InvalidCheck ('Invalid number of {} arguments' .format (c .cmd ))
669689
670690 elif c .cmd == 'valid-html' :
671- raise InvalidCheck ('Unimplemented @ valid-html' )
691+ raise InvalidCheck ('Unimplemented valid-html' )
672692
673693 elif c .cmd == 'valid-links' :
674- raise InvalidCheck ('Unimplemented @ valid-links' )
694+ raise InvalidCheck ('Unimplemented valid-links' )
675695
676696 else :
677- raise InvalidCheck ('Unrecognized @ {}' .format (c .cmd ))
697+ raise InvalidCheck ('Unrecognized {}' .format (c .cmd ))
678698
679699 if ret == c .negated :
680700 raise FailedCheck (cerr )
681701
682702 except FailedCheck as err :
683- message = '@ {}{} check failed' .format ('!' if c .negated else '' , c .cmd )
703+ message = '{}{} check failed' .format ('!' if c .negated else '' , c .cmd )
684704 print_err (c .lineno , c .context , str (err ), message )
685705 except InvalidCheck as err :
686706 print_err (c .lineno , c .context , str (err ))
0 commit comments