-
Notifications
You must be signed in to change notification settings - Fork 0
/
uon_revised_grammar_test.py
199 lines (167 loc) · 3.65 KB
/
uon_revised_grammar_test.py
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
from pathlib import Path
from lark import Lark
from pprint import pprint
from transformer.uon_tree_transformer import (
UonTreeToPython,
UonIndenter
)
from binary.codec import (
decode_binary, decode_binary_value,
decode_schema
)
import logging
logging.basicConfig(level=logging.DEBUG)
uon_grammar_file = Path('grammar/uon_grammar.lark')
simple_mapping_example = """
happy: yes
scale: null
"""
simple_seq_example = """
- happy
- sad
"""
simple_nested_mapping_example = """
happy: yes
scale:
max: 10
min:
scale: 10
value: 2
sad: no
"""
simple_nested_seq_example = """
- a
-
- b
- c
"""
multiline_string_json = """
{
a: an example of a loooong
string, b: (Another example of
a loooooooooong string)
}
"""
multiline_string_yaml = """
a : a normal string
b : (32000000 is a very loooooooooooooooong
number)
"""
punctuated_strings = r"""
{
a : "a punctuated {}(),?[] string but we cannot put quotes",
b : We can put not put commas inside unescaped strings but we can put ?!"',
c : ''
}
"""
number_in_strings = """
a : number is 3
b : !str 3
c : -3
"""
simple_number = """
c : 32 K
"""
regex = """
a : /asdnaksdl/
"""
true_false = """
old: !bool false
young(description: "are we young?", description: "yes", optional : false): !str true
oldAgain: true
"""
json_structure = """
{foo: 42,
bar: {
hey: ho,
boy: hood
},
l : [one, 2, three]}
"""
uon_simple = """
foo (description: "A foo", optional: true): 42
h : 1
"""
uon = """
foo (description: "A foo", optional: true): 42
nested (description: "A dictionary"):
h: 1
c: 2
"""
number_coercion = """
a : !int32 !float64 58767638927.4
"""
schema = """
!!person: !schema {
name(description: name of the person, optional: false): !str(min:3, max:25),
age: !uint(min: 0, max: 125),
minor (optional: false): !bool,
linkedin link: !url
}
"""
schema_with_quantity = """
!!temperature: !schema {
t(description: The temperature of the room,
optional : false): !int (quantity: temperature)
}
"""
schema_with_description = """
!!person: !schema (
name: "A Person",
description: "A description of a person",
uuid : http://www.google.com
) {
name(description: name of the person, optional: false): !str(min:3, max:25),
age: !uint(min: 0, max: 125),
minor (optional: false): !bool,
linkedin link: !url
}
"""
schema_validation = """
{p: !!person {
name: Stephane,
age: !uint32 25,
minor: !bool true,
linkedin link: www.google.com,
s: hole
}
}
"""
schema_validation_2 = """
{
p: !!person {
big age: !uint32 25,
minor: !bool true,
linkedin link : https://github.com/uon-language/uon-parser
},
q: !!person {
big age: !uint32 24,
minor: !bool false,
linkedin: https://hello.com
},
something: Ok
}
"""
schema_validation_yaml = """
p: !!person
name: stephane
age: 25
"""
uon_parser_2 = Lark.open(uon_grammar_file, parser='lalr',
postlex=UonIndenter(), start='start',
maybe_placeholders=True, debug=True)
def parse():
parse_tree = uon_parser_2.parse(json_structure)
print(parse_tree.pretty(indent_str=' '))
transformed = UonTreeToPython(debug=True).transform(parse_tree)
print(transformed)
with open("output/Transform.txt", "w") as text_file:
pprint(repr(transformed), stream=text_file)
transformed_to_binary = transformed.to_binary()
logging.debug(transformed_to_binary)
logging.debug("\n")
logging.debug(repr(transformed))
logging.debug("\n")
logging.debug(str(transformed))
if __name__ == '__main__':
parse()