Question

Regex is out of my wheelhouse, but I'm still trying to update the existing syntax definition for PIC 18 Assembly (https://github.com/amorrow/pic-assembly-textmate-bundle) to the newer PIC 24/33 instruction set.

I'm basically just extending the keyword list. The issue is that many (but not all) of the instructions have byte variants, eg, the mov instruction has a mov.b variant. The highlighting works for the mov portion of the instruction, but omits highlighting the .b. Adding mov as well as mov.b to the keyword list didn't work, and neither did adding mov\.b.

Here's what it looks like (instruction list abbreviated for readability):

    <dict>
        <key>match</key>
        <string>\b(add|addc|mov)\b</string>
        <key>name</key>
        <string>keyword.mnemonic</string>
    </dict>

Regex is still very alien to me, and I'm unsure how to let it know that mov.b is one entire instruction.

Was it helpful?

Solution 2

Adding the prefixed instructions before the base instruction in the list fixed the issue.

Eg:

<dict>
    <key>match</key>
    <string>\b(add\.b|add|addc\.b|addc|mov\.b|mov)\b</string>
    <key>name</key>
    <string>keyword.mnemonic</string>
</dict>

OTHER TIPS

What about \b(?:add|addc|mov)(?:\.\S+)?\b? Works for things like "mov.b", "mov.b.cde", etc.

I added an optional "." followed by one or more non-whitespace characters.

As I suggest it in my comment you can use a non-capturing group (?:...) and the quantifier ? to reduce the regex engine work:

<dict>
    <key>match</key>
    <string>\b(add(?:\.b)?|addc(?:\.b)?|mov(?:\.b)?)\b</string>
    <key>name</key>
    <string>keyword.mnemonic</string>
</dict>

or with more factorization: (I added the nop and rlf mnemonics to show the level depth of parenthesis)

<dict>
    <key>match</key>
    <string>\b(nop|(?:addc?|mov)(?:\.b)?|rlf)\b</string>
    <key>name</key>
    <string>keyword.mnemonic</string>
</dict>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top