mirror of
https://github.com/typst/typst
synced 2025-06-28 16:22:53 +08:00
Some code and styling improvements 🎨
This commit is contained in:
parent
e9a9581252
commit
332f83ed2d
@ -213,7 +213,7 @@ impl<'s> FuncParser<'s> {
|
|||||||
Ok(Spanned::new(
|
Ok(Spanned::new(
|
||||||
FuncArg::Key(Spanned::new(Pair { key: ident, value }, span)),
|
FuncArg::Key(Spanned::new(Pair { key: ident, value }, span)),
|
||||||
span,
|
span,
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
// Add a positional argument because there was no equals
|
// Add a positional argument because there was no equals
|
||||||
// sign after the identifier that could have been a key.
|
// sign after the identifier that could have been a key.
|
||||||
@ -230,12 +230,11 @@ impl<'s> FuncParser<'s> {
|
|||||||
}).v
|
}).v
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse an expression which may contain math operands.
|
/// Parse an expression which may contain math operands. For this, this
|
||||||
/// For this, this method looks for operators in
|
/// method looks for operators in descending order of associativity, i.e. we
|
||||||
/// descending order of associativity, i.e. we first drill down to find all
|
/// first drill down to find all negations, brackets and tuples, the next
|
||||||
/// negations, brackets and tuples, the next level,
|
/// level, we look for multiplication and division and here finally, for
|
||||||
/// we look for multiplication and division and here finally, for addition
|
/// addition and subtraction.
|
||||||
/// and subtraction.
|
|
||||||
fn parse_expr(&mut self) -> Option<Spanned<Expr>> {
|
fn parse_expr(&mut self) -> Option<Spanned<Expr>> {
|
||||||
let o1 = self.parse_term()?;
|
let o1 = self.parse_term()?;
|
||||||
self.parse_binop(o1, "summand", Self::parse_expr, |token| match token {
|
self.parse_binop(o1, "summand", Self::parse_expr, |token| match token {
|
||||||
@ -258,40 +257,37 @@ impl<'s> FuncParser<'s> {
|
|||||||
&mut self,
|
&mut self,
|
||||||
o1: Spanned<Expr>,
|
o1: Spanned<Expr>,
|
||||||
operand_name: &str,
|
operand_name: &str,
|
||||||
mut parse_operand: F,
|
parse_operand: F,
|
||||||
parse_op: G,
|
parse_op: G,
|
||||||
) -> Option<Spanned<Expr>>
|
) -> Option<Spanned<Expr>>
|
||||||
where
|
where
|
||||||
F: FnMut(&mut Self) -> Option<Spanned<Expr>>,
|
F: FnOnce(&mut Self) -> Option<Spanned<Expr>>,
|
||||||
G: FnOnce(Token) -> Option<fn(Box<Spanned<Expr>>, Box<Spanned<Expr>>) -> Expr>,
|
G: FnOnce(Token) -> Option<fn(Box<Spanned<Expr>>, Box<Spanned<Expr>>) -> Expr>,
|
||||||
{
|
{
|
||||||
self.skip_whitespace();
|
self.skip_whitespace();
|
||||||
|
|
||||||
if let Some(next) = self.peek() {
|
if let Some(next) = self.peek() {
|
||||||
let binop = match parse_op(next.v) {
|
if let Some(binop) = parse_op(next.v) {
|
||||||
Some(op) => op,
|
self.eat();
|
||||||
None => return Some(o1),
|
self.skip_whitespace();
|
||||||
};
|
|
||||||
|
|
||||||
self.eat();
|
if let Some(o2) = parse_operand(self) {
|
||||||
self.skip_whitespace();
|
let span = Span::merge(o1.span, o2.span);
|
||||||
|
let expr = binop(Box::new(o1), Box::new(o2));
|
||||||
if let Some(o2) = parse_operand(self) {
|
return Some(Spanned::new(expr, span));
|
||||||
let span = Span::merge(o1.span, o2.span);
|
} else {
|
||||||
let expr = binop(Box::new(o1), Box::new(o2));
|
self.feedback.errors.push(err!(
|
||||||
return Some(Spanned::new(expr, span));
|
Span::merge(next.span, o1.span);
|
||||||
} else {
|
"missing right {}", operand_name,
|
||||||
self.feedback.errors.push(err!(
|
));
|
||||||
Span::merge(next.span, o1.span);
|
}
|
||||||
"missing right {}", operand_name,
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(o1)
|
Some(o1)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse expressions that are of the form value or -value
|
/// Parse expressions that are of the form value or -value.
|
||||||
fn parse_factor(&mut self) -> Option<Spanned<Expr>> {
|
fn parse_factor(&mut self) -> Option<Spanned<Expr>> {
|
||||||
let first = self.peek()?;
|
let first = self.peek()?;
|
||||||
if first.v == Token::Hyphen {
|
if first.v == Token::Hyphen {
|
||||||
@ -349,12 +345,12 @@ impl<'s> FuncParser<'s> {
|
|||||||
},
|
},
|
||||||
|
|
||||||
Token::LeftParen => {
|
Token::LeftParen => {
|
||||||
let mut tuple = self.parse_tuple();
|
let (mut tuple, can_be_coerced) = self.parse_tuple();
|
||||||
// Coerce 1-tuple into value
|
// Coerce 1-tuple into value
|
||||||
if tuple.1 && tuple.0.v.items.len() > 0 {
|
if can_be_coerced && tuple.v.items.len() > 0 {
|
||||||
tuple.0.v.items.pop().expect("Length is one")
|
tuple.v.items.pop().expect("length is at least one")
|
||||||
} else {
|
} else {
|
||||||
tuple.0.map(|t| Expr::Tuple(t))
|
tuple.map(|t| Expr::Tuple(t))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Token::LeftBrace => self.parse_object().map(|o| Expr::Object(o)),
|
Token::LeftBrace => self.parse_object().map(|o| Expr::Object(o)),
|
||||||
@ -364,14 +360,14 @@ impl<'s> FuncParser<'s> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Parse a tuple expression: `(<expr>, ...)`. The boolean in the return
|
/// Parse a tuple expression: `(<expr>, ...)`. The boolean in the return
|
||||||
/// values showes whether the tuple can be coerced into a single value
|
/// values showes whether the tuple can be coerced into a single value.
|
||||||
fn parse_tuple(&mut self) -> (Spanned<Tuple>, bool) {
|
fn parse_tuple(&mut self) -> (Spanned<Tuple>, bool) {
|
||||||
let token = self.eat();
|
let token = self.eat();
|
||||||
debug_assert_eq!(token.map(Spanned::value), Some(Token::LeftParen));
|
debug_assert_eq!(token.map(Spanned::value), Some(Token::LeftParen));
|
||||||
|
|
||||||
// Parse a collection until a right paren appears and complain about
|
// Parse a collection until a right paren appears and complain about
|
||||||
// missing a `value` when an invalid token is encoutered.
|
// missing a `value` when an invalid token is encoutered.
|
||||||
self.parse_collection_bracket_aware(Some(Token::RightParen),
|
self.parse_collection_comma_aware(Some(Token::RightParen),
|
||||||
|p| p.parse_expr().ok_or(("value", None)))
|
|p| p.parse_expr().ok_or(("value", None)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -413,12 +409,25 @@ impl<'s> FuncParser<'s> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parse a comma-separated collection where each item is parsed through
|
||||||
|
/// `parse_item` until the `end` token is met.
|
||||||
|
fn parse_collection<C, I, F>(
|
||||||
|
&mut self,
|
||||||
|
end: Option<Token>,
|
||||||
|
parse_item: F
|
||||||
|
) -> Spanned<C>
|
||||||
|
where
|
||||||
|
C: FromIterator<Spanned<I>>,
|
||||||
|
F: FnMut(&mut Self) -> Result<Spanned<I>, (&'static str, Option<Position>)>,
|
||||||
|
{
|
||||||
|
self.parse_collection_comma_aware(end, parse_item).0
|
||||||
|
}
|
||||||
|
|
||||||
/// Parse a comma-separated collection where each item is parsed through
|
/// Parse a comma-separated collection where each item is parsed through
|
||||||
/// `parse_item` until the `end` token is met. The first item in the return
|
/// `parse_item` until the `end` token is met. The first item in the return
|
||||||
/// tuple is the collection, the second item indicates whether the
|
/// tuple is the collection, the second item indicates whether the
|
||||||
/// collection can be coerced into a single item
|
/// collection can be coerced into a single item (i.e. no comma appeared).
|
||||||
/// (i.e. at least one comma appeared).
|
fn parse_collection_comma_aware<C, I, F>(
|
||||||
fn parse_collection_bracket_aware<C, I, F>(
|
|
||||||
&mut self,
|
&mut self,
|
||||||
end: Option<Token>,
|
end: Option<Token>,
|
||||||
mut parse_item: F
|
mut parse_item: F
|
||||||
@ -490,20 +499,6 @@ impl<'s> FuncParser<'s> {
|
|||||||
(Spanned::new(collection, Span { start, end }), can_be_coerced)
|
(Spanned::new(collection, Span { start, end }), can_be_coerced)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse a comma-separated collection where each item is parsed through
|
|
||||||
/// `parse_item` until the `end` token is met.
|
|
||||||
fn parse_collection<C, I, F>(
|
|
||||||
&mut self,
|
|
||||||
end: Option<Token>,
|
|
||||||
parse_item: F
|
|
||||||
) -> Spanned<C>
|
|
||||||
where
|
|
||||||
C: FromIterator<Spanned<I>>,
|
|
||||||
F: FnMut(&mut Self) -> Result<Spanned<I>, (&'static str, Option<Position>)>,
|
|
||||||
{
|
|
||||||
self.parse_collection_bracket_aware(end, parse_item).0
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Try to parse an identifier and do nothing if the peekable token is no
|
/// Try to parse an identifier and do nothing if the peekable token is no
|
||||||
/// identifier.
|
/// identifier.
|
||||||
fn parse_ident(&mut self) -> Option<Spanned<Ident>> {
|
fn parse_ident(&mut self) -> Option<Spanned<Ident>> {
|
||||||
|
@ -133,9 +133,9 @@ impl<'s> Token<'s> {
|
|||||||
ExprSize(_) => "size",
|
ExprSize(_) => "size",
|
||||||
ExprBool(_) => "bool",
|
ExprBool(_) => "bool",
|
||||||
ExprHex(_) => "hex value",
|
ExprHex(_) => "hex value",
|
||||||
Plus => "plus",
|
Plus => "plus",
|
||||||
Hyphen => "minus",
|
Hyphen => "minus",
|
||||||
Slash => "slash",
|
Slash => "slash",
|
||||||
Star => "star",
|
Star => "star",
|
||||||
Underscore => "underscore",
|
Underscore => "underscore",
|
||||||
Backslash => "backslash",
|
Backslash => "backslash",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user