-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathenum.py
More file actions
86 lines (63 loc) · 1.98 KB
/
enum.py
File metadata and controls
86 lines (63 loc) · 1.98 KB
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
import pyparsing as pp
from .common import _, _c, c, end, n, note
from .generic import name
from pydbml.parser.blueprints import EnumBlueprint
from pydbml.parser.blueprints import EnumItemBlueprint
pp.ParserElement.set_default_whitespace_chars(' \t\r')
enum_settings = '[' + _ - note('note') + _ - ']' + c
def parse_enum_settings(s, loc, tok):
'''
[note: "note content"] // comment
'''
result = {}
if 'note' in tok:
result['note'] = tok['note']
if 'comment' in tok:
result['comment'] = tok['comment'][0]
return result
enum_settings.set_parse_action(parse_enum_settings)
enum_item = _c + (name('name') + c + enum_settings('settings')[0, 1])
def parse_enum_item(s, loc, tok):
'''
student [note: "is stupid"]
'''
init_dict = {'name': tok['name']}
if 'settings' in tok:
init_dict.update(tok['settings'])
# comments after settings have priority
if 'comment' in tok['settings']:
init_dict['comment'] = tok['settings']['comment']
if 'comment' not in init_dict and 'comment_before' in tok:
comment = '\n'.join(c[0] for c in tok['comment_before'])
init_dict['comment'] = comment
return EnumItemBlueprint(**init_dict)
enum_item.set_parse_action(parse_enum_item)
enum_body = enum_item[1, ...]
enum_name = pp.Combine(name("schema") + '.' + name("name")) | name("name")
enum = _c + (
pp.CaselessLiteral('enum')
- enum_name + _
- '{'
+ enum_body('items') + n
- '}'
) + end
def parse_enum(s, loc, tok):
'''
enum members {
janitor
student
teacher
headmaster
}
'''
init_dict = {
'name': tok['name'],
'items': list(tok['items'])
}
if 'schema' in tok:
init_dict['schema'] = tok['schema']
if 'comment_before' in tok:
comment = '\n'.join(c[0] for c in tok['comment_before'])
init_dict['comment'] = comment
return EnumBlueprint(**init_dict)
enum.set_parse_action(parse_enum)