I'm playing with PEG.js and reading and in the Nathans University i found a good "explain" how to build my own languaje but i'm stuck with this step
i dont understand the primary
can explain me please
start =
comma
comma =
left: additive "," right:comma
{ return {tag: ",", left:left, right:right}; }
/ additive
additive =
left:multiplicative "+" right:additive
{ return {tag: "+", left:left, right:right}; }
/ multiplicative
multiplicative =
left:primary "*" right:multiplicative
{ return {tag: "*", left:left, right:right}; }
/ primary
primary =
integer
/ "(" comma:comma ")" # Can explain me why is necesary "(" and ")"
{ return comma; }
integer =
digits:[0-9]+
{ return parseInt(digits.join(""), 10); }
test
var parse = wrapExceptions(PEG.buildParser(answer).parse);
assert_eq(parse("1+2"),
{tag:"+", left:1, right:2},
"parse 1+2");
assert_eq(parse("1+2*3"),
{tag:"+", left:1, right:{tag:"*", left:2, right:3}},
"parse 1+2*3");
assert_eq(parse("1,2"),
{tag:",", left:1, right:2},
"parse 1,2");
assert_eq(parse("1,2+3"),
{tag:",", left:1, right:{tag:"+", left:2, right:3}},
"parse 1,2+3");
assert_eq(parse("1*2,3"),
{tag:",", left:{tag:"*", left:1, right:2}, right:3},
"parse 1*2,3");
my question is why is necesary the / "(" comma:comma ")"
if the entry does not have parenthesis, if remove that line the last test fails