Skip to content

Commit

Permalink
rename Span up_to to until
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Aug 6, 2023
1 parent 42ad3d2 commit fc72ab4
Show file tree
Hide file tree
Showing 33 changed files with 208 additions and 214 deletions.
17 changes: 8 additions & 9 deletions crates/hdx_lexer/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ impl Span {
&source_text[self.start as usize..self.end as usize]
}

pub fn with_start(&self, span: &Self) -> Self {
Self { start: span.start, end: self.end }
}

pub fn with_end(&self, span: &Self) -> Self {
Self { start: self.start, end: span.end }
}

pub fn up_to(&self, span: &Self) -> Self {
/// Returns a `Span` from the beginning of `self` until the beginning of `end`.
///
/// ```text
/// ____ ___
/// self lorem ipsum end
/// ^^^^^^^^^^^^^^^^^
/// ```
pub fn until(&self, span: Self) -> Self {
Self { start: self.start, end: span.start }
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/hdx_parser/src/css/component_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ impl<'a> Parse<'a> for ComponentValue<'a> {
match parser.cur().kind {
Kind::LeftCurly | Kind::LeftSquare | Kind::LeftParen => {
Ok(Self::SimpleBlock(SimpleBlock::parse(parser)?)
.spanned(span.up_to(&parser.cur().span)))
.spanned(span.until(parser.cur().span)))
}
Kind::Function => {
Ok(Self::Function(Function::parse(parser)?).spanned(span.up_to(&parser.cur().span)))
Ok(Self::Function(Function::parse(parser)?).spanned(span.until(parser.cur().span)))
}
_ => {
let token = parser.cur().clone();
Expand All @@ -63,7 +63,7 @@ impl<'a> Parse<'a> for SimpleBlock<'a> {
.ok_or_else(|| diagnostics::Unexpected(parser.cur().kind, span))?;
parser.advance();
let value = parser.parse_component_values(pairwise.end(), true)?;
Ok(Self { value: parser.boxup(value), pairwise }.spanned(span.up_to(&parser.cur().span)))
Ok(Self { value: parser.boxup(value), pairwise }.spanned(span.until(parser.cur().span)))
}
}

Expand All @@ -73,6 +73,6 @@ impl<'a> Parse<'a> for Function<'a> {
let span = parser.cur().span;
let name = parser.expect_function()?;
let value = parser.parse_component_values(Kind::RightParen, false)?;
Ok(Self { name, value: parser.boxup(value) }.spanned(span.up_to(&parser.cur().span)))
Ok(Self { name, value: parser.boxup(value) }.spanned(span.until(parser.cur().span)))
}
}
4 changes: 2 additions & 2 deletions crates/hdx_parser/src/css/parser_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<'a> Parser<'a> {
let mut prelude = None;
loop {
match self.cur().kind {
Kind::Eof => Err(diagnostics::Unexpected(Kind::Eof, span.up_to(&self.cur().span)))?,
Kind::Eof => Err(diagnostics::Unexpected(Kind::Eof, span.until(self.cur().span)))?,
Kind::RightCurly => {
if nested {
Err(diagnostics::Unexpected(self.cur().kind, self.cur().span))?
Expand Down Expand Up @@ -181,7 +181,7 @@ impl<'a> Parser<'a> {
Err(diagnostics::Unimplemented(span))?;
}
self.expect(Kind::Colon)
.map_err(|_| diagnostics::BadDeclaration(span.up_to(&self.cur().span)))?;
.map_err(|_| diagnostics::BadDeclaration(span.until(self.cur().span)))?;
let value = Value::parse(self)?;
let mut important = false;
loop {
Expand Down
22 changes: 10 additions & 12 deletions crates/hdx_parser/src/css/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl<'a> Parse<'a> for Custom<'a> {
let checkpoint = parser.checkpoint();
let value_span = parser.cur().span;
let value_like = ValueLike::parse(parser)
.unwrap_or(ValueLike::Unknown.spanned(value_span.up_to(&parser.cur().span)));
.unwrap_or(ValueLike::Unknown.spanned(value_span.until(parser.cur().span)));
parser.rewind(checkpoint);
let mut value = parser.parse_component_values(Kind::Semicolon, true)?;
if parser.at(Kind::Semicolon) {
Expand All @@ -33,7 +33,7 @@ impl<'a> Parse<'a> for Custom<'a> {
}
}
Ok(Self { name, value_like, value: parser.boxup(value), important }
.spanned(span.up_to(&parser.cur().span)))
.spanned(span.until(parser.cur().span)))
}
}

Expand All @@ -44,28 +44,26 @@ impl<'a> Parse<'a> for ValueLike<'a> {
let token = parser.cur().clone();
let parsed = MathExpr::<Length>::parse(parser);
if let Ok(value) = parsed {
return Ok(Self::Length(parser.boxup(value)).spanned(span.up_to(&parser.cur().span)));
return Ok(Self::Length(parser.boxup(value)).spanned(span.until(parser.cur().span)));
}
parser.rewind(checkpoint);
let checkpoint = parser.checkpoint();
let parsed = MathExpr::<LengthPercentage>::parse(parser);
if let Ok(value) = parsed {
return Ok(
Self::LengthPercentage(parser.boxup(value)).spanned(span.up_to(&parser.cur().span))
Self::LengthPercentage(parser.boxup(value)).spanned(span.until(parser.cur().span))
);
}
parser.rewind(checkpoint);
let checkpoint = parser.checkpoint();
let parsed = Expr::<ColorValue>::parse(parser);
if let Ok(value) = parsed {
return Ok(Self::Color(parser.boxup(value)).spanned(span.up_to(&parser.cur().span)));
return Ok(Self::Color(parser.boxup(value)).spanned(span.until(parser.cur().span)));
}
parser.rewind(checkpoint);
let parsed = ExprList::<FontFamilyValue>::parse(parser);
if let Ok(value) = parsed {
return Ok(
Self::FontFamily(parser.boxup(value)).spanned(span.up_to(&parser.cur().span))
);
return Ok(Self::FontFamily(parser.boxup(value)).spanned(span.until(parser.cur().span)));
}
Err(diagnostics::Unexpected(token.kind, token.span).into())
}
Expand All @@ -80,7 +78,7 @@ macro_rules! parse_properties {
parser.parse_declaration(
Some($prop::name_as_atom()),
|parser: &mut Parser<'a>, _name: &Token, value: Spanned<<$prop as Declaration>::Value>, important: bool| {
Ok($prop { value: parser.boxup(value), important }.spanned(span.up_to(&parser.cur().span)))
Ok($prop { value: parser.boxup(value), important }.spanned(span.until(parser.cur().span)))
},
)
}
Expand All @@ -92,7 +90,7 @@ macro_rules! parse_properties {
let span = parser.cur().span;
if parser.cur().is_dashed_ident() {
let custom = Custom::parse(parser)?;
return Ok(Property::Custom(parser.boxup(custom)).spanned(span.up_to(&parser.cur().span)));
return Ok(Property::Custom(parser.boxup(custom)).spanned(span.until(parser.cur().span)));
}
let checkpoint = parser.checkpoint();
let property = match PropertyId::from_atom(parser.cur().as_atom_lower().unwrap_or(atom!(""))) {
Expand All @@ -110,10 +108,10 @@ macro_rules! parse_properties {
let parsed =
UnknownDeclaration::parse(parser).map(|p| Property::Unknown(parser.boxup(p)));
parser.warnings.push(e);
parser.warnings.push(diagnostics::UnknownDeclaration(span.up_to(&parser.cur().span)).into());
parser.warnings.push(diagnostics::UnknownDeclaration(span.until(parser.cur().span)).into());
parsed
})?;
Ok(property.spanned(span.up_to(&parser.cur().span)))
Ok(property.spanned(span.until(parser.cur().span)))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/hdx_parser/src/css/rules/charset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ impl<'a> Parse<'a> for CSSCharsetRule {
parser.expect_at_keyword_of(atom!("charset"))?;
let encoding = parser.expect_string()?;
parser.expect(Kind::Semicolon)?;
Ok(Self { encoding }.spanned(span.up_to(&parser.cur().span)))
Ok(Self { encoding }.spanned(span.until(parser.cur().span)))
}
}
2 changes: 1 addition & 1 deletion crates/hdx_parser/src/css/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ impl<'a> Parse<'a> for NoPreludeAllowed {
fn parse(parser: &mut Parser<'a>) -> Result<Spanned<Self>> {
let span = parser.cur().span;
parser.expect_without_advance(Kind::LeftCurly)?;
Ok(Self {}.spanned(span.up_to(&parser.cur().span)))
Ok(Self {}.spanned(span.until(parser.cur().span)))
}
}
10 changes: 5 additions & 5 deletions crates/hdx_parser/src/css/rules/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<'a> Parse<'a> for CSSPageRule<'a> {
declarations: parser.boxup(declarations),
rules: parser.boxup(rules),
}
.spanned(span.up_to(&parser.cur().span)))
.spanned(span.until(parser.cur().span)))
},
)
}
Expand All @@ -36,7 +36,7 @@ impl<'a> Parse<'a> for PageSelectorList<'a> {
fn parse(parser: &mut Parser<'a>) -> Result<Spanned<Self>> {
let span = parser.cur().span;
let ok = Ok(Self { children: parser.parse_comma_list_of::<PageSelector>()? }
.spanned(span.up_to(&parser.cur().span)));
.spanned(span.until(parser.cur().span)));
ok
}
}
Expand All @@ -59,7 +59,7 @@ impl<'a> Parse<'a> for PageSelector<'a> {
}
}
}
Ok(Self { page_type, pseudos }.spanned(span.up_to(&parser.cur().span)))
Ok(Self { page_type, pseudos }.spanned(span.until(parser.cur().span)))
}
}

Expand All @@ -69,7 +69,7 @@ impl<'a> Parse<'a> for PagePseudoClass {
parser.expect(Kind::Colon)?;
let name = parser.expect_ident()?;
match Self::from_atom(name.clone()) {
Some(v) => Ok(v.spanned(span.up_to(&parser.cur().span))),
Some(v) => Ok(v.spanned(span.until(parser.cur().span))),
_ => Err(diagnostics::UnexpectedPseudo(name, span).into()),
}
}
Expand All @@ -86,7 +86,7 @@ impl<'a> Parse<'a> for CSSMarginRule<'a> {
_rules: Vec<'a, Spanned<CSSMarginRule<'a>>>,
declarations: Vec<'a, Spanned<Property<'a>>>| {
Ok(Self { name: PageMarginBox::TopLeft, declarations }
.spanned(span.up_to(&parser.cur().span)))
.spanned(span.until(parser.cur().span)))
},
)
}
Expand Down
27 changes: 13 additions & 14 deletions crates/hdx_parser/src/css/selector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl<'a> Parse<'a> for Selector<'a> {
}
components.push(component);
}
Ok(Self { components: parser.boxup(components) }.spanned(span.up_to(&parser.cur().span)))
Ok(Self { components: parser.boxup(components) }.spanned(span.until(parser.cur().span)))
}
}

Expand All @@ -101,7 +101,7 @@ impl<'a> Parse<'a> for Component<'a> {
match parser.cur().kind {
Kind::Whitespace => {
parser.next_token();
Ok(Self::Combinator(Combinator::Descendant).spanned(span.up_to(&parser.cur().span)))
Ok(Self::Combinator(Combinator::Descendant).spanned(span.until(parser.cur().span)))
}
Kind::Ident => {
let name = parser.cur().as_atom_lower().unwrap();
Expand All @@ -117,8 +117,7 @@ impl<'a> Parse<'a> for Component<'a> {
let ident = parser.cur().as_atom().unwrap();
if let Some(selector) = PseudoElement::from_atom(ident) {
parser.next_token();
Ok(Self::PseudoElement(selector)
.spanned(span.up_to(&parser.cur().span)))
Ok(Self::PseudoElement(selector).spanned(span.until(parser.cur().span)))
} else {
Err(diagnostics::Unimplemented(parser.cur().span))?
}
Expand All @@ -128,10 +127,10 @@ impl<'a> Parse<'a> for Component<'a> {
let ident = parser.cur().as_atom().unwrap();
if let Some(selector) = PseudoClass::from_atom(ident.clone()) {
parser.next_token();
Ok(Self::PseudoClass(selector).spanned(span.up_to(&parser.cur().span)))
Ok(Self::PseudoClass(selector).spanned(span.until(parser.cur().span)))
} else if let Some(e) = LegacyPseudoElement::from_atom(ident.clone()) {
parser.next_token();
Ok(Self::LegacyPseudoElement(e).spanned(span.up_to(&parser.cur().span)))
Ok(Self::LegacyPseudoElement(e).spanned(span.until(parser.cur().span)))
} else {
Err(diagnostics::UnexpectedIdent(ident, parser.cur().span))?
}
Expand All @@ -142,7 +141,7 @@ impl<'a> Parse<'a> for Component<'a> {
Kind::Hash => {
let name = parser.cur().as_atom().unwrap();
parser.next_token();
Ok(Self::Id(name).spanned(span.up_to(&parser.cur().span)))
Ok(Self::Id(name).spanned(span.until(parser.cur().span)))
}
Kind::Delim => match parser.cur().value.as_char() {
Some('.') => {
Expand All @@ -152,7 +151,7 @@ impl<'a> Parse<'a> for Component<'a> {
parser.next_token();
let ident = parser.cur().as_atom().unwrap();
parser.next_token();
Ok(Self::Class(ident).spanned(span.up_to(&parser.cur().span)))
Ok(Self::Class(ident).spanned(span.until(parser.cur().span)))
}
_ => Err(diagnostics::Unimplemented(parser.cur().span))?,
}
Expand All @@ -163,19 +162,19 @@ impl<'a> Parse<'a> for Component<'a> {
Kind::Delim if next_token.value.as_char().unwrap() == '|' => {
let (prefix, atom) = parse_wq_name(parser)?;
Ok(Self::NSPrefixedType(parser.boxup((prefix, atom)))
.spanned(span.up_to(&parser.cur().span)))
.spanned(span.until(parser.cur().span)))
}
_ => {
parser.next_token();
Ok(Self::Wildcard.spanned(span.up_to(&parser.cur().span)))
Ok(Self::Wildcard.spanned(span.until(parser.cur().span)))
}
}
}
_ => Err(diagnostics::Unimplemented(parser.cur().span))?,
},
Kind::LeftSquare => {
let attr = Attribute::parse(parser)?;
Ok(Component::Attribute(parser.boxup(attr)).spanned(span.up_to(&parser.cur().span)))
Ok(Component::Attribute(parser.boxup(attr)).spanned(span.until(parser.cur().span)))
}
_ => Err(diagnostics::Unimplemented(parser.cur().span))?,
}
Expand All @@ -194,7 +193,7 @@ impl<'a> Parse<'a> for Attribute {
Kind::RightSquare => {
parser.next_token();
return Ok(Self { ns_prefix, name, value, modifier, matcher }
.spanned(span.up_to(&parser.cur().span)));
.spanned(span.until(parser.cur().span)));
}
Kind::Delim => {
let delim_span = parser.cur().span;
Expand Down Expand Up @@ -232,7 +231,7 @@ impl<'a> Parse<'a> for Attribute {
Kind::RightSquare => {
parser.next_token();
Ok(Self { ns_prefix, name, value, modifier, matcher }
.spanned(span.up_to(&parser.cur().span)))
.spanned(span.until(parser.cur().span)))
}
Kind::Ident => {
let ident_span = parser.cur().span;
Expand All @@ -243,7 +242,7 @@ impl<'a> Parse<'a> for Attribute {
};
parser.expect(Kind::RightSquare)?;
Ok(Self { ns_prefix, name, value, modifier, matcher }
.spanned(span.up_to(&parser.cur().span)))
.spanned(span.until(parser.cur().span)))
}
k => Err(diagnostics::Unexpected(k, parser.cur().span))?,
}
Expand Down
4 changes: 2 additions & 2 deletions crates/hdx_parser/src/css/stylerule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ impl<'a> Parse<'a> for CSSStyleRule<'a> {
rules: Vec<'a, Spanned<CSSStyleRule<'a>>>,
declarations: Vec<'a, Spanned<Property<'a>>>| {
if selectors.is_none() {
Err(diagnostics::NoSelector(span, span.up_to(&parser.cur().span)))?
Err(diagnostics::NoSelector(span, span.until(parser.cur().span)))?
}
Ok(Self {
selectors: parser.boxup(selectors.unwrap()),
declarations: parser.boxup(declarations),
rules: parser.boxup(rules),
}
.spanned(span.up_to(&parser.cur().span)))
.spanned(span.until(parser.cur().span)))
},
)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/hdx_parser/src/css/stylesheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ impl<'a> Parse<'a> for CSSStyleSheet<'a> {
}
}
}
Ok(Self { rules }.spanned(span.up_to(&parser.cur().span)))
Ok(Self { rules }.spanned(span.until(parser.cur().span)))
}
}

impl<'a> Parse<'a> for SelectorSet<'a> {
fn parse(parser: &mut Parser<'a>) -> Result<Spanned<Self>> {
let span = parser.cur().span;
Ok(Self { children: parser.parse_comma_list_of::<Selector>()? }
.spanned(span.up_to(&parser.cur().span)))
.spanned(span.until(parser.cur().span)))
}
}

Expand Down
8 changes: 4 additions & 4 deletions crates/hdx_parser/src/css/unknown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl<'a> Parse<'a> for UnknownAtRule<'a> {
rules: parser.boxup(rules),
properties: parser.boxup(properties),
}
.spanned(span.up_to(&parser.cur().span)))
.spanned(span.until(parser.cur().span)))
},
)
}
Expand All @@ -37,7 +37,7 @@ impl<'a> Parse<'a> for UnknownRule<'a> {
rules: parser.boxup(rules),
properties: parser.boxup(properties),
}
.spanned(span.up_to(&parser.cur().span)))
.spanned(span.until(parser.cur().span)))
},
)
}
Expand All @@ -47,7 +47,7 @@ impl<'a> Parse<'a> for UnknownPrelude<'a> {
fn parse(parser: &mut Parser<'a>) -> Result<Spanned<Self>> {
let span = parser.cur().span;
let value = parser.parse_component_values(Kind::Semicolon, false)?;
Ok(Self { value: parser.boxup(value) }.spanned(span.up_to(&parser.cur().span)))
Ok(Self { value: parser.boxup(value) }.spanned(span.until(parser.cur().span)))
}
}

Expand Down Expand Up @@ -81,7 +81,7 @@ impl<'a> Parse<'a> for UnknownDeclaration<'a> {
parser.advance();
}
Ok(Self { name, value_like, value: parser.boxup(value), important: false }
.spanned(span.up_to(&parser.cur().span)))
.spanned(span.until(parser.cur().span)))
}
}

Expand Down
Loading

0 comments on commit fc72ab4

Please sign in to comment.