Package org.antlr.codegen
Class Target
java.lang.Object
org.antlr.codegen.Target
- Direct Known Subclasses:
ActionScriptTarget
,CppTarget
,CSharp3Target
,CTarget
,DelphiTarget
,JavaScriptTarget
,JavaTarget
,ObjCTarget
,Perl5Target
,Python3Target
,PythonTarget
,RubyTarget
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 Summary
FieldsModifier and TypeFieldDescriptionprotected String[]
For pure strings of Java 16-bit unicode char, how can we display it in the target language as a literal. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionencodeIntAsCharEscape
(int v) protected void
genRecognizerFile
(Tool tool, CodeGenerator generator, Grammar grammar, org.stringtemplate.v4.ST outputFileST) protected void
genRecognizerHeaderFile
(Tool tool, CodeGenerator generator, Grammar grammar, org.stringtemplate.v4.ST headerFileST, String extName) int
getMaxCharValue
(CodeGenerator generator) Some targets only support ASCII or 8-bit chars/strings.getTarget64BitStringFromValue
(long word) Convert long to 0xNNNNNNNNNNNNNNNN by default for spitting out with bitsets.getTargetCharLiteralFromANTLRCharLiteral
(CodeGenerator generator, String literal) Convert from an ANTLR char literal found in a grammar file to an equivalent char literal in the target language.getTargetStringLiteralFromANTLRStringLiteral
(CodeGenerator generator, String literal) Convert from an ANTLR string literal found in a grammar file to an equivalent string literal in the target language.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.getTokenTypeAsTargetLabel
(CodeGenerator generator, int ttype) Target must be able to override the labels used for token typesboolean
isValidActionScope
(int grammarType, 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.protected void
performGrammarAnalysis
(CodeGenerator generator, Grammar grammar) postProcessAction
(List<Object> chunks, Token actionToken) Give target a chance to do some postprocessing on actions.boolean
-
Field Details
-
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 Details
-
Target
public Target()
-
-
Method Details
-
useBaseTemplatesForSynPredFragments
public boolean useBaseTemplatesForSynPredFragments() -
genRecognizerFile
protected void genRecognizerFile(Tool tool, CodeGenerator generator, Grammar grammar, org.stringtemplate.v4.ST outputFileST) throws IOException - Throws:
IOException
-
genRecognizerHeaderFile
protected void genRecognizerHeaderFile(Tool tool, CodeGenerator generator, Grammar grammar, org.stringtemplate.v4.ST headerFileST, String extName) throws IOException - Throws:
IOException
-
performGrammarAnalysis
-
isValidActionScope
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
Target must be able to override the labels used for token types -
getTargetCharLiteralFromANTLRCharLiteral
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
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
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
-
getTarget64BitStringFromValue
Convert long to 0xNNNNNNNNNNNNNNNN by default for spitting out with bitsets. I.e., convert bytes to hex string. -
encodeIntAsCharEscape
-
getMaxCharValue
Some targets only support ASCII or 8-bit chars/strings. For example, C++ will probably want to return 0xFF here. -
postProcessAction
Give target a chance to do some postprocessing on actions. Python for example will have to fix the indention.
-