Skip to content

Commit 4e1658b

Browse files
Ertanicalerque
authored andcommitted
refactor(fluent-syntax): Make Span a wrapper around std::ops::Range<usize>
1 parent 6ac6610 commit 4e1658b

File tree

6 files changed

+50
-42
lines changed

6 files changed

+50
-42
lines changed

fluent-syntax/src/ast/mod.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ mod helper;
8888

8989
#[cfg(feature = "serde")]
9090
use serde::{Deserialize, Serialize};
91+
use std::ops::Deref;
9192
#[cfg(feature = "spans")]
9293
use std::ops::Range;
9394

@@ -1427,15 +1428,15 @@ pub enum InlineExpression<S> {
14271428

14281429
#[cfg(feature = "spans")]
14291430
impl<S> InlineExpression<S> {
1430-
pub fn get_span(&self) -> Span {
1431+
pub fn get_span(&self) -> &Span {
14311432
match self {
14321433
InlineExpression::StringLiteral { span, .. }
14331434
| InlineExpression::TermReference { span, .. }
14341435
| InlineExpression::VariableReference { span, .. }
14351436
| InlineExpression::Placeable { span, .. }
14361437
| InlineExpression::NumberLiteral { span, .. }
14371438
| InlineExpression::FunctionReference { span, .. }
1438-
| InlineExpression::MessageReference { span, .. } => *span,
1439+
| InlineExpression::MessageReference { span, .. } => span,
14391440
}
14401441
}
14411442
}
@@ -1559,19 +1560,15 @@ pub enum Expression<S> {
15591560
/// assert_eq!(id.span, Span { start: 0, end: 11 }); // the span of hello-world identifier
15601561
/// ```
15611562
#[cfg(feature = "spans")]
1562-
#[derive(Debug, Clone, Copy, Default, PartialEq)]
1563+
#[derive(Debug, Clone, Default, PartialEq)]
15631564
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1564-
pub struct Span {
1565-
pub start: usize,
1566-
pub end: usize,
1567-
}
1565+
pub struct Span(pub Range<usize>);
15681566

15691567
#[cfg(feature = "spans")]
1570-
impl Span {
1571-
pub fn new(range: Range<usize>) -> Self {
1572-
Self {
1573-
start: range.start,
1574-
end: range.end,
1575-
}
1568+
impl Deref for Span {
1569+
type Target = Range<usize>;
1570+
1571+
fn deref(&self) -> &Self::Target {
1572+
&self.0
15761573
}
1577-
}
1574+
}

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: ast::Span::new(start_pos..self.ptr),
57+
span: ast::Span(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: ast::Span::new(entry_start..self.ptr),
73+
span: ast::Span(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: ast::Span::new(0..self.length) })
84+
Ok(ast::Resource { body, #[cfg(feature = "spans")] span: ast::Span(0..self.length) })
8585
} else {
86-
Err((ast::Resource { body, #[cfg(feature = "spans")] span: ast::Span::new(0..self.length) }, errors))
86+
Err((ast::Resource { body, #[cfg(feature = "spans")] span: ast::Span(0..self.length) }, errors))
8787
}
8888
}
8989

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

@@ -152,7 +152,7 @@ where
152152
attributes,
153153
comment: None,
154154
#[cfg(feature = "spans")]
155-
span: ast::Span::new(entry_start..self.ptr),
155+
span: ast::Span(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: ast::Span::new(self.ptr - 1..self.ptr),
199+
span: ast::Span(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: ast::Span::new(start..self.ptr),
221+
span: ast::Span(start..self.ptr),
222222
}
223223
}
224224

@@ -305,7 +305,7 @@ where
305305
value,
306306
default,
307307
#[cfg(feature = "spans")]
308-
span: ast::Span::new((if default { start - 1 } else { start })..self.ptr),
308+
span: ast::Span((if default { start - 1 } else { start })..self.ptr),
309309
});
310310
self.skip_blank();
311311
} else {

fluent-syntax/src/parser/expression.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ where
2323
return Ok(ast::Expression::Inline(
2424
exp,
2525
#[cfg(feature = "spans")]
26-
ast::Span::new(start_span..self.ptr),
26+
ast::Span(start_span..self.ptr),
2727
));
2828
}
2929

@@ -68,7 +68,7 @@ where
6868
selector: exp,
6969
variants,
7070
#[cfg(feature = "spans")]
71-
span: ast::Span::new(start_span..self.ptr),
71+
span: ast::Span(start_span..self.ptr),
7272
})
7373
}
7474

@@ -112,15 +112,15 @@ where
112112
Ok(ast::InlineExpression::StringLiteral {
113113
value: slice,
114114
#[cfg(feature = "spans")]
115-
span: ast::Span::new(start..self.ptr),
115+
span: ast::Span(start..self.ptr),
116116
})
117117
}
118118
Some(b) if b.is_ascii_digit() => {
119119
let num = self.get_number_literal()?;
120120
Ok(ast::InlineExpression::NumberLiteral {
121121
value: num,
122122
#[cfg(feature = "spans")]
123-
span: ast::Span::new(start..self.ptr),
123+
span: ast::Span(start..self.ptr),
124124
})
125125
}
126126
Some(b'-') if !only_literal => {
@@ -135,15 +135,15 @@ where
135135
attribute,
136136
arguments,
137137
#[cfg(feature = "spans")]
138-
span: ast::Span::new(start..self.ptr),
138+
span: ast::Span(start..self.ptr),
139139
})
140140
} else {
141141
self.ptr -= 1;
142142
let num = self.get_number_literal()?;
143143
Ok(ast::InlineExpression::NumberLiteral {
144144
value: num,
145145
#[cfg(feature = "spans")]
146-
span: ast::Span::new(start..self.ptr),
146+
span: ast::Span(start..self.ptr),
147147
})
148148
}
149149
}
@@ -153,7 +153,7 @@ where
153153
Ok(ast::InlineExpression::VariableReference {
154154
id,
155155
#[cfg(feature = "spans")]
156-
span: ast::Span::new(start..self.ptr),
156+
span: ast::Span(start..self.ptr),
157157
})
158158
}
159159
Some(b) if b.is_ascii_alphabetic() => {
@@ -169,15 +169,15 @@ where
169169
id,
170170
arguments,
171171
#[cfg(feature = "spans")]
172-
span: ast::Span::new(start..self.ptr),
172+
span: ast::Span(start..self.ptr),
173173
})
174174
} else {
175175
let attribute = self.get_attribute_accessor()?;
176176
Ok(ast::InlineExpression::MessageReference {
177177
id,
178178
attribute,
179179
#[cfg(feature = "spans")]
180-
span: ast::Span::new(start..self.ptr),
180+
span: ast::Span(start..self.ptr),
181181
})
182182
}
183183
}
@@ -187,7 +187,7 @@ where
187187
Ok(ast::InlineExpression::Placeable {
188188
expression: Box::new(exp),
189189
#[cfg(feature = "spans")]
190-
span: ast::Span::new(start..self.ptr),
190+
span: ast::Span(start..self.ptr),
191191
})
192192
}
193193
_ if only_literal => error!(ErrorKind::ExpectedLiteral, self.ptr),
@@ -239,11 +239,11 @@ where
239239
name: ast::Identifier {
240240
name: id.name.clone(),
241241
#[cfg(feature = "spans")]
242-
span: id.span,
242+
span: id.span.clone(),
243243
},
244244
value: val,
245245
#[cfg(feature = "spans")]
246-
span: ast::Span::new(id.span.start..self.ptr),
246+
span: ast::Span(id.span.start..self.ptr),
247247
});
248248
} else {
249249
if !argument_names.is_empty() {
@@ -269,7 +269,7 @@ where
269269
positional,
270270
named,
271271
#[cfg(feature = "spans")]
272-
span: ast::Span::new(start..self.ptr),
272+
span: ast::Span(start..self.ptr),
273273
}))
274274
}
275275
}

fluent-syntax/src/parser/pattern.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ where
147147
PatternElementPlaceholders::Placeable(expression, range) => {
148148
ast::PatternElement::Placeable {
149149
expression,
150-
span: ast::Span::new(range),
150+
span: ast::Span(range),
151151
}
152152
}
153153
#[cfg(not(feature = "spans"))]
@@ -170,15 +170,15 @@ where
170170
ast::PatternElement::TextElement {
171171
value,
172172
#[cfg(feature = "spans")]
173-
span: ast::Span::new(start..end),
173+
span: ast::Span(start..end),
174174
}
175175
}
176176
})
177177
.collect();
178178
return Ok(Some(ast::Pattern {
179179
elements,
180180
#[cfg(feature = "spans")]
181-
span: ast::Span::new(start_pos..self.ptr),
181+
span: ast::Span(start_pos..self.ptr),
182182
}));
183183
}
184184

fluent-syntax/src/parser/runtime.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,28 @@ where
3737
body.push(ast::Entry::Junk {
3838
content,
3939
#[cfg(feature = "spans")]
40-
span: ast::Span::new(entry_start..self.ptr),
40+
span: ast::Span(entry_start..self.ptr),
4141
});
4242
}
4343
}
4444
self.skip_blank_block();
4545
}
4646

4747
if errors.is_empty() {
48-
Ok(ast::Resource { body, #[cfg(feature = "spans")] span: ast::Span::new(0..self.length) })
48+
Ok(ast::Resource {
49+
body,
50+
#[cfg(feature = "spans")]
51+
span: ast::Span(0..self.length),
52+
})
4953
} else {
50-
Err((ast::Resource { body, #[cfg(feature = "spans")] span: ast::Span::new(0..self.length) }, errors))
54+
Err((
55+
ast::Resource {
56+
body,
57+
#[cfg(feature = "spans")]
58+
span: ast::Span(0..self.length),
59+
},
60+
errors,
61+
))
5162
}
5263
}
5364

0 commit comments

Comments
 (0)