summaryrefslogtreecommitdiffstats
path: root/genchartbl.py
blob: 3529edebc5a8250c65b4a50db0a6d8e0e7a9b36b (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
#!/usr/bin/env python3
import sys
import string

def name(i):
    if i < 0x21:
        return \
            ['NUL ', 'SOH ', 'STX ', 'ETX ', 'EOT ', 'ENQ ', 'ACK ', 'BEL ',
             'BS  ', 'HT  ', 'LF  ', 'VT  ', 'FF  ', 'CR  ', 'SO  ', 'SI  ',
             'DLE ', 'DC1 ', 'DC2 ', 'DC3 ', 'DC4 ', 'NAK ', 'SYN ', 'ETB ',
             'CAN ', 'EM  ', 'SUB ', 'ESC ', 'FS  ', 'GS  ', 'RS  ', 'US  ',
             'SPC '][i]
    if i == 0x7f:
        return 'DEL '

def gentbl(tblname, pred):
    sys.stdout.write('''\
/* Generated by genchartbl.py */
static const int {}[] = {{
'''.format(tblname))

    for i in range(256):
        if pred(chr(i)):
            v = 1
        else:
            v = 0

        if 0x21 <= i and i < 0x7f:
            sys.stdout.write('{} /* {}    */, '.format(v, chr(i)))
        elif 0x80 <= i:
            sys.stdout.write('{} /* {} */, '.format(v, hex(i)))
        else:
            sys.stdout.write('{} /* {} */, '.format(v, name(i)))
        if (i + 1)%4 == 0:
            sys.stdout.write('\n')

    sys.stdout.write('};\n')

def sf_key():
    gentbl('SF_KEY_CHARS', lambda c: c in string.ascii_lowercase or
           c in string.digits or c in '_-.*')

def sf_dquote():
    gentbl('SF_DQUOTE_CHARS', lambda c: (0x20 <= ord(c) and ord(c) <= 0x21) or
           (0x23 <= ord(c) and ord(c) <= 0x5b) or
           (0x5d <= ord(c) and ord(c) <= 0x7e))

def sf_token():
    gentbl('SF_TOKEN_CHARS', lambda c: c in "!#$%&'*+-.^_`|~:/" or
           c in string.digits or c in string.ascii_letters)

def sf_byteseq():
    gentbl('SF_BYTESEQ_CHARS', lambda c: c in string.ascii_letters or
           c in string.digits or c in '+/=')

sf_key()
sys.stdout.write('\n')

sf_dquote()
sys.stdout.write('\n')

sf_token()
sys.stdout.write('\n')

sf_byteseq()
sys.stdout.write('\n')