| 1 |
use nom::{error::ErrorKind, error_position, AsBytes, FindSubstring, IResult, Input, Parser}; |
| 2 |
use nom_locate::LocatedSpan; |
| 3 |
use std::cmp; |
| 4 |
use std::fmt::Debug; |
| 5 |
|
| 6 |
#[cfg(feature = "alloc")] |
| 7 |
use nom::bytes::complete::escaped_transform; |
| 8 |
|
| 9 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 10 |
use nom::{ |
| 11 |
bytes::complete::{tag, take_until}, |
| 12 |
character::complete::{char, multispace0}, |
| 13 |
combinator::eof, |
| 14 |
multi::many0, |
| 15 |
sequence::{delimited, preceded}, |
| 16 |
}; |
| 17 |
|
| 18 |
type StrSpan<'a> = LocatedSpan<&'a str>; |
| 19 |
type BytesSpan<'a> = LocatedSpan<&'a [u8]>; |
| 20 |
|
| 21 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 22 |
fn simple_parser_str(i: StrSpan) -> IResult<StrSpan, Vec<StrSpan>> { |
| 23 |
let (i, foo) = delimited(multispace0, tag("foo"), multispace0).parse(i)?; |
| 24 |
let (i, bar) = delimited(multispace0, tag("bar"), multispace0).parse(i)?; |
| 25 |
let (i, baz) = many0(delimited(multispace0, tag("baz"), multispace0)).parse(i)?; |
| 26 |
let (i, eof) = eof(i)?; |
| 27 |
|
| 28 |
Ok({ |
| 29 |
let mut res = vec![foo, bar]; |
| 30 |
res.extend(baz); |
| 31 |
res.push(eof); |
| 32 |
(i, res) |
| 33 |
}) |
| 34 |
} |
| 35 |
|
| 36 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 37 |
fn simple_parser_u8(i: BytesSpan) -> IResult<BytesSpan, Vec<BytesSpan>> { |
| 38 |
let (i, foo) = delimited(multispace0, tag("foo"), multispace0).parse(i)?; |
| 39 |
let (i, bar) = delimited(multispace0, tag("bar"), multispace0).parse(i)?; |
| 40 |
let (i, baz) = many0(delimited(multispace0, tag("baz"), multispace0)).parse(i)?; |
| 41 |
let (i, eof) = eof(i)?; |
| 42 |
|
| 43 |
Ok({ |
| 44 |
let mut res = vec![foo, bar]; |
| 45 |
res.extend(baz); |
| 46 |
res.push(eof); |
| 47 |
(i, res) |
| 48 |
}) |
| 49 |
} |
| 50 |
|
| 51 |
struct Position { |
| 52 |
line: u32, |
| 53 |
column: usize, |
| 54 |
offset: usize, |
| 55 |
fragment_len: usize, |
| 56 |
} |
| 57 |
|
| 58 |
fn test_str_fragments<'a, F, T>(parser: F, input: T, positions: Vec<Position>) |
| 59 |
where |
| 60 |
F: Fn(LocatedSpan<T>) -> IResult<LocatedSpan<T>, Vec<LocatedSpan<T>>>, |
| 61 |
T: Input + Debug + PartialEq + AsBytes, |
| 62 |
{ |
| 63 |
let res = parser(LocatedSpan::new(input.take_from(0))) |
| 64 |
.map_err(|err| { |
| 65 |
eprintln!( |
| 66 |
"for={:?} -- The parser should run successfully\n{:?}", |
| 67 |
input, err |
| 68 |
); |
| 69 |
|
| 70 |
format!("The parser should run successfully") |
| 71 |
}) |
| 72 |
.unwrap(); |
| 73 |
// assert!(res.is_ok(), "the parser should run successfully"); |
| 74 |
let (remaining, output) = res; |
| 75 |
assert!( |
| 76 |
remaining.fragment().input_len() == 0, |
| 77 |
"no input should remain" |
| 78 |
); |
| 79 |
assert_eq!(output.len(), positions.len()); |
| 80 |
for (output_item, pos) in output.iter().zip(positions.iter()) { |
| 81 |
assert_eq!(output_item.location_offset(), pos.offset); |
| 82 |
assert_eq!(output_item.location_line(), pos.line); |
| 83 |
assert_eq!( |
| 84 |
output_item.fragment(), |
| 85 |
&input |
| 86 |
.take(cmp::min(pos.offset + pos.fragment_len, input.input_len())) |
| 87 |
.take_from(pos.offset) |
| 88 |
); |
| 89 |
assert_eq!( |
| 90 |
output_item.get_utf8_column(), |
| 91 |
pos.column, |
| 92 |
"columns should be equal" |
| 93 |
); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 98 |
#[test] |
| 99 |
fn it_locates_str_fragments() { |
| 100 |
test_str_fragments( |
| 101 |
simple_parser_str, |
| 102 |
"foobarbaz", |
| 103 |
vec![ |
| 104 |
Position { |
| 105 |
line: 1, |
| 106 |
column: 1, |
| 107 |
offset: 0, |
| 108 |
fragment_len: 3, |
| 109 |
}, |
| 110 |
Position { |
| 111 |
line: 1, |
| 112 |
column: 4, |
| 113 |
offset: 3, |
| 114 |
fragment_len: 3, |
| 115 |
}, |
| 116 |
Position { |
| 117 |
line: 1, |
| 118 |
column: 7, |
| 119 |
offset: 6, |
| 120 |
fragment_len: 3, |
| 121 |
}, |
| 122 |
Position { |
| 123 |
line: 1, |
| 124 |
column: 10, |
| 125 |
offset: 9, |
| 126 |
fragment_len: 3, |
| 127 |
}, |
| 128 |
], |
| 129 |
); |
| 130 |
test_str_fragments( |
| 131 |
simple_parser_str, |
| 132 |
" foo |
| 133 |
bar |
| 134 |
baz", |
| 135 |
vec![ |
| 136 |
Position { |
| 137 |
line: 1, |
| 138 |
column: 2, |
| 139 |
offset: 1, |
| 140 |
fragment_len: 3, |
| 141 |
}, |
| 142 |
Position { |
| 143 |
line: 2, |
| 144 |
column: 9, |
| 145 |
offset: 13, |
| 146 |
fragment_len: 3, |
| 147 |
}, |
| 148 |
Position { |
| 149 |
line: 3, |
| 150 |
column: 13, |
| 151 |
offset: 29, |
| 152 |
fragment_len: 3, |
| 153 |
}, |
| 154 |
Position { |
| 155 |
line: 3, |
| 156 |
column: 16, |
| 157 |
offset: 32, |
| 158 |
fragment_len: 3, |
| 159 |
}, |
| 160 |
], |
| 161 |
); |
| 162 |
} |
| 163 |
|
| 164 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 165 |
#[test] |
| 166 |
fn it_locates_u8_fragments() { |
| 167 |
test_str_fragments( |
| 168 |
simple_parser_u8, |
| 169 |
b"foobarbaz", |
| 170 |
vec![ |
| 171 |
Position { |
| 172 |
line: 1, |
| 173 |
column: 1, |
| 174 |
offset: 0, |
| 175 |
fragment_len: 3, |
| 176 |
}, |
| 177 |
Position { |
| 178 |
line: 1, |
| 179 |
column: 4, |
| 180 |
offset: 3, |
| 181 |
fragment_len: 3, |
| 182 |
}, |
| 183 |
Position { |
| 184 |
line: 1, |
| 185 |
column: 7, |
| 186 |
offset: 6, |
| 187 |
fragment_len: 3, |
| 188 |
}, |
| 189 |
Position { |
| 190 |
line: 1, |
| 191 |
column: 10, |
| 192 |
offset: 9, |
| 193 |
fragment_len: 3, |
| 194 |
}, |
| 195 |
], |
| 196 |
); |
| 197 |
test_str_fragments( |
| 198 |
simple_parser_u8, |
| 199 |
b" foo |
| 200 |
bar |
| 201 |
baz", |
| 202 |
vec![ |
| 203 |
Position { |
| 204 |
line: 1, |
| 205 |
column: 2, |
| 206 |
offset: 1, |
| 207 |
fragment_len: 3, |
| 208 |
}, |
| 209 |
Position { |
| 210 |
line: 2, |
| 211 |
column: 9, |
| 212 |
offset: 13, |
| 213 |
fragment_len: 3, |
| 214 |
}, |
| 215 |
Position { |
| 216 |
line: 3, |
| 217 |
column: 13, |
| 218 |
offset: 29, |
| 219 |
fragment_len: 3, |
| 220 |
}, |
| 221 |
Position { |
| 222 |
line: 3, |
| 223 |
column: 16, |
| 224 |
offset: 32, |
| 225 |
fragment_len: 3, |
| 226 |
}, |
| 227 |
], |
| 228 |
); |
| 229 |
} |
| 230 |
|
| 231 |
fn find_substring<'a>( |
| 232 |
input: StrSpan<'a>, |
| 233 |
substr: &'static str, |
| 234 |
) -> IResult<StrSpan<'a>, StrSpan<'a>> { |
| 235 |
let substr_len = substr.len(); |
| 236 |
match input.find_substring(substr) { |
| 237 |
None => Err(nom::Err::Error(error_position!(input, ErrorKind::Tag))), |
| 238 |
Some(pos) => Ok(( |
| 239 |
input.take_from(pos + substr_len), |
| 240 |
input.take(pos + substr_len).take_from(pos), |
| 241 |
)), |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
#[cfg(feature = "alloc")] |
| 246 |
#[test] |
| 247 |
fn test_escaped_string() { |
| 248 |
#[allow(unused)] |
| 249 |
use nom::Needed; // https://github.com/Geal/nom/issues/780 |
| 250 |
fn string(i: StrSpan) -> IResult<StrSpan, String> { |
| 251 |
delimited( |
| 252 |
char('"'), |
| 253 |
escaped_transform( |
| 254 |
nom::character::complete::alpha1, |
| 255 |
'\\', |
| 256 |
nom::character::complete::anychar, |
| 257 |
), |
| 258 |
char('"'), |
| 259 |
) |
| 260 |
.parse(i) |
| 261 |
} |
| 262 |
|
| 263 |
let res = string(LocatedSpan::new("\"foo\\\"bar\"")); |
| 264 |
assert!(res.is_ok()); |
| 265 |
let (span, remaining) = res.unwrap(); |
| 266 |
assert_eq!(span.location_offset(), 10); |
| 267 |
assert_eq!(span.location_line(), 1); |
| 268 |
assert_eq!(span.fragment(), &""); |
| 269 |
assert_eq!(remaining, "foo\"bar".to_string()); |
| 270 |
} |
| 271 |
|
| 272 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 273 |
fn plague(i: StrSpan) -> IResult<StrSpan, Vec<StrSpan>> { |
| 274 |
let (i, ojczyzno) = find_substring(i, "Ojczyzno")?; |
| 275 |
let (i, jak) = many0(|i| find_substring(i, "jak ")).parse(i)?; |
| 276 |
let (i, zielona) = find_substring(i, "Zielona")?; |
| 277 |
let (i, _) = preceded(take_until("."), tag(".")).parse(i)?; |
| 278 |
|
| 279 |
Ok({ |
| 280 |
let mut res = vec![ojczyzno]; |
| 281 |
res.extend(jak); |
| 282 |
res.push(zielona); |
| 283 |
(i, res) |
| 284 |
}) |
| 285 |
} |
| 286 |
|
| 287 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 288 |
#[test] |
| 289 |
fn it_locates_complex_fragments() { |
| 290 |
// Pan Tadeusz. https://pl.m.wikisource.org/wiki/Pan_Tadeusz_(wyd._1834)/Ksi%C4%99ga_pierwsza |
| 291 |
let input = "Litwo! Ojczyzno moja! ty jestes jak zdrowie; |
| 292 |
Ile cie trzeba cenic, ten tylko sie dowie |
| 293 |
Kto cie stracil. Dzis pieknosc twa w calej ozdobie |
| 294 |
Widze i opisuje, bo tesknie po tobie. |
| 295 |
|
| 296 |
Panno swieta, co jasnej bronisz Czestochowy |
| 297 |
I w Ostrej swiecisz Bramie! Ty, co grod zamkowy |
| 298 |
|
| 299 |
Nowogrodzki ochraniasz z jego wiernym ludem! |
| 300 |
Jak mnie dziecko do zdrowia powrocilas cudem, |
| 301 |
(Gdy od placzacej matki, pod Twoje opieke |
| 302 |
Ofiarowany, martwa podnioslem powieke; |
| 303 |
I zaraz moglem pieszo, do Twych swiatyn progu |
| 304 |
Isc za wrocone zycie podziekowac Bogu;) |
| 305 |
Tak nas powrocisz cudem na Ojczyzny lono. |
| 306 |
Tymczasem przenos moje dusze uteskniona |
| 307 |
Do tych pagorkow lesnych, do tych lak zielonych, |
| 308 |
Szeroko nad blekitnym Niemnem rosciagnionych; |
| 309 |
Do tych pol malowanych zbozem rozmaitem, |
| 310 |
Wyzlacanych pszenica, posrebrzanych zytem; |
| 311 |
Gdzie bursztynowy swierzop, gryka jak snieg biala, |
| 312 |
Gdzie panienskim rumiencem dziecielina pala, |
| 313 |
A wszystko przepasane jakby wstega, miedza |
| 314 |
Zielona, na niej zrzadka ciche grusze siedza."; |
| 315 |
|
| 316 |
let expected = vec![ |
| 317 |
Position { |
| 318 |
line: 1, |
| 319 |
column: 8, |
| 320 |
offset: 7, |
| 321 |
fragment_len: 8, |
| 322 |
}, |
| 323 |
Position { |
| 324 |
line: 1, |
| 325 |
column: 33, |
| 326 |
offset: 32, |
| 327 |
fragment_len: 4, |
| 328 |
}, |
| 329 |
Position { |
| 330 |
line: 21, |
| 331 |
column: 35, |
| 332 |
offset: 823, |
| 333 |
fragment_len: 4, |
| 334 |
}, |
| 335 |
Position { |
| 336 |
line: 24, |
| 337 |
column: 1, |
| 338 |
offset: 928, |
| 339 |
fragment_len: 7, |
| 340 |
}, |
| 341 |
]; |
| 342 |
|
| 343 |
test_str_fragments(plague, input, expected); |
| 344 |
} |
| 345 |
|
| 346 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 347 |
#[test] |
| 348 |
fn test_take_until_str() { |
| 349 |
fn parser(i: StrSpan) -> IResult<StrSpan, ()> { |
| 350 |
let (i, _) = delimited(take_until("foo"), tag("foo"), multispace0).parse(i)?; |
| 351 |
let (i, _) = delimited(take_until("bar"), tag("bar"), multispace0).parse(i)?; |
| 352 |
let (i, _) = eof(i)?; |
| 353 |
Ok((i, ())) |
| 354 |
} |
| 355 |
let res = parser(LocatedSpan::new(" X foo Y bar ")); |
| 356 |
assert!(res.is_ok()); |
| 357 |
let (span, _) = res.unwrap(); |
| 358 |
assert_eq!(span.location_offset(), 13); |
| 359 |
assert_eq!(span.location_line(), 1); |
| 360 |
assert_eq!(*span.fragment(), ""); |
| 361 |
} |
| 362 |
|
| 363 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 364 |
#[test] |
| 365 |
fn test_take_until_u8() { |
| 366 |
fn parser(i: BytesSpan) -> IResult<BytesSpan, ()> { |
| 367 |
// Mix string and byte conditions. |
| 368 |
let (i, _) = delimited(take_until("foo"), tag("foo"), multispace0).parse(i)?; |
| 369 |
let (i, _) = delimited(take_until(&b"bar"[..]), tag(&b"bar"[..]), multispace0).parse(i)?; |
| 370 |
let (i, _) = eof(i)?; |
| 371 |
Ok((i, ())) |
| 372 |
} |
| 373 |
let res = parser(LocatedSpan::new(&b" X foo Y bar "[..])); |
| 374 |
assert!(res.is_ok()); |
| 375 |
let (span, _) = res.unwrap(); |
| 376 |
assert_eq!(span.location_offset(), 13); |
| 377 |
assert_eq!(span.location_line(), 1); |
| 378 |
assert_eq!(*span.fragment(), &b""[..]); |
| 379 |
} |