Syntax: Multiple Scopes with Same `starts-with`

A syntax question for the brain trust…

Rust has slightly different syntax for declaring when a method is for conforming to a trait (traits work like interfaces). Here is a standard impl block for declaring struct methods:

impl MyStruct {
    // method declarations...
    fn someMethod(&self) {}
}

And here is an impl block for declaring methods that conform to a trait (interface):

impl SomeTrait for MyStruct {
    // methods that satisfy trait
}

Because the structs/types and traits in the declarations above can specify lifetimes and generics (and generics can be nested), I’m attempting to define the scopes with <starts-with>\b(impl)\b</starts-with> and then use anchored subscopes for types, lifetimes, generics, and the for keyword.

The problem with my current approach is that the parser goes with whatever scope (with the same starts-with) is defined first, even if for example I have the for keyword as a required subscope. It doesn’t hit the eject button and try something else, rather it leaves the subtree empty.

So what I’m essentially asking is am I able to have different scopes with the same starts-with, or do I need to create some big gnarly regexes for these scopes?

I tend to forget about lookaheads. I updated my starts-with to scan ahead for the for keyword before a curly brace:

<starts-with>\b(impl)\b(?=[^\{]*\bfor\b)</starts-with>
1 Like