Skip to content

antlr-ng Tool Class Hierarchy / codegen/ITargetGenerator / ITargetGenerator

Interface: ITargetGenerator ​

Defines the structure for a target generator.

Extends ​

Properties ​

codeFileExtension ​

ts
readonly codeFileExtension: string;

The extension to be used for generated files which contain code (including the dot).


contextNameSuffix ​

ts
readonly contextNameSuffix: string;

The rule context name is the rule followed by a suffix; e.g., r becomes rContext.


declarationFileExtension? ​

ts
readonly optional declarationFileExtension: string;

The extension to be used for generated files which contain type definitions (including the dot).


id ​

ts
readonly id: string;

A unique identifier for the generator.


inlineTestSetWordSize ​

ts
readonly inlineTestSetWordSize: number;

How many bits should be used to do inline token type tests? Java assumes a 64-bit word for bitsets. Must be a valid word size for your target like 8, 16, 32, 64, etc...


inputFilter()? ​

ts
optional inputFilter: (grammar) => string;

Allows to alter a grammar text before it is processed by antlr-ng.

Parameters ​

grammar ​

string

Returns ​

string


isATNSerializedAsInts ​

ts
readonly isATNSerializedAsInts: boolean;

language ​

ts
readonly language: string;

The human readably language name for the generator.


languageSpecifiers ​

ts
readonly languageSpecifiers: string[];

A list of specifiers that can be used to identify this language (not case sensitive).


lexerCallCommandMap ​

ts
readonly lexerCallCommandMap: Map<string, (arg, grammar) => Lines>;

Maps lexer call commands to methods which render that command. The first argument is the command argument, the second is the grammar object.


lexerCommandMap ​

ts
readonly lexerCommandMap: Map<string, () => Lines>;

Maps lexer commands to methods which render that command.


lexerRuleContext ​

ts
readonly lexerRuleContext: string;

needsDeclarationFile? ​

ts
readonly optional needsDeclarationFile: boolean;

Does this target need a declaration file (e.g. a C/C++ header file or type definitions for JavaScript)?


outputFilter()? ​

ts
optional outputFilter: (code) => string;

Allows to alter the output of antlr-ng after it was processed by the generator (e.g. to remove unwanted parts). This is called once per generated file, right before it is written to the file system.

Parameters ​

code ​

string

Returns ​

string


reservedWords ​

ts
readonly reservedWords: Set<string>;

Reserved words of this language.


ruleContextNameSuffix ​

ts
readonly ruleContextNameSuffix: string;

supportsOverloadedMethods ​

ts
readonly supportsOverloadedMethods: boolean;

tokenNameTransformer()? ​

ts
optional tokenNameTransformer: (name) => string;

Allows to transform a token identifier to a different string (e.g. to avoid keyword collissions).

Parameters ​

name ​

string

Returns ​

string


version ​

ts
readonly version: string;

The version of the generator.


wantsBaseListener ​

ts
readonly wantsBaseListener: boolean;

wantsBaseVisitor ​

ts
readonly wantsBaseVisitor: boolean;

Methods ​

escapeIfNeeded() ​

ts
escapeIfNeeded(identifier): string;

Parameters ​

identifier ​

string

Returns ​

string

Inherited from ​

ITargetGeneratorCallables.escapeIfNeeded


getBaseListenerFileName() ​

ts
getBaseListenerFileName(forDeclarationFile, grammarName): string;

A given grammar T, return a blank listener implementation such as TBaseListener.java, if we're using the Java target.

Parameters ​

forDeclarationFile ​

boolean

If true, the file name will be for a declaration file (e.g. TParser.h), otherwise it will be for a code file (e.g. TParser.java).

grammarName ​

string

The name of the grammar, which determinse the base name of the listener.

Returns ​

string

The file name for the base listener, such as TBaseListener.java or TBaseListener.ts.


getBaseVisitorFileName() ​

ts
getBaseVisitorFileName(forDeclarationFile, grammarName): string;

A given grammar T, return a blank vistor implementation such as TBaseListener.java, if we're using the Java target.

Parameters ​

forDeclarationFile ​

boolean

If true, the file name will be for a declaration file (e.g. TParser.h), otherwise it will be for a code file (e.g. TParser.java).

grammarName ​

string

The name of the grammar, which determinse the base name of the listener.

Returns ​

string

The file name for the base visitor, such as TBaseVisitor.java or TBaseVisitor.ts.


getListenerFileName() ​

ts
getListenerFileName(forDeclarationFile, grammarName): string;

A given grammar T, return the listener name such as TListener.java, if we're using the Java target.

Parameters ​

forDeclarationFile ​

boolean

If true, the file name will be for a declaration file (e.g. TParser.h), otherwise it will be for a code file (e.g. TParser.java).

grammarName ​

string

The name of the grammar, which determinse the base name of the listener.

Returns ​

string

The file name for the listener, such as TListener.java or TListener.ts.


getLoopLabel() ​

ts
getLoopLabel(ast): string;

Parameters ​

ast ​

GrammarAST

Returns ​

string


getRecognizerFileName() ​

ts
getRecognizerFileName(forDeclarationFile, recognizerName): string;

Generate TParser.java and TLexer.java from T.g4 if combined, else just use T.java as output regardless of type.

Parameters ​

forDeclarationFile ​

boolean

If true, the file name will be for a declaration file (e.g. TParser.h), otherwise it will be for a code file (e.g. TParser.java).

recognizerName ​

string

The name of the recognizer, such as TParser or TLexer.

Returns ​

string

The file name for the recognizer, such as TParser.java or TLexer.cpp.


getRuleFunctionContextStructName() ​

ts
getRuleFunctionContextStructName(r): string;

Parameters ​

r ​

Rule

Returns ​

string

the full name for a rule function.


getSerializedATNSegmentLimit() ​

ts
getSerializedATNSegmentLimit(): number;

Gets the maximum number of 16-bit unsigned integers that can be encoded in a single segment (a declaration in target language) of the serialized ATN. E.g., in C++, a small segment length results in multiple decls like:

static const int32_t serializedATNSegment1[] = { 0x7, 0x12, 0x2, 0x13, 0x7, 0x13, 0x2, 0x14, 0x7, 0x14, 0x2, 0x15, 0x7, 0x15, 0x2, 0x16, 0x7, 0x16, 0x2, 0x17, 0x7, 0x17, 0x2, 0x18, 0x7, 0x18, 0x2, 0x19, 0x7, 0x19, 0x2, 0x1a, 0x7, 0x1a, 0x2, 0x1b, 0x7, 0x1b, 0x2, 0x1c, 0x7, 0x1c, 0x2, 0x1d, 0x7, 0x1d, 0x2, 0x1e, 0x7, 0x1e, 0x2, 0x1f, 0x7, 0x1f, 0x2, 0x20, 0x7, 0x20, 0x2, 0x21, 0x7, 0x21, 0x2, 0x22, 0x7, 0x22, 0x2, 0x23, 0x7, 0x23, 0x2, 0x24, 0x7, 0x24, 0x2, 0x25, 0x7, 0x25, 0x2, 0x26, };

instead of one big one. Targets are free to ignore this like JavaScript does.

This is primarily needed by Java target to limit size of any single ATN string to 65k length.

SerializedATN.getSegments

Returns ​

number

the serialized ATN segment limit


getTargetStringLiteralFromANTLRStringLiteral() ​

ts
getTargetStringLiteralFromANTLRStringLiteral(
   literal, 
   addQuotes, 
   escapeSpecial?): string;

Converts from an TypeScript string literal found in a grammar file to an equivalent string literal in the target language.

For Java, this is the translation 'a\n"' -> "a\n\"". Expect single quotes around the incoming literal. Just flip the quotes and replace double quotes with \".

Note that we have decided to allow people to use '"' without penalty, so we must build the target string in a loop as String.replaceAll cannot handle both \" and " without a lot of messing around.

Parameters ​

literal ​

string

The string literal to convert.

addQuotes ​

boolean

If true, the string is quoted. If false, it is not.

escapeSpecial? ​

boolean

If true, escape special characters.

Returns ​

string

The converted string.


getTargetStringLiteralFromString() ​

ts
getTargetStringLiteralFromString(s, quoted?): string;

Given a random string of unicode chars, return a new string with optionally appropriate quote characters for target language and possibly with some escaped characters.

Parameters ​

s ​

string

The string to be converted.

quoted? ​

boolean

If true, the string will be quoted.

Returns ​

string

The converted string.


getTokenTypeAsTargetLabel() ​

ts
getTokenTypeAsTargetLabel(g, ttype): string;

Get a meaningful name for a token type useful during code generation. Literals without associated names are converted to the string equivalent of their integer values. Used to generate x==ID and x==34 type comparisons etc... Essentially we are looking for the most obvious way to refer to a token type in the generated code.

Parameters ​

g ​

Grammar

The grammar object containing the token names.

ttype ​

number

The token type to convert to a label.

Returns ​

string

The token type as a label. If the token type is not defined in the grammar, it returns the string representation of the token type.


getVisitorFileName() ​

ts
getVisitorFileName(forDeclarationFile, grammarName): string;

A given grammar T, return the visitor name such as TVisitor.java, if we're using the Java target.

Parameters ​

forDeclarationFile ​

boolean

If true, the file name will be for a declaration file (e.g. TParser.h), otherwise it will be for a code file (e.g. TParser.java).

grammarName ​

string

The name of the grammar, which determinse the base name of the listener.

Returns ​

string

The file name for the visitor, such as TVisitor.java or TVisitor.ts.


renderBaseListenerFile() ​

ts
renderBaseListenerFile(file, declaration): string;

Renders a base version of a ListenerFile output model. This base class implements all methods of the listener interface as empty methods. This is useful for the user to create a custom listener class by extending this base class and overriding only the methods of interest.

Languages that support optional methods (e.g. TypeSricpt) don't need such a base class. Custom listeners can simply implement the interface and override only the methods of interest. The code generator has to insert code to test if the method is implemented or not.

Parameters ​

file ​

ListenerFile

The model object for details.

declaration ​

boolean

If true, render the declaration file for the base listener file.

Returns ​

string

The generated code as a string.

Inherited from ​

ITargetGeneratorCallables.renderBaseListenerFile


renderBaseVisitorFile() ​

ts
renderBaseVisitorFile(file, declaration): string;

Renders a base version of a VisitorFile output model. This base class implements all methods of the visitor interface as empty methods. This is useful for the user to create a custom vistitors class by extending this base class and overriding only the methods of interest.

Languages that support optional methods (e.g. TypeSricpt) don't need such a base class. Custom listeners can simply implement the interface and override only the methods of interest. The code generator has to insert code to test if the method is implemented or not.

Parameters ​

file ​

VisitorFile

The model object for details.

declaration ​

boolean

If true, render the declaration file for the base visitor file.

Returns ​

string

The generated code as a string.

Inherited from ​

ITargetGeneratorCallables.renderBaseVisitorFile


renderImplicitRuleLabel() ​

ts
renderImplicitRuleLabel(ruleName): string;

Parameters ​

ruleName ​

string

Returns ​

string

Inherited from ​

ITargetGeneratorCallables.renderImplicitRuleLabel


renderImplicitSetLabel() ​

ts
renderImplicitSetLabel(id): string;

Parameters ​

id ​

string

Returns ​

string

Inherited from ​

ITargetGeneratorCallables.renderImplicitSetLabel


renderImplicitTokenLabel() ​

ts
renderImplicitTokenLabel(tokenName): string;

Should be same for all refs to same token like ctx.ID within single rule function for literals like 'while', we gen _s<ttype>

Parameters ​

tokenName ​

string

Returns ​

string

Inherited from ​

ITargetGeneratorCallables.renderImplicitTokenLabel


renderLexerFile() ​

ts
renderLexerFile(file, declaration): string;

Renders a LexerFile output model.

Parameters ​

file ​

LexerFile

The model object for details.

declaration ​

boolean

If true, render the declaration file for the lexer file.

Returns ​

string

The generated code as a string.

Inherited from ​

ITargetGeneratorCallables.renderLexerFile


renderListenerFile() ​

ts
renderListenerFile(file, declaration): string;

Renders a ListenerFile output model.

Parameters ​

file ​

ListenerFile

The model object for details.

declaration ​

boolean

If true, render the declaration file for the listener file.

Returns ​

string

The generated code as a string.

Inherited from ​

ITargetGeneratorCallables.renderListenerFile


renderListLabelName() ​

ts
renderListLabelName(label): string;

Parameters ​

label ​

string

Returns ​

string

Inherited from ​

ITargetGeneratorCallables.renderListLabelName


renderParserFile() ​

ts
renderParserFile(file, declaration): string;

Renders a ParserFile output model.

Parameters ​

file ​

ParserFile

The model object for details.

declaration ​

boolean

If true, render the declaration file for the parser file.

Returns ​

string

The generated code as a string.

Inherited from ​

ITargetGeneratorCallables.renderParserFile


renderRecRuleAltPredicate() ​

ts
renderRecRuleAltPredicate(ruleName, opPrec): Lines;

Parameters ​

ruleName ​

string

opPrec ​

number

Returns ​

Lines


renderRecRuleAltStartAction() ​

ts
renderRecRuleAltStartAction(
   parserName, 
   ruleName, 
   ctxName, 
   label, 
   isListLabel): Lines;

Parameters ​

parserName ​

string

ruleName ​

string

ctxName ​

string

label ​

undefined | string

isListLabel ​

boolean

Returns ​

Lines


renderRecRuleLabeledAltStartAction() ​

ts
renderRecRuleLabeledAltStartAction(
   parserName, 
   ruleName, 
   currentAltLabel, 
   label, 
   isListLabel): Lines;

Parameters ​

parserName ​

string

ruleName ​

string

currentAltLabel ​

string

label ​

undefined | string

isListLabel ​

boolean

Returns ​

Lines


renderRecRuleReplaceContext() ​

ts
renderRecRuleReplaceContext(ctxName): Lines;

Part of the left-recursive-rule pre-rendering.

Parameters ​

ctxName ​

string

Returns ​

Lines


renderRecRuleSetPrevCtx() ​

ts
renderRecRuleSetPrevCtx(): Lines;

Returns ​

Lines


renderRecRuleSetReturnAction() ​

ts
renderRecRuleSetReturnAction(src, name): Lines;

Parameters ​

src ​

string

name ​

string

Returns ​

Lines


renderRecRuleSetStopToken() ​

ts
renderRecRuleSetStopToken(): Lines;

Returns ​

Lines


renderTestFile() ​

ts
renderTestFile(
   grammarName, 
   lexerName, 
   parserName, 
   parserStartRuleName, 
   showDiagnosticErrors, 
   traceATN, 
   profile, 
   showDFA, 
   useListener, 
   useVisitor, 
   predictionMode, 
   buildParseTree): string;

Renders the content of the unit test file with the given parameters. The test file is used to execute a single unit test for a grammar. It contains the necessary imports, the test function and the code to parse a given input string with the specified parser and lexer.

Parameters ​

grammarName ​

string

The name of the grammar to be tested.

lexerName ​

string

The name of the lexer to be used in the test.

parserName ​

The name of the parser to be used in the test.

undefined | string

parserStartRuleName ​

The start rule of the parser to be used in the test.

undefined | string

showDiagnosticErrors ​

boolean

If true, the test will show diagnostic errors in the output.

traceATN ​

boolean

If true, the test will print ATN tracing information.

profile ​

boolean

If true, the test will profile the parsing process.

showDFA ​

boolean

If true, the test will show the DFA state information from the lexer.

useListener ​

boolean

If true, the test will use a listener to print processing information.

useVisitor ​

boolean

If true, the test will use a visitor. This is used mostly to test importing the generated visitor class.

predictionMode ​

string

The prediction mode to be used in the test. This can be one of the following: "LL", "SLL", "LL_EXACT_AMBIG_DETECTION". If undefined, the default prediction mode of the parser will be used.

buildParseTree ​

boolean

If true, the test will build a parse tree from the input string and print it.

Returns ​

string

Inherited from ​

ITargetGeneratorCallables.renderTestFile


renderVisitorFile() ​

ts
renderVisitorFile(file, declaration): string;

Renders a VistiorFile output model.

Parameters ​

file ​

VisitorFile

The model object for details.

declaration ​

boolean

If true, render the declaration file for the visitor file.

Returns ​

string

The generated code as a string.

Inherited from ​

ITargetGeneratorCallables.renderVisitorFile