mirror of
https://github.com/typst/typst
synced 2025-05-15 01:25:28 +08:00
Fix newline parsing behavior in code mode (#3780)
This commit is contained in:
parent
ca0754d80f
commit
88b305c3f9
@ -88,8 +88,10 @@ impl Lexer<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Shared.
|
/// Shared methods with all [`LexMode`].
|
||||||
impl Lexer<'_> {
|
impl Lexer<'_> {
|
||||||
|
/// Proceed to the next token and return its [`SyntaxKind`]. Note the
|
||||||
|
/// token could be a [trivia](SyntaxKind::is_trivia).
|
||||||
pub fn next(&mut self) -> SyntaxKind {
|
pub fn next(&mut self) -> SyntaxKind {
|
||||||
if self.mode == LexMode::Raw {
|
if self.mode == LexMode::Raw {
|
||||||
let Some((kind, end)) = self.raw.pop() else {
|
let Some((kind, end)) = self.raw.pop() else {
|
||||||
@ -121,6 +123,7 @@ impl Lexer<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Eat whitespace characters greedily.
|
||||||
fn whitespace(&mut self, start: usize, c: char) -> SyntaxKind {
|
fn whitespace(&mut self, start: usize, c: char) -> SyntaxKind {
|
||||||
let more = self.s.eat_while(|c| is_space(c, self.mode));
|
let more = self.s.eat_while(|c| is_space(c, self.mode));
|
||||||
let newlines = match c {
|
let newlines = match c {
|
||||||
@ -760,7 +763,7 @@ impl ScannerExt for Scanner<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Whether a character will become a Space token in Typst
|
/// Whether a character will become a [`SyntaxKind::Space`] token.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn is_space(character: char, mode: LexMode) -> bool {
|
fn is_space(character: char, mode: LexMode) -> bool {
|
||||||
match mode {
|
match mode {
|
||||||
|
@ -1748,15 +1748,27 @@ impl<'s> Parser<'s> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn next_non_trivia(lexer: &mut Lexer<'s>) -> SyntaxKind {
|
||||||
|
loop {
|
||||||
|
let next = lexer.next();
|
||||||
|
// Loop is terminatable, because SyntaxKind::Eof is not a trivia.
|
||||||
|
if !next.is_trivia() {
|
||||||
|
break next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn lex(&mut self) {
|
fn lex(&mut self) {
|
||||||
self.current_start = self.lexer.cursor();
|
self.current_start = self.lexer.cursor();
|
||||||
self.current = self.lexer.next();
|
self.current = self.lexer.next();
|
||||||
|
|
||||||
|
// Special cases to handle newlines in code mode.
|
||||||
if self.lexer.mode() == LexMode::Code
|
if self.lexer.mode() == LexMode::Code
|
||||||
&& self.lexer.newline()
|
&& self.lexer.newline()
|
||||||
&& match self.newline_modes.last() {
|
&& match self.newline_modes.last() {
|
||||||
Some(NewlineMode::Continue) => false,
|
Some(NewlineMode::Continue) => false,
|
||||||
Some(NewlineMode::Contextual) => !matches!(
|
Some(NewlineMode::Contextual) => !matches!(
|
||||||
self.lexer.clone().next(),
|
Self::next_non_trivia(&mut self.lexer.clone()),
|
||||||
SyntaxKind::Else | SyntaxKind::Dot
|
SyntaxKind::Else | SyntaxKind::Dot
|
||||||
),
|
),
|
||||||
Some(NewlineMode::Stop) => true,
|
Some(NewlineMode::Stop) => true,
|
||||||
|
@ -22,3 +22,63 @@
|
|||||||
else {
|
else {
|
||||||
("1", "2")
|
("1", "2")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
---
|
||||||
|
// Ref: false
|
||||||
|
#test({
|
||||||
|
"hi 1"
|
||||||
|
|
||||||
|
.clusters()
|
||||||
|
}, ("h", "i", " ", "1"))
|
||||||
|
|
||||||
|
---
|
||||||
|
// Ref: false
|
||||||
|
#test({
|
||||||
|
"hi 2"// comment
|
||||||
|
.clusters()
|
||||||
|
}, ("h", "i", " ", "2"))
|
||||||
|
|
||||||
|
---
|
||||||
|
// Ref: false
|
||||||
|
#test({
|
||||||
|
"hi 3"/* comment */
|
||||||
|
.clusters()
|
||||||
|
}, ("h", "i", " ", "3"))
|
||||||
|
|
||||||
|
---
|
||||||
|
// Ref: false
|
||||||
|
#test({
|
||||||
|
"hi 4"
|
||||||
|
// comment
|
||||||
|
.clusters()
|
||||||
|
}, ("h", "i", " ", "4"))
|
||||||
|
|
||||||
|
---
|
||||||
|
// Ref: false
|
||||||
|
#test({
|
||||||
|
"hi 5"
|
||||||
|
/*comment*/.clusters()
|
||||||
|
}, ("h", "i", " ", "5"))
|
||||||
|
|
||||||
|
---
|
||||||
|
// Ref: false
|
||||||
|
#test({
|
||||||
|
"hi 6"
|
||||||
|
// comment
|
||||||
|
|
||||||
|
|
||||||
|
/* comment */
|
||||||
|
.clusters()
|
||||||
|
}, ("h", "i", " ", "6"))
|
||||||
|
|
||||||
|
---
|
||||||
|
// Ref: false
|
||||||
|
#test({
|
||||||
|
let foo(x) = {
|
||||||
|
if x < 0 { "negative" }
|
||||||
|
// comment
|
||||||
|
else { "non-negative" }
|
||||||
|
}
|
||||||
|
|
||||||
|
foo(1)
|
||||||
|
}, "non-negative")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user