time/format_description/parse/ast.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
//! AST for parsing format descriptions.
use alloc::string::String;
use alloc::vec::Vec;
use core::iter;
use core::iter::Peekable;
use super::{lexer, Error, Location, Span};
/// One part of a complete format description.
#[allow(variant_size_differences)]
pub(super) enum Item<'a> {
/// A literal string, formatted and parsed as-is.
Literal {
/// The string itself.
value: &'a [u8],
/// Where the string originates from in the format string.
_span: Span,
},
/// A sequence of brackets. The first acts as the escape character.
EscapedBracket {
/// The first bracket.
_first: Location,
/// The second bracket.
_second: Location,
},
/// Part of a type, along with its modifiers.
Component {
/// Where the opening bracket was in the format string.
_opening_bracket: Location,
/// Whitespace between the opening bracket and name.
_leading_whitespace: Option<Whitespace<'a>>,
/// The name of the component.
name: Name<'a>,
/// The modifiers for the component.
modifiers: Vec<Modifier<'a>>,
/// Whitespace between the modifiers and closing bracket.
_trailing_whitespace: Option<Whitespace<'a>>,
/// Where the closing bracket was in the format string.
_closing_bracket: Location,
},
}
/// Whitespace within a component.
pub(super) struct Whitespace<'a> {
/// The whitespace itself.
pub(super) _value: &'a [u8],
/// Where the whitespace was in the format string.
pub(super) span: Span,
}
/// The name of a component.
pub(super) struct Name<'a> {
/// The name itself.
pub(super) value: &'a [u8],
/// Where the name was in the format string.
pub(super) span: Span,
}
/// A modifier for a component.
pub(super) struct Modifier<'a> {
/// Whitespace preceding the modifier.
pub(super) _leading_whitespace: Whitespace<'a>,
/// The key of the modifier.
pub(super) key: Key<'a>,
/// Where the colon of the modifier was in the format string.
pub(super) _colon: Location,
/// The value of the modifier.
pub(super) value: Value<'a>,
}
/// The key of a modifier.
pub(super) struct Key<'a> {
/// The key itself.
pub(super) value: &'a [u8],
/// Where the key was in the format string.
pub(super) span: Span,
}
/// The value of a modifier.
pub(super) struct Value<'a> {
/// The value itself.
pub(super) value: &'a [u8],
/// Where the value was in the format string.
pub(super) span: Span,
}
/// Parse the provided tokens into an AST.
pub(super) fn parse<'a>(
tokens: impl Iterator<Item = lexer::Token<'a>>,
) -> impl Iterator<Item = Result<Item<'a>, Error>> {
let mut tokens = tokens.peekable();
iter::from_fn(move || {
Some(match tokens.next()? {
lexer::Token::Literal { value, span } => Ok(Item::Literal { value, _span: span }),
lexer::Token::Bracket {
kind: lexer::BracketKind::Opening,
location,
} => {
// escaped bracket
if let Some(&lexer::Token::Bracket {
kind: lexer::BracketKind::Opening,
location: second_location,
}) = tokens.peek()
{
tokens.next(); // consume
Ok(Item::EscapedBracket {
_first: location,
_second: second_location,
})
}
// component
else {
parse_component(location, &mut tokens)
}
}
lexer::Token::Bracket {
kind: lexer::BracketKind::Closing,
location: _,
} => unreachable!(
"internal error: closing bracket should have been consumed by `parse_component`",
),
lexer::Token::ComponentPart {
kind: _,
value: _,
span: _,
} => unreachable!(
"internal error: component part should have been consumed by `parse_component`",
),
})
})
}
/// Parse a component. This assumes that the opening bracket has already been consumed.
fn parse_component<'a>(
opening_bracket: Location,
tokens: &mut Peekable<impl Iterator<Item = lexer::Token<'a>>>,
) -> Result<Item<'a>, Error> {
let leading_whitespace = if let Some(&lexer::Token::ComponentPart {
kind: lexer::ComponentKind::Whitespace,
value,
span,
}) = tokens.peek()
{
tokens.next(); // consume
Some(Whitespace {
_value: value,
span,
})
} else {
None
};
let name = if let Some(&lexer::Token::ComponentPart {
kind: lexer::ComponentKind::NotWhitespace,
value,
span,
}) = tokens.peek()
{
tokens.next(); // consume
Name { value, span }
} else {
let span = leading_whitespace.map_or_else(
|| Span {
start: opening_bracket,
end: opening_bracket,
},
|whitespace| whitespace.span.shrink_to_end(),
);
return Err(Error {
_inner: span.error("expected component name"),
public: crate::error::InvalidFormatDescription::MissingComponentName {
index: span.start_byte(),
},
});
};
let mut modifiers = Vec::new();
let trailing_whitespace = loop {
let whitespace = if let Some(&lexer::Token::ComponentPart {
kind: lexer::ComponentKind::Whitespace,
value,
span,
}) = tokens.peek()
{
tokens.next(); // consume
Whitespace {
_value: value,
span,
}
} else {
break None;
};
if let Some(&lexer::Token::ComponentPart {
kind: lexer::ComponentKind::NotWhitespace,
value,
span,
}) = tokens.peek()
{
tokens.next(); // consume
let colon_index = match value.iter().position(|&b| b == b':') {
Some(index) => index,
None => {
return Err(Error {
_inner: span.error("modifier must be of the form `key:value`"),
public: crate::error::InvalidFormatDescription::InvalidModifier {
value: String::from_utf8_lossy(value).into_owned(),
index: span.start_byte(),
},
});
}
};
let key = &value[..colon_index];
let value = &value[colon_index + 1..];
if key.is_empty() {
return Err(Error {
_inner: span.shrink_to_start().error("expected modifier key"),
public: crate::error::InvalidFormatDescription::InvalidModifier {
value: String::new(),
index: span.start_byte(),
},
});
}
if value.is_empty() {
return Err(Error {
_inner: span.shrink_to_end().error("expected modifier value"),
public: crate::error::InvalidFormatDescription::InvalidModifier {
value: String::new(),
index: span.shrink_to_end().start_byte(),
},
});
}
modifiers.push(Modifier {
_leading_whitespace: whitespace,
key: Key {
value: key,
span: span.subspan(..colon_index),
},
_colon: span.start.offset(colon_index),
value: Value {
value,
span: span.subspan(colon_index + 1..),
},
});
} else {
break Some(whitespace);
}
};
let closing_bracket = if let Some(&lexer::Token::Bracket {
kind: lexer::BracketKind::Closing,
location,
}) = tokens.peek()
{
tokens.next(); // consume
location
} else {
return Err(Error {
_inner: opening_bracket.error("unclosed bracket"),
public: crate::error::InvalidFormatDescription::UnclosedOpeningBracket {
index: opening_bracket.byte,
},
});
};
Ok(Item::Component {
_opening_bracket: opening_bracket,
_leading_whitespace: leading_whitespace,
name,
modifiers,
_trailing_whitespace: trailing_whitespace,
_closing_bracket: closing_bracket,
})
}