Is tree-sitter good enough?
blog.jez.io
blog.jez.io
As it currently works, the interpretation as a method definition is not even considered, because the grammar doesn’t have method definitions in that syntactic context. But that’s not how human pattern recognition works: we tend to recognize a method definition regardless of the context. So maybe we could build a parser that would try do determine all possible valid interpretations of all token subsequences in parallel (like a nondeterministic generalized parser), outputting errors where neighboring /overlapping sequences clash which each other (“this starts out like a method body but then transitions into a method definition”).
Example from a sourcegraph docs generation project:
https://github.com/sourcegraph/doctree/blob/07a2d593fe9855be...
https://github.com/sourcegraph/doctree/blob/07a2d593fe9855be...