Top Fields Constructors Methods
org.antlr.v4.tool

public Class Grammar

extends Object
implements AttributeResolver
Class Inheritance
All Implemented Interfaces
org.antlr.v4.tool.AttributeResolver
Known Direct Subclasses
org.antlr.v4.tool.LexerGrammar, org.antlr.v4.gui.Interpreter.IgnoreTokenVocabGrammar
Imports
org.antlr.v4.Tool, org.antlr.v4.analysis.LeftRecursiveRuleTransformer, org.antlr.v4.automata.ParserATNFactory, org.antlr.v4.misc.CharSupport, .OrderedHashMap, .Utils, org.antlr.v4.parse.ANTLRParser, .GrammarASTAdaptor, .GrammarTreeVisitor, .TokenVocabParser, org.antlr.v4.runtime.CharStream, .Lexer, .LexerInterpreter, .ParserInterpreter, .Token, .TokenStream, .Vocabulary, .VocabularyImpl, org.antlr.v4.runtime.atn.ATN, .ATNDeserializer, .ATNSerializer, .SemanticContext, org.antlr.v4.runtime.dfa.DFA, org.antlr.v4.runtime.misc.*, org.antlr.v4.tool.ast.ActionAST, .GrammarAST, .GrammarASTWithOptions, .GrammarRootAST, .PredAST, .RuleAST, .TerminalAST, java.io.IOException, java.util.ArrayList, .Arrays, .HashMap, .HashSet, .LinkedHashMap, .LinkedHashSet, .List, .Map, .Set

Field Summary

Modifier and TypeField and Description
public static final Set<String>
public GrammarRootAST
public ATN
atn

The ATN that represents the grammar with edges labelled with tokens or epsilon.

public static final String
public static final String
public final Map<String, Integer>
channelNameToValueMap

Map channel like COMMENTS_CHANNEL to its constant channel value.

public final List<String>
channelValueToNameList

Map a constant channel value to its name.

public Map<Integer, DFA>
public List<IntervalSet[]>
public static final Set<String>
public String
public static final String
public static final Map<String, AttributeDict>
public LexerGrammar
implicitLexer

Was this parser grammar created from a COMBINED grammar?

public List<Grammar>
public LinkedHashMap<Integer, PredAST>
indexToPredMap

Map the other direction upon demand

public List<Rule>
public static final 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.

public static final 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.

public LinkedHashMap<ActionAST, Integer>
lexerActions

Tracks all user lexer actions in all alternatives of all rules.

public static final Set<String>
public static final Set<String>
public static final Set<String>
pack-priv int
maxChannelType

The maximum channel value which is assigned by this grammar.

pack-priv int
maxTokenType

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

public String
public Map<String, ActionAST>
namedActions

Map a name to an action.

public Grammar
originalGrammar

If this is an extracted/implicit lexer, we point at original grammar

public TokenStream
originalTokenStream

If we transform grammar, track original unaltered token stream.

public Grammar
parent

If we're imported, who imported us?

public static final Set<String>
public static final Set<String>
public static final Set<String>
pack-priv int
public static final Set<String>
ruleRefOptions

Legal options for rule refs like id<key=value>

public OrderedHashMap<String, Rule>
rules

All rules defined in this specific grammar, not imported.

public static final Set<String>
public LinkedHashMap<PredAST, Integer>
sempreds

All sempreds found in grammar; maps tree node to sempred index; sempred index is 0..n-1

public Map<Integer, Interval>
pack-priv int
public final Map<String, Integer>
stringLiteralToTypeMap

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

public String
public final Map<String, Integer>
tokenNameToTypeMap

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

public static final Set<String>
tokenOptions

Legal options for terminal refs like ID<assoc=right>

public final TokenStream
tokenStream

Track token stream used to create this grammar

public final Tool
public final List<String>
public final List<String>
typeToTokenList

Map a token type to its token name.

Constructor Summary

AccessConstructor and Description
public
public
Grammar(String grammarText)

For testing

public
Grammar(String grammarText, LexerGrammar tokenVocabSource)

public
Grammar(String grammarText, ANTLRToolListener listener)

For testing

public
Grammar(String fileName, String grammarText)

For testing; builds trees, does sem anal

public
Grammar(String fileName, String grammarText, ANTLRToolListener listener)

For testing; builds trees, does sem anal

public
Grammar(String fileName, String grammarText, Grammar tokenVocabSource, ANTLRToolListener listener)

For testing; builds trees, does sem anal

Method Summary

Modifier and TypeMethod and Description
public GrammarParserInterpreter
public LexerInterpreter
public ParserInterpreter
protected static boolean
defAlias(GrammarAST r, String pattern, TreeWizard wiz, List<Pair<GrammarAST, GrammarAST>> lexerRuleToStringLiteral)

public void
public int

Returns:

The constant channel value assigned to the channel.
defineChannelName
(String
The channel name.
name
)

Define a token channel with a specified name.

public int

Returns:

The constant channel value assigned to the channel.
defineChannelName
(String
The channel name.
name
,
int value)

Define a token channel with a specified name.

public boolean

Returns:

true if the rule was added to the Grammar instance; otherwise, false if a rule with this name already existed in the grammar instance.
defineRule
(Rule
The rule to define in the grammar.
r
)

Define the specified rule in the grammar.

public int
public int
defineStringLiteral(String lit, int ttype)

public int
public int
public int
defineTokenName(String name, int ttype)

public IntSet
getAllCharValues()

Return min to max char as defined by the target.

public List<Grammar>
getAllImportedGrammars()

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

public ATN
public int

Returns:

The channel value, if channel is the name of a known user-defined token channel; otherwise, -1.
getChannelValue
(String
The channel name.
channel
)

Gets the constant channel value for a user-defined channel.

public String
getDefaultActionScope()

Given a grammar type, what should be the default action scope?

public List<Grammar>
getGrammarAncestors()

Return list of imported grammars from root down to our parent.

public static String
public LexerGrammar
public Grammar
getImportedGrammar(String name)

Return grammar directly imported by this grammar

public List<Grammar>
public LinkedHashMap<Integer, PredAST>
public String
public int
getMaxCharValue()

What is the max char value possible for this grammar's target?

public int
getMaxTokenType()

How many token types have been allocated so far?

public int
getNewChannelNumber()

Return a new unique integer in the channel value space.

public int
getNewTokenType()

Return a new unique integer in the token type space

public String
public Grammar
getOutermostGrammar()

Return the grammar that imported us and our parents.

public String
public String
getRecognizerName()

Get the name of the generated recognizer; may or may not be same as grammar name.

public Rule
public Rule
getRule(int index)

public Rule
getRule(String grammarName, String ruleName)

public String[]

Returns:

The names of all rules defined in the grammar.
getRuleNames
()

Gets an array of rule names for rules defined or imported by the grammar.

public String
getSemanticContextDisplayString(SemanticContext semctx)

Given an arbitrarily complex SemanticContext, walk the "tree" and get display string.

public Interval
getStateToGrammarRegion(int atnStateNumber)

Given an ATN state number, return the token index range within the grammar from which that ATN state was derived.

public static Map<Integer, Interval>
public static List<Pair<GrammarAST, GrammarAST>>
getStringLiteralAliasesFromLexerRules(GrammarRootAST ast)

Return list of (TOKEN_NAME node, 'literal' node) pairs

public String
public Set<String>
public String
getTokenDisplayName(int ttype)

Given a token type, get a meaningful name for it such as the ID or string literal.

public String[]

Returns:

The display names of all tokens defined in the grammar.
getTokenDisplayNames
()

Gets an array of display names for tokens defined or imported by the grammar.

public String[]
getTokenLiteralNames()

Gets the literal names assigned to tokens in the grammar.

public String
public String

Returns:

The name of the token with the specified type.
getTokenName
(int
The token type.
ttype
)

Gets the name by which a token can be referenced in the generated code.

public String[]

Returns:

The token names of all tokens defined in the grammar.
getTokenNames
()

Gets an array of token names for tokens defined or imported by the grammar.

public TokenStream
public String[]
getTokenSymbolicNames()

Gets the symbolic names assigned to tokens in the grammar.

public int
public IntSet
getTokenTypes()

Return a set of all possible token or char types for this grammar

public int
public String
public Vocabulary
getVocabulary()

Gets a Vocabulary instance describing the vocabulary used by the grammar.

public void
public void
protected void
public boolean
public boolean
public boolean
public static boolean
isTokenName(String id)

Is id a valid token name?

public String
public static Grammar
load(String fileName)

convenience method for Tool.loadGrammar()

public void
private void
public boolean
public boolean
public boolean
public boolean
public Attribute
public Attribute
public void
setChannelNameForValue(int
The constant value for the channel.
channelValue
,
String
The channel name.
name
)

Sets the channel name associated with a particular channel value.

public void
setLookaheadDFA(int decision, DFA lookaheadDFA)

public static void
setNodeOptions(GrammarAST node, GrammarAST options)

Given ^(TOKEN_REF ^(OPTIONS ^(ELEMENT_OPTIONS (= assoc right)))) set option assoc=right in TOKEN_REF.

public void
setTokenForType(int ttype, String text)

public boolean

Returns:

true if the rule was removed from the Grammar instance; otherwise, false if the specified rule was not defined in the grammar.
undefineRule
(Rule r)

Undefine the specified rule from this Grammar instance.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait