antlr-ng Tool Class Hierarchy / index / Grammar
Class: Grammar
Implements
IGrammarIAttributeResolver
Constructors
Constructor
new Grammar(tool, ast): Grammar;Parameters
tool
ITool
ast
Returns
Grammar
Constructor
new Grammar(grammarText, tokenVocabSource?): Grammar;For testing
Parameters
grammarText
string
tokenVocabSource?
LexerGrammar
Returns
Grammar
Properties
ast
ast: GrammarRootAST;Implementation of
IGrammar.astatn?
optional atn: ATN;The ATN that represents the grammar with edges labelled with tokens or epsilon. It is more suitable to analysis than an AST representation.
Implementation of
IGrammar.atnchannelNameToValueMap
readonly channelNameToValueMap: Map<string, number>;Map channel like COMMENTS_CHANNEL to its constant channel value. Only user-defined channels are defined in this map.
Implementation of
IGrammar.channelNameToValueMapchannelValueToNameList
readonly channelValueToNameList: string[];Map a constant channel value to its name. Indexed with raw channel value. The predefined channels Token.DEFAULT_CHANNEL and Token.HIDDEN_CHANNEL are not stored in this list, so the values at the corresponding indexes is null.
Implementation of
IGrammar.channelValueToNameListdecisionLookahead
decisionLookahead: IntervalSet[][];Implementation of
IGrammar.decisionLookaheadfileName
fileName: string;Implementation of
IGrammar.fileNameimplicitLexer
implicitLexer: undefined | LexerGrammar;Was this parser grammar created from a COMBINED grammar? If so, this is what we extracted.
Implementation of
IGrammar.implicitLexerlexerActions
lexerActions: Map<ActionAST, number>;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).
Implementation of
IGrammar.lexerActionsname
name: string = "<not set>";Implementation of
IGrammar.namenamedActions
namedActions: Map<string, ActionAST>;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.
Implementation of
IGrammar.namedActionsoriginalGrammar?
optional originalGrammar: Grammar;If this is an extracted/implicit lexer, we point at original grammar.
Implementation of
IGrammar.originalGrammaroriginalTokenStream
originalTokenStream: TokenStream;If 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.
Implementation of
IGrammar.originalTokenStreamrules
rules: Map<string, Rule>;All rules defined in this specific grammar, not imported. Also does not include lexical rules if combined.
Implementation of
IGrammar.rulessempreds
sempreds: Map<PredAST, number>;All sempreds found in grammar; maps tree node to sempred index; sempred index is 0..n - 1.
Implementation of
IGrammar.sempredsstringLiteralToTypeMap
readonly stringLiteralToTypeMap: Map<string, number>;Map token literals like '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.
Implementation of
IGrammar.stringLiteralToTypeMaptokenNameToTypeMap
readonly tokenNameToTypeMap: Map<string, number>;Map token like ID (but not literals like 'while') to its token type.
Implementation of
IGrammar.tokenNameToTypeMaptokenStream
tokenStream: TokenStream;Track token stream used to create this grammar
Implementation of
IGrammar.tokenStreamtool
tool: ITool;Implementation of
IGrammar.tooltypeToStringLiteralList
readonly typeToStringLiteralList: (null | string)[];Reverse index for stringLiteralToTypeMap. Indexed with raw token type. 0 is invalid.
Implementation of
IGrammar.typeToStringLiteralListtypeToTokenList
readonly typeToTokenList: (null | string)[] = [];Map a token type to its token name. Indexed with raw token type. 0 is invalid.
Implementation of
IGrammar.typeToTokenListactionOptions
readonly static actionOptions: Set<string>;AUTO_GENERATED_TOKEN_NAME_PREFIX
readonly static AUTO_GENERATED_TOKEN_NAME_PREFIX: "T__" = "T__";caseInsensitiveOptionName
readonly static caseInsensitiveOptionName: "caseInsensitive" = "caseInsensitive";doNotCopyOptionsToLexer
readonly static doNotCopyOptionsToLexer: Set<string>;grammarAndLabelRefTypeToScope
readonly static grammarAndLabelRefTypeToScope: Map<string, AttributeDict>;INVALID_TOKEN_NAME
readonly static INVALID_TOKEN_NAME: "<INVALID>" = "<INVALID>";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.
lexerBlockOptions
readonly static lexerBlockOptions: Set<string>;lexerOptions
readonly static lexerOptions: Set<string> = Grammar.parserOptions;lexerRuleOptions
readonly static lexerRuleOptions: Set<string>;parserBlockOptions
readonly static parserBlockOptions: Set<string>;parserOptions
readonly static parserOptions: Set<string>;parseRuleOptions
readonly static parseRuleOptions: Set<string>;ruleRefOptions
readonly static ruleRefOptions: Set<string>;Legal options for rule refs like id<key=value>
semPredOptions
readonly static semPredOptions: Set<string>;tokenOptions
readonly static tokenOptions: Set<string>;Legal options for terminal refs like ID<assoc=right>
Accessors
type
Get Signature
get type(): GrammarType;Returns
GrammarType
Implementation of
IGrammar.typeMethods
createGrammarParserInterpreter()
createGrammarParserInterpreter(tokenStream): GrammarParserInterpreter;Parameters
tokenStream
TokenStream
Returns
GrammarParserInterpreter
Implementation of
IGrammar.createGrammarParserInterpretercreateLexerInterpreter()
createLexerInterpreter(input): LexerInterpreter;Parameters
input
CharStream
Returns
LexerInterpreter
Implementation of
IGrammar.createLexerInterpretercreateParserInterpreter()
createParserInterpreter(tokenStream): ParserInterpreter;For testing.
Parameters
tokenStream
TokenStream
Returns
ParserInterpreter
Implementation of
IGrammar.createParserInterpreterdefineAction()
defineAction(atAST): void;Parameters
atAST
Returns
void
Implementation of
IGrammar.defineActiondefineChannelName()
defineChannelName(name, value?): number;Defines 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
string
The channel name.
value?
number
Returns
number
The constant channel value assigned to the channel.
Implementation of
IGrammar.defineChannelNamedefineRule()
defineRule(r): boolean;Defines the specified rule in the grammar. This method assigns the rule's Rule.index according to the ruleNumber field, and adds the Rule instance to rules and indexToRule.
Parameters
r
Rule
The rule to define in the grammar.
Returns
boolean
true if the rule was added to the Grammar instance; otherwise, false if a rule with this name already existed in the grammar instance.
Implementation of
IGrammar.defineRuledefineStringLiteral()
defineStringLiteral(lit, ttype?): number;Parameters
lit
string
ttype?
number
Returns
number
Implementation of
IGrammar.defineStringLiteraldefineTokenAlias()
defineTokenAlias(name, lit): number;Parameters
name
string
lit
string
Returns
number
Implementation of
IGrammar.defineTokenAliasdefineTokenName()
defineTokenName(name, ttype?): number;Parameters
name
string
ttype?
number
Returns
number
Implementation of
IGrammar.defineTokenNamegetAllCharValues()
getAllCharValues(): IntervalSet;Returns
IntervalSet
min to max char as defined by the target. If no target, use max unicode char value.
Implementation of
IGrammar.getAllCharValuesgetAllImportedGrammars()
getAllImportedGrammars(): Grammar[];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.
Returns
Grammar[]
Implementation of
IGrammar.getAllImportedGrammarsgetATN()
getATN(): ATN;Needed for tests.
Returns
ATN
Implementation of
IGrammar.getATNgetChannelValue()
getChannelValue(channel): number;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 and Token:HIDDEN_CHANNEL along with any channel defined in code (e.g. in a @members{} block), are ignored.
Parameters
channel
string
The channel name.
Returns
number
The channel value, if channel is the name of a known user-defined token channel; otherwise, -1.
Implementation of
IGrammar.getChannelValuegetDefaultActionScope()
getDefaultActionScope(): null | string;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".
Returns
null | string
Implementation of
IGrammar.getDefaultActionScopegetGrammarAncestors()
getGrammarAncestors(): null | Grammar[];Return list of imported grammars from root down to our parent. Order is [root, ..., this.parent] (us not included).
Returns
null | Grammar[]
Implementation of
IGrammar.getGrammarAncestorsgetImportedGrammar()
getImportedGrammar(name): null | Grammar;Parameters
name
string
Returns
null | Grammar
grammar directly imported by this grammar.
Implementation of
IGrammar.getImportedGrammargetImportedGrammars()
getImportedGrammars(): Grammar[];Returns
Grammar[]
Implementation of
IGrammar.getImportedGrammarsgetIndexToPredicateMap()
getIndexToPredicateMap(): Map<number, PredAST>;Returns
Map<number, PredAST>
Implementation of
IGrammar.getIndexToPredicateMapgetLanguage()
getLanguage():
| "Cpp"
| "CSharp"
| "Dart"
| "Go"
| "JavaScript"
| "Java"
| "PHP"
| "Python3"
| "Swift"
| "TypeScript";Returns
| "Cpp" | "CSharp" | "Dart" | "Go" | "JavaScript" | "Java" | "PHP" | "Python3" | "Swift" | "TypeScript"
Implementation of
IGrammar.getLanguagegetMaxCharValue()
getMaxCharValue(): number;What is the max char value possible for this grammar's target? Use unicode max if no target defined.
Returns
number
Implementation of
IGrammar.getMaxCharValuegetMaxTokenType()
getMaxTokenType(): number;How many token types have been allocated so far?
Returns
number
Implementation of
IGrammar.getMaxTokenTypegetNewChannelNumber()
getNewChannelNumber(): number;Returns
number
a new unique integer in the channel value space.
Implementation of
IGrammar.getNewChannelNumbergetNewTokenType()
getNewTokenType(): number;Returns
number
a new unique integer in the token type space.
Implementation of
IGrammar.getNewTokenTypegetOptionString()
getOptionString(key): undefined | string;Parameters
key
string
Returns
undefined | string
Implementation of
IGrammar.getOptionStringgetOutermostGrammar()
getOutermostGrammar(): Grammar;Returns
Grammar
the grammar that imported us and our parents, or this if we're root.
Implementation of
IGrammar.getOutermostGrammargetPredicateDisplayString()
getPredicateDisplayString(pred): string;Parameters
pred
Predicate
Returns
string
Implementation of
IGrammar.getPredicateDisplayStringgetRecognizerName()
getRecognizerName(): string;Gets 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.
Returns
string
Implementation of
IGrammar.getRecognizerNamegetRule()
Call Signature
getRule(name): null | Rule;Parameters
name
string | number
Returns
null | Rule
Implementation of
IGrammar.getRuleCall Signature
getRule(grammarName, ruleName): null | Rule;Parameters
grammarName
string
ruleName
string
Returns
null | Rule
Implementation of
IGrammar.getRulegetRuleNames()
getRuleNames(): string[];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 corresponding 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.
Returns
string[]
The names of all rules defined in the grammar.
Implementation of
IGrammar.getRuleNamesgetStringLiteralLexerRuleName()
getStringLiteralLexerRuleName(_literal): string;Parameters
_literal
string
Returns
string
Implementation of
IGrammar.getStringLiteralLexerRuleNamegetStringLiterals()
getStringLiterals(): Set<string>;Returns
Set<string>
Implementation of
IGrammar.getStringLiteralsgetTokenDisplayName()
getTokenDisplayName(ttype): string;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.
Parameters
ttype
number
Returns
string
Implementation of
IGrammar.getTokenDisplayNamegetTokenDisplayNames()
getTokenDisplayNames(): (null | string)[];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 of getTokenDisplayName for the corresponding token type.
Returns
(null | string)[]
The display names of all tokens defined in the grammar.
Implementation of
IGrammar.getTokenDisplayNamesgetTokenLiteralNames()
getTokenLiteralNames(): (null | string)[];Gets the literal names assigned to tokens in the grammar.
Returns
(null | string)[]
Implementation of
IGrammar.getTokenLiteralNamesgetTokenName()
getTokenName(literalOrTokenType): null | string;Gets the name by which a token can be referenced in the generated code. For tokens defined in a 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.
Parameters
literalOrTokenType
The token type.
string | number
Returns
null | string
The name of the token with the specified type.
Implementation of
IGrammar.getTokenNamegetTokenNames()
getTokenNames(): (null | string)[];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 of getTokenName for the corresponding token type.
Returns
(null | string)[]
The token names of all tokens defined in the grammar.
Implementation of
IGrammar.getTokenNamesgetTokenSymbolicNames()
getTokenSymbolicNames(): (null | string)[];Gets the symbolic names assigned to tokens in the grammar.
Returns
(null | string)[]
Implementation of
IGrammar.getTokenSymbolicNamesgetTokenType()
getTokenType(token): number;Parameters
token
string
Returns
number
Implementation of
IGrammar.getTokenTypegetTokenTypes()
getTokenTypes(): IntervalSet;Returns
IntervalSet
a set of all possible token or char types for this grammar.
Implementation of
IGrammar.getTokenTypesgetTypeString()
getTypeString(): null | string;Returns
null | string
Implementation of
IGrammar.getTypeStringgetVocabulary()
getVocabulary(): Vocabulary;Gets a Vocabulary instance describing the vocabulary used by the grammar.
Returns
Vocabulary
Implementation of
IGrammar.getVocabularyimportTokensFromTokensFile()
importTokensFromTokensFile(): void;Returns
void
Implementation of
IGrammar.importTokensFromTokensFileimportVocab()
importVocab(importG): void;Parameters
importG
Grammar
Returns
void
Implementation of
IGrammar.importVocabinitTokenSymbolTables()
protected initTokenSymbolTables(): void;Returns
void
Implementation of
IGrammar.initTokenSymbolTablesisCombined()
isCombined(): boolean;Returns
boolean
Implementation of
IGrammar.isCombinedisLexer()
isLexer(): boolean;Returns
boolean
Implementation of
IGrammar.isLexerisParser()
isParser(): boolean;Returns
boolean
Implementation of
IGrammar.isParserloadImportedGrammars()
loadImportedGrammars(visited): void;Parameters
visited
Set<string>
Returns
void
Implementation of
IGrammar.loadImportedGrammarsresolvesToAttributeDict()
resolvesToAttributeDict(x, node): boolean;Parameters
x
string
node
ActionAST
Returns
boolean
Implementation of
IGrammar.resolvesToAttributeDictresolvesToLabel()
resolvesToLabel(x, node): boolean;Parameters
x
string
node
ActionAST
Returns
boolean
Implementation of
IGrammar.resolvesToLabelresolvesToListLabel()
resolvesToListLabel(x, node): boolean;Parameters
x
string
node
ActionAST
Returns
boolean
Implementation of
IGrammar.resolvesToListLabelresolvesToToken()
resolvesToToken(x, node): boolean;Parameters
x
string
node
ActionAST
Returns
boolean
Implementation of
IGrammar.resolvesToTokenresolveToAttribute()
Call Signature
resolveToAttribute(x, node): IAttribute;Parameters
x
string
node
ActionAST
Returns
IAttribute
Implementation of
IGrammar.resolveToAttributeCall Signature
resolveToAttribute(
x,
y,
node): null | IAttribute;Parameters
x
string
y
string
node
ActionAST
Returns
null | IAttribute
Implementation of
IGrammar.resolveToAttributesetChannelNameForValue()
setChannelNameForValue(channelValue, name): void;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
number
The constant value for the channel.
name
string
The channel name.
Returns
void
Implementation of
IGrammar.setChannelNameForValuesetTokenForType()
setTokenForType(ttype, text): void;Parameters
ttype
number
text
string
Returns
void
Implementation of
IGrammar.setTokenForTypeundefineRule()
protected undefineRule(r): boolean;Undefines the specified rule from this 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.
Parameters
r
Rule
Returns
boolean
true if the rule was removed from the Grammar instance; otherwise, false if the specified rule was not defined in the grammar.
Implementation of
IGrammar.undefineRuledefAlias()
protected static defAlias(
r,
pattern,
wiz,
lexerRuleToStringLiteral): boolean;Parameters
r
pattern
string
wiz
TreeWizard
lexerRuleToStringLiteral
[GrammarAST, GrammarAST][]
Returns
boolean
forFile()
static forFile<T>(
c,
fileName,
grammarText,
tokenVocabSource?,
listener?): T;Type Parameters
T
T extends Grammar
Parameters
c
Constructor<T>
fileName
string
grammarText
string
tokenVocabSource?
Grammar
listener?
Returns
T
getGrammarTypeToFileNameSuffix()
static getGrammarTypeToFileNameSuffix(type): string;Parameters
type
GrammarType
Returns
string
getStringLiteralAliasesFromLexerRules()
static getStringLiteralAliasesFromLexerRules(ast):
| null
| [GrammarAST, GrammarAST][];Parameters
ast
Returns
| null | [GrammarAST, GrammarAST][]
list of (TOKEN_NAME node, 'literal' node) pairs
setNodeOptions()
static setNodeOptions(node, options): void;Given ^(TOKEN_REF ^(OPTIONS ^(ELEMENT_OPTIONS (= assoc right)))) sets option assoc=right in TOKEN_REF.
Parameters
node
options
Returns
void