|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--antlr.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 | |
protected LLkGrammarAnalyzer |
analyzer
The LLk analyzer |
protected Tool |
antlrTool
|
protected DefineGrammarSymbols |
behavior
The grammar behavior |
protected static int |
BITSET_OPTIMIZE_INIT_THRESHOLD
If there are more than 8 long words to init in a bitset, try to optimize it; e.g., detect runs of -1L and 0L. |
protected Vector |
bitsetsUsed
List of all bitsets that must be dumped. |
protected int |
bitsetTestThreshold
This is a hint for the language-specific code generator. |
protected CharFormatter |
charFormatter
Object used to format characters in the target language. |
protected PrintWriter |
currentOutput
Current output Stream |
protected boolean |
DEBUG_CODE_GENERATOR
Use option "codeGenDebug" to generate debugging output |
protected static int |
DEFAULT_BITSET_TEST_THRESHOLD
|
protected static int |
DEFAULT_MAKE_SWITCH_THRESHOLD
Default values for code-generation thresholds |
protected Grammar |
grammar
The grammar for which we generate code |
protected int |
makeSwitchThreshold
This is a hint for the language-specific code generator. |
protected int |
tabs
Current tab indentation for code output |
static String |
TokenTypesFileExt
|
static String |
TokenTypesFileSuffix
|
Constructor Summary | |
CodeGenerator()
Construct code generator base class |
Method Summary | |
protected void |
_print(String s)
Output a String to the currentOutput stream. |
protected void |
_printAction(String s)
Print an action without leading tabs, attempting to preserve the current indentation level for multi-line actions Ignored if string is null. |
protected void |
_println(String s)
Output a String followed by newline, to the currentOutput stream. |
static String |
decodeLexerRuleName(String id)
|
static boolean |
elementsAreRange(int[] elems)
Test if a set element array represents a contiguous range. |
static String |
encodeLexerRuleName(String id)
|
protected String |
extractIdOfAction(String s,
int line,
int column)
Get the identifier portion of an argument-action. |
protected String |
extractIdOfAction(Token t)
Get the identifier portion of an argument-action token. |
protected String |
extractTypeOfAction(String s,
int line,
int column)
Get the type portion of an argument-action. |
protected String |
extractTypeOfAction(Token t)
Get the type string out of an argument-action token. |
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. |
protected void |
genTokenInterchange(antlr.TokenManager tm)
Generate the token types as a text file for persistence across shared lexer/parser |
abstract String |
getASTCreateString(antlr.GrammarAtom atom,
String str)
Get a string for an expression to generate creating of an AST node |
abstract String |
getASTCreateString(Vector v)
Get a string for an expression to generate creation of an AST subtree. |
protected String |
getBitsetName(int index)
Given the index of a bitset in the bitset list, generate a unique name. |
String |
getFIRSTBitSet(String ruleName,
int k)
|
String |
getFOLLOWBitSet(String ruleName,
int k)
|
abstract String |
mapTreeId(String id,
ActionTransInfo tInfo)
Map an identifier to it's corresponding tree-node variable. |
protected int |
markBitsetForGen(BitSet p)
Add a bitset to the list of bitsets to be generated. |
protected void |
print(String s)
Output tab indent followed by a String, to the currentOutput stream. |
protected void |
printAction(String s)
Print an action with leading tabs, attempting to preserve the current indentation level for multi-line actions Ignored if string is null. |
protected void |
println(String s)
Output tab indent followed by a String followed by newline, to the currentOutput stream. |
protected void |
printTabs()
Output the current tab indentation. |
protected abstract String |
processActionForSpecialSymbols(String actionStr,
int line,
RuleBlock currentRule,
ActionTransInfo tInfo)
Lexically process $ and # references within the action. |
String |
processStringForASTConstructor(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. |
protected String |
removeAssignmentFromDeclaration(String d)
Remove the assignment portion of a declaration, if any. |
static String |
reverseLexerRuleName(String id)
|
void |
setAnalyzer(LLkGrammarAnalyzer analyzer_)
|
void |
setBehavior(DefineGrammarSymbols behavior_)
|
protected void |
setGrammar(Grammar g)
Set a grammar for the code generator to use |
void |
setTool(Tool tool)
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected Tool antlrTool
protected int tabs
protected transient PrintWriter currentOutput
protected Grammar grammar
protected Vector bitsetsUsed
protected DefineGrammarSymbols behavior
protected LLkGrammarAnalyzer analyzer
protected CharFormatter charFormatter
protected boolean DEBUG_CODE_GENERATOR
protected static final int DEFAULT_MAKE_SWITCH_THRESHOLD
protected static final int DEFAULT_BITSET_TEST_THRESHOLD
protected static final int BITSET_OPTIMIZE_INIT_THRESHOLD
protected int makeSwitchThreshold
protected int bitsetTestThreshold
public static String TokenTypesFileSuffix
public static String TokenTypesFileExt
Constructor Detail |
public CodeGenerator()
Method Detail |
protected void _print(String s)
s
- The string to outputprotected void _printAction(String s)
s
- The action string to outputprotected void _println(String s)
s
- The string to outputpublic static boolean elementsAreRange(int[] elems)
elems
- The array of elements representing the set, usually from BitSet.toArray().
protected String extractIdOfAction(Token t)
t
- The action token
protected String extractIdOfAction(String s, int line, int column)
s
- The action textline
- Line used for error reporting.column
- Line used for error reporting.
protected String extractTypeOfAction(Token t)
t
- The action token
protected String extractTypeOfAction(String s, int line, int column)
s
- The action textline
- Line used for error reporting.
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 IOException
IOException
public abstract void gen(antlr.OneOrMoreBlock blk)
blk
- The (...)+ block to generatepublic abstract void gen(antlr.ParserGrammar g) throws IOException
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 IOException
IOException
public abstract void gen(antlr.WildcardElement wc)
wc
- The wildcard element to generatepublic abstract void gen(antlr.ZeroOrMoreBlock blk)
blk
- The (...)* block to generateprotected void genTokenInterchange(antlr.TokenManager tm) throws IOException
IOException
public String processStringForASTConstructor(String str)
str
- A String.public abstract 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 String getASTCreateString(antlr.GrammarAtom atom, String str)
str
- The text of the arguments to the AST constructionprotected String getBitsetName(int index)
index
- The index of the bitset in the bitset list.public static String encodeLexerRuleName(String id)
public static String decodeLexerRuleName(String id)
public abstract String mapTreeId(String id, ActionTransInfo tInfo)
id
- The identifier name to map
protected int markBitsetForGen(BitSet p)
p
- Bit set to mark for code generation
protected void print(String s)
s
- The string to output.protected void printAction(String s)
s
- The action string to outputprotected void println(String s)
s
- The string to outputprotected void printTabs()
protected abstract String processActionForSpecialSymbols(String actionStr, int line, RuleBlock currentRule, ActionTransInfo tInfo)
public String getFOLLOWBitSet(String ruleName, int k)
public String getFIRSTBitSet(String ruleName, int k)
protected String removeAssignmentFromDeclaration(String d)
d
- the declaration
public static String reverseLexerRuleName(String id)
public void setAnalyzer(LLkGrammarAnalyzer analyzer_)
public void setBehavior(DefineGrammarSymbols behavior_)
protected void setGrammar(Grammar g)
public void setTool(Tool tool)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |