Distinguishing between type aliases and variable names in templates #741
-
Problem to be solvedC++ style template is supported in my language, the problem is that the template argument can be either a type or an expression, but parse rules for type and expression overlap, i.e. for an identifier we cannot know it is a type alias or a variable / function name, unless we have some semantic information. Currently, writing something like Existing approachesSuch problem can be solved by the lexer hack method, which requires a context-free lexer, information from the semantic table is fed back to the lexer, but I don't think this is a proper solution for Langium since this approach may require hacking Chevrotain if I understand it correctly. A second approach mentioned in the wiki is to allow multiple attempts to parse the tokens, and when the parser fails it backtracks to try the alternative rules. I'm not sure if this method is applicable to Langium, any comments on this approach are welcome. The last method does not differentiate between type names and variable names at the parsing time, whether the identifier is a type or an expression is determined by the semantic analysis that follows, and this approach is adopted by Clang. As far as I can understand, I think this approach suites the Langium framework the best, but it requries creating a new set of parse rules to mix expressions and types, and these rules are only used in the template related parse rules. I haven't tried it because I'm still figuring how to write such rules. The questionWhich solution do you think is the most appropriate in the Langium framework? Or is there a better solution? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @luan-xiaokun,
I believe so as well. While we recently added support for custom lexers (#721), I'm worried that the lexer hack won't turn out as expected. The Clang approach is definitely the one that is best suited for Langium, and the implementation is fairly straightforward (although verbose). I imagine your type syntax and expression syntax to use something like this for referencing:
Given that Langium informs you of the common
This basically moves |
Beta Was this translation helpful? Give feedback.
Hey @luan-xiaokun,
I believe so as well. While we recently added support for custom lexers (#721), I'm worried that the lexer hack won't turn out as expected. The Clang approach is definitely the one that is best suited…