/* LanguageTool, a natural language style checker 
 * Copyright (C) 2010 Marcin Miłkowski (http://www.languagetool.org)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

package de.danielnaber.languagetool.rules.ru;

import java.util.ResourceBundle;
import java.util.regex.Pattern;

import de.danielnaber.languagetool.AnalyzedTokenReadings;
import de.danielnaber.languagetool.Language;
import de.danielnaber.languagetool.rules.GenericUnpairedBracketsRule;

public class RussianUnpairedBracketsRule extends GenericUnpairedBracketsRule {

  private static final String[] RU_START_SYMBOLS = { "[", "(", "{", "„", "«", "\"", "'" };
  private static final String[] RU_END_SYMBOLS   = { "]", ")", "}", "“", "»", "\"", "'" };

  private static final Pattern NUMERALS_RU = Pattern
    .compile("(?i)\\d{1,2}?[а-я]*|[а-я]|[А-Я]|[а-я][а-я]|[А-Я][А-Я]");

  
  @Override
  protected boolean isNoException(final String token,
      final AnalyzedTokenReadings[] tokens, final int i, final int j,
      final boolean precSpace,
      final boolean follSpace) {
    // exception for Russian bullets: а), б), Д)..., ДД), аа) and 1а).
    if (i > 1 && endSymbols[j].equals(")") &&
        NUMERALS_RU.matcher(tokens[i - 1].getToken()).matches() && 
        !(!symbolStack.empty() && "(".equals(symbolStack.peek().symbol))) {
       return false;
     }
    return true;
  }
  
  public RussianUnpairedBracketsRule(final ResourceBundle messages,
      final Language language) {
    super(messages, language);
    startSymbols = RU_START_SYMBOLS;
    endSymbols = RU_END_SYMBOLS;
    uniqueMapInit();
  }

  @Override
  public String getId() {
    return "RU_UNPAIRED_BRACKETS";
  }
  
}

@@ -860,17 +860,16 @@
 USA%0A */%0A
-%0A
 package 
@@ -1227,16 +1227,18 @@
 Rule %7B%0A%0A
+  
   privat
@@ -1281,17 +1281,16 @@
 BOLS = %7B
- 
 %22%5B%22, %22(%22
@@ -1319,14 +1319,15 @@
  %22'%22
- 
 %7D;%0A  
+ 
+ 
 priv
@@ -1371,14 +1371,11 @@
 OLS 
-  
 = %7B
- 
 %22%5D%22,
@@ -1408,13 +1408,13 @@
  %22'%22
- 
 %7D;%0A
-%0A
+  
   pr
@@ -1461,21 +1461,16 @@
  Pattern
-%0A    
 .compile
@@ -1528,529 +1528,90 @@
 %D0%90-%D0%AF%5D
-%22);%0A%0A  %0A  @Override%0A  protected boolean isNoException(final String token,%0A      final AnalyzedTokenReadings%5B%5D tokens, final int i, final int j,%0A      final boolean precSpace,%0A      final boolean follSpace) %7B%0A    // exception for Russian bullets: %D0%B0), %D0%B1), %D0%94)..., %D0%94%D0%94), %D0%B0%D0%B0) and 1%D0%B0).%0A    if (i %3E 1 && endSymbols%5Bj%5D.equals(%22)%22) &&%0A        NUMERALS_RU.matcher(tokens%5Bi - 1%5D.getToken()).matches() && %0A        !(!symbolStack.empty() && %22(%22.equals(symbolStack.peek().symbol))) %7B%0A       return false;%0A     %7D%0A    return true;%0A  %7D
+%7C(?i)%5C%5Cd%7B1,2%7D?%5Ba-z'%5D*%7CM*(D?C%7B0,3%7D%7CC%5BDM%5D)(L?X%7B0,3%7D%7CX%5BLC%5D)(V?I%7B0,3%7D%7CI%5BVX%5D)$%22);%0A%0A%0A
 %0A  
-%0A
   pu
@@ -1674,16 +1674,22 @@
 ssages,%0A
+      
       fi
@@ -1717,16 +1717,20 @@
 ) %7B%0A    
+ 
+   
 super(me
@@ -1748,16 +1748,20 @@
 guage);%0A
+    
     star
@@ -1793,16 +1793,20 @@
 LS;%0A    
+   
+ 
 endSymbo
@@ -1822,24 +1822,59 @@
 ND_SYMBOLS;%0A
+        NUMERALS=NUMERALS_RU; %0A    
     uniqueMa
@@ -1886,13 +1886,17 @@
 ();%0A
+  
   %7D%0A%0A
+  
   @O
@@ -1903,16 +1903,18 @@
 verride%0A
+  
   public
@@ -1927,24 +1927,28 @@
 g getId() %7B%0A
+    
     return %22
@@ -1976,11 +1976,10 @@
 ;%0A  
-%7D%0A
   
+%7D
 %0A%7D%0A

fix sf bug 3334498