title: String interpolation: is it possible without adding members?
body: <p>I would like to create an Antlr parser for custom language and decided to pick a simple <a href="https://github.com/ouyi/antlr4calc/blob/master/Calculator.g4" rel="nofollow noreferrer">calculator</a> as an example. In my new grammar it should be possible to define a string, like this:</p>
  
  <pre><code>s = "Hello, I am a string"
  </code></pre>
  
  <p>and handle string interpolation.
  Text in double quotes enclosed in persent should be treated as interpolated, e.g.</p>
  
  <pre><code>s = "Hello, did you know that %2 + 2% is 4?"
  </code></pre>
  
  <p>Double percent sign should not be processed, e.g.</p>
  
  <pre><code>s = "He wants 50%% of this deal."
  </code></pre>
  
  <p>But at the same time my calculator should support modulus operation:</p>
  
  <pre><code>x = 5 % 2
  </code></pre>
  
  <p>So far, I was able to craft a Lexer/Grammar, which could switch mode and parse simple strings, here they are:</p>
  
  <pre><code>lexer grammar CalcLexer;
  
  EQ: '=';
  PLUS: '+';
  MINUS: '-';
  MULT: '*';
  DIV: '/';
  
  LPAREN : '(' ;
  RPAREN : ')' ;
  
  SINGLE_PERCENT_POP: '%' -&gt; popMode;
  
  ID  :   [a-zA-Z]+ ;
  INT :   [0-9]+ ;
  
  OPEN_DOUBLE_QUOTE: '"' -&gt; pushMode(STRING_MODE);
  
  NEWLINE:'\r'? '\n' ;
  WS  :   [ \t]+ -&gt; skip;
  
  
  mode STRING_MODE;
  DOUBLE_PERCENT: '%%';
  SINGLE_PERCENT: '%' -&gt; pushMode(DEFAULT_MODE);
  TEXT: ~('%'|'\n'|'"')+;
  
  
  CLOSE_DOUBLE_QUOTE: '"' -&gt; popMode;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>parser grammar CalcGrammar;
  
  options { tokenVocab=CalcLexer; } // use tokens from CalcLexer.g4
  
  prog:   stat+ ;
  
  stat:   expr NEWLINE
      |   ID EQ (expr|text) NEWLINE
      |   NEWLINE
      ;
  
  text: OPEN_DOUBLE_QUOTE content* CLOSE_DOUBLE_QUOTE;
  
  content: DOUBLE_PERCENT | TEXT | SINGLE_PERCENT expr SINGLE_PERCENT_POP;
  
  expr:   expr (MULT|DIV) expr
      |   expr (PLUS|MINUS) expr
      |   INT
      |   ID
      |   LPAREN expr RPAREN
      ;
  </code></pre>
  
  <p>But only thing doesn't work and I'm not sure if it ever possible to implement without custom code (members) is modulus operation:</p>
  
  <pre><code>x = 5 % 2
  </code></pre>
  
  <p>There is no way I can ask Anltr to check for previous mode and safely pop mode.
  But I hope my understanding is wrong and there is some way to treat <code>%</code> sign as operator in default mode?</p>
  
  <p>I have found several sources for inspiration, probably they would help you as well:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/1850468/parsing-string-interpolation-in-antlr">Parsing string interpolation in ANTLR</a></li>
  <li><a href="https://stackoverflow.com/questions/37916661/antlr-string-interpolation">ANTLR String interpolation</a></li>
  <li><a href="https://thosakwe.com/parsing-string-interpolations-with-antlr4/" rel="nofollow noreferrer">Parsing String Interpolations with ANTLR4</a></li>
  <li><a href="https://groups.google.com/forum/#!topic/antlr-discussion/81PVv9xLmP0" rel="nofollow noreferrer">String interpolation and lexer modes</a></li>
  </ul>