title: Is ANTLR the right approach for something like this?
body: <p>So, I'm really stuck and starting to think its just not the right approach.
  Consider this string: "[APPLE|ORANGE] PEAR"
  The logic here <em>should</em> be 
  "IF APPLE is in string, get me its color ELSE IF ORANGE is in string, get me its color ELSE return empty string. And now always return PEAR color".</p>
  
  <p>I was able to get a <a href="http://www.certpal.com/blogs/2011/01/antlr-tutorial-hello-antlr/" rel="nofollow">Hello World</a> working just fine.
  1. I'm struggling with getting ANTLR to understand the conditional logic in my string.
  2. I'm struggling with getting ANTLR to invoke getColor() per identifier.
  Any help or guidance would be appreciated. I'm running on fumes at the moment.</p>
  
  <pre><code>grammar Test;
  
  @header {
    package org.mytest.Test;
  }
  
  @members {
      private String answer = "";
  
      private void getColor(String fruit)
      {
          //Use java reflection to get Fruit class and invoke method getColor()
          answer = fruit.color;
      }
  
  }
  
   @lexer::header {
    package org.mytest.Test;
  }
  
  
  /*------------------------------------------------------------------
   * PARSER RULES
   *------------------------------------------------------------------*/
  
  row returns [List&lt;String&gt; list]
  @init {list = new ArrayList&lt;String&gt;();}
    :  a=value {list.add($a.val);} (WS b=value {list.add($b.val);})* (EOF)
    ;
  
  conditionalString    returns [String color]:
                  (
                  a=IDENTIFIER (WS IDENTIFIER)* {getColor($a.text); } 
                  )
                  {$color=answer;};
  
  //Get the text string for the matched identifier?
  value returns [String val]  :  IDENTIFIER {val = $IDENTIFIER.text;}  ;
  
  
  /*------------------------------------------------------------------
   * LEXER RULES
   *------------------------------------------------------------------*/
  
  IDENTIFIER  :   ('A'..'Z')+;
  
  
  WS : ( '\t' | ' ')+     { $channel = HIDDEN; } ;
  </code></pre>
  
  <p>The ideal usage is below. The "context" argument would be some resource handle.
  I would manually edit the Constructor to pass the context so ANTL knows how to handle each token.</p>
  
  <pre><code>TestLexer lex = new TestLexer(new ANTLRStringStream("[APPLE|ORANGE] PEAR"));
  CommonTokenStream tok = new CommonTokenStream(lex);
  TestParser par = new TestParser(context,tok);
  System.out.println(par.conditionalString());
  </code></pre>