Sublime Forum

How to specify that a double tick mark should be escaped in a syntax file

#1

Hi all, I’m creating a custom syntax file for a programming language that is not defined to Sublime Text.

One of the rules of this language is, like YAML itself, that if a double tick mark (or a “double single quote,” if you will, like this: '') appears within a single quoted string, it should be escaped and shown as a literal single quote mark.

For example: 'My name is ''David''' appears as My name is 'David'.

How do I define this in the YAML specification? This is what I’m trying:

strings:
  - match: '"'
    scope: punctuation.definition.string.double.lang
    push: inside_string
  - match: ''''
    scope: punctuation.definition.string.single.lang
    push: inside_string

inside_string:
  - meta_include_prototype: false
  - meta_scope: string.quoted.double.lang
  - match: '"'
    scope: punctuation.definition.string.double.lang
    pop: true
  # Escaped double tick mark should show as an escaped quote within
  # a single quoted string
  - match: ''''''
    scope: constant.character.escape.lang
    pop: false
  - match: ''''
    scope: punctuation.definition.string.single.lang
    pop: true

But I think my problem is that when defining constant.character.escape.lang, it thinks that '' is supposed to be the “escape character” (meaning it should be followed by something else) and not just escape the single quote itself.

I’m not familiar with YAML, so how can this be defined in the syntax definition?

0 Likes

#2

The highlighting of your YAML by the excellent PackageDev package seems to indicate that you’re doing it correctly.

You do not need a pop: false, though, and I’m not sure what that does. Because the match appears above the single single-quote, it will match first and not pop the stack.

You may also want to have separate inside_string_double and inside_string_single contexts, unless 'My name is David" is intended to be a complete string.

0 Likes