Package org.antlr.v4.codegen
Class Target
- java.lang.Object
-
- org.antlr.v4.codegen.Target
-
- Direct Known Subclasses:
CppTarget,CSharpTarget,GoTarget,JavaScriptTarget,JavaTarget,Python2Target,Python3Target,SwiftTarget
public abstract class Target extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description protected CodeGeneratorgenprotected java.lang.String[]targetCharValueEscapeFor pure strings of Java 16-bit Unicode char, how can we display it in the target language as a literal.
-
Constructor Summary
Constructors Modifier Constructor Description protectedTarget(CodeGenerator gen, java.lang.String language)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract voidappendUnicodeEscapedCodePoint(int codePoint, java.lang.StringBuilder sb)Escape the Unicode code point appropriately for this language and append the escaped value tosb.java.lang.StringencodeIntAsCharEscape(int v)Assume 16-bit charprotected voidgenFile(Grammar g, org.stringtemplate.v4.ST outputFileST, java.lang.String fileName)java.lang.StringgetAltLabelContextStructName(java.lang.String label)java.lang.StringgetBaseListenerFileName(boolean header)A given grammar T, return a blank listener implementation such as TBaseListener.java, if we're using the Java target.java.lang.StringgetBaseVisitorFileName(boolean header)A given grammar T, return a blank listener implementation such as TBaseListener.java, if we're using the Java target.CodeGeneratorgetCodeGenerator()java.lang.StringgetElementListName(java.lang.String name)java.lang.StringgetElementName(java.lang.String name)java.lang.StringgetImplicitRuleLabel(java.lang.String ruleName)java.lang.StringgetImplicitSetLabel(java.lang.String id)java.lang.StringgetImplicitTokenLabel(java.lang.String tokenName)intgetInlineTestSetWordSize()How many bits should be used to do inline token type tests? Java assumes a 64-bit word for bitsets.java.lang.StringgetLanguage()java.lang.StringgetListenerFileName(boolean header)A given grammar T, return the listener name such as TListener.java, if we're using the Java target.java.lang.StringgetListLabel(java.lang.String label)java.lang.StringgetLoopCounter(GrammarAST ast)java.lang.StringgetLoopLabel(GrammarAST ast)java.lang.StringgetRecognizerFileName(boolean header)Generate TParser.java and TLexer.java from T.g4 if combined, else just use T.java as output regardless of type.java.lang.StringgetRuleFunctionContextStructName(RuleFunction function)If we know which actual function, we can provide the actual ctx type.java.lang.StringgetRuleFunctionContextStructName(Rule r)intgetSerializedATNSegmentLimit()Gets the maximum number of 16-bit unsigned integers that can be encoded in a single segment of the serialized ATN.java.lang.StringgetTargetStringLiteralFromANTLRStringLiteral(CodeGenerator generator, java.lang.String literal, boolean addQuotes)Convert from an ANTLR string literal found in a grammar file to an equivalent string literal in the target language.java.lang.StringgetTargetStringLiteralFromString(java.lang.String s)java.lang.StringgetTargetStringLiteralFromString(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.org.stringtemplate.v4.STGroupgetTemplates()java.lang.StringgetTokenTypeAsTargetLabel(Grammar g, int ttype)Get a meaningful name for a token type useful during code generation.java.lang.String[]getTokenTypesAsTargetLabels(Grammar g, int[] ttypes)abstract java.lang.StringgetVersion()ANTLR tool should check output templates / target are compatible with tool code generation.java.lang.StringgetVisitorFileName(boolean header)A given grammar T, return the visitor name such as TVisitor.java, if we're using the Java target.booleangrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode)protected org.stringtemplate.v4.STGrouploadTemplates()booleanneedsHeader()booleansupportsOverloadedMethods()booleantemplatesExist()protected abstract booleanvisibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode)booleanwantsBaseListener()booleanwantsBaseVisitor()
-
-
-
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
Targetsubclass is free to alter the translated chars or add more definitions. This is non-static so each target can have a different set in memory at same time.
-
gen
protected final CodeGenerator gen
-
-
Constructor Detail
-
Target
protected Target(CodeGenerator gen, java.lang.String language)
-
-
Method Detail
-
getCodeGenerator
public CodeGenerator getCodeGenerator()
-
getLanguage
public java.lang.String getLanguage()
-
getVersion
public abstract java.lang.String getVersion()
ANTLR tool should check output templates / target are compatible with tool code generation. For now, a simple string match used on x.y of x.y.z scheme. We use a method to avoid mismatches between a template called VERSION. This value is checked against Tool.VERSION during load of templates. This additional method forces all targets 4.3 and beyond to add this method.- Since:
- 4.3
-
getTemplates
public org.stringtemplate.v4.STGroup getTemplates()
-
genFile
protected void genFile(Grammar g, org.stringtemplate.v4.ST outputFileST, java.lang.String fileName)
-
getTokenTypeAsTargetLabel
public java.lang.String getTokenTypeAsTargetLabel(Grammar g, int ttype)
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.
-
getTokenTypesAsTargetLabels
public java.lang.String[] getTokenTypesAsTargetLabels(Grammar g, int[] ttypes)
-
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.
-
appendUnicodeEscapedCodePoint
protected abstract void appendUnicodeEscapedCodePoint(int codePoint, java.lang.StringBuilder sb)Escape the Unicode code point appropriately for this language and append the escaped value tosb.
-
getTargetStringLiteralFromString
public java.lang.String getTargetStringLiteralFromString(java.lang.String s)
-
getTargetStringLiteralFromANTLRStringLiteral
public java.lang.String getTargetStringLiteralFromANTLRStringLiteral(CodeGenerator generator, java.lang.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.
-
encodeIntAsCharEscape
public java.lang.String encodeIntAsCharEscape(int v)
Assume 16-bit char
-
getLoopLabel
public java.lang.String getLoopLabel(GrammarAST ast)
-
getLoopCounter
public java.lang.String getLoopCounter(GrammarAST ast)
-
getListLabel
public java.lang.String getListLabel(java.lang.String label)
-
getRuleFunctionContextStructName
public java.lang.String getRuleFunctionContextStructName(Rule r)
-
getAltLabelContextStructName
public java.lang.String getAltLabelContextStructName(java.lang.String label)
-
getRuleFunctionContextStructName
public java.lang.String getRuleFunctionContextStructName(RuleFunction function)
If we know which actual function, we can provide the actual ctx type. This will contain implicit labels etc... From outside, though, we see only ParserRuleContext unless there are externally visible stuff like args, locals, explicit labels, etc...
-
getImplicitTokenLabel
public java.lang.String getImplicitTokenLabel(java.lang.String tokenName)
-
getImplicitSetLabel
public java.lang.String getImplicitSetLabel(java.lang.String id)
-
getImplicitRuleLabel
public java.lang.String getImplicitRuleLabel(java.lang.String ruleName)
-
getElementListName
public java.lang.String getElementListName(java.lang.String name)
-
getElementName
public java.lang.String getElementName(java.lang.String name)
-
getRecognizerFileName
public java.lang.String getRecognizerFileName(boolean header)
Generate TParser.java and TLexer.java from T.g4 if combined, else just use T.java as output regardless of type.
-
getListenerFileName
public java.lang.String getListenerFileName(boolean header)
A given grammar T, return the listener name such as TListener.java, if we're using the Java target.
-
getVisitorFileName
public java.lang.String getVisitorFileName(boolean header)
A given grammar T, return the visitor name such as TVisitor.java, if we're using the Java target.
-
getBaseListenerFileName
public java.lang.String getBaseListenerFileName(boolean header)
A given grammar T, return a blank listener implementation such as TBaseListener.java, if we're using the Java target.
-
getBaseVisitorFileName
public java.lang.String getBaseVisitorFileName(boolean header)
A given grammar T, return a blank listener implementation such as TBaseListener.java, if we're using the Java target.
-
getSerializedATNSegmentLimit
public int getSerializedATNSegmentLimit()
Gets the maximum number of 16-bit unsigned integers that can be encoded in a single segment of the serialized ATN.- Returns:
- the serialized ATN segment limit
- See Also:
SerializedATN.getSegments()
-
getInlineTestSetWordSize
public int getInlineTestSetWordSize()
How many bits should be used to do inline token type tests? Java assumes a 64-bit word for bitsets. Must be a valid wordsize for your target like 8, 16, 32, 64, etc...- Since:
- 4.5
-
grammarSymbolCausesIssueInGeneratedCode
public boolean grammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode)
-
visibleGrammarSymbolCausesIssueInGeneratedCode
protected abstract boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode)
-
templatesExist
public boolean templatesExist()
-
loadTemplates
protected org.stringtemplate.v4.STGroup loadTemplates()
-
wantsBaseListener
public boolean wantsBaseListener()
- Since:
- 4.3
-
wantsBaseVisitor
public boolean wantsBaseVisitor()
- Since:
- 4.3
-
supportsOverloadedMethods
public boolean supportsOverloadedMethods()
- Since:
- 4.3
-
needsHeader
public boolean needsHeader()
- Since:
- 4.6
-
-