mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Remove redundant Expr
prefix from tokens 🧽
This commit is contained in:
parent
2467cd6272
commit
da2ade6542
@ -284,7 +284,7 @@ impl<'s> FuncParser<'s> {
|
|||||||
impl FuncParser<'_> {
|
impl FuncParser<'_> {
|
||||||
fn parse_ident(&mut self) -> Option<Spanned<Ident>> {
|
fn parse_ident(&mut self) -> Option<Spanned<Ident>> {
|
||||||
self.peek().and_then(|token| match token.v {
|
self.peek().and_then(|token| match token.v {
|
||||||
Token::ExprIdent(id) => self.eat_span(Ident(id.to_string())),
|
Token::Ident(id) => self.eat_span(Ident(id.to_string())),
|
||||||
_ => None,
|
_ => None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -360,7 +360,7 @@ impl FuncParser<'_> {
|
|||||||
let Spanned { v: token, span } = self.peek()?;
|
let Spanned { v: token, span } = self.peek()?;
|
||||||
match token {
|
match token {
|
||||||
// This could be a named tuple or an identifier.
|
// This could be a named tuple or an identifier.
|
||||||
Token::ExprIdent(id) => {
|
Token::Ident(id) => {
|
||||||
let name = Spanned::new(Ident(id.to_string()), span);
|
let name = Spanned::new(Ident(id.to_string()), span);
|
||||||
self.eat();
|
self.eat();
|
||||||
self.skip_white();
|
self.skip_white();
|
||||||
@ -371,17 +371,17 @@ impl FuncParser<'_> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
Token::ExprStr { string, terminated } => {
|
Token::Str { string, terminated } => {
|
||||||
if !terminated {
|
if !terminated {
|
||||||
self.expected_at("quote", span.end);
|
self.expected_at("quote", span.end);
|
||||||
}
|
}
|
||||||
self.eat_span(Expr::Str(unescape_string(string)))
|
self.eat_span(Expr::Str(unescape_string(string)))
|
||||||
}
|
}
|
||||||
|
|
||||||
Token::ExprBool(b) => self.eat_span(Expr::Bool(b)),
|
Token::Bool(b) => self.eat_span(Expr::Bool(b)),
|
||||||
Token::ExprNumber(n) => self.eat_span(Expr::Number(n)),
|
Token::Number(n) => self.eat_span(Expr::Number(n)),
|
||||||
Token::ExprLength(s) => self.eat_span(Expr::Length(s)),
|
Token::Length(s) => self.eat_span(Expr::Length(s)),
|
||||||
Token::ExprHex(s) => {
|
Token::Hex(s) => {
|
||||||
if let Ok(color) = RgbaColor::from_str(s) {
|
if let Ok(color) = RgbaColor::from_str(s) {
|
||||||
self.eat_span(Expr::Color(color))
|
self.eat_span(Expr::Color(color))
|
||||||
} else {
|
} else {
|
||||||
|
@ -54,15 +54,15 @@ pub enum Token<'s> {
|
|||||||
|
|
||||||
/// A colon in a function header: `:`.
|
/// A colon in a function header: `:`.
|
||||||
Colon,
|
Colon,
|
||||||
/// A comma in a function header: `:`.
|
/// A comma in a function header: `,`.
|
||||||
Comma,
|
Comma,
|
||||||
/// An equals sign in a function header: `=`.
|
/// An equals sign in a function header: `=`.
|
||||||
Equals,
|
Equals,
|
||||||
|
|
||||||
/// An identifier in a function header: `center`.
|
/// An identifier in a function header: `center`.
|
||||||
ExprIdent(&'s str),
|
Ident(&'s str),
|
||||||
/// A quoted string in a function header: `"..."`.
|
/// A quoted string in a function header: `"..."`.
|
||||||
ExprStr {
|
Str {
|
||||||
/// The string inside the quotes.
|
/// The string inside the quotes.
|
||||||
///
|
///
|
||||||
/// _Note_: If the string contains escape sequences these are not yet
|
/// _Note_: If the string contains escape sequences these are not yet
|
||||||
@ -73,13 +73,13 @@ pub enum Token<'s> {
|
|||||||
terminated: bool,
|
terminated: bool,
|
||||||
},
|
},
|
||||||
/// A boolean in a function header: `true | false`.
|
/// A boolean in a function header: `true | false`.
|
||||||
ExprBool(bool),
|
Bool(bool),
|
||||||
/// A number in a function header: `3.14`.
|
/// A number in a function header: `3.14`.
|
||||||
ExprNumber(f64),
|
Number(f64),
|
||||||
/// A length in a function header: `12pt`.
|
/// A length in a function header: `12pt`.
|
||||||
ExprLength(Length),
|
Length(Length),
|
||||||
/// A hex value in a function header: `#20d82a`.
|
/// A hex value in a function header: `#20d82a`.
|
||||||
ExprHex(&'s str),
|
Hex(&'s str),
|
||||||
/// A plus in a function header, signifying the addition of expressions.
|
/// A plus in a function header, signifying the addition of expressions.
|
||||||
Plus,
|
Plus,
|
||||||
/// A hyphen in a function header, signifying the subtraction of
|
/// A hyphen in a function header, signifying the subtraction of
|
||||||
@ -127,12 +127,12 @@ impl<'s> Token<'s> {
|
|||||||
Colon => "colon",
|
Colon => "colon",
|
||||||
Comma => "comma",
|
Comma => "comma",
|
||||||
Equals => "equals sign",
|
Equals => "equals sign",
|
||||||
ExprIdent(_) => "identifier",
|
Ident(_) => "identifier",
|
||||||
ExprStr { .. } => "string",
|
Str { .. } => "string",
|
||||||
ExprBool(_) => "bool",
|
Bool(_) => "bool",
|
||||||
ExprNumber(_) => "number",
|
Number(_) => "number",
|
||||||
ExprLength(_) => "length",
|
Length(_) => "length",
|
||||||
ExprHex(_) => "hex value",
|
Hex(_) => "hex value",
|
||||||
Plus => "plus",
|
Plus => "plus",
|
||||||
Hyphen => "minus",
|
Hyphen => "minus",
|
||||||
Slash => "slash",
|
Slash => "slash",
|
||||||
@ -370,7 +370,7 @@ impl<'s> Tokens<'s> {
|
|||||||
|
|
||||||
fn read_string(&mut self) -> Token<'s> {
|
fn read_string(&mut self) -> Token<'s> {
|
||||||
let (string, terminated) = self.read_until_unescaped('"');
|
let (string, terminated) = self.read_until_unescaped('"');
|
||||||
ExprStr { string, terminated }
|
Str { string, terminated }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_raw(&mut self) -> Token<'s> {
|
fn read_raw(&mut self) -> Token<'s> {
|
||||||
@ -414,7 +414,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.
|
||||||
ExprHex(self.read_string_until(
|
Hex(self.read_string_until(
|
||||||
|n| !n.is_ascii_alphanumeric(),
|
|n| !n.is_ascii_alphanumeric(),
|
||||||
false, 0, 0
|
false, 0, 0
|
||||||
).0)
|
).0)
|
||||||
@ -422,15 +422,15 @@ impl<'s> Tokens<'s> {
|
|||||||
|
|
||||||
fn read_expr(&mut self, text: &'s str) -> Token<'s> {
|
fn read_expr(&mut self, text: &'s str) -> Token<'s> {
|
||||||
if let Ok(b) = text.parse::<bool>() {
|
if let Ok(b) = text.parse::<bool>() {
|
||||||
ExprBool(b)
|
Bool(b)
|
||||||
} else if let Ok(num) = text.parse::<f64>() {
|
} else if let Ok(num) = text.parse::<f64>() {
|
||||||
ExprNumber(num)
|
Number(num)
|
||||||
} else if let Some(num) = parse_percentage(text) {
|
} else if let Some(num) = parse_percentage(text) {
|
||||||
ExprNumber(num / 100.0)
|
Number(num / 100.0)
|
||||||
} else if let Ok(length) = text.parse::<Length>() {
|
} else if let Ok(length) = text.parse::<Length>() {
|
||||||
ExprLength(length)
|
Length(length)
|
||||||
} else if is_identifier(text) {
|
} else if is_identifier(text) {
|
||||||
ExprIdent(text)
|
Ident(text)
|
||||||
} else {
|
} else {
|
||||||
Invalid(text)
|
Invalid(text)
|
||||||
}
|
}
|
||||||
@ -542,11 +542,11 @@ mod tests {
|
|||||||
LineComment as LC, BlockComment as BC,
|
LineComment as LC, BlockComment as BC,
|
||||||
LeftParen as LP, RightParen as RP,
|
LeftParen as LP, RightParen as RP,
|
||||||
LeftBrace as LB, RightBrace as RB,
|
LeftBrace as LB, RightBrace as RB,
|
||||||
ExprIdent as Id,
|
Ident as Id,
|
||||||
ExprBool as Bool,
|
Bool,
|
||||||
ExprNumber as Num,
|
Number as Num,
|
||||||
ExprLength as Len,
|
Length as Len,
|
||||||
ExprHex as Hex,
|
Hex,
|
||||||
Plus,
|
Plus,
|
||||||
Hyphen as Min,
|
Hyphen as Min,
|
||||||
Slash,
|
Slash,
|
||||||
@ -563,7 +563,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn Str(string: &str, terminated: bool) -> Token { Token::ExprStr { string, terminated } }
|
fn Str(string: &str, terminated: bool) -> Token { Token::Str { string, terminated } }
|
||||||
fn Raw(raw: &str, terminated: bool) -> Token { Token::Raw { raw, terminated } }
|
fn Raw(raw: &str, terminated: bool) -> Token { Token::Raw { raw, terminated } }
|
||||||
|
|
||||||
macro_rules! func {
|
macro_rules! func {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user