15592
|
1 |
use nom::{
|
|
2 |
branch::alt,
|
15594
|
3 |
bytes::complete::{escaped_transform, is_not, tag, take_while, take_while1},
|
|
4 |
character::{is_alphanumeric, is_digit, is_space},
|
15592
|
5 |
combinator::{map, map_res},
|
15823
|
6 |
multi::{many0, separated_list0},
|
15600
|
7 |
sequence::{delimited, pair, preceded, separated_pair, terminated},
|
15597
|
8 |
ExtendInto, IResult,
|
15592
|
9 |
};
|
15596
|
10 |
use std::{
|
|
11 |
collections::HashMap,
|
|
12 |
fmt::{Display, Error, Formatter},
|
|
13 |
};
|
15592
|
14 |
|
15599
|
15 |
type HaskellResult<'a, T> = IResult<&'a [u8], T>;
|
15592
|
16 |
|
|
17 |
#[derive(Debug, PartialEq)]
|
|
18 |
pub enum HaskellValue {
|
15599
|
19 |
Boolean(bool),
|
15596
|
20 |
Number(u8),
|
|
21 |
String(String),
|
15592
|
22 |
Tuple(Vec<HaskellValue>),
|
15596
|
23 |
List(Vec<HaskellValue>),
|
|
24 |
AnonStruct {
|
|
25 |
name: String,
|
|
26 |
fields: Vec<HaskellValue>,
|
|
27 |
},
|
15592
|
28 |
Struct {
|
|
29 |
name: String,
|
15593
|
30 |
fields: HashMap<String, HaskellValue>,
|
15592
|
31 |
},
|
15596
|
32 |
}
|
|
33 |
|
15598
|
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 |
|
15596
|
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() {
|
15598
|
99 |
write!(f, "{}", value)?;
|
15596
|
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() {
|
15597
|
115 |
write!(f, "{}", c)?;
|
15596
|
116 |
} else {
|
|
117 |
let mut bytes = [0u8; 4];
|
|
118 |
let size = c.encode_utf8(&mut bytes).len();
|
|
119 |
for byte in &bytes[0..size] {
|
15597
|
120 |
write!(f, "\\{:03}", byte)?;
|
15596
|
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 {
|
15599
|
130 |
HaskellValue::Boolean(value) => write!(f, "{}", if *value { "True" } else { "False" }),
|
15596
|
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 } => {
|
15597
|
136 |
write!(f, "{} ", name)?;
|
15596
|
137 |
write_sequence(f, b" \0", fields.iter())
|
|
138 |
}
|
|
139 |
HaskellValue::Struct { name, fields } => {
|
15597
|
140 |
write!(f, "{} {{", name)?;
|
15596
|
141 |
let fields = fields.iter().collect::<Vec<_>>();
|
|
142 |
let mut items = fields.iter();
|
|
143 |
while let Some((field_name, value)) = items.next() {
|
15597
|
144 |
write!(f, "{} = {}", field_name, value)?;
|
15596
|
145 |
if !items.as_slice().is_empty() {
|
|
146 |
write!(f, ", ")?;
|
|
147 |
}
|
|
148 |
}
|
|
149 |
write!(f, "}}")
|
|
150 |
}
|
|
151 |
}
|
|
152 |
}
|
15592
|
153 |
}
|
|
154 |
|
15593
|
155 |
fn comma(input: &[u8]) -> HaskellResult<&[u8]> {
|
|
156 |
delimited(take_while(is_space), tag(","), take_while(is_space))(input)
|
|
157 |
}
|
|
158 |
|
15592
|
159 |
fn surrounded<'a, P, O>(
|
|
160 |
prefix: &'static str,
|
|
161 |
suffix: &'static str,
|
15823
|
162 |
mut parser: P,
|
|
163 |
) -> impl FnMut(&'a [u8]) -> HaskellResult<'a, O>
|
15592
|
164 |
where
|
15823
|
165 |
P: FnMut(&'a [u8]) -> HaskellResult<'a, O>,
|
15592
|
166 |
{
|
15593
|
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 |
}
|
15592
|
174 |
}
|
|
175 |
|
15599
|
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 |
|
15592
|
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 |
|
15597
|
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 |
}
|
15592
|
208 |
|
15597
|
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::*;
|
15592
|
226 |
alt((
|
15597
|
227 |
map(number_raw, |n| Byte(n)),
|
15592
|
228 |
alt((
|
15597
|
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')),
|
15592
|
247 |
)),
|
|
248 |
alt((
|
15597
|
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')),
|
15592
|
268 |
)),
|
|
269 |
))(input)
|
|
270 |
}
|
|
271 |
|
15603
|
272 |
fn string_content(input: &[u8]) -> HaskellResult<String> {
|
15592
|
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> {
|
15599
|
280 |
map(
|
|
281 |
delimited(tag("\""), string_content, tag("\"")),
|
|
282 |
HaskellValue::String,
|
|
283 |
)(input)
|
15592
|
284 |
}
|
|
285 |
|
|
286 |
fn tuple(input: &[u8]) -> HaskellResult<HaskellValue> {
|
|
287 |
map(
|
15823
|
288 |
surrounded("(", ")", separated_list0(comma, value)),
|
15592
|
289 |
HaskellValue::Tuple,
|
|
290 |
)(input)
|
|
291 |
}
|
|
292 |
|
|
293 |
fn list(input: &[u8]) -> HaskellResult<HaskellValue> {
|
|
294 |
map(
|
15823
|
295 |
surrounded("[", "]", separated_list0(comma, value)),
|
15592
|
296 |
HaskellValue::List,
|
|
297 |
)(input)
|
|
298 |
}
|
|
299 |
|
|
300 |
fn identifier(input: &[u8]) -> HaskellResult<String> {
|
15594
|
301 |
map_res(take_while1(is_alphanumeric), |s| {
|
15592
|
302 |
std::str::from_utf8(s).map_err(|_| ()).map(String::from)
|
|
303 |
})(input)
|
|
304 |
}
|
|
305 |
|
15593
|
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)
|
15592
|
312 |
}
|
|
313 |
|
|
314 |
fn structure(input: &[u8]) -> HaskellResult<HaskellValue> {
|
15594
|
315 |
alt((
|
|
316 |
map(
|
|
317 |
pair(
|
|
318 |
identifier,
|
15823
|
319 |
surrounded("{", "}", separated_list0(comma, named_field)),
|
15594
|
320 |
),
|
|
321 |
|(name, mut fields)| HaskellValue::Struct {
|
|
322 |
name,
|
|
323 |
fields: fields.drain(..).collect(),
|
|
324 |
},
|
15592
|
325 |
),
|
15594
|
326 |
map(
|
|
327 |
pair(
|
|
328 |
identifier,
|
15599
|
329 |
preceded(
|
|
330 |
take_while(is_space),
|
15600
|
331 |
many0(terminated(value, take_while(is_space))),
|
15599
|
332 |
),
|
15594
|
333 |
),
|
15603
|
334 |
|(name, fields)| HaskellValue::AnonStruct {
|
15594
|
335 |
name: name.clone(),
|
|
336 |
fields,
|
|
337 |
},
|
|
338 |
),
|
|
339 |
))(input)
|
15592
|
340 |
}
|
|
341 |
|
|
342 |
fn value(input: &[u8]) -> HaskellResult<HaskellValue> {
|
15599
|
343 |
alt((boolean, number, string, tuple, list, structure))(input)
|
15592
|
344 |
}
|
|
345 |
|
|
346 |
#[inline]
|
|
347 |
pub fn parse(input: &[u8]) -> HaskellResult<HaskellValue> {
|
15593
|
348 |
delimited(take_while(is_space), value, take_while(is_space))(input)
|
15592
|
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))));
|
15599
|
359 |
matches!(number(b"adas"), Err(nom::Err::Error(_)));
|
15592
|
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),
|
15597
|
373 |
String("text\t1".to_string()),
|
15592
|
374 |
List(vec![Number(1), Number(2), Number(3)]),
|
|
375 |
]);
|
|
376 |
|
15597
|
377 |
assert_eq!(
|
|
378 |
tuple(b"(64, \"text\\t1\", [1 , 2, 3])"),
|
|
379 |
Ok((&b""[..], value))
|
|
380 |
);
|
15592
|
381 |
}
|
|
382 |
|
|
383 |
#[test]
|
|
384 |
fn structures() {
|
|
385 |
use HaskellValue::*;
|
|
386 |
|
|
387 |
let value = Struct {
|
|
388 |
name: "Hog".to_string(),
|
15593
|
389 |
fields: vec![
|
15592
|
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!(
|
15594
|
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(),
|
15599
|
404 |
fields: vec![Boolean(true), Number(100), String("\u{1f994}".to_string())],
|
15594
|
405 |
};
|
|
406 |
|
|
407 |
assert_eq!(
|
15599
|
408 |
structure(b"Hog True 100 \"\\240\\159\\166\\148\""),
|
15592
|
409 |
Ok((&b""[..], value))
|
|
410 |
);
|
|
411 |
}
|
|
412 |
}
|