forked from ajtulloch/dnngraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_lenses.py
executable file
·65 lines (53 loc) · 1.59 KB
/
add_lenses.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
#! /usr/bin/env python
"""
This is an incredibly hacky way to add automatically generated
lenses to protocol buffers, but it works.
"""
import sys
import re
with open(sys.argv[1]) as f:
lines = f.readlines()
def find_lens():
for (i, line) in enumerate(lines):
match = re.match(r"data ([A-Za-z]+) =", line)
if match:
ty = match.group(1)
return (ty, i)
return None
def find_deriving():
for (i, line) in enumerate(lines):
match = re.search(r"deriving", line)
if match:
return i
return None
def find_export_line():
for (i, line) in enumerate(lines):
match = re.search(r"module .* where", line)
if match:
return i
return None
try:
(ty, data_line) = find_lens()
except:
print "Couldn't find Data declaration, exiting"
sys.exit(0)
deriving_line = find_deriving()
export_line = find_export_line()
def remove_braces_and_container(line):
start = line.find("(")
end = line.rfind(")")
return line[:start] + line[end+1:]
lens = """
makeLensesWith (lensRules & lensField .~ \_ _ name -> [TopName (mkName Prelude'.$ "_" Prelude'.++ nameBase name)]) ''{}\n
"""
newlines = ["{-# LANGUAGE TemplateHaskell #-}\n"] \
+ lines[:export_line] \
+ [remove_braces_and_container(lines[export_line])] \
+ lines[export_line+1:data_line] \
+ ["import Language.Haskell.TH.Syntax\n"] \
+ ["import Control.Lens\n"] \
+ lines[data_line:deriving_line+1] \
+ [lens.format(ty)] \
+ lines[deriving_line+1:]
with open(sys.argv[1], "w") as f:
f.write("".join(newlines))