[TxMt] Re: Composable grammars

Martin Kühl martin.kuehl at gmail.com
Thu Apr 7 09:27:15 UTC 2016

On 6 April 2016 at 22:14, Jacob Carlborg <doob at me.com> wrote:
> I've been working for quite a while with trying to rewrite the grammar for
> the D bundle to be more accurate to the official grammar. The grammar for D
> is quite complex, that in the combination with the syntax for grammars in
> TextMate doesn't allow any good ways to reuse or compose rules making it
> very difficult to describe a grammar. I know it's possible to reuse rules
> with the repository, but that seems to be mostly useful when matching with
> "begin" and "end".
> For example, this is the grammar for a function declaration from the
> official D grammar:
> FuncDeclaration:
>     StorageClasses(opt) BasicType FuncDeclarator FunctionBody
>     AutoFuncDeclaration
> AutoFuncDeclaration:
>     StorageClasses Identifier FuncDeclaratorSuffix FunctionBody
> FuncDeclarator:
>     BasicType2(opt) Identifier FuncDeclaratorSuffix
> FuncDeclaratorSuffix:
>     Parameters MemberFunctionAttributes(opt)
>     TemplateParameters Parameters MemberFunctionAttributes(opt)
> Constraint(opt)
> Each of these parts/rules of the grammar consists of several other rules,
> many levels deep.
> It would be really nice if the TextMate grammar syntax allowed, somehow, to
> define rules, or parts of a rule, which the other rules can be composed of,
> similar to above.
> Or is there a way to already do something similar with the current syntax?

I tried to approximate something like this with my Maude grammar[1].
It's been a while, but I believe the basic idea was to split scopes into
parts using lookaheads and -behinds and to include specific rules there.

Take #equation as an example: An equation begins with `eq` and ends with
`.`, but in between I can differentiate the left hand side from the
right hand side using (entirely too many) lookarounds.

Of course this only works because I can use `==` as a kind of anchor in
between, I'm assuming most rules of the grammar you quoted above don't
include any meaningful symbols but are mostly one (or, worse, possibly
several) words.
So this approach probably won't help you that much. It's just as far as I got.


[1]: https://github.com/mkhl/maude.tmbundle/blob/master/Syntaxes/Maude.tmLanguage

More information about the textmate mailing list