File: parser.py

package info (click to toggle)
python-awkward 2.6.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 23,088 kB
  • sloc: python: 148,689; cpp: 33,562; sh: 432; makefile: 21; javascript: 8
file content (679 lines) | stat: -rw-r--r-- 18,234 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
import json

import lark

import awkward as ak

from awkward.types.numpytype import NumpyType
from awkward.types.unknowntype import UnknownType
from awkward.types.regulartype import RegularType
from awkward.types.listtype import ListType
from awkward.types.optiontype import OptionType
from awkward.types.recordtype import RecordType
from awkward.types.uniontype import UnionType
from awkward.types.arraytype import ArrayType


grammar = r"""
start: type

type: numpytype
    | unknowntype
    | regulartype
    | listtype
    | varlen_string
    | fixedlen_string
    | char
    | byte
    | option1
    | option2
    | tuple
    | tuple_parameters
    | record
    | record_parameters
    | named0
    | named
    | union
    | list_parameters
    | categorical

numpytype: numpytype_name ("[" "parameters" "=" json_object "]")?

numpytype_name: DTYPE
              | DATETIME64
              | TIMEDELTA64

DTYPE.2: "bool"
       | "int8"
       | "uint8"
       | "int16"
       | "uint16"
       | "int32"
       | "uint32"
       | "int64"
       | "uint64"
       | "float32"
       | "float64"
       | "complex64"
       | "complex128"

DATETIME64:  /datetime64(\[(\s*-?[0-9]*)?(Y|M|W|D|h|m|s|ms|us|\u03bc|ns|ps|fs|as)\])?/
TIMEDELTA64: /timedelta64(\[(\s*-?[0-9]*)?(Y|M|W|D|h|m|s|ms|us|\u03bc|ns|ps|fs|as)\])?/

unknowntype: "unknown" ("[" "parameters" "=" json_object "]")?

regulartype: INT "*" type

listtype: "var" "*" type

varlen_string: "string" -> varlen_string
             | "bytes" -> varlen_bytestring

fixedlen_string: ("string" "[" INT "]") -> fixedlen_string
               | ("bytes" "[" INT "]") -> fixedlen_bytestring

char: "char"

byte: "byte"

option1: "?" type

option2: "option" "[" type ("," "parameters" "=" json_object)? "]"

tuple: "(" types? ")"
types: type ("," type)*

tuple_parameters: "tuple" "[" "[" types? "]" ("," "parameters" "=" json_object)? "]"

record: "{" pairs? "}"
pairs:  pair ("," pair)*
pair:   key ":" type
key:    ESCAPED_STRING -> string
      | CNAME          -> identifier

record_parameters: "struct" "[" "{" pairs? "}" ("," "parameters" "=" json_object)? "]"

named0:      CNAME "[" ("parameters" "=" json_object)? "]"
named:       CNAME "[" (named_types | named_pairs) "]"
named_types: type ("," (named_types | "parameters" "=" json_object))?
named_pairs: named_pair ("," (named_pairs | "parameters" "=" json_object))?
named_pair:  named_key ":" type
named_key:   ESCAPED_STRING -> string
           | CNAME          -> identifier

union: "union" "[" named_types? "]"

list_parameters: "[" type "," "parameters" "=" json_object "]"

categorical: "categorical" "[" "type" "=" type "]"

json: ESCAPED_STRING -> string
    | SIGNED_NUMBER  -> number
    | "true"         -> true
    | "false"        -> false
    | "null"         -> null
    | json_array
    | json_object

json_array:  "[" [json ("," json)*] "]"
json_object: "{" [json_pair ("," json_pair)*] "}"
json_pair:   ESCAPED_STRING ":" json

%import common.INT
%import common.CNAME
%import common.ESCAPED_STRING
%import common.SIGNED_NUMBER
%import common.WS

%ignore WS
"""


class Transformer:
    @staticmethod
    def _parameters(args, i):
        if i < len(args):
            return args[i]
        else:
            return None

    def start(self, args):
        return args[0]

    def type(self, args):
        return args[0]

    def numpytype(self, args):
        return ak.types.NumpyType(args[0], parameters=self._parameters(args, 1))

    def numpytype_name(self, args):
        return str(args[0])

    def unknowntype(self, args):
        return ak.types.UnknownType(parameters=self._parameters(args, 0))

    def regulartype(self, args):
        return ak.types.RegularType(args[1], int(args[0]))

    def listtype(self, args):
        return ak.types.ListType(args[0])

    def varlen_string(self, args):
        return ak.types.ListType(
            ak.types.NumpyType("uint8", {"__array__": "char"}),
            {"__array__": "string"},
        )

    def varlen_bytestring(self, args):
        return ak.types.ListType(
            ak.types.NumpyType("uint8", {"__array__": "byte"}),
            {"__array__": "bytestring"},
        )

    def fixedlen_string(self, args):
        return ak.types.RegularType(
            ak.types.NumpyType("uint8", {"__array__": "char"}),
            int(args[0]),
            {"__array__": "string"},
        )

    def fixedlen_bytestring(self, args):
        return ak.types.RegularType(
            ak.types.NumpyType("uint8", {"__array__": "byte"}),
            int(args[0]),
            {"__array__": "bytestring"},
        )

    def char(self, args):
        return ak.types.NumpyType("uint8", {"__array__": "char"})

    def byte(self, args):
        return ak.types.NumpyType("uint8", {"__array__": "byte"})

    def option1(self, args):
        return ak.types.OptionType(args[0])

    def option2(self, args):
        return ak.types.OptionType(args[0], parameters=self._parameters(args, 1))

    def tuple(self, args):
        if len(args) == 0:
            types = []
        else:
            types = args[0]
        return ak.types.RecordType(types, None)

    def types(self, args):
        return args

    def tuple_parameters(self, args):
        if len(args) != 0 and isinstance(args[0], list):
            types = args[0]
        else:
            types = []

        if len(args) != 0 and isinstance(args[-1], dict):
            parameters = args[-1]
        else:
            parameters = {}

        return ak.types.RecordType(types, None, parameters)

    def record(self, args):
        if len(args) == 0:
            fields = []
            types = []
        else:
            fields = [x[0] for x in args[0]]
            types = [x[1] for x in args[0]]
        return ak.types.RecordType(types, fields)

    def pairs(self, args):
        return args

    def pair(self, args):
        return tuple(args)

    def record_parameters(self, args):
        if len(args) != 0 and isinstance(args[0], list):
            fields = [x[0] for x in args[0]]
            types = [x[1] for x in args[0]]
        else:
            fields = []
            types = []

        if len(args) != 0 and isinstance(args[-1], dict):
            parameters = args[-1]
        else:
            parameters = {}

        return ak.types.RecordType(types, fields, parameters)

    def named0(self, args):
        parameters = {"__record__": str(args[0])}
        if 1 < len(args):
            parameters.update(args[1])
        return ak.types.RecordType([], None, parameters)

    def named(self, args):
        parameters = {"__record__": str(args[0])}

        if isinstance(args[1][-1], dict):
            arguments = args[1][:-1]
            parameters.update(args[1][-1])
        else:
            arguments = args[1]

        if any(isinstance(x, tuple) for x in arguments):
            fields = [x[0] for x in arguments]
            contents = [x[1] for x in arguments]
        else:
            fields = None
            contents = arguments

        return ak.types.RecordType(contents, fields, parameters)

    def named_types(self, args):
        if len(args) == 2 and isinstance(args[1], list):
            return args[:1] + args[1]
        else:
            return args

    def named_pairs(self, args):
        if len(args) == 2 and isinstance(args[1], list):
            return args[:1] + args[1]
        else:
            return args

    def named_pair(self, args):
        return tuple(args)

    def identifier(self, args):
        return str(args[0])

    def union(self, args):
        if len(args) == 0:
            arguments = []
            parameters = None
        elif isinstance(args[0][-1], dict):
            arguments = args[0][:-1]
            parameters = args[0][-1]
        else:
            arguments = args[0]
            parameters = None

        return ak.types.UnionType(arguments, parameters)

    def list_parameters(self, args):
        # modify recently created type object
        args[0].parameters.update(args[1])
        return args[0]

    def categorical(self, args):
        # modify recently created type object
        args[0].parameters["__categorical__"] = True
        return args[0]

    def json(self, args):
        return args[0]

    def json_object(self, args):
        return dict(args)

    def json_pair(self, args):
        return (json.loads(args[0]), args[1])

    def json_array(self, args):
        return list(args)

    def string(self, args):
        return json.loads(args[0])

    def number(self, args):
        try:
            return int(args[0])
        except ValueError:
            return float(args[0])

    def true(self, args):
        return True

    def false(self, args):
        return False

    def null(self, args):
        return None


parser = lark.Lark(grammar, parser="lalr", transformer=Transformer())


def test_numpytype_int32():
    t = NumpyType("int32")
    assert str(parser.parse(str(t))) == str(t)


def test_numpytype_datetime64():
    t = NumpyType("datetime64")
    assert str(parser.parse(str(t))) == str(t)


def test_numpytype_datetime64_10s():
    t = NumpyType("datetime64[10s]")
    assert str(parser.parse(str(t))) == str(t)


def test_numpytype_int32_parameter():
    t = NumpyType("int32", {"__array__": "Something"})
    assert str(parser.parse(str(t))) == str(t)


def test_numpytype_datetime64_parameter():
    t = NumpyType("datetime64", {"__array__": "Something"})
    assert str(parser.parse(str(t))) == str(t)


def test_numpytype_datetime64_10s_parameter():
    t = NumpyType("datetime64[10s]", {"__array__": "Something"})
    assert str(parser.parse(str(t))) == str(t)


def test_numpytype_int32_categorical():
    t = NumpyType("int32", {"__categorical__": True})
    assert str(parser.parse(str(t))) == str(t)


def test_numpytype_int32_parameters_categorical():
    t = NumpyType("int32", {"__array__": "Something", "__categorical__": True})
    assert str(parser.parse(str(t))) == str(t)


def test_unknowntype():
    t = UnknownType()
    assert str(parser.parse(str(t))) == str(t)


def test_unknowntype_parameter():
    t = UnknownType({"__array__": "Something"})
    assert str(parser.parse(str(t))) == str(t)


def test_unknowntype_categorical():
    t = UnknownType({"__categorical__": True})
    assert str(parser.parse(str(t))) == str(t)


def test_unknowntype_categorical_parameter():
    t = UnknownType({"__array__": "Something", "__categorical__": True})
    assert str(parser.parse(str(t))) == str(t)


def test_regulartype_numpytype():
    t = RegularType(NumpyType("int32"), 5)
    assert str(parser.parse(str(t))) == str(t)


def test_regulartype_numpytype_parameter():
    t = RegularType(NumpyType("int32"), 5, {"__array__": "Something"})
    assert str(parser.parse(str(t))) == str(t)


def test_regulartype_numpytype_categorical():
    t = RegularType(NumpyType("int32"), 5, {"__categorical__": True})
    assert str(parser.parse(str(t))) == str(t)


def test_regulartype_numpytype_categorical_parameter():
    t = RegularType(
        NumpyType("int32"), 5, {"__categorical__": True, "__array__": "Something"}
    )
    assert str(parser.parse(str(t))) == str(t)


def test_listtype_numpytype():
    t = ListType(NumpyType("int32"))
    assert str(parser.parse(str(t))) == str(t)


def test_listtype_numpytype_parameter():
    t = ListType(NumpyType("int32"), {"__array__": "Something"})
    assert str(parser.parse(str(t))) == str(t)


def test_listtype_numpytype_categorical():
    t = ListType(NumpyType("int32"), {"__categorical__": True})
    assert str(parser.parse(str(t))) == str(t)


def test_listtype_numpytype_categorical_parameter():
    t = ListType(
        NumpyType("int32"), {"__categorical__": True, "__array__": "Something"}
    )
    assert str(parser.parse(str(t))) == str(t)


def test_varlen_string():
    t = ListType(NumpyType("uint8", {"__array__": "char"}), {"__array__": "string"})
    assert str(parser.parse(str(t))) == str(t)


def test_varlen_bytestring():
    t = ListType(NumpyType("uint8", {"__array__": "char"}), {"__array__": "bytestring"})
    assert str(parser.parse(str(t))) == str(t)


def test_fixedlen_string():
    t = RegularType(
        NumpyType("uint8", {"__array__": "char"}), 5, {"__array__": "string"}
    )
    assert str(parser.parse(str(t))) == str(t)


def test_fixedlen_bytestring():
    t = RegularType(
        NumpyType("uint8", {"__array__": "byte"}), 5, {"__array__": "bytestring"}
    )
    assert str(parser.parse(str(t))) == str(t)


def test_char():
    t = NumpyType("uint8", {"__array__": "char"})
    assert str(parser.parse(str(t))) == str(t)


def test_byte():
    t = NumpyType("uint8", {"__array__": "byte"})
    assert str(parser.parse(str(t))) == str(t)


def test_optiontype_numpytype_int32():
    t = OptionType(NumpyType("int32"))
    assert str(parser.parse(str(t))) == str(t)


def test_optiontype_numpytype_int32_parameters():
    t = OptionType(NumpyType("int32"), {"__array__": "Something"})
    assert str(parser.parse(str(t))) == str(t)


def test_optiontype_numpytype_int32_categorical():
    t = OptionType(NumpyType("int32"), {"__categorical__": True})
    assert str(parser.parse(str(t))) == str(t)


def test_optiontype_numpytype_int32_categorical_parameters():
    t = OptionType(
        NumpyType("int32"), {"__array__": "Something", "__categorical__": True}
    )
    assert str(parser.parse(str(t))) == str(t)


def test_option_varlen_string():
    t = OptionType(
        ListType(NumpyType("uint8", {"__array__": "char"}), {"__array__": "string"})
    )
    assert str(parser.parse(str(t))) == str(t)


def test_option_varlen_string_parameters():
    t = OptionType(
        ListType(NumpyType("uint8", {"__array__": "char"}), {"__array__": "string"}),
        {"__array__": "Something"},
    )
    assert str(parser.parse(str(t))) == str(t)


def test_record_empty():
    t = RecordType([], None)
    assert str(parser.parse(str(t))) == str(t)


def test_record_fields_empty():
    t = RecordType([], [])
    assert str(parser.parse(str(t))) == str(t)


def test_record_int32():
    t = RecordType([NumpyType("int32")], None)
    assert str(parser.parse(str(t))) == str(t)


def test_record_int32_float64():
    t = RecordType([NumpyType("int32"), NumpyType("float64")], None)
    assert str(parser.parse(str(t))) == str(t)


def test_record_fields_int32():
    t = RecordType([NumpyType("int32")], ["one"])
    assert str(parser.parse(str(t))) == str(t)


def test_record_fields_int32_float64():
    t = RecordType([NumpyType("int32"), NumpyType("float64")], ["one", "t w o"])
    assert str(parser.parse(str(t))) == str(t)


def test_record_empty_parameters():
    t = RecordType([], None, {"p": [123]})
    assert str(parser.parse(str(t))) == str(t)


def test_record_fields_empty_parameters():
    t = RecordType([], [], {"p": [123]})
    assert str(parser.parse(str(t))) == str(t)


def test_record_int32_parameters():
    t = RecordType([NumpyType("int32")], None, {"p": [123]})
    assert str(parser.parse(str(t))) == str(t)


def test_record_int32_float64_parameters():
    t = RecordType([NumpyType("int32"), NumpyType("float64")], None, {"p": [123]})
    assert str(parser.parse(str(t))) == str(t)


def test_record_fields_int32_parameters():
    t = RecordType([NumpyType("int32")], ["one"], {"p": [123]})
    assert str(parser.parse(str(t))) == str(t)


def test_record_fields_int32_float64_parameters():
    t = RecordType(
        [NumpyType("int32"), NumpyType("float64")], ["one", "t w o"], {"p": [123]}
    )
    assert str(parser.parse(str(t))) == str(t)


def test_named_record_empty():
    t = RecordType([], None, {"__record__": "Name"})
    assert str(parser.parse(str(t))) == str(t)


def test_named_record_int32():
    t = RecordType([NumpyType("int32")], None, {"__record__": "Name"})
    assert str(parser.parse(str(t))) == str(t)


def test_named_record_int32_float64():
    t = RecordType(
        [NumpyType("int32"), NumpyType("float64")], None, {"__record__": "Name"}
    )
    assert str(parser.parse(str(t))) == str(t)


def test_named_record_fields_int32():
    t = RecordType([NumpyType("int32")], ["one"], {"__record__": "Name"})
    assert str(parser.parse(str(t))) == str(t)


def test_named_record_fields_int32_float64():
    t = RecordType(
        [NumpyType("int32"), NumpyType("float64")],
        ["one", "t w o"],
        {"__record__": "Name"},
    )
    assert str(parser.parse(str(t))) == str(t)


def test_named_record_empty_parameters():
    t = RecordType([], None, {"__record__": "Name", "p": [123]})
    assert str(parser.parse(str(t))) == str(t)


def test_named_record_int32_parameters():
    t = RecordType([NumpyType("int32")], None, {"__record__": "Name", "p": [123]})
    assert str(parser.parse(str(t))) == str(t)


def test_named_record_int32_float64_parameters():
    t = RecordType(
        [NumpyType("int32"), NumpyType("float64")],
        None,
        {"__record__": "Name", "p": [123]},
    )
    assert str(parser.parse(str(t))) == str(t)


def test_named_record_fields_int32_parameters():
    t = RecordType([NumpyType("int32")], ["one"], {"__record__": "Name", "p": [123]})
    assert str(parser.parse(str(t))) == str(t)


def test_named_record_fields_int32_float64_parameters():
    t = RecordType(
        [NumpyType("int32"), NumpyType("float64")],
        ["one", "t w o"],
        {"__record__": "Name", "p": [123]},
    )
    assert str(parser.parse(str(t))) == str(t)


def test_union_empty():
    t = UnionType([])
    assert str(parser.parse(str(t))) == str(t)


def test_union_float64():
    t = UnionType([NumpyType("float64")])
    assert str(parser.parse(str(t))) == str(t)


def test_union_float64_datetime64():
    t = UnionType(
        [NumpyType("float64"), NumpyType("datetime64")],
    )
    assert str(parser.parse(str(t))) == str(t)


def test_union_float64_parameters():
    t = UnionType([NumpyType("float64")], {"__array__": "Something"})
    assert str(parser.parse(str(t))) == str(t)


def test_union_float64_datetime64_parameters():
    t = UnionType(
        [NumpyType("float64"), NumpyType("datetime64")],
        {"__array__": "Something"},
    )
    assert str(parser.parse(str(t))) == str(t)