java.lang.Objectantlr.CodeGenerator
A generic ANTLR code generator. All code generators Derive from this class.
A CodeGenerator knows about a Grammar data structure and a grammar analyzer. The Grammar is walked to generate the appropriate code for both a parser and lexer (if present). This interface may change slightly so that the lexer is itself living inside of a Grammar object (in which case, this class generates only one recognizer). The main method to call is gen(), which initiates all code gen.
The interaction of the code generator with the analyzer is simple: each subrule block calls deterministic() before generating code for the block. Method deterministic() sets lookahead caches in each Alternative object. Technically, a code generator doesn't need the grammar analyzer if all lookahead analysis is done at runtime, but this would result in a slower parser.
This class provides a set of support utilities to handle argument list parsing and so on.
JavaCodeGenerator
,
DiagnosticCodeGenerator
,
LLkAnalyzer
,
Grammar
,
AlternativeElement
,
Lookahead
Field Summary | |
static java.lang.String |
TokenTypesFileExt
|
static java.lang.String |
TokenTypesFileSuffix
|
Constructor Summary | |
CodeGenerator()
Construct code generator base class |
Method Summary | |
static java.lang.String |
decodeLexerRuleName(java.lang.String id)
|
static boolean |
elementsAreRange(int[] elems)
Test if a set element array represents a contiguous range. |
static java.lang.String |
encodeLexerRuleName(java.lang.String id)
|
abstract void |
gen()
Generate the code for all grammars |
abstract void |
gen(antlr.ActionElement action)
Generate code for the given grammar element. |
abstract void |
gen(antlr.AlternativeBlock blk)
Generate code for the given grammar element. |
abstract void |
gen(antlr.BlockEndElement end)
Generate code for the given grammar element. |
abstract void |
gen(antlr.CharLiteralElement atom)
Generate code for the given grammar element. |
abstract void |
gen(antlr.CharRangeElement r)
Generate code for the given grammar element. |
abstract void |
gen(antlr.LexerGrammar g)
Generate the code for a parser |
abstract void |
gen(antlr.OneOrMoreBlock blk)
Generate code for the given grammar element. |
abstract void |
gen(antlr.ParserGrammar g)
Generate the code for a parser |
abstract void |
gen(antlr.RuleRefElement rr)
Generate code for the given grammar element. |
abstract void |
gen(antlr.StringLiteralElement atom)
Generate code for the given grammar element. |
abstract void |
gen(antlr.TokenRangeElement r)
Generate code for the given grammar element. |
abstract void |
gen(antlr.TokenRefElement atom)
Generate code for the given grammar element. |
abstract void |
gen(antlr.TreeElement t)
Generate code for the given grammar element. |
abstract void |
gen(antlr.TreeWalkerGrammar g)
Generate the code for a parser |
abstract void |
gen(antlr.WildcardElement wc)
Generate code for the given grammar element. |
abstract void |
gen(antlr.ZeroOrMoreBlock blk)
Generate code for the given grammar element. |
abstract java.lang.String |
getASTCreateString(antlr.GrammarAtom atom,
java.lang.String str)
Get a string for an expression to generate creating of an AST node |
abstract java.lang.String |
getASTCreateString(Vector v)
Get a string for an expression to generate creation of an AST subtree. |
java.lang.String |
getFIRSTBitSet(java.lang.String ruleName,
int k)
|
java.lang.String |
getFOLLOWBitSet(java.lang.String ruleName,
int k)
|
abstract java.lang.String |
mapTreeId(java.lang.String id,
ActionTransInfo tInfo)
Map an identifier to it's corresponding tree-node variable. |
java.lang.String |
processStringForASTConstructor(java.lang.String str)
Process a string for an simple expression for use in xx/action.g it is used to cast simple tokens/references to the right type for the generated language. |
static java.lang.String |
reverseLexerRuleName(java.lang.String id)
|
void |
setAnalyzer(LLkGrammarAnalyzer analyzer_)
|
void |
setBehavior(DefineGrammarSymbols behavior_)
|
void |
setTool(Tool tool)
|
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static java.lang.String TokenTypesFileSuffix
public static java.lang.String TokenTypesFileExt
Constructor Detail |
public CodeGenerator()
Method Detail |
public static boolean elementsAreRange(int[] elems)
elems
- The array of elements representing the set, usually from BitSet.toArray().
public abstract void gen()
public abstract void gen(antlr.ActionElement action)
action
- The {...} action to generatepublic abstract void gen(antlr.AlternativeBlock blk)
blk
- The "x|y|z|..." block to generatepublic abstract void gen(antlr.BlockEndElement end)
end
- The block-end element to generate. Block-end
elements are synthesized by the grammar parser to represent
the end of a block.public abstract void gen(antlr.CharLiteralElement atom)
atom
- The character literal reference to generatepublic abstract void gen(antlr.CharRangeElement r)
r
- The character-range reference to generatepublic abstract void gen(antlr.LexerGrammar g) throws java.io.IOException
java.io.IOException
public abstract void gen(antlr.OneOrMoreBlock blk)
blk
- The (...)+ block to generatepublic abstract void gen(antlr.ParserGrammar g) throws java.io.IOException
java.io.IOException
public abstract void gen(antlr.RuleRefElement rr)
rr
- The rule-reference to generatepublic abstract void gen(antlr.StringLiteralElement atom)
atom
- The string-literal reference to generatepublic abstract void gen(antlr.TokenRangeElement r)
r
- The token-range reference to generatepublic abstract void gen(antlr.TokenRefElement atom)
atom
- The token-reference to generatepublic abstract void gen(antlr.TreeElement t)
public abstract void gen(antlr.TreeWalkerGrammar g) throws java.io.IOException
java.io.IOException
public abstract void gen(antlr.WildcardElement wc)
wc
- The wildcard element to generatepublic abstract void gen(antlr.ZeroOrMoreBlock blk)
blk
- The (...)* block to generatepublic java.lang.String processStringForASTConstructor(java.lang.String str)
str
- A String.public abstract java.lang.String getASTCreateString(Vector v)
v
- A Vector of String, where each element is an expression in the target language yielding an AST node.public abstract java.lang.String getASTCreateString(antlr.GrammarAtom atom, java.lang.String str)
str
- The text of the arguments to the AST constructionpublic static java.lang.String encodeLexerRuleName(java.lang.String id)
public static java.lang.String decodeLexerRuleName(java.lang.String id)
public abstract java.lang.String mapTreeId(java.lang.String id, ActionTransInfo tInfo)
id
- The identifier name to map
public java.lang.String getFOLLOWBitSet(java.lang.String ruleName, int k)
public java.lang.String getFIRSTBitSet(java.lang.String ruleName, int k)
public static java.lang.String reverseLexerRuleName(java.lang.String id)
public void setAnalyzer(LLkGrammarAnalyzer analyzer_)
public void setBehavior(DefineGrammarSymbols behavior_)
public void setTool(Tool tool)