public class Grammar extends Objectimplements AttributeResolver
| Modifier and Type | Field and Description |
|---|---|
static Set |
actionOptions
|
GrammarRootAST |
ast
|
ATN |
atn
The ATN that represents the grammar with edges labelled with tokens or epsilon.
|
static String |
AUTO_GENERATED_TOKEN_NAME_PREFIX
|
Map |
channelNameToValueMap
Map channel like
COMMENTS_CHANNEL to its constant channel value.
|
List |
channelValueToNameList
Map a constant channel value to its name.
|
Map |
decisionDFAs
|
List |
decisionLOOK
|
static Set |
doNotCopyOptionsToLexer
|
String |
fileName
|
static String |
GRAMMAR_FROM_STRING_NAME
|
static Map |
grammarAndLabelRefTypeToScope
|
LexerGrammar |
implicitLexer
Was this parser grammar created from a COMBINED grammar? If so, this is what we extracted.
|
List |
importedGrammars
|
LinkedHashMap |
indexToPredMap
Map the other direction upon demand
|
List |
indexToRule
|
static String |
INVALID_RULE_NAME
This value is used as the name for elements in the array returned by
getRuleNames() for indexes not associated with a rule.
|
static String |
INVALID_TOKEN_NAME
This value is used in the following situations to indicate that a token type does not have an associated name which can be directly referenced in a grammar.
|
LinkedHashMap |
lexerActions
Tracks all user lexer actions in all alternatives of all rules.
|
static Set |
LexerBlockOptions
|
static Set |
lexerOptions
|
String |
name
|
Map |
namedActions
Map a name to an action.
|
Grammar |
originalGrammar
If this is an extracted/implicit lexer, we point at original grammar
|
org.antlr.runtime.TokenStream |
originalTokenStream
If we transform grammar, track original unaltered token stream.
|
Grammar |
parent
If we're imported, who imported us? If null, implies grammar is root
|
static Set |
ParserBlockOptions
|
static Set |
parserOptions
|
static Set |
ruleOptions
|
static Set |
ruleRefOptions
Legal options for rule refs like id
|
OrderedHashMap |
rules
All rules defined in this specific grammar, not imported.
|
static Set |
semPredOptions
|
LinkedHashMap |
sempreds
All sempreds found in grammar; maps tree node to sempred index; sempred index is 0..n-1
|
Map |
stateToGrammarRegionMap
|
Map |
stringLiteralToTypeMap
Map token literals like
'while' to its token type.
|
String |
text
|
Map |
tokenNameToTypeMap
Map token like
ID (but not literals like
'while') to its token type.
|
static Set |
tokenOptions
Legal options for terminal refs like ID
|
org.antlr.runtime.TokenStream |
tokenStream
Track token stream used to create this grammar
|
Tool |
tool
|
List |
typeToStringLiteralList
Reverse index for
stringLiteralToTypeMap.
|
List |
typeToTokenList
Map a token type to its token name.
|
| Constructor and Description |
|---|
Grammar(String
For testing
|
Grammar(String
For testing
|
Grammar(String
|
Grammar(String
For testing; builds trees, does sem anal
|
Grammar(String
For testing; builds trees, does sem anal
|
Grammar(String
For testing; builds trees, does sem anal
|
Grammar(Tool
|
| Modifier and Type | Method and Description |
|---|---|
GrammarParserInterpreter |
createGrammarParserInterpreter(TokenStream tokenStream)
|
LexerInterpreter |
createLexerInterpreter(CharStream input)
|
ParserInterpreter |
createParserInterpreter(TokenStream tokenStream)
|
protected static boolean |
defAlias(GrammarAST
|
void |
defineAction(GrammarAST
|
int |
defineChannelName(String
Define a token channel with a specified name.
|
int |
defineChannelName(String
Define a token channel with a specified name.
|
boolean |
defineRule(Rule
Define the specified rule in the grammar.
|
int |
defineStringLiteral(String
|
int |
defineStringLiteral(String
|
int |
defineTokenAlias(String
|
int |
defineTokenName(String
|
int |
defineTokenName(String
|
IntSet |
getAllCharValues()
Return min to max char as defined by the target.
|
List |
getAllImportedGrammars()
Get list of all imports from all grammars in the delegate subtree of g.
|
ATN |
getATN()
|
int |
getChannelValue(String
Gets the constant channel value for a user-defined channel.
|
String |
getDefaultActionScope()
Given a grammar type, what should be the default action scope? If I say @members in a COMBINED grammar, for example, the default scope should be "parser".
|
List |
getGrammarAncestors()
Return list of imported grammars from root down to our parent.
|
static String |
getGrammarTypeToFileNameSuffix(int type)
|
LexerGrammar |
getImplicitLexer()
Get delegates below direct delegates of g public List getIndirectDelegates(Grammar g) { List direct = getDirectDelegates(g); List delegates = getDelegates(g); delegates.removeAll(direct); return delegates; }
|
Grammar |
getImportedGrammar(String
Return grammar directly imported by this grammar
|
List |
getImportedGrammars()
|
LinkedHashMap |
getIndexToPredicateMap()
|
int |
getMaxCharValue()
What is the max char value possible for this grammar's target? Use unicode max if no target defined.
|
int |
getMaxTokenType()
How many token types have been allocated so far?
|
int |
getNewChannelNumber()
Return a new unique integer in the channel value space.
|
int |
getNewTokenType()
Return a new unique integer in the token type space
|
String |
getOptionString(String
|
Grammar |
getOutermostGrammar()
Return the grammar that imported us and our parents.
|
String |
getPredicateDisplayString(SemanticContext.Predicate pred)
|
String |
getRecognizerName()
Get the name of the generated recognizer; may or may not be same as grammar name.
|
Rule |
getRule(int index)
|
Rule |
getRule(String
|
Rule |
getRule(String
|
String |
getRuleNames()
Gets an array of rule names for rules defined or imported by the grammar.
|
String |
getSemanticContextDisplayString(SemanticContext semctx)
Given an arbitrarily complex SemanticContext, walk the "tree" and get display string.
|
Interval |
getStateToGrammarRegion(int atnStateNumber)
Given an ATN state number, return the token index range within the grammar from which that ATN state was derived.
|
static Map |
getStateToGrammarRegionMap(GrammarRootAST
|
static List |
getStringLiteralAliasesFromLexerRules(GrammarRootAST
Return list of (TOKEN_NAME node, 'literal' node) pairs
|
String |
getStringLiteralLexerRuleName(String
|
Set |
getStringLiterals()
|
String |
getTokenDisplayName(int ttype)
Given a token type, get a meaningful name for it such as the ID or string literal.
|
String |
getTokenDisplayNames()
Gets an array of display names for tokens defined or imported by the grammar.
|
String |
getTokenLiteralNames()
Gets the literal names assigned to tokens in the grammar.
|
String |
getTokenName(int ttype)
Gets the name by which a token can be referenced in the generated code.
|
String |
getTokenNames()
Gets an array of token names for tokens defined or imported by the grammar.
|
org.antlr.runtime.TokenStream |
getTokenStream()
|
String |
getTokenSymbolicNames()
Gets the symbolic names assigned to tokens in the grammar.
|
int |
getTokenType(String
|
IntSet |
getTokenTypes()
Return a set of all possible token or char types for this grammar
|
int |
getType()
|
String |
getTypeString()
|
Vocabulary |
getVocabulary()
Gets a
Vocabulary instance describing the vocabulary used by the grammar.
|
void |
importTokensFromTokensFile()
|
void |
importVocab(Grammar
|
protected void |
initTokenSymbolTables()
|
boolean |
isCombined()
|
boolean |
isLexer()
|
boolean |
isParser()
|
static boolean |
isTokenName(String
Is id a valid token name? Does id start with an uppercase letter?
|
String |
joinPredicateOperands(SemanticContext.Operator op, String
|
static Grammar |
load(String
convenience method for Tool.loadGrammar()
|
void |
loadImportedGrammars()
|
boolean |
resolvesToAttributeDict(String
|
boolean |
resolvesToLabel(String
|
boolean |
resolvesToListLabel(String
|
boolean |
resolvesToToken(String
|
Attribute |
resolveToAttribute(String
|
Attribute |
resolveToAttribute(String
|
void |
setChannelNameForValue(int channelValue, String
Sets the channel name associated with a particular channel value.
|
void |
setLookaheadDFA(int decision, DFA lookaheadDFA)
|
static void |
setNodeOptions(GrammarAST
Given ^(TOKEN_REF ^(OPTIONS ^(ELEMENT_OPTIONS (= assoc right)))) set option assoc=right in TOKEN_REF.
|
void |
setTokenForType(int ttype, String
|
boolean |
undefineRule(Rule
Undefine the specified rule from this
Grammar instance.
|
public static final StringGRAMMAR_FROM_STRING_NAME
public static final StringINVALID_TOKEN_NAME
Token#INVALID_TYPE.public static final StringINVALID_RULE_NAME
getRuleNames() for indexes not associated with a rule.
public static final Map<String ,AttributeDict > grammarAndLabelRefTypeToScope
public Stringname
public GrammarRootASTast
public final org.antlr.runtime.TokenStream tokenStream
public org.antlr.runtime.TokenStream originalTokenStream
public Stringtext
public StringfileName
public LexerGrammarimplicitLexer
public GrammaroriginalGrammar
public Grammarparent
public OrderedHashMap<String ,Rule > rules
public ATN atn
public List<IntervalSet []> decisionLOOK
public final Tooltool
public final Map<String ,Integer > tokenNameToTypeMap
ID (but not literals like
'while') to its token type.
public final Map<String ,Integer > stringLiteralToTypeMap
'while' to its token type. It may be that
WHILE="while"=35, in which case both
tokenNameToTypeMap and this field will have entries both mapped to 35.
public final List<String > typeToStringLiteralList
stringLiteralToTypeMap. Indexed with raw token type. 0 is invalid.
public final List<String > typeToTokenList
public final Map<String ,Integer > channelNameToValueMap
COMMENTS_CHANNEL to its constant channel value. Only user-defined channels are defined in this map.
public final List<String > channelValueToNameList
Token#DEFAULT_CHANNEL and
Token#HIDDEN_CHANNEL are not stored in this list, so the values at the corresponding indexes is
null.
public Map<String ,ActionAST > namedActions
public LinkedHashMap<ActionAST ,Integer > lexerActions
public LinkedHashMap<PredAST ,Integer > sempreds
public LinkedHashMap<Integer ,PredAST > indexToPredMap
public static final StringAUTO_GENERATED_TOKEN_NAME_PREFIX
public Grammar(Tooltool, GrammarRootAST ast)
public Grammar(StringgrammarText) throws org .antlr .runtime .RecognitionException
org.antlr.runtime.RecognitionException
public Grammar(StringgrammarText, LexerGrammar tokenVocabSource) throws org .antlr .runtime .RecognitionException
org.antlr.runtime.RecognitionException
public Grammar(StringgrammarText, ANTLRToolListener listener) throws org .antlr .runtime .RecognitionException
org.antlr.runtime.RecognitionException
public Grammar(StringfileName, String grammarText) throws org .antlr .runtime .RecognitionException
org.antlr.runtime.RecognitionException
public Grammar(StringfileName, String grammarText, ANTLRToolListener listener) throws org .antlr .runtime .RecognitionException
org.antlr.runtime.RecognitionException
public Grammar(StringfileName, String grammarText, Grammar tokenVocabSource, ANTLRToolListener listener) throws org .antlr .runtime .RecognitionException
org.antlr.runtime.RecognitionException
protected void initTokenSymbolTables()
public void loadImportedGrammars()
public void defineAction(GrammarASTatAST)
public boolean defineRule(Ruler)
Rule.index according to the
ruleNumber field, and adds the
Rule instance to
rules and
indexToRule.
r - The rule to define in the grammar.
true if the rule was added to the
Grammar instance; otherwise,
false if a rule with this name already existed in the grammar instance.
public boolean undefineRule(Ruler)
Grammar instance. The instance
r is removed from
rules and
indexToRule. This method updates the
Rule.index field for all rules defined after
r, and decrements
ruleNumber in preparation for adding new rules.
This method does nothing if the current Grammar does not contain the instance r at index r.index in indexToRule.
r -
true if the rule was removed from the
Grammar instance; otherwise,
false if the specified rule was not defined in the grammar.
public ATN getATN()
public RulegetRule(int index)
public List<Grammar > getAllImportedGrammars()
public LexerGrammargetImplicitLexer()
public List<Grammar > getGrammarAncestors()
public GrammargetOutermostGrammar()
public StringgetRecognizerName()
public GrammargetImportedGrammar(String name)
public int getTokenType(Stringtoken)
public StringgetTokenDisplayName(int ttype)
public StringgetTokenName(int ttype)
tokens{} block or via a lexer rule, this is the declared name of the token. For token types generated by the use of a string literal within a parser rule of a combined grammar, this is the automatically generated token type which includes the
AUTO_GENERATED_TOKEN_NAME_PREFIX prefix. For types which are not associated with a defined token, this method returns
INVALID_TOKEN_NAME.
ttype - The token type.
public int getChannelValue(Stringchannel)
This method only returns channel values for user-defined channels. All other channels, including the predefined channels Token#DEFAULT_CHANNEL and Token#HIDDEN_CHANNEL along with any channel defined in code (e.g. in a @members{} block), are ignored.
channel - The channel name.
channel is the name of a known user-defined token channel; otherwise, -1.
public String[] getRuleNames()
Rule.index .
If no rule is defined with an index for an element of the resulting array, the value of that element is INVALID_RULE_NAME.
public String[] getTokenNames()
getTokenName(int) for the corresponding token type.
getTokenName(int)
public String[] getTokenDisplayNames()
getTokenDisplayName(int) for the corresponding token type.
getTokenDisplayName(int)
public String[] getTokenLiteralNames()
public String[] getTokenSymbolicNames()
public Vocabulary getVocabulary()
Vocabulary instance describing the vocabulary used by the grammar.
public StringgetSemanticContextDisplayString(SemanticContext semctx)
public StringjoinPredicateOperands(SemanticContext.Operator op, String separator)
public LinkedHashMap<Integer ,PredAST > getIndexToPredicateMap()
public StringgetPredicateDisplayString(SemanticContext.Predicate pred)
public int getMaxCharValue()
public IntSet getTokenTypes()
public IntSet getAllCharValues()
public int getMaxTokenType()
public int getNewTokenType()
public int getNewChannelNumber()
public void importTokensFromTokensFile()
public void importVocab(GrammarimportG)
public int defineTokenName(Stringname)
public int defineTokenName(Stringname, int ttype)
public int defineStringLiteral(Stringlit)
public int defineStringLiteral(Stringlit, int ttype)
public void setTokenForType(int ttype,
String text) public int defineChannelName(Stringname)
If a channel with the specified name already exists, the previously assigned channel value is returned.
name - The channel name.
public int defineChannelName(Stringname, int value)
If a channel with the specified name already exists, the previously assigned channel value is not altered.
name - The channel name.
public void setChannelNameForValue(int channelValue,
String name)
If a name has already been assigned to the channel with constant value channelValue, this method does nothing.
channelValue - The constant value for the channel.
name - The channel name.
public StringgetDefaultActionScope()
public int getType()
public org.antlr.runtime.TokenStream getTokenStream()
public boolean isLexer()
public boolean isParser()
public boolean isCombined()
public static boolean isTokenName(Stringid)
public StringgetTypeString()
public static StringgetGrammarTypeToFileNameSuffix(int type)
public static void setNodeOptions(GrammarASTnode, GrammarAST options)
public static List<<any> > getStringLiteralAliasesFromLexerRules(GrammarRootAST ast)
protected static boolean defAlias(GrammarASTr, String pattern, org.antlr.runtime.tree.TreeWizard wiz, List <<any> > lexerRuleToStringLiteral)
public void setLookaheadDFA(int decision,
DFA lookaheadDFA)public static Map<Integer ,Interval > getStateToGrammarRegionMap(GrammarRootAST ast, IntervalSet grammarTokenTypes)
public Interval getStateToGrammarRegion(int atnStateNumber)
public LexerInterpreter createLexerInterpreter(CharStream input)
public GrammarParserInterpretercreateGrammarParserInterpreter(TokenStream tokenStream)
public ParserInterpreter createParserInterpreter(TokenStream tokenStream)