Make some code easier for rustfmt 🍦

This commit is contained in:
Laurenz 2020-08-30 22:09:33 +02:00
parent 3337d24543
commit 18c515cde5
3 changed files with 39 additions and 37 deletions

View File

@ -1,4 +1,5 @@
/// Unwrap the option if it is `Some(T)` or evaluate `$or` if it is `None`. /// Unwrap the option if it is `Some(T)` or evaluate `$or` if it is `None`.
#[allow(unused)]
macro_rules! try_or { macro_rules! try_or {
($option:expr, $or:expr $(,)?) => { ($option:expr, $or:expr $(,)?) => {
match $option { match $option {

View File

@ -230,11 +230,12 @@ impl Parser<'_> {
Some(Token::Equals) => { Some(Token::Equals) => {
self.eat(); self.eat();
self.skip_white(); self.skip_white();
if let Some(value) = self.parse_expr() {
(Some(ident), try_or!(self.parse_expr(), { (Some(ident), value)
} else {
self.expected("value"); self.expected("value");
continue; continue;
})) }
} }
Some(Token::LeftParen) => { Some(Token::LeftParen) => {
@ -244,11 +245,11 @@ impl Parser<'_> {
_ => (None, ident.map(Expr::Ident)) _ => (None, ident.map(Expr::Ident))
} }
} else if let Some(value) = self.parse_expr() {
(None, value)
} else { } else {
(None, try_or!(self.parse_expr(), { self.expected("value");
self.expected("value"); continue;
continue;
}))
}; };
let behind = val.span.end; let behind = val.span.end;
@ -274,6 +275,8 @@ impl Parser<'_> {
} }
} }
type Binop = fn(Box<Spanned<Expr>>, Box<Spanned<Expr>>) -> Expr;
// Expressions and values. // Expressions and values.
impl Parser<'_> { impl Parser<'_> {
fn parse_expr(&mut self) -> Option<Spanned<Expr>> { fn parse_expr(&mut self) -> Option<Spanned<Expr>> {
@ -297,9 +300,7 @@ impl Parser<'_> {
&mut self, &mut self,
operand_name: &str, operand_name: &str,
mut parse_operand: impl FnMut(&mut Self) -> Option<Spanned<Expr>>, mut parse_operand: impl FnMut(&mut Self) -> Option<Spanned<Expr>>,
mut parse_op: impl FnMut(Token) -> Option< mut parse_op: impl FnMut(Token) -> Option<Binop>,
fn(Box<Spanned<Expr>>, Box<Spanned<Expr>>) -> Expr
>,
) -> Option<Spanned<Expr>> { ) -> Option<Spanned<Expr>> {
let mut left = parse_operand(self)?; let mut left = parse_operand(self)?;

View File

@ -270,8 +270,10 @@ impl<'s> Iterator for Tokens<'s> {
c => { c => {
let body = self.mode == Body; let body = self.mode == Body;
let start_offset = -(c.len_utf8() as isize);
let mut last_was_e = false; let mut last_was_e = false;
let text = self.read_string_until(|n| {
let (text, _) = self.read_string_until(false, start_offset, 0, |n| {
let val = match n { let val = match n {
c if c.is_whitespace() => true, c if c.is_whitespace() => true,
'[' | ']' | '{' | '}' | '/' | '*' => true, '[' | ']' | '{' | '}' | '/' | '*' => true,
@ -283,7 +285,7 @@ impl<'s> Iterator for Tokens<'s> {
last_was_e = n == 'e' || n == 'E'; last_was_e = n == 'e' || n == 'E';
val val
}, false, -(c.len_utf8() as isize), 0).0; });
if self.mode == Header { if self.mode == Header {
self.read_expr(text) self.read_expr(text)
@ -302,21 +304,21 @@ impl<'s> Iterator for Tokens<'s> {
impl<'s> Tokens<'s> { impl<'s> Tokens<'s> {
fn read_line_comment(&mut self) -> Token<'s> { fn read_line_comment(&mut self) -> Token<'s> {
LineComment(self.read_string_until(is_newline_char, false, 1, 0).0) self.eat();
LineComment(self.read_string_until(false, 0, 0, is_newline_char).0)
} }
fn read_block_comment(&mut self) -> Token<'s> { fn read_block_comment(&mut self) -> Token<'s> {
enum Last { Slash, Star, Other } enum Last { Slash, Star, Other }
self.eat();
let mut depth = 0; let mut depth = 0;
let mut last = Last::Other; let mut last = Last::Other;
// Find the first `*/` that does not correspond to a nested `/*`. // Find the first `*/` that does not correspond to a nested `/*`.
// Remove the last two bytes to obtain the raw inner text without `*/`. // Remove the last two bytes to obtain the raw inner text without `*/`.
BlockComment(self.read_string_until(|n| { self.eat();
match n { let (content, _) = self.read_string_until(true, 0, -2, |c| {
match c {
'/' => match last { '/' => match last {
Last::Star if depth == 0 => return true, Last::Star if depth == 0 => return true,
Last::Star => depth -= 1, Last::Star => depth -= 1,
@ -330,7 +332,9 @@ impl<'s> Tokens<'s> {
} }
false false
}, true, 0, -2).0) });
BlockComment(content)
} }
fn read_chain(&mut self) -> Token<'s> { fn read_chain(&mut self) -> Token<'s> {
@ -339,7 +343,7 @@ impl<'s> Tokens<'s> {
} }
fn read_whitespace(&mut self, start: Pos) -> Token<'s> { fn read_whitespace(&mut self, start: Pos) -> Token<'s> {
self.read_string_until(|n| !n.is_whitespace(), false, 0, 0); self.read_string_until(false, 0, 0, |n| !n.is_whitespace());
let end = self.pos(); let end = self.pos();
Space(end.line - start.line) Space(end.line - start.line)
@ -358,11 +362,11 @@ impl<'s> Tokens<'s> {
// Reads the lang tag (until newline or whitespace). // Reads the lang tag (until newline or whitespace).
let start = self.pos(); let start = self.pos();
let lang = self.read_string_until( let (lang, _) = self.read_string_until(false, 0, 0, |c| {
|c| c == '`' || c.is_whitespace() || is_newline_char(c), c == '`' || c.is_whitespace() || is_newline_char(c)
false, 0, 0, });
).0;
let end = self.pos(); let end = self.pos();
let lang = if !lang.is_empty() { let lang = if !lang.is_empty() {
Some(Spanned::new(lang, Span::new(start, end))) Some(Spanned::new(lang, Span::new(start, end)))
} else { } else {
@ -413,17 +417,17 @@ impl<'s> Tokens<'s> {
} }
} }
fn read_until_unescaped(&mut self, c: char) -> (&'s str, bool) { fn read_until_unescaped(&mut self, end: char) -> (&'s str, bool) {
let mut escaped = false; let mut escaped = false;
self.read_string_until(|n| { self.read_string_until(true, 0, -1, |c| {
match n { match c {
n if n == c && !escaped => return true, c if c == end && !escaped => return true,
'\\' => escaped = !escaped, '\\' => escaped = !escaped,
_ => escaped = false, _ => escaped = false,
} }
false false
}, true, 0, -1) })
} }
fn read_escaped(&mut self) -> Token<'s> { fn read_escaped(&mut self) -> Token<'s> {
@ -439,10 +443,9 @@ impl<'s> Tokens<'s> {
self.eat(); self.eat();
if self.peek() == Some('{') { if self.peek() == Some('{') {
self.eat(); self.eat();
let sequence = self.read_string_until( let (sequence, _) = self.read_string_until(false, 0, 0, |c| {
|c| !c.is_ascii_hexdigit(), !c.is_ascii_hexdigit()
false, 0, 0, });
).0;
let terminated = self.peek() == Some('}'); let terminated = self.peek() == Some('}');
if terminated { if terminated {
@ -468,10 +471,7 @@ impl<'s> Tokens<'s> {
fn read_hex(&mut self) -> Token<'s> { fn read_hex(&mut self) -> Token<'s> {
// This will parse more than the permissable 0-9, a-f, A-F character // This will parse more than the permissable 0-9, a-f, A-F character
// ranges to provide nicer error messages later. // ranges to provide nicer error messages later.
Hex(self.read_string_until( Hex(self.read_string_until(false, 0, 0, |n| !n.is_ascii_alphanumeric()).0)
|n| !n.is_ascii_alphanumeric(),
false, 0, 0
).0)
} }
fn read_expr(&mut self, text: &'s str) -> Token<'s> { fn read_expr(&mut self, text: &'s str) -> Token<'s> {
@ -497,10 +497,10 @@ impl<'s> Tokens<'s> {
/// after the match depending on `eat_match`. /// after the match depending on `eat_match`.
fn read_string_until( fn read_string_until(
&mut self, &mut self,
mut f: impl FnMut(char) -> bool,
eat_match: bool, eat_match: bool,
offset_start: isize, offset_start: isize,
offset_end: isize, offset_end: isize,
mut f: impl FnMut(char) -> bool,
) -> (&'s str, bool) { ) -> (&'s str, bool) {
let start = ((self.index() as isize) + offset_start) as usize; let start = ((self.index() as isize) + offset_start) as usize;
let mut matched = false; let mut matched = false;