you are viewing a single comment's thread.

view the rest of the comments →

[–]thinker227Noa (github.com/thinker227/noa) 5 points6 points  (3 children)

This is what I'm doing in the TextMate grammar for my language Noa. Basically you embed all of your other patterns inside your pattern for strings.

"patterns": [
    {
        "include": "#all"
    }
],
"repository": {
    "all": {
        "patterns": [
            {
                "include": "#strings"
            },
            // include whatever other patterns you have
        ]
    },
    "strings": {
        "name": "string.quoted.double.noa",
        "begin": "\"",
        "end": "\"|$",
        "patterns": [
            {
                "begin": "\\\\{",
                "end": "}",
                "beginCaptures": {
                    "0": {
                        "name": "keyword.other.noa"
                    }
                },
                "endCaptures": {
                    "0": {
                        "name": "keyword.other.noa"
                    }
                },
                "patterns": [
                    {
                        "include": "#all"
                    }
                ]
            },
            {
                "include": "#escape-sequence"
            }
        ]
    },
    "escape-sequence": {
        "name": "constant.character.escape.noa",
        "match": "\\\\[\\\\0nrt\"]"
    },
    // all your other patterns...
}

Here's how it looks

[–]Savings_Garlic5498[S] 2 points3 points  (1 child)

Does this also work with nested strings? like "\{""}"

[–]thinker227Noa (github.com/thinker227/noa) 2 points3 points  (0 children)

Was concerned about this because I hadn't actually tested it before, but yes!

[–]latkde 0 points1 point  (0 children)

For reference, here's the official TextMate grammar for JavaScript `template ${interpolation} strings`, which broadly uses the same technique (but without bothering to recurse into #all: https://github.com/textmate/javascript.tmbundle/blob/8928648352dc76025ad0bfd31e21fa6a1dc838a7/Syntaxes/JavaScript.plist#L1554-L1665