SublimeText3 防止反斜杠在带引号的字符串中转义

SublimeText3 prevent backslash from escaping in quoted strings

我已经为一种专门的语言创建了一个 tmlanguage 文件。这种语言允许在字符串中使用反斜杠,并且不会对它们进行转义。所以当我创建变量时:

路径 = 'C:\Temp\';

Sublime 认为引号被转义了,然后我的脚本其余部分的语法着色关闭了。

如何防止这种情况发生?

编辑:糟糕!忘记来源(感谢 MattDMo)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>fileTypes</key>
    <array>
        <string>txt</string>
    </array>
    <key>name</key>
    <string>TM1</string>
    <key>patterns</key>
    <array>
        <dict>
            <key>match</key>
            <string>(=|&lt;&gt;|&gt;|&lt;|@=|@&lt;&gt;|@&gt;|@&lt;)</string>
            <key>name</key>
            <string>keyword.operator.tm1</string>
        </dict>     
        <dict>
            <key>match</key>
            <string>(?i)\b(If|While|Else|ElseIf|End|EndIf)\b</string>
            <key>name</key>
            <string>keyword.control.tm1</string>
        </dict> 
        <dict>
            <key>begin</key>
            <string>'</string>
            <key>beginCaptures</key>
            <dict>
                <key>0</key>
                <dict>
                    <key>name</key>
                    <string>punctuation.definition.string.begin.tm1</string>
                </dict>
            </dict>
            <key>end</key>
            <string>'</string>
            <key>endCaptures</key>
            <dict>
                <key>0</key>
                <dict>
                    <key>name</key>
                    <string>punctuation.definition.string.end.tm1</string>
                </dict>
            </dict>
            <key>name</key>
            <string>string.quoted.single.tm1</string>
            <key>patterns</key>
            <array>
                <dict>
                    <key>match</key>
                    <string>\(x\h{2}|[0-2][0-7]{,2}|3[0-6][0-7]?|37[0-7]?|[4-7][0-7]?|.)</string>
                    <key>name</key>
                    <string>constant.character.escape.tm1</string>
                </dict>
            </array>
        </dict>
        <dict>
            <key>begin</key>
            <string>"</string>
            <key>beginCaptures</key>
            <dict>
                <key>0</key>
                <dict>
                    <key>name</key>
                    <string>punctuation.definition.string.begin.tm1</string>
                </dict>
            </dict>
            <key>end</key>
            <string>"</string>
            <key>endCaptures</key>
            <dict>
                <key>0</key>
                <dict>
                    <key>name</key>
                    <string>punctuation.definition.string.end.tm1</string>
                </dict>
            </dict>
            <key>name</key>
            <string>string.quoted.double.tm1</string>
            <key>patterns</key>
            <array>
                <dict>
                    <key>match</key>
                    <string>\(x\h{2}|[0-2][0-7]{,2}|3[0-6][0-7]|37[0-7]?|[4-7][0-7]?|.)</string>
                    <key>name</key>
                    <string>constant.character.escape.tm1</string>
                </dict>
            </array>
        </dict>
        <dict>
            <key>captures</key>
            <dict>
                <key>1</key>
                <dict>
                    <key>name</key>
                    <string>punctuation.definition.comment.tm1</string>
                </dict>
            </dict>
            <key>match</key>
            <string>(#).*$\n?</string>
            <key>name</key>
            <string>comment.line.double-slash.tm1</string>
        </dict>
        <dict>
            <key>match</key>
            <string>\;</string>
            <key>name</key>
            <string>punctuation.terminator.statement.tm1</string>
        </dict>
    </array>
    <key>scopeName</key>
    <string>source.tm1</string>
</dict>
</plist>

如果您还没有使用 PackageDev 及其 YAML-tmLanguage 格式进行语言开发,您 真的 应该是。以下是您发布的所有 XML 的样子:

# [PackageDev] target_format: plist, ext: tmLanguage
name: TM1
scopeName: source.tm1
fileTypes: [txt]

patterns:
- name: keyword.operator.tm1
  match: (=|<>|>|<|@=|@<>|@>|@<)

- name: keyword.control.tm1
  match: (?i)\b(If|While|Else|ElseIf|End|EndIf)\b

- name: string.quoted.single.tm1
  begin: ''''
  beginCaptures:
    '0': {name: punctuation.definition.string.begin.tm1}
  end: ''''
  endCaptures:
    '0': {name: punctuation.definition.string.end.tm1}
  patterns:
  - name: constant.character.escape.tm1
    match: \(x\h{2}|[0-2][0-7]{,2}|3[0-6][0-7]?|37[0-7]?|[4-7][0-7]?|.)

- name: string.quoted.double.tm1
  begin: '"'
  beginCaptures:
    '0': {name: punctuation.definition.string.begin.tm1}
  end: '"'
  endCaptures:
    '0': {name: punctuation.definition.string.end.tm1}
  patterns:
  - name: constant.character.escape.tm1
    match: \(x\h{2}|[0-2][0-7]{,2}|3[0-6][0-7]|37[0-7]?|[4-7][0-7]?|.)

- name: comment.line.double-slash.tm1
  match: (#).*$\n?
  captures:
    '1': {name: punctuation.definition.comment.tm1}

- name: punctuation.terminator.statement.tm1
  match: \;

一旦我以这种格式输入它,问题就立刻跳出来了:它与你的两个 string.quoted 部分的 constant.character.escape.tm1 部分有关。正则表达式:

\(x\h{2}|[0-2][0-7]{,2}|3[0-6][0-7]|37[0-7]?|[4-7][0-7]?|.)

匹配一个反斜杠后跟一系列选项(由 | 字符分隔),最后一个是 .。这就是导致您出现问题的原因 - . 匹配任何字符(换行符除外),因此您的正则表达式实质上是在说 "match a backslash followed by any of these combinations, including a backslash followed by any single character." 摆脱 |.在每个正则表达式的末尾,你会没事的。

附带说明一下,您确实应该使用存储库来保存 constant.character.escape.tm1 范围:

- name: string.quoted.double.tm1
  begin: '"'
  beginCaptures:
    '0': {name: punctuation.definition.string.begin.tm1}
  end: '"'
  endCaptures:
    '0': {name: punctuation.definition.string.end.tm1}
  patterns:
  - include: '#constant_character_escape'

  (...)

repository:
  constant_character_escape:
    name: constant.character.escape.tm1
    match: \(x\h{2}|[0-2][0-7]{,2}|3[0-6][0-7]|37[0-7]?|[4-7][0-7]?)

repository 部分应该(我相信)在语言定义的末尾,因为这是我见过的唯一地方。使用此模式,您基本上可以将定义分成块并分配它们 "variable names",然后在需要的地方分配它们 include