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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
extern crate lalrpop_util;
extern crate regex;
extern crate base64;
pub mod ast;
pub mod haskell;
pub mod util;
pub mod conv;
use regex::{Captures, Regex};
fn strip_comments(text: &str) -> String {
let re = Regex::new(r"\{-[\s\S]*?-\}").unwrap();
let text = re.replace_all(&text, "").to_string();
let re = Regex::new(r#"--[^\n\r"][^\n\r]*"#).unwrap();
let text = re.replace_all(&text, "").to_string();
let re = Regex::new(r#"--([\n\r])"#).unwrap();
let text = re.replace_all(&text, "$1").to_string();
let re = Regex::new(r"(?m);+\s*$").unwrap();
let text = re.replace_all(&text, "").to_string();
let re = Regex::new(r"(?m)^#(if|ifn?def|endif|else).*").unwrap();
let text = re.replace_all(&text, "").to_string();
let escape_re = Regex::new(r#"\\([abfnrtv'"\\0]|NUL|ESC)"#).unwrap();
let decode_escapes = |text: &str| {
let text = escape_re.replace_all(text, |caps: &Captures| {
match &caps[1] {
"a" => "\u{0007}",
"b" => "\u{0008}",
"f" => "\u{000C}",
"n" => "\n",
"r" => "\r",
"t" => "\t",
"v" => "\u{000B}",
"'" => "'",
"\"" => "\"",
"\\" => "\\",
"0" | "NUL" => "\0",
"ESC" => "\x1b",
s => panic!("str escape {}", s),
}.into()
});
text.to_string()
};
let re = Regex::new(r"'([^'\\]|\\[A-Z]{1,3}|\\.)'").unwrap();
let text = re.replace_all(&text, |caps: &Captures| {
let v = decode_escapes(&caps[1]);
assert!(v.len() == 1, "multi char literal {:?}", v);
format!("'{}'", base64::encode(&v))
}).to_string();
let re = Regex::new(r#""(([^"\\]|\\.)*?)""#).unwrap();
let text = re.replace_all(&text, |caps: &Captures| {
let v = decode_escapes(&caps[1]);
format!("\"{}\"", base64::encode(&v))
}).to_string();
text
}
fn decode_literal(s: &str) -> String {
let vec = base64::decode(s).unwrap_or_else(|_| panic!("invalid base64: {:?}", s));
String::from_utf8(vec).expect("invalid UTF-8")
}
fn word_is_block_word(word: &str) -> bool {
word == "do" || word == "where" || word == "of" || word == "let"
}
fn commify(val: &str) -> String {
let re_space = Regex::new(r#"^[ \t]+"#).unwrap();
let re_nl = Regex::new(r#"^\r?\n"#).unwrap();
let re_word = Regex::new(r#"([\(\{\[\]\}\)]|[^ \t\r\n\(\{\[\]\}\)]+)"#).unwrap();
let commentless = strip_comments(val);
let mut stash: Vec<usize> = vec![];
let mut braces: Vec<isize> = vec![];
let mut trigger = None;
let mut indent = 0;
let mut first = true;
let mut out = String::new();
let mut v: &str = &commentless;
while v.len() > 0 {
if let Some(cap) = re_space.captures(v) {
let word = &cap[0];
out.push_str(word);
v = &v[word.len()..];
indent += word.len();
} else if let Some(cap) = re_nl.captures(v) {
let word = &cap[0];
out.push_str(word);
v = &v[word.len()..];
indent = 0;
first = true;
if stash.len() > 1 {
for _ in &stash[1..] {
out.push_str(" ");
}
}
} else if let Some(cap) = re_word.captures(v) {
let word = &cap[0];
if first {
while {
if let Some(last_level) = stash.last().map(|x| *x) {
last_level > indent
} else {
false
}
} {
stash.pop();
braces.pop();
out.push_str("}");
}
if let Some(i) = stash.last() {
if *i == indent && trigger.is_none() {
out.push_str(";");
}
}
}
if ["]", ")", "}"].contains(&word) {
if let Some(brace) = braces.last_mut() {
*brace -= 1;
}
}
if ["[", "(", "{"].contains(&word) {
if let Some(brace) = braces.last_mut() {
*brace += 1;
}
}
while {
if let Some(brace) = braces.last().map(|x| *x) {
brace < 0
} else {
false
}
} {
stash.pop();
braces.pop();
out.push_str("}");
if braces.len() > 0 {
*braces.last_mut().unwrap() -= 1;
}
}
out.push_str(word);
v = &v[word.len()..];
if trigger.is_some() {
stash.push(indent);
}
first = false;
trigger = if word_is_block_word(word) { Some(indent) } else { None };
if trigger.is_some() {
out.push_str("{");
braces.push(0);
}
indent += word.len();
} else {
unreachable!("unknown prop {:?}", v);
}
}
for _ in 0..stash.len() {
out.push_str("}");
}
let re = Regex::new(r#"where\s+;"#).unwrap();
let out = re.replace_all(&out, r#"where "#).to_string();
let re = Regex::new(r#"\};\}"#).unwrap();
let out = re.replace_all(&out, r#"}}"#).to_string();
out
}
pub fn preprocess(input: &str) -> String {
commify(input)
}
pub fn parse<'input, 'err>(
errors: &'err mut Vec<lalrpop_util::ErrorRecovery<usize, (usize, &'input str), ()>>,
input: &'input str
) -> Result<ast::Module, lalrpop_util::ParseError<usize, (usize, &'input str), ()>>
{
haskell::parse_Module(errors, &input)
}