0

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"!";

catzilla
  • 1,901
  • 18
  • 31

2 Answers2

1

You need to match all characters that are not " and also combinations of \+anything in between the quotes.

Use

"[^"\\]*(?:\\.[^"\\]*)*"

See this regex demo

It can also be written as "(?:[^"\\]|\\.)*". It is easier to read but is less efficient.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • I found this answer correct and useful, however I also found a better implementation using YAML tmlanguage specifiers. – catzilla Apr 18 '16 at 06:07
  • 1
    Then you can post. Actually, within begin/end, you can use nested rules, so you can declare the quotes as begin and end patterns and then use a nesnested rule for an escaped entity. – Wiktor Stribiżew Apr 18 '16 at 06:10
  • yes, that is true.. what you said is exactly what I did. I used `begin` and `end`. and also used nesting.. :) – catzilla Apr 18 '16 at 06:12
1

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

catzilla
  • 1,901
  • 18
  • 31