about summary refs log tree commit diff
path: root/src/etc/extract-tests.py
blob: 12740a5616b8dd30e589a3c90cbd1ea46563c63d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# xfail-license

# Script for extracting compilable fragments from markdown
# documentation. See prep.js for a description of the format
# recognized by this tool. Expects a directory fragments/ to exist
# under the current directory, and writes the fragments in there as
# individual .rs files.

import sys, re

if len(sys.argv) < 3:
    print("Please provide an input filename")
    sys.exit(1)

filename = sys.argv[1]
dest = sys.argv[2]
f = open(filename)
lines = f.readlines()
f.close()

cur = 0
line = ""
chapter = ""
chapter_n = 0

while cur < len(lines):
    line = lines[cur]
    cur += 1
    chap = re.match("# (.*)", line)
    if chap:
        chapter = re.sub(r"\W", "_", chap.group(1)).lower()
        chapter_n = 1
    elif re.match("~~~", line):
        # Parse the tags that open a code block in the pandoc format:
        # ~~~ {.tag1 .tag2}
        tags = re.findall("\.([\w-]*)", line)
        block = ""
        ignore = "notrust" in tags or "ignore" in tags
        # Some tags used by the language ref that indicate not rust
        ignore |= "ebnf" in tags
        ignore |= "abnf" in tags
        ignore |= "keyword" in tags
        ignore |= "field" in tags
        ignore |= "precedence" in tags
        xfail = "xfail-test" in tags
        while cur < len(lines):
            line = lines[cur]
            cur += 1
            if re.match("~~~", line):
                break
            else:
                # Lines beginning with '# ' are turned into valid code
                line = re.sub("^# ", "", line)
                # Allow ellipses in code snippets
                line = re.sub("\.\.\.", "", line)
                block += line
        if not ignore:
            if not re.search(r"\bfn main\b", block):
                block = "fn main() {\n" + block + "\n}\n"
            if not re.search(r"\bextern mod std\b", block):
                block = "extern mod std;\n" + block
            block = """#[ forbid(ctypes) ];
#[ forbid(deprecated_pattern) ];
#[ forbid(implicit_copies) ];
#[ forbid(non_implicitly_copyable_typarams) ];
#[ forbid(path_statement) ];
#[ forbid(type_limits) ];
#[ forbid(unrecognized_lint) ];
#[ forbid(unused_imports) ];
#[ forbid(while_true) ];

#[ warn(non_camel_case_types) ];\n
""" + block
            if xfail:
                block = "// xfail-test\n" + block
            filename = (dest + "/" + str(chapter)
                        + "_" + str(chapter_n) + ".rs")
            chapter_n += 1
            f = open(filename, 'w')
            f.write(block)
            f.close()