mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Remove unnecessary group
This commit is contained in:
parent
76b1d4a93f
commit
3a07603b66
@ -276,24 +276,25 @@ fn enum_node(p: &mut Parser, at_start: bool) {
|
|||||||
|
|
||||||
/// Parse an expression within markup mode.
|
/// Parse an expression within markup mode.
|
||||||
fn markup_expr(p: &mut Parser) {
|
fn markup_expr(p: &mut Parser) {
|
||||||
if let Some(token) = p.peek() {
|
// Does the expression need termination or can content follow directly?
|
||||||
let stmt = matches!(
|
let stmt = matches!(
|
||||||
token,
|
p.peek(),
|
||||||
|
Some(
|
||||||
NodeKind::Let
|
NodeKind::Let
|
||||||
| NodeKind::Set
|
| NodeKind::Set
|
||||||
| NodeKind::Show
|
| NodeKind::Show
|
||||||
| NodeKind::Wrap
|
| NodeKind::Wrap
|
||||||
| NodeKind::Import
|
| NodeKind::Import
|
||||||
);
|
| NodeKind::Include
|
||||||
let group = if stmt { Group::Stmt } else { Group::Expr };
|
)
|
||||||
|
);
|
||||||
|
|
||||||
p.start_group(group);
|
p.start_group(Group::Expr);
|
||||||
let res = expr_prec(p, true, 0);
|
let res = expr_prec(p, true, 0);
|
||||||
if stmt && res.is_ok() && !p.eof() {
|
if stmt && res.is_ok() && !p.eof() {
|
||||||
p.expected_at("semicolon or line break");
|
p.expected_at("semicolon or line break");
|
||||||
}
|
|
||||||
p.end_group();
|
|
||||||
}
|
}
|
||||||
|
p.end_group();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse an expression.
|
/// Parse an expression.
|
||||||
@ -626,7 +627,7 @@ fn block(p: &mut Parser) {
|
|||||||
p.perform(NodeKind::Block, |p| {
|
p.perform(NodeKind::Block, |p| {
|
||||||
p.start_group(Group::Brace);
|
p.start_group(Group::Brace);
|
||||||
while !p.eof() {
|
while !p.eof() {
|
||||||
p.start_group(Group::Stmt);
|
p.start_group(Group::Expr);
|
||||||
if expr(p).is_ok() && !p.eof() {
|
if expr(p).is_ok() && !p.eof() {
|
||||||
p.expected_at("semicolon or line break");
|
p.expected_at("semicolon or line break");
|
||||||
}
|
}
|
||||||
|
@ -248,7 +248,6 @@ impl<'s> Parser<'s> {
|
|||||||
Group::Paren => self.eat_assert(&NodeKind::LeftParen),
|
Group::Paren => self.eat_assert(&NodeKind::LeftParen),
|
||||||
Group::Bracket => self.eat_assert(&NodeKind::LeftBracket),
|
Group::Bracket => self.eat_assert(&NodeKind::LeftBracket),
|
||||||
Group::Brace => self.eat_assert(&NodeKind::LeftBrace),
|
Group::Brace => self.eat_assert(&NodeKind::LeftBrace),
|
||||||
Group::Stmt => {}
|
|
||||||
Group::Expr => {}
|
Group::Expr => {}
|
||||||
Group::Imports => {}
|
Group::Imports => {}
|
||||||
}
|
}
|
||||||
@ -274,8 +273,7 @@ impl<'s> Parser<'s> {
|
|||||||
Group::Paren => Some((NodeKind::RightParen, true)),
|
Group::Paren => Some((NodeKind::RightParen, true)),
|
||||||
Group::Bracket => Some((NodeKind::RightBracket, true)),
|
Group::Bracket => Some((NodeKind::RightBracket, true)),
|
||||||
Group::Brace => Some((NodeKind::RightBrace, true)),
|
Group::Brace => Some((NodeKind::RightBrace, true)),
|
||||||
Group::Stmt => Some((NodeKind::Semicolon, false)),
|
Group::Expr => Some((NodeKind::Semicolon, false)),
|
||||||
Group::Expr => None,
|
|
||||||
Group::Imports => None,
|
Group::Imports => None,
|
||||||
} {
|
} {
|
||||||
if self.current.as_ref() == Some(&end) {
|
if self.current.as_ref() == Some(&end) {
|
||||||
@ -324,7 +322,7 @@ impl<'s> Parser<'s> {
|
|||||||
Some(NodeKind::RightParen) => self.inside(Group::Paren),
|
Some(NodeKind::RightParen) => self.inside(Group::Paren),
|
||||||
Some(NodeKind::RightBracket) => self.inside(Group::Bracket),
|
Some(NodeKind::RightBracket) => self.inside(Group::Bracket),
|
||||||
Some(NodeKind::RightBrace) => self.inside(Group::Brace),
|
Some(NodeKind::RightBrace) => self.inside(Group::Brace),
|
||||||
Some(NodeKind::Semicolon) => self.inside(Group::Stmt),
|
Some(NodeKind::Semicolon) => self.inside(Group::Expr),
|
||||||
Some(NodeKind::From) => self.inside(Group::Imports),
|
Some(NodeKind::From) => self.inside(Group::Imports),
|
||||||
Some(NodeKind::Space(n)) => *n >= 1 && self.stop_at_newline(),
|
Some(NodeKind::Space(n)) => *n >= 1 && self.stop_at_newline(),
|
||||||
Some(_) => false,
|
Some(_) => false,
|
||||||
@ -352,7 +350,7 @@ impl<'s> Parser<'s> {
|
|||||||
fn stop_at_newline(&self) -> bool {
|
fn stop_at_newline(&self) -> bool {
|
||||||
matches!(
|
matches!(
|
||||||
self.groups.last().map(|group| group.kind),
|
self.groups.last().map(|group| group.kind),
|
||||||
Some(Group::Stmt | Group::Expr | Group::Imports)
|
Some(Group::Expr | Group::Imports)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -488,8 +486,6 @@ pub enum Group {
|
|||||||
/// A parenthesized group: `(...)`.
|
/// A parenthesized group: `(...)`.
|
||||||
Paren,
|
Paren,
|
||||||
/// A group ended by a semicolon or a line break: `;`, `\n`.
|
/// A group ended by a semicolon or a line break: `;`, `\n`.
|
||||||
Stmt,
|
|
||||||
/// A group for a single expression, ended by a line break.
|
|
||||||
Expr,
|
Expr,
|
||||||
/// A group for import items, ended by a semicolon, line break or `from`.
|
/// A group for import items, ended by a semicolon, line break or `from`.
|
||||||
Imports,
|
Imports,
|
||||||
|
@ -875,54 +875,6 @@ impl SetExpr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
node! {
|
|
||||||
/// An import expression: `import a, b, c from "utils.typ"`.
|
|
||||||
ImportExpr
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ImportExpr {
|
|
||||||
/// The items to be imported.
|
|
||||||
pub fn imports(&self) -> Imports {
|
|
||||||
self.0
|
|
||||||
.children()
|
|
||||||
.find_map(|node| match node.kind() {
|
|
||||||
NodeKind::Star => Some(Imports::Wildcard),
|
|
||||||
NodeKind::ImportItems => {
|
|
||||||
let items = node.children().filter_map(RedRef::cast).collect();
|
|
||||||
Some(Imports::Items(items))
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
})
|
|
||||||
.expect("import is missing items")
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The location of the importable file.
|
|
||||||
pub fn path(&self) -> Expr {
|
|
||||||
self.0.cast_last_child().expect("import is missing path")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The items that ought to be imported from a file.
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub enum Imports {
|
|
||||||
/// All items in the scope of the file should be imported.
|
|
||||||
Wildcard,
|
|
||||||
/// The specified items from the file should be imported.
|
|
||||||
Items(Vec<Ident>),
|
|
||||||
}
|
|
||||||
|
|
||||||
node! {
|
|
||||||
/// An include expression: `include "chapter1.typ"`.
|
|
||||||
IncludeExpr
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IncludeExpr {
|
|
||||||
/// The location of the file to be included.
|
|
||||||
pub fn path(&self) -> Expr {
|
|
||||||
self.0.cast_last_child().expect("include is missing path")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
node! {
|
node! {
|
||||||
/// A show expression: `show heading(body) as [*{body}*]`.
|
/// A show expression: `show heading(body) as [*{body}*]`.
|
||||||
ShowExpr
|
ShowExpr
|
||||||
@ -1041,6 +993,54 @@ impl ForPattern {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
node! {
|
||||||
|
/// An import expression: `import a, b, c from "utils.typ"`.
|
||||||
|
ImportExpr
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ImportExpr {
|
||||||
|
/// The items to be imported.
|
||||||
|
pub fn imports(&self) -> Imports {
|
||||||
|
self.0
|
||||||
|
.children()
|
||||||
|
.find_map(|node| match node.kind() {
|
||||||
|
NodeKind::Star => Some(Imports::Wildcard),
|
||||||
|
NodeKind::ImportItems => {
|
||||||
|
let items = node.children().filter_map(RedRef::cast).collect();
|
||||||
|
Some(Imports::Items(items))
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.expect("import is missing items")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The location of the importable file.
|
||||||
|
pub fn path(&self) -> Expr {
|
||||||
|
self.0.cast_last_child().expect("import is missing path")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The items that ought to be imported from a file.
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub enum Imports {
|
||||||
|
/// All items in the scope of the file should be imported.
|
||||||
|
Wildcard,
|
||||||
|
/// The specified items from the file should be imported.
|
||||||
|
Items(Vec<Ident>),
|
||||||
|
}
|
||||||
|
|
||||||
|
node! {
|
||||||
|
/// An include expression: `include "chapter1.typ"`.
|
||||||
|
IncludeExpr
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IncludeExpr {
|
||||||
|
/// The location of the file to be included.
|
||||||
|
pub fn path(&self) -> Expr {
|
||||||
|
self.0.cast_last_child().expect("include is missing path")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
node! {
|
node! {
|
||||||
/// An identifier.
|
/// An identifier.
|
||||||
Ident: NodeKind::Ident(_)
|
Ident: NodeKind::Ident(_)
|
||||||
|
@ -26,3 +26,7 @@
|
|||||||
// The variables of the file should not appear in this scope.
|
// The variables of the file should not appear in this scope.
|
||||||
// Error: 1-6 unknown variable
|
// Error: 1-6 unknown variable
|
||||||
#name
|
#name
|
||||||
|
|
||||||
|
---
|
||||||
|
// Error: 18 expected semicolon or line break
|
||||||
|
#include "hi.typ" Hi
|
||||||
|
Loading…
x
Reference in New Issue
Block a user