Class JavaTarget

java.lang.Object
org.antlr.v4.codegen.Target
org.antlr.v4.codegen.target.JavaTarget

public class JavaTarget extends Target
  • Field Details

    • targetTemplates

      private static final ThreadLocal<org.stringtemplate.v4.STGroup> targetTemplates
      The Java target can cache the code generation templates.
    • javaKeywords

      protected static final String[] javaKeywords
    • badWords

      protected final Set<String> badWords
      Avoid grammar symbols in this set to prevent conflicts in gen'd code.
  • Constructor Details

  • Method Details

    • getBadWords

      public Set<String> getBadWords()
    • addBadWords

      protected void addBadWords()
    • getTargetStringLiteralFromANTLRStringLiteral

      public String getTargetStringLiteralFromANTLRStringLiteral(CodeGenerator generator, String literal, boolean addQuotes)
      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 people to use '\"' without penalty, so we must build the target string in a loop as String.replace(char, char) cannot handle both \" and " without a lot of messing around.

      Specified by:
      getTargetStringLiteralFromANTLRStringLiteral in class Target
    • encodeIntAsCharEscape

      public String encodeIntAsCharEscape(int v)
      Description copied from class: Target
      Assume 16-bit char
      Specified by:
      encodeIntAsCharEscape in class Target
    • getSerializedATNSegmentLimit

      public int getSerializedATNSegmentLimit()
      Description copied from class: Target
      Gets the maximum number of 16-bit unsigned integers that can be encoded in a single segment of the serialized ATN.
      Overrides:
      getSerializedATNSegmentLimit in class Target
      Returns:
      the serialized ATN segment limit
      See Also:
    • visibleGrammarSymbolCausesIssueInGeneratedCode

      protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode)
      Specified by:
      visibleGrammarSymbolCausesIssueInGeneratedCode in class Target
    • loadTemplates

      protected org.stringtemplate.v4.STGroup loadTemplates()
      Overrides:
      loadTemplates in class Target
    • appendJavaStringLiteralText

      private void appendJavaStringLiteralText(String text, StringBuilder sb)
      Append grammar text that is not a Unicode scalar. Backslash and quote are escaped so javac does not treat a later quote or unicode escape as syntax.
    • isHexDigit

      private static boolean isHexDigit(char c)
    • appendJavaStringCodePoint

      private void appendJavaStringCodePoint(int codePoint, StringBuilder sb)
      Append one code point inside a Java double-quoted literal. Code points that javac treats as raw characters even inside a \\u escape use a short escape instead.
      Parameters:
      codePoint - a Unicode scalar value, 0..Character.MAX_CODE_POINT
      Throws:
      IllegalArgumentException - when codePoint is not a scalar. Callers preserve an undecodable grammar escape as source text instead.
    • appendUnicodeEscapedCodePoint

      protected void appendUnicodeEscapedCodePoint(int codePoint, StringBuilder sb)
      Description copied from class: Target
      Escape the Unicode code point appropriately for this language and append the escaped value to sb.
      Specified by:
      appendUnicodeEscapedCodePoint in class Target