Class Target

  • Direct Known Subclasses:
    ActionScriptTarget, CppTarget, CSharp3Target, CTarget, DelphiTarget, JavaScriptTarget, JavaTarget, ObjCTarget, Perl5Target, Python3Target, PythonTarget, RubyTarget

    public class Target
    extends java.lang.Object
    The code generator for ANTLR can usually be retargeted just by providing a new X.stg file for language X, however, sometimes the files that must be generated vary enough that some X-specific functionality is required. For example, in C, you must generate header files whereas in Java you do not. Other languages may want to keep DFA separate from the main generated recognizer file. The notion of a Code Generator target abstracts out the creation of the various files. As new language targets get added to the ANTLR system, this target class may have to be altered to handle more functionality. Eventually, just about all language generation issues will be expressible in terms of these methods. If org.antlr.codegen.XTarget class exists, it is used else Target base class is used. I am using a superclass rather than an interface for this target concept because I can add functionality later without breaking previously written targets (extra interface methods would force adding dummy functions to all code generator target classes).
    • Field Detail

      • targetCharValueEscape

        protected java.lang.String[] targetCharValueEscape
        For pure strings of Java 16-bit unicode char, how can we display it in the target language as a literal. Useful for dumping predicates and such that may refer to chars that need to be escaped when represented as strings. Also, templates need to be escaped so that the target language can hold them as a string. I have defined (via the constructor) the set of typical escapes, but your Target subclass is free to alter the translated chars or add more definitions. This is nonstatic so each target can have a different set in memory at same time.
    • Constructor Detail

      • Target

        public Target()
    • Method Detail

      • useBaseTemplatesForSynPredFragments

        public boolean useBaseTemplatesForSynPredFragments()
      • genRecognizerFile

        protected void genRecognizerFile​(Tool tool,
                                         CodeGenerator generator,
                                         Grammar grammar,
                                         org.stringtemplate.v4.ST outputFileST)
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • genRecognizerHeaderFile

        protected void genRecognizerHeaderFile​(Tool tool,
                                               CodeGenerator generator,
                                               Grammar grammar,
                                               org.stringtemplate.v4.ST headerFileST,
                                               java.lang.String extName)
                                        throws java.io.IOException
        Throws:
        java.io.IOException
      • performGrammarAnalysis

        protected void performGrammarAnalysis​(CodeGenerator generator,
                                              Grammar grammar)
      • isValidActionScope

        public boolean isValidActionScope​(int grammarType,
                                          java.lang.String scope)
        Is scope in @scope::name {action} valid for this kind of grammar? Targets like C++ may want to allow new scopes like headerfile or some such. The action names themselves are not policed at the moment so targets can add template actions w/o having to recompile ANTLR.
      • getTokenTypeAsTargetLabel

        public java.lang.String getTokenTypeAsTargetLabel​(CodeGenerator generator,
                                                          int ttype)
        Target must be able to override the labels used for token types
      • getTargetCharLiteralFromANTLRCharLiteral

        public java.lang.String getTargetCharLiteralFromANTLRCharLiteral​(CodeGenerator generator,
                                                                         java.lang.String literal)
        Convert from an ANTLR char literal found in a grammar file to an equivalent char literal in the target language. For most languages, this means leaving 'x' as 'x'. Actually, we need to escape ' ' so that it doesn't get converted to \n by the compiler. Convert the literal to the char value and then to an appropriate target char literal. Expect single quotes around the incoming literal.
      • getTargetStringLiteralFromANTLRStringLiteral

        public java.lang.String getTargetStringLiteralFromANTLRStringLiteral​(CodeGenerator generator,
                                                                             java.lang.String literal)
        Convert from an ANTLR string literal found in a grammar file to an equivalent string literal in the target language. For Java, this is the translation 'a\n"' → "a\n\"". Expect single quotes around the incoming literal. Just flip the quotes and replace double quotes with \" Note that we have decided to allow poeple to use '\"' without penalty, so we must build the target string in a loop as Utils.replae cannot handle both \" and " without a lot of messing around.
      • getTargetStringLiteralFromString

        public java.lang.String getTargetStringLiteralFromString​(java.lang.String s,
                                                                 boolean quoted)
        Given a random string of Java unicode chars, return a new string with optionally appropriate quote characters for target language and possibly with some escaped characters. For example, if the incoming string has actual newline characters, the output of this method would convert them to the two char sequence \n for Java, C, C++, ... The new string has double-quotes around it as well. Example String in memory: a"[newlinechar]b'c[carriagereturnchar]d[tab]e\f would be converted to the valid Java s: "a\"\nb'c\rd\te\\f" or a\"\nb'c\rd\te\\f depending on the quoted arg.
      • getTargetStringLiteralFromString

        public java.lang.String getTargetStringLiteralFromString​(java.lang.String s)
      • getTarget64BitStringFromValue

        public java.lang.String getTarget64BitStringFromValue​(long word)
        Convert long to 0xNNNNNNNNNNNNNNNN by default for spitting out with bitsets. I.e., convert bytes to hex string.
      • encodeIntAsCharEscape

        public java.lang.String encodeIntAsCharEscape​(int v)
      • getMaxCharValue

        public int getMaxCharValue​(CodeGenerator generator)
        Some targets only support ASCII or 8-bit chars/strings. For example, C++ will probably want to return 0xFF here.
      • postProcessAction

        public java.util.List<java.lang.Object> postProcessAction​(java.util.List<java.lang.Object> chunks,
                                                                  org.antlr.runtime.Token actionToken)
        Give target a chance to do some postprocessing on actions. Python for example will have to fix the indention.