-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntax_language.py
More file actions
168 lines (139 loc) · 4.91 KB
/
syntax_language.py
File metadata and controls
168 lines (139 loc) · 4.91 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
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
import exrex
from lark import Lark, Transformer, v_args
import random
from data_language import maybe_int, maybe_string, maybe_value, _create_regex, try_eval
try:
input = raw_input # For Python2 compatibility
except NameError:
pass
syntax_grammar = """
?start: reduce
?reduce: split
| join
| list
?list: "[" (transform ", ")* reduce (", " apply_one)* "]" -> eval_apply_list
?transform: map
| reverse
| null
| id
?map: "MAP(" apply_one ")" -> eval_map
| "LIST(" reduce ")" -> eval_make_list
?apply_one: id
| paren
| substr
?reverse: "REVERSE" -> eval_reverse
?split: "SPLIT(" numliteral ", " transform ", " transform ", " reduce ")" -> eval_split
?join: "JOIN(" literal ")" -> eval_join
?id: "ID" -> eval_identity
?paren: "PAREN(" literal ", " literal ")" -> eval_paren
?substr: "SUBSTR(" numliteral ", " numliteral ")" -> eval_substr
?null: "NULL" -> eval_null
?literal: list
| STRING -> eval_literal
| STRING "+" STRING -> eval_add
| numliteral -> eval_literal
?numliteral: NUMBER -> eval_literal
| numliteral "+" numliteral -> eval_add
| numliteral "-" numliteral -> eval_sub
| numliteral "*" numliteral -> eval_mul
| numliteral "/" numliteral -> eval_div
| "-" numliteral -> eval_neg
| sample
?sample: "SAMPLE(" NAME ", " numliteral (", " numliteral)? ")" -> eval_sample
| "RANDINT(" numliteral (", " numliteral)? ")" -> eval_random_number
%import common.CNAME -> NAME
%import common.ESCAPED_STRING -> STRING
%import common.NUMBER
%import common.WS_INLINE
%ignore WS_INLINE
"""
@v_args(tree=True) # Affects the signatures of the methods
class FormatData(Transformer):
def __init__(self):
pass
def eval_identity(self, tree):
return lambda x: x
def eval_paren(self, tree):
return lambda x: str(tree.children[0]) + str(x) + str(tree.children[1])
def eval_substr(self, tree):
return lambda x: str(x)[tree.children[0]:tree.children[1]]
def eval_map(self, tree):
return lambda x: list(map(tree.children[0], x))
def eval_join(self, tree):
# print(tree.children[0])
# return lambda x: print(x, tree.children[0])
# def foo(x):
# print(x, tree.children[0])
# import pdb; pdb.set_trace()
# return tree.children[0].join(x)
return lambda x: tree.children[0].join(list(map(str, x)))
# return foo
def eval_make_list(self, tree):
return lambda x: [tree.children[0](x)]
def eval_reverse(self, tree):
return lambda x: x[::-1]
def eval_null(self, tree):
return lambda x: []
def eval_split(self, tree):
idx = tree.children[0]
lst_1 = lambda x: tree.children[1](x[:idx])
lst_2 = lambda x: tree.children[2](x[idx:])
return lambda x: tree.children[3](lst_1(x) + lst_2(x))
def eval_apply_list(self, tree):
def func(x):
for f in tree.children:
x = f(x)
return x
return func
def eval_literal(self, tree):
return try_eval(maybe_int(maybe_string(maybe_value(tree.children[0]))))
def eval_sample(self, tree):
char_type = tree.children[0]
regex = _create_regex(char_type)
args = tree.children[1:]
if len(tree.children) == 3:
regex += '{%s,%s}' % tuple(args)
else:
regex += '{%s}' % tuple(args)
is_int = (char_type == 'NUMBER')
return maybe_int(exrex.getone(regex), is_int)
def eval_random_number(self, tree):
return random.randint(tree.children[0], tree.children[1])
def eval_add(self, tree):
return tree.children[0] + tree.children[1]
def eval_sub(self, tree):
return tree.children[0] - tree.children[1]
def eval_mul(self, tree):
return tree.children[0] * tree.children[1]
def eval_div(self, tree):
return tree.children[0] / tree.children[1]
def eval_neg(self, tree):
return -tree.children[0]
syntax_parser = Lark(syntax_grammar, parser='lalr')
transformer = FormatData()
syntax_transformer = Lark(syntax_grammar, parser='lalr', transformer=transformer).parse
class Formatter:
def __init__(self, definition):
self.definition = definition
def format(self, data):
return syntax_transformer(self.definition)(data)
def run(s, data):
tree = syntax_parser.parse(s)
# print(tree)
# print('---')
print(transformer.transform(tree)(data))
# print()
if __name__ == '__main__':
# To escape ": use two slashes, \\"
run('SPLIT(1, MAP(PAREN("(", ")")), LIST(JOIN("-")), JOIN(" "))', list('abcde'))
run('JOIN(" ")', list('abcde'))
run('JOIN("::")', list('abcde'))
run('SPLIT(1, MAP(ID), LIST([MAP(PAREN("(", ")")), JOIN("::")]), JOIN("::"))', list('abcde'))
run('[JOIN("::"), PAREN("(", ")")]', list('abcde'))
run('[MAP(PAREN("(", ")")), JOIN("::")]', list('abcde'))
run('[REVERSE, JOIN("::")]', list('abcde'))
run('[SPLIT(2, REVERSE, REVERSE, JOIN(", "))]', list('abcde'))
run('[JOIN(""), SUBSTR(1, 4)]', list('abcde'))
# run('[REVERSE, SPLIT(2, REVERSE, MAP(PAREN("a", "")), JOIN(" "))]', [1997, '03', 28])
# run('SPLIT(1, MAP(SUBSTR(2, 4)), ID, [REVERSE, JOIN("-")])', [1997, '03', 28])
# run('SPLIT(1, MAP(SUBSTR(2, 4)), ID, JOIN("-"))', [1997, '03', 28])