antlr-ng
Version:
Next generation ANTLR Tool
190 lines (189 loc) • 4.71 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
import * as path from "path";
import { GrammarType } from "../../support/GrammarType.js";
import { Target } from "../Target.js";
class GoTarget extends Target {
static {
__name(this, "GoTarget");
}
static reservedWords = /* @__PURE__ */ new Set([
// keywords
"break",
"default",
"func",
"interface",
"select",
"case",
"defer",
"go",
"map",
"struct",
"chan",
"else",
"goto",
"package",
"switch",
"const",
"fallthrough",
"if",
"range",
"type",
"continue",
"for",
"import",
"return",
"var",
// pre-declared identifiers https://golang.org/ref/spec#Predeclared_identifiers
"bool",
"byte",
"complex64",
"complex128",
"error",
"float32",
"float64",
"int",
"int8",
"int16",
"int32",
"int64",
"rune",
"string",
"uint",
"uint8",
"uint16",
"uint32",
"uint64",
"uintptr",
"true",
"false",
"iota",
"nil",
"append",
"cap",
"close",
"complex",
"copy",
"delete",
"imag",
"len",
"make",
"new",
"panic",
"print",
"println",
"real",
"recover",
"string",
// interface definition of RuleContext from runtime/Go/antlr/rule_context.go
"Accept",
"GetAltNumber",
"GetBaseRuleContext",
"GetChild",
"GetChildCount",
"GetChildren",
"GetInvokingState",
"GetParent",
"GetPayload",
"GetRuleContext",
"GetRuleIndex",
"GetSourceInterval",
"GetText",
"IsEmpty",
"SetAltNumber",
"SetInvokingState",
"SetParent",
"String",
// misc
"rule",
"parserRule",
"action",
// The use of start or stop abd others as a label name will cause the generation of a GetStart() or
// GetStop() method, which then clashes with the GetStart() or GetStop() method that is generated by the
// code gen for the rule. So, we need to convert it. This is not ideal as it will still probably confuse
// authors of parse listeners etc. but the code will compile.
"start",
"stop",
"exception"
]);
static doGoFormat = (() => {
let result = false;
let value = process.env.ANTLR_GO_DISABLE_GOFMT;
if (value == null) {
value = process.env["antlr.go.disable-gofmt"];
}
if (value != null) {
result = value.toLowerCase() === "true";
}
return result;
})();
getRecognizerFileName(header) {
const gen = this.getCodeGenerator();
const g = gen.g;
let name;
switch (g.type) {
case GrammarType.Parser: {
name = g.name.endsWith("Parser") ? g.name.substring(0, g.name.length - 6) : g.name;
return name.toLowerCase() + "_parser.go";
}
case GrammarType.Lexer: {
name = g.name.endsWith("Lexer") ? g.name.substring(0, g.name.length - 5) : g.name;
return name.toLowerCase() + "_lexer.go";
}
case GrammarType.Combined: {
return g.name.toLowerCase() + "_parser.go";
}
default: {
return "INVALID_FILE_NAME";
}
}
}
/**
* A given grammar T, return the listener name such as TListener.java, if we're using the Java target.
*/
getListenerFileName(header) {
const gen = this.getCodeGenerator();
const g = gen.g;
return g.name.toLowerCase() + "_listener.go";
}
/**
* A given grammar T, return the visitor name such as TVisitor.java, if we're using the Java target.
*/
getVisitorFileName(header) {
const gen = this.getCodeGenerator();
const g = gen.g;
return g.name.toLowerCase() + "_visitor.go";
}
/**
* A given grammar T, return a blank listener implementation such as TBaseListener.java, if we're using
* the Java target.
*/
getBaseListenerFileName(header) {
const gen = this.getCodeGenerator();
const g = gen.g;
return g.name.toLowerCase() + "_base_listener.go";
}
/**
* A given grammar T, return a blank listener implementation such as TBaseListener.java, if we're using
* the Java target.
*/
getBaseVisitorFileName(header) {
const gen = this.getCodeGenerator();
const g = gen.g;
return g.name.toLowerCase() + "_base_visitor.go";
}
get reservedWords() {
return GoTarget.reservedWords;
}
genFile(g, outputFileST, fileName) {
super.genFile(g, outputFileST, fileName);
if (g && GoTarget.doGoFormat && !fileName.startsWith(".") && fileName.endsWith(".go")) {
this.gofmt(path.join(g.tool.getOutputDirectory(g.fileName), fileName));
}
}
gofmt(fileName) {
}
}
export {
GoTarget
};