1use crate::quotes::{DelimitedReferences, QUOTES, Reading, read_reference};
2use nom::{
3 IResult, Parser,
4 branch::alt,
5 bytes::complete::{tag, take_while},
6 character::complete::{char, line_ending},
7 combinator::eof,
8 multi::{many0, many1},
9 sequence::{preceded, terminated},
10};
11use std::cell::{Cell, OnceCell, RefCell};
12use std::collections::HashMap;
13use std::ops::Range;
14
15#[derive(Debug, Clone, PartialEq)]
16pub struct Link {
17 pub id: Option<String>,
18 pub values: Vec<Link>,
19 pub children: Vec<Link>,
20 pub is_indented_id: bool,
21 pub nested: Option<Vec<Link>>,
24 pub id_span: Option<Range<usize>>,
30}
31
32impl Link {
33 pub fn new_singlet(id: String) -> Self {
34 Link {
35 id: Some(id),
36 values: vec![],
37 children: vec![],
38 is_indented_id: false,
39 nested: None,
40 id_span: None,
41 }
42 }
43
44 pub fn new_indented_id(id: String) -> Self {
45 Link {
46 id: Some(id),
47 values: vec![],
48 children: vec![],
49 is_indented_id: true,
50 nested: None,
51 id_span: None,
52 }
53 }
54
55 pub fn new_value(values: Vec<Link>) -> Self {
56 Link {
57 id: None,
58 values,
59 children: vec![],
60 is_indented_id: false,
61 nested: None,
62 id_span: None,
63 }
64 }
65
66 pub fn new_link(id: Option<String>, values: Vec<Link>) -> Self {
67 Link {
68 id,
69 values,
70 children: vec![],
71 is_indented_id: false,
72 nested: None,
73 id_span: None,
74 }
75 }
76
77 pub fn new_nested(body: Vec<Link>) -> Self {
80 Link {
81 id: None,
82 values: vec![],
83 children: vec![],
84 is_indented_id: false,
85 nested: Some(body),
86 id_span: None,
87 }
88 }
89
90 pub fn with_children(mut self, children: Vec<Link>) -> Self {
91 self.children = children;
92 self
93 }
94
95 pub fn with_id_span(mut self, span: Range<usize>) -> Self {
97 self.id_span = Some(span);
98 self
99 }
100}
101
102pub const DEFAULT_MAX_DEPTH: usize = 64;
111
112pub struct ParserState {
113 indentation_stack: RefCell<Vec<usize>>,
114 base_indentation: RefCell<Option<usize>>,
115 nested_depth: RefCell<usize>,
116 context_depth: RefCell<usize>,
119 max_depth: usize,
120 too_deep: RefCell<Option<usize>>,
123 furthest: RefCell<FurthestFailure>,
124 unreadable_lines: RefCell<HashMap<LineKey, LineFailure>>,
125 references: OnceCell<DelimitedReferences>,
128 source: Option<Box<str>>,
133 document_start: Cell<usize>,
136}
137
138type LineKey = (usize, bool);
143
144type LineFailure = (usize, nom::error::ErrorKind);
147
148#[derive(Debug, Clone, Default)]
158struct FurthestFailure {
159 address: Option<usize>,
162 expected: Vec<&'static str>,
163}
164
165#[derive(Debug, Clone, PartialEq, Eq)]
167pub struct ParseFailure {
168 pub offset: usize,
170 pub expected: Vec<&'static str>,
174 pub kind: Option<nom::error::ErrorKind>,
178 pub max_depth_exceeded: Option<usize>,
181}
182
183pub struct SavedContext {
185 indentation_stack: Vec<usize>,
186 base_indentation: Option<usize>,
187 context_depth: usize,
188}
189
190impl Default for ParserState {
191 fn default() -> Self {
192 Self::new()
193 }
194}
195
196impl ParserState {
197 pub fn new() -> Self {
198 Self::with_max_depth(DEFAULT_MAX_DEPTH)
199 }
200
201 pub fn with_max_depth(max_depth: usize) -> Self {
203 ParserState {
204 indentation_stack: RefCell::new(vec![0]),
205 base_indentation: RefCell::new(None),
206 nested_depth: RefCell::new(0),
207 context_depth: RefCell::new(0),
208 max_depth,
209 too_deep: RefCell::new(None),
210 furthest: RefCell::new(FurthestFailure::default()),
211 unreadable_lines: RefCell::new(HashMap::new()),
212 references: OnceCell::new(),
213 source: None,
214 document_start: Cell::new(0),
215 }
216 }
217
218 fn written<'s>(&'s self, input: &'s str) -> &'s str {
224 match &self.source {
225 Some(source) if input.as_bytes().first().is_some_and(|b| QUOTES.contains(b)) => source
226 .len()
227 .checked_sub(input.len())
228 .and_then(|start| source.get(start..))
229 .unwrap_or(input),
230 _ => input,
231 }
232 }
233
234 fn references(&self, input: &str) -> &DelimitedReferences {
238 self.references
239 .get_or_init(|| DelimitedReferences::new(self.source.as_deref().unwrap_or(input)))
240 }
241
242 pub fn set_base_indentation(&self, indent: usize) {
243 let mut base = self.base_indentation.borrow_mut();
244 if base.is_none() {
245 *base = Some(indent);
246 }
247 }
248
249 pub fn get_base_indentation(&self) -> usize {
250 self.base_indentation.borrow().unwrap_or(0)
251 }
252
253 pub fn normalize_indentation(&self, indent: usize) -> usize {
254 let base = self.get_base_indentation();
255 indent.saturating_sub(base)
256 }
257
258 pub fn push_indentation(&self, indent: usize) {
259 self.indentation_stack.borrow_mut().push(indent);
260 }
261
262 pub fn pop_indentation(&self) {
263 let mut stack = self.indentation_stack.borrow_mut();
264 if stack.len() > 1 {
265 stack.pop();
266 }
267 }
268
269 pub fn current_indentation(&self) -> usize {
270 *self.indentation_stack.borrow().last().unwrap_or(&0)
271 }
272
273 pub fn check_indentation(&self, indent: usize) -> bool {
274 indent >= self.current_indentation()
275 }
276
277 pub fn enter_nested_context(&self) -> SavedContext {
280 let depth = self.depth() + 1;
281 let saved = SavedContext {
282 indentation_stack: self.indentation_stack.replace(vec![0]),
283 base_indentation: self.base_indentation.replace(None),
284 context_depth: self.context_depth.replace(depth),
285 };
286 *self.nested_depth.borrow_mut() += 1;
287 saved
288 }
289
290 pub fn exit_nested_context(&self, saved: SavedContext) {
292 *self.indentation_stack.borrow_mut() = saved.indentation_stack;
293 *self.base_indentation.borrow_mut() = saved.base_indentation;
294 *self.context_depth.borrow_mut() = saved.context_depth;
295 let mut depth = self.nested_depth.borrow_mut();
296 if *depth > 0 {
297 *depth -= 1;
298 }
299 }
300
301 pub fn is_inside_nested_context(&self) -> bool {
302 *self.nested_depth.borrow() > 0
303 }
304
305 pub fn depth(&self) -> usize {
308 *self.context_depth.borrow() + self.indentation_stack.borrow().len() - 1
309 }
310
311 fn check_depth<'a>(&self, at: &'a str, depth: usize) -> IResult<&'a str, ()> {
315 if depth <= self.max_depth {
316 return Ok((at, ()));
317 }
318 self.too_deep
319 .borrow_mut()
320 .get_or_insert(at.as_ptr() as usize);
321 Err(nom::Err::Failure(nom::error::Error::new(
322 at,
323 nom::error::ErrorKind::TooLarge,
324 )))
325 }
326
327 fn expected_at(&self, at: &str, what: &'static str) {
331 let address = at.as_ptr() as usize;
332 let mut furthest = self.furthest.borrow_mut();
333 match furthest.address {
334 Some(recorded) if recorded > address => {}
335 Some(recorded) if recorded == address => {
336 if !furthest.expected.contains(&what) {
337 furthest.expected.push(what);
338 }
339 }
340 _ => {
341 furthest.address = Some(address);
342 furthest.expected = vec![what];
343 }
344 }
345 }
346
347 fn failure(&self, document: &str, error: &nom::Err<nom::error::Error<&str>>) -> ParseFailure {
353 let base = document.as_ptr() as usize;
354 let (nom_offset, kind) = match error {
355 nom::Err::Error(e) | nom::Err::Failure(e) => (
356 (e.input.as_ptr() as usize).saturating_sub(base),
357 Some(e.code),
358 ),
359 nom::Err::Incomplete(_) => (document.len(), None),
360 };
361 if let Some(address) = *self.too_deep.borrow() {
362 return ParseFailure {
363 offset: address.saturating_sub(base).min(document.len()),
364 expected: Vec::new(),
365 kind,
366 max_depth_exceeded: Some(self.max_depth),
367 };
368 }
369 let furthest = self.furthest.borrow();
370 let tracked = furthest
371 .address
372 .map(|address| address.saturating_sub(base))
373 .unwrap_or(0);
374 let offset = tracked.max(nom_offset).min(document.len());
375 let expected = if tracked == offset {
376 furthest.expected.clone()
377 } else {
378 Vec::new()
381 };
382 ParseFailure {
383 offset,
384 expected,
385 kind,
386 max_depth_exceeded: None,
387 }
388 }
389}
390
391fn expected<'a, T>(
393 input: &'a str,
394 state: &ParserState,
395 what: &'static str,
396 kind: nom::error::ErrorKind,
397) -> IResult<&'a str, T> {
398 state.expected_at(input, what);
399 Err(nom::Err::Error(nom::error::Error::new(input, kind)))
400}
401
402pub(crate) fn is_whitespace_char(c: char) -> bool {
403 c == ' ' || c == '\t' || c == '\n' || c == '\r'
404}
405
406fn is_horizontal_whitespace(c: char) -> bool {
407 c == ' ' || c == '\t'
408}
409
410fn is_reference_char(c: char) -> bool {
411 !is_whitespace_char(c) && c != '(' && c != ':' && c != ')'
412}
413
414fn horizontal_whitespace(input: &str) -> IResult<&str, &str> {
415 take_while(is_horizontal_whitespace)(input)
416}
417
418fn whitespace(input: &str) -> IResult<&str, &str> {
419 take_while(is_whitespace_char)(input)
420}
421
422fn simple_reference(input: &str) -> IResult<&str, String> {
426 if !input.chars().next().is_some_and(is_reference_char) {
427 return Err(nom::Err::Error(nom::error::Error::new(
428 input,
429 nom::error::ErrorKind::TakeWhile1,
430 )));
431 }
432 let end = bare_reference_end(input, 0);
433 Ok((&input[end..], input[..end].to_string()))
434}
435
436pub(crate) fn bare_reference_end(text: &str, start: usize) -> usize {
446 let bytes = text.as_bytes();
447 let mut end = start + 1;
450 while end < bytes.len() {
451 match bytes[end] {
452 b':' => {
453 let mut after = end;
454 while bytes.get(after) == Some(&b':') {
455 after += 1;
456 }
457 let continues = bytes
458 .get(after)
459 .is_some_and(|next| !NOT_AFTER_INNER_COLONS.contains(next))
460 && crate::reference_literal::prefix_end(&text[after..]).is_none();
461 if !continues {
462 break;
463 }
464 end = after + 1;
465 }
466 b' ' | b'\t' | b'\n' | b'\r' | b'(' | b')' => break,
467 _ => end += 1,
468 }
469 }
470 end
471}
472
473const NOT_AFTER_INNER_COLONS: &[u8] = b" \t\n\r(:)\"'`";
475
476pub fn quoted_reference_end(document: &str, start: usize) -> Option<usize> {
483 let reading = read_reference(document.get(start..)?)?;
484 Some(start + reading.length)
485}
486
487fn delimited_reference<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, String> {
495 match state.references(input).read(state.written(input)) {
496 Some(Reading { value, length, .. }) => Ok((&input[length..], value)),
497 None => Err(nom::Err::Error(nom::error::Error::new(
498 input,
499 nom::error::ErrorKind::Tag,
500 ))),
501 }
502}
503
504fn reference<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, String> {
505 if let Some(prefix) = crate::reference_literal::prefix_end(input) {
506 if let Some(closing) = input[prefix..].find('}') {
507 let length = prefix + closing + 1;
508 if let Ok(value) = crate::decode_reference_literal(&input[..length]) {
509 return Ok((&input[length..], value));
510 }
511 }
512 state.expected_at(input, "a valid version 1 reference literal");
513 return Err(nom::Err::Failure(nom::error::Error::new(
514 input,
515 nom::error::ErrorKind::Tag,
516 )));
517 }
518 let parsed = alt((|i| delimited_reference(i, state), simple_reference)).parse(input);
521 if parsed.is_err() {
522 state.expected_at(input, "a reference");
523 }
524 parsed
525}
526
527fn spanned_reference<'a>(
530 input: &'a str,
531 state: &ParserState,
532) -> IResult<&'a str, (String, Range<usize>)> {
533 let (rest, value) = reference(input, state)?;
534 let start = (input.as_ptr() as usize).saturating_sub(state.document_start.get());
535 let end = start + (input.len() - rest.len());
536 Ok((rest, (value, start..end)))
537}
538
539fn eol<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, &'a str> {
540 let parsed = alt((
541 preceded(horizontal_whitespace, alt((line_ending, tag("\r")))),
542 preceded(horizontal_whitespace, eof),
543 |i| nested_group_end(i, state),
544 ))
545 .parse(input);
546 if parsed.is_err() {
547 state.expected_at(input, "end of line");
548 }
549 parsed
550}
551
552fn nested_group_end<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, &'a str> {
555 if !state.is_inside_nested_context() {
556 return Err(nom::Err::Error(nom::error::Error::new(
557 input,
558 nom::error::ErrorKind::Verify,
559 )));
560 }
561 let (rest, _) = horizontal_whitespace(input)?;
562 if rest.starts_with(')') {
563 Ok((rest, ""))
564 } else {
565 expected(rest, state, "\")\"", nom::error::ErrorKind::Char)
566 }
567}
568
569fn skip_empty_lines(input: &str) -> &str {
572 let mut rest = input;
573 loop {
574 let line_start = rest.trim_start_matches(is_horizontal_whitespace);
575 match strip_line_ending(line_start) {
576 Some(next) => rest = next,
577 None => return rest,
578 }
579 }
580}
581
582fn strip_line_ending(input: &str) -> Option<&str> {
583 input
584 .strip_prefix("\r\n")
585 .or_else(|| input.strip_prefix('\n'))
586 .or_else(|| input.strip_prefix('\r'))
587}
588
589fn reference_or_link<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
590 alt((
591 |i| nested_group(i, state),
592 (|i| spanned_reference(i, state))
593 .map(|(id, span)| Link::new_singlet(id).with_id_span(span)),
594 ))
595 .parse(input)
596}
597
598fn single_line_value_and_whitespace<'a>(
599 input: &'a str,
600 state: &ParserState,
601) -> IResult<&'a str, Link> {
602 preceded(horizontal_whitespace, |i| reference_or_link(i, state)).parse(input)
603}
604
605fn single_line_values<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Vec<Link>> {
606 many1(|i| single_line_value_and_whitespace(i, state)).parse(input)
607}
608
609fn single_line_link<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
610 let (input, _) = horizontal_whitespace(input)?;
611 let (input, (id, span)) = spanned_reference(input, state)?;
612 let (input, _) = horizontal_whitespace(input)?;
613 let (input, _) = colon(input, state)?;
614 let (input, values) = single_line_values(input, state)?;
615 Ok((input, Link::new_link(Some(id), values).with_id_span(span)))
616}
617
618fn colon<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, char> {
620 character(':', input, state, "\":\"")
621}
622
623fn character<'a>(
625 wanted: char,
626 input: &'a str,
627 state: &ParserState,
628 what: &'static str,
629) -> IResult<&'a str, char> {
630 let parsed: IResult<&'a str, char> = char(wanted).parse(input);
631 match parsed {
632 Ok(parsed) => Ok(parsed),
633 Err(_) => expected(input, state, what, nom::error::ErrorKind::Char),
634 }
635}
636
637fn single_line_value_link<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
638 (|i| single_line_values(i, state))
639 .map(Link::new_value)
640 .parse(input)
641}
642
643fn indented_id_link<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
644 let (input, (id, span)) = spanned_reference(input, state)?;
645 let (input, _) = horizontal_whitespace(input)?;
646 let (input, _) = colon(input, state)?;
647 let (input, _) = eol(input, state)?;
648 Ok((input, Link::new_indented_id(id).with_id_span(span)))
649}
650
651fn nested_group<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
655 let (body_input, _) = character('(', input, state, "\"(\"")?;
656 state.check_depth(input, state.depth() + 1)?;
657 let saved = state.enter_nested_context();
658 let result = nested_group_body(body_input, state);
659 state.exit_nested_context(saved);
660 result
661}
662
663fn nested_group_body<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
664 match links(skip_empty_lines(input), state) {
665 Ok((rest, body)) => {
666 let (rest, _) = whitespace(rest)?;
667 let (rest, _) = closing_parenthesis(rest, state)?;
668 return Ok((rest, Link::new_nested(body)));
669 }
670 Err(failure @ nom::Err::Failure(_)) => return Err(failure),
671 Err(_) => {}
672 }
673 let (rest, _) = whitespace(input)?;
674 let (rest, _) = closing_parenthesis(rest, state)?;
675 Ok((rest, Link::new_nested(vec![])))
676}
677
678fn closing_parenthesis<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, char> {
680 character(')', input, state, "\")\"")
681}
682
683fn single_line_any_link<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
684 alt((
685 terminated(|i| single_line_link(i, state), |i| eol(i, state)),
686 terminated(|i| single_line_value_link(i, state), |i| eol(i, state)),
687 ))
688 .parse(input)
689}
690
691fn any_link<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
698 let key = (input.as_ptr() as usize, state.is_inside_nested_context());
699 if let Some(&(offset, kind)) = state.unreadable_lines.borrow().get(&key) {
700 return Err(nom::Err::Error(nom::error::Error::new(
701 &input[offset..],
702 kind,
703 )));
704 }
705 let parsed = read_any_link(input, state);
706 if let Err(nom::Err::Error(error)) = &parsed {
707 let offset = error.input.as_ptr() as usize - input.as_ptr() as usize;
708 state
709 .unreadable_lines
710 .borrow_mut()
711 .insert(key, (offset, error.code));
712 }
713 parsed
714}
715
716fn read_any_link<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
724 let (rest, group) = match nested_group(input, state) {
725 Ok(parsed) => parsed,
726 Err(_) if input.starts_with('(') => {
731 return spanned_reference(input, state)
732 .map(|(rest, (id, span))| (rest, Link::new_singlet(id).with_id_span(span)));
733 }
734 Err(_) => {
735 return alt((
736 |i| indented_id_link(i, state),
737 |i| single_line_any_link(i, state),
738 ))
739 .parse(input);
740 }
741 };
742 if let Ok((rest, _)) = eol(rest, state) {
743 return Ok((rest, group));
744 }
745 let (rest, more) = many0(|i| single_line_value_and_whitespace(i, state)).parse(rest)?;
746 let (rest, _) = eol(rest, state)?;
747 let mut values = Vec::with_capacity(more.len() + 1);
748 values.push(group);
749 values.extend(more);
750 Ok((rest, Link::new_value(values)))
751}
752
753fn count_indentation(input: &str) -> IResult<&str, usize> {
754 take_while(|c| c == ' ').map(|s: &str| s.len()).parse(input)
755}
756
757fn push_indentation<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, ()> {
758 let (input, spaces) = count_indentation(skip_empty_lines(input))?;
759 let normalized_spaces = state.normalize_indentation(spaces);
760 let current = state.current_indentation();
761
762 if normalized_spaces > current {
763 state.push_indentation(normalized_spaces);
764 Ok((input, ()))
765 } else {
766 Err(nom::Err::Error(nom::error::Error::new(
767 input,
768 nom::error::ErrorKind::Verify,
769 )))
770 }
771}
772
773fn check_indentation<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, ()> {
774 let (input, spaces) = count_indentation(input)?;
775 let normalized_spaces = state.normalize_indentation(spaces);
776
777 if state.check_indentation(normalized_spaces) {
778 Ok((input, ()))
779 } else {
780 Err(nom::Err::Error(nom::error::Error::new(
781 input,
782 nom::error::ErrorKind::Verify,
783 )))
784 }
785}
786
787fn element<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
788 let start = input;
789 let (input, link) = any_link(input, state)?;
790 state.check_depth(start, state.depth())?;
793
794 let indentation = state.indentation_stack.borrow().clone();
795 if let Ok((child_input, _)) = push_indentation(input, state) {
796 match links(child_input, state) {
797 Ok((rest, children)) => return Ok((rest, link.with_children(children))),
798 Err(failure @ nom::Err::Failure(_)) => return Err(failure),
799 Err(_) => {}
800 }
801 state.indentation_stack.replace(indentation);
804 }
805 Ok((input, link))
806}
807
808fn first_line<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
809 let (input, spaces) = count_indentation(input)?;
812 state.set_base_indentation(spaces);
813 element(input, state)
814}
815
816fn line<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
817 preceded(|i| check_indentation(i, state), |i| element(i, state)).parse(skip_empty_lines(input))
819}
820
821fn links<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Vec<Link>> {
822 let (input, first) = first_line(input, state)?;
823 let (input, rest) = many0(|i| line(i, state)).parse(input)?;
824
825 state.pop_indentation();
826
827 let mut result = vec![first];
828 result.extend(rest);
829 Ok((input, result))
830}
831
832pub fn parse_document(input: &str) -> IResult<&str, Vec<Link>> {
833 let state = ParserState::new();
834 document(input, &state)
835}
836
837pub fn parse_document_with_diagnostics(input: &str) -> Result<Vec<Link>, ParseFailure> {
844 parse_document_with_max_depth(input, DEFAULT_MAX_DEPTH)
845}
846
847pub fn parse_document_with_max_depth(
851 input: &str,
852 max_depth: usize,
853) -> Result<Vec<Link>, ParseFailure> {
854 let state = ParserState::with_max_depth(max_depth);
855 match document(input, &state) {
856 Ok((_, links)) => Ok(links),
857 Err(error) => Err(state.failure(input, &error)),
858 }
859}
860
861pub(crate) fn parse_prepared_document(
865 prepared: &str,
866 source: &str,
867 max_depth: usize,
868) -> Result<Vec<Link>, ParseFailure> {
869 debug_assert_eq!(prepared.len(), source.len());
870 let mut state = ParserState::with_max_depth(max_depth);
871 if prepared != source {
872 state.source = Some(source.into());
873 }
874 match document(prepared, &state) {
875 Ok((_, links)) => Ok(links),
876 Err(error) => Err(state.failure(prepared, &error)),
877 }
878}
879
880fn document<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Vec<Link>> {
881 state.references(input);
884 state.document_start.set(input.as_ptr() as usize);
885
886 let document = skip_empty_lines(input);
888
889 if document.trim_matches(is_whitespace_char).is_empty() {
891 return Ok(("", vec![]));
892 }
893
894 let (rest, result) = links(document, state)?;
895 let (rest, _) = whitespace(rest)?;
896 let end: IResult<&'a str, &'a str> = eof(rest);
897 let (rest, _) = match end {
898 Ok(parsed) => parsed,
899 Err(_) => return expected(rest, state, "end of input", nom::error::ErrorKind::Eof),
900 };
901
902 Ok((rest, result))
903}