Skip to content

Commit be46c67

Browse files
committed
Add span to VariantKey
1 parent 2761527 commit be46c67

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

fluent-bundle/src/resolver/expression.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ impl<'bundle> WriteValue<'bundle> for ast::Expression<&'bundle str> {
3232
FluentValue::String(_) | FluentValue::Number(_) => {
3333
for variant in variants {
3434
let key = match variant.key {
35-
ast::VariantKey::Identifier { name } => name.into(),
36-
ast::VariantKey::NumberLiteral { value } => {
35+
ast::VariantKey::Identifier { name, .. } => name.into(),
36+
ast::VariantKey::NumberLiteral { value, .. } => {
3737
FluentValue::try_number(value)
3838
}
3939
};

fluent-syntax/src/ast/mod.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -785,8 +785,16 @@ pub struct Variant<S> {
785785
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
786786
#[cfg_attr(feature = "serde", serde(tag = "type"))]
787787
pub enum VariantKey<S> {
788-
Identifier { name: S },
789-
NumberLiteral { value: S },
788+
Identifier {
789+
name: S,
790+
#[cfg(feature = "spans")]
791+
span: Span,
792+
},
793+
NumberLiteral {
794+
value: S,
795+
#[cfg(feature = "spans")]
796+
span: Span,
797+
},
790798
}
791799

792800
/// Fluent [`Comment`].
@@ -1539,12 +1547,12 @@ pub enum Expression<S> {
15391547
///
15401548
/// ```
15411549
/// #![cfg(feature = "spans")]
1542-
///
1550+
///
15431551
/// use fluent_syntax::parser;
15441552
/// use fluent_syntax::ast::*;
1545-
///
1553+
///
15461554
/// let ftl = "hello-world = Hello, World!";
1547-
///
1555+
///
15481556
/// let resource = parser::parse(ftl).expect("Failed to parse an FTL resource.");
15491557
/// let Entry::Message(Message { ref id, .. }) = resource.body[0] else { unreachable!() };
15501558
///
@@ -1563,4 +1571,4 @@ impl Deref for Span {
15631571
fn deref(&self) -> &Self::Target {
15641572
&self.0
15651573
}
1566-
}
1574+
}

fluent-syntax/src/parser/core.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use super::{
33
errors::{ErrorKind, ParserError},
44
slice::Slice,
55
};
6-
use crate::ast;
6+
use crate::ast::{self, Span};
77

88
pub type Result<T> = std::result::Result<T, ParserError>;
99

@@ -248,12 +248,22 @@ where
248248
self.skip_blank();
249249

250250
let key = if self.is_number_start() {
251+
#[cfg(feature = "spans")]
252+
let start = self.ptr;
253+
251254
ast::VariantKey::NumberLiteral {
252255
value: self.get_number_literal()?,
256+
#[cfg(feature = "spans")]
257+
span: Span(start..self.ptr),
253258
}
254259
} else {
260+
#[cfg(feature = "spans")]
261+
let start = self.ptr;
262+
255263
ast::VariantKey::Identifier {
256264
name: self.get_identifier()?.name,
265+
#[cfg(feature = "spans")]
266+
span: Span(start..self.ptr),
257267
}
258268
};
259269

fluent-syntax/src/serializer.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ impl Serializer {
325325

326326
fn serialize_variant_key<'s, S: Slice<'s>>(&mut self, key: &VariantKey<S>) {
327327
match key {
328-
VariantKey::NumberLiteral { value } | VariantKey::Identifier { name: value } => {
328+
VariantKey::NumberLiteral { value, .. } | VariantKey::Identifier { name: value, .. } => {
329329
self.writer.write_literal(value.as_ref());
330330
}
331331
}
@@ -577,7 +577,7 @@ mod test {
577577
let mut ast = parse(message).expect("failed to parse ftl resource");
578578

579579
let one_variant = Variant {
580-
key: VariantKey::Identifier { name: "one" },
580+
key: VariantKey::Identifier { name: "one", span: Span::default() },
581581
value: Pattern {
582582
elements: vec![
583583
PatternElement::Placeable {

0 commit comments

Comments
 (0)