1#!/usr/bin/python3
2#
3# Copyright 2013-2023 The Khronos Group Inc.
4#
5# SPDX-License-Identifier: Apache-2.0
6
7# Usage: realign [infile] > outfile
8# Used to realign XML tags in the Vulkan registry after it is operated on by
9# some other filter, since whitespace inside a tag is not part of the
10# internal representation.
11
12import copy, sys, string, re
13
14def realignXML(fp):
15    patterns = [
16        [ r'(^ *\<type .*)(category=[\'"]bitmask[\'"].*)', 58 ],
17        [ r'(^ *\<enum [bv].*)(name=.*)',     28 ],
18        [ r'(^ *\<enum [bv].*)(comment=.*)',  85 ]
19    ]
20
21    # Assemble compiled expressions to match and alignment columns
22    numpat = len(patterns)
23    regexp = [ re.compile(patterns[i][0]) for i in range(0,numpat)]
24    column = [ patterns[i][1] for i in range(0,numpat)]
25
26    lines = fp.readlines()
27    for line in lines:
28        emitted = False
29        for i in range(0,len(patterns)):
30            match = regexp[i].match(line)
31            if (match):
32                if (not emitted):
33                    #print('# While processing line: ' + line, end='')
34                    emitted = True
35                #print('# matched expression: ' + patterns[i][0])
36                #print('# clause 1 = ' + match.group(1))
37                #print('# clause 2 = ' + match.group(2))
38                line = match.group(1).ljust(column[i]) + match.group(2)
39        if (emitted):
40            print(line)
41        else:
42            print(line, end='')
43
44if __name__ == '__main__':
45    if (len(sys.argv) > 1):
46        realignXML(open(sys.argv[1], 'r', encoding='utf-8'))
47    else:
48        realignXML(sys.stdin)
49