diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-06-30 22:32:24 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-06-30 22:32:24 +0200 |
| commit | 17e89468847735df10381c47c46c7d82d33cc463 (patch) | |
| tree | b0ce55f7d62aac399717aac3ab5a76c981c66f65 /src/parse | |
| parent | 911b5818344e85a58da9db895a333d22484b7ae7 (diff) | |
Remove color literals (#39)
Diffstat (limited to 'src/parse')
| -rw-r--r-- | src/parse/mod.rs | 8 | ||||
| -rw-r--r-- | src/parse/tokens.rs | 56 |
2 files changed, 17 insertions, 47 deletions
diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 0afcd88b..a56e451d 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -385,7 +385,6 @@ fn literal(p: &mut Parser) -> Option<Expr> { Token::Angle(val, unit) => Expr::Angle(span, val, unit), Token::Percent(p) => Expr::Percent(span, p), Token::Fraction(p) => Expr::Fractional(span, p), - Token::Color(color) => Expr::Color(span, color), Token::Str(token) => Expr::Str(span, { if !token.terminated { p.expected_at("quote", p.peek_span().end); @@ -672,10 +671,9 @@ fn if_expr(p: &mut Parser) -> Option<Expr> { // We are in code mode but still want to react to `#else` if the // outer mode is markup. - if match p.outer_mode() { - TokenMode::Markup => p.eat_if(Token::Invalid("#else")), - TokenMode::Code => p.eat_if(Token::Else), - } { + if (p.outer_mode() == TokenMode::Code || p.eat_if(Token::Invalid("#"))) + && p.eat_if(Token::Else) + { else_body = body(p); } diff --git a/src/parse/tokens.rs b/src/parse/tokens.rs index 4d90dded..522b3136 100644 --- a/src/parse/tokens.rs +++ b/src/parse/tokens.rs @@ -1,8 +1,6 @@ use std::fmt::{self, Debug, Formatter}; -use std::str::FromStr; use super::{is_newline, Scanner}; -use crate::color::RgbaColor; use crate::geom::{AngularUnit, LengthUnit}; use crate::syntax::*; @@ -71,9 +69,6 @@ impl<'s> Iterator for Tokens<'s> { '{' => Token::LeftBrace, '}' => Token::RightBrace, - // Headings, keywords, identifiers, colors. - '#' => self.hash(start), - // Whitespace. c if c.is_whitespace() => self.whitespace(c), @@ -104,6 +99,9 @@ impl<'s> Tokens<'s> { '-' => self.hyph(start), c if c == '.' || c.is_ascii_digit() => self.numbering(start, c), + // Headings, keywords and identifiers. + '#' => self.hash(start), + // Plain text. _ => self.text(start), } @@ -236,29 +234,17 @@ impl<'s> Tokens<'s> { } fn hash(&mut self, start: usize) -> Token<'s> { - match self.mode { - TokenMode::Markup => { - if self.s.check(is_id_start) { - let read = self.s.eat_while(is_id_continue); - if let Some(keyword) = keyword(read) { - keyword - } else { - Token::Ident(read) - } - } else if self.s.check(|c| c != '#' && !c.is_whitespace()) { - Token::Text(self.s.eaten_from(start)) - } else { - Token::Hashtag - } - } - TokenMode::Code => { - let read = self.s.eat_while(is_id_continue); - if let Ok(color) = RgbaColor::from_str(read) { - Token::Color(color) - } else { - Token::Invalid(self.s.eaten_from(start)) - } + if self.s.check(is_id_start) { + let read = self.s.eat_while(is_id_continue); + if let Some(keyword) = keyword(read) { + keyword + } else { + Token::Ident(read) } + } else if self.s.check(|c| c != '#' && !c.is_whitespace()) { + Token::Text(self.s.eaten_from(start)) + } else { + Token::Hashtag } } @@ -528,10 +514,6 @@ mod tests { Token::Math(MathToken { formula, display, terminated }) } - const fn Color(r: u8, g: u8, b: u8, a: u8) -> Token<'static> { - Token::Color(RgbaColor { r, g, b, a }) - } - const fn Str(string: &str, terminated: bool) -> Token { Token::Str(StrToken { string, terminated }) } @@ -583,7 +565,6 @@ mod tests { ('/', Some(Code), "(", LeftParen), ('/', Some(Code), ":", Colon), ('/', Some(Code), "+=", PlusEq), - ('/', Some(Code), "#123", Color(0x11, 0x22, 0x33, 0xff)), ]; macro_rules! t { @@ -927,13 +908,6 @@ mod tests { } #[test] - fn test_tokenize_color() { - t!(Code[" /"]: "#ABC" => Color(0xAA, 0xBB, 0xCC, 0xff)); - t!(Code[" /"]: "#6ae6dd" => Color(0x6a, 0xe6, 0xdd, 0xff)); - t!(Code[" /"]: "#8A083caf" => Color(0x8A, 0x08, 0x3c, 0xaf)); - } - - #[test] fn test_tokenize_strings() { // Test basic strings. t!(Code: "\"hi\"" => Str("hi", true)); @@ -999,13 +973,11 @@ mod tests { t!(Code: r"\:" => Invalid(r"\"), Colon); t!(Code: "meal⌚" => Ident("meal"), Invalid("⌚")); t!(Code[" /"]: r"\a" => Invalid(r"\"), Ident("a")); + t!(Code[" /"]: "#" => Invalid("#")); // Test invalid number suffixes. t!(Code[" /"]: "1foo" => Invalid("1foo")); t!(Code: "1p%" => Invalid("1p"), Invalid("%")); t!(Code: "1%%" => Percent(1.0), Invalid("%")); - - // Test invalid color. - t!(Code[" /"]: r"#letter" => Invalid(r"#letter")); } } |
