How to implement C++ style templates using Langium #921
-
Hi guys, I've spent some time lately on parsing C++ style function template, and I'm interested in how to implement this complex feature in Langium, since the language that I'm working on also has this feature. To be more specific, the C++ style template is complex because:
The first type of ambiguity is related to the notation of the template, we can avoid it by changing its notation, such as Here are some ways that I've learned to solve the problem:
I've tried to solve the second issue before by introducing a I know that the syntaxes of TypeScript and C++ are quite different, but as far as I can see, they share the same ambiguities in terms of template function. So when I see @msujew mentioned in #895 that it's possible to implement some complex features of TypeScript in Langium, such as template strings |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Hey @luan-xiaokun, thanks for the question! I think this can be solved in theory using multiple different approaches, but I believe the simplest way of achieving this is by manipulating the lexing using positive lookahead in terminal rules. You can take a look at a working example of this in this playground link. The magic mostly lies in the The pure regex solution isn't as maintainable as I'd hoped it'd be, so I created this PR in hopes of improving that. It would allow to re-use terminals in positive/negative lookahead terminal context. Also, regarding template strings: You will need a multi-mode lexer for this. I've been meaning to write some documentation on that for some time already, so I should get to it soonish. |
Beta Was this translation helpful? Give feedback.
Hey @luan-xiaokun,
thanks for the question! I think this can be solved in theory using multiple different approaches, but I believe the simplest way of achieving this is by manipulating the lexing using positive lookahead in terminal rules. You can take a look at a working example of this in this playground link. The magic mostly lies in the
GENERICS_OPEN
terminal rule with its positive lookahead. TheLESS_THAN
is there so that we manually change the ordering of terminal in the lexer. TheGENERICS_OPEN
needs to have the opportunity to be lexed first, withLESS_THAN
being a sort of fallback for the<
character.The pure regex solution isn't as maintainable as I'd hoped it'd be, so I created …