summaryrefslogtreecommitdiff
path: root/interface/input/parsers.py
blob: 5e53e0327ff91692849581fac7019d8af17cef97 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
def parse_interval(s):
    if not s:
        return None, s
    dash = False
    init_s = s
    buff = ""
    r = [-1, -1]
    i = 0
    while s:
        if not dash and s[0] == '-':
            if buff:
                r[0] = int(buff)
                buff = ""
            dash = True
        elif s[0] in "0123456789":
            buff += s[0]
        else:
            break
        s = s[1:]
        i += 1

    if buff:
        r[1 if dash else 0] = int(buff)
    elif not buff and not dash:
        return None, s, None
    
    if not dash:
        r[1] = r[0]

    return tuple(r), s, init_s[:i]

def parse_range(s):
    if not s:
        return None, s, None

    rs = []
    disp = []
    tc = False
    while s:
        r, s, d = parse_interval(s)
        if r is None:
            break
        tc = False
        rs.append(r)
        disp.append(d)
        if s and s[0] == ',':
            s = s[1:]
            tc = True
        else:
            break

    if not rs:
        return None, s, None
    return tuple(rs), s, ", ".join(disp) + (", " if tc else "")

def parse_channelrange(s):
    if not s:
        return None, s, None

    f, s, d1 = parse_range(s)
    if f is None:
        return None, s, None
    elif not s or s[0] != ';':
        return (f, ((-1,-1),)), s, d1


    s = s[1:]
    c, s, d2 = parse_range(s)
    if c is None:
        return (f, ((-1, -1),)), s, d1 + "; "

    return (f, c), s, d1+"; "+d2

def parse_value(s):
    if not s:
        return None, s
    
    buff = ""
    while s:
        if s[0] in "0123456789" and int(buff+s[0]) < 256:
            buff += s[0]
            s = s[1:]
        else:
            break

    return (None if not buff else int(buff)), s, buff

def parse_num(s):
    if not s:
        return None, s
    
    buff = ""
    while s:
        if s[0] in "0123456789":
            buff += s[0]
            s = s[1:]
        else:
            break

    return (None if not buff else int(buff)), s, buff

def make_parse_letter(letter, display):
    def inner(s):
        if not s or s[0] != letter:
            return None, s, None
        return True, s[1:], display

    return inner

def parse_null(s):
    return True, s, ""

def parse_string(s):
    if not s:
        return None, s, None

    buff = ""
    while s:
        if s[0] != ' ':
            buff += s[0]
            s = s[1:]
        else:
            if not buff:
                return None, s, None
            break

    return buff, s, buff

def parse_quotedstring(s):
    if not s:
        return None, s, None

    if s[0] != "'":
        return None, s, None 
    s = s[1:]

    buff = ""
    bs = False
    while s:
        if s[0] == '\\':
            buff += s[0]
            bs = not bs
        elif s[0] == "'":
            if bs:
                buff += s[0]
                bs = False
            else:
                s = s[1:]
                break
        else:
            buff += s[0]
            bs = False
        s = s[1:]
    else:
        return buff, s, "'"+buff
    return buff, s, "'"+buff+"'"

PARSE_MAP = {
    "$channel_range": parse_channelrange,
    "$value": parse_value,
    "$null": parse_null,
    "$string": parse_string,
    "$num": parse_num,
    "$quoted_string": parse_quotedstring,
}