Package org.antlr.v4.codegen
Class Target
- java.lang.Object
-
- org.antlr.v4.codegen.Target
-
- Direct Known Subclasses:
CppTarget
,CSharpTarget
,DartTarget
,GoTarget
,JavaScriptTarget
,JavaTarget
,PHPTarget
,Python3Target
,SwiftTarget
,TypeScriptTarget
public abstract class Target extends Object
-
-
Field Summary
Fields Modifier and Type Field Description protected static Map<Character,String>
defaultCharValueEscape
protected CodeGenerator
gen
-
Constructor Summary
Constructors Modifier Constructor Description protected
Target(CodeGenerator gen)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected static void
addEscapedChar(HashMap<Character,String> map, char key)
protected static void
addEscapedChar(HashMap<Character,String> map, char key, char representation)
protected void
appendUnicodeEscapedCodePoint(int codePoint, StringBuilder sb)
Escape the Unicode code point appropriately for this language and append the escaped value tosb
.String
encodeInt16AsCharEscape(int v)
Assume 16-bit charprotected String
escapeChar(int v)
String
escapeIfNeeded(String identifier)
protected String
escapeWord(String word)
protected void
genFile(Grammar g, org.stringtemplate.v4.ST outputFileST, String fileName)
String
getAltLabelContextStructName(String label)
String
getBaseListenerFileName(boolean header)
A given grammar T, return a blank listener implementation such as TBaseListener.java, if we're using the Java target.String
getBaseVisitorFileName(boolean header)
A given grammar T, return a blank listener implementation such as TBaseListener.java, if we're using the Java target.CodeGenerator
getCodeGenerator()
String
getElementListName(String name)
String
getElementName(String name)
String
getImplicitRuleLabel(String ruleName)
String
getImplicitSetLabel(String id)
String
getImplicitTokenLabel(String tokenName)
int
getInlineTestSetWordSize()
How many bits should be used to do inline token type tests? Java assumes a 64-bit word for bitsets.String
getLanguage()
String
getListenerFileName(boolean header)
A given grammar T, return the listener name such as TListener.java, if we're using the Java target.String
getListLabel(String label)
String
getLoopCounter(GrammarAST ast)
String
getLoopLabel(GrammarAST ast)
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.protected abstract Set<String>
getReservedWords()
String
getRuleFunctionContextStructName(RuleFunction function)
If we know which actual function, we can provide the actual ctx type.String
getRuleFunctionContextStructName(Rule r)
int
getSerializedATNSegmentLimit()
Gets the maximum number of 16-bit unsigned integers that can be encoded in a single segment (a declaration in target language) of the serialized ATN.Map<Character,String>
getTargetCharValueEscape()
For pure strings of Unicode char, how can we display it in the target language as a literal.String
getTargetStringLiteralFromANTLRStringLiteral(CodeGenerator generator, String literal, boolean addQuotes)
String
getTargetStringLiteralFromANTLRStringLiteral(CodeGenerator generator, String literal, boolean addQuotes, boolean escapeSpecial)
Convert from an ANTLR string literal found in a grammar file to an equivalent string literal in the target language.String
getTargetStringLiteralFromString(String s)
String
getTargetStringLiteralFromString(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.STGroup
getTemplates()
String
getTokenTypeAsTargetLabel(Grammar g, int ttype)
Get a meaningful name for a token type useful during code generation.String[]
getTokenTypesAsTargetLabels(Grammar g, int[] ttypes)
String
getVersion()
ANTLR tool should check output templates / target are compatible with tool code generation.String
getVisitorFileName(boolean header)
A given grammar T, return the visitor name such as TVisitor.java, if we're using the Java target.boolean
grammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode)
boolean
isATNSerializedAsInts()
protected org.stringtemplate.v4.STGroup
loadTemplates()
boolean
needsHeader()
protected boolean
shouldUseUnicodeEscapeForCodePointInDoubleQuotedString(int codePoint)
boolean
supportsOverloadedMethods()
boolean
templatesExist()
protected boolean
visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode)
Deprecated.boolean
wantsBaseListener()
boolean
wantsBaseVisitor()
-
-
-
Field Detail
-
gen
protected final CodeGenerator gen
-
-
Constructor Detail
-
Target
protected Target(CodeGenerator gen)
-
-
Method Detail
-
getTargetCharValueEscape
public Map<Character,String> getTargetCharValueEscape()
For pure strings of 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. Each target can have a different set in memory at same time.
-
addEscapedChar
protected static void addEscapedChar(HashMap<Character,String> map, char key, char representation)
-
getLanguage
public String getLanguage()
-
getCodeGenerator
public CodeGenerator getCodeGenerator()
-
getVersion
public 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()
-
getTokenTypeAsTargetLabel
public 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.
-
getTargetStringLiteralFromString
public String getTargetStringLiteralFromString(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 void appendUnicodeEscapedCodePoint(int codePoint, StringBuilder sb)
Escape the Unicode code point appropriately for this language and append the escaped value tosb
. It exists for flexibility and backward compatibility with external targets The static methodUnicodeEscapes.appendEscapedCodePoint(StringBuilder, int, String)
can be used as well if default escaping method (Java) is used or language is officially supported
-
getTargetStringLiteralFromANTLRStringLiteral
public String getTargetStringLiteralFromANTLRStringLiteral(CodeGenerator generator, String literal, boolean addQuotes)
-
getTargetStringLiteralFromANTLRStringLiteral
public String getTargetStringLiteralFromANTLRStringLiteral(CodeGenerator generator, String literal, boolean addQuotes, boolean escapeSpecial)
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.
-
shouldUseUnicodeEscapeForCodePointInDoubleQuotedString
protected boolean shouldUseUnicodeEscapeForCodePointInDoubleQuotedString(int codePoint)
-
encodeInt16AsCharEscape
public String encodeInt16AsCharEscape(int v)
Assume 16-bit char
-
escapeChar
protected String escapeChar(int v)
-
getLoopLabel
public String getLoopLabel(GrammarAST ast)
-
getLoopCounter
public String getLoopCounter(GrammarAST ast)
-
getRuleFunctionContextStructName
public 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...
-
getRecognizerFileName
public 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 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 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 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 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 (a declaration in target language) of the serialized ATN. E.g., in C++, a small segment length results in multiple decls like: static const int32_t serializedATNSegment1[] = { 0x7, 0x12, 0x2, 0x13, 0x7, 0x13, 0x2, 0x14, 0x7, 0x14, 0x2, 0x15, 0x7, 0x15, 0x2, 0x16, 0x7, 0x16, 0x2, 0x17, 0x7, 0x17, 0x2, 0x18, 0x7, 0x18, 0x2, 0x19, 0x7, 0x19, 0x2, 0x1a, 0x7, 0x1a, 0x2, 0x1b, 0x7, 0x1b, 0x2, 0x1c, 0x7, 0x1c, 0x2, 0x1d, 0x7, 0x1d, 0x2, 0x1e, 0x7, 0x1e, 0x2, 0x1f, 0x7, 0x1f, 0x2, 0x20, 0x7, 0x20, 0x2, 0x21, 0x7, 0x21, 0x2, 0x22, 0x7, 0x22, 0x2, 0x23, 0x7, 0x23, 0x2, 0x24, 0x7, 0x24, 0x2, 0x25, 0x7, 0x25, 0x2, 0x26, }; instead of one big one. Targets are free to ignore this like JavaScript does. This is primarily needed by Java target to limit size of any single ATN string to 65k length.- 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
@Deprecated protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode)
Deprecated.
-
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
-
isATNSerializedAsInts
public boolean isATNSerializedAsInts()
-
needsHeader
public boolean needsHeader()
- Since:
- 4.6
-
-