antlr-ng Tool Class Hierarchy / index / Grammar
Class: Grammar
Implements
IGrammar
IAttributeResolver
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.ast
atn?
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.atn
channelNameToValueMap
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.channelNameToValueMap
channelValueToNameList
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.channelValueToNameList
decisionLookahead
decisionLookahead: IntervalSet[][];
Implementation of
IGrammar.decisionLookahead
fileName
fileName: string;
Implementation of
IGrammar.fileName
implicitLexer
implicitLexer: undefined | LexerGrammar;
Was this parser grammar created from a COMBINED grammar? If so, this is what we extracted.
Implementation of
IGrammar.implicitLexer
lexerActions
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.lexerActions
name
name: string = "<not set>";
Implementation of
IGrammar.name
namedActions
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.namedActions
originalGrammar?
optional originalGrammar: Grammar;
If this is an extracted/implicit lexer, we point at original grammar.
Implementation of
IGrammar.originalGrammar
originalTokenStream
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.originalTokenStream
rules
rules: Map<string, Rule>;
All rules defined in this specific grammar, not imported. Also does not include lexical rules if combined.
Implementation of
IGrammar.rules
sempreds
sempreds: Map<PredAST, number>;
All sempreds found in grammar; maps tree node to sempred index; sempred index is 0..n - 1.
Implementation of
IGrammar.sempreds
stringLiteralToTypeMap
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.stringLiteralToTypeMap
tokenNameToTypeMap
readonly tokenNameToTypeMap: Map<string, number>;
Map token like ID
(but not literals like 'while'
) to its token type.
Implementation of
IGrammar.tokenNameToTypeMap
tokenStream
tokenStream: TokenStream;
Track token stream used to create this grammar
Implementation of
IGrammar.tokenStream
tool
tool: ITool;
Implementation of
IGrammar.tool
typeToStringLiteralList
readonly typeToStringLiteralList: (null | string)[];
Reverse index for stringLiteralToTypeMap. Indexed with raw token type. 0 is invalid.
Implementation of
IGrammar.typeToStringLiteralList
typeToTokenList
readonly typeToTokenList: (null | string)[] = [];
Map a token type to its token name. Indexed with raw token type. 0 is invalid.
Implementation of
IGrammar.typeToTokenList
actionOptions
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.type
Methods
createGrammarParserInterpreter()
createGrammarParserInterpreter(tokenStream): GrammarParserInterpreter;
Parameters
tokenStream
TokenStream
Returns
GrammarParserInterpreter
Implementation of
IGrammar.createGrammarParserInterpreter
createLexerInterpreter()
createLexerInterpreter(input): LexerInterpreter;
Parameters
input
CharStream
Returns
LexerInterpreter
Implementation of
IGrammar.createLexerInterpreter
createParserInterpreter()
createParserInterpreter(tokenStream): ParserInterpreter;
For testing.
Parameters
tokenStream
TokenStream
Returns
ParserInterpreter
Implementation of
IGrammar.createParserInterpreter
defineAction()
defineAction(atAST): void;
Parameters
atAST
Returns
void
Implementation of
IGrammar.defineAction
defineChannelName()
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.defineChannelName
defineRule()
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.defineRule
defineStringLiteral()
defineStringLiteral(lit, ttype?): number;
Parameters
lit
string
ttype?
number
Returns
number
Implementation of
IGrammar.defineStringLiteral
defineTokenAlias()
defineTokenAlias(name, lit): number;
Parameters
name
string
lit
string
Returns
number
Implementation of
IGrammar.defineTokenAlias
defineTokenName()
defineTokenName(name, ttype?): number;
Parameters
name
string
ttype?
number
Returns
number
Implementation of
IGrammar.defineTokenName
getAllCharValues()
getAllCharValues(): IntervalSet;
Returns
IntervalSet
min to max char as defined by the target. If no target, use max unicode char value.
Implementation of
IGrammar.getAllCharValues
getAllImportedGrammars()
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.getAllImportedGrammars
getATN()
getATN(): ATN;
Needed for tests.
Returns
ATN
Implementation of
IGrammar.getATN
getChannelValue()
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.getChannelValue
getDefaultActionScope()
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.getDefaultActionScope
getGrammarAncestors()
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.getGrammarAncestors
getImportedGrammar()
getImportedGrammar(name): null | Grammar;
Parameters
name
string
Returns
null
| Grammar
grammar directly imported by this grammar.
Implementation of
IGrammar.getImportedGrammar
getImportedGrammars()
getImportedGrammars(): Grammar[];
Returns
Grammar
[]
Implementation of
IGrammar.getImportedGrammars
getIndexToPredicateMap()
getIndexToPredicateMap(): Map<number, PredAST>;
Returns
Map
<number
, PredAST
>
Implementation of
IGrammar.getIndexToPredicateMap
getLanguage()
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.getLanguage
getMaxCharValue()
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.getMaxCharValue
getMaxTokenType()
getMaxTokenType(): number;
How many token types have been allocated so far?
Returns
number
Implementation of
IGrammar.getMaxTokenType
getNewChannelNumber()
getNewChannelNumber(): number;
Returns
number
a new unique integer in the channel value space.
Implementation of
IGrammar.getNewChannelNumber
getNewTokenType()
getNewTokenType(): number;
Returns
number
a new unique integer in the token type space.
Implementation of
IGrammar.getNewTokenType
getOptionString()
getOptionString(key): undefined | string;
Parameters
key
string
Returns
undefined
| string
Implementation of
IGrammar.getOptionString
getOutermostGrammar()
getOutermostGrammar(): Grammar;
Returns
Grammar
the grammar that imported us and our parents, or this if we're root.
Implementation of
IGrammar.getOutermostGrammar
getPredicateDisplayString()
getPredicateDisplayString(pred): string;
Parameters
pred
Predicate
Returns
string
Implementation of
IGrammar.getPredicateDisplayString
getRecognizerName()
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.getRecognizerName
getRule()
Call Signature
getRule(name): null | Rule;
Parameters
name
string
| number
Returns
null
| Rule
Implementation of
IGrammar.getRule
Call Signature
getRule(grammarName, ruleName): null | Rule;
Parameters
grammarName
string
ruleName
string
Returns
null
| Rule
Implementation of
IGrammar.getRule
getRuleNames()
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.getRuleNames
getStringLiteralLexerRuleName()
getStringLiteralLexerRuleName(_literal): string;
Parameters
_literal
string
Returns
string
Implementation of
IGrammar.getStringLiteralLexerRuleName
getStringLiterals()
getStringLiterals(): Set<string>;
Returns
Set
<string
>
Implementation of
IGrammar.getStringLiterals
getTokenDisplayName()
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.getTokenDisplayName
getTokenDisplayNames()
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.getTokenDisplayNames
getTokenLiteralNames()
getTokenLiteralNames(): (null | string)[];
Gets the literal names assigned to tokens in the grammar.
Returns
(null
| string
)[]
Implementation of
IGrammar.getTokenLiteralNames
getTokenName()
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.getTokenName
getTokenNames()
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.getTokenNames
getTokenSymbolicNames()
getTokenSymbolicNames(): (null | string)[];
Gets the symbolic names assigned to tokens in the grammar.
Returns
(null
| string
)[]
Implementation of
IGrammar.getTokenSymbolicNames
getTokenType()
getTokenType(token): number;
Parameters
token
string
Returns
number
Implementation of
IGrammar.getTokenType
getTokenTypes()
getTokenTypes(): IntervalSet;
Returns
IntervalSet
a set of all possible token or char types for this grammar.
Implementation of
IGrammar.getTokenTypes
getTypeString()
getTypeString(): null | string;
Returns
null
| string
Implementation of
IGrammar.getTypeString
getVocabulary()
getVocabulary(): Vocabulary;
Gets a Vocabulary instance describing the vocabulary used by the grammar.
Returns
Vocabulary
Implementation of
IGrammar.getVocabulary
importTokensFromTokensFile()
importTokensFromTokensFile(): void;
Returns
void
Implementation of
IGrammar.importTokensFromTokensFile
importVocab()
importVocab(importG): void;
Parameters
importG
Grammar
Returns
void
Implementation of
IGrammar.importVocab
initTokenSymbolTables()
protected initTokenSymbolTables(): void;
Returns
void
Implementation of
IGrammar.initTokenSymbolTables
isCombined()
isCombined(): boolean;
Returns
boolean
Implementation of
IGrammar.isCombined
isLexer()
isLexer(): boolean;
Returns
boolean
Implementation of
IGrammar.isLexer
isParser()
isParser(): boolean;
Returns
boolean
Implementation of
IGrammar.isParser
loadImportedGrammars()
loadImportedGrammars(visited): void;
Parameters
visited
Set
<string
>
Returns
void
Implementation of
IGrammar.loadImportedGrammars
resolvesToAttributeDict()
resolvesToAttributeDict(x, node): boolean;
Parameters
x
string
node
ActionAST
Returns
boolean
Implementation of
IGrammar.resolvesToAttributeDict
resolvesToLabel()
resolvesToLabel(x, node): boolean;
Parameters
x
string
node
ActionAST
Returns
boolean
Implementation of
IGrammar.resolvesToLabel
resolvesToListLabel()
resolvesToListLabel(x, node): boolean;
Parameters
x
string
node
ActionAST
Returns
boolean
Implementation of
IGrammar.resolvesToListLabel
resolvesToToken()
resolvesToToken(x, node): boolean;
Parameters
x
string
node
ActionAST
Returns
boolean
Implementation of
IGrammar.resolvesToToken
resolveToAttribute()
Call Signature
resolveToAttribute(x, node): IAttribute;
Parameters
x
string
node
ActionAST
Returns
IAttribute
Implementation of
IGrammar.resolveToAttribute
Call Signature
resolveToAttribute(
x,
y,
node): null | IAttribute;
Parameters
x
string
y
string
node
ActionAST
Returns
null
| IAttribute
Implementation of
IGrammar.resolveToAttribute
setChannelNameForValue()
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.setChannelNameForValue
setTokenForType()
setTokenForType(ttype, text): void;
Parameters
ttype
number
text
string
Returns
void
Implementation of
IGrammar.setTokenForType
undefineRule()
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.undefineRule
defAlias()
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