Skip to content

Commit e4612e8

Browse files
committed
Revert "Span struct has been replaced by std::ops::Range<usize>"
This reverts commit a31f76a.
1 parent a31f76a commit e4612e8

File tree

8 files changed

+103
-73
lines changed

8 files changed

+103
-73
lines changed

fluent-syntax/src/ast/helper.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::Comment;
22
#[cfg(feature = "spans")]
3-
use super::Range;
3+
use super::Span;
44
#[cfg(feature = "serde")]
55
use serde::{Deserialize, Serialize};
66

@@ -14,12 +14,12 @@ pub enum CommentDef<S> {
1414
Single {
1515
content: S,
1616
#[cfg(feature = "spans")]
17-
span: Range<usize>,
17+
span: Span,
1818
},
1919
Multi {
2020
content: Vec<S>,
2121
#[cfg(feature = "spans")]
22-
span: Range<usize>,
22+
span: Span,
2323
},
2424
}
2525

fluent-syntax/src/ast/mod.rs

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ use std::ops::Range;
118118
pub struct Resource<S> {
119119
pub body: Vec<Entry<S>>,
120120
#[cfg(feature = "spans")]
121-
pub span: Range<usize>,
121+
pub span: Span,
122122
}
123123

124124
/// A top-level node representing an entry of a [`Resource`].
@@ -209,7 +209,7 @@ pub enum Entry<S> {
209209
Junk {
210210
content: S,
211211
#[cfg(feature = "spans")]
212-
span: Range<usize>,
212+
span: Span,
213213
},
214214
}
215215

@@ -269,7 +269,7 @@ pub struct Message<S> {
269269
pub attributes: Vec<Attribute<S>>,
270270
pub comment: Option<Comment<S>>,
271271
#[cfg(feature = "spans")]
272-
pub span: Range<usize>,
272+
pub span: Span,
273273
}
274274

275275
/// A Fluent [`Term`].
@@ -325,7 +325,7 @@ pub struct Term<S> {
325325
pub attributes: Vec<Attribute<S>>,
326326
pub comment: Option<Comment<S>>,
327327
#[cfg(feature = "spans")]
328-
pub span: Range<usize>,
328+
pub span: Span,
329329
}
330330

331331
/// Pattern contains a value of a [`Message`], [`Term`] or an [`Attribute`].
@@ -404,7 +404,7 @@ pub struct Term<S> {
404404
pub struct Pattern<S> {
405405
pub elements: Vec<PatternElement<S>>,
406406
#[cfg(feature = "spans")]
407-
pub span: Range<usize>,
407+
pub span: Span,
408408
}
409409

410410
/// `PatternElement` is an element of a [`Pattern`].
@@ -485,12 +485,12 @@ pub enum PatternElement<S> {
485485
TextElement {
486486
value: S,
487487
#[cfg(feature = "spans")]
488-
span: Range<usize>,
488+
span: Span,
489489
},
490490
Placeable {
491491
expression: Expression<S>,
492492
#[cfg(feature = "spans")]
493-
span: Range<usize>,
493+
span: Span,
494494
},
495495
}
496496

@@ -563,7 +563,7 @@ pub struct Attribute<S> {
563563
pub id: Identifier<S>,
564564
pub value: Pattern<S>,
565565
#[cfg(feature = "spans")]
566-
pub span: Range<usize>,
566+
pub span: Span,
567567
}
568568

569569
/// Identifier is part of nodes such as [`Message`], [`Term`] and [`Attribute`].
@@ -613,7 +613,7 @@ pub struct Attribute<S> {
613613
pub struct Identifier<S> {
614614
pub name: S,
615615
#[cfg(feature = "spans")]
616-
pub span: Range<usize>,
616+
pub span: Span,
617617
}
618618

619619
/// Variant is a single branch of a value in a [`Select`](Expression::Select) expression.
@@ -701,7 +701,7 @@ pub struct Variant<S> {
701701
pub value: Pattern<S>,
702702
pub default: bool,
703703
#[cfg(feature = "spans")]
704-
pub span: Range<usize>,
704+
pub span: Span,
705705
}
706706

707707
/// A key of a [`Variant`].
@@ -832,7 +832,7 @@ pub enum VariantKey<S> {
832832
pub struct Comment<S> {
833833
pub content: Vec<S>,
834834
#[cfg(feature = "spans")]
835-
pub span: Range<usize>,
835+
pub span: Span,
836836
}
837837

838838
/// List of arguments for a [`FunctionReference`](InlineExpression::FunctionReference) or a
@@ -916,7 +916,7 @@ pub struct CallArguments<S> {
916916
pub positional: Vec<InlineExpression<S>>,
917917
pub named: Vec<NamedArgument<S>>,
918918
#[cfg(feature = "spans")]
919-
pub span: Range<usize>,
919+
pub span: Span,
920920
}
921921

922922
/// A key-value pair used in [`CallArguments`].
@@ -987,7 +987,7 @@ pub struct NamedArgument<S> {
987987
pub name: Identifier<S>,
988988
pub value: InlineExpression<S>,
989989
#[cfg(feature = "spans")]
990-
pub span: Range<usize>,
990+
pub span: Span,
991991
}
992992

993993
/// A subset of expressions which can be used as [`Placeable`](PatternElement::Placeable),
@@ -1091,7 +1091,7 @@ pub enum InlineExpression<S> {
10911091
StringLiteral {
10921092
value: S,
10931093
#[cfg(feature = "spans")]
1094-
span: Range<usize>,
1094+
span: Span,
10951095
},
10961096
/// A number literal.
10971097
///
@@ -1141,7 +1141,7 @@ pub enum InlineExpression<S> {
11411141
NumberLiteral {
11421142
value: S,
11431143
#[cfg(feature = "spans")]
1144-
span: Range<usize>,
1144+
span: Span,
11451145
},
11461146
/// A function reference.
11471147
///
@@ -1195,7 +1195,7 @@ pub enum InlineExpression<S> {
11951195
id: Identifier<S>,
11961196
arguments: CallArguments<S>,
11971197
#[cfg(feature = "spans")]
1198-
span: Range<usize>,
1198+
span: Span,
11991199
},
12001200
/// A reference to another message.
12011201
///
@@ -1249,7 +1249,7 @@ pub enum InlineExpression<S> {
12491249
id: Identifier<S>,
12501250
attribute: Option<Identifier<S>>,
12511251
#[cfg(feature = "spans")]
1252-
span: Range<usize>,
1252+
span: Span,
12531253
},
12541254
/// A reference to a term.
12551255
///
@@ -1305,7 +1305,7 @@ pub enum InlineExpression<S> {
13051305
attribute: Option<Identifier<S>>,
13061306
arguments: Option<CallArguments<S>>,
13071307
#[cfg(feature = "spans")]
1308-
span: Range<usize>,
1308+
span: Span,
13091309
},
13101310
/// A reference to a variable.
13111311
///
@@ -1357,7 +1357,7 @@ pub enum InlineExpression<S> {
13571357
VariableReference {
13581358
id: Identifier<S>,
13591359
#[cfg(feature = "spans")]
1360-
span: Range<usize>,
1360+
span: Span,
13611361
},
13621362
/// A placeable which may contain another expression.
13631363
///
@@ -1413,21 +1413,21 @@ pub enum InlineExpression<S> {
14131413
Placeable {
14141414
expression: Box<Expression<S>>,
14151415
#[cfg(feature = "spans")]
1416-
span: Range<usize>,
1416+
span: Span,
14171417
},
14181418
}
14191419

14201420
#[cfg(feature = "spans")]
14211421
impl<S> InlineExpression<S> {
1422-
pub fn get_span(&self) -> Range<usize> {
1422+
pub fn get_span(&self) -> Span {
14231423
match self {
14241424
InlineExpression::StringLiteral { span, .. }
14251425
| InlineExpression::TermReference { span, .. }
14261426
| InlineExpression::VariableReference { span, .. }
14271427
| InlineExpression::Placeable { span, .. }
14281428
| InlineExpression::NumberLiteral { span, .. }
14291429
| InlineExpression::FunctionReference { span, .. }
1430-
| InlineExpression::MessageReference { span, .. } => span.clone(), // Why doesn't std::ops::Range implement the Copy trait...?
1430+
| InlineExpression::MessageReference { span, .. } => *span,
14311431
}
14321432
}
14331433
}
@@ -1521,13 +1521,49 @@ pub enum Expression<S> {
15211521
selector: InlineExpression<S>,
15221522
variants: Vec<Variant<S>>,
15231523
#[cfg(feature = "spans")]
1524-
span: Range<usize>,
1524+
span: Span,
15251525
},
15261526

15271527
/// An inline expression such as `${ username }`:
15281528
///
15291529
/// ```ftl
15301530
/// hello-user = Hello ${ username }
15311531
/// ```
1532-
Inline(InlineExpression<S>, #[cfg(feature = "spans")] Range<usize>),
1532+
Inline(InlineExpression<S>, #[cfg(feature = "spans")] Span),
1533+
}
1534+
1535+
/// A span of a node. Allows you to get the index of the start and end character of a node.
1536+
///
1537+
/// # Example
1538+
///
1539+
/// ```
1540+
/// #![cfg(feature = "spans")]
1541+
///
1542+
/// use fluent_syntax::parser;
1543+
/// use fluent_syntax::ast::*;
1544+
///
1545+
/// let ftl = "hello-world = Hello, World!";
1546+
///
1547+
/// let resource = parser::parse(ftl).expect("Failed to parse an FTL resource.");
1548+
/// let Entry::Message(Message { ref id, .. }) = resource.body[0] else { unreachable!() };
1549+
///
1550+
/// assert_eq!(resource.span, Span { start: 0, end: 27 });
1551+
/// assert_eq!(id.span, Span { start: 0, end: 11 }); // the span of hello-world identifier
1552+
/// ```
1553+
#[cfg(feature = "spans")]
1554+
#[derive(Debug, Clone, Copy, Default, PartialEq)]
1555+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1556+
pub struct Span {
1557+
pub start: usize,
1558+
pub end: usize,
1559+
}
1560+
1561+
#[cfg(feature = "spans")]
1562+
impl Span {
1563+
pub fn new(range: Range<usize>) -> Self {
1564+
Self {
1565+
start: range.start,
1566+
end: range.end,
1567+
}
1568+
}
15331569
}

fluent-syntax/src/parser/comment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ where
5454
ast::Comment {
5555
content,
5656
#[cfg(feature = "spans")]
57-
span: start_pos..self.ptr,
57+
span: ast::Span::new(start_pos..self.ptr),
5858
},
5959
level,
6060
))

fluent-syntax/src/parser/core.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ where
7070
body.push(ast::Entry::Junk {
7171
content,
7272
#[cfg(feature = "spans")]
73-
span: entry_start..self.ptr,
73+
span: ast::Span::new(entry_start..self.ptr),
7474
});
7575
}
7676
}
@@ -81,9 +81,9 @@ where
8181
body.push(ast::Entry::Comment(last_comment));
8282
}
8383
if errors.is_empty() {
84-
Ok(ast::Resource { body, #[cfg(feature = "spans")] span: 0..self.length })
84+
Ok(ast::Resource { body, #[cfg(feature = "spans")] span: ast::Span::new(0..self.length) })
8585
} else {
86-
Err((ast::Resource { body, #[cfg(feature = "spans")] span: 0..self.length }, errors))
86+
Err((ast::Resource { body, #[cfg(feature = "spans")] span: ast::Span::new(0..self.length) }, errors))
8787
}
8888
}
8989

@@ -128,7 +128,7 @@ where
128128
attributes,
129129
comment: None,
130130
#[cfg(feature = "spans")]
131-
span: entry_start..self.ptr,
131+
span: ast::Span::new(entry_start..self.ptr),
132132
})
133133
}
134134

@@ -152,7 +152,7 @@ where
152152
attributes,
153153
comment: None,
154154
#[cfg(feature = "spans")]
155-
span: entry_start..self.ptr,
155+
span: ast::Span::new(entry_start..self.ptr),
156156
})
157157
} else {
158158
error!(
@@ -196,7 +196,7 @@ where
196196
id,
197197
value: pattern,
198198
#[cfg(feature = "spans")]
199-
span: self.ptr - 1..self.ptr,
199+
span: ast::Span::new(self.ptr - 1..self.ptr),
200200
}),
201201
None => error!(ErrorKind::MissingValue, self.ptr),
202202
}
@@ -218,7 +218,7 @@ where
218218
ast::Identifier {
219219
name,
220220
#[cfg(feature = "spans")]
221-
span: start..self.ptr,
221+
span: ast::Span::new(start..self.ptr),
222222
}
223223
}
224224

@@ -295,7 +295,7 @@ where
295295
value,
296296
default,
297297
#[cfg(feature = "spans")]
298-
span: (if default { start - 1 } else { start })..self.ptr,
298+
span: ast::Span::new((if default { start - 1 } else { start })..self.ptr),
299299
});
300300
self.skip_blank();
301301
} else {

0 commit comments

Comments
 (0)