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