| Modifier and Type | Field and Description |
|---|---|
| public ACyclicDFACodeGenerator | acyclicDFAGenerator
I have factored out the generation of acyclic DFAs to separate class |
| protected 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. |
| public static String | |
| protected boolean | debug
Generate debugging event method calls |
| public boolean | |
| public Grammar | grammar
Which grammar are we generating code for? |
| protected ST | |
| protected String | language
What language are we generating? |
| public static boolean | |
| protected int | |
| public static final int | |
| public static int | |
| public static int | |
| public static int | |
| public static final int | |
| public static final int | MSCL_DEFAULT
When generating SWITCH statements, some targets might need to limit the size (based upon the number of case labels). |
| protected ST | |
| protected boolean | profile
Track runtime parsing information about decisions etc... |
| protected ST | |
| public Target | target
The target specifies how to write out files and do other language specific actions. |
| protected 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 |
| public static final String | VOCAB_FILE_EXTENSION
I have factored out the generation of cyclic DFAs to separate class |
| protected static final String |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| 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). |
| public String | |
| public void | generateLocalFOLLOW(GrammarAST referencedElementNode, String referencedElementName, String enclosingRuleName, int elementIndex)
Error recovery in ANTLR recognizers. |
| public ST | generateSpecialState(DFAState s)
A special state is huge (too big for state tables) or has a predicated edge. |
| protected ST | genLabelExpr(STGroup templates, Transition edge, int k)
Generate an expression for traversing an edge. |
| public ST | genLookaheadDecision(ST recognizerST, DFA dfa)
Generate code that computes the predicted alt given a DFA. |
| public ST | genRecognizer()
Given the grammar to which we are attached, walk the AST associated with that grammar to create NFAs. |
| protected ST | |
| public ST | genSetExpr(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(ST code)
Set attributes tokens and literals attributes in the incoming code template. |
| protected void | genTokenTypeNames(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 ST | |
| public STGroup | |
| public static List | |
| public 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. |
| public 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. |
| public ST | |
| public STGroup | |
| public String | getTokenTypeAsTargetLabel(int ttype)
Get a meaningful name for a token type useful during code generation. |
| public String | |
| public void | issueInvalidAttributeError(String x, String y, Rule enclosingRule, Token actionToken, int outerAltNum)
|
| public void | |
| public void | |
| public static Target | |
| public void | |
| public void | |
| public void | |
| public void | |
| public List | |
| protected void | |
| public void | translateActionAttributeReferencesForSingleScope(Rule r, Map<String, Object> scopeActions)
Use for translating rule @init{...} actions that have no scope |
| public List | translateArgAction(String ruleName, GrammarAST actionTree)
Translate an action like [3,"foo",a[3]] and return a List of the translated actions. |
| public 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 | |
| public void |
| acyclicDFAGenerator | back to summary |
|---|---|
| public ACyclicDFACodeGenerator acyclicDFAGenerator I have factored out the generation of acyclic DFAs to separate class | |
| baseTemplates | back to summary |
|---|---|
| protected 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. This is used for generating syntactic predicates. | |
| classpathTemplateRootDirectoryName | back to summary |
|---|---|
| public static String classpathTemplateRootDirectoryName | |
| debug | back to summary |
|---|---|
| protected boolean debug Generate debugging event method calls | |
| GENERATE_SWITCHES_WHEN_POSSIBLE | back to summary |
|---|---|
| public boolean GENERATE_SWITCHES_WHEN_POSSIBLE | |
| grammar | back to summary |
|---|---|
| public Grammar grammar Which grammar are we generating code for? Each generator is attached to a specific grammar. | |
| headerFileST | back to summary |
|---|---|
| protected ST headerFileST | |
| language | back to summary |
|---|---|
| protected String language What language are we generating? | |
| LAUNCH_ST_INSPECTOR | back to summary |
|---|---|
| public static boolean LAUNCH_ST_INSPECTOR | |
| lineWidth | back to summary |
|---|---|
| protected int lineWidth | |
| MADSI_DEFAULT | back to summary |
|---|---|
| public static final int MADSI_DEFAULT | |
| MAX_ACYCLIC_DFA_STATES_INLINE | back to summary |
|---|---|
| public static int MAX_ACYCLIC_DFA_STATES_INLINE | |
| MAX_SWITCH_CASE_LABELS | back to summary |
|---|---|
| public static int MAX_SWITCH_CASE_LABELS | |
| MIN_SWITCH_ALTS | back to summary |
|---|---|
| public static int MIN_SWITCH_ALTS | |
| MSA_DEFAULT | back to summary |
|---|---|
| public static final int MSA_DEFAULT | |
| MSCL_DEFAULT | back to summary |
|---|---|
| public static final int MSCL_DEFAULT When generating SWITCH statements, some targets might need to limit the size (based upon the number of case labels). Generally, this limit will be hit only for lexers where wildcard in a UNICODE vocabulary environment would generate a SWITCH with 65000 labels. | |
| outputFileST | back to summary |
|---|---|
| protected ST outputFileST | |
| profile | back to summary |
|---|---|
| protected boolean profile Track runtime parsing information about decisions etc... This requires the debugging event mechanism to work. | |
| recognizerST | back to summary |
|---|---|
| protected ST recognizerST | |
| target | back to summary |
|---|---|
| public Target target The target specifies how to write out files and do other language specific actions. | |
| templates | back to summary |
|---|---|
| protected STGroup templates Where are the templates this generator should use to generate code? | |
| tool | back to summary |
|---|---|
| protected Tool tool A reference to the ANTLR tool so we can learn about output directories and such. | |
| trace | back to summary |
|---|---|
| protected boolean trace Create a Tracer object and make the recognizer invoke this. | |
| uniqueLabelNumber | back to summary |
|---|---|
| protected int uniqueLabelNumber Used to create unique labels | |
| VOCAB_FILE_EXTENSION | back to summary |
|---|---|
| public static final String VOCAB_FILE_EXTENSION I have factored out the generation of cyclic DFAs to separate class | |
| vocabFilePattern | back to summary |
|---|---|
| protected static final String vocabFilePattern | |
| CodeGenerator | back to summary |
|---|---|
| public CodeGenerator(Tool tool, Grammar grammar, String language) | |
| canGenerateSwitch | back to 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). | |
| createUniqueLabel | back to summary |
|---|---|
| public String createUniqueLabel(String name) Create a label to track a token / rule reference's result. Technically, this is a place where I break model-view separation as I am creating a variable name that could be invalid in a target language, however, label ::= <ID><INT> is probably ok in all languages we care about. | |
| generateLocalFOLLOW | back to summary |
|---|---|
| public void generateLocalFOLLOW(GrammarAST referencedElementNode, String referencedElementName, String enclosingRuleName, int elementIndex) Error recovery in ANTLR recognizers. Based upon original ideas: Algorithms + Data Structures = Programs by Niklaus Wirth and A note on error recovery in recursive descent parsers: http://portal.acm.org/citation.cfm?id=947902.947905 Later, Josef Grosch had some good ideas: Efficient and Comfortable Error Recovery in Recursive Descent Parsers: ftp://www.cocolab.com/products/cocktail/doca4.ps/ell.ps.zip Like Grosch I implemented local FOLLOW sets that are combined at run-time upon error to avoid parsing overhead. | |
| generateSpecialState | back to summary |
|---|---|
| public ST generateSpecialState(DFAState s) A special state is huge (too big for state tables) or has a predicated edge. Generate a simple if-then-else. Cannot be an accept state as they have no emanating edges. Don't worry about switch vs if-then-else because if you get here, the state is super complicated and needs an if-then-else. This is used by the new DFA scheme created June 2006. | |
| genLabelExpr | back to summary |
|---|---|
| protected ST genLabelExpr(STGroup templates, Transition edge, int k) Generate an expression for traversing an edge. | |
| genLookaheadDecision | back to summary |
|---|---|
| public ST genLookaheadDecision(ST recognizerST, DFA dfa) Generate code that computes the predicted alt given a DFA. The recognizerST can be either the main generated recognizerTemplate for storage in the main parser file or a separate file. It's up to the code that ultimately invokes the codegen.g grammar rule. Regardless, the output file and header file get a copy of the DFAs. | |
| genRecognizer | back to summary |
|---|---|
| public ST genRecognizer() Given the grammar to which we are attached, walk the AST associated with that grammar to create NFAs. Then create the DFAs for all decision points in the grammar by converting the NFAs to DFAs. Finally, walk the AST again to generate code. Either 1 or 2 files are written: recognizer: the main parser/lexer/treewalker item header file: language like C/C++ need extern definitions The target, such as JavaTarget, dictates which files get written. | |
| genSemanticPredicateExpr | back to summary |
|---|---|
| protected ST genSemanticPredicateExpr(STGroup templates, Transition edge) | |
| genSetExpr | back to summary |
|---|---|
| public ST genSetExpr(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) | |
| genTokenTypeConstants | back to summary |
|---|---|
| protected void genTokenTypeConstants(ST code) Set attributes tokens and literals attributes in the incoming code template. This is not the token vocab interchange file, but rather a list of token type ID needed by the recognizer. | |
| genTokenTypeNames | back to summary |
|---|---|
| protected void genTokenTypeNames(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". | |
| genTokenVocabOutput | back to summary |
|---|---|
| protected ST genTokenVocabOutput() Generate a token vocab file with all the token names/types. For example: ID=7 FOR=8 'for'=8 This is independent of the target language; used by antlr internally | |
| getBaseTemplates | back to summary |
|---|---|
| public STGroup getBaseTemplates() | |
| getListOfArgumentsFromAction | back to summary |
|---|---|
| public static List | |
| getListOfArgumentsFromAction | back to summary |
|---|---|
| public 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. Allow nested square brackets etc... Set separatorChar to ';' or ',' or whatever you want. | |
| getRecognizerFileName | back to summary |
|---|---|
| public 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. | |
| getRecognizerST | back to summary |
|---|---|
| public ST getRecognizerST() | |
| getTemplates | back to summary |
|---|---|
| public STGroup getTemplates() | |
| getTokenTypeAsTargetLabel | back to summary |
|---|---|
| public String getTokenTypeAsTargetLabel(int ttype) Get a meaningful name for a token type useful during code generation. Literals without associated names are converted to the string equivalent of their integer values. Used to generate x==ID and x==34 type comparisons etc... Essentially we are looking for the most obvious way to refer to a token type in the generated code. If in the lexer, return the char literal translated to the target language. For example, ttype=10 will yield '\n' from the getTokenDisplayName method. That must be converted to the target languages literals. For most C-derived languages no translation is needed. | |
| getVocabFileName | back to summary |
|---|---|
| public String getVocabFileName() What is the name of the vocab file generated for this grammar? Returns null if no .tokens file should be generated. | |
| issueInvalidAttributeError | back to summary |
|---|---|
| public void issueInvalidAttributeError(String x, String y, Rule enclosingRule, Token actionToken, int outerAltNum) | |
| issueInvalidAttributeError | back to summary |
|---|---|
| public void issueInvalidAttributeError(String x, Rule enclosingRule, Token actionToken, int outerAltNum) | |
| issueInvalidScopeError | back to summary |
|---|---|
| public void issueInvalidScopeError(String x, String y, Rule enclosingRule, Token actionToken, int outerAltNum) | |
| loadLanguageTarget | back to summary |
|---|---|
| public static Target loadLanguageTarget(String language) | |
| loadTemplates | back to summary |
|---|---|
| public void loadTemplates(String language) load the main language.stg template group file | |
| setDebug | back to summary |
|---|---|
| public void setDebug(boolean debug) | |
| setProfile | back to summary |
|---|---|
| public void setProfile(boolean profile) | |
| setTrace | back to summary |
|---|---|
| public void setTrace(boolean trace) | |
| translateAction | back to summary |
|---|---|
| public List | |
| translateActionAttributeReferences | back to summary |
|---|---|
| protected void translateActionAttributeReferences(Map<String, Map<String, Object>> actions) Actions may reference $x::y attributes, call translateAction on each action and replace that action in the Map. | |
| translateActionAttributeReferencesForSingleScope | back to summary |
|---|---|
| public void translateActionAttributeReferencesForSingleScope(Rule r, Map<String, Object> scopeActions) Use for translating rule @init{...} actions that have no scope | |
| translateArgAction | back to summary |
|---|---|
| public List Translate an action like [3,"foo",a[3]] and return a List of the translated actions. Because actions are themselves translated to a list of chunks, must cat together into a ST>. Don't translate to strings early as we need to eval templates in context. | |
| translateTemplateConstructor | back to summary |
|---|---|
| public 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. This translates a *piece* of the action. | |
| verifyActionScopesOkForTarget | back to summary |
|---|---|
| protected void verifyActionScopesOkForTarget(Map<String, Map<String, Object>> actions) Some targets will have some extra scopes like C++ may have '@headerfile:name {action}' or something. Make sure the target likes the scopes in action table. | |
| write | back to summary |
|---|---|
| public void write(ST code, String fileName) throws IOException | |