Skip to content

How to implement C++ style templates using Langium #921

Answered by msujew
luan-xiaokun asked this question in Q&A
Discussion options

You must be logged in to vote

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. The LESS_THAN is there so that we manually change the ordering of terminal in the lexer. The GENERICS_OPEN needs to have the opportunity to be lexed first, with LESS_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

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@luan-xiaokun
Comment options

@msujew
Comment options

@luan-xiaokun
Comment options

@agacek
Comment options

@msujew
Comment options

Answer selected by luan-xiaokun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants