private static IndexToken ScanForMatchingBrace(IndexTokenNode tokenNode) { TokenType matchingType = tokenNode.IndexToken.Token.Type.MatchingBraceType(); IndexTokenNode candidateToken = tokenNode; bool matchForward = tokenNode.IndexToken.Token.Type.IsBraceStart(); int braceCounter = 0; while (candidateToken != null) { if (candidateToken.IndexToken.Token.Type == matchingType) braceCounter--; if (candidateToken.IndexToken.Token.Type == tokenNode.IndexToken.Token.Type) braceCounter++; if (braceCounter == 0) return candidateToken.IndexToken; candidateToken = matchForward ? candidateToken.Next() : candidateToken.Previous(); } return null; }
private static MatchingBracePair FindMatchingBracePair(IndexTokenNode tokenNode) { IndexToken matchingBrace = ScanForMatchingBrace(tokenNode); if (matchingBrace == null) return new MatchingBracePair(tokenNode.IndexToken, null); return new MatchingBracePair(tokenNode.IndexToken, matchingBrace); }