tree grammar ClientApiTreeParser; options { ASTLabelType = CommonTree; backtrack = true; superClass = ClientApiBaseTreeParser; tokenVocab = ClientApi; } @header { package com.wachovia.cib.robert.antlr.clientApi; import java.io.*; import com.wachovia.cib.cdl.*; } @members { private String mCdlVariableName; public static void main(String[] inArgs) throws Exception { if (inArgs.length == 1) { BufferedReader theInputReader = new BufferedReader(new InputStreamReader(new FileInputStream(inArgs[0]))); String theLine; while ((theLine = theInputReader.readLine()) != null) { int theIndex = theLine.indexOf("->"); String theCdlClassName = theLine.substring(0, theIndex); String theXPathExpression = theLine.substring(theIndex + 2); try { System.out.println("\nCode to extract \"" + theXPathExpression + "\" from \"" + theCdlClassName + "\":"); System.out.println('\t' + getTreeParser(theXPathExpression).start(theCdlClassName, "inCdlObject")); } catch (Throwable t) { t.printStackTrace(); } } } else { System.err.println("Please provide the name of a file containing a list of expressions to be evaluated"); } System.exit(0); } public static String getExtractionCode(String inCdlClassName, String inCdlVariableName, String inXPathExpression) throws RecognitionException { return getTreeParser(inXPathExpression).start(inCdlClassName, inCdlVariableName); } private static ClientApiTreeParser getTreeParser(String inXPathExpression) throws RecognitionException { CharStream theTextStream = new ANTLRStringStream(inXPathExpression); ClientApiLexer theLexer = new ClientApiLexer(theTextStream); CommonTokenStream theTokenStream = new CommonTokenStream(theLexer); ClientApiParser theParser = new ClientApiParser(theTokenStream); CommonTree theTree = (CommonTree) theParser.start().getTree(); CommonTreeNodeStream theNodes = new CommonTreeNodeStream(theTree); ClientApiTreeParser theTreeParser = new ClientApiTreeParser(theNodes); return theTreeParser; } } start[String inCdlClassName, String inCdlVariableName] returns [String code] @init { linkDefinitions(); pushDefiniton(inCdlClassName); mCdlVariableName = inCdlVariableName; CodeBuilder builder = new CodeBuilder(); } @after { $code = builder.toString(); unlinkDefinitions(); } : booleanExpression[inCdlVariableName] { builder.append($booleanExpression.code); } | objectPath[builder] ; booleanExpression[String variableName] returns [String code] @init { CodeBuilder builder = new CodeBuilder(); } @after { $code = builder.toString(); } : left=relationalExpression { builder.append($left.code); } ((booleanOperator) right=relationalExpression)* { builder.insert(0, '(').append($booleanOperator.text).append($right.code).append(')'); } ; relationalExpression returns [String code] @init { linkDefinitions(); CodeBuilder builder = new CodeBuilder(); } @after { $code = builder.toString(); unlinkDefinitions(); } : objectPath[builder] relationalOperator literal { builder.append(' ').append($relationalOperator.text).append(' ').append($literal.text); } | function[builder] relationalOperator literal { builder.append(" ").append($relationalOperator.text).append(' ').append($literal.text); } ; objectPath[CodeBuilder code] : memberReference[code] memberReference[code]* ; memberReference[CodeBuilder code] : ^(name[code] predicate[code]) { addCast(code); } | name[code] ; name[CodeBuilder code] : Name { String theName = $Name.text.substring(0, 1).toUpperCase() + $Name.text.substring(1); // Change first letter to upper case code.append(".get").append(theName).append("()"); pushDefiniton(theName); } ; predicate[CodeBuilder code] : IntegerLiteral { code.append(".get(").append($IntegerLiteral.text).append(')'); } | relationalExpression { code.append(".find(").append($relationalExpression.code).append(')'); } ; function[CodeBuilder code] : ^(Function FunctionName objectPath[code]) ; booleanOperator returns [String text] : BooleanOperator { $text = "or".equals($BooleanOperator.text) ? "||" : "&&"; } ; relationalOperator returns [String text] : RelationalOperator { $text = "=".equals($RelationalOperator.text) ? "==" : $RelationalOperator.text; } ; numericLiteral returns [String text] : IntegerLiteral { $text = $IntegerLiteral.text; } | DecimalLiteral { $text = $DecimalLiteral.text; } | DoubleLiteral { $text = $DoubleLiteral.text; } ; literal returns [String text] : StringLiteral { $text = $StringLiteral.text; } | numericLiteral { $text = $numericLiteral.text; } ;