int matchedTokens = 0;
boolean isWrapped = false;
// first try to parse the wrapped element directly, for supporting wrapped elements nesting
if (isWrappingOptional) {
ParseResult wrappedResult = wrappedElement.getParser().parse(node, optional, depth + 1, context);
if (wrappedResult.isMatch()) {
matchedTokens = matchedTokens + wrappedResult.getMatchedTokens();
return stepOut(marker, depth, wrappedResult.getType(), matchedTokens, node, context);
} else {
builder.markerRollbackTo(marker, null);
marker = builder.mark(null);
}
}
// parse begin token
ParseResult beginTokenResult = beginTokenElement.getParser().parse(node, optional, depth + 1, context);
if (beginTokenResult.isMatch()) {
isWrapped = true;
matchedTokens++;
}
if (beginTokenResult.isMatch() || isWrappingOptional) {
ParseResult wrappedResult = wrappedElement.getParser().parse(node, false, depth -1, context);
matchedTokens = matchedTokens + wrappedResult.getMatchedTokens();
if (isWrapped) {
// check the end element => exit with partial match if not available
ParseResult endTokenResult = endTokenElement.getParser().parse(node, false, depth -1, context);
if (endTokenResult.isMatch()) {
matchedTokens++;
return stepOut(marker, depth, ParseResultType.FULL_MATCH, matchedTokens, node, context);
} else {
return stepOut(marker, depth, wrappedResult.getType(), matchedTokens, node, context);
}