antlr-ng
Version:
Next generation ANTLR Tool
115 lines (114 loc) • 5.63 kB
TypeScript
import { Grammar } from "src/tool/Grammar.js";
import type { CodeGenerator } from "./CodeGenerator.js";
/** Represets a single code point in Unicode. */
export type CodePoint = number;
/**
* Base class for all target generators. It provides some common functionality.
* The actual code generation is done in the subclasses.
*/
export declare class GeneratorHelper {
static readonly defaultCharValueEscape: Map<number, string>;
/**
* For pure strings of Unicode char, how can we display it in the target language as a literal. Useful for dumping
* predicates and such that may refer to chars that need to be escaped when represented as strings.
*
* @returns The default map with the most common escape sequences. Subclasses can override this method to provide
* additional or different escape sequences.
*/
static getTargetCharValueEscape(): Map<CodePoint, string> | undefined;
static escapeIfNeeded(identifier: string): 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.
*
* @param g The grammar object.
* @param ttype The token type to convert.
*
* @returns The token type as a string.
*/
static getTokenTypeAsTargetLabel(g: Grammar, ttype: number): string;
static getTokenTypesAsTargetLabels(g: Grammar, tokenTypes: number[]): 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. For example, if the incoming string has actual
* newline characters, the output of this method would convert them to the two char sequence \n for Java, C,
* C++, ... The new string has double-quotes around it as well. Example string in memory:
*
* ```
* a"[newlineChar]b'c[carriageReturnChar]d[tab]e\f
* ```
* would be converted to the valid string:
* ```
* "a\"\nb'c\rd\te\\f"
* ```
* or
* ```
* a\"\nb'c\rd\te\\f
* ```
* depending on the quoted arg.
*
* @param s The string to convert.
* @param quoted If true, the string is quoted. If false, it is not.
*
* @returns The converted string.
*/
static getTargetStringLiteralFromString(s: string, quoted?: boolean): string;
/**
* Converts from an antlr-ng 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 {@link String.replaceAll} cannot handle both `\"` and `"` without a lot of messing around.
*
* @param generator The code generator instance.
* @param literal The string literal to convert.
* @param addQuotes If true, the string is quoted. If false, it is not.
* @param escapeSpecial If true, escape special characters.
*
* @returns The converted string.
*/
static getTargetStringLiteralFromANTLRStringLiteral(generator: CodeGenerator, literal: string, addQuotes: boolean, escapeSpecial?: boolean): string;
/**
* Generates a list of entries in a format that is suitable for TypeScript.
* The list is split into multiple lines if it exceeds the specified wrap length.
*
* @param list The list of entries to be formatted. We are using string coercion here, so make sure any
* non-primitive type is rendered to a string, before calling this method.
* @param wrap The maximum number of elements per line.
*
* @returns A string representation of the formatted list.
*/
static renderList(list: unknown[], wrap: number): string;
/**
* Takes a list of lines and formats them with the specified indentation. It filters out undefined
* lines, and adds the specified indentation to each line.
*
* @param lines The lines to format.
* @param indentation The number of spaces to use for indentation.
*
* @returns The formatted lines as a single string.
*/
static formatLines(lines: Array<string | undefined>, indentation: number): string;
static toTitleCase(str: string): string;
protected static escapeWord(word: string): string;
protected static escapeCodePoint(codePoint: number): string;
protected static shouldUseUnicodeEscapeForCodePointInDoubleQuotedString(codePoint: number): boolean;
protected static escapeChar(v: number): string;
/**
* Escapes the Unicode code point appropriately for this language and append the escaped value to `sb`.
* It exists for flexibility and backward compatibility with external targets, The static method
* {@link UnicodeEscapes.appendEscapedCodePoint(StringBuilder, int, String)} can be used as well
* if default escaping method (Java) is used or language is officially supported
*
* @param codePoint The code point to escape.
* @param escape If true, the code point is escaped.
*
* @returns The escaped code point as a string.
*/
private static createUnicodeEscapedCodePoint;
}