1 36 package org.columba.ristretto.imap.parser; 37 38 import org.columba.ristretto.parser.ParserException; 39 40 45 public class ParenthesisParser { 46 private static final int NORMAL = 0; 47 private static final int IN_QUOTES = 1; 48 49 57 public static int getClosingPos(CharSequence s, int openPos) throws ParserException { 58 int mode = NORMAL; 59 int depth = 1; 60 int idx = openPos + 1; 61 62 while( idx < s.length() ) { 63 switch( s.charAt(idx) ) { 64 case '\"' : { 65 mode ^= 1; 67 break; 68 } 69 70 case '(' : { 71 if( mode == NORMAL ) { 72 depth++; 73 } 74 break; 75 } 76 77 case ')' : { 78 if( mode == NORMAL ) { 79 depth--; 80 if( depth == 0 ) return idx; 81 } 82 break; 83 } 84 } 85 86 idx++; 87 } 88 89 throw new ParserException("No matching Parenthesis found :" + s.toString() ); 90 } 91 92 } 93 | Popular Tags |