/*
[The "BSD licence"]
Copyright (c) 2014 Vlad Shlosberg
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.BitSet;
import java.util.List;
import org.antlr.v4.runtime.ANTLRErrorListener;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.BufferedTokenStream;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.atn.ATNConfigSet;
import org.antlr.v4.runtime.atn.PredictionMode;
import org.antlr.v4.runtime.dfa.DFA;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.Nullable;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.testng.Assert;

public class TestBase
{
  protected void assertEqualsNoOrder(List actual, List expected)
  {
    Assert.assertEqualsNoOrder(actual.toArray(), expected.toArray(), "Not Equal Expected: " +  Arrays.toString(expected.toArray()) + " Actual: " + Arrays.toString(
        actual.toArray()));
  }

  protected ScssParser.StylesheetContext parse(String ... lines)
  {
    try
    {
      StringBuilder fileLines = new StringBuilder();
      for (String line : lines)
      {
        fileLines.append(line);
      }

      ANTLRInputStream source = new ANTLRInputStream(new ByteArrayInputStream(fileLines.toString().getBytes()));

      ScssLexer lexer = new ScssLexer(source);
      TokenStream stream = new BufferedTokenStream(lexer);
      ScssParser par = new ScssParser(stream);
      par.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);

      par.addErrorListener(new FailOnErrorListener());
      return par.stylesheet();
    }
    catch (IOException e)
    {
      Assert.fail("Exception should not happen here", e);
    }
    return null;
  }

  private class FailOnErrorListener implements ANTLRErrorListener
  {
    @Override
    public void syntaxError(@NotNull Recognizer<?, ?> recognizer,
                            @Nullable Object o,
                            int i,
                            int i2,
                            @NotNull String s,
                            @Nullable RecognitionException e)
    {
      Assert.fail("Syntax error in detection.");
    }

    @Override
    public void reportAmbiguity(@NotNull Parser parser,
                                @NotNull DFA dfa,
                                int i,
                                int i2,
                                boolean b,
                                @Nullable BitSet bitSet,
                                @NotNull ATNConfigSet atnConfigs)
    {
      Assert.fail("Ambiguity error in detection.");

    }

    @Override
    public void reportAttemptingFullContext(@NotNull Parser parser,
                                            @NotNull DFA dfa,
                                            int i,
                                            int i2,
                                            @Nullable BitSet bitSet,
                                            @NotNull ATNConfigSet atnConfigs)
    {
      Assert.fail("Attempting full context error in detection.");

    }

    @Override
    public void reportContextSensitivity(@NotNull Parser parser,
                                         @NotNull DFA dfa,
                                         int i,
                                         int i2,
                                         int i3,
                                         @NotNull ATNConfigSet atnConfigs)
    {
      Assert.fail("Context sensitivity error in detection.");

    }
  }


  private String getExpressionMap(ParseTree start, int tabs)
  {
    if (start == null)
    {
      return "";
    }

    StringBuilder bld = new StringBuilder();
    if (TerminalNode.class.isAssignableFrom(start.getClass()))
    {
      for (int t = 0; t < tabs; t++)
      {
        bld.append("  ");
      }
      bld.append(start.getText());
      bld.append("\n");

    }
    int count = start.getChildCount();
    for (int i = 0; i < count; i++)
    {
      ParseTree child = start.getChild(i);
      for (int t = 0; t < tabs; t++)
      {
        bld.append("  ");
      }

      bld.append(child.getClass().getSimpleName() + ":\n" + getExpressionMap(child, tabs + 1));
    }
    return bld.toString();
  }

}

@@ -1996,93 +1996,8 @@
 FA;%0A
-import org.antlr.v4.runtime.misc.NotNull;%0Aimport org.antlr.v4.runtime.misc.Nullable;%0A
 impo
@@ -3294,25 +3294,16 @@
 axError(
-@NotNull 
 Recogniz
@@ -3351,26 +3351,16 @@
         
-@Nullable 
 Object o
@@ -3460,25 +3460,16 @@
         
-@NotNull 
 String s
@@ -3498,26 +3498,16 @@
         
-@Nullable 
 Recognit
@@ -3626,33 +3626,24 @@
 rtAmbiguity(
-@NotNull 
 Parser parse
@@ -3673,33 +3673,24 @@
             
-@NotNull 
 DFA dfa,%0A   
@@ -3836,34 +3836,24 @@
             
-@Nullable 
 BitSet bitSe
@@ -3883,33 +3883,24 @@
             
-@NotNull 
 ATNConfigSet
@@ -4036,25 +4036,16 @@
 Context(
-@NotNull 
 Parser p
@@ -4091,33 +4091,24 @@
             
-@NotNull 
 DFA dfa,%0A   
@@ -4255,18 +4255,8 @@
     
-@Nullable 
 BitS
@@ -4306,33 +4306,24 @@
             
-@NotNull 
 ATNConfigSet
@@ -4470,25 +4470,16 @@
 itivity(
-@NotNull 
 Parser p
@@ -4526,25 +4526,16 @@
         
-@NotNull 
 DFA dfa,
@@ -4726,17 +4726,8 @@
     
-@NotNull 
 ATNC

Remove Nullable and NotNull annotations from tests.