You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Nicolas Pillot <ni...@gmail.com> on 2016/11/26 08:32:33 UTC

Parentheses required for some piece of dsl code

Hi

I am building a DSL and everythings is working smoothly, except that i
need to add parentheses to some constructs even if they are top-level
statements, otherwise startup fails with
MultipleCompilationErrorsException.

Here is the sample dsl :

test1 592 // ok
test2 { n 723 } // ok
test3 772, { n 60 } // ok
test4 ({ n 121 }, 1) // ok  with paren
test5 651, { n 122 }, 212
test6 ({ n 349 }, { t "eufysalnvlw" }) // ok with paren
test7 806, { n 757 }, { t "guffsvrtm" } // ok
test8 ({ n 581 }, 561, { t "kjxer" }) // ok with paren

// fails with: expecting EOF, found ',' @ line 4, column 16.
// test4 { n 121 }, 1

// fails with: expecting EOF, found ',' @ line 11, column 16
// test6 { n 349 }, { t "eufysalnvlw" }

// fails with: expecting EOF, found ',' @ line 18, column 16.
// test8 { n 581 }, 561, { t "kjxer" }

The spec class looks like this (btw, class is @TypeChecked and each
arg has @DelegatesTo)

abstract class MasterScriptSpec extends Script {
  void test1(int num1) {}
  void test2(Closure cl1) {}
  void test3(int num1, Closure cl2) {}
  void test4(Closure cl1, int num2) {}
  void test5(int num1, Closure cl2, int num3) {}
  void test6(Closure cl1, Closure cl2) {}
  void test7(int num1, Closure cl2, Closure cl3) {}
  void test8(Closure cl1, int num2, Closure cl3) {}
}

I run the whole thing with : groovy -cp class/path -b MasterScriptSpec
sample.dsl

Question is : I cannot figure out what rule or what reason "forces" me
to add parentheses to test 4/6/8 ... and i would love to know !

Thanks in advance for your insights

Nicolas

Re: Parentheses required for some piece of dsl code

Posted by Nicolas Pillot <ni...@gmail.com>.
Thanks a lot for the explanation

[...]
> So imho the parser simply applies the wrong rule
> first and the problems you see are a result of
> priorities... or missing lookahead

Re: Parentheses required for some piece of dsl code

Posted by Jochen Theodorou <bl...@gmx.org>.
On 26.11.2016 09:32, Nicolas Pillot wrote:
[...]
> // fails with: expecting EOF, found ',' @ line 4, column 16.
> // test4 { n 121 }, 1
>
> // fails with: expecting EOF, found ',' @ line 11, column 16
> // test6 { n 349 }, { t "eufysalnvlw" }
>
> // fails with: expecting EOF, found ',' @ line 18, column 16.
> // test8 { n 581 }, 561, { t "kjxer" }
[...]
> Question is : I cannot figure out what rule or what reason "forces" me
> to add parentheses to test 4/6/8 ... and i would love to know !

This is actually not so easy to explain, since this is a side-effect of 
the way the grammar for the parser is written... and they are 
potentially bugs.

if you have foo {}, then this is seen as a complete method call, the 
parser simply does not expect further arguments after the {} if you wrap 
the whole thing in parens, you actually have a different rule applying, 
which explains why it works there. But that is actually not precise 
enough. we have also a different rule for foo {} and foo 1, {}. So imho 
the parser simply applies the wrong rule first and the problems you see 
are a result of priorities... or missing lookahead

bye Jochen