Sublime Forum

[Sublime Syntax] Allow adding a scope without consuming

#1

Say I have the following syntax definition. I want to make it so the { is tagged with invalid.illegal so it’s obvious that the closing ) is missing. I also want to make it so the block context matches and continues to work like normal.

main:
  - include: parens
  - include: block
block:
  - match: \{
    scope: punctuation.section.block.begin
    push:
      - match: \}
        scope: punctuation.section.block.end
        pop: true
parens:
  - match: \(
    scope: punctuation.section.parens.begin
    push:
      - match: \)
        scope: punctuation.section.parens.end
        pop: true
      - match: \{
        scope: invalid.illegal.stray-terminator-end
        pop: true
        consume: false # <-- New keyword that will allow applying a scope but not consume it

Example:

if (true {
//       ^ invalid.illegal punctuation.section.block.begin.java
}

Though thinking about it more I guess I can work around it by reworking the scopes:

main:
  - include: parens
  - include: block
block-body:
  - match: \}
    scope: punctuation.section.block.end
    pop: true
block:
  - match: \{
    scope: punctuation.section.block.begin
    push: block-body
parens:
  - match: \(
    scope: punctuation.section.parens.begin
    push:
      - match: \)
        scope: punctuation.section.parens.end
        pop: true
      - match: \{
        scope: invalid.illegal.stray-terminator-end punctuation.section.block.begin
        set: block-body
0 Likes

#2

Implementation of non-consuming matches would make the lexer codebase even trickier to follow, and make it harder to detect non-terminating loops. I think your revised contexts are probably the best solution, and more or less what I was going to recommend.

1 Like