antlr
Class CodeGenerator

java.lang.Object
  |
  +--antlr.CodeGenerator
Direct Known Subclasses:
CppCodeGenerator, DiagnosticCodeGenerator, HTMLCodeGenerator, JavaCodeGenerator, SatherCodeGenerator

public abstract class CodeGenerator
extends Object

Untamed:


Field Summary
protected  LLkGrammarAnalyzer analyzer
          The LLk analyzer
protected  DefineGrammarSymbols behavior
          The grammar behavior
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.
private static boolean OLD_ACTION_TRANSLATOR
           
protected  int tabs
          Current tab indentation for code output
static String TokenTypesFileExt
          Enabled:
static String TokenTypesFileSuffix
          Enabled:
protected  Tool tool
          The antlr Tool
 
Constructor Summary
CodeGenerator()
          Enabled: 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 boolean elementsAreRange(int[] elems)
          Enabled: Test if a set element array represents a contiguous range.
protected  String extractIdOfAction(String s, int line)
          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)
          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()
          Enabled: Generate the code for all grammars
abstract  void gen(ActionElement action)
          Enabled: Generate code for the given grammar element.
abstract  void gen(AlternativeBlock blk)
          Enabled: Generate code for the given grammar element.
abstract  void gen(BlockEndElement end)
          Enabled: Generate code for the given grammar element.
abstract  void gen(CharLiteralElement atom)
          Enabled: Generate code for the given grammar element.
abstract  void gen(CharRangeElement r)
          Enabled: Generate code for the given grammar element.
abstract  void gen(LexerGrammar g)
          Enabled: Generate the code for a parser
abstract  void gen(OneOrMoreBlock blk)
          Enabled: Generate code for the given grammar element.
abstract  void gen(ParserGrammar g)
          Enabled: Generate the code for a parser
abstract  void gen(RuleRefElement rr)
          Enabled: Generate code for the given grammar element.
abstract  void gen(StringLiteralElement atom)
          Enabled: Generate code for the given grammar element.
abstract  void gen(TokenRangeElement r)
          Enabled: Generate code for the given grammar element.
abstract  void gen(TokenRefElement atom)
          Enabled: Generate code for the given grammar element.
abstract  void gen(TreeElement t)
          Enabled: Generate code for the given grammar element.
abstract  void gen(TreeWalkerGrammar g)
          Enabled: Generate the code for a parser
abstract  void gen(WildcardElement wc)
          Enabled: Generate code for the given grammar element.
abstract  void gen(ZeroOrMoreBlock blk)
          Enabled: Generate code for the given grammar element.
protected  void genTokenInterchange(TokenManager tm)
          Generate the token types as a text file for persistence across shared lexer/parser
abstract  String getASTCreateString(GrammarAtom atom, String str)
          Enabled: Get a string for an expression to generate creating of an AST node
abstract  String getASTCreateString(Vector v)
          Enabled: 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.
static String lexerRuleName(String id)
          Enabled:
abstract  String mapTreeId(String id, ActionTransInfo tInfo)
          Enabled: 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  String processActionForTreeSpecifiers(String actionStr, int line, RuleBlock currentRule, ActionTransInfo tInfo)
          Lexically process tree-specifiers in the action.
 String processStringForASTConstructor(String str)
          Enabled: 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.
private  void reset()
          Set all fields back like one just created
static String reverseLexerRuleName(String id)
          Enabled:
 void setAnalyzer(LLkGrammarAnalyzer analyzer_)
          Enabled:
 void setBehavior(DefineGrammarSymbols behavior_)
          Enabled:
protected  void setGrammar(Grammar g)
          Set a grammar for the code generator to use
 void setTool(Tool tool_)
          Enabled:
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

tabs

protected int tabs
Current tab indentation for code output


currentOutput

protected transient PrintWriter currentOutput
Current output Stream


grammar

protected Grammar grammar
The grammar for which we generate code


bitsetsUsed

protected Vector bitsetsUsed
List of all bitsets that must be dumped. These are Vectors of BitSet.


tool

protected Tool tool
The antlr Tool


behavior

protected DefineGrammarSymbols behavior
The grammar behavior


analyzer

protected LLkGrammarAnalyzer analyzer
The LLk analyzer


charFormatter

protected CharFormatter charFormatter
Object used to format characters in the target language. subclass must initialize this to the language-specific formatter


DEBUG_CODE_GENERATOR

protected boolean DEBUG_CODE_GENERATOR
Use option "codeGenDebug" to generate debugging output


DEFAULT_MAKE_SWITCH_THRESHOLD

protected static final int DEFAULT_MAKE_SWITCH_THRESHOLD
Default values for code-generation thresholds


DEFAULT_BITSET_TEST_THRESHOLD

protected static final int DEFAULT_BITSET_TEST_THRESHOLD

makeSwitchThreshold

protected int makeSwitchThreshold
This is a hint for the language-specific code generator. A switch() or language-specific equivalent will be generated instead of a series of if/else statements for blocks with number of alternates greater than or equal to this number of non-predicated LL(1) alternates. This is modified by the grammar option "codeGenMakeSwitchThreshold"


bitsetTestThreshold

protected int bitsetTestThreshold
This is a hint for the language-specific code generator. A bitset membership test will be generated instead of an ORed series of LA(k) comparisions for lookahead sets with degree greater than or equal to this value. This is modified by the grammar option "codeGenBitsetTestThreshold"


OLD_ACTION_TRANSLATOR

private static boolean OLD_ACTION_TRANSLATOR

TokenTypesFileSuffix

public static String TokenTypesFileSuffix
Enabled:


TokenTypesFileExt

public static String TokenTypesFileExt
Enabled:

Constructor Detail

CodeGenerator

public CodeGenerator()
Enabled: Construct code generator base class

Method Detail

_print

protected void _print(String s)
Output a String to the currentOutput stream. Ignored if string is null.

Parameters:
s - The string to output

_printAction

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.

Parameters:
s - The action string to output

_println

protected void _println(String s)
Output a String followed by newline, to the currentOutput stream. Ignored if string is null.

Parameters:
s - The string to output

elementsAreRange

public static boolean elementsAreRange(int[] elems)
Enabled: Test if a set element array represents a contiguous range.

Parameters:
elems - The array of elements representing the set, usually from BitSet.toArray().
Returns:
true if the elements are a contiguous range (with two or more).

extractIdOfAction

protected String extractIdOfAction(Token t)
Get the identifier portion of an argument-action token. The ID of an action is assumed to be a trailing identifier. Specific code-generators may want to override this if the language has unusual declaration syntax.

Parameters:
t - The action token
Returns:
A string containing the text of the identifier

extractIdOfAction

protected String extractIdOfAction(String s,
                                   int line)
Get the identifier portion of an argument-action. The ID of an action is assumed to be a trailing identifier. Specific code-generators may want to override this if the language has unusual declaration syntax.

Parameters:
s - The action text
line - Line used for error reporting.
Returns:
A string containing the text of the identifier

extractTypeOfAction

protected String extractTypeOfAction(Token t)
Get the type string out of an argument-action token. The type of an action is assumed to precede a trailing identifier Specific code-generators may want to override this if the language has unusual declaration syntax.

Parameters:
t - The action token
Returns:
A string containing the text of the type

extractTypeOfAction

protected String extractTypeOfAction(String s,
                                     int line)
Get the type portion of an argument-action. The type of an action is assumed to precede a trailing identifier Specific code-generators may want to override this if the language has unusual declaration syntax.

Parameters:
s - The action text
line - Line used for error reporting.
Returns:
A string containing the text of the type

gen

public abstract void gen()
Enabled: Generate the code for all grammars


gen

public abstract void gen(ActionElement action)
Enabled: Generate code for the given grammar element.

Parameters:
action - The {...} action to generate

gen

public abstract void gen(AlternativeBlock blk)
Enabled: Generate code for the given grammar element.

Parameters:
blk - The "x|y|z|..." block to generate

gen

public abstract void gen(BlockEndElement end)
Enabled: Generate code for the given grammar element.

Parameters:
end - The block-end element to generate. Block-end elements are synthesized by the grammar parser to represent the end of a block.

gen

public abstract void gen(CharLiteralElement atom)
Enabled: Generate code for the given grammar element.

Parameters:
atom - The character literal reference to generate

gen

public abstract void gen(CharRangeElement r)
Enabled: Generate code for the given grammar element.

Parameters:
r - The character-range reference to generate

gen

public abstract void gen(LexerGrammar g)
                  throws IOException
Enabled: Generate the code for a parser

IOException

gen

public abstract void gen(OneOrMoreBlock blk)
Enabled: Generate code for the given grammar element.

Parameters:
blk - The (...)+ block to generate

gen

public abstract void gen(ParserGrammar g)
                  throws IOException
Enabled: Generate the code for a parser

IOException

gen

public abstract void gen(RuleRefElement rr)
Enabled: Generate code for the given grammar element.

Parameters:
rr - The rule-reference to generate

gen

public abstract void gen(StringLiteralElement atom)
Enabled: Generate code for the given grammar element.

Parameters:
atom - The string-literal reference to generate

gen

public abstract void gen(TokenRangeElement r)
Enabled: Generate code for the given grammar element.

Parameters:
r - The token-range reference to generate

gen

public abstract void gen(TokenRefElement atom)
Enabled: Generate code for the given grammar element.

Parameters:
atom - The token-reference to generate

gen

public abstract void gen(TreeElement t)
Enabled: Generate code for the given grammar element.


gen

public abstract void gen(TreeWalkerGrammar g)
                  throws IOException
Enabled: Generate the code for a parser

IOException

gen

public abstract void gen(WildcardElement wc)
Enabled: Generate code for the given grammar element.

Parameters:
wc - The wildcard element to generate

gen

public abstract void gen(ZeroOrMoreBlock blk)
Enabled: Generate code for the given grammar element.

Parameters:
blk - The (...)* block to generate

genTokenInterchange

protected void genTokenInterchange(TokenManager tm)
                            throws IOException
Generate the token types as a text file for persistence across shared lexer/parser

IOException

processStringForASTConstructor

public String processStringForASTConstructor(String str)
Enabled: 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.

Parameters:
str - A String.

getASTCreateString

public abstract String getASTCreateString(Vector v)
Enabled: Get a string for an expression to generate creation of an AST subtree.

Parameters:
v - A Vector of String, where each element is an expression in the target language yielding an AST node.

getASTCreateString

public abstract String getASTCreateString(GrammarAtom atom,
                                          String str)
Enabled: Get a string for an expression to generate creating of an AST node

Parameters:
str - The text of the arguments to the AST construction

getBitsetName

protected String getBitsetName(int index)
Given the index of a bitset in the bitset list, generate a unique name. Specific code-generators may want to override this if the language does not allow '_' or numerals in identifiers.

Parameters:
index - The index of the bitset in the bitset list.

lexerRuleName

public static String lexerRuleName(String id)
Enabled:


mapTreeId

public abstract String mapTreeId(String id,
                                 ActionTransInfo tInfo)
Enabled: Map an identifier to it's corresponding tree-node variable. This is context-sensitive, depending on the rule and alternative being generated

Parameters:
id - The identifier name to map
Returns:
The mapped id (which may be the same as the input), or null if the mapping is invalid due to duplicates

markBitsetForGen

protected int markBitsetForGen(BitSet p)
Add a bitset to the list of bitsets to be generated. if the bitset is already in the list, ignore the request. Always adds the bitset to the end of the list, so the caller can rely on the position of bitsets in the list. The returned position can be used to format the bitset name, since it is invariant.

Parameters:
p - Bit set to mark for code generation
Returns:
The position of the bitset in the list.

print

protected void print(String s)
Output tab indent followed by a String, to the currentOutput stream. Ignored if string is null.

Parameters:
s - The string to output.

printAction

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.

Parameters:
s - The action string to output

println

protected void println(String s)
Output tab indent followed by a String followed by newline, to the currentOutput stream. Ignored if string is null.

Parameters:
s - The string to output

printTabs

protected void printTabs()
Output the current tab indentation. This outputs the number of tabs indicated by the "tabs" variable to the currentOutput stream.


processActionForTreeSpecifiers

protected String processActionForTreeSpecifiers(String actionStr,
                                                int line,
                                                RuleBlock currentRule,
                                                ActionTransInfo tInfo)
Lexically process tree-specifiers in the action. This will replace #id and #(...) with the appropriate function calls and/or variables. This is the default Java action translator, but I have made it work for C++ also.


removeAssignmentFromDeclaration

protected String removeAssignmentFromDeclaration(String d)
Remove the assignment portion of a declaration, if any.

Parameters:
d - the declaration
Returns:
the declaration without any assignment portion

reset

private void reset()
Set all fields back like one just created


reverseLexerRuleName

public static String reverseLexerRuleName(String id)
Enabled:


setAnalyzer

public void setAnalyzer(LLkGrammarAnalyzer analyzer_)
Enabled:


setBehavior

public void setBehavior(DefineGrammarSymbols behavior_)
Enabled:


setGrammar

protected void setGrammar(Grammar g)
Set a grammar for the code generator to use


setTool

public void setTool(Tool tool_)
Enabled:



comments?