summaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorT0mstone <39707032+T0mstone@users.noreply.github.com>2025-01-23 14:05:12 +0100
committerGitHub <noreply@github.com>2025-01-23 13:05:12 +0000
commitb7546bace7fb8640d1e7121b8bd7baf3cdb576e1 (patch)
tree0ea8eb3f1208b3c88f73bef2a4f9a24c3f51c0a1 /crates
parentce299d5832095013bbcf2baef38552df6d2fc21b (diff)
Ignore shebang at start of file (#5702)
Diffstat (limited to 'crates')
-rw-r--r--crates/typst-syntax/src/highlight.rs1
-rw-r--r--crates/typst-syntax/src/kind.rs9
-rw-r--r--crates/typst-syntax/src/lexer.rs6
-rw-r--r--crates/typst-syntax/src/parser.rs2
4 files changed, 17 insertions, 1 deletions
diff --git a/crates/typst-syntax/src/highlight.rs b/crates/typst-syntax/src/highlight.rs
index de8ed65c..c59a0338 100644
--- a/crates/typst-syntax/src/highlight.rs
+++ b/crates/typst-syntax/src/highlight.rs
@@ -287,6 +287,7 @@ pub fn highlight(node: &LinkedNode) -> Option<Tag> {
SyntaxKind::Destructuring => None,
SyntaxKind::DestructAssignment => None,
+ SyntaxKind::Shebang => Some(Tag::Comment),
SyntaxKind::LineComment => Some(Tag::Comment),
SyntaxKind::BlockComment => Some(Tag::Comment),
SyntaxKind::Error => Some(Tag::Error),
diff --git a/crates/typst-syntax/src/kind.rs b/crates/typst-syntax/src/kind.rs
index 0a7c160b..b4a97a3e 100644
--- a/crates/typst-syntax/src/kind.rs
+++ b/crates/typst-syntax/src/kind.rs
@@ -9,6 +9,8 @@ pub enum SyntaxKind {
/// An invalid sequence of characters.
Error,
+ /// A shebang: `#! ...`
+ Shebang,
/// A line comment: `// ...`.
LineComment,
/// A block comment: `/* ... */`.
@@ -357,7 +359,11 @@ impl SyntaxKind {
pub fn is_trivia(self) -> bool {
matches!(
self,
- Self::LineComment | Self::BlockComment | Self::Space | Self::Parbreak
+ Self::Shebang
+ | Self::LineComment
+ | Self::BlockComment
+ | Self::Space
+ | Self::Parbreak
)
}
@@ -371,6 +377,7 @@ impl SyntaxKind {
match self {
Self::End => "end of tokens",
Self::Error => "syntax error",
+ Self::Shebang => "shebang",
Self::LineComment => "line comment",
Self::BlockComment => "block comment",
Self::Markup => "markup",
diff --git a/crates/typst-syntax/src/lexer.rs b/crates/typst-syntax/src/lexer.rs
index 6b5d2816..17401044 100644
--- a/crates/typst-syntax/src/lexer.rs
+++ b/crates/typst-syntax/src/lexer.rs
@@ -103,6 +103,7 @@ impl Lexer<'_> {
self.newline = false;
let kind = match self.s.eat() {
Some(c) if is_space(c, self.mode) => self.whitespace(start, c),
+ Some('#') if start == 0 && self.s.eat_if('!') => self.shebang(),
Some('/') if self.s.eat_if('/') => self.line_comment(),
Some('/') if self.s.eat_if('*') => self.block_comment(),
Some('*') if self.s.eat_if('/') => {
@@ -151,6 +152,11 @@ impl Lexer<'_> {
}
}
+ fn shebang(&mut self) -> SyntaxKind {
+ self.s.eat_until(is_newline);
+ SyntaxKind::Shebang
+ }
+
fn line_comment(&mut self) -> SyntaxKind {
self.s.eat_until(is_newline);
SyntaxKind::LineComment
diff --git a/crates/typst-syntax/src/parser.rs b/crates/typst-syntax/src/parser.rs
index 5b9e66e2..5de71caf 100644
--- a/crates/typst-syntax/src/parser.rs
+++ b/crates/typst-syntax/src/parser.rs
@@ -93,6 +93,8 @@ fn markup_expr(p: &mut Parser, at_start: bool, nesting: &mut usize) {
p.hint("try using a backslash escape: \\]");
}
+ SyntaxKind::Shebang => p.eat(),
+
SyntaxKind::Text
| SyntaxKind::Linebreak
| SyntaxKind::Escape