dlang-community / Pegged

A Parsing Expression Grammar (PEG) module, using the D programming language.
534 stars 66 forks source link

GrammarTester enhancment proposal #337

Closed ricardaxel closed 1 year ago

ricardaxel commented 1 year ago

Hello,

The GrammarTester is very usefull to validate grammars, but it sometimes leads to very long tests.

For example it is quite complicated to test C grammar , since a single a = 1 expression is represented as shown below, which is pretty long.

Expression->AssignmentExpression->                                                                                                
{                                                                                                                                                                 
    UnaryExpression->PostfixExpression->PrimaryExpression->Identifier
    AssignmentOperator
    AssignmentExpression->ConditionalExpression->LogicalORExpression->LogicalANDExpression->
        InclusiveORExpression->ExclusiveORExpression->ANDExpression->EqualityExpression->
        RelationalExpression->ShiftExpression->AdditiveExpression->MultiplicativeExpression->
        UnaryExpression->PostfixExpression->PrimaryExpression->IntegerLiteral 
}    

Would you be ok to extend testing grammar to shorten some tests, by adding a {...} directive that would skip some parts of the tests.

This would give something like this :

tester.assertSimilar(`a = 3`, ` Expression->AssignmentExpression->{...}`); // OK

// Also OK
tester.assertSimilar(`a = 3`, ` 
    Expression->AssignmentExpression->
   {
        UnaryExpression->{...}
        AssignmentOperator
        AssignmentExpression->{...}
    }`); 

If you are ok with this proposal, I could make a PR soon

veelo commented 1 year ago

I must confess that I have never used GrammarTester, and don't know how it works. The wiki warns that the design may change in the future, so I don't think there is a problem changing it. If I understand you correctly, your proposal would be backwards compatible anyways, right?

Sounds like a good idea, and I look forward to your PR. After it is merged, I kindly request that you extend the existing wiki page to document your changes. And if you could, it would be nice have the CI test the grammar tester, to make sure nothing breaks in the future.

ricardaxel commented 1 year ago

If I understand you correctly, your proposal would be backwards compatible anyways, right?

I confirm. This is just an extension.

And if you could, it would be nice have the CI test the grammar tester, to make sure nothing breaks in the future.

I also fixed C example since it didn't build anymore, and add test for it using GrammarTester. You can check usage of it here

veelo commented 1 year ago

Could you please extend https://github.com/PhilippeSigaud/Pegged/wiki/Grammar-Testing with your contribution?

ricardaxel commented 1 year ago

Done, I let you check if it's all good

veelo commented 1 year ago

Looks great, thanks.