|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.antlr.codegen.CodeGenerator
public class CodeGenerator
ANTLR's code generator. Generate recognizers derived from grammars. Language independence achieved through the use of STGroup objects. All output strings are completely encapsulated in the group files such as Java.stg. Some computations are done that are unused by a particular language. This generator just computes and sets the values into the templates; the templates are free to use or not use the information. To make a new code generation target, define X.stg for language X by copying from existing Y.stg most closely releated to your language; e.g., to do CSharp.stg copy Java.stg. The template group file has a bunch of templates that are needed by the code generator. You can add a new target w/o even recompiling ANTLR itself. The language=X option in a grammar file dictates which templates get loaded/used. Some language like C need both parser files and header files. Java needs to have a separate file for the cyclic DFA as ANTLR generates bytecodes directly (which cannot be in the generated parser Java file). To facilitate this, cyclic can be in same file, but header, output must be searpate. recognizer is in outptufile.
| Field Summary | |
|---|---|
ACyclicDFACodeGenerator |
acyclicDFAGenerator
I have factored out the generation of acyclic DFAs to separate class |
protected org.stringtemplate.v4.STGroup |
baseTemplates
The basic output templates without AST or templates stuff; this will be the templates loaded for the language such as Java.stg *and* the Dbg stuff if turned on. |
static String |
classpathTemplateRootDirectoryName
|
protected boolean |
debug
Generate debugging event method calls |
boolean |
GENERATE_SWITCHES_WHEN_POSSIBLE
|
Grammar |
grammar
Which grammar are we generating code for? Each generator is attached to a specific grammar. |
protected org.stringtemplate.v4.ST |
headerFileST
|
protected String |
language
What language are we generating? |
static boolean |
LAUNCH_ST_INSPECTOR
|
protected int |
lineWidth
|
static int |
MADSI_DEFAULT
|
static int |
MAX_ACYCLIC_DFA_STATES_INLINE
|
static int |
MAX_SWITCH_CASE_LABELS
|
static int |
MIN_SWITCH_ALTS
|
static int |
MSA_DEFAULT
|
static int |
MSCL_DEFAULT
When generating SWITCH statements, some targets might need to limit the size (based upon the number of case labels). |
protected org.stringtemplate.v4.ST |
outputFileST
|
protected boolean |
profile
Track runtime parsing information about decisions etc... |
protected org.stringtemplate.v4.ST |
recognizerST
|
Target |
target
The target specifies how to write out files and do other language specific actions. |
protected org.stringtemplate.v4.STGroup |
templates
Where are the templates this generator should use to generate code? |
protected Tool |
tool
A reference to the ANTLR tool so we can learn about output directories and such. |
protected boolean |
trace
Create a Tracer object and make the recognizer invoke this. |
protected int |
uniqueLabelNumber
Used to create unique labels |
static String |
VOCAB_FILE_EXTENSION
I have factored out the generation of cyclic DFAs to separate class |
protected static String |
vocabFilePattern
|
| Constructor Summary | |
|---|---|
CodeGenerator(Tool tool,
Grammar grammar,
String language)
|
|
| Method Summary | |
|---|---|
protected boolean |
canGenerateSwitch(DFAState s)
You can generate a switch rather than if-then-else for a DFA state if there are no semantic predicates and the number of edge label values is small enough; e.g., don't generate a switch for a state containing an edge label such as 20..52330 (the resulting byte codes would overflow the method 65k limit probably). |
String |
createUniqueLabel(String name)
Create a label to track a token / rule reference's result. |
void |
generateLocalFOLLOW(GrammarAST referencedElementNode,
String referencedElementName,
String enclosingRuleName,
int elementIndex)
Error recovery in ANTLR recognizers. |
org.stringtemplate.v4.ST |
generateSpecialState(DFAState s)
A special state is huge (too big for state tables) or has a predicated edge. |
protected org.stringtemplate.v4.ST |
genLabelExpr(org.stringtemplate.v4.STGroup templates,
Transition edge,
int k)
Generate an expression for traversing an edge. |
org.stringtemplate.v4.ST |
genLookaheadDecision(org.stringtemplate.v4.ST recognizerST,
DFA dfa)
Generate code that computes the predicted alt given a DFA. |
org.stringtemplate.v4.ST |
genRecognizer()
Given the grammar to which we are attached, walk the AST associated with that grammar to create NFAs. |
protected org.stringtemplate.v4.ST |
genSemanticPredicateExpr(org.stringtemplate.v4.STGroup templates,
Transition edge)
|
org.stringtemplate.v4.ST |
genSetExpr(org.stringtemplate.v4.STGroup templates,
IntSet set,
int k,
boolean partOfDFA)
For intervals such as [3..3, 30..35], generate an expression that tests the lookahead similar to LA(1)==3 || (LA(1)>=30&&LA(1)<=35) |
protected void |
genTokenTypeConstants(org.stringtemplate.v4.ST code)
Set attributes tokens and literals attributes in the incoming code template. |
protected void |
genTokenTypeNames(org.stringtemplate.v4.ST code)
Generate a token names table that maps token type to a printable name: either the label like INT or the literal like "begin". |
protected org.stringtemplate.v4.ST |
genTokenVocabOutput()
Generate a token vocab file with all the token names/types. |
org.stringtemplate.v4.STGroup |
getBaseTemplates()
|
static List<String> |
getListOfArgumentsFromAction(String actionText,
int separatorChar)
|
static int |
getListOfArgumentsFromAction(String actionText,
int start,
int targetChar,
int separatorChar,
List<String> args)
Given an arg action like [x, (*a).foo(21,33), 3.2+1, '\n', "a,oo\nick", {bl, "fdkj"eck}, ["cat\n,", x, 43]] convert to a list of arguments. |
String |
getRecognizerFileName(String name,
int type)
Generate TParser.java and TLexer.java from T.g if combined, else just use T.java as output regardless of type. |
org.stringtemplate.v4.ST |
getRecognizerST()
|
org.stringtemplate.v4.STGroup |
getTemplates()
|
String |
getTokenTypeAsTargetLabel(int ttype)
Get a meaningful name for a token type useful during code generation. |
String |
getVocabFileName()
What is the name of the vocab file generated for this grammar? Returns null if no .tokens file should be generated. |
void |
issueInvalidAttributeError(String x,
Rule enclosingRule,
Token actionToken,
int outerAltNum)
|
void |
issueInvalidAttributeError(String x,
String y,
Rule enclosingRule,
Token actionToken,
int outerAltNum)
|
void |
issueInvalidScopeError(String x,
String y,
Rule enclosingRule,
Token actionToken,
int outerAltNum)
|
static Target |
loadLanguageTarget(String language)
|
void |
loadTemplates(String language)
load the main language.stg template group file |
void |
setDebug(boolean debug)
|
void |
setProfile(boolean profile)
|
void |
setTrace(boolean trace)
|
List |
translateAction(String ruleName,
GrammarAST actionTree)
|
protected void |
translateActionAttributeReferences(Map actions)
Actions may reference $x::y attributes, call translateAction on each action and replace that action in the Map. |
void |
translateActionAttributeReferencesForSingleScope(Rule r,
Map scopeActions)
Use for translating rule @init{...} actions that have no scope |
List<org.stringtemplate.v4.ST> |
translateArgAction(String ruleName,
GrammarAST actionTree)
Translate an action like [3,"foo",a[3]] and return a List of the translated actions. |
org.stringtemplate.v4.ST |
translateTemplateConstructor(String ruleName,
int outerAltNum,
Token actionToken,
String templateActionText)
Given a template constructor action like %foo(a={...}) in an action, translate it to the appropriate template constructor from the templateLib. |
protected void |
verifyActionScopesOkForTarget(Map actions)
Some targets will have some extra scopes like C++ may have '@headerfile:name {action}' or something. |
void |
write(org.stringtemplate.v4.ST code,
String fileName)
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final int MSCL_DEFAULT
public static int MAX_SWITCH_CASE_LABELS
public static final int MSA_DEFAULT
public static int MIN_SWITCH_ALTS
public boolean GENERATE_SWITCHES_WHEN_POSSIBLE
public static boolean LAUNCH_ST_INSPECTOR
public static final int MADSI_DEFAULT
public static int MAX_ACYCLIC_DFA_STATES_INLINE
public static String classpathTemplateRootDirectoryName
public Grammar grammar
protected String language
public Target target
protected org.stringtemplate.v4.STGroup templates
protected org.stringtemplate.v4.STGroup baseTemplates
protected org.stringtemplate.v4.ST recognizerST
protected org.stringtemplate.v4.ST outputFileST
protected org.stringtemplate.v4.ST headerFileST
protected int uniqueLabelNumber
protected Tool tool
protected boolean debug
protected boolean trace
protected boolean profile
protected int lineWidth
public ACyclicDFACodeGenerator acyclicDFAGenerator
public static final String VOCAB_FILE_EXTENSION
protected static final String vocabFilePattern
| Constructor Detail |
|---|
public CodeGenerator(Tool tool,
Grammar grammar,
String language)
| Method Detail |
|---|
public static Target loadLanguageTarget(String language)
public void loadTemplates(String language)
public org.stringtemplate.v4.ST genRecognizer()
protected void verifyActionScopesOkForTarget(Map actions)
protected void translateActionAttributeReferences(Map actions)
public void translateActionAttributeReferencesForSingleScope(Rule r,
Map scopeActions)
public void generateLocalFOLLOW(GrammarAST referencedElementNode,
String referencedElementName,
String enclosingRuleName,
int elementIndex)
public org.stringtemplate.v4.ST genLookaheadDecision(org.stringtemplate.v4.ST recognizerST,
DFA dfa)
public org.stringtemplate.v4.ST generateSpecialState(DFAState s)
protected org.stringtemplate.v4.ST genLabelExpr(org.stringtemplate.v4.STGroup templates,
Transition edge,
int k)
protected org.stringtemplate.v4.ST genSemanticPredicateExpr(org.stringtemplate.v4.STGroup templates,
Transition edge)
public org.stringtemplate.v4.ST genSetExpr(org.stringtemplate.v4.STGroup templates,
IntSet set,
int k,
boolean partOfDFA)
protected void genTokenTypeConstants(org.stringtemplate.v4.ST code)
protected void genTokenTypeNames(org.stringtemplate.v4.ST code)
public String getTokenTypeAsTargetLabel(int ttype)
protected org.stringtemplate.v4.ST genTokenVocabOutput()
public List translateAction(String ruleName,
GrammarAST actionTree)
public List<org.stringtemplate.v4.ST> translateArgAction(String ruleName,
GrammarAST actionTree)
public static List<String> getListOfArgumentsFromAction(String actionText,
int separatorChar)
public static int getListOfArgumentsFromAction(String actionText,
int start,
int targetChar,
int separatorChar,
List<String> args)
public org.stringtemplate.v4.ST translateTemplateConstructor(String ruleName,
int outerAltNum,
Token actionToken,
String templateActionText)
public void issueInvalidScopeError(String x,
String y,
Rule enclosingRule,
Token actionToken,
int outerAltNum)
public void issueInvalidAttributeError(String x,
String y,
Rule enclosingRule,
Token actionToken,
int outerAltNum)
public void issueInvalidAttributeError(String x,
Rule enclosingRule,
Token actionToken,
int outerAltNum)
public org.stringtemplate.v4.STGroup getTemplates()
public org.stringtemplate.v4.STGroup getBaseTemplates()
public void setDebug(boolean debug)
public void setTrace(boolean trace)
public void setProfile(boolean profile)
public org.stringtemplate.v4.ST getRecognizerST()
public String getRecognizerFileName(String name,
int type)
public String getVocabFileName()
public void write(org.stringtemplate.v4.ST code,
String fileName)
throws IOException
IOExceptionprotected boolean canGenerateSwitch(DFAState s)
public String createUniqueLabel(String name)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||