15570
|
1 |
use nom::{
|
|
2 |
branch::alt,
|
15572
|
3 |
bytes::complete::{escaped_transform, is_not, tag, take_while, take_while1},
|
|
4 |
character::{is_alphanumeric, is_digit, is_space},
|
15570
|
5 |
combinator::{map, map_res},
|
15801
|
6 |
multi::{many0, separated_list0},
|
15578
|
7 |
sequence::{delimited, pair, preceded, separated_pair, terminated},
|
15575
|
8 |
ExtendInto, IResult,
|
15570
|
9 |
};
|
15574
|
10 |
use std::{
|
|
11 |
collections::HashMap,
|
|
12 |
fmt::{Display, Error, Formatter},
|
|
13 |
};
|
15570
|
14 |
|
15577
|
15 |
type HaskellResult<'a, T> = IResult<&'a [u8], T>;
|
15570
|
16 |
|
|
17 |
#[derive(Debug, PartialEq)]
|
|
18 |
pub enum HaskellValue {
|
15577
|
19 |
Boolean(bool),
|
15574
|
20 |
Number(u8),
|
|
21 |
String(String),
|
15570
|
22 |
Tuple(Vec<HaskellValue>),
|
15574
|
23 |
List(Vec<HaskellValue>),
|
|
24 |
AnonStruct {
|
|
25 |
name: String,
|
|
26 |
fields: Vec<HaskellValue>,
|
|
27 |
},
|
15570
|
28 |
Struct {
|
|
29 |
name: String,
|
15571
|
30 |
fields: HashMap<String, HaskellValue>,
|
15570
|
31 |
},
|
15574
|
32 |
}
|
|
33 |
|
15576
|
34 |
impl HaskellValue {
|
|
35 |
pub fn to_number(&self) -> Option<u8> {
|
|
36 |
match self {
|
|
37 |
HaskellValue::Number(value) => Some(*value),
|
|
38 |
_ => None,
|
|
39 |
}
|
|
40 |
}
|
|
41 |
|
|
42 |
pub fn into_number(self) -> Option<u8> {
|
|
43 |
match self {
|
|
44 |
HaskellValue::Number(value) => Some(value),
|
|
45 |
_ => None,
|
|
46 |
}
|
|
47 |
}
|
|
48 |
|
|
49 |
pub fn to_string(&self) -> Option<&str> {
|
|
50 |
match self {
|
|
51 |
HaskellValue::String(value) => Some(value),
|
|
52 |
_ => None,
|
|
53 |
}
|
|
54 |
}
|
|
55 |
|
|
56 |
pub fn into_string(self) -> Option<String> {
|
|
57 |
match self {
|
|
58 |
HaskellValue::String(value) => Some(value),
|
|
59 |
_ => None,
|
|
60 |
}
|
|
61 |
}
|
|
62 |
|
|
63 |
pub fn into_list(self) -> Option<Vec<HaskellValue>> {
|
|
64 |
match self {
|
|
65 |
HaskellValue::List(items) => Some(items),
|
|
66 |
_ => None,
|
|
67 |
}
|
|
68 |
}
|
|
69 |
|
|
70 |
pub fn into_tuple(self) -> Option<Vec<HaskellValue>> {
|
|
71 |
match self {
|
|
72 |
HaskellValue::Tuple(items) => Some(items),
|
|
73 |
_ => None,
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
pub fn into_anon_struct(self) -> Option<(String, Vec<HaskellValue>)> {
|
|
78 |
match self {
|
|
79 |
HaskellValue::AnonStruct { name, fields } => Some((name, fields)),
|
|
80 |
_ => None,
|
|
81 |
}
|
|
82 |
}
|
|
83 |
|
|
84 |
pub fn into_struct(self) -> Option<(String, HashMap<String, HaskellValue>)> {
|
|
85 |
match self {
|
|
86 |
HaskellValue::Struct { name, fields } => Some((name, fields)),
|
|
87 |
_ => None,
|
|
88 |
}
|
|
89 |
}
|
|
90 |
}
|
|
91 |
|
15574
|
92 |
fn write_sequence(
|
|
93 |
f: &mut Formatter<'_>,
|
|
94 |
brackets: &[u8; 2],
|
|
95 |
mut items: std::slice::Iter<HaskellValue>,
|
|
96 |
) -> Result<(), Error> {
|
|
97 |
write!(f, "{}", brackets[0] as char)?;
|
|
98 |
while let Some(value) = items.next() {
|
15576
|
99 |
write!(f, "{}", value)?;
|
15574
|
100 |
if !items.as_slice().is_empty() {
|
|
101 |
write!(f, ", ")?;
|
|
102 |
}
|
|
103 |
}
|
|
104 |
if brackets[1] != b'\0' {
|
|
105 |
write!(f, "{}", brackets[1] as char)
|
|
106 |
} else {
|
|
107 |
Ok(())
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|
|
111 |
fn write_text(f: &mut Formatter<'_>, text: &str) -> Result<(), Error> {
|
|
112 |
write!(f, "\"")?;
|
|
113 |
for c in text.chars() {
|
|
114 |
if c.is_ascii() && !(c as u8).is_ascii_control() {
|
15575
|
115 |
write!(f, "{}", c)?;
|
15574
|
116 |
} else {
|
|
117 |
let mut bytes = [0u8; 4];
|
|
118 |
let size = c.encode_utf8(&mut bytes).len();
|
|
119 |
for byte in &bytes[0..size] {
|
15575
|
120 |
write!(f, "\\{:03}", byte)?;
|
15574
|
121 |
}
|
|
122 |
}
|
|
123 |
}
|
|
124 |
write!(f, "\"")
|
|
125 |
}
|
|
126 |
|
|
127 |
impl Display for HaskellValue {
|
|
128 |
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
|
|
129 |
match self {
|
15577
|
130 |
HaskellValue::Boolean(value) => write!(f, "{}", if *value { "True" } else { "False" }),
|
15574
|
131 |
HaskellValue::Number(value) => write!(f, "{}", value),
|
|
132 |
HaskellValue::String(value) => write_text(f, value),
|
|
133 |
HaskellValue::Tuple(items) => write_sequence(f, b"()", items.iter()),
|
|
134 |
HaskellValue::List(items) => write_sequence(f, b"[]", items.iter()),
|
|
135 |
HaskellValue::AnonStruct { name, fields } => {
|
15575
|
136 |
write!(f, "{} ", name)?;
|
15574
|
137 |
write_sequence(f, b" \0", fields.iter())
|
|
138 |
}
|
|
139 |
HaskellValue::Struct { name, fields } => {
|
15575
|
140 |
write!(f, "{} {{", name)?;
|
15574
|
141 |
let fields = fields.iter().collect::<Vec<_>>();
|
|
142 |
let mut items = fields.iter();
|
|
143 |
while let Some((field_name, value)) = items.next() {
|
15575
|
144 |
write!(f, "{} = {}", field_name, value)?;
|
15574
|
145 |
if !items.as_slice().is_empty() {
|
|
146 |
write!(f, ", ")?;
|
|
147 |
}
|
|
148 |
}
|
|
149 |
write!(f, "}}")
|
|
150 |
}
|
|
151 |
}
|
|
152 |
}
|
15570
|
153 |
}
|
|
154 |
|
15571
|
155 |
fn comma(input: &[u8]) -> HaskellResult<&[u8]> {
|
|
156 |
delimited(take_while(is_space), tag(","), take_while(is_space))(input)
|
|
157 |
}
|
|
158 |
|
15570
|
159 |
fn surrounded<'a, P, O>(
|
|
160 |
prefix: &'static str,
|
|
161 |
suffix: &'static str,
|
15801
|
162 |
mut parser: P,
|
|
163 |
) -> impl FnMut(&'a [u8]) -> HaskellResult<'a, O>
|
15570
|
164 |
where
|
15801
|
165 |
P: FnMut(&'a [u8]) -> HaskellResult<'a, O>,
|
15570
|
166 |
{
|
15571
|
167 |
move |input| {
|
|
168 |
delimited(
|
|
169 |
delimited(take_while(is_space), tag(prefix), take_while(is_space)),
|
|
170 |
|i| parser(i),
|
|
171 |
delimited(take_while(is_space), tag(suffix), take_while(is_space)),
|
|
172 |
)(input)
|
|
173 |
}
|
15570
|
174 |
}
|
|
175 |
|
15577
|
176 |
fn boolean(input: &[u8]) -> HaskellResult<HaskellValue> {
|
|
177 |
map(
|
|
178 |
alt((map(tag("True"), |_| true), map(tag("False"), |_| false))),
|
|
179 |
HaskellValue::Boolean,
|
|
180 |
)(input)
|
|
181 |
}
|
|
182 |
|
15570
|
183 |
fn number_raw(input: &[u8]) -> HaskellResult<u8> {
|
|
184 |
use std::str::FromStr;
|
|
185 |
map_res(take_while(is_digit), |s| {
|
|
186 |
std::str::from_utf8(s)
|
|
187 |
.map_err(|_| ())
|
|
188 |
.and_then(|s| u8::from_str(s).map_err(|_| ()))
|
|
189 |
})(input)
|
|
190 |
}
|
|
191 |
|
|
192 |
fn number(input: &[u8]) -> HaskellResult<HaskellValue> {
|
|
193 |
map(number_raw, HaskellValue::Number)(input)
|
|
194 |
}
|
|
195 |
|
15575
|
196 |
enum Escape {
|
|
197 |
Empty,
|
|
198 |
Byte(u8),
|
|
199 |
}
|
|
200 |
|
|
201 |
impl ExtendInto for Escape {
|
|
202 |
type Item = u8;
|
|
203 |
type Extender = Vec<u8>;
|
|
204 |
|
|
205 |
fn new_builder(&self) -> Self::Extender {
|
|
206 |
Vec::new()
|
|
207 |
}
|
15570
|
208 |
|
15575
|
209 |
fn extend_into(&self, acc: &mut Self::Extender) {
|
|
210 |
if let Escape::Byte(b) = self {
|
|
211 |
acc.push(*b);
|
|
212 |
}
|
|
213 |
}
|
|
214 |
}
|
|
215 |
|
|
216 |
impl Extend<Escape> for Vec<u8> {
|
|
217 |
fn extend<T: IntoIterator<Item = Escape>>(&mut self, iter: T) {
|
|
218 |
for item in iter {
|
|
219 |
item.extend_into(self);
|
|
220 |
}
|
|
221 |
}
|
|
222 |
}
|
|
223 |
|
|
224 |
fn string_escape(input: &[u8]) -> HaskellResult<Escape> {
|
|
225 |
use Escape::*;
|
15570
|
226 |
alt((
|
15575
|
227 |
map(number_raw, |n| Byte(n)),
|
15570
|
228 |
alt((
|
15575
|
229 |
map(tag("\\"), |_| Byte(b'\\')),
|
|
230 |
map(tag("\""), |_| Byte(b'\"')),
|
|
231 |
map(tag("'"), |_| Byte(b'\'')),
|
|
232 |
map(tag("n"), |_| Byte(b'\n')),
|
|
233 |
map(tag("r"), |_| Byte(b'\r')),
|
|
234 |
map(tag("t"), |_| Byte(b'\t')),
|
|
235 |
map(tag("a"), |_| Byte(b'\x07')),
|
|
236 |
map(tag("b"), |_| Byte(b'\x08')),
|
|
237 |
map(tag("v"), |_| Byte(b'\x0B')),
|
|
238 |
map(tag("f"), |_| Byte(b'\x0C')),
|
|
239 |
map(tag("&"), |_| Empty),
|
|
240 |
map(tag("NUL"), |_| Byte(b'\x00')),
|
|
241 |
map(tag("SOH"), |_| Byte(b'\x01')),
|
|
242 |
map(tag("STX"), |_| Byte(b'\x02')),
|
|
243 |
map(tag("ETX"), |_| Byte(b'\x03')),
|
|
244 |
map(tag("EOT"), |_| Byte(b'\x04')),
|
|
245 |
map(tag("ENQ"), |_| Byte(b'\x05')),
|
|
246 |
map(tag("ACK"), |_| Byte(b'\x06')),
|
15570
|
247 |
)),
|
|
248 |
alt((
|
15575
|
249 |
map(tag("SO"), |_| Byte(b'\x0E')),
|
|
250 |
map(tag("SI"), |_| Byte(b'\x0F')),
|
|
251 |
map(tag("DLE"), |_| Byte(b'\x10')),
|
|
252 |
map(tag("DC1"), |_| Byte(b'\x11')),
|
|
253 |
map(tag("DC2"), |_| Byte(b'\x12')),
|
|
254 |
map(tag("DC3"), |_| Byte(b'\x13')),
|
|
255 |
map(tag("DC4"), |_| Byte(b'\x14')),
|
|
256 |
map(tag("NAK"), |_| Byte(b'\x15')),
|
|
257 |
map(tag("SYN"), |_| Byte(b'\x16')),
|
|
258 |
map(tag("ETB"), |_| Byte(b'\x17')),
|
|
259 |
map(tag("CAN"), |_| Byte(b'\x18')),
|
|
260 |
map(tag("EM"), |_| Byte(b'\x19')),
|
|
261 |
map(tag("SUB"), |_| Byte(b'\x1A')),
|
|
262 |
map(tag("ESC"), |_| Byte(b'\x1B')),
|
|
263 |
map(tag("FS"), |_| Byte(b'\x1C')),
|
|
264 |
map(tag("GS"), |_| Byte(b'\x1D')),
|
|
265 |
map(tag("RS"), |_| Byte(b'\x1E')),
|
|
266 |
map(tag("US"), |_| Byte(b'\x1F')),
|
|
267 |
map(tag("DEL"), |_| Byte(b'\x7F')),
|
15570
|
268 |
)),
|
|
269 |
))(input)
|
|
270 |
}
|
|
271 |
|
15581
|
272 |
fn string_content(input: &[u8]) -> HaskellResult<String> {
|
15570
|
273 |
map_res(
|
|
274 |
escaped_transform(is_not("\"\\"), '\\', string_escape),
|
|
275 |
|bytes| String::from_utf8(bytes).map_err(|_| ()),
|
|
276 |
)(input)
|
|
277 |
}
|
|
278 |
|
|
279 |
fn string(input: &[u8]) -> HaskellResult<HaskellValue> {
|
15577
|
280 |
map(
|
|
281 |
delimited(tag("\""), string_content, tag("\"")),
|
|
282 |
HaskellValue::String,
|
|
283 |
)(input)
|
15570
|
284 |
}
|
|
285 |
|
|
286 |
fn tuple(input: &[u8]) -> HaskellResult<HaskellValue> {
|
|
287 |
map(
|
15801
|
288 |
surrounded("(", ")", separated_list0(comma, value)),
|
15570
|
289 |
HaskellValue::Tuple,
|
|
290 |
)(input)
|
|
291 |
}
|
|
292 |
|
|
293 |
fn list(input: &[u8]) -> HaskellResult<HaskellValue> {
|
|
294 |
map(
|
15801
|
295 |
surrounded("[", "]", separated_list0(comma, value)),
|
15570
|
296 |
HaskellValue::List,
|
|
297 |
)(input)
|
|
298 |
}
|
|
299 |
|
|
300 |
fn identifier(input: &[u8]) -> HaskellResult<String> {
|
15572
|
301 |
map_res(take_while1(is_alphanumeric), |s| {
|
15570
|
302 |
std::str::from_utf8(s).map_err(|_| ()).map(String::from)
|
|
303 |
})(input)
|
|
304 |
}
|
|
305 |
|
15571
|
306 |
fn named_field(input: &[u8]) -> HaskellResult<(String, HaskellValue)> {
|
|
307 |
separated_pair(
|
|
308 |
identifier,
|
|
309 |
delimited(take_while(is_space), tag("="), take_while(is_space)),
|
|
310 |
value,
|
|
311 |
)(input)
|
15570
|
312 |
}
|
|
313 |
|
|
314 |
fn structure(input: &[u8]) -> HaskellResult<HaskellValue> {
|
15572
|
315 |
alt((
|
|
316 |
map(
|
|
317 |
pair(
|
|
318 |
identifier,
|
15801
|
319 |
surrounded("{", "}", separated_list0(comma, named_field)),
|
15572
|
320 |
),
|
|
321 |
|(name, mut fields)| HaskellValue::Struct {
|
|
322 |
name,
|
|
323 |
fields: fields.drain(..).collect(),
|
|
324 |
},
|
15570
|
325 |
),
|
15572
|
326 |
map(
|
|
327 |
pair(
|
|
328 |
identifier,
|
15577
|
329 |
preceded(
|
|
330 |
take_while(is_space),
|
15578
|
331 |
many0(terminated(value, take_while(is_space))),
|
15577
|
332 |
),
|
15572
|
333 |
),
|
15581
|
334 |
|(name, fields)| HaskellValue::AnonStruct {
|
15572
|
335 |
name: name.clone(),
|
|
336 |
fields,
|
|
337 |
},
|
|
338 |
),
|
|
339 |
))(input)
|
15570
|
340 |
}
|
|
341 |
|
|
342 |
fn value(input: &[u8]) -> HaskellResult<HaskellValue> {
|
15577
|
343 |
alt((boolean, number, string, tuple, list, structure))(input)
|
15570
|
344 |
}
|
|
345 |
|
|
346 |
#[inline]
|
|
347 |
pub fn parse(input: &[u8]) -> HaskellResult<HaskellValue> {
|
15571
|
348 |
delimited(take_while(is_space), value, take_while(is_space))(input)
|
15570
|
349 |
}
|
|
350 |
|
|
351 |
mod test {
|
|
352 |
use super::*;
|
|
353 |
|
|
354 |
#[test]
|
|
355 |
fn terminals() {
|
|
356 |
use HaskellValue::*;
|
|
357 |
|
|
358 |
matches!(number(b"127"), Ok((_, Number(127))));
|
15577
|
359 |
matches!(number(b"adas"), Err(nom::Err::Error(_)));
|
15570
|
360 |
|
|
361 |
assert_eq!(
|
|
362 |
string(b"\"Hail \\240\\159\\166\\148!\""),
|
|
363 |
Ok((&b""[..], String("Hail \u{1f994}!".to_string())))
|
|
364 |
);
|
|
365 |
}
|
|
366 |
|
|
367 |
#[test]
|
|
368 |
fn sequences() {
|
|
369 |
use HaskellValue::*;
|
|
370 |
|
|
371 |
let value = Tuple(vec![
|
|
372 |
Number(64),
|
15575
|
373 |
String("text\t1".to_string()),
|
15570
|
374 |
List(vec![Number(1), Number(2), Number(3)]),
|
|
375 |
]);
|
|
376 |
|
15575
|
377 |
assert_eq!(
|
|
378 |
tuple(b"(64, \"text\\t1\", [1 , 2, 3])"),
|
|
379 |
Ok((&b""[..], value))
|
|
380 |
);
|
15570
|
381 |
}
|
|
382 |
|
|
383 |
#[test]
|
|
384 |
fn structures() {
|
|
385 |
use HaskellValue::*;
|
|
386 |
|
|
387 |
let value = Struct {
|
|
388 |
name: "Hog".to_string(),
|
15571
|
389 |
fields: vec![
|
15570
|
390 |
("name".to_string(), String("\u{1f994}".to_string())),
|
|
391 |
("health".to_string(), Number(100)),
|
|
392 |
]
|
|
393 |
.drain(..)
|
|
394 |
.collect(),
|
|
395 |
};
|
|
396 |
|
|
397 |
assert_eq!(
|
15572
|
398 |
structure(b"Hog {name = \"\\240\\159\\166\\148\", health = 100}"),
|
|
399 |
Ok((&b""[..], value))
|
|
400 |
);
|
|
401 |
|
|
402 |
let value = AnonStruct {
|
|
403 |
name: "Hog".to_string(),
|
15577
|
404 |
fields: vec![Boolean(true), Number(100), String("\u{1f994}".to_string())],
|
15572
|
405 |
};
|
|
406 |
|
|
407 |
assert_eq!(
|
15577
|
408 |
structure(b"Hog True 100 \"\\240\\159\\166\\148\""),
|
15570
|
409 |
Ok((&b""[..], value))
|
|
410 |
);
|
|
411 |
}
|
|
412 |
}
|