title: How to extract line with syntax error when parsing PlSQL using Antlr4
body: <p>I am using the grammar file for PlSql from this <a href="https://github.com/antlr/grammars-v4/tree/master/sql/plsql" rel="nofollow noreferrer">Github repository</a>. I want to underline the line in plsql file that I parse if it has a syntax error. I have the following snippet to do so:</p>
  <pre><code>public static class UnderlineListener extends BaseErrorListener {
  
      public void syntaxError(Recognizer&lt;?, ?&gt; recognizer,
                              Object offendingSymbol,
                              int line, int charPositionInLine,
                              String msg,
                              RecognitionException e)
      {
          System.err.println(&quot;line &quot;+line+&quot;:&quot;+charPositionInLine+&quot; &quot;+msg);
          underlineError(recognizer,(Token)offendingSymbol,
                         line, charPositionInLine);
      }
  
      protected void underlineError(Recognizer recognizer,
                                    Token offendingToken, int line,
                                    int charPositionInLine) {
          CommonTokenStream tokens =
              (CommonTokenStream)recognizer.getInputStream();
          String input = tokens.getTokenSource().getInputStream().toString();
          String[] lines = input.split(&quot;\n&quot;);
          String errorLine = lines[line - 1];
          System.err.println(errorLine);
          for (int i=0; i&lt;charPositionInLine; i++) System.err.print(&quot; &quot;);
          int start = offendingToken.getStartIndex();
          int stop = offendingToken.getStopIndex();
          if ( start&gt;=0 &amp;&amp; stop&gt;=0 ) {
          for (int i=start; i&lt;=stop; i++) System.err.print(&quot;^&quot;);
          }
      System.err.println();
      }
  }
  </code></pre>
  <p>While this works fine in most cases, some scripting languages, like PlSql, need <a href="https://github.com/antlr/grammars-v4/blob/master/sql/plsql/README.md" rel="nofollow noreferrer">special handling for case-sensitivity</a>. This means I had to use <a href="https://github.com/antlr/antlr4/blob/master/doc/case-insensitive-lexing.md#custom-character-streams-approach" rel="nofollow noreferrer">CaseChangingCharStream</a> as follows:</p>
  <pre><code>CharStream s = CharStreams.fromPath(Paths.get('test.sql'));
  CaseChangingCharStream upper = new CaseChangingCharStream(s, true);
  Lexer lexer = new SomeSQLLexer(upper);
  </code></pre>
  <p>Now when I try to get the input text inside my <code>UnderlineListener</code> using <code>String input = tokens.getTokenSource().getInputStream().toString();</code>, I do not get the actual text of my <code>test.sql</code>. This is because <code>getInputStream()</code> is returning <code>CaseChangingCharStream</code> object which does not give the desired actual text of my <code>test.sql</code>.</p>
  <p>How do I get the actual file text in my case? One way could be to pass the file content to the the constructor of <code>UnderlineListener</code>, but I would prefer to stick to the above method of getting actual file text since it can be used for cases where <code>CaseChangingCharStream</code> is not used.</p>