Skip to content

Incorrect string syntax highlighting in attributes in VS Code #6042

@brendanzab

Description

@brendanzab

When using logos, I get the following broken syntax highlighting:

Screen Shot 2020-09-21 at 9 40 44 am

Original source for replication purposes:

#[derive(Debug, Clone, Logos)]
enum Quoted<'source> {
    #[regex(r#"[^\\"']+"#)]
    Text(&'source str),
    #[token("\\")]
    StartEscape,
    #[token("\'", |_| Quote::Single)]
    #[token("\"", |_| Quote::Double)]
    End(Quote),

    #[error]
    Error,
}

#[derive(Debug, Clone, Logos)]
enum Escape {
    #[token("\\", |_| '\\')]
    #[token("n", |_| '\n')]
    #[token("r", |_| '\r')]
    #[token("t", |_| '\t')]
    #[token("0", |_| '\0')]
    #[token("\'", |_| '\'')]
    #[token("\"", |_| '\"')]
    Single(char),
    #[token("u")]
    StartUnicodeEscape,
    #[token("x")]
    StartAsciiEscape,

    #[error]
    Error,
}

#[derive(Debug, Clone, Logos)]
enum UnicodeEscape<'source> {
    #[regex(r"\{[0-9a-fA-F]*\}", |lexer| &lexer.slice()[1..(lexer.slice().len() - 1)])]
    CharCode(&'source str),
    #[token("\'", |_| Quote::Single)]
    #[token("\"", |_| Quote::Double)]
    End(Quote),

    #[error]
    Error,
}

A workaround (suggested by @Kixiron and seen in the wild in Crunch) is to add // workaround highlighting: " to the offending lines:

 #[derive(Debug, Copy, Clone, PartialEq)]
 enum Quote {
     Single,
@@ -33,7 +61,7 @@ enum QuotedLiteral {

 #[derive(Debug, Clone, Logos)]
 enum Quoted<'source> {
-    #[regex(r#"[^\\"']+"#)]
+    #[regex(r#"[^\\"']+"#)] // workaround highlighting: "
     Text(&'source str),
     #[token("\\")]
     StartEscape,
@@ -53,7 +81,7 @@ enum Escape {
     #[token("t", |_| '\t')]
     #[token("0", |_| '\0')]
     #[token("\'", |_| '\'')]
-    #[token("\"", |_| '\"')]
+    #[token("\"", |_| '\"')] // workaround highlighting: "
     Single(char),
     #[token("u")]
     StartUnicodeEscape,

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-unknownIt's unclear if the issue is E-hard or E-easy without digging inS-actionableSomeone could pick this issue up and work on it right now

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions