hoaproject/Ruler

Lint

dhaarbrink opened this issue · 15 comments

Hi,

I would really like to be able to lint an expression.
Ruler::interpret() seems like a candidate, but doesn't really cut it.

Something like Ruler::lint($rule):bool would be enough for my purposes. A ::getLintErrors() is optional and could be implemented later.
Examples:
lint('points > 40') > true
lint('points && true') > false

Could something like this be done?
I am also willing to build it myself, if anyone can provide me with some guidance.

Hywan commented

What do you mean by “lint”?

Basically a syntax check, see wiki, php.net and SO for example.

Hywan commented

Ok, this kind of lint :-). Only the syntax or also the semantics?

To check the syntax:

Ruler::getCompiler()->parse($rule);

To check the semantics:

Ruler::interpret($rule);

In both cases, if no exception is thrown, it means the rule is syntactically and/or semantically correct.

We definitively need to update the documentation. However, do you think we need a specific method/helper for that?

They are both not quite what is needed.

For example, the rule group && true is valid according to both methods. But there is no '&&' operator so assert() will still fail.

So what it would need to do is make sure that assert() will never fail. But I don't have any context at that moment, so I cannot simply execute assert().

Hywan commented

Operators belong to the asserter. Do you have an assert available?

What do you mean with 'Do you have an assert available'?

Hywan commented

An asserter*, sorry. If we have no asserter, we cannot know if operators are correctly used or if they at least exist.

I don't know, I just create a new \Hoa\Ruler\Ruler() (to which I have assigned any operators/functions).
For the time being I just supply a rule and context to its assert() method. It works good enough for now in this specific use case, but I won't always have a (real or synthetic) context available.

Hywan commented

Withouh any context, we are not able to check that all operators exist or are correctly used, so the “linter” will be very limited.

As far as I know the operators are not defined in the context, only the 'parameters' or 'variables' or whatever you call them. Maybe we can discuss this in a chat somewhere? Do you maybe have an irc channel?

Hywan commented

ping?

I am not working anymore on the project I needed this for.
And I don't think I will find the time nor inclination to get this built.

Hywan commented

OK :-).

Hywan commented

Thanks for your question. We will still be happy to help in the future!