Search code examples
regexstringsublimetext2yamltmlanguage

How to match operator-separated strings in Sublime Package Development YAML tmlanguage


I am making a syntax definition for a custom-made language in sublime text 2 using PackageDevelopment's .YAML-tmLanguage. For now I want my syntax to identify strings to non strings.

sample line of code:

string name = "Chuck Norris";
string message = "I am " + name + ", don't mess with a \"ROCKSTAR\"!";

my pattern for double quoted string:

- comment: strings in double quotes
  match: (".+")
  captures:
    '1': {name: string.quoted.double.me}

what the pattern captures:

string name = "Chuck Norris";
string message = "I am " + name + ", don't mess with a "ROCKSTAR"!";

line 1 above is correct but line 2 seems to capture all.

what I want is:

string name = "Chuck Norris";
string message = "I am " + name + ", don't mess with a "ROCKSTAR"!";


Solution

  • I discovered a good implementation on how to match operator-separated strings and also to match double quoted strings and some additional functionalities using YAML.

    @Wiktor Stribiżewv's answer also fulfills the question but I found a good implementation for this:

    - comment: strings in double quotes
      name: string.quoted.double.hit
      begin: \"
      end: \"
      patterns:
      - comment: escape characters
        name: constant.character.escape.hit
        match: \\.
    
      - comment: special characters
        name: constant.character.hit
        match: \%.
    

    this also matches escape characters like \", \n and special characters %s, %d