Sublime Forum

Syntax: special value to push the current context

#1

Imagine a DSL with a set of keywords, such that { after akeyworrd opens a specific kind of block, Thus, we would have

location { //this is a location 
} 
character { //this is a character block 
}

Additionally, a { NOT preceded by a keyword, would open a nested block of the same type.

location { //this is a location block 
  { //this is a location block inside a location block 
  }
} 
character { //this is a character block 
  { //this is a character block inside a character block 
  }
}

location { //this is a location { // and this is a location inside a location } }

/Now, imagine that each keyword-block pushes a new context with wildly different rules (the script valid inside a location block is disjoint from the script valid in a character block. And it’s relevant to me to be able to know the level of nesting for plugin functionality.

Now, this could be very easily achieved if we could say:

anonymous_block: #this context will always be included, never pushed or set.
- match: (?<!{{keyword}}){'
  scope: punctuation.section.braces
  push: ITSELF

And that the parser would substitute for ITSELF whatever the context we’re in: when it’s included in a character context, character would be substituted for ITSELF, when in location, location, and so on, which obviously is less maintainable and scales worse with the complexity of the DSL…

instead, now I have to have a family of quasi-identical contexts, such as

anonymous_block_inside_character: #this context will always be included, never pushed or set.
- match: (?<!{{keyword}}){'
  scope: punctuation.section.braces
  push: character

anonymous_block_inside_province: #this context will always be included, never pushed or set.
- match: '(?<!{{keyword}}){'
  scope: punctuation.section.braces
  push: province

#so on and so forth, one for each keyword.

If I’m not mistaken, this is a functionality present in the tmLanguage format ($self U think is the sintax?). It’d be really nice to have it ported to sublime-syntax.

0 Likes

#2

I’m not sure that this situation is common enough to justify new functionality given that it doesn’t add expressive power.

If you run into this very often, it might be worth trying YAML Macros. It’s quite useful for simplifying repeated constructs.

1 Like

#3

Also, please try to avoid lookbehinds and instead restructure the matches to push contexts when the keyword is encountered or include the keyword in the match. They are significantly slower.

0 Likes