Class Grammar
- All Implemented Interfaces:
AttributeResolver
- Direct Known Subclasses:
Interpreter.IgnoreTokenVocabGrammar
,LexerGrammar
-
Field Summary
FieldsModifier and TypeFieldDescriptionThe ATN that represents the grammar with edges labelled with tokens or epsilon.static final String
static final String
Map channel likeCOMMENTS_CHANNEL
to its constant channel value.Map a constant channel value to its name.List
<IntervalSet[]> static final String
static final Map
<String, AttributeDict> Was this parser grammar created from a COMBINED grammar? If so, this is what we extracted.Map the other direction upon demandstatic final String
This value is used as the name for elements in the array returned bygetRuleNames()
for indexes not associated with a rule.static final String
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.Tracks all user lexer actions in all alternatives of all rules.(package private) int
The maximum channel value which is assigned by this grammar.(package private) int
Token names and literal tokens like "void" are uniquely indexed.Map a name to an action.If this is an extracted/implicit lexer, we point at original grammarorg.antlr.runtime.TokenStream
If we transform grammar, track original unaltered token stream.If we're imported, who imported us? If null, implies grammar is root(package private) int
Legal options for rule refs like id<key=value>All rules defined in this specific grammar, not imported.All sempreds found in grammar; maps tree node to sempred index; sempred index is 0..n-1(package private) int
Map token literals like'while'
to its token type.Map token likeID
(but not literals like'while'
) to its token type.Legal options for terminal refs like ID<assoc=right>final org.antlr.runtime.TokenStream
Track token stream used to create this grammarfinal Tool
Reverse index forstringLiteralToTypeMap
.Map a token type to its token name. -
Constructor Summary
ConstructorsConstructorDescriptionFor testingFor testing; builds trees, does sem analGrammar
(String fileName, String grammarText, ANTLRToolListener listener) For testing; builds trees, does sem analGrammar
(String fileName, String grammarText, Grammar tokenVocabSource, ANTLRToolListener listener) For testing; builds trees, does sem analGrammar
(String grammarText, ANTLRToolListener listener) For testingGrammar
(String grammarText, LexerGrammar tokenVocabSource) Grammar
(Tool tool, GrammarRootAST ast) -
Method Summary
Modifier and TypeMethodDescriptioncreateGrammarParserInterpreter
(TokenStream tokenStream) createLexerInterpreter
(CharStream input) createParserInterpreter
(TokenStream tokenStream) protected static boolean
defAlias
(GrammarAST r, String pattern, org.antlr.runtime.tree.TreeWizard wiz, List<Pair<GrammarAST, GrammarAST>> lexerRuleToStringLiteral) void
defineAction
(GrammarAST atAST) int
defineChannelName
(String name) Define a token channel with a specified name.int
defineChannelName
(String name, int value) Define a token channel with a specified name.boolean
defineRule
(Rule r) Define the specified rule in the grammar.int
int
defineStringLiteral
(String lit, int ttype) int
defineTokenAlias
(String name, String lit) int
defineTokenName
(String name) int
defineTokenName
(String name, int ttype) Return min to max char as defined by the target.Get list of all imports from all grammars in the delegate subtree of g.getATN()
int
getChannelValue
(String channel) Gets the constant channel value for a user-defined channel.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".Return list of imported grammars from root down to our parent.static String
getGrammarTypeToFileNameSuffix
(int type) getImportedGrammar
(String name) Return grammar directly imported by this grammarint
What is the max char value possible for this grammar's target? Use unicode max if no target defined.int
How many token types have been allocated so far?int
Return a new unique integer in the channel value space.int
Return a new unique integer in the token type spacegetOptionString
(String key) Return the grammar that imported us and our parents.Get the name of the generated recognizer; may or may not be same as grammar name.getRule
(int index) String[]
Gets an array of rule names for rules defined or imported by the grammar.Given an arbitrarily complex SemanticContext, walk the "tree" and get display string.getStateToGrammarRegion
(int atnStateNumber) Given an ATN state number, return the token index range within the grammar from which that ATN state was derived.getStateToGrammarRegionMap
(GrammarRootAST ast, IntervalSet grammarTokenTypes) static List
<Pair<GrammarAST, GrammarAST>> Return list of (TOKEN_NAME node, 'literal' node) pairsgetTokenDisplayName
(int ttype) Given a token type, get a meaningful name for it such as the ID or string literal.String[]
Gets an array of display names for tokens defined or imported by the grammar.String[]
Gets the literal names assigned to tokens in the grammar.getTokenName
(int ttype) Gets the name by which a token can be referenced in the generated code.getTokenName
(String literal) String[]
Gets an array of token names for tokens defined or imported by the grammar.org.antlr.runtime.TokenStream
String[]
Gets the symbolic names assigned to tokens in the grammar.int
getTokenType
(String token) Return a set of all possible token or char types for this grammarint
getType()
Gets aVocabulary
instance describing the vocabulary used by the grammar.void
void
importVocab
(Grammar importG) protected void
boolean
boolean
isLexer()
boolean
isParser()
static boolean
isTokenName
(String id) Is id a valid token name? Does id start with an uppercase letter?joinPredicateOperands
(SemanticContext.Operator op, String separator) static Grammar
convenience method for Tool.loadGrammar()void
private void
loadImportedGrammars
(Set<String> visited) boolean
resolvesToAttributeDict
(String x, ActionAST node) boolean
resolvesToLabel
(String x, ActionAST node) boolean
resolvesToListLabel
(String x, ActionAST node) boolean
resolvesToToken
(String x, ActionAST node) resolveToAttribute
(String x, String y, ActionAST node) resolveToAttribute
(String x, ActionAST node) void
setChannelNameForValue
(int channelValue, 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, String text) boolean
undefineRule
(Rule r) Undefine the specified rule from thisGrammar
instance.
-
Field Details
-
GRAMMAR_FROM_STRING_NAME
- See Also:
-
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.- This value is the name and display name for the token with type
Token.INVALID_TYPE
. - This value is the name for tokens with a type not represented by a named token. The display name for these tokens is simply the string representation of the token type as an integer.
- See Also:
- This value is the name and display name for the token with type
-
INVALID_RULE_NAME
This value is used as the name for elements in the array returned bygetRuleNames()
for indexes not associated with a rule.- See Also:
-
caseInsensitiveOptionName
- See Also:
-
parserOptions
-
lexerOptions
-
lexerRuleOptions
-
parseRuleOptions
-
parserBlockOptions
-
lexerBlockOptions
-
ruleRefOptions
Legal options for rule refs like id<key=value> -
tokenOptions
Legal options for terminal refs like ID<assoc=right> -
actionOptions
-
semPredOptions
-
doNotCopyOptionsToLexer
-
grammarAndLabelRefTypeToScope
-
name
-
ast
-
tokenStream
public final org.antlr.runtime.TokenStream tokenStreamTrack token stream used to create this grammar -
originalTokenStream
public org.antlr.runtime.TokenStream originalTokenStreamIf we transform grammar, track original unaltered token stream. This is set to the same value as tokenStream when tokenStream is initially set. If this field differs from tokenStream, then we have transformed the grammar. -
text
-
fileName
-
implicitLexer
Was this parser grammar created from a COMBINED grammar? If so, this is what we extracted. -
originalGrammar
If this is an extracted/implicit lexer, we point at original grammar -
parent
If we're imported, who imported us? If null, implies grammar is root -
importedGrammars
-
rules
All rules defined in this specific grammar, not imported. Also does not include lexical rules if combined. -
indexToRule
-
ruleNumber
int ruleNumber -
stringLiteralRuleNumber
int stringLiteralRuleNumber -
atn
The ATN that represents the grammar with edges labelled with tokens or epsilon. It is more suitable to analysis than an AST representation. -
stateToGrammarRegionMap
-
decisionDFAs
-
decisionLOOK
-
tool
-
maxTokenType
int maxTokenTypeToken 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/ATN 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. -
tokenNameToTypeMap
Map token likeID
(but not literals like'while'
) to its token type. -
stringLiteralToTypeMap
Map token literals like'while'
to its token type. It may be thatWHILE="while"=35
, in which case bothtokenNameToTypeMap
and this field will have entries both mapped to 35. -
typeToStringLiteralList
Reverse index forstringLiteralToTypeMap
. Indexed with raw token type. 0 is invalid. -
typeToTokenList
Map a token type to its token name. Indexed with raw token type. 0 is invalid. -
maxChannelType
int maxChannelTypeThe maximum channel value which is assigned by this grammar. Values belowToken.MIN_USER_CHANNEL_VALUE
are assumed to be predefined. -
channelNameToValueMap
Map channel likeCOMMENTS_CHANNEL
to its constant channel value. Only user-defined channels are defined in this map. -
channelValueToNameList
Map a constant channel value to its name. Indexed with raw channel value. The predefined channelsToken.DEFAULT_CHANNEL
andToken.HIDDEN_CHANNEL
are not stored in this list, so the values at the corresponding indexes isnull
. -
namedActions
Map a name to an action. The code generator will use this to fill holes in the output files. I track the AST node for the action in case I need the line number for errors. -
lexerActions
Tracks all user lexer actions in all alternatives of all rules. Doesn't track sempreds. maps tree node to action index (alt number 1..n). -
sempreds
All sempreds found in grammar; maps tree node to sempred index; sempred index is 0..n-1 -
indexToPredMap
Map the other direction upon demand -
AUTO_GENERATED_TOKEN_NAME_PREFIX
- See Also:
-
-
Constructor Details
-
Grammar
-
Grammar
For testing- Throws:
org.antlr.runtime.RecognitionException
-
Grammar
public Grammar(String grammarText, LexerGrammar tokenVocabSource) throws org.antlr.runtime.RecognitionException - Throws:
org.antlr.runtime.RecognitionException
-
Grammar
public Grammar(String grammarText, ANTLRToolListener listener) throws org.antlr.runtime.RecognitionException For testing- Throws:
org.antlr.runtime.RecognitionException
-
Grammar
For testing; builds trees, does sem anal- Throws:
org.antlr.runtime.RecognitionException
-
Grammar
public Grammar(String fileName, String grammarText, ANTLRToolListener listener) throws org.antlr.runtime.RecognitionException For testing; builds trees, does sem anal- Throws:
org.antlr.runtime.RecognitionException
-
Grammar
public Grammar(String fileName, String grammarText, Grammar tokenVocabSource, ANTLRToolListener listener) throws org.antlr.runtime.RecognitionException For testing; builds trees, does sem anal- Throws:
org.antlr.runtime.RecognitionException
-
-
Method Details
-
initTokenSymbolTables
protected void initTokenSymbolTables() -
loadImportedGrammars
public void loadImportedGrammars() -
loadImportedGrammars
-
defineAction
-
defineRule
Define the specified rule in the grammar. This method assigns the rule'sRule.index
according to theruleNumber
field, and adds theRule
instance torules
andindexToRule
.- Parameters:
r
- The rule to define in the grammar.- Returns:
true
if the rule was added to theGrammar
instance; otherwise,false
if a rule with this name already existed in the grammar instance.
-
undefineRule
Undefine the specified rule from thisGrammar
instance. The instancer
is removed fromrules
andindexToRule
. This method updates theRule.index
field for all rules defined afterr
, and decrementsruleNumber
in preparation for adding new rules.This method does nothing if the current
Grammar
does not contain the instancer
at indexr.index
inindexToRule
.- Parameters:
r
-- Returns:
true
if the rule was removed from theGrammar
instance; otherwise,false
if the specified rule was not defined in the grammar.
-
getRule
-
getATN
-
getRule
-
getRule
-
getAllImportedGrammars
Get list of all imports from all grammars in the delegate subtree of g. The grammars are in import tree preorder. Don't include ourselves in list as we're not a delegate of ourselves. -
getImportedGrammars
-
getImplicitLexer
-
load
convenience method for Tool.loadGrammar() -
getGrammarAncestors
Return list of imported grammars from root down to our parent. Order is [root, ..., this.parent]. (us not included). -
getOutermostGrammar
Return the grammar that imported us and our parents. Return this if we're root. -
getRecognizerName
Get the name of the generated recognizer; may or may not be same as grammar name. Recognizer is TParser and TLexer from T if combined, else just use T regardless of grammar type. -
getStringLiteralLexerRuleName
-
getImportedGrammar
Return grammar directly imported by this grammar -
getTokenType
-
getTokenName
-
getTokenDisplayName
Given a token type, get a meaningful name for it such as the ID or string literal. If this is a lexer and the ttype is in the char vocabulary, compute an ANTLR-valid (possibly escaped) char literal. -
getTokenName
Gets the name by which a token can be referenced in the generated code. For tokens defined in atokens{}
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 theAUTO_GENERATED_TOKEN_NAME_PREFIX
prefix. For types which are not associated with a defined token, this method returnsINVALID_TOKEN_NAME
.- Parameters:
ttype
- The token type.- Returns:
- The name of the token with the specified type.
-
getChannelValue
Gets the constant channel value for a user-defined channel.This method only returns channel values for user-defined channels. All other channels, including the predefined channels
Token.DEFAULT_CHANNEL
andToken.HIDDEN_CHANNEL
along with any channel defined in code (e.g. in a@members{}
block), are ignored.- Parameters:
channel
- The channel name.- Returns:
- The channel value, if
channel
is the name of a known user-defined token channel; otherwise, -1.
-
getRuleNames
Gets an array of rule names for rules defined or imported by the grammar. The array index is the rule index, and the value is the name of the rule with the correspondingRule.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
.- Returns:
- The names of all rules defined in the grammar.
-
getTokenNames
Gets an array of token names for tokens defined or imported by the grammar. The array index is the token type, and the value is the result ofgetTokenName(java.lang.String)
for the corresponding token type.- Returns:
- The token names of all tokens defined in the grammar.
- See Also:
-
getTokenDisplayNames
Gets an array of display names for tokens defined or imported by the grammar. The array index is the token type, and the value is the result ofgetTokenDisplayName(int)
for the corresponding token type.- Returns:
- The display names of all tokens defined in the grammar.
- See Also:
-
getTokenLiteralNames
Gets the literal names assigned to tokens in the grammar. -
getTokenSymbolicNames
Gets the symbolic names assigned to tokens in the grammar. -
getVocabulary
Gets aVocabulary
instance describing the vocabulary used by the grammar. -
getSemanticContextDisplayString
Given an arbitrarily complex SemanticContext, walk the "tree" and get display string. Pull predicates from grammar text. -
joinPredicateOperands
-
getIndexToPredicateMap
-
getPredicateDisplayString
-
getMaxCharValue
public int getMaxCharValue()What is the max char value possible for this grammar's target? Use unicode max if no target defined. -
getTokenTypes
Return a set of all possible token or char types for this grammar -
getAllCharValues
Return min to max char as defined by the target. If no target, use max unicode char value. -
getMaxTokenType
public int getMaxTokenType()How many token types have been allocated so far? -
getNewTokenType
public int getNewTokenType()Return a new unique integer in the token type space -
getNewChannelNumber
public int getNewChannelNumber()Return a new unique integer in the channel value space. -
importTokensFromTokensFile
public void importTokensFromTokensFile() -
importVocab
-
defineTokenName
-
defineTokenName
-
defineStringLiteral
-
defineStringLiteral
-
defineTokenAlias
-
setTokenForType
-
defineChannelName
Define a token channel with a specified name.If a channel with the specified name already exists, the previously assigned channel value is returned.
- Parameters:
name
- The channel name.- Returns:
- The constant channel value assigned to the channel.
-
defineChannelName
Define a token channel with a specified name.If a channel with the specified name already exists, the previously assigned channel value is not altered.
- Parameters:
name
- The channel name.- Returns:
- The constant channel value assigned to the channel.
-
setChannelNameForValue
Sets the channel name associated with a particular channel value.If a name has already been assigned to the channel with constant value
channelValue
, this method does nothing.- Parameters:
channelValue
- The constant value for the channel.name
- The channel name.
-
resolveToAttribute
- Specified by:
resolveToAttribute
in interfaceAttributeResolver
-
resolveToAttribute
- Specified by:
resolveToAttribute
in interfaceAttributeResolver
-
resolvesToLabel
- Specified by:
resolvesToLabel
in interfaceAttributeResolver
-
resolvesToListLabel
- Specified by:
resolvesToListLabel
in interfaceAttributeResolver
-
resolvesToToken
- Specified by:
resolvesToToken
in interfaceAttributeResolver
-
resolvesToAttributeDict
- Specified by:
resolvesToAttributeDict
in interfaceAttributeResolver
-
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". -
getType
public int getType() -
getTokenStream
public org.antlr.runtime.TokenStream getTokenStream() -
isLexer
public boolean isLexer() -
isParser
public boolean isParser() -
isCombined
public boolean isCombined() -
isTokenName
Is id a valid token name? Does id start with an uppercase letter? -
getTypeString
-
getGrammarTypeToFileNameSuffix
-
getLanguage
-
getOptionString
-
setNodeOptions
Given ^(TOKEN_REF ^(OPTIONS ^(ELEMENT_OPTIONS (= assoc right)))) set option assoc=right in TOKEN_REF. -
getStringLiteralAliasesFromLexerRules
public static List<Pair<GrammarAST,GrammarAST>> getStringLiteralAliasesFromLexerRules(GrammarRootAST ast) Return list of (TOKEN_NAME node, 'literal' node) pairs -
defAlias
protected static boolean defAlias(GrammarAST r, String pattern, org.antlr.runtime.tree.TreeWizard wiz, List<Pair<GrammarAST, GrammarAST>> lexerRuleToStringLiteral) -
getStringLiterals
-
setLookaheadDFA
-
getStateToGrammarRegionMap
public static Map<Integer,Interval> getStateToGrammarRegionMap(GrammarRootAST ast, IntervalSet grammarTokenTypes) -
getStateToGrammarRegion
Given an ATN state number, return the token index range within the grammar from which that ATN state was derived. -
createLexerInterpreter
-
createGrammarParserInterpreter
- Since:
- 4.5.1
-
createParserInterpreter
-