Top Description Fields Constructors Methods
org.antlr.tool

public Class CompositeGrammar

extends Object
Class Inheritance
Imports
org.antlr.analysis.Label, .NFAState, org.antlr.grammar.v3.ANTLRParser, .AssignTokenTypesWalker, org.antlr.misc.Utils, org.antlr.runtime.RecognitionException, org.antlr.runtime.tree.CommonTreeNodeStream, java.util.ArrayList, .HashSet, .Iterator, .LinkedHashMap, .List, .Map, .Set, .Vector

A tree of component (delegate) grammars. Rules defined in delegates are "inherited" like multi-inheritance so you can override them. All token types must be consistent across rules from all delegate grammars, so they must be stored here in one central place. We have to start out assuming a composite grammar situation as we can't look into the grammar files a priori to see if there is a delegate statement. Because of this, and to avoid duplicating token type tracking in each grammar, even single noncomposite grammars use one of these objects to track token types.

Field Summary

Modifier and TypeField and Description
public CompositeGrammarTree
protected Set<String>
lexerRules

If combined or lexer grammar, track the rules.

protected int
maxTokenType

Token names and literal tokens like "void" are uniquely indexed.

public static final int
protected Vector<NFAState>
numberToStateList

The NFA states in the NFA built from rules across grammars in composite.

protected Set<NFAState>
refClosureBusy

Used during getRuleReferenceClosure to detect computation cycles

protected int
ruleIndex

Rules are uniquely labeled from 1..n among all grammars

protected Vector<Rule>
ruleIndexToRuleList

Map a rule index to its name; use a Vector on purpose as new collections stuff won't let me setSize and make it grow.

public int
stateCounter

Used to assign state numbers; all grammars in composite share common NFA space.

public Map<String, Integer>
stringLiteralToTypeMap

Map token literals like "while" to its token type.

public Map<String, Integer>
tokenIDToTypeMap

Map token like ID (but not literals like "while") to its token type

public Vector<String>
typeToStringLiteralList

Reverse index for stringLiteralToTypeMap

public Vector<String>
typeToTokenList

Map a token type to its token name.

public boolean

Constructor Summary

AccessConstructor and Description
public
public

Method Summary

Modifier and TypeMethod and Description
public void
public void
addGrammar(Grammar delegator, Grammar delegate)

Add delegate grammar as child of delegator

public void
public void
public void
public void
public Set<? extends Rule>
getAllImportedRules(Grammar g)

Get all rule definitions from all direct/indirect delegate grammars of g.

public Set<? extends Rule>
getDelegatedRules(Grammar g)

Get set of rules for grammar g that need to have manual delegation methods.

public List<Grammar>
getDelegates(Grammar g)

Get list of all delegates from all grammars in the delegate subtree of g.

public Grammar
getDelegator(Grammar g)

Get parent of this grammar

public List<Grammar>
getDelegators(Grammar g)

Return list of delegate grammars from root down to g.

public List<Grammar>
public Grammar
getGrammar(String grammarName)

public List<Grammar>
getIndirectDelegates(Grammar g)

Get delegates below direct delegates of g

public int
public Object
public Grammar
public Rule
getRule(String ruleName)

public NFAState
getState(int s)

protected void
public void
public void
public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

delegateGrammarTreeRootback to summary
public CompositeGrammarTree delegateGrammarTreeRoot
lexerRulesback to summary
protected Set<String> lexerRules

If combined or lexer grammar, track the rules. Track lexer rules so we can warn about undefined tokens. This is combined set of lexer rules from all lexer grammars seen in all imports.

maxTokenTypeback to summary
protected int maxTokenType

Token names and literal tokens like "void" are uniquely indexed. with -1 implying EOF. Characters are different; they go from -1 (EOF) to ￾. For example, 0 could be a binary byte you want to lexer. Labels of DFA/NFA transitions can be both tokens and characters. I use negative numbers for bookkeeping labels like EPSILON. Char/String literals and token types overlap in the same space, however.

MIN_RULE_INDEXback to summary
public static final int MIN_RULE_INDEX
numberToStateListback to summary
protected Vector<NFAState> numberToStateList

The NFA states in the NFA built from rules across grammars in composite. Maps state number to NFAState object. This is a Vector instead of a List because I need to be able to grow this properly. After talking to Josh Bloch, Collections guy at Sun, I decided this was easiest solution.

refClosureBusyback to summary
protected Set<NFAState> refClosureBusy

Used during getRuleReferenceClosure to detect computation cycles

ruleIndexback to summary
protected int ruleIndex

Rules are uniquely labeled from 1..n among all grammars

ruleIndexToRuleListback to summary
protected Vector<Rule> ruleIndexToRuleList

Map a rule index to its name; use a Vector on purpose as new collections stuff won't let me setSize and make it grow. :( I need a specific guaranteed index, which the Collections stuff won't let me have.

stateCounterback to summary
public int stateCounter

Used to assign state numbers; all grammars in composite share common NFA space. This NFA tracks state numbers number to state mapping.

stringLiteralToTypeMapback to summary
public Map<String, Integer> stringLiteralToTypeMap

Map token literals like "while" to its token type. It may be that WHILE="while"=35, in which case both tokenIDToTypeMap and this field will have entries both mapped to 35.

tokenIDToTypeMapback to summary
public Map<String, Integer> tokenIDToTypeMap

Map token like ID (but not literals like "while") to its token type

typeToStringLiteralListback to summary
public Vector<String> typeToStringLiteralList

Reverse index for stringLiteralToTypeMap

typeToTokenListback to summary
public Vector<String> typeToTokenList

Map a token type to its token name. Must subtract MIN_TOKEN_TYPE from index.

watchNFAConversionback to summary
public boolean watchNFAConversion

Constructor Detail

CompositeGrammarback to summary
public CompositeGrammar()
Annotations
@SuppressWarnings:OverridableMethodCallInConstructor
CompositeGrammarback to summary
public CompositeGrammar(Grammar g)
Annotations
@SuppressWarnings:OverridableMethodCallInConstructor

Method Detail

_minimizeRuleSetback to summary
public void _minimizeRuleSet(Set<String> ruleDefs, CompositeGrammarTree p)
addGrammarback to summary
public void addGrammar(Grammar delegator, Grammar delegate)

Add delegate grammar as child of delegator

addStateback to summary
public void addState(NFAState state)
assignTokenTypesback to summary
public void assignTokenTypes() throws RecognitionException
createNFAsback to summary
public void createNFAs()
defineGrammarSymbolsback to summary
public void defineGrammarSymbols()
getAllImportedRulesback to summary
public Set<? extends Rule> getAllImportedRules(Grammar g)

Get all rule definitions from all direct/indirect delegate grammars of g.

getDelegatedRulesback to summary
public Set<? extends Rule> getDelegatedRules(Grammar g)

Get set of rules for grammar g that need to have manual delegation methods. This is the list of rules collected from all direct/indirect delegates minus rules overridden in grammar g. This returns null except for the delegate root because it is the only one that has to have a complete grammar rule interface. The delegates should not be instantiated directly for use as parsers (you can create them to pass to the root parser's ctor as arguments).

getDelegatesback to summary
public List<Grammar> getDelegates(Grammar g)

Get list of all delegates from all grammars in the delegate subtree of g. The grammars are in delegation tree preorder. Don't include g itself in list as it is not a delegate of itself.

getDelegatorback to summary
public Grammar getDelegator(Grammar g)

Get parent of this grammar

getDelegatorsback to summary
public List<Grammar> getDelegators(Grammar g)

Return list of delegate grammars from root down to g. Order is root, ..., g.parent. (g not included).

getDirectDelegatesback to summary
public List<Grammar> getDirectDelegates(Grammar g)
getGrammarback to summary
public Grammar getGrammar(String grammarName)
getIndirectDelegatesback to summary
public List<Grammar> getIndirectDelegates(Grammar g)

Get delegates below direct delegates of g

getNewNFAStateNumberback to summary
public int getNewNFAStateNumber()
getOptionback to summary
public Object getOption(String key)
getRootGrammarback to summary
public Grammar getRootGrammar()
getRuleback to summary
public Rule getRule(String ruleName)
getStateback to summary
public NFAState getState(int s)
initTokenSymbolTablesback to summary
protected void initTokenSymbolTables()
minimizeRuleSetback to summary
public void minimizeRuleSet()
setDelegationRootback to summary
public void setDelegationRoot(Grammar root)
translateLeftRecursiveRulesback to summary
public void translateLeftRecursiveRules()