File: parser.py

package info (click to toggle)
pycson 0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 284 kB
  • sloc: python: 546; makefile: 3
file content (295 lines) | stat: -rw-r--r-- 6,534 bytes parent folder | download
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
from speg import peg
import re, sys

if sys.version_info[0] == 2:
    _chr = unichr
else:
    _chr = chr

def load(fin):
    return loads(fin.read())

def loads(s):
    if isinstance(s, bytes):
        s = s.decode('utf-8')
    if s.startswith(u'\ufeff'):
        s = s[1:]
    return peg(s.replace('\r\n', '\n'), _p_root)

def _p_ws(p):
    p('[ \t]*')

def _p_nl(p):
    p(r'([ \t]*(?:#[^\n]*)?\r?\n)+')

def _p_ews(p):
    with p:
        p(_p_nl)
    p(_p_ws)

def _p_id(p):
    return p(r'[$a-zA-Z_][$0-9a-zA-Z_]*')

_escape_table = {
    'r': '\r',
    'n': '\n',
    't': '\t',
    'f': '\f',
    'b': '\b',
}
def _p_unescape(p):
    esc = p('\\\\(?:u[0-9a-fA-F]{4}|[^\n])')
    if esc[1] == 'u':
        return _chr(int(esc[2:], 16))
    return _escape_table.get(esc[1:], esc[1:])

_re_indent = re.compile(r'[ \t]*')
def _p_block_str(p, c):
    p(r'{c}{c}{c}'.format(c=c))
    lines = [['']]
    with p:
        while True:
            s = p(r'(?:{c}(?!{c}{c})|[^{c}\\])*'.format(c=c))
            l = s.split('\n')
            lines[-1].append(l[0])
            lines.extend([x] for x in l[1:])
            if p(r'(?:\\\n[ \t]*)*'):
                continue
            p.commit()
            lines[-1].append(p(_p_unescape))
    p(r'{c}{c}{c}'.format(c=c))

    lines = [''.join(l) for l in lines]
    strip_ws = len(lines) > 1
    if strip_ws and all(c in ' \t' for c in lines[-1]):
        lines.pop()

    indent = None
    for line in lines[1:]:
        if not line:
            continue
        if indent is None:
            indent = _re_indent.match(line).group(0)
            continue
        for i, (c1, c2) in enumerate(zip(indent, line)):
            if c1 != c2:
                indent = indent[:i]
                break

    ind_len = len(indent or '')
    if strip_ws and all(c in ' \t' for c in lines[0]):
        lines = [line[ind_len:] for line in lines[1:]]
    else:
        lines[1:] = [line[ind_len:] for line in lines[1:]]

    return '\n'.join(lines)

_re_mstr_nl = re.compile(r'(?:[ \t]*\n)+[ \t]*')
_re_mstr_trailing_nl = re.compile(_re_mstr_nl.pattern + r'\Z')
def _p_multiline_str(p, c):
    p('{c}(?!{c}{c})(?:[ \t]*\n[ \t]*)?'.format(c=c))
    string_parts = []
    with p:
        while True:
            string_parts.append(p(r'[^{c}\\]*'.format(c=c)))
            if p(r'(?:\\\n[ \t]*)*'):
                string_parts.append('')
                continue
            p.commit()
            string_parts.append(p(_p_unescape))
    p(c)
    string_parts[-1] = _re_mstr_trailing_nl.sub('', string_parts[-1])
    string_parts[::2] = [_re_mstr_nl.sub(' ', part) for part in string_parts[::2]]
    return ''.join(string_parts)

def _p_string(p):
    with p:
        return p(_p_block_str, '"')
    with p:
        return p(_p_block_str, "'")
    with p:
        return p(_p_multiline_str, '"')
    return p(_p_multiline_str, "'")

def _p_array_value(p):
    with p:
        p(_p_nl)
        return p(_p_object)
    with p:
        p(_p_ws)
        return p(_p_line_object)
    p(_p_ews)
    return p(_p_simple_value)

def _p_key(p):
    with p:
        return p(_p_id)
    return p(_p_string)

def _p_flow_kv(p):
    k = p(_p_key)
    p(_p_ews)
    p(':')
    with p:
        p(_p_nl)
        return k, p(_p_object)
    with p:
        p(_p_ws)
        return k, p(_p_line_object)
    p(_p_ews)
    return k, p(_p_simple_value)

def _p_flow_obj_sep(p):
    with p:
        p(_p_ews)
        p(',')
        p(_p_ews)
        return

    p(_p_nl)
    p(_p_ws)

def _p_simple_value(p):
    with p:
        p('null')
        return None

    with p:
        p('false')
        return False
    with p:
        p('true')
        return True

    with p:
        return int(p('0b[01]+')[2:], 2)
    with p:
        return int(p('0o[0-7]+')[2:], 8)
    with p:
        return int(p('0x[0-9a-fA-F]+')[2:], 16)
    with p:
        return float(p(r'-?(?:[1-9][0-9]*|0)?\.[0-9]+(?:[Ee][\+-]?[0-9]+)?|(?:[1-9][0-9]*|0)(?:\.[0-9]+)?[Ee][\+-]?[0-9]+'))
    with p:
        return int(p('-?[1-9][0-9]*|0'), 10)

    with p:
        return p(_p_string)

    with p:
        p(r'\[')
        r = []
        with p:
            p.set('I', '')
            r.append(p(_p_array_value))
            with p:
                while True:
                    with p:
                        p(_p_ews)
                        p(',')
                        rr = p(_p_array_value)
                    if not p:
                        p(_p_nl)
                        with p:
                            rr = p(_p_object)
                        if not p:
                            p(_p_ews)
                            rr = p(_p_simple_value)
                    r.append(rr)
                    p.commit()
            with p:
                p(_p_ews)
                p(',')
        p(_p_ews)
        p(r'\]')
        return r

    p(r'\{')

    r = {}
    p(_p_ews)
    with p:
        p.set('I', '')
        k, v = p(_p_flow_kv)
        r[k] = v
        with p:
            while True:
                p(_p_flow_obj_sep)
                k, v = p(_p_flow_kv)
                r[k] = v
                p.commit()
        p(_p_ews)
        with p:
            p(',')
            p(_p_ews)
    p(r'\}')
    return r

def _p_line_kv(p):
    k = p(_p_key)
    p(_p_ws)
    p(':')
    p(_p_ws)
    with p:
        p(_p_nl)
        p(p.get('I'))
        return k, p(_p_indented_object)
    with p:
        return k, p(_p_line_object)
    with p:
        return k, p(_p_simple_value)
    p(_p_nl)
    p(p.get('I'))
    p('[ \t]')
    p(_p_ws)
    return k, p(_p_simple_value)

def _p_line_object(p):
    k, v = p(_p_line_kv)
    r = { k: v }
    with p:
        while True:
            p(_p_ws)
            p(',')
            p(_p_ws)
            k, v = p(_p_line_kv)
            r[k] = v # uniqueness
            p.commit()
    return r

def _p_object(p):
    p.set('I', p.get('I') + p('[ \t]*'))
    r = p(_p_line_object)
    with p:
        while True:
            p(_p_ws)
            with p:
                p(',')
            p(_p_nl)
            p(p.get('I'))
            rr = p(_p_line_object)
            r.update(rr) # unqueness
            p.commit()
    return r

def _p_indented_object(p):
    p.set('I', p.get('I') + p('[ \t]'))
    return p(_p_object)

def _p_root(p):
    with p:
        p(_p_nl)

    with p:
        p.set('I', '')
        r = p(_p_object)
        p(_p_ws)
        with p:
            p(',')

    if not p:
        p(_p_ws)
        r = p(_p_simple_value)

    p(_p_ews)
    p(p.eof)
    return r