uqbar-project / wollok

Wollok Programming Language
GNU General Public License v3.0
60 stars 16 forks source link

Error when declare var on other line: Enhance text #663

Closed asanzo closed 7 years ago

asanzo commented 8 years ago

When you declare a var in an incorrect place (for example at the bottom of a class) it says "} missing" or something like that. It would be nice for it to say "vars should be declared at the begginning of the class"

javierfernandes commented 8 years ago

Yes, this makes sense. Just writing a "technical description" to make it easier to fix later.

This is because the rule is implemented at "grammar-level". Wollok's xtext grammar defines that a class/object must first have all variables, then methods, etc. So when writing a var in between methods the parser fails with that generic error.

I'm not sure it would be possible to fix this at parser level, or even customize the message. The easiest solution is to:

    members += ClassMember

ClassMember: Variable | Constructor | Method

And then have a static check rule (Validator method) that will check that all variables are declared first and if not then fail with the custom message Alf is mentioning.

The side-effect of this change is that it will make the check slower. Parser-level validations are faster than validator-level. But well,.. is the price :P And we already have a lot of validator's checks so

npasserini commented 7 years ago

@javierfernandes I like your idea, but there are plenty of this kind of parser errors. So I wonder if it is possible to customize parser errors, because maybe in other cases it is not possible to solve it by just changing the grammar.

I've seen some posts in Internet that could help customizin parser error messages: https://tomkutz.wordpress.com/2013/06/13/xtext-customizing-error-messages-unordered-group/

npasserini commented 7 years ago

I think I've got a solution for this problem. Expected error reporting will be like this:

class Golondrina {
    // XPECT errors --> "Couldn't resolve reference to Referenciable 'energia'." at "energia"
    method energia() = energia

    // XPECT errors --> "You should declare variables before constructors and methods." at "var"
    var energia = 0
}