datalust / superpower

A C# parser construction toolkit with high-quality error reporting
Apache License 2.0
1.05k stars 98 forks source link

[Question] How to add parsing logic ? #119

Closed Maskime closed 3 years ago

Maskime commented 3 years ago

Hi everyone,

I've written a tokenizer/parser for a domain programing language I'm working on. This language is a "C# like" language (way more lighter) and should be able to handle overloading the same way C# does:

vod AFunction();
vod AFunction(dbl paramName);
// This should be invalid
bol AFunction(dbl paramName);

The last overload declaration is invalid as it declares the same function with the same param but with a different return type.

As far as I could see, there is no actual way to enforce this directly from Superpower text parsers. The best I could come with was to let Superpower handle the basic tokenizing/parsing and after that apply those kinds of rules on the parsing result.

But then I'm wondering how will I construct the error message like : Line 3, column 1 : Wrong overload definition, same name, same params but different return type.

My idea was to link my AST objects to their Span counterpart from the tokenization to the superpower parsing and then to my second parsing, but this seems a lot of work, is there any other way to do it ?

In any case, thanks for the hardwork !

z93blom commented 3 years ago

This type of detection is not something that should be done while parsing, as you've already guessed. The text has the correct syntax, but it cannot describe a program since there are "logical" errors in the program.

Once you have your AST, then you can check those rules. If you want to point back to where the error is in the source code you would need to carry that information with you into the AST to be able to look it up later and supply the kind of error message you are looking for.

Maskime commented 3 years ago

Arf, no shortcut to glory then :( I was hopping that someone would tell me something like : "Don't you know about this feature ?" and this comment would have solved all my problems :D

Thanks for the feedback.