overturetool / overture

The Overture Tool
http://overturetool.org
GNU General Public License v3.0
48 stars 25 forks source link

Quick Interpreter should check for rubbish after expressions #617

Closed nickbattle closed 7 years ago

nickbattle commented 7 years ago

The Quick Interpreter window reads a single expression in the input window, but the parser is not checking whether there are extra tokens after the expression. That allows you to say things like:

exists p in set {1,2,3} & p=1 & stuff

Note that there are two "&" clauses. The parse stops after p=1, and the rest is ignored. But the whole line is printed as the expression evaluated:

exists p in set {1,2,3} & p=1 & stuff = true

Ideally this ought to raise an error, perhaps "Tokens found after expression"?

peterwvj commented 7 years ago

Thanks for reporting, Nick!

It all boils down to the Quick interpreter eventually calling Moduleinterpreter.parseExpression which is defined as:

@Override
protected PExp parseExpression(String line, String module) throws Exception
{
    LexTokenReader ltr = new LexTokenReader(line, Dialect.VDM_SL, Console.charset);
    ExpressionReader reader = new ExpressionReader(ltr);
    reader.setCurrentModule(getDefaultName());
    return reader.readExpression();
}

When this method is passed the arguments "exists p in set {1,2,3} & p=1 & stuff" and "DEFAULT" the reader simply ignores the next & token. So I'm wondering why ExpressionReader does not throw an exception?

nickbattle commented 7 years ago

Absolutely right PJ :) The fix is in that very method (and its twin in ClassInterpreter, so that the command line also works consistently). Just pushed to ncb/development. Maybe you could give it a try?

The example above now produces the following in the Quick Interpreter:

exists p in set {1,2,3} & p=1 & stuff = Error 2330: Tokens found after expression at & 
peterwvj commented 7 years ago

I tried it out using different examples and the quick interpreter is correctly reporting parse/type/evaluation errors now. However, the fix also detected some parse errors in some of the interpreter's test input files, which is quite nice :). See 0607bee3d9092ca7fcca62404939fb724a4395f3 for details.

Anyway, I'm happy to consider this issue solved.

nickbattle commented 7 years ago

Ah yes, sorry. I had to fix those same three tests in my copy of the old CSK suite. I should have re-run the Overture test suite before committing (but I didn't think the fix would affect anything!).

peterwvj commented 7 years ago

No worries. To be honest I was surprised to find parse errors in the interpreter tests too :)