diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-08-30 22:18:55 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-08-30 22:18:55 +0200 |
| commit | 181f756a9e8f7b664101058fe91e36b3858c2d02 (patch) | |
| tree | 542e7c694e91d8cc91fa97a328e9bda0567db679 /src/syntax | |
| parent | 0d44cf532136f3ba8e34d6f967f9e844cfb9c3f0 (diff) | |
Format everything with rustfmt! 💚
Diffstat (limited to 'src/syntax')
| -rw-r--r-- | src/syntax/mod.rs | 4 | ||||
| -rw-r--r-- | src/syntax/parsing.rs | 74 | ||||
| -rw-r--r-- | src/syntax/tokens.rs | 87 | ||||
| -rw-r--r-- | src/syntax/tree.rs | 12 |
4 files changed, 88 insertions, 89 deletions
diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index 596291a5..a9fe7c2e 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -37,9 +37,9 @@ impl Debug for Ident { #[cfg(test)] mod tests { - use std::fmt::Debug; - use crate::prelude::*; use super::span; + use crate::prelude::*; + use std::fmt::Debug; /// Assert that expected and found are equal, printing both and panicking /// and the source of their test case if they aren't. diff --git a/src/syntax/parsing.rs b/src/syntax/parsing.rs index d2688951..ae9cfdb1 100644 --- a/src/syntax/parsing.rs +++ b/src/syntax/parsing.rs @@ -2,16 +2,14 @@ use std::str::FromStr; -use crate::{Feedback, Pass}; -use crate::color::RgbaColor; -use crate::compute::table::SpannedEntry; use super::decoration::Decoration; use super::span::{Pos, Span, Spanned}; use super::tokens::{is_newline_char, Token, TokenMode, Tokens}; -use super::tree::{ - CallExpr, Expr, SyntaxNode, SyntaxTree, TableExpr, Code, -}; +use super::tree::{CallExpr, Code, Expr, SyntaxNode, SyntaxTree, TableExpr}; use super::Ident; +use crate::color::RgbaColor; +use crate::compute::table::SpannedEntry; +use crate::{Feedback, Pass}; /// Parse a string of source code. pub fn parse(src: &str) -> Pass<SyntaxTree> { @@ -106,9 +104,7 @@ impl Parser<'_> { self.with_span(SyntaxNode::Code(Code { lang, lines, block })) } - Token::Text(text) => { - self.with_span(SyntaxNode::Text(text.to_string())) - } + Token::Text(text) => self.with_span(SyntaxNode::Text(text.to_string())), Token::UnicodeEscape { sequence, terminated } => { if !terminated { @@ -222,7 +218,10 @@ impl Parser<'_> { let mut table = TableExpr::new(); let mut comma_and_keyless = true; - while { self.skip_white(); !self.eof() } { + while { + self.skip_white(); + !self.eof() + } { let (key, val) = if let Some(ident) = self.parse_ident() { self.skip_white(); @@ -243,7 +242,7 @@ impl Parser<'_> { (None, call.map(Expr::Call)) } - _ => (None, ident.map(Expr::Ident)) + _ => (None, ident.map(Expr::Ident)), } } else if let Some(value) = self.parse_expr() { (None, value) @@ -256,13 +255,17 @@ impl Parser<'_> { if let Some(key) = key { comma_and_keyless = false; table.insert(key.v.0, SpannedEntry::new(key.span, val)); - self.feedback.decorations + self.feedback + .decorations .push(Spanned::new(Decoration::TableKey, key.span)); } else { table.push(SpannedEntry::val(val)); } - if { self.skip_white(); self.eof() } { + if { + self.skip_white(); + self.eof() + } { break; } @@ -389,9 +392,7 @@ impl Parser<'_> { let span = self.end_group(); let expr = if coercable { - table.into_values() - .next() - .expect("table is coercable").val.v + table.into_values().next().expect("table is coercable").val.v } else { Expr::Table(table) }; @@ -479,8 +480,7 @@ impl<'s> Parser<'s> { fn end_group(&mut self) -> Span { let peeked = self.peek(); - let (start, end_token) = self.delimiters.pop() - .expect("group was not started"); + let (start, end_token) = self.delimiters.pop().expect("group was not started"); if end_token != Token::Chain && peeked != None { self.delimiters.push((start, end_token)); @@ -529,11 +529,7 @@ impl<'s> Parser<'s> { } fn check_eat(&mut self, token: Token<'_>) -> Option<Spanned<Token<'s>>> { - if self.check(token) { - self.eat() - } else { - None - } + if self.check(token) { self.eat() } else { None } } /// Checks if the next token is of some kind @@ -590,8 +586,7 @@ impl Group { fn is_delimiter(token: Token<'_>) -> bool { matches!( token, - Token::RightParen | Token::RightBracket - | Token::RightBrace | Token::Chain + Token::RightParen | Token::RightBracket | Token::RightBrace | Token::Chain ) } @@ -655,7 +650,10 @@ fn unescape_string(string: &str) -> String { } Some('n') => out.push('\n'), Some('t') => out.push('\t'), - Some(c) => { out.push('\\'); out.push(c); } + Some(c) => { + out.push('\\'); + out.push(c); + } None => out.push('\\'), } } else { @@ -785,22 +783,20 @@ fn split_lines(text: &str) -> Vec<String> { #[cfg(test)] #[allow(non_snake_case)] mod tests { - use crate::syntax::tests::*; - use crate::length::Length; use super::*; + use crate::length::Length; + use crate::syntax::tests::*; use Decoration::*; // ----------------------- Construct Syntax Nodes ----------------------- // use SyntaxNode::{ - Spacing as S, - Linebreak as L, - Parbreak as P, - ToggleItalic as I, - ToggleBolder as B, + Linebreak as L, Parbreak as P, Spacing as S, ToggleBolder as B, ToggleItalic as I, }; - fn T(text: &str) -> SyntaxNode { SyntaxNode::Text(text.to_string()) } + fn T(text: &str) -> SyntaxNode { + SyntaxNode::Text(text.to_string()) + } macro_rules! R { ($($line:expr),* $(,)?) => { @@ -833,10 +829,14 @@ mod tests { // ------------------------ Construct Expressions ----------------------- // - use Expr::{Bool, Number as Num, Length as Len, Color}; + use Expr::{Bool, Color, Length as Len, Number as Num}; - fn Id(ident: &str) -> Expr { Expr::Ident(Ident(ident.to_string())) } - fn Str(string: &str) -> Expr { Expr::Str(string.to_string()) } + fn Id(ident: &str) -> Expr { + Expr::Ident(Ident(ident.to_string())) + } + fn Str(string: &str) -> Expr { + Expr::Str(string.to_string()) + } macro_rules! Table { (@table=$table:expr,) => {}; diff --git a/src/syntax/tokens.rs b/src/syntax/tokens.rs index dad13230..d566363c 100644 --- a/src/syntax/tokens.rs +++ b/src/syntax/tokens.rs @@ -4,8 +4,8 @@ use std::iter::Peekable; use std::str::Chars; use unicode_xid::UnicodeXID; -use crate::length::Length; use super::span::{Pos, Span, Spanned}; +use crate::length::Length; use Token::*; use TokenMode::*; @@ -224,7 +224,10 @@ impl<'s> Iterator for Tokens<'s> { // Comments. '/' if self.peek() == Some('/') => self.read_line_comment(), '/' if self.peek() == Some('*') => self.read_block_comment(), - '*' if self.peek() == Some('/') => { self.eat(); Invalid("*/") } + '*' if self.peek() == Some('/') => { + self.eat(); + Invalid("*/") + } // Whitespace. c if c.is_whitespace() => self.read_whitespace(start), @@ -241,8 +244,7 @@ impl<'s> Iterator for Tokens<'s> { ':' if self.mode == Header => Colon, ',' if self.mode == Header => Comma, '=' if self.mode == Header => Equals, - '>' if self.mode == Header && self.peek() == Some('>') => - self.read_chain(), + '>' if self.mode == Header && self.peek() == Some('>') => self.read_chain(), // Expression operators. '+' if self.mode == Header => Plus, @@ -276,10 +278,10 @@ impl<'s> Iterator for Tokens<'s> { let (text, _) = self.read_string_until(false, start_offset, 0, |n| { let val = match n { c if c.is_whitespace() => true, - '[' | ']' | '{' | '}' | '/' | '*' => true, + '[' | ']' | '{' | '}' | '/' | '*' => true, '\\' | '_' | '`' if body => true, - ':' | '=' | ',' | '"' | '(' | ')' if !body => true, - '+' | '-' if !body && !last_was_e => true, + ':' | '=' | ',' | '"' | '(' | ')' if !body => true, + '+' | '-' if !body && !last_was_e => true, _ => false, }; @@ -309,7 +311,11 @@ impl<'s> Tokens<'s> { } fn read_block_comment(&mut self) -> Token<'s> { - enum Last { Slash, Star, Other } + enum Last { + Slash, + Star, + Other, + } let mut depth = 0; let mut last = Last::Other; @@ -322,12 +328,12 @@ impl<'s> Tokens<'s> { '/' => match last { Last::Star if depth == 0 => return true, Last::Star => depth -= 1, - _ => last = Last::Slash - } + _ => last = Last::Slash, + }, '*' => match last { Last::Slash => depth += 1, _ => last = Last::Star, - } + }, _ => last = Last::Other, } @@ -409,8 +415,8 @@ impl<'s> Tokens<'s> { Code { lang, - raw: &self.src[start..end], - terminated + raw: &self.src[start .. end], + terminated, } } else { Raw { raw, terminated } @@ -443,9 +449,8 @@ impl<'s> Tokens<'s> { self.eat(); if self.peek() == Some('{') { self.eat(); - let (sequence, _) = self.read_string_until(false, 0, 0, |c| { - !c.is_ascii_hexdigit() - }); + let (sequence, _) = + self.read_string_until(false, 0, 0, |c| !c.is_ascii_hexdigit()); let terminated = self.peek() == Some('}'); if terminated { @@ -460,7 +465,7 @@ impl<'s> Tokens<'s> { Some(c) if is_escapable(c) => { let index = self.index(); self.eat(); - Text(&self.src[index..index + c.len_utf8()]) + Text(&self.src[index .. index + c.len_utf8()]) } Some(c) if c.is_whitespace() => Backslash, Some(_) => Text("\\"), @@ -522,7 +527,7 @@ impl<'s> Tokens<'s> { end = ((end as isize) + offset_end) as usize; } - (&self.src[start..end], matched) + (&self.src[start .. end], matched) } fn eat(&mut self) -> Option<char> { @@ -546,7 +551,7 @@ impl<'s> Tokens<'s> { fn parse_percentage(text: &str) -> Option<f64> { if text.ends_with('%') { - text[..text.len() - 1].parse::<f64>().ok() + text[.. text.len() - 1].parse::<f64>().ok() } else { None } @@ -556,7 +561,7 @@ fn parse_percentage(text: &str) -> Option<f64> { pub fn is_newline_char(character: char) -> bool { match character { // Line Feed, Vertical Tab, Form Feed, Carriage Return. - '\x0A'..='\x0D' => true, + '\x0A' ..= '\x0D' => true, // Next Line, Line Separator, Paragraph Separator. '\u{0085}' | '\u{2028}' | '\u{2029}' => true, _ => false, @@ -588,35 +593,33 @@ pub fn is_identifier(string: &str) -> bool { #[cfg(test)] #[allow(non_snake_case)] mod tests { + use super::super::span::Spanned; + use super::*; use crate::length::Length; use crate::syntax::tests::*; - use super::*; - use super::super::span::Spanned; use Token::{ - Space as S, - LineComment as LC, BlockComment as BC, - LeftBracket as L, RightBracket as R, - LeftParen as LP, RightParen as RP, - LeftBrace as LB, RightBrace as RB, - Chain, - Ident as Id, - Bool, - Number as Num, - Length as Len, - Hex, - Plus, - Hyphen as Min, - Slash, - Star, - Text as T, + BlockComment as BC, Bool, Chain, Hex, Hyphen as Min, Ident as Id, + LeftBrace as LB, LeftBracket as L, LeftParen as LP, Length as Len, + LineComment as LC, Number as Num, Plus, RightBrace as RB, RightBracket as R, + RightParen as RP, Slash, Space as S, Star, Text as T, }; - fn Str(string: &str, terminated: bool) -> Token { Token::Str { string, terminated } } - fn Raw(raw: &str, terminated: bool) -> Token { Token::Raw { raw, terminated } } + fn Str(string: &str, terminated: bool) -> Token { + Token::Str { string, terminated } + } + fn Raw(raw: &str, terminated: bool) -> Token { + Token::Raw { raw, terminated } + } fn Code<'a>(lang: Option<&'a str>, raw: &'a str, terminated: bool) -> Token<'a> { - Token::Code { lang: lang.map(Spanned::zero), raw, terminated } + Token::Code { + lang: lang.map(Spanned::zero), + raw, + terminated, + } + } + fn UE(sequence: &str, terminated: bool) -> Token { + Token::UnicodeEscape { sequence, terminated } } - fn UE(sequence: &str, terminated: bool) -> Token { Token::UnicodeEscape { sequence, terminated } } macro_rules! t { ($($tts:tt)*) => {test!(@spans=false, $($tts)*)} } macro_rules! ts { ($($tts:tt)*) => {test!(@spans=true, $($tts)*)} } diff --git a/src/syntax/tree.rs b/src/syntax/tree.rs index 44acd023..7295ec04 100644 --- a/src/syntax/tree.rs +++ b/src/syntax/tree.rs @@ -2,15 +2,15 @@ use std::fmt::{self, Debug, Formatter}; +use super::decoration::Decoration; +use super::span::{SpanVec, Spanned}; +use super::Ident; use crate::color::RgbaColor; use crate::compute::table::{SpannedEntry, Table}; use crate::compute::value::{TableValue, Value}; use crate::layout::LayoutContext; use crate::length::Length; use crate::{DynFuture, Feedback}; -use super::decoration::Decoration; -use super::span::{Spanned, SpanVec}; -use super::Ident; /// A collection of nodes which form a tree together with the nodes' children. pub type SyntaxTree = SpanVec<SyntaxNode>; @@ -96,11 +96,7 @@ impl Expr { } /// Evaluate the expression to a value. - pub async fn eval( - &self, - ctx: &LayoutContext<'_>, - f: &mut Feedback, - ) -> Value { + pub async fn eval(&self, ctx: &LayoutContext<'_>, f: &mut Feedback) -> Value { use Expr::*; match self { Ident(i) => Value::Ident(i.clone()), |
