Skip to content

antlr-ng Tool Class Hierarchy / index / Grammar

Class: Grammar

Implements

  • IGrammar
  • IAttributeResolver

Constructors

Constructor

ts
new Grammar(tool, ast): Grammar;

Parameters

tool

ITool

ast

GrammarRootAST

Returns

Grammar

Constructor

ts
new Grammar(grammarText, tokenVocabSource?): Grammar;

For testing

Parameters

grammarText

string

tokenVocabSource?

LexerGrammar

Returns

Grammar

Properties

ast

ts
ast: GrammarRootAST;

Implementation of

ts
IGrammar.ast

atn?

ts
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

ts
IGrammar.atn

channelNameToValueMap

ts
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

ts
IGrammar.channelNameToValueMap

channelValueToNameList

ts
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

ts
IGrammar.channelValueToNameList

decisionLookahead

ts
decisionLookahead: IntervalSet[][];

Implementation of

ts
IGrammar.decisionLookahead

fileName

ts
fileName: string;

Implementation of

ts
IGrammar.fileName

implicitLexer

ts
implicitLexer: undefined | LexerGrammar;

Was this parser grammar created from a COMBINED grammar? If so, this is what we extracted.

Implementation of

ts
IGrammar.implicitLexer

lexerActions

ts
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

ts
IGrammar.lexerActions

name

ts
name: string = "<not set>";

Implementation of

ts
IGrammar.name

namedActions

ts
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

ts
IGrammar.namedActions

originalGrammar?

ts
optional originalGrammar: Grammar;

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

Implementation of

ts
IGrammar.originalGrammar

originalTokenStream

ts
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

ts
IGrammar.originalTokenStream

rules

ts
rules: Map<string, Rule>;

All rules defined in this specific grammar, not imported. Also does not include lexical rules if combined.

Implementation of

ts
IGrammar.rules

sempreds

ts
sempreds: Map<PredAST, number>;

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

Implementation of

ts
IGrammar.sempreds

stringLiteralToTypeMap

ts
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

ts
IGrammar.stringLiteralToTypeMap

tokenNameToTypeMap

ts
readonly tokenNameToTypeMap: Map<string, number>;

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

Implementation of

ts
IGrammar.tokenNameToTypeMap

tokenStream

ts
tokenStream: TokenStream;

Track token stream used to create this grammar

Implementation of

ts
IGrammar.tokenStream

tool

ts
tool: ITool;

Implementation of

ts
IGrammar.tool

typeToStringLiteralList

ts
readonly typeToStringLiteralList: (null | string)[];

Reverse index for stringLiteralToTypeMap. Indexed with raw token type. 0 is invalid.

Implementation of

ts
IGrammar.typeToStringLiteralList

typeToTokenList

ts
readonly typeToTokenList: (null | string)[] = [];

Map a token type to its token name. Indexed with raw token type. 0 is invalid.

Implementation of

ts
IGrammar.typeToTokenList

actionOptions

ts
readonly static actionOptions: Set<string>;

AUTO_GENERATED_TOKEN_NAME_PREFIX

ts
readonly static AUTO_GENERATED_TOKEN_NAME_PREFIX: "T__" = "T__";

caseInsensitiveOptionName

ts
readonly static caseInsensitiveOptionName: "caseInsensitive" = "caseInsensitive";

doNotCopyOptionsToLexer

ts
readonly static doNotCopyOptionsToLexer: Set<string>;

grammarAndLabelRefTypeToScope

ts
readonly static grammarAndLabelRefTypeToScope: Map<string, AttributeDict>;

INVALID_TOKEN_NAME

ts
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

ts
readonly static lexerBlockOptions: Set<string>;

lexerOptions

ts
readonly static lexerOptions: Set<string> = Grammar.parserOptions;

lexerRuleOptions

ts
readonly static lexerRuleOptions: Set<string>;

parserBlockOptions

ts
readonly static parserBlockOptions: Set<string>;

parserOptions

ts
readonly static parserOptions: Set<string>;

parseRuleOptions

ts
readonly static parseRuleOptions: Set<string>;

ruleRefOptions

ts
readonly static ruleRefOptions: Set<string>;

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


semPredOptions

ts
readonly static semPredOptions: Set<string>;

tokenOptions

ts
readonly static tokenOptions: Set<string>;

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

Accessors

type

Get Signature

ts
get type(): GrammarType;
Returns

GrammarType

Implementation of

ts
IGrammar.type

Methods

createGrammarParserInterpreter()

ts
createGrammarParserInterpreter(tokenStream): GrammarParserInterpreter;

Parameters

tokenStream

TokenStream

Returns

GrammarParserInterpreter

Implementation of

ts
IGrammar.createGrammarParserInterpreter

createLexerInterpreter()

ts
createLexerInterpreter(input): LexerInterpreter;

Parameters

input

CharStream

Returns

LexerInterpreter

Implementation of

ts
IGrammar.createLexerInterpreter

createParserInterpreter()

ts
createParserInterpreter(tokenStream): ParserInterpreter;

For testing.

Parameters

tokenStream

TokenStream

Returns

ParserInterpreter

Implementation of

ts
IGrammar.createParserInterpreter

defineAction()

ts
defineAction(atAST): void;

Parameters

atAST

GrammarAST

Returns

void

Implementation of

ts
IGrammar.defineAction

defineChannelName()

ts
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

ts
IGrammar.defineChannelName

defineRule()

ts
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

ts
IGrammar.defineRule

defineStringLiteral()

ts
defineStringLiteral(lit, ttype?): number;

Parameters

lit

string

ttype?

number

Returns

number

Implementation of

ts
IGrammar.defineStringLiteral

defineTokenAlias()

ts
defineTokenAlias(name, lit): number;

Parameters

name

string

lit

string

Returns

number

Implementation of

ts
IGrammar.defineTokenAlias

defineTokenName()

ts
defineTokenName(name, ttype?): number;

Parameters

name

string

ttype?

number

Returns

number

Implementation of

ts
IGrammar.defineTokenName

getAllCharValues()

ts
getAllCharValues(): IntervalSet;

Returns

IntervalSet

min to max char as defined by the target. If no target, use max unicode char value.

Implementation of

ts
IGrammar.getAllCharValues

getAllImportedGrammars()

ts
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

ts
IGrammar.getAllImportedGrammars

getATN()

ts
getATN(): ATN;

Needed for tests.

Returns

ATN

Implementation of

ts
IGrammar.getATN

getChannelValue()

ts
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

ts
IGrammar.getChannelValue

getDefaultActionScope()

ts
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

ts
IGrammar.getDefaultActionScope

getGrammarAncestors()

ts
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

ts
IGrammar.getGrammarAncestors

getImportedGrammar()

ts
getImportedGrammar(name): null | Grammar;

Parameters

name

string

Returns

null | Grammar

grammar directly imported by this grammar.

Implementation of

ts
IGrammar.getImportedGrammar

getImportedGrammars()

ts
getImportedGrammars(): Grammar[];

Returns

Grammar[]

Implementation of

ts
IGrammar.getImportedGrammars

getIndexToPredicateMap()

ts
getIndexToPredicateMap(): Map<number, PredAST>;

Returns

Map<number, PredAST>

Implementation of

ts
IGrammar.getIndexToPredicateMap

getLanguage()

ts
getLanguage(): 
  | "Cpp"
  | "CSharp"
  | "Dart"
  | "Go"
  | "JavaScript"
  | "Java"
  | "PHP"
  | "Python3"
  | "Swift"
  | "TypeScript";

Returns

| "Cpp" | "CSharp" | "Dart" | "Go" | "JavaScript" | "Java" | "PHP" | "Python3" | "Swift" | "TypeScript"

Implementation of

ts
IGrammar.getLanguage

getMaxCharValue()

ts
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

ts
IGrammar.getMaxCharValue

getMaxTokenType()

ts
getMaxTokenType(): number;

How many token types have been allocated so far?

Returns

number

Implementation of

ts
IGrammar.getMaxTokenType

getNewChannelNumber()

ts
getNewChannelNumber(): number;

Returns

number

a new unique integer in the channel value space.

Implementation of

ts
IGrammar.getNewChannelNumber

getNewTokenType()

ts
getNewTokenType(): number;

Returns

number

a new unique integer in the token type space.

Implementation of

ts
IGrammar.getNewTokenType

getOptionString()

ts
getOptionString(key): undefined | string;

Parameters

key

string

Returns

undefined | string

Implementation of

ts
IGrammar.getOptionString

getOutermostGrammar()

ts
getOutermostGrammar(): Grammar;

Returns

Grammar

the grammar that imported us and our parents, or this if we're root.

Implementation of

ts
IGrammar.getOutermostGrammar

getPredicateDisplayString()

ts
getPredicateDisplayString(pred): string;

Parameters

pred

Predicate

Returns

string

Implementation of

ts
IGrammar.getPredicateDisplayString

getRecognizerName()

ts
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

ts
IGrammar.getRecognizerName

getRule()

Call Signature

ts
getRule(name): null | Rule;
Parameters
name

string | number

Returns

null | Rule

Implementation of
ts
IGrammar.getRule

Call Signature

ts
getRule(grammarName, ruleName): null | Rule;
Parameters
grammarName

string

ruleName

string

Returns

null | Rule

Implementation of
ts
IGrammar.getRule

getRuleNames()

ts
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

ts
IGrammar.getRuleNames

getStringLiteralLexerRuleName()

ts
getStringLiteralLexerRuleName(_literal): string;

Parameters

_literal

string

Returns

string

Implementation of

ts
IGrammar.getStringLiteralLexerRuleName

getStringLiterals()

ts
getStringLiterals(): Set<string>;

Returns

Set<string>

Implementation of

ts
IGrammar.getStringLiterals

getTokenDisplayName()

ts
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

ts
IGrammar.getTokenDisplayName

getTokenDisplayNames()

ts
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

ts
IGrammar.getTokenDisplayNames

getTokenLiteralNames()

ts
getTokenLiteralNames(): (null | string)[];

Gets the literal names assigned to tokens in the grammar.

Returns

(null | string)[]

Implementation of

ts
IGrammar.getTokenLiteralNames

getTokenName()

ts
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

ts
IGrammar.getTokenName

getTokenNames()

ts
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

ts
IGrammar.getTokenNames

getTokenSymbolicNames()

ts
getTokenSymbolicNames(): (null | string)[];

Gets the symbolic names assigned to tokens in the grammar.

Returns

(null | string)[]

Implementation of

ts
IGrammar.getTokenSymbolicNames

getTokenType()

ts
getTokenType(token): number;

Parameters

token

string

Returns

number

Implementation of

ts
IGrammar.getTokenType

getTokenTypes()

ts
getTokenTypes(): IntervalSet;

Returns

IntervalSet

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

Implementation of

ts
IGrammar.getTokenTypes

getTypeString()

ts
getTypeString(): null | string;

Returns

null | string

Implementation of

ts
IGrammar.getTypeString

getVocabulary()

ts
getVocabulary(): Vocabulary;

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

Returns

Vocabulary

Implementation of

ts
IGrammar.getVocabulary

importTokensFromTokensFile()

ts
importTokensFromTokensFile(): void;

Returns

void

Implementation of

ts
IGrammar.importTokensFromTokensFile

importVocab()

ts
importVocab(importG): void;

Parameters

importG

Grammar

Returns

void

Implementation of

ts
IGrammar.importVocab

initTokenSymbolTables()

ts
protected initTokenSymbolTables(): void;

Returns

void

Implementation of

ts
IGrammar.initTokenSymbolTables

isCombined()

ts
isCombined(): boolean;

Returns

boolean

Implementation of

ts
IGrammar.isCombined

isLexer()

ts
isLexer(): boolean;

Returns

boolean

Implementation of

ts
IGrammar.isLexer

isParser()

ts
isParser(): boolean;

Returns

boolean

Implementation of

ts
IGrammar.isParser

loadImportedGrammars()

ts
loadImportedGrammars(visited): void;

Parameters

visited

Set<string>

Returns

void

Implementation of

ts
IGrammar.loadImportedGrammars

resolvesToAttributeDict()

ts
resolvesToAttributeDict(x, node): boolean;

Parameters

x

string

node

ActionAST

Returns

boolean

Implementation of

ts
IGrammar.resolvesToAttributeDict

resolvesToLabel()

ts
resolvesToLabel(x, node): boolean;

Parameters

x

string

node

ActionAST

Returns

boolean

Implementation of

ts
IGrammar.resolvesToLabel

resolvesToListLabel()

ts
resolvesToListLabel(x, node): boolean;

Parameters

x

string

node

ActionAST

Returns

boolean

Implementation of

ts
IGrammar.resolvesToListLabel

resolvesToToken()

ts
resolvesToToken(x, node): boolean;

Parameters

x

string

node

ActionAST

Returns

boolean

Implementation of

ts
IGrammar.resolvesToToken

resolveToAttribute()

Call Signature

ts
resolveToAttribute(x, node): IAttribute;
Parameters
x

string

node

ActionAST

Returns

IAttribute

Implementation of
ts
IGrammar.resolveToAttribute

Call Signature

ts
resolveToAttribute(
   x, 
   y, 
   node): null | IAttribute;
Parameters
x

string

y

string

node

ActionAST

Returns

null | IAttribute

Implementation of
ts
IGrammar.resolveToAttribute

setChannelNameForValue()

ts
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

ts
IGrammar.setChannelNameForValue

setTokenForType()

ts
setTokenForType(ttype, text): void;

Parameters

ttype

number

text

string

Returns

void

Implementation of

ts
IGrammar.setTokenForType

undefineRule()

ts
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

ts
IGrammar.undefineRule

defAlias()

ts
protected static defAlias(
   r, 
   pattern, 
   wiz, 
   lexerRuleToStringLiteral): boolean;

Parameters

r

GrammarAST

pattern

string

wiz

TreeWizard

lexerRuleToStringLiteral

[GrammarAST, GrammarAST][]

Returns

boolean


forFile()

ts
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?

ToolListener

Returns

T


getGrammarTypeToFileNameSuffix()

ts
static getGrammarTypeToFileNameSuffix(type): string;

Parameters

type

GrammarType

Returns

string


getStringLiteralAliasesFromLexerRules()

ts
static getStringLiteralAliasesFromLexerRules(ast): 
  | null
  | [GrammarAST, GrammarAST][];

Parameters

ast

GrammarRootAST

Returns

| null | [GrammarAST, GrammarAST][]

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


setNodeOptions()

ts
static setNodeOptions(node, options): void;

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

Parameters

node

GrammarAST

options

GrammarAST

Returns

void