Debian Codegraph @

examples/benchmarks/json/parsers/textparser_json.py

raw symbols SCIP
1 import timeit
2
3 import textparser
4 from textparser import Forward
5 from textparser import Sequence
6 from textparser import DelimitedList
7 from textparser import choice
8 from textparser import Optional
9
10
11 class Parser(textparser.Parser):
12
13 def token_specs(self):
14 return [
15 ('SKIP', r'[ \r\n\t]+'),
16 ('NUMBER', r'-?\d+(\.\d+)?([eE][+-]?\d+)?'),
17 ('TRUE', r'true'),
18 ('FALSE', r'false'),
19 ('NULL', r'null'),
20 ('ESCAPED_STRING', r'"(\\"|[^"])*?"'),
21 ('LPAREN', '(', r'\('),
22 ('RPAREN', ')', r'\)'),
23 ('LBRACKET', '[', r'\['),
24 ('RBRACKET', ']', r'\]'),
25 ('LBRACE', '{', r'\{'),
26 ('RBRACE', '}', r'\}'),
27 ('COMMA', ',', r','),
28 ('COLON', ':', r':'),
29 ('MISMATCH', r'.')
30 ]
31
32 def grammar(self):
33 value = Forward()
34 list_ = Sequence('[', Optional(DelimitedList(value)), ']')
35 pair = Sequence('ESCAPED_STRING', ':', value)
36 dict_ = Sequence('{', Optional(DelimitedList(pair)), '}')
37 value <<= choice(list_,
38 dict_,
39 'ESCAPED_STRING',
40 'NUMBER',
41 'TRUE',
42 'FALSE',
43 'NULL')
44
45 return value
46
47
48 def parse_time(json_string, iterations):
49 parser = Parser()
50
51 def _parse():
52 parser.parse(json_string)
53
54 return timeit.timeit(_parse, number=iterations)
55
56
57 def parse(json_string):
58 return Parser().parse(json_string)
59
60
61 def version():
62 return textparser.__version__