public static Target loadLanguageTarget(String language) { Target target = null; String targetName = "org.antlr.codegen."+language+"Target"; try { Class<? extends Target> c = Class.forName(targetName).asSubclass(Target.class); target = (Target)c.newInstance(); } catch (ClassNotFoundException cnfe) { target = new Target(); // use default } catch (InstantiationException ie) { ErrorManager.error(ErrorManager.MSG_CANNOT_CREATE_TARGET_GENERATOR, targetName, ie); } catch (IllegalAccessException cnfe) { ErrorManager.error(ErrorManager.MSG_CANNOT_CREATE_TARGET_GENERATOR, targetName, cnfe); } return target; }
encoded.add(generator.target.encodeIntAsCharEscape((char)n)); encoded.add(generator.target.encodeIntAsCharEscape((char)I.intValue())); i+=n;
/** What is the max char value possible for this grammar's target? Use * unicode max if no target defined. */ public int getMaxCharValue() { if ( generator!=null ) { return generator.target.getMaxCharValue(generator); } else { return Label.MAX_CHAR_VALUE; } }
/** Get a meaningful name for a token type useful during code generation. * Literals without associated names are converted to the string equivalent * of their integer values. Used to generate x==ID and x==34 type comparisons * etc... Essentially we are looking for the most obvious way to refer * to a token type in the generated code. If in the lexer, return the * char literal translated to the target language. For example, ttype=10 * will yield '\n' from the getTokenDisplayName method. That must * be converted to the target languages literals. For most C-derived * languages no translation is needed. */ public String getTokenTypeAsTargetLabel(int ttype) { if ( grammar.type==Grammar.LEXER ) { String name = grammar.getTokenDisplayName(ttype); return target.getTargetCharLiteralFromANTLRCharLiteral(this,name); } return target.getTokenTypeAsTargetLabel(this,ttype); }
return null; target.performGrammarAnalysis(this, grammar); target.getTargetStringLiteralFromString(grammar.getFileName()); outputFileST.setAttribute("fileName", targetAppropriateFileNameString); headerFileST.setAttribute("fileName", targetAppropriateFileNameString); target.genRecognizerFile(tool,this,grammar,outputFileST); if ( templates.isDefined("headerFile") ) { StringTemplate extST = templates.getInstanceOf("headerFileExtension"); target.genRecognizerHeaderFile(tool,this,grammar,headerFileST,extST.toString());
public String getTargetStringLiteralFromString(String s) { return getTargetStringLiteralFromString(s, false); }
String low = generator.target.getTargetCharLiteralFromANTLRCharLiteral(generator,(a!=null?a.getText():null)); String high = generator.target.getTargetCharLiteralFromANTLRCharLiteral(generator,(b!=null?b.getText():null)); retval.code.add("a", low); retval.code.add("b", high); retval.code = templates.getInstanceOf("validateSemanticPredicate"); retval.code.add("pred", generator.translateAction(currentRuleName,sp)); String description = generator.target.getTargetStringLiteralFromString((sp!=null?sp.getText():null)); retval.code.add("description", description);
code = templates.getInstanceOf("charRef"); code.setAttribute("char", generator.target.getTargetCharLiteralFromANTLRCharLiteral(generator,c.getText())); if ( label!=null ) { code.setAttribute("label", labelText); code = templates.getInstanceOf("lexerStringRef"); code.setAttribute("string", generator.target.getTargetStringLiteralFromANTLRStringLiteral(generator,s.getText())); code.setAttribute("elementIndex", i); if ( label!=null ) {
/** Some targets will have some extra scopes like C++ may have * '@headerfile:name {action}' or something. Make sure the * target likes the scopes in action table. */ protected void verifyActionScopesOkForTarget(Map actions) { Set actionScopeKeySet = actions.keySet(); for (Iterator it = actionScopeKeySet.iterator(); it.hasNext();) { String scope = (String)it.next(); if ( !target.isValidActionScope(grammar.type, scope) ) { // get any action from the scope to get error location Map scopeActions = (Map)actions.get(scope); GrammarAST actionAST = (GrammarAST)scopeActions.values().iterator().next(); ErrorManager.grammarError( ErrorManager.MSG_INVALID_ACTION_SCOPE,grammar, actionAST.getToken(),scope, grammar.getGrammarTypeString()); } } }
if ( ruleDescr.isSynPred && generator.target.useBaseTemplatesForSynPredFragments() ) false); description = generator.target.getTargetStringLiteralFromString(description); (b!=null?((CodeGenTreeWalker.block_return)b).code:null).add("description", description); description = generator.target.getTargetStringLiteralFromString(description); retval.code.add("description", description);
for (int j = 0; j < words.length; j++) { long w = words[j]; wordStrings[j] = target.getTarget64BitStringFromValue(w);
@Override protected void genRecognizerFile(Tool tool, CodeGenerator generator, Grammar grammar, ST outputFileST) throws IOException { if (!grammar.getGrammarIsRoot()) { Grammar rootGrammar = grammar.composite.getRootGrammar(); String actionScope = grammar.getDefaultActionScope(grammar.type); Map<String, Object> actions = rootGrammar.getActions().get(actionScope); Object rootNamespace = actions != null ? actions.get("namespace") : null; if (actions != null && rootNamespace != null) { actions = grammar.getActions().get(actionScope); if (actions == null) { actions = new HashMap<String, Object>(); grammar.getActions().put(actionScope, actions); } actions.put("namespace", rootNamespace); } } generator.getTemplates().registerRenderer(String.class, new StringRenderer(generator, this)); super.genRecognizerFile(tool, generator, grammar, outputFileST); }
return null; target.performGrammarAnalysis(this, grammar); target.getTargetStringLiteralFromString(grammar.getFileName()); outputFileST.setAttribute("fileName", targetAppropriateFileNameString); headerFileST.setAttribute("fileName", targetAppropriateFileNameString); target.genRecognizerFile(tool,this,grammar,outputFileST); if ( templates.isDefined("headerFile") ) { StringTemplate extST = templates.getInstanceOf("headerFileExtension"); target.genRecognizerHeaderFile(tool,this,grammar,headerFileST,extST.toString());
public String getTargetStringLiteralFromString(String s) { return getTargetStringLiteralFromString(s, false); }
/** Get a meaningful name for a token type useful during code generation. * Literals without associated names are converted to the string equivalent * of their integer values. Used to generate x==ID and x==34 type comparisons * etc... Essentially we are looking for the most obvious way to refer * to a token type in the generated code. If in the lexer, return the * char literal translated to the target language. For example, ttype=10 * will yield '\n' from the getTokenDisplayName method. That must * be converted to the target languages literals. For most C-derived * languages no translation is needed. */ public String getTokenTypeAsTargetLabel(int ttype) { if ( grammar.type==Grammar.LEXER ) { String name = grammar.getTokenDisplayName(ttype); return target.getTargetCharLiteralFromANTLRCharLiteral(this,name); } return target.getTokenTypeAsTargetLabel(this,ttype); }
String low = generator.target.getTargetCharLiteralFromANTLRCharLiteral(generator,(a!=null?a.getText():null)); String high = generator.target.getTargetCharLiteralFromANTLRCharLiteral(generator,(b!=null?b.getText():null)); retval.code.add("a", low); retval.code.add("b", high); retval.code = templates.getInstanceOf("validateSemanticPredicate"); retval.code.add("pred", generator.translateAction(currentRuleName,sp)); String description = generator.target.getTargetStringLiteralFromString((sp!=null?sp.getText():null)); retval.code.add("description", description);
generator.target.getTargetCharLiteralFromANTLRCharLiteral(generator,(c!=null?c.getText():null))); if ( label!=null ) generator.target.getTargetStringLiteralFromANTLRStringLiteral(generator,(s!=null?s.getText():null))); retval.code.add("elementIndex", i); if ( label!=null )
/** Some targets will have some extra scopes like C++ may have * '@headerfile:name {action}' or something. Make sure the * target likes the scopes in action table. */ protected void verifyActionScopesOkForTarget(Map<String, Map<String, Object>> actions) { for (Map.Entry<String, Map<String, Object>> entry : actions.entrySet()) { String scope = entry.getKey(); if ( !target.isValidActionScope(grammar.type, scope) ) { // get any action from the scope to get error location Map<String, Object> scopeActions = entry.getValue(); GrammarAST actionAST = (GrammarAST)scopeActions.values().iterator().next(); ErrorManager.grammarError( ErrorManager.MSG_INVALID_ACTION_SCOPE,grammar, actionAST.getToken(),scope, grammar.getGrammarTypeString()); } } }
if ( ruleDescr.isSynPred && generator.target.useBaseTemplatesForSynPredFragments() ) false); description = generator.target.getTargetStringLiteralFromString(description); (b!=null?((CodeGenTreeWalker.block_return)b).code:null).add("description", description); description = generator.target.getTargetStringLiteralFromString(description); retval.code.add("description", description);
for (int j = 0; j < words.length; j++) { long w = words[j]; wordStrings[j] = target.getTarget64BitStringFromValue(w);