Update Makefile to make it compile with recent pilex
[c_to_python.git] / scan-code_to_l.py
1 #!/usr/bin/env python3
2
3 import sys
4 import xml.etree.ElementTree
5
6 def get_text(root, i):
7   if i < 0:
8     i += len(root) + 1
9   text = root.text if i == 0 else root[i - 1].tail
10   return '' if text is None else text
11
12 def to_text(root):
13   return ''.join(
14     [
15       j
16       for i in range(len(root))
17       for j in [get_text(root, i), to_text(root[i])]
18     ] +
19     [get_text(root, len(root))]
20   )
21
22 text = to_text(xml.etree.ElementTree.parse(sys.stdin).getroot()[0])
23
24 # see tests/scan-code.l
25 text = text.replace(r'{tag}([^\0\n>]|->)+', '{tag}')
26 text = text.replace(r'{tag}(?:([^\0\n>]|->)+)', '{tag}')
27
28 text = text.replace(r'{splice}(\\[ \f\t\v]*\n)*', '{splice}')
29 text = text.replace(r'{splice}(?:(\\[ \f\t\v]*\n)*)', '{splice}')
30
31 text = text.replace(r'{letter}[.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]', '{letter}')
32 text = text.replace(r'{letter}(?:[.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_])', '{letter}')
33 text = text.replace(r'{id}{letter}({letter}|[-0-9])*', '{id}')
34 text = text.replace(r'{id}(?:{letter}({letter}|[-0-9])*)', '{id}')
35 text = text.replace(r'{ref}-?[0-9]+|{id}|"["{id}"]"|"$"', '{ref}')
36 text = text.replace(r'{ref}(?:-?[0-9]+|{id}|"["{id}"]"|"$")', '{ref}')
37
38 # we can only calculate column numbering once all substitutions done
39 i = 0
40 j = text.find(' /*COLUMN32*/ ', i)
41 while j != -1:
42   k = text.rfind('\n', 0, j)
43   col = j - k - 1
44   if col >= 32:
45     tab = ' '
46   else:
47     tab = '\t' * ((32 - col + 7) // 8)
48   text = text[:j] + tab + text[j + 14:]
49   i = j
50   j = text.find(' /*COLUMN32*/ ', i)
51
52 sys.stdout.write(text)