title: How to preserve whitespace in ANTLR4 when reading an string?
body: <p>I am creating a grammar which will be used to create a lexical analyser and parser for C#. This will take the below input and will output SQL.</p>
  
  <p><code>(path.path.path="how to do something")</code></p>
  
  <p>at the moment I am ignoring whitespace by using:</p>
  
  <p><code>WS              :   [ \t\r\n]+ -&gt; skip;               // Skips whitespace</code></p>
  
  <p>The problem is, when I'm reading the content within the quotes, I need to preserve the whitespace as it will be used in a search.
  How do I go about doing this?
  Thank you.</p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>Here is my current attempt at parsing strings:</p>
  
  <pre><code>TEXT            :   [a-zA-Z_]+;
  </code></pre>
  
  <p>There isn't much as I have just run into this problem, but I can't find a solution that I understand how to implement.</p>
  
  <p>I have also added</p>
  
  <pre><code>@lexer::members{
      //Lexer members
  
      //Used to preserve whitespace when reading in statements
      boolean ignore=true;
  }
  </code></pre>
  
  <p>I have seen something similar to this dotted about.
  Where the value of <code>ignore</code> will determine whether the whitespace will skip. I have also changed my whitespace rule to:</p>
  
  <pre><code>WS              :   [ \t\r\n]+ {if(ignore) skip();};   
  </code></pre>
  
  <p>But I'm unsure how I would set <code>ignore</code> to <code>false</code> before scanning over the statement and how I would change it back to <code>true</code> after I have finished.</p>
  
  <p><strong>Edit 2</strong></p>
  
  <p>I have copied over the entirety of my grammar file:</p>
  
  <pre><code>// Grammar for Search Criteria
  grammar SearchGen;
  
  @members{
      //Lexer members
  
      //Used to preserve whitespace when reading in statements
      boolean ignore=true;
  }
  
  r               :   block_clause*
                  |   block
                  |   statement;
  
  // block = ( statement CLAUSE statement )
  block           :   OPEN_BRACKET start_segment+ end_segment CLOSE_BRACKET;
  
  block_clause    :   block clause;
  
  start_segment   :   statement clause;
  
  end_segment     :   statement;
  
  statement       :   OPEN_BRACKET path search_term CLOSE_BRACKET; //Change TEXT to allows for blah.blah="hiv"
  
  path            :   TEXT '.' TEXT '.' TEXT;
  
  clause          :   NOT | OR | AND | WITHIN;
  
  search_term     :   OPERATOR SEARCH_TYPE;
  
  OPEN_BRACKET    :   '(';
  
  CLOSE_BRACKET   :   ')';
  
  UNDERSCORE      :   '_';
  
  SEARCH_TYPE     :   '"' (~["\\] | '\\' .)* '"';
  
  OPERATOR        :   EQUALS | GREATER_THAN | LESS_THAN | AMP; //Maybe put the amp and quotes in TEXT/
  
  GREATER_THAN    :   '&gt;' | '&amp;gt;';
  
  LESS_THAN       :   '&lt;' | '&amp;lt;';
  
  QUOTE           :   '"' | '&amp;amp;quot;';
  
  EQUALS          :   '=';
  
  AMP             :   '&amp;' | '&amp;amp;';
  
  NOT             :   'NOT' | 'not';
  
  OR              :   'OR' | 'or';
  
  AND             :   'AND' | 'and';
  
  WITHIN          :   'WITHIN' | 'within';
  
  
  
  //Possible problem : If a keyword that the user is looking for matches one of the above tokens
  
  TEXT            :   [a-zA-Z_]+; // Include Underscore
  
  DIGIT           :   [0-9]+;
  
  // yyyy-mm-dd
  
  DATE            : YEAR'-'MONTH'-'DAY;
  
  YEAR            :   [1-2][(0-9)][(0-9)][(0-9)];
  
  MONTH           :   [0][1-9] | [1-9] | [1][(0-2)];
  
  DAY             :   [0][1-9] | [1-2][0-9] | [3][0-1];
  
  WS              :   [ \t\r\n]+ -&gt;skip;               // Skips whitespace
  
  
  
  </code></pre>