Sublime Forum

How to define custom syntax for a set structure with a specific number of elements?

#1

For context, here’s the syntax I’m trying to match:

<1,2,3>
< 1.0 , 2.0 , 3.0 >
<var, var, var>
<(float)var, (var > 0), 0>

As you can see, the structure always has 3 elements, surrounded by angle-brackets and separated by commas. Each element can be a literal value or an expression. I can handle the expression part, but for some reason I’m really struggling with specifying “element, comma, element, comma, element.”

The simplest match I’ve managed to make is the following, which correctly matches only the full pattern regardless of newlines or other whitespace. It doesn’t (and shouldn’t) match something like <1,2> or <1,2,>.

vector:
  - match: \<
    scope: constant.numeric.complex.vector.begin.lsl
    set: vectorBody

vectorBody:
  - meta_scope: constant.numeric.complex.vector.lsl
  - match: ''
    push:
      - match: '[0-9]+'
        scope: arg-1.lsl
        push:
          - match: ','
            scope: comma-1.lsl
            push:
              - match: '[0-9]+'
                scope: arg-2.lsl
                push:
                  - match: ','
                    scope: comma-2.lsl
                    push:
                      - match: '[0-9]+'
                        scope: arg-3.lsl
                        push:
                          - match: '>'
                            scope: constant.numeric.complex.vector.end.lsl
                            pop: 7

I have defined an expression context which could be used here, I just haven’t been able to figure out the proper way to do that. (The expression context is used in other places already.) It doesn’t seem like include can be combined with push (as in “if include matches, then push”) and you can’t match the context directly.

0 Likes