0

我为一种专门的语言创建了一个 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>
4

1 回答 1

0

如果您还没有使用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: \;

一旦我把它放在这种格式中,问题马上就跳出来了:它与constant.character.escape.tm1你的两个string.quoted部分的部分有关。正则表达式:

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

匹配一个反斜杠后跟一堆选项(由|字符分隔),最后一个是.. 这就是导致您的问题的原因 -.匹配任何字符(换行符除外),因此您的正则表达式本质上是说“匹配反斜杠后跟任何这些组合,包括反斜杠后跟任何单个字符。” |.只需在每个正则表达式的末尾去掉,就可以了。

作为旁注,您确实应该使用存储库来保存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部分应该(我相信)位于语言定义的末尾,因为这是我见过的唯一地方。使用这种模式,您基本上可以将定义分解为块并为它们分配“变量名”,然后include在需要的地方分配它们。

于 2015-11-04T22:31:47.337 回答