mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Ignore shebang at start of file (#5702)
This commit is contained in:
parent
ce299d5832
commit
b7546bace7
@ -287,6 +287,7 @@ pub fn highlight(node: &LinkedNode) -> Option<Tag> {
|
|||||||
SyntaxKind::Destructuring => None,
|
SyntaxKind::Destructuring => None,
|
||||||
SyntaxKind::DestructAssignment => None,
|
SyntaxKind::DestructAssignment => None,
|
||||||
|
|
||||||
|
SyntaxKind::Shebang => Some(Tag::Comment),
|
||||||
SyntaxKind::LineComment => Some(Tag::Comment),
|
SyntaxKind::LineComment => Some(Tag::Comment),
|
||||||
SyntaxKind::BlockComment => Some(Tag::Comment),
|
SyntaxKind::BlockComment => Some(Tag::Comment),
|
||||||
SyntaxKind::Error => Some(Tag::Error),
|
SyntaxKind::Error => Some(Tag::Error),
|
||||||
|
@ -9,6 +9,8 @@ pub enum SyntaxKind {
|
|||||||
/// An invalid sequence of characters.
|
/// An invalid sequence of characters.
|
||||||
Error,
|
Error,
|
||||||
|
|
||||||
|
/// A shebang: `#! ...`
|
||||||
|
Shebang,
|
||||||
/// A line comment: `// ...`.
|
/// A line comment: `// ...`.
|
||||||
LineComment,
|
LineComment,
|
||||||
/// A block comment: `/* ... */`.
|
/// A block comment: `/* ... */`.
|
||||||
@ -357,7 +359,11 @@ impl SyntaxKind {
|
|||||||
pub fn is_trivia(self) -> bool {
|
pub fn is_trivia(self) -> bool {
|
||||||
matches!(
|
matches!(
|
||||||
self,
|
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 {
|
match self {
|
||||||
Self::End => "end of tokens",
|
Self::End => "end of tokens",
|
||||||
Self::Error => "syntax error",
|
Self::Error => "syntax error",
|
||||||
|
Self::Shebang => "shebang",
|
||||||
Self::LineComment => "line comment",
|
Self::LineComment => "line comment",
|
||||||
Self::BlockComment => "block comment",
|
Self::BlockComment => "block comment",
|
||||||
Self::Markup => "markup",
|
Self::Markup => "markup",
|
||||||
|
@ -103,6 +103,7 @@ impl Lexer<'_> {
|
|||||||
self.newline = false;
|
self.newline = false;
|
||||||
let kind = match self.s.eat() {
|
let kind = match self.s.eat() {
|
||||||
Some(c) if is_space(c, self.mode) => self.whitespace(start, c),
|
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.line_comment(),
|
||||||
Some('/') if self.s.eat_if('*') => self.block_comment(),
|
Some('/') if self.s.eat_if('*') => self.block_comment(),
|
||||||
Some('*') if self.s.eat_if('/') => {
|
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 {
|
fn line_comment(&mut self) -> SyntaxKind {
|
||||||
self.s.eat_until(is_newline);
|
self.s.eat_until(is_newline);
|
||||||
SyntaxKind::LineComment
|
SyntaxKind::LineComment
|
||||||
|
@ -93,6 +93,8 @@ fn markup_expr(p: &mut Parser, at_start: bool, nesting: &mut usize) {
|
|||||||
p.hint("try using a backslash escape: \\]");
|
p.hint("try using a backslash escape: \\]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SyntaxKind::Shebang => p.eat(),
|
||||||
|
|
||||||
SyntaxKind::Text
|
SyntaxKind::Text
|
||||||
| SyntaxKind::Linebreak
|
| SyntaxKind::Linebreak
|
||||||
| SyntaxKind::Escape
|
| SyntaxKind::Escape
|
||||||
|
7
tests/suite/syntax/shebang.typ
Normal file
7
tests/suite/syntax/shebang.typ
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
// Test shebang support.
|
||||||
|
|
||||||
|
--- shebang ---
|
||||||
|
#!typst compile
|
||||||
|
|
||||||
|
// Error: 2-3 the character `!` is not valid in code
|
||||||
|
#!not-a-shebang
|
Loading…
x
Reference in New Issue
Block a user