Sublime Forum

Apply imported syntax on sections only

#1

In my syntax highlighter for language A, I’m trying to add syntax highlighting for language B inside specific blocks. Since HTML can include CSS or JavaScript, I took a look at how this is done in HTML.sublime-syntax:

- match: '(?:^\s+)?(<)((?i:style))\b(?![^>]*/>)'
  captures:
    1: punctuation.definition.tag.begin.html
    2: entity.name.tag.style.html
  push:
    - match: (?i)(</)(style)(>)
      captures:
        1: punctuation.definition.tag.begin.html
        2: entity.name.tag.style.html
        3: punctuation.definition.tag.end.html
      pop: true
    - include: tag-stuff
    - match: '>'
      scope: punctuation.definition.tag.end.html
      push:
        - meta_content_scope: source.css.embedded.html
        - include: 'scope:source.css'
      with_prototype:
        - match: (?i)(?=</style)
          pop: true

In my case, language A is nsL Assembler and language B is NSIS. nsL Assembler can not only be transpiled into NSIS, but also contain blocks of vanilla NSIS.

Example:

MessageBox("This is nsL Assembler");

#nsis
    MessageBox MB_OK "This is vanilla NSIS"
#nsisend

Here’s my attempt at implementing this in a trial & error fashion:

- match: '\b(#nsis)\b'
  captures:
    1: keyword.other.nsl
  push:
    - match: '\b(#nsisend)\b'
      captures:
        1: keyword.other.nsl
      pop: true
      push:
        - meta_content_scope: source.nsis.embedded.nsl
        - include: 'scope:source.nsis'
      with_prototype:
        - match: '(?i)(?=#nsisend)'
          pop: true

This results in the following error:

Can someone please help me figuring this out?

0 Likes

#2

you have a push and a pop for the same match… maybe you meant to use the following, so that the with_prototype is associated with the correct push:

- match: '\b(#nsis)\b'
  captures:
    1: keyword.other.nsl
  push:
    - meta_content_scope: source.nsis.embedded.nsl
    - match: '\b(#nsisend)\b'
      captures:
        1: keyword.other.nsl
      pop: true
    - include: 'scope:source.nsis'
  with_prototype:
    - match: '(?i)(?=#nsisend)'
      pop: true
1 Like

#3

I’m far from understanding the with_prototype bit, but that was great help – thanks! I had to adjust the my matches and ended up with this:

- match: '^\s*(#nsis)\b'
  captures:
    1: keyword.other.nsl
  push:
    - meta_content_scope: source.nsis.embedded.nsl
    - match: '^\s*(#nsisend)\b'
      captures:
        1: keyword.other.nsl
      pop: true
    - include: 'scope:source.nsis'
  with_prototype:
    - match: '(?i)(?=#nsisend)'
      pop: true
0 Likes