Skip to content

Commit 8d94f42

Browse files
committed
Accept escaped newlines as spaces.
1 parent 14bc021 commit 8d94f42

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/expressions.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,4 +1698,16 @@ mod tests {
16981698
)));
16991699
}
17001700

1701+
1702+
#[test]
1703+
fn test_escaped_newline() {
1704+
let test = ExpressionParser::<NewlinesAreNotSpaces>::test;
1705+
1706+
assert_parse_eq(test(make_strspan("a <= \\\nb")), Ok((make_strspan(""),
1707+
Box::new(Expression::Bop(Bop::Leq,
1708+
Box::new(Expression::Name("a".to_string())),
1709+
Box::new(Expression::Name("b".to_string())),
1710+
))
1711+
)));
1712+
}
17011713
}

src/helpers.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ use nom_locate::LocatedSpan;
88
pub(crate) type StrSpan<'a> = LocatedSpan<CompleteStr<'a>>;
99

1010

11-
named!(pub space<StrSpan, StrSpan>, eat_separator!(&b" \t"[..]));
12-
1311
/// Like `ws!()`, but does not allow newlines.
1412
#[macro_export]
1513
macro_rules! ws2 (
@@ -18,10 +16,10 @@ macro_rules! ws2 (
1816
use nom::Convert;
1917
use nom::Err;
2018

21-
match sep!($i, $crate::helpers::space, $($args)*) {
19+
match sep!($i, $crate::helpers::spaces2, $($args)*) {
2220
Err(e) => Err(e),
2321
Ok((i1,o)) => {
24-
match $crate::helpers::space(i1) {
22+
match $crate::helpers::spaces2(i1) {
2523
Err(e) => Err(Err::convert(e)),
2624
Ok((i2,_)) => Ok((i2, o))
2725
}
@@ -52,20 +50,24 @@ macro_rules! ws4 (
5250
)
5351
);
5452

53+
named!(escaped_newline<StrSpan, ()>,
54+
map!(terminated!(char!('\\'), char!('\n')), |_| ())
55+
);
56+
5557
named!(pub spaces<StrSpan, ()>,
56-
map!(many0!(alt!(map!(one_of!(" \t"), |_|()) | newline)), |_| ())
58+
map!(many0!(alt!(one_of!(" \t") => { |_|() } | escaped_newline | newline)), |_| ())
5759
);
5860

5961
named!(pub spaces2<StrSpan, ()>,
60-
map!(many0!(one_of!(" \t")), |_| ())
62+
map!(many0!(alt!(one_of!(" \t") => { |_| () }|escaped_newline)), |_| ())
6163
);
6264

6365
named!(pub space_sep<StrSpan, ()>,
64-
map!(many1!(alt!(map!(one_of!(" \t"), |_|()) | newline)), |_| ())
66+
map!(many1!(alt!(one_of!(" \t") => { |_|() } | escaped_newline | newline)), |_| ())
6567
);
6668

6769
named!(pub space_sep2<StrSpan, ()>,
68-
map!(many1!(one_of!(" \t")), |_| ())
70+
map!(many1!(alt!(one_of!(" \t") => { |_| () } | escaped_newline)), |_| ())
6971
);
7072

7173
// Let me explain this ugliness.
@@ -128,7 +130,7 @@ named!(pub newline<StrSpan, ()>,
128130
map!(
129131
many1!(
130132
tuple!(
131-
space,
133+
spaces2,
132134
opt!(preceded!(char!('#'), many0!(none_of!("\n")))),
133135
char!('\n')
134136
)

0 commit comments

Comments
 (0)