public static Expression compile ( String exp ) { if ( Miscellaneous . isEmpty ( exp ) || exp . equals ( "." ) ) { exp = "$" ; } Queue < String > queue = new LinkedList < String > ( ) ; List < String > tokens = parseTokens ( exp ) ; boolean isInBracket = false ; int numInBracket = 0 ; StringBuilder sb = new StringBuilder ( ) ; for ( int i = 0 ; i < tokens . size ( ) ; i ++ ) { String token = tokens . get ( i ) ; if ( token . equals ( "[" ) ) { if ( isInBracket ) { throw new IllegalArgumentException ( "Error parsing expression '" + exp + "': Nested [ found" ) ; } isInBracket = true ; numInBracket = 0 ; sb . append ( token ) ; } else if ( token . equals ( "]" ) ) { if ( ! isInBracket ) { throw new IllegalArgumentException ( "Error parsing expression '" + exp + "': Unbalanced ] found" ) ; } isInBracket = false ; sb . append ( token ) ; queue . add ( sb . toString ( ) ) ; sb = new StringBuilder ( ) ; } else { if ( isInBracket ) { if ( numInBracket > 0 ) { throw new IllegalArgumentException ( "Error parsing expression '" + exp + "': Multiple tokens found inside a bracket" ) ; } sb . append ( token ) ; numInBracket ++ ; } else { queue . add ( token ) ; } } if ( i == tokens . size ( ) - 1 ) { if ( isInBracket ) { throw new IllegalArgumentException ( "Error parsing expression '" + exp + "': Unbalanced [ found" ) ; } } } return new Expression ( queue , exp ) ; }
Compiles the expression .