2024-02-24 21:30:30 +00:00
|
|
|
import * as babelparser from "@babel/parser";
|
2023-12-11 18:38:30 +00:00
|
|
|
|
2024-02-24 21:30:30 +00:00
|
|
|
import * as t from "@babel/types";
|
2023-12-11 18:38:30 +00:00
|
|
|
|
2024-02-24 21:30:30 +00:00
|
|
|
export interface InternalDSLVariable {
|
|
|
|
type: string[];
|
|
|
|
dsl_name: string;
|
2023-12-11 18:38:30 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 21:30:30 +00:00
|
|
|
export interface InternalParseResult {
|
|
|
|
internals: Map<string, InternalDSLVariable>;
|
|
|
|
cleanedJS: string;
|
2023-12-11 18:38:30 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 21:30:30 +00:00
|
|
|
export function parseInternal(applicableTo: string): InternalParseResult {
|
|
|
|
let lastChar: null | string = null;
|
|
|
|
let inDslParseMode = false;
|
2023-12-11 18:38:30 +00:00
|
|
|
|
2024-02-24 21:30:30 +00:00
|
|
|
let inDslParseString = "";
|
2023-12-11 18:38:30 +00:00
|
|
|
|
2024-02-24 21:30:30 +00:00
|
|
|
let internalParseResult: InternalParseResult = {
|
|
|
|
internals: new Map(),
|
|
|
|
cleanedJS: "",
|
|
|
|
};
|
2023-12-11 18:38:30 +00:00
|
|
|
|
2024-02-24 21:30:30 +00:00
|
|
|
for (let char of applicableTo) {
|
|
|
|
if (inDslParseMode) {
|
|
|
|
if (char == ">" && lastChar == ">") {
|
|
|
|
//remove first closing >
|
|
|
|
inDslParseString = inDslParseString.slice(0, -1);
|
|
|
|
let { identifier, type, replaceWith } =
|
|
|
|
parseInternalString(inDslParseString);
|
|
|
|
internalParseResult.cleanedJS += replaceWith;
|
|
|
|
internalParseResult.internals.set("___" + identifier, {
|
|
|
|
type: type,
|
|
|
|
dsl_name: identifier,
|
|
|
|
});
|
|
|
|
inDslParseString = "";
|
|
|
|
inDslParseMode = false;
|
|
|
|
continue;
|
2023-12-11 18:38:30 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 21:30:30 +00:00
|
|
|
inDslParseString += char;
|
|
|
|
} else {
|
|
|
|
if (char == "<" && lastChar == "<") {
|
|
|
|
//Remove previous <
|
|
|
|
internalParseResult.cleanedJS =
|
|
|
|
internalParseResult.cleanedJS.slice(0, -1);
|
|
|
|
inDslParseMode = true;
|
|
|
|
continue;
|
2023-12-11 18:38:30 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 21:30:30 +00:00
|
|
|
internalParseResult.cleanedJS += char;
|
2023-12-11 18:38:30 +00:00
|
|
|
}
|
2024-02-24 21:30:30 +00:00
|
|
|
|
|
|
|
lastChar = char;
|
2023-12-11 18:38:30 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 21:30:30 +00:00
|
|
|
return internalParseResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseInternalString(dslString: string) {
|
|
|
|
let splitted = dslString.split(":");
|
|
|
|
|
|
|
|
return {
|
|
|
|
identifier: splitted[0],
|
|
|
|
type: splitted.length > 1 ? splitted[1].split("|") : [""],
|
|
|
|
replaceWith: "___" + splitted[0],
|
|
|
|
};
|
|
|
|
}
|
2023-12-11 18:38:30 +00:00
|
|
|
|
2024-02-24 21:30:30 +00:00
|
|
|
export function parse_with_plugins(
|
|
|
|
code: string
|
|
|
|
): babelparser.ParseResult<t.File> {
|
|
|
|
return babelparser.parse(code, {
|
|
|
|
plugins: [["pipelineOperator", { proposal: "hack", topicToken: "%" }]],
|
|
|
|
});
|
2023-12-11 18:38:30 +00:00
|
|
|
}
|