From 0cc248516af858183ef633043e486cf21f7b3045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Oddsson?= Date: Sun, 28 Jul 2024 22:12:31 +0200 Subject: [PATCH] fix more compile errors in parsing token kind (#37) I messed around some more. Not 100% sure this is the correct way to do this still. --- crates/hdx_parser/src/traits/declarations.rs | 21 ++++++---- crates/hdx_parser/src/traits/rules.rs | 44 +++++++++++--------- 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/crates/hdx_parser/src/traits/declarations.rs b/crates/hdx_parser/src/traits/declarations.rs index 1af6bfbc..f0f81bed 100644 --- a/crates/hdx_parser/src/traits/declarations.rs +++ b/crates/hdx_parser/src/traits/declarations.rs @@ -1,7 +1,7 @@ use hdx_atom::{atom, Atom}; -use hdx_lexer::{Include, Kind, Token}; +use hdx_lexer::{Include, Kind}; -use crate::{discard, expect, expect_ignore_case, match_ignore_case, parser::Parser, unexpected, Result}; +use crate::{discard, expect, expect_ignore_case, parser::Parser, unexpected, Result}; use super::Parse; @@ -9,18 +9,23 @@ pub trait Declaration<'a>: Sized + Parse<'a> { type DeclarationValue: DeclarationValue<'a>; fn parse_name(parser: &mut Parser<'a>) -> Result { - match parser.next().clone() { - Token::Ident(atom) => { + let token = parser.next(); + match token.kind() { + Kind::Ident => { expect!(parser.next(), Kind::Colon); - Ok(atom.to_ascii_lowercase()) + Ok(parser.parse_atom_lower(token)) } - token => unexpected!(parser, token), + _ => unexpected!(parser, token), } } fn parse_important(parser: &mut Parser<'a>) -> Result { - if matches!(parser.peek(), Token::Delim('!')) - && match_ignore_case!(parser.peek_n(2), Token::Ident(atom!("important"))) + let peeked = parser.peek(); + let nexted_peek = parser.peek_n(2); + if matches!(peeked.kind(), Kind::Delim) + && matches!(peeked.char(), Some('!')) + && matches!(nexted_peek.kind(), Kind::Ident) + && matches!(parser.parse_atom_lower(*nexted_peek), atom!("important")) { parser.advance(); expect_ignore_case!(parser.next_with(Include::all()), Kind::Ident, atom!("important")); diff --git a/crates/hdx_parser/src/traits/rules.rs b/crates/hdx_parser/src/traits/rules.rs index cda9f303..97471d67 100644 --- a/crates/hdx_parser/src/traits/rules.rs +++ b/crates/hdx_parser/src/traits/rules.rs @@ -1,4 +1,4 @@ -use hdx_lexer::{Kind, Token}; +use hdx_lexer::Kind; use crate::{discard, expect, parser::Parser, peek, span::Spanned, unexpected, Result, State, Vec}; @@ -15,8 +15,8 @@ pub trait AtRule<'a>: Sized + Parse<'a> { // parse_prelude returns an Option, and rules that either require can check // in parse() or override parse_prelude() to err. fn parse_prelude(parser: &mut Parser<'a>) -> Result>> { - match parser.peek() { - Token::LeftCurly | Token::Semicolon | Token::Eof => Ok(None), + match parser.peek().kind() { + Kind::LeftCurly | Kind::Semicolon | Kind::Eof => Ok(None), _ => Ok(Some(Self::Prelude::parse_spanned(parser)?)), } } @@ -25,13 +25,14 @@ pub trait AtRule<'a>: Sized + Parse<'a> { // one). The default parse_prelude returns an Option, and rules that either // require can check in parse() or override parse_prelude() to err. fn parse_block(parser: &mut Parser<'a>) -> Result>> { - match parser.peek() { - Token::Semicolon | Token::Eof => { + let token = parser.peek(); + match token.kind() { + Kind::Semicolon | Kind::Eof => { parser.advance(); Ok(None) } - Token::LeftCurly => Ok(Some(Self::Block::parse_spanned(parser)?)), - token => unexpected!(parser, token), + Kind::LeftCurly => Ok(Some(Self::Block::parse_spanned(parser)?)), + _ => unexpected!(parser, token), } } @@ -66,19 +67,21 @@ pub trait QualifiedRule<'a>: Sized + Parse<'a> { // https://drafts.csswg.org/css-syntax-3/#consume-a-qualified-rule fn parse_qualified_rule(parser: &mut Parser<'a>) -> Result<(Spanned, Spanned)> { - match parser.peek().clone() { - token @ Token::Eof => unexpected!(parser, token), - token @ Token::RightCurly if !parser.is(State::Nested) => unexpected!(parser, token), - Token::Ident(atom) if peek!(parser, 2, Kind::RightCurly) && atom.starts_with("--") => { + let token = parser.peek().clone(); + match token.kind() { + Kind::Eof => unexpected!(parser, token), + Kind::RightCurly if !parser.is(State::Nested) => unexpected!(parser, token), + Kind::Ident if peek!(parser, 2, Kind::RightCurly) && token.is_dashed_ident() => { unexpected!(parser); } _ => {} } let prelude = Self::parse_prelude(parser)?; - match parser.peek().clone() { - token @ Token::Eof => unexpected!(parser, token), - token @ Token::RightCurly if !parser.is(State::Nested) => unexpected!(parser, token), - Token::Ident(atom) if peek!(parser, 2, Kind::RightCurly) && atom.starts_with("--") => { + let token = parser.peek().clone(); + match token.kind() { + Kind::Eof => unexpected!(parser, token), + Kind::RightCurly if !parser.is(State::Nested) => unexpected!(parser, token), + Kind::Ident if peek!(parser, 2, Kind::RightCurly) && token.is_dashed_ident() => { unexpected!(parser); } _ => {} @@ -116,18 +119,19 @@ pub trait DeclarationRuleList<'a>: Sized + Parse<'a> { let mut declarations = parser.new_vec(); let mut rules = parser.new_vec(); loop { - match parser.peek() { - Token::AtKeyword(_) => { + let token = parser.peek(); + match token.kind() { + Kind::AtKeyword => { rules.push(Self::AtRule::parse_spanned(parser)?); } - Token::Ident(_) => { + Kind::Ident => { declarations.push(Self::Declaration::parse_spanned(parser)?); } - Token::RightCurly => { + Kind::RightCurly => { parser.advance(); return Ok((declarations, rules)); } - token => unexpected!(parser, token), + _ => unexpected!(parser, token), } } }