public class Grammar extends java.lang.Object implements AttributeResolver
| Modifier and Type | Field and Description |
|---|---|
static java.util.Set<java.lang.String> |
actionOptions |
GrammarRootAST |
ast |
ATN |
atn
The ATN that represents the grammar with edges labelled with tokens
or epsilon.
|
static java.lang.String |
AUTO_GENERATED_TOKEN_NAME_PREFIX |
java.util.Map<java.lang.String,java.lang.Integer> |
channelNameToValueMap
Map channel like
COMMENTS_CHANNEL to its constant channel value. |
java.util.List<java.lang.String> |
channelValueToNameList
Map a constant channel value to its name.
|
java.util.Map<java.lang.Integer,DFA> |
decisionDFAs |
java.util.List<IntervalSet[]> |
decisionLOOK |
static java.util.Set<java.lang.String> |
doNotCopyOptionsToLexer |
java.lang.String |
fileName |
static java.lang.String |
GRAMMAR_FROM_STRING_NAME |
static java.util.Map<java.lang.String,AttributeDict> |
grammarAndLabelRefTypeToScope |
LexerGrammar |
implicitLexer
Was this parser grammar created from a COMBINED grammar? If so,
this is what we extracted.
|
java.util.List<Grammar> |
importedGrammars |
java.util.LinkedHashMap<java.lang.Integer,PredAST> |
indexToPredMap
Map the other direction upon demand
|
java.util.List<Rule> |
indexToRule |
static java.lang.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 java.lang.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.
|
java.util.LinkedHashMap<ActionAST,java.lang.Integer> |
lexerActions
Tracks all user lexer actions in all alternatives of all rules.
|
static java.util.Set<java.lang.String> |
LexerBlockOptions |
static java.util.Set<java.lang.String> |
lexerOptions |
java.lang.String |
name |
java.util.Map<java.lang.String,ActionAST> |
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 java.util.Set<java.lang.String> |
ParserBlockOptions |
static java.util.Set<java.lang.String> |
parserOptions |
static java.util.Set<java.lang.String> |
ruleOptions |
static java.util.Set<java.lang.String> |
ruleRefOptions
Legal options for rule refs like id
|
OrderedHashMap<java.lang.String,Rule> |
rules
All rules defined in this specific grammar, not imported.
|
static java.util.Set<java.lang.String> |
semPredOptions |
java.util.LinkedHashMap<PredAST,java.lang.Integer> |
sempreds
All sempreds found in grammar; maps tree node to sempred index;
sempred index is 0..n-1
|
java.util.Map<java.lang.Integer,Interval> |
stateToGrammarRegionMap |
java.util.Map<java.lang.String,java.lang.Integer> |
stringLiteralToTypeMap
Map token literals like
'while' to its token type. |
java.lang.String |
text |
java.util.Map<java.lang.String,java.lang.Integer> |
tokenNameToTypeMap
Map token like
ID (but not literals like 'while') to its
token type. |
static java.util.Set<java.lang.String> |
tokenOptions
Legal options for terminal refs like ID
|
org.antlr.runtime.TokenStream |
tokenStream
Track token stream used to create this grammar
|
Tool |
tool |
java.util.List<java.lang.String> |
typeToStringLiteralList
Reverse index for
stringLiteralToTypeMap. |
java.util.List<java.lang.String> |
typeToTokenList
Map a token type to its token name.
|
| Constructor and Description |
|---|
Grammar(java.lang.String grammarText)
For testing
|
Grammar(java.lang.String grammarText,
ANTLRToolListener listener)
For testing
|
Grammar(java.lang.String grammarText,
LexerGrammar tokenVocabSource) |
Grammar(java.lang.String fileName,
java.lang.String grammarText)
For testing; builds trees, does sem anal
|
Grammar(java.lang.String fileName,
java.lang.String grammarText,
ANTLRToolListener listener)
For testing; builds trees, does sem anal
|
Grammar(java.lang.String fileName,
java.lang.String grammarText,
Grammar tokenVocabSource,
ANTLRToolListener listener)
For testing; builds trees, does sem anal
|
Grammar(Tool tool,
GrammarRootAST ast) |
| Modifier and Type | Method and Description |
|---|---|
LexerInterpreter |
createLexerInterpreter(CharStream input) |
ParserInterpreter |
createParserInterpreter(TokenStream tokenStream) |
protected static boolean |
defAlias(GrammarAST r,
java.lang.String pattern,
org.antlr.runtime.tree.TreeWizard wiz,
java.util.List<Pair<GrammarAST,GrammarAST>> lexerRuleToStringLiteral) |
void |
defineAction(GrammarAST atAST) |
int |
defineChannelName(java.lang.String name)
Define a token channel with a specified name.
|
int |
defineChannelName(java.lang.String name,
int value)
Define a token channel with a specified name.
|
boolean |
defineRule(Rule r)
Define the specified rule in the grammar.
|
int |
defineStringLiteral(java.lang.String lit) |
int |
defineStringLiteral(java.lang.String lit,
int ttype) |
int |
defineTokenAlias(java.lang.String name,
java.lang.String lit) |
int |
defineTokenName(java.lang.String name) |
int |
defineTokenName(java.lang.String name,
int ttype) |
IntSet |
getAllCharValues()
Return min to max char as defined by the target.
|
java.util.List<Grammar> |
getAllImportedGrammars()
Get list of all imports from all grammars in the delegate subtree of g.
|
int |
getChannelValue(java.lang.String channel)
Gets the constant channel value for a user-defined channel.
|
java.lang.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".
|
java.util.List<Grammar> |
getGrammarAncestors()
Return list of imported grammars from root down to our parent.
|
static java.lang.String |
getGrammarTypeToFileNameSuffix(int type) |
LexerGrammar |
getImplicitLexer()
Get delegates below direct delegates of g
public List
|
Grammar |
getImportedGrammar(java.lang.String name)
Return grammar directly imported by this grammar
|
java.util.List<Grammar> |
getImportedGrammars() |
java.util.LinkedHashMap<java.lang.Integer,PredAST> |
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
|
java.lang.String |
getOptionString(java.lang.String key) |
Grammar |
getOutermostGrammar()
Return the grammar that imported us and our parents.
|
java.lang.String |
getPredicateDisplayString(SemanticContext.Predicate pred) |
java.lang.String |
getRecognizerName()
Get the name of the generated recognizer; may or may not be same
as grammar name.
|
Rule |
getRule(int index) |
Rule |
getRule(java.lang.String name) |
Rule |
getRule(java.lang.String grammarName,
java.lang.String ruleName) |
java.lang.String[] |
getRuleNames()
Gets an array of rule names for rules defined or imported by the
grammar.
|
java.lang.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 java.util.Map<java.lang.Integer,Interval> |
getStateToGrammarRegionMap(GrammarRootAST ast,
IntervalSet grammarTokenTypes) |
static java.util.List<Pair<GrammarAST,GrammarAST>> |
getStringLiteralAliasesFromLexerRules(GrammarRootAST ast)
Return list of (TOKEN_NAME node, 'literal' node) pairs
|
java.lang.String |
getStringLiteralLexerRuleName(java.lang.String lit) |
java.util.Set<java.lang.String> |
getStringLiterals() |
java.lang.String |
getTokenDisplayName(int ttype)
Given a token type, get a meaningful name for it such as the ID
or string literal.
|
java.lang.String[] |
getTokenDisplayNames()
Gets an array of display names for tokens defined or imported by the
grammar.
|
java.lang.String[] |
getTokenLiteralNames()
Gets the literal names assigned to tokens in the grammar.
|
java.lang.String |
getTokenName(int ttype)
Gets the name by which a token can be referenced in the generated code.
|
java.lang.String[] |
getTokenNames()
Gets an array of token names for tokens defined or imported by the
grammar.
|
org.antlr.runtime.TokenStream |
getTokenStream() |
java.lang.String[] |
getTokenSymbolicNames()
Gets the symbolic names assigned to tokens in the grammar.
|
int |
getTokenType(java.lang.String token) |
IntSet |
getTokenTypes()
Return a set of all possible token or char types for this grammar
|
int |
getType() |
java.lang.String |
getTypeString() |
Vocabulary |
getVocabulary()
Gets a
Vocabulary instance describing the vocabulary used by the
grammar. |
void |
importTokensFromTokensFile() |
void |
importVocab(Grammar importG) |
protected void |
initTokenSymbolTables() |
boolean |
isCombined() |
boolean |
isLexer() |
boolean |
isParser() |
static boolean |
isTokenName(java.lang.String id)
Is id a valid token name? Does id start with an uppercase letter?
|
java.lang.String |
joinPredicateOperands(SemanticContext.Operator op,
java.lang.String separator) |
static Grammar |
load(java.lang.String fileName)
convenience method for Tool.loadGrammar()
|
void |
loadImportedGrammars() |
boolean |
resolvesToAttributeDict(java.lang.String x,
ActionAST node) |
boolean |
resolvesToLabel(java.lang.String x,
ActionAST node) |
boolean |
resolvesToListLabel(java.lang.String x,
ActionAST node) |
boolean |
resolvesToToken(java.lang.String x,
ActionAST node) |
Attribute |
resolveToAttribute(java.lang.String x,
ActionAST node) |
Attribute |
resolveToAttribute(java.lang.String x,
java.lang.String y,
ActionAST node) |
void |
setChannelNameForValue(int channelValue,
java.lang.String name)
Sets the channel name associated with a particular channel value.
|
void |
setLookaheadDFA(int decision,
DFA lookaheadDFA) |
static void |
setNodeOptions(GrammarAST node,
GrammarAST options)
Given ^(TOKEN_REF ^(OPTIONS ^(ELEMENT_OPTIONS (= assoc right))))
set option assoc=right in TOKEN_REF.
|
void |
setTokenForType(int ttype,
java.lang.String text) |
boolean |
undefineRule(Rule r)
Undefine the specified rule from this
Grammar instance. |
public static final java.lang.String GRAMMAR_FROM_STRING_NAME
public static final java.lang.String INVALID_TOKEN_NAME
Token.INVALID_TYPE.public static final java.lang.String INVALID_RULE_NAME
getRuleNames() for indexes not associated with a rule.public static final java.util.Set<java.lang.String> parserOptions
public static final java.util.Set<java.lang.String> lexerOptions
public static final java.util.Set<java.lang.String> ruleOptions
public static final java.util.Set<java.lang.String> ParserBlockOptions
public static final java.util.Set<java.lang.String> LexerBlockOptions
public static final java.util.Set<java.lang.String> ruleRefOptions
public static final java.util.Set<java.lang.String> tokenOptions
public static final java.util.Set<java.lang.String> actionOptions
public static final java.util.Set<java.lang.String> semPredOptions
public static final java.util.Set<java.lang.String> doNotCopyOptionsToLexer
public static final java.util.Map<java.lang.String,AttributeDict> grammarAndLabelRefTypeToScope
public java.lang.String name
public GrammarRootAST ast
public final org.antlr.runtime.TokenStream tokenStream
public org.antlr.runtime.TokenStream originalTokenStream
public java.lang.String text
public java.lang.String fileName
public LexerGrammar implicitLexer
public Grammar originalGrammar
public Grammar parent
public java.util.List<Grammar> importedGrammars
public OrderedHashMap<java.lang.String,Rule> rules
public java.util.List<Rule> indexToRule
public ATN atn
public java.util.Map<java.lang.Integer,Interval> stateToGrammarRegionMap
public java.util.Map<java.lang.Integer,DFA> decisionDFAs
public java.util.List<IntervalSet[]> decisionLOOK
public final Tool tool
public final java.util.Map<java.lang.String,java.lang.Integer> tokenNameToTypeMap
ID (but not literals like 'while') to its
token type.public final java.util.Map<java.lang.String,java.lang.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 java.util.List<java.lang.String> typeToStringLiteralList
stringLiteralToTypeMap. Indexed with raw token
type. 0 is invalid.public final java.util.List<java.lang.String> typeToTokenList
public final java.util.Map<java.lang.String,java.lang.Integer> channelNameToValueMap
COMMENTS_CHANNEL to its constant channel value.
Only user-defined channels are defined in this map.public final java.util.List<java.lang.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 java.util.Map<java.lang.String,ActionAST> namedActions
public java.util.LinkedHashMap<ActionAST,java.lang.Integer> lexerActions
public java.util.LinkedHashMap<PredAST,java.lang.Integer> sempreds
public java.util.LinkedHashMap<java.lang.Integer,PredAST> indexToPredMap
public static final java.lang.String AUTO_GENERATED_TOKEN_NAME_PREFIX
public Grammar(Tool tool, GrammarRootAST ast)
public Grammar(java.lang.String grammarText)
throws org.antlr.runtime.RecognitionException
org.antlr.runtime.RecognitionExceptionpublic Grammar(java.lang.String grammarText,
LexerGrammar tokenVocabSource)
throws org.antlr.runtime.RecognitionException
org.antlr.runtime.RecognitionExceptionpublic Grammar(java.lang.String grammarText,
ANTLRToolListener listener)
throws org.antlr.runtime.RecognitionException
org.antlr.runtime.RecognitionExceptionpublic Grammar(java.lang.String fileName,
java.lang.String grammarText)
throws org.antlr.runtime.RecognitionException
org.antlr.runtime.RecognitionExceptionpublic Grammar(java.lang.String fileName,
java.lang.String grammarText,
ANTLRToolListener listener)
throws org.antlr.runtime.RecognitionException
org.antlr.runtime.RecognitionExceptionpublic Grammar(java.lang.String fileName,
java.lang.String grammarText,
Grammar tokenVocabSource,
ANTLRToolListener listener)
throws org.antlr.runtime.RecognitionException
org.antlr.runtime.RecognitionExceptionprotected void initTokenSymbolTables()
public void loadImportedGrammars()
public void defineAction(GrammarAST atAST)
public boolean defineRule(Rule r)
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(Rule r)
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 Rule getRule(java.lang.String name)
public Rule getRule(int index)
public Rule getRule(java.lang.String grammarName, java.lang.String ruleName)
public java.util.List<Grammar> getAllImportedGrammars()
public java.util.List<Grammar> getImportedGrammars()
public LexerGrammar getImplicitLexer()
public static Grammar load(java.lang.String fileName)
public java.util.List<Grammar> getGrammarAncestors()
public Grammar getOutermostGrammar()
public java.lang.String getRecognizerName()
public java.lang.String getStringLiteralLexerRuleName(java.lang.String lit)
public Grammar getImportedGrammar(java.lang.String name)
public int getTokenType(java.lang.String token)
public java.lang.String getTokenDisplayName(int ttype)
public java.lang.String getTokenName(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(java.lang.String channel)
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 java.lang.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 java.lang.String[] getTokenNames()
getTokenName(int) for the corresponding token type.getTokenName(int)public java.lang.String[] getTokenDisplayNames()
getTokenDisplayName(int) for the corresponding token type.getTokenDisplayName(int)public java.lang.String[] getTokenLiteralNames()
public java.lang.String[] getTokenSymbolicNames()
public Vocabulary getVocabulary()
Vocabulary instance describing the vocabulary used by the
grammar.public java.lang.String getSemanticContextDisplayString(SemanticContext semctx)
public java.lang.String joinPredicateOperands(SemanticContext.Operator op, java.lang.String separator)
public java.util.LinkedHashMap<java.lang.Integer,PredAST> getIndexToPredicateMap()
public java.lang.String getPredicateDisplayString(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(Grammar importG)
public int defineTokenName(java.lang.String name)
public int defineTokenName(java.lang.String name,
int ttype)
public int defineStringLiteral(java.lang.String lit)
public int defineStringLiteral(java.lang.String lit,
int ttype)
public int defineTokenAlias(java.lang.String name,
java.lang.String lit)
public void setTokenForType(int ttype,
java.lang.String text)
public int defineChannelName(java.lang.String name)
If a channel with the specified name already exists, the previously assigned channel value is returned.
name - The channel name.public int defineChannelName(java.lang.String name,
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,
java.lang.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 Attribute resolveToAttribute(java.lang.String x, ActionAST node)
resolveToAttribute in interface AttributeResolverpublic Attribute resolveToAttribute(java.lang.String x, java.lang.String y, ActionAST node)
resolveToAttribute in interface AttributeResolverpublic boolean resolvesToLabel(java.lang.String x,
ActionAST node)
resolvesToLabel in interface AttributeResolverpublic boolean resolvesToListLabel(java.lang.String x,
ActionAST node)
resolvesToListLabel in interface AttributeResolverpublic boolean resolvesToToken(java.lang.String x,
ActionAST node)
resolvesToToken in interface AttributeResolverpublic boolean resolvesToAttributeDict(java.lang.String x,
ActionAST node)
resolvesToAttributeDict in interface AttributeResolverpublic java.lang.String getDefaultActionScope()
public int getType()
public org.antlr.runtime.TokenStream getTokenStream()
public boolean isLexer()
public boolean isParser()
public boolean isCombined()
public static boolean isTokenName(java.lang.String id)
public java.lang.String getTypeString()
public static java.lang.String getGrammarTypeToFileNameSuffix(int type)
public java.lang.String getOptionString(java.lang.String key)
public static void setNodeOptions(GrammarAST node, GrammarAST options)
public static java.util.List<Pair<GrammarAST,GrammarAST>> getStringLiteralAliasesFromLexerRules(GrammarRootAST ast)
protected static boolean defAlias(GrammarAST r, java.lang.String pattern, org.antlr.runtime.tree.TreeWizard wiz, java.util.List<Pair<GrammarAST,GrammarAST>> lexerRuleToStringLiteral)
public java.util.Set<java.lang.String> getStringLiterals()
public void setLookaheadDFA(int decision,
DFA lookaheadDFA)
public static java.util.Map<java.lang.Integer,Interval> getStateToGrammarRegionMap(GrammarRootAST ast, IntervalSet grammarTokenTypes)
public Interval getStateToGrammarRegion(int atnStateNumber)
public LexerInterpreter createLexerInterpreter(CharStream input)
public ParserInterpreter createParserInterpreter(TokenStream tokenStream)