public IterationElementTypeParser(IterationElementType elementType) {
super(elementType);
}
public ParseResult parse(@NotNull ParsePathNode parentNode, boolean optional, int depth, ParserContext context) throws ParseException {
ParserBuilder builder = context.getBuilder();
logBegin(builder, optional, depth);
ParsePathNode node = createParseNode(parentNode, builder.getCurrentOffset());
PsiBuilder.Marker marker = builder.mark(node);
IterationElementType elementType = getElementType();
ElementType iteratedElementType = elementType.getIteratedElementType();
TokenElementType[] separatorTokens = elementType.getSeparatorTokens();
int elementCounter = 0;
int matchedTokens = 0;
//TokenType tokenType = (TokenType) builder.getTokenType();
// check if the token objectType can be part of this iteration
//if (isDummyToken(builder.getTokenText()) || isSuppressibleReservedWord(tokenType, node) || iteratedElementType.containsToken(tokenType)) {
ParseResult result = iteratedElementType.getParser().parse(node, optional, depth + 1, context);
// check first iteration element
if (result.isMatch()) {
if (node.isRecursive(node.getStartOffset())) {
return stepOut(marker, depth, ParseResultType.FULL_MATCH, matchedTokens, node, context);
}
while (true) {
elementCounter++;
// check separator
// if not matched just step out
if (separatorTokens != null) {
for (TokenElementType separatorToken : separatorTokens) {
result = separatorToken.getParser().parse(node, false, depth + 1, context);
matchedTokens = matchedTokens + result.getMatchedTokens();
if (result.isMatch()) break;
}
if (result.isNoMatch()) {
// if NO_MATCH, no additional separator found, hence then iteration should exit with MATCH
ParseResultType resultType = matchesElementsCount(elementCounter) ?
ParseResultType.FULL_MATCH :
ParseResultType.PARTIAL_MATCH;
return stepOut(marker, depth, resultType, matchedTokens, node, context);
} else {
node.setCurrentOffset(builder.getCurrentOffset());
}
}
// check consecutive iterated element
// if not matched, step out with error