Skip to content

Instantly share code, notes, and snippets.

@perillo
Created January 18, 2023 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perillo/f5f96fda2bffa549bf50dd8588a0c700 to your computer and use it in GitHub Desktop.
Save perillo/f5f96fda2bffa549bf50dd8588a0c700 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Validate the code_begin lines in the Zig Language Reference.
import argparse
import sys
CODE_INDENT = 6
def validate(file, verbose=False):
seen = set()
doctests = 0
tuples = []
for i, line in enumerate(file):
idx = line.find("{#code_begin|")
if idx == -1:
continue
# Check that the code block is indented consistently.
if idx != CODE_INDENT:
debug(f"line: {i}: invalid indentation for code block")
line = line.strip()
line = line.removeprefix("{#")
line = line.removesuffix("#}")
fields = line.split("|")[1:]
if len(fields) < 2:
debug(f"line: {i}: missing doctest name")
kind = fields[0]
name = fields[1]
if kind in ("test_err", "test_safety", "obj_err") and len(fields) < 3:
debug(f"line: {i}: missing doctest error_str: {kind}|{name}")
if name in seen:
debug(f"line: {i}: duplicate doctest name: {name}")
seen.add(name)
doctests += 1
if verbose:
tuples.append((kind, name))
if verbose:
tuples.sort()
for kind, name in tuples:
print(kind + "|" + name)
print(f"{doctests} doctests")
def debug(msg):
sys.stderr.write(msg + "\n")
def main(path):
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", action="store_true")
args = parser.parse_args()
with open(path) as file:
validate(file, args.verbose)
if __name__ == "__main__":
main("doc/langref.html.in")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment