From f85e5aac64784deac75950a1307f2ca802ad6765 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 8 Jul 2021 19:37:38 +0200 Subject: [PATCH] URL special case for line comments --- src/parse/scanner.rs | 5 ----- src/parse/tokens.rs | 13 ++++++++----- tests/ref/code/comment.png | Bin 708 -> 1904 bytes tests/typ/code/comment.typ | 5 +++++ tools/support/typst.tmLanguage.json | 18 ++++++++++++------ 5 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/parse/scanner.rs b/src/parse/scanner.rs index 1a0e3045b..fad44e89f 100644 --- a/src/parse/scanner.rs +++ b/src/parse/scanner.rs @@ -96,11 +96,6 @@ impl<'s> Scanner<'s> { self.peek().map(f).unwrap_or(false) } - /// Checks whether the remaining source starts with the given string. - pub fn starts_with(&self, string: &str) -> bool { - self.rest().starts_with(string) - } - /// The previous index in the source string. pub fn last_index(&self) -> usize { self.eaten() diff --git a/src/parse/tokens.rs b/src/parse/tokens.rs index c9b7dc21e..f21967402 100644 --- a/src/parse/tokens.rs +++ b/src/parse/tokens.rs @@ -72,9 +72,9 @@ impl<'s> Iterator for Tokens<'s> { // Whitespace. c if c.is_whitespace() => self.whitespace(c), - // Comments. - '/' if self.s.eat_if('/') => self.line_comment(), + // Comments with special case for URLs. '/' if self.s.eat_if('*') => self.block_comment(), + '/' if !self.maybe_in_url() && self.s.eat_if('/') => self.line_comment(), '*' if self.s.eat_if('/') => Token::Invalid(self.s.eaten_from(start)), // Other things. @@ -216,7 +216,7 @@ impl<'s> Tokens<'s> { self.s.eat_assert(c); Token::Text(&self.s.eaten_from(start)) } - 'u' if self.s.starts_with("u{") => { + 'u' if self.s.rest().starts_with("u{") => { self.s.eat_assert('u'); self.s.eat_assert('{'); Token::UnicodeEscape(UnicodeEscapeToken { @@ -367,7 +367,7 @@ impl<'s> Tokens<'s> { // Read the fractional part if not already done. // Make sure not to confuse a range for the decimal separator. - if c != '.' && !self.s.starts_with("..") && self.s.eat_if('.') { + if c != '.' && !self.s.rest().starts_with("..") && self.s.eat_if('.') { self.s.eat_while(|c| c.is_ascii_digit()); } @@ -464,6 +464,10 @@ impl<'s> Tokens<'s> { Token::BlockComment(self.s.get(start .. end)) } + + fn maybe_in_url(&self) -> bool { + self.mode == TokenMode::Markup && self.s.eaten().ends_with(":/") + } } impl Debug for Tokens<'_> { @@ -688,7 +692,6 @@ mod tests { t!(Markup[" /"]: r"\a" => Text(r"\"), Text("a")); t!(Markup[" /"]: r"\u" => Text(r"\"), Text("u")); t!(Markup[" /"]: r"\1" => Text(r"\"), Text("1")); - t!(Markup[" /"]: r"\:" => Text(r"\"), Text(":")); t!(Markup[" /"]: r#"\""# => Text(r"\"), Text("\"")); // Test basic unicode escapes. diff --git a/tests/ref/code/comment.png b/tests/ref/code/comment.png index 6654e1255f466031825e7f32df41ac3d5d521bb2..bb8bf69ab794e054c4c65a67e5d96349c841b88c 100644 GIT binary patch literal 1904 zcmZ`)dpOgJ8~={U+>Xg5a+#U)Yg7_L#Ee{1BzI1Thp0x5+r-ceCkfB_btto(vg%>W z9BIpKm`f$)+FX_$5-Yh3ZF9?hI_Ix*`9073ywB%(pU?9?&wKq`KjZ1DB)?xC001R- zH>a}z00QsgO~|eTA_}7w06^Bt-O0fhH?cUmm`t+O>{&^d9s?}fwcGdLq+38d%w>$F zzV?99EhQAeWdI`KOMleiKKe9FaSwhzDugp)5et=r9ypcgg8RsCwo01rFaX0L4iHBu z0ZP>W3(QaNw9YIG?DS};Ip~65I zNA|+OE}myG0b|T%U`ekw>z>N_j^cY#T>=>eSAt+E;0rTV`Z^5yRz2Ewzf`6~kjN96 zOC^aXU3W~R27b}1dUHb)E91$bO{F zBF;>f3%}bce$uw)O-QNv_p<+pviBhM zc`1^=g^fu~m&XkF?A=BfiUkBjKn0wEV_ zmAk*1NR2RICq$xR30WICn<>zg0l8!^%r6*Ry0m5gRNjyMN>4 z)=V=mjAp+T@x=-&_qmyOlP)^2L=I5e%#xqRFGs&<^KDC|vQoi6Z4ibYOgF>pLhV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092d+W0)!AM+KkFJZpK_^zSE;5MUbz@aH0-Z$F{S!l1-UAsxC_}t`^((yIP zk9MZHfPnw9Gfa#u0uDesP>Gg&Cl3DWuh-uZShC7{y~^2hYvg_f8>hD#%s%>d>vjd# z^y6U_f(NT-Z@b>i>Upq6k7Lp!PuK72EYrE0maLq9|LkGj#7&0cES_gqRhsiRpS`rz z=9|f$whdP<)yc7G*W~)1V08?#_k6YOv%w9WwR7hdI|k^?Enau;+~K$RO@6=6_ss7x zxXYGk&0pSf@K=n$tlR6Wt@(E_$juyb}6D|`?rm0tsmliCT+Q&w6UE~n$AIj8eBKe<#k&E)-j z;aN(PeML2w@w(|gog`+ddbGG|(w~2=JZ WYs>yOPK+-GB~MRRKbLh*2~7Y^dll~h diff --git a/tests/typ/code/comment.typ b/tests/typ/code/comment.typ index 25180211f..694600d6c 100644 --- a/tests/typ/code/comment.typ +++ b/tests/typ/code/comment.typ @@ -14,6 +14,11 @@ C/* #test(type(/*1*/ 1) // , "integer") +--- +// Line comments have a special case for URLs. +https://example.com \ +https:/* block comments don't ... */ + --- // End should not appear without start. // Error: 1:7-1:9 unexpected end of block comment diff --git a/tools/support/typst.tmLanguage.json b/tools/support/typst.tmLanguage.json index aaad9ba8f..e1f416535 100644 --- a/tools/support/typst.tmLanguage.json +++ b/tools/support/typst.tmLanguage.json @@ -6,12 +6,6 @@ "repository": { "common": { "patterns": [ - { - "name": "comment.line.double-slash.typst", - "begin": "//", - "end": "\n", - "beginCaptures": { "0": { "name": "punctuation.definition.comment.typst" } } - }, { "name": "comment.block.typst", "begin": "/\\*", @@ -38,6 +32,12 @@ "markup": { "patterns": [ { "include": "#common" }, + { + "name": "comment.line.double-slash.typst", + "begin": "(?