Sublime Forum

Syntax Embedding with beginning match also part of the embedded syntax

#1

I am trying to write a sublime-syntax to highlight a language that looks roughly like:

{
<embedded Go syntax>
}

Something <- More Somethings {
<embedded Go Syntax
}

Essentially almost anything between outer {} should be treated as Go syntax (except for brackets within string literals and the like which I can already handle). The problem I seem to be running into is that the Go language will already use the } character in many places. I was trying to use the embed item in the match on the open bracket with an escape of the closing bracket like so:

contexts:
  main:
  - match: "{"
    embed: scope:source.go
    escape: "}"

However this stops syntax highlighting after the first } seen within the block of Go code which is almost always the wrong thing to do. I saw the bracket matching example in the docs but I am not sure how to use that while embedding a syntax.

Essentially I need to stop treating source as Go code when I see the matching close bracket (not just any closing bracket). Is this possible?

For reference here is an example file I am trying to highlight: https://github.com/mna/pigeon/blob/master/examples/calculator/calculator.peg

0 Likes

#2

Can you use indentation for Go syntax?
e.g.:

external syntax {
    go {
        // more go
    }
}
0 Likes

#3

Indenting isn’t really an option for me.

0 Likes

#4

any extra string after } go-block closer that can exactly differ it from go-brackets?

0 Likes

#5

Embed won’t work - it will pop out at the first close brace. You need them balanced, so just create a context with a rule to match the close brace and then include go as the next item in the context.

0 Likes

#6

Do you have an example of how to do this by any chance. I was experimenting with bracket balancing using more contexts but I wasn’t getting anything to work.

0 Likes

#7

In general, something like this should work. I haven’t tested it with the Go syntax to ensure it works.

 - match: '{'
   scope: your.scope
   push:
    - match: '}'
      scope: your.scope
      pop: true
    - include: Go.sublime-syntax
0 Likes

#8

For some reason including Go.sublime-syntax did not work however using -include: scope:source.go did. The basics of what I got working are:

file_extensions:
  - peg
scope: source.pigeon

contexts:
  main:
    - match: '{'
      scope: source.pigeon
      push: go-code

  go-code:
    - clear_scopes: true
    - match: '{'
      scope: source.go
      push: go-code
    - match: '}'
      pop: true
    - include: scope:source.go

With this it seems to handle matching brackets properly now.

Thanks for all the help.

0 Likes