antlr-ng
Version:
Next generation ANTLR Tool
170 lines (169 loc) • 7.68 kB
TypeScript
import * as OutputModelObjects from "src/codegen/model/index.js";
import { GeneratorBase } from "../codegen/GeneratorBase.js";
import type { ITargetGenerator, Lines } from "../codegen/ITargetGenerator.js";
import type { Grammar } from "../tool/Grammar.js";
import type { Rule } from "../tool/Rule.js";
export declare class TypeScriptTargetGenerator extends GeneratorBase implements ITargetGenerator {
readonly id = "generator.default.typescript";
readonly language = "TypeScript";
readonly languageSpecifiers: string[];
readonly codeFileExtension = ".ts";
readonly contextNameSuffix = "Context";
readonly lexerRuleContext = "antlr.ParserRuleContext";
/** The rule context name is the rule followed by a suffix, e.g. r becomes rContext. */
readonly ruleContextNameSuffix = "Context";
/**
* https://github.com/microsoft/TypeScript/issues/2536
*/
readonly reservedWords: Set<string>;
readonly lexerCommandMap: Map<string, () => Lines>;
readonly lexerCallCommandMap: Map<string, (arg: string, grammar: Grammar) => Lines>;
private static readonly defaultValues;
/**
* Code blocks are at the heart of code generation. This map allows easy lookup of the correct render method for a
* specific output model code object, named a `SrcOp`. Source ops are code snippets that are put together
* to form the final code.
*/
private readonly srcOpMap;
constructor();
renderParserFile(parserFile: OutputModelObjects.ParserFile): string;
renderLexerFile(lexerFile: OutputModelObjects.LexerFile): string;
renderBaseListenerFile(file: OutputModelObjects.ListenerFile, declaration: boolean): string;
renderListenerFile(listenerFile: OutputModelObjects.ListenerFile): string;
renderBaseVisitorFile(file: OutputModelObjects.VisitorFile, declaration: boolean): string;
renderVisitorFile(visitorFile: OutputModelObjects.VisitorFile, declaration: boolean): string;
renderLexerRuleContext(): Lines;
getRuleFunctionContextStructName(r: Rule): string;
renderRecRuleReplaceContext(ctxName: string): Lines;
renderRecRuleAltPredicate(ruleName: string, opPrec: number): Lines;
renderRecRuleSetReturnAction(src: string, name: string): Lines;
renderRecRuleSetStopToken(): Lines;
renderRecRuleSetPrevCtx(): Lines;
renderRecRuleLabeledAltStartAction(parserName: string, ruleName: string, currentAltLabel: string, label: string | undefined, isListLabel: boolean): Lines;
renderRecRuleAltStartAction(parserName: string, ruleName: string, ctxName: string, label: string | undefined, isListLabel: boolean): Lines;
renderTestFile(grammarName: string, lexerName: string, parserName: string | undefined, parserStartRuleName: string | undefined, showDiagnosticErrors: boolean, traceATN: boolean, profile: boolean, showDFA: boolean, useListener: boolean, useVisitor: boolean, predictionMode: string, buildParseTree: boolean): string;
private renderParser;
private renderLexer;
private renderActionHandlers;
private renderRuleActionFunction;
/**
* This generates a private method since the predIndex is generated, making an overriding implementation
* impossible to maintain.
*/
private renderRuleSempredFunction;
private renderRuleFunction;
private renderSourceOps;
private renderLeftRecursiveRuleFunction;
private renderCodeBlockForOuterMostAlt;
private renderCodeBlockForAlt;
private renderDecls;
private renderLL1AltBlock;
private renderLL1OptionalBlock;
private renderLL1OptionalBlockSingleAlt;
private renderLL1StarBlockSingleAlt;
private renderLL1PlusBlockSingleAlt;
/**
* Renders the decision block for multiple alternatives. It starts by rendering some management code
* and then renders the switch statement with the alternatives (as case branches).
*
* @param outputFile The current output file (lexer, parser etc.).
* @param recognizerName The name of the recognizer (parser or lexer).
* @param choice The choice to render.
*
* @returns The rendered lines of code.
*/
private renderAltBlock;
private renderOptionalBlock;
private renderStarBlock;
private renderPlusBlock;
private renderThrowNoViableAlt;
private renderTestSetInline;
/** Produces smaller bytecode only when `bits.ttypes` contains more than two items. */
private renderBitsetBitfieldComparison;
private renderBitsetInlineComparison;
private renderOffsetShiftVar;
/** Javascript language spec - shift operators are 32 bits long max. */
private renderTestShiftInRange;
private renderCases;
private renderInvokeRule;
private renderMatchToken;
private renderMatchSet;
private renderMatchNotSet;
private renderCommonSetStuff;
private renderWildcard;
private renderAction;
private renderSemPred;
private renderExceptionClause;
private renderActionText;
private renderArgRef;
private renderLabelRef;
private renderListLabelRef;
private renderLocalRef;
private renderNonLocalAttrRef;
private renderQRetValueRef;
private renderRetValueRef;
private renderRulePropertyRef;
private renderRulePropertyRefCtx;
private renderRulePropertyRefParser;
private renderRulePropertyRefStart;
private renderRulePropertyRefStop;
private renderRulePropertyRefText;
private renderSetAttr;
private renderSetNonLocalAttr;
private renderThisRulePropertyRefCtx;
private renderThisRulePropertyRefParser;
private renderThisRulePropertyRefStart;
private renderThisRulePropertyRefStop;
private renderThisRulePropertyRefText;
private renderTokenLabelType;
private renderTokenPropertyRefChannel;
private renderTokenPropertyRefIndex;
private renderTokenPropertyRefInt;
private renderTokenPropertyRefLine;
private renderTokenPropertyRefPos;
private renderTokenPropertyRefText;
private renderTokenPropertyRefType;
/** How to translate $tokenLabel */
private renderTokenRef;
private renderInputSymbolType;
private renderAddToLabelList;
private renderTokenDecl;
private renderTokenTypeDecl;
private renderTokenListDecl;
private renderRuleContextDecl;
private renderRuleContextListDecl;
private renderContextTokenGetterDecl;
private renderContextTokenListGetterDecl;
private renderContextTokenListIndexedGetterDecl;
private renderContextRuleGetterDecl;
private renderContextRuleListGetterDecl;
private renderContextRuleListIndexedGetterDecl;
private renderCaptureNextToken;
private renderCaptureNextTokenType;
private renderStructDecl;
private renderAltLabelStructDecl;
private renderListenerDispatchMethod;
private renderVisitorDispatchMethod;
private renderAttributeDecl;
private renderSerializedATN;
private renderLexerSkipCommand;
private renderLexerMoreCommand;
private renderLexerPopModeCommand;
private renderLexerTypeCommand;
private renderLexerChannelCommand;
private renderLexerModeCommand;
private renderLexerPushModeCommand;
private renderActionChunks;
private renderImplicitTokenLabel;
private renderImplicitRuleLabel;
private renderImplicitSetLabel;
private renderListLabelName;
/** For any action chunk, what is correctly-typed context struct ptr? */
private renderContext;
/** Only casts localContext to the type when the cast isn't redundant (i.e. to a sub-context for a labeled alt) */
private renderTypedContext;
private renderFileHeader;
private initValue;
private startRendering;
private endRendering;
}