Closed SerafimArts closed 6 years ago
Try combining with optional()
. I guess this would work as you'd expect:
$ts = TokenStream::fromSourceWithoutOpenTag(' {a b c} ');
$parser = chain(
token('{'),
ls(
label(),
optional(token(','))
),
token('}')
);
$ast = $parser->parse($ts);
var_dump($ast->unwrap());
var_dump($ts->index());
Outputs:
[
Token { +0: "T_STRING(a)"},
Token { +0: "T_STRING(b)"},
Token { +0: "T_STRING(c)"},
]
object(NodeEnd) {}
And perhaps this is more elegant depending on how you want the final AST to be:
$parser = between(
token('{'),
ls(
label(),
optional(token(','))
),
token('}')
);
I'm closing this one but, please, reopen if necessary.
:wink:
It's not entirely clear how to make a comma optional in the
list
method.Like this:
Grammar:
Error: