| 1 |
#![allow(clippy::type_complexity)] |
| 2 |
|
| 3 |
use crate::RawString; |
| 4 |
#[cfg(not(feature = "unbounded"))] |
| 5 |
use toml_parser::parser::RecursionGuard; |
| 6 |
use toml_parser::parser::ValidateWhitespace; |
| 7 |
use winnow::stream::Stream as _; |
| 8 |
|
| 9 |
pub(crate) mod array; |
| 10 |
#[cfg(feature = "debug")] |
| 11 |
pub(crate) mod debug; |
| 12 |
pub(crate) mod document; |
| 13 |
pub(crate) mod inline_table; |
| 14 |
pub(crate) mod key; |
| 15 |
pub(crate) mod value; |
| 16 |
|
| 17 |
pub(crate) fn parse_document<'s>( |
| 18 |
source: toml_parser::Source<'s>, |
| 19 |
errors: &mut dyn prelude::ErrorSink, |
| 20 |
) -> crate::Document<&'s str> { |
| 21 |
let tokens = source.lex().into_vec(); |
| 22 |
|
| 23 |
let mut events = Vec::with_capacity(tokens.len()); |
| 24 |
let mut receiver = ValidateWhitespace::new(&mut events, source); |
| 25 |
#[cfg(not(feature = "unbounded"))] |
| 26 |
let mut receiver = RecursionGuard::new(&mut receiver, LIMIT); |
| 27 |
#[cfg(not(feature = "unbounded"))] |
| 28 |
let receiver = &mut receiver; |
| 29 |
#[cfg(feature = "unbounded")] |
| 30 |
let receiver = &mut receiver; |
| 31 |
toml_parser::parser::parse_document(&tokens, receiver, errors); |
| 32 |
|
| 33 |
let mut input = prelude::Input::new(&events); |
| 34 |
let doc = document::document(&mut input, source, errors); |
| 35 |
doc |
| 36 |
} |
| 37 |
|
| 38 |
pub(crate) fn parse_key( |
| 39 |
source: toml_parser::Source<'_>, |
| 40 |
errors: &mut dyn prelude::ErrorSink, |
| 41 |
) -> crate::Key { |
| 42 |
let tokens = source.lex().into_vec(); |
| 43 |
|
| 44 |
let mut events = Vec::with_capacity(tokens.len()); |
| 45 |
let mut receiver = ValidateWhitespace::new(&mut events, source); |
| 46 |
#[cfg(not(feature = "unbounded"))] |
| 47 |
let mut receiver = RecursionGuard::new(&mut receiver, LIMIT); |
| 48 |
#[cfg(not(feature = "unbounded"))] |
| 49 |
let receiver = &mut receiver; |
| 50 |
#[cfg(feature = "unbounded")] |
| 51 |
let receiver = &mut receiver; |
| 52 |
toml_parser::parser::parse_simple_key(&tokens, receiver, errors); |
| 53 |
|
| 54 |
if let Some(event) = events |
| 55 |
.iter() |
| 56 |
.find(|e| e.kind() == toml_parser::parser::EventKind::SimpleKey) |
| 57 |
{ |
| 58 |
let (raw, key) = key::on_simple_key(event, source, errors); |
| 59 |
crate::Key::new(key).with_repr_unchecked(crate::Repr::new_unchecked(raw)) |
| 60 |
} else { |
| 61 |
let key = source.input(); |
| 62 |
let raw = RawString::with_span(0..source.input().len()); |
| 63 |
crate::Key::new(key).with_repr_unchecked(crate::Repr::new_unchecked(raw)) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
pub(crate) fn parse_key_path( |
| 68 |
source: toml_parser::Source<'_>, |
| 69 |
errors: &mut dyn prelude::ErrorSink, |
| 70 |
) -> Vec<crate::Key> { |
| 71 |
let tokens = source.lex().into_vec(); |
| 72 |
|
| 73 |
let mut events = Vec::with_capacity(tokens.len()); |
| 74 |
let mut receiver = ValidateWhitespace::new(&mut events, source); |
| 75 |
#[cfg(not(feature = "unbounded"))] |
| 76 |
let mut receiver = RecursionGuard::new(&mut receiver, LIMIT); |
| 77 |
#[cfg(not(feature = "unbounded"))] |
| 78 |
let receiver = &mut receiver; |
| 79 |
#[cfg(feature = "unbounded")] |
| 80 |
let receiver = &mut receiver; |
| 81 |
toml_parser::parser::parse_key(&tokens, receiver, errors); |
| 82 |
|
| 83 |
let mut input = prelude::Input::new(&events); |
| 84 |
let mut prefix = None; |
| 85 |
let mut path = None; |
| 86 |
let mut key = None; |
| 87 |
let mut suffix = None; |
| 88 |
while let Some(event) = input.next_token() { |
| 89 |
match event.kind() { |
| 90 |
toml_parser::parser::EventKind::Whitespace => { |
| 91 |
let raw = RawString::with_span(event.span().start()..event.span().end()); |
| 92 |
if prefix.is_none() { |
| 93 |
prefix = Some(raw); |
| 94 |
} else if suffix.is_none() { |
| 95 |
suffix = Some(raw); |
| 96 |
} |
| 97 |
} |
| 98 |
_ => { |
| 99 |
let (local_path, local_key) = key::on_key(event, &mut input, source, errors); |
| 100 |
path = Some(local_path); |
| 101 |
key = local_key; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
if let Some(mut key) = key { |
| 106 |
if let Some(prefix) = prefix { |
| 107 |
key.leaf_decor.set_prefix(prefix); |
| 108 |
} |
| 109 |
if let Some(suffix) = suffix { |
| 110 |
key.leaf_decor.set_suffix(suffix); |
| 111 |
} |
| 112 |
let mut path = path.unwrap_or_default(); |
| 113 |
path.push(key); |
| 114 |
path |
| 115 |
} else { |
| 116 |
Default::default() |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
pub(crate) fn parse_value( |
| 121 |
source: toml_parser::Source<'_>, |
| 122 |
errors: &mut dyn prelude::ErrorSink, |
| 123 |
) -> crate::Value { |
| 124 |
let tokens = source.lex().into_vec(); |
| 125 |
|
| 126 |
let mut events = Vec::with_capacity(tokens.len()); |
| 127 |
let mut receiver = ValidateWhitespace::new(&mut events, source); |
| 128 |
#[cfg(not(feature = "unbounded"))] |
| 129 |
let mut receiver = RecursionGuard::new(&mut receiver, LIMIT); |
| 130 |
#[cfg(not(feature = "unbounded"))] |
| 131 |
let receiver = &mut receiver; |
| 132 |
#[cfg(feature = "unbounded")] |
| 133 |
let receiver = &mut receiver; |
| 134 |
toml_parser::parser::parse_value(&tokens, receiver, errors); |
| 135 |
|
| 136 |
let mut input = prelude::Input::new(&events); |
| 137 |
let value = value::value(&mut input, source, errors); |
| 138 |
value |
| 139 |
} |
| 140 |
|
| 141 |
#[cfg(not(feature = "unbounded"))] |
| 142 |
const LIMIT: u32 = 80; |
| 143 |
|
| 144 |
pub(crate) mod prelude { |
| 145 |
pub(crate) use toml_parser::ErrorSink; |
| 146 |
pub(crate) use toml_parser::ParseError; |
| 147 |
pub(crate) use toml_parser::parser::EventKind; |
| 148 |
pub(crate) use winnow::stream::Stream as _; |
| 149 |
|
| 150 |
pub(crate) type Input<'i> = winnow::stream::TokenSlice<'i, toml_parser::parser::Event>; |
| 151 |
|
| 152 |
#[cfg(feature = "debug")] |
| 153 |
pub(crate) use super::debug::TraceScope; |
| 154 |
#[cfg(feature = "debug")] |
| 155 |
pub(crate) use super::debug::trace; |
| 156 |
} |