"use strict";
var zigAnalysis;
const NAV_MODES = {
API: "#A;",
GUIDES: "#G;",
};
(function () {
const domStatus = document.getElementById("status");
const domSectNav = document.getElementById("sectNav");
const domListNav = document.getElementById("listNav");
const domApiSwitch = document.getElementById("ApiSwitch");
const domGuideSwitch = document.getElementById("guideSwitch");
const domGuidesMenu = document.getElementById("guidesMenu");
const domApiMenu = document.getElementById("apiMenu");
const domGuidesList = document.getElementById("guidesList");
const domSectMainMod = document.getElementById("sectMainMod");
const domSectMods = document.getElementById("sectMods");
const domListMods = document.getElementById("listMods");
const domSectTypes = document.getElementById("sectTypes");
const domListTypes = document.getElementById("listTypes");
const domSectTests = document.getElementById("sectTests");
const domListTests = document.getElementById("listTests");
const domSectDocTests = document.getElementById("sectDocTests");
const domDocTestsCode = document.getElementById("docTestsCode");
const domSectNamespaces = document.getElementById("sectNamespaces");
const domListNamespaces = document.getElementById("listNamespaces");
const domSectErrSets = document.getElementById("sectErrSets");
const domListErrSets = document.getElementById("listErrSets");
const domSectFns = document.getElementById("sectFns");
const domListFns = document.getElementById("listFns");
const domSectFields = document.getElementById("sectFields");
const domListFields = document.getElementById("listFields");
const domSectGlobalVars = document.getElementById("sectGlobalVars");
const domListGlobalVars = document.getElementById("listGlobalVars");
const domSectValues = document.getElementById("sectValues");
const domListValues = document.getElementById("listValues");
const domFnProto = document.getElementById("fnProto");
const domFnProtoCode = document.getElementById("fnProtoCode");
const domFnSourceLink = document.getElementById("fnSourceLink");
const domSectParams = document.getElementById("sectParams");
const domListParams = document.getElementById("listParams");
const domTldDocs = document.getElementById("tldDocs");
const domSectFnErrors = document.getElementById("sectFnErrors");
const domListFnErrors = document.getElementById("listFnErrors");
const domTableFnErrors = document.getElementById("tableFnErrors");
const domFnErrorsAnyError = document.getElementById("fnErrorsAnyError");
const domFnExamples = document.getElementById("fnExamples");
// const domListFnExamples = (document.getElementById("listFnExamples"));
const domFnNoExamples = document.getElementById("fnNoExamples");
const domDeclNoRef = document.getElementById("declNoRef");
const domSearch = document.getElementById("search");
const domSectSearchResults = document.getElementById("sectSearchResults");
const domSectSearchAllResultsLink = document.getElementById("sectSearchAllResultsLink");
const domDocs = document.getElementById("docs");
const domGuidesSection = document.getElementById("guides");
const domActiveGuide = document.getElementById("activeGuide");
const domListSearchResults = document.getElementById("listSearchResults");
const domSectSearchNoResults = document.getElementById("sectSearchNoResults");
const domSectInfo = document.getElementById("sectInfo");
// const domTdTarget = (document.getElementById("tdTarget"));
const domTdZigVer = document.getElementById("tdZigVer");
const domHdrName = document.getElementById("hdrName");
const domHelpModal = document.getElementById("helpModal");
const domSearchPlaceholder = document.getElementById("searchPlaceholder");
const sourceFileUrlTemplate = "src/{{mod}}/{{file}}.html#L{{line}}"
const domLangRefLink = document.getElementById("langRefLink");
let searchTimer = null;
let searchTrimResults = true;
let escapeHtmlReplacements = {
"&": "&",
'"': """,
"<": "<",
">": ">",
};
let typeKinds = indexTypeKinds();
let typeTypeId = findTypeTypeId();
let pointerSizeEnum = { One: 0, Many: 1, Slice: 2, C: 3 };
let declSearchIndex = new RadixTree();
window.search = declSearchIndex;
// for each module, is an array with modules to get to this one
let canonModPaths = computeCanonicalModulePaths();
// for each decl, is an array with {declNames, modNames} to get to this one
let canonDeclPaths = null; // lazy; use getCanonDeclPath
// for each type, is an array with {declNames, modNames} to get to this one
let canonTypeDecls = null; // lazy; use getCanonTypeDecl
let curNav = {
mode: NAV_MODES.API,
activeGuide: "",
// each element is a module name, e.g. @import("a") then within there @import("b")
// starting implicitly from root module
modNames: [],
// same as above except actual modules, not names
modObjs: [],
// Each element is a decl name, `a.b.c`, a is 0, b is 1, c is 2, etc.
// empty array means refers to the module itself
declNames: [],
// these will be all types, except the last one may be a type or a decl
declObjs: [],
// (a, b, c, d) comptime call; result is the value the docs refer to
callName: null,
};
let curNavSearch = "";
let curSearchIndex = -1;
let imFeelingLucky = false;
let rootIsStd = detectRootIsStd();
// map of decl index to list of non-generic fn indexes
// let nodesToFnsMap = indexNodesToFns();
// map of decl index to list of comptime fn calls
// let nodesToCallsMap = indexNodesToCalls();
let guidesSearchIndex = {};
window.guideSearch = guidesSearchIndex;
parseGuides();
domSearch.disabled = false;
domSearch.addEventListener("keydown", onSearchKeyDown, false);
domSearch.addEventListener("focus", ev => {
domSearchPlaceholder.classList.add("hidden");
});
domSearch.addEventListener("blur", ev => {
if (domSearch.value.length == 0)
domSearchPlaceholder.classList.remove("hidden");
});
domSectSearchAllResultsLink.addEventListener('click', onClickSearchShowAllResults, false);
function onClickSearchShowAllResults(ev) {
ev.preventDefault();
ev.stopPropagation();
searchTrimResults = false;
onHashChange();
}
if (location.hash == "") {
location.hash = "#A;";
}
// make the modal disappear if you click outside it
domHelpModal.addEventListener("click", ev => {
if (ev.target.className == "help-modal")
domHelpModal.classList.add("hidden");
});
window.addEventListener("hashchange", onHashChange, false);
window.addEventListener("keydown", onWindowKeyDown, false);
onHashChange();
let langRefVersion = zigAnalysis.params.zigVersion;
if (!/^\d+\.\d+\.\d+$/.test(langRefVersion)) {
// the version is probably not released yet
langRefVersion = "master";
}
domLangRefLink.href = `https://ziglang.org/documentation/${langRefVersion}/`;
function renderTitle() {
let suffix = " - Zig";
switch (curNav.mode) {
case NAV_MODES.API:
let list = curNav.modNames.concat(curNav.declNames);
if (list.length === 0) {
document.title = zigAnalysis.modules[zigAnalysis.rootMod].name + suffix;
} else {
document.title = list.join(".") + suffix;
}
return;
case NAV_MODES.GUIDES:
document.title = "[G] " + curNav.activeGuide + suffix;
return;
}
}
function isDecl(x) {
return "value" in x;
}
function isType(x) {
return "kind" in x && !("value" in x);
}
function isContainerType(x) {
return isType(x) && typeKindIsContainer(x.kind);
}
function typeShorthandName(expr) {
let resolvedExpr = resolveValue({ expr: expr });
if (!("type" in resolvedExpr)) {
return null;
}
let type = getType(resolvedExpr.type);
outer: for (let i = 0; i < 10000; i += 1) {
switch (type.kind) {
case typeKinds.Optional:
case typeKinds.Pointer:
let child = type.child;
let resolvedChild = resolveValue(child);
if ("type" in resolvedChild) {
type = getType(resolvedChild.type);
continue;
} else {
return null;
}
default:
break outer;
}
if (i == 9999) throw "Exhausted typeShorthandName quota";
}
let name = undefined;
if (type.kind === typeKinds.Struct) {
name = "struct";
} else if (type.kind === typeKinds.Enum) {
name = "enum";
} else if (type.kind === typeKinds.Union) {
name = "union";
} else {
console.log("TODO: unhalndled case in typeShortName");
return null;
}
return escapeHtml(name);
}
function typeKindIsContainer(typeKind) {
return (
typeKind === typeKinds.Struct ||
typeKind === typeKinds.Union ||
typeKind === typeKinds.Enum ||
typeKind === typeKinds.Opaque
);
}
function declCanRepresentTypeKind(typeKind) {
return typeKind === typeKinds.ErrorSet || typeKindIsContainer(typeKind);
}
//
// function findCteInRefPath(path) {
// for (let i = path.length - 1; i >= 0; i -= 1) {
// const ref = path[i];
// if ("string" in ref) continue;
// if ("comptimeExpr" in ref) return ref;
// if ("refPath" in ref) return findCteInRefPath(ref.refPath);
// return null;
// }
// return null;
// }
function resolveValue(value) {
let i = 0;
while (true) {
i += 1;
if (i >= 10000) {
throw "resolveValue quota exceeded"
}
if ("refPath" in value.expr) {
value = { expr: value.expr.refPath[value.expr.refPath.length - 1] };
continue;
}
if ("declRef" in value.expr) {
value = getDecl(value.expr.declRef).value;
continue;
}
if ("as" in value.expr) {
value = {
typeRef: zigAnalysis.exprs[value.expr.as.typeRefArg],
expr: zigAnalysis.exprs[value.expr.as.exprArg],
};
continue;
}
return value;
}
}
function resolveGenericRet(genericFunc) {
if (genericFunc.generic_ret == null) return null;
let result = resolveValue({expr: genericFunc.generic_ret});
let i = 0;
while (true) {
i += 1;
if (i >= 10000) {
throw "resolveGenericRet quota exceeded"
}
if ("call" in result.expr) {
let call = zigAnalysis.calls[result.expr.call];
let resolvedFunc = resolveValue({ expr: call.func });
if (!("type" in resolvedFunc.expr)) return null;
let callee = getType(resolvedFunc.expr.type);
if (!callee.generic_ret) return null;
result = resolveValue({ expr: callee.generic_ret });
continue;
}
return result;
}
}
// function typeOfDecl(decl){
// return decl.value.typeRef;
//
// let i = 0;
// while(i < 1000) {
// i += 1;
// console.assert(isDecl(decl));
// if ("type" in decl.value) {
// return ({ type: typeTypeId });
// }
//
//// if ("string" in decl.value) {
//// return ({ type: {
//// kind: typeKinds.Pointer,
//// size: pointerSizeEnum.One,
//// child: });
//// }
//
// if ("refPath" in decl.value) {
// decl = ({
// value: decl.value.refPath[decl.value.refPath.length -1]
// });
// continue;
// }
//
// if ("declRef" in decl.value) {
// decl = zigAnalysis.decls[decl.value.declRef];
// continue;
// }
//
// if ("int" in decl.value) {
// return decl.value.int.typeRef;
// }
//
// if ("float" in decl.value) {
// return decl.value.float.typeRef;
// }
//
// if ("array" in decl.value) {
// return decl.value.array.typeRef;
// }
//
// if ("struct" in decl.value) {
// return decl.value.struct.typeRef;
// }
//
// if ("comptimeExpr" in decl.value) {
// const cte = zigAnalysis.comptimeExprs[decl.value.comptimeExpr];
// return cte.typeRef;
// }
//
// if ("call" in decl.value) {
// const fn_call = zigAnalysis.calls[decl.value.call];
// let fn_decl = undefined;
// if ("declRef" in fn_call.func) {
// fn_decl = zigAnalysis.decls[fn_call.func.declRef];
// } else if ("refPath" in fn_call.func) {
// console.assert("declRef" in fn_call.func.refPath[fn_call.func.refPath.length -1]);
// fn_decl = zigAnalysis.decls[fn_call.func.refPath[fn_call.func.refPath.length -1].declRef];
// } else throw {};
//
// const fn_decl_value = resolveValue(fn_decl.value);
// console.assert("type" in fn_decl_value); //TODO handle comptimeExpr
// const fn_type = (zigAnalysis.types[fn_decl_value.type]);
// console.assert(fn_type.kind === typeKinds.Fn);
// return fn_type.ret;
// }
//
// if ("void" in decl.value) {
// return ({ type: typeTypeId });
// }
//
// if ("bool" in decl.value) {
// return ({ type: typeKinds.Bool });
// }
//
// console.log("TODO: handle in `typeOfDecl` more cases: ", decl);
// console.assert(false);
// throw {};
// }
// console.assert(false);
// return ({});
// }
function renderGuides() {
renderTitle();
// set guide mode
domGuideSwitch.classList.add("active");
domApiSwitch.classList.remove("active");
domDocs.classList.add("hidden");
domGuidesSection.classList.remove("hidden");
domActiveGuide.classList.add("hidden");
domApiMenu.classList.add("hidden");
domSectSearchResults.classList.add("hidden");
domSectSearchAllResultsLink.classList.add("hidden");
domSectSearchNoResults.classList.add("hidden");
// sidebar guides list
const section_list = zigAnalysis.guide_sections;
resizeDomList(domGuidesList, section_list.length, '
There are no doc comments for this declaration.
";
}
domTldDocs.classList.remove("hidden");
}
function typeIsErrSet(typeIndex) {
let typeObj = getType(typeIndex);
return typeObj.kind === typeKinds.ErrorSet;
}
function typeIsStructWithNoFields(typeIndex) {
let typeObj = getType(typeIndex);
if (typeObj.kind !== typeKinds.Struct) return false;
return typeObj.field_types.length == 0;
}
function typeIsGenericFn(typeIndex) {
let typeObj = getType(typeIndex);
if (typeObj.kind !== typeKinds.Fn) {
return false;
}
return typeObj.generic_ret != null;
}
function renderFn(fnDecl) {
if ("refPath" in fnDecl.value.expr) {
let last = fnDecl.value.expr.refPath.length - 1;
let lastExpr = fnDecl.value.expr.refPath[last];
console.assert("declRef" in lastExpr);
fnDecl = getDecl(lastExpr.declRef);
}
let value = resolveValue(fnDecl.value);
console.assert("type" in value.expr);
let typeObj = getType(value.expr.type);
domFnProtoCode.innerHTML = exprName(value.expr, {
wantHtml: true,
wantLink: true,
fnDecl,
});
domFnSourceLink.innerHTML = "[" + short + "
" + "
" + long + "
";
}
else {
tdDesc.innerHTML = markdown(short, container);
}
} else {
tdDesc.innerHTML = "
No documentation provided.
";
}
}
domSectFns.classList.remove("hidden");
}
let containerNode = getAstNode(container.src);
if (containerNode.fields && containerNode.fields.length > 0) {
resizeDomList(domListFields, containerNode.fields.length, "
");
for (let i = 0; i < containerNode.fields.length; i += 1) {
let fieldNode = getAstNode(containerNode.fields[i]);
let divDom = domListFields.children[i];
let fieldName = fieldNode.name;
let docs = fieldNode.docs;
let docsNonEmpty = docs != null && docs !== "";
let extraPreClass = docsNonEmpty ? " fieldHasDocs" : "";
let html =
'
";
if (docsNonEmpty) {
html += '
' + markdown(docs) + "
";
}
divDom.innerHTML = html;
}
domSectFields.classList.remove("hidden");
}
if (varsList.length !== 0) {
resizeDomList(
domListGlobalVars,
varsList.length,
'
| | |
'
);
for (let i = 0; i < varsList.length; i += 1) {
let decl = varsList[i];
let trDom = domListGlobalVars.children[i];
let tdName = trDom.children[0];
let tdNameA = tdName.children[0];
let tdType = trDom.children[1];
let tdDesc = trDom.children[2];
tdNameA.setAttribute("href", navLinkDecl(decl.name));
tdNameA.textContent = decl.name;
tdType.innerHTML = exprName(walkResultTypeRef(decl.value), {
wantHtml: true,
wantLink: true,
});
let docs = getAstNode(decl.src).docs;
if (docs != null) {
tdDesc.innerHTML = shortDescMarkdown(docs);
} else {
tdDesc.textContent = "";
}
}
domSectGlobalVars.classList.remove("hidden");
}
if (valsList.length !== 0) {
resizeDomList(
domListValues,
valsList.length,
'
| | |
'
);
for (let i = 0; i < valsList.length; i += 1) {
let decl = valsList[i];
let trDom = domListValues.children[i];
let tdName = trDom.children[0];
let tdNameA = tdName.children[0];
let tdType = trDom.children[1];
let tdDesc = trDom.children[2];
tdNameA.setAttribute("href", navLinkDecl(decl.name));
tdNameA.textContent = decl.name;
tdType.innerHTML = exprName(walkResultTypeRef(decl.value), {
wantHtml: true,
wantLink: true,
});
let docs = getAstNode(decl.src).docs;
if (docs != null) {
tdDesc.innerHTML = shortDescMarkdown(docs);
} else {
tdDesc.textContent = "";
}
}
domSectValues.classList.remove("hidden");
}
if (testsList.length !== 0) {
resizeDomList(
domListTests,
testsList.length,
'
| | |
'
);
for (let i = 0; i < testsList.length; i += 1) {
let decl = testsList[i];
let trDom = domListTests.children[i];
let tdName = trDom.children[0];
let tdNameA = tdName.children[0];
let tdType = trDom.children[1];
let tdDesc = trDom.children[2];
tdNameA.setAttribute("href", navLinkDecl(decl.name));
tdNameA.textContent = decl.name;
tdType.innerHTML = exprName(walkResultTypeRef(decl.value), {
wantHtml: true,
wantLink: true,
});
let docs = getAstNode(decl.src).docs;
if (docs != null) {
tdDesc.innerHTML = shortDescMarkdown(docs);
} else {
tdDesc.textContent = "";
}
}
domSectTests.classList.remove("hidden");
}
}
function operatorCompare(a, b) {
if (a === b) {
return 0;
} else if (a < b) {
return -1;
} else {
return 1;
}
}
function detectRootIsStd() {
let rootMod = zigAnalysis.modules[zigAnalysis.rootMod];
if (rootMod.table["std"] == null) {
// no std mapped into the root module
return false;
}
let stdMod = zigAnalysis.modules[rootMod.table["std"]];
if (stdMod == null) return false;
return rootMod.file === stdMod.file;
}
function indexTypeKinds() {
let map = {};
for (let i = 0; i < zigAnalysis.typeKinds.length; i += 1) {
map[zigAnalysis.typeKinds[i]] = i;
}
// This is just for debugging purposes, not needed to function
let assertList = [
"Type",
"Void",
"Bool",
"NoReturn",
"Int",
"Float",
"Pointer",
"Array",
"Struct",
"ComptimeFloat",
"ComptimeInt",
"Undefined",
"Null",
"Optional",
"ErrorUnion",
"ErrorSet",
"Enum",
"Union",
"Fn",
"Opaque",
"Frame",
"AnyFrame",
"Vector",
"EnumLiteral",
];
for (let i = 0; i < assertList.length; i += 1) {
if (map[assertList[i]] == null)
throw new Error("No type kind '" + assertList[i] + "' found");
}
return map;
}
function findTypeTypeId() {
for (let i = 0; i < zigAnalysis.types.length; i += 1) {
if (getType(i).kind == typeKinds.Type) {
return i;
}
}
throw new Error("No type 'type' found");
}
function updateCurNav() {
curNav = {
mode: NAV_MODES.API,
modNames: [],
modObjs: [],
declNames: [],
declObjs: [],
callName: null,
};
curNavSearch = "";
const mode = location.hash.substring(0, 3);
let query = location.hash.substring(3);
let qpos = query.indexOf("?");
let nonSearchPart;
if (qpos === -1) {
nonSearchPart = query;
} else {
nonSearchPart = query.substring(0, qpos);
curNavSearch = decodeURIComponent(query.substring(qpos + 1));
}
const DEFAULT_HASH = NAV_MODES.API + zigAnalysis.modules[zigAnalysis.rootMod].name;
switch (mode) {
case NAV_MODES.API:
// #A;MODULE:decl.decl.decl?search-term
curNav.mode = mode;
let parts = nonSearchPart.split(":");
if (parts[0] == "") {
location.hash = DEFAULT_HASH;
} else {
curNav.modNames = decodeURIComponent(parts[0]).split(".");
}
if (parts[1] != null) {
curNav.declNames = decodeURIComponent(parts[1]).split(".");
}
return;
case NAV_MODES.GUIDES:
const sections = zigAnalysis.guide_sections;
if (sections.length != 0 && sections[0].guides.length != 0 && nonSearchPart == "") {
location.hash = NAV_MODES.GUIDES + sections[0].guides[0].name;
if (qpos != -1) {
location.hash += query.substring(qpos);
}
return;
}
curNav.mode = mode;
curNav.activeGuide = nonSearchPart;
return;
default:
location.hash = DEFAULT_HASH;
return;
}
}
function onHashChange() {
updateCurNav();
if (domSearch.value !== curNavSearch) {
domSearch.value = curNavSearch;
if (domSearch.value.length == 0)
domSearchPlaceholder.classList.remove("hidden");
else
domSearchPlaceholder.classList.add("hidden");
}
render();
if (imFeelingLucky) {
imFeelingLucky = false;
activateSelectedResult();
}
}
function findSubDecl(parentTypeOrDecl, childName) {
let parentType = parentTypeOrDecl;
{
// Generic functions / resolving decls
if ("value" in parentType) {
const rv = resolveValue(parentType.value);
if ("type" in rv.expr) {
const t = getType(rv.expr.type);
parentType = t;
if (t.kind == typeKinds.Fn && t.generic_ret != null) {
let resolvedGenericRet = resolveValue({ expr: t.generic_ret });
if ("call" in resolvedGenericRet.expr) {
let call = zigAnalysis.calls[resolvedGenericRet.expr.call];
let resolvedFunc = resolveValue({ expr: call.func });
if (!("type" in resolvedFunc.expr)) return null;
let callee = getType(resolvedFunc.expr.type);
if (!callee.generic_ret) return null;
resolvedGenericRet = resolveValue({ expr: callee.generic_ret });
}
if ("type" in resolvedGenericRet.expr) {
parentType = getType(resolvedGenericRet.expr.type);
}
}
}
}
}
if (parentType.pubDecls) {
for (let i = 0; i < parentType.pubDecls.length; i += 1) {
let declIndex = parentType.pubDecls[i];
let childDecl = getDecl(declIndex);
if (childDecl.name === childName) {
childDecl.find_subdecl_idx = declIndex;
return childDecl;
} else if (childDecl.is_uns) {
let declValue = resolveValue(childDecl.value);
if (!("type" in declValue.expr)) continue;
let uns_container = getType(declValue.expr.type);
let uns_res = findSubDecl(uns_container, childName);
if (uns_res !== null) return uns_res;
}
}
}
return null;
}
function computeCanonicalModulePaths() {
let list = new Array(zigAnalysis.modules.length);
// Now we try to find all the modules from root.
let rootMod = zigAnalysis.modules[zigAnalysis.rootMod];
// Breadth-first to keep the path shortest possible.
let stack = [
{
path: [],
mod: rootMod,
},
];
while (stack.length !== 0) {
let item = stack.shift();
for (let key in item.mod.table) {
let childModIndex = item.mod.table[key];
if (list[childModIndex] != null) continue;
let childMod = zigAnalysis.modules[childModIndex];
if (childMod == null) continue;
let newPath = item.path.concat([key]);
list[childModIndex] = newPath;
stack.push({
path: newPath,
mod: childMod,
});
}
}
for (let i = 0; i < zigAnalysis.modules.length; i += 1) {
const p = zigAnalysis.modules[i];
// TODO
// declSearchIndex.add(p.name, {moduleId: i});
}
return list;
}
function computeCanonDeclPaths() {
let list = new Array(zigAnalysis.decls.length);
canonTypeDecls = new Array(zigAnalysis.types.length);
for (let modI = 0; modI < zigAnalysis.modules.length; modI += 1) {
let mod = zigAnalysis.modules[modI];
let modNames = canonModPaths[modI];
if (modNames === undefined) continue;
let stack = [
{
declNames: [],
declIndexes: [],
type: getType(mod.main),
},
];
while (stack.length !== 0) {
let item = stack.shift();
if (isContainerType(item.type)) {
let t = item.type;
let len = t.pubDecls ? t.pubDecls.length : 0;
for (let declI = 0; declI < len; declI += 1) {
let declIndex = t.pubDecls[declI];
if (list[declIndex] != null) continue;
let decl = getDecl(declIndex);
if (decl.is_uns) {
let unsDeclList = [decl];
while(unsDeclList.length != 0) {
let unsDecl = unsDeclList.pop();
let unsDeclVal = resolveValue(unsDecl.value);
if (!("type" in unsDeclVal.expr)) continue;
let unsType = getType(unsDeclVal.expr.type);
if (!isContainerType(unsType)) continue;
let unsPubDeclLen = unsType.pubDecls ? unsType.pubDecls.length : 0;
for (let unsDeclI = 0; unsDeclI < unsPubDeclLen; unsDeclI += 1) {
let childDeclIndex = unsType.pubDecls[unsDeclI];
let childDecl = getDecl(childDeclIndex);
if (childDecl.is_uns) {
unsDeclList.push(childDecl);
} else {
addDeclToSearchResults(childDecl, childDeclIndex, modNames, item, list, stack);
}
}
}
} else {
addDeclToSearchResults(decl, declIndex, modNames, item, list, stack);
}
}
}
}
}
window.cdp = list;
return list;
}
function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
let declVal = resolveValue(decl.value);
let declNames = item.declNames.concat([decl.name]);
let declIndexes = item.declIndexes.concat([declIndex]);
if (list[declIndex] != null) return;
list[declIndex] = {
modNames: modNames,
declNames: declNames,
declIndexes: declIndexes,
};
// add to search index
{
declSearchIndex.add(decl.name, {declIndex});
}
if ("type" in declVal.expr) {
let value = getType(declVal.expr.type);
if (declCanRepresentTypeKind(value.kind)) {
canonTypeDecls[declVal.type] = declIndex;
}
if (isContainerType(value)) {
stack.push({
declNames: declNames,
declIndexes: declIndexes,
type: value,
});
}
// Generic function
if (typeIsGenericFn(declVal.expr.type)) {
let ret = resolveGenericRet(value);
if (ret != null && "type" in ret.expr) {
let generic_type = getType(ret.expr.type);
if (isContainerType(generic_type)) {
stack.push({
declNames: declNames,
declIndexes: declIndexes,
type: generic_type,
});
}
}
}
}
}
function getCanonDeclPath(index) {
if (canonDeclPaths == null) {
canonDeclPaths = computeCanonDeclPaths();
}
//let cd = (canonDeclPaths);
return canonDeclPaths[index];
}
function getCanonTypeDecl(index) {
getCanonDeclPath(0);
//let ct = (canonTypeDecls);
return canonTypeDecls[index];
}
function escapeHtml(text) {
return text.replace(/[&"<>]/g, function (m) {
return escapeHtmlReplacements[m];
});
}
function shortDesc(docs) {
const trimmed_docs = docs.trim();
let index = trimmed_docs.indexOf("\n\n");
let cut = false;
if (index < 0 || index > 80) {
if (trimmed_docs.length > 80) {
index = 80;
cut = true;
} else {
index = trimmed_docs.length;
}
}
let slice = trimmed_docs.slice(0, index);
if (cut) slice += "...";
return slice;
}
function shortDescMarkdown(docs) {
return markdown(shortDesc(docs));
}
function parseGuides() {
for (let j = 0; j < zigAnalysis.guide_sections.length; j+=1){
const section = zigAnalysis.guide_sections[j];
for (let i = 0; i < section.guides.length; i+=1){
let reader = new commonmark.Parser({smart: true});
const guide = section.guides[i];
const ast = reader.parse(guide.body);
// Find the first text thing to use as a sidebar title
guide.title = "[empty guide]";
{
let walker = ast.walker();
let event, node;
while ((event = walker.next())) {
node = event.node;
if (node.type === 'text') {
guide.title = node.literal;
break;
}
}
}
// Index this guide
{
let walker = ast.walker();
let event, node;
while ((event = walker.next())) {
node = event.node;
if (event.entering == true && node.type === 'text') {
indexTextForGuide(j, i, node);
}
}
}
}
}
}
function indexTextForGuide(section_idx, guide_idx, node){
const terms = node.literal.split(" ");
for (let i = 0; i < terms.length; i += 1){
const t = terms[i];
if (!guidesSearchIndex[t]) guidesSearchIndex[t] = new Set();
node.guide = {section_idx, guide_idx};
guidesSearchIndex[t].add(node);
}
}
function markdown(input, contextType) {
const raw_lines = input.split("\n"); // zig allows no '\r', so we don't need to split on CR
const lines = [];
// PHASE 1:
// Dissect lines and determine the type for each line.
// Also computes indentation level and removes unnecessary whitespace
let is_reading_code = false;
let code_indent = 0;
for (let line_no = 0; line_no < raw_lines.length; line_no++) {
const raw_line = raw_lines[line_no];
const line = {
indent: 0,
raw_text: raw_line,
text: raw_line.trim(),
type: "p", // p, h1 … h6, code, ul, ol, blockquote, skip, empty
ordered_number: -1, // NOTE: hack to make the type checker happy
};
if (!is_reading_code) {
while (
line.indent < line.raw_text.length &&
line.raw_text[line.indent] == " "
) {
line.indent += 1;
}
if (line.text.startsWith("######")) {
line.type = "h6";
line.text = line.text.substr(6);
} else if (line.text.startsWith("#####")) {
line.type = "h5";
line.text = line.text.substr(5);
} else if (line.text.startsWith("####")) {
line.type = "h4";
line.text = line.text.substr(4);
} else if (line.text.startsWith("###")) {
line.type = "h3";
line.text = line.text.substr(3);
} else if (line.text.startsWith("##")) {
line.type = "h2";
line.text = line.text.substr(2);
} else if (line.text.startsWith("#")) {
line.type = "h1";
line.text = line.text.substr(1);
} else if (line.text.match(/^-[ \t]+.*$/)) {
// line starts with a hyphen, followed by spaces or tabs
const match = line.text.match(/^-[ \t]+/);
line.type = "ul";
line.text = line.text.substr(match[0].length);
} else if (line.text.match(/^\d+\.[ \t]+.*$/)) {
// line starts with {number}{dot}{spaces or tabs}
const match = line.text.match(/(\d+)\.[ \t]+/);
line.type = "ol";
line.text = line.text.substr(match[0].length);
line.ordered_number = Number(match[1].length);
} else if (line.text == "```") {
line.type = "skip";
is_reading_code = true;
code_indent = line.indent;
} else if (line.text == "") {
line.type = "empty";
}
} else {
if (line.text == "```") {
is_reading_code = false;
line.type = "skip";
} else {
line.type = "code";
line.text = line.raw_text.substr(code_indent); // remove the indent of the ``` from all the code block
}
}
if (line.type != "skip") {
lines.push(line);
}
}
// PHASE 2:
// Render HTML from markdown lines.
// Look at each line and emit fitting HTML code
function markdownInlines(innerText, contextType) {
// inline types:
// **{INLINE}** :
// __{INLINE}__ :
// ~~{INLINE}~~ :
// *{INLINE}* :
// _{INLINE}_ :
// `{TEXT}` :
// [{INLINE}]({URL}) :
//  :
// [[std;format.fmt]] : (inner link)
const formats = [
{
marker: "**",
tag: "strong",
},
{
marker: "~~",
tag: "s",
},
{
marker: "__",
tag: "u",
},
{
marker: "*",
tag: "em",
},
];
const stack = [];
let innerHTML = "";
let currentRun = "";
function flushRun() {
if (currentRun != "") {
innerHTML += escapeHtml(currentRun);
}
currentRun = "";
}
let parsing_code = false;
let codetag = "";
let in_code = false;
// state used to link decl references
let quote_start = undefined;
let quote_start_html = undefined;
for (let i = 0; i < innerText.length; i++) {
if (parsing_code && in_code) {
if (innerText.substr(i, codetag.length) == codetag) {
// remove leading and trailing whitespace if string both starts and ends with one.
if (
currentRun[0] == " " &&
currentRun[currentRun.length - 1] == " "
) {
currentRun = currentRun.substr(1, currentRun.length - 2);
}
flushRun();
i += codetag.length - 1;
in_code = false;
parsing_code = false;
innerHTML += "";
codetag = "";
// find out if this is a decl that should be linked
const maybe_decl_path = innerText.substr(quote_start, i-quote_start);
const decl_hash = detectDeclPath(maybe_decl_path, contextType);
if (decl_hash) {
const anchor_opening_tag = "";
innerHTML = innerHTML.slice(0, quote_start_html)
+ anchor_opening_tag
+ innerHTML.slice(quote_start_html) + "";
}
} else {
currentRun += innerText[i];
}
continue;
}
if (innerText[i] == "`") {
flushRun();
if (!parsing_code) {
quote_start = i + 1;
quote_start_html = innerHTML.length;
innerHTML += "";
}
parsing_code = true;
codetag += "`";
continue;
}
if (parsing_code) {
currentRun += innerText[i];
in_code = true;
} else {
let any = false;
for (
let idx = stack.length > 0 ? -1 : 0;
idx < formats.length;
idx++
) {
const fmt = idx >= 0 ? formats[idx] : stack[stack.length - 1];
if (innerText.substr(i, fmt.marker.length) == fmt.marker) {
flushRun();
if (stack[stack.length - 1] == fmt) {
stack.pop();
innerHTML += "" + fmt.tag + ">";
} else {
stack.push(fmt);
innerHTML += "<" + fmt.tag + ">";
}
i += fmt.marker.length - 1;
any = true;
break;
}
}
if (!any) {
currentRun += innerText[i];
}
}
}
flushRun();
if (in_code) {
in_code = false;
parsing_code = false;
innerHTML += "";
codetag = "";
}
while (stack.length > 0) {
const fmt = stack.pop();
innerHTML += "" + fmt.tag + ">";
}
return innerHTML;
}
function detectDeclPath(text, context) {
let result = "";
let separator = ":";
const components = text.split(".");
let curDeclOrType = undefined;
let curContext = context;
let limit = 10000;
while (curContext) {
limit -= 1;
if (limit == 0) {
throw "too many iterations";
}
curDeclOrType = findSubDecl(curContext, components[0]);
if (!curDeclOrType) {
if (curContext.parent_container == null) break;
curContext = getType(curContext.parent_container);
continue;
}
if (curContext == context) {
separator = '.';
result = location.hash + separator + components[0];
} else {
// We had to go up, which means we need a new path!
const canonPath = getCanonDeclPath(curDeclOrType.find_subdecl_idx);
if (!canonPath) return;
let lastModName = canonPath.modNames[canonPath.modNames.length - 1];
let fullPath = lastModName + ":" + canonPath.declNames.join(".");
separator = '.';
result = "#A;" + fullPath;
}
break;
}
if (!curDeclOrType) {
for (let i = 0; i < zigAnalysis.modules.length; i += 1){
const p = zigAnalysis.modules[i];
if (p.name == components[0]) {
curDeclOrType = getType(p.main);
result += "#A;" + components[0];
break;
}
}
}
if (!curDeclOrType) return null;
for (let i = 1; i < components.length; i += 1) {
curDeclOrType = findSubDecl(curDeclOrType, components[i]);
if (!curDeclOrType) return null;
result += separator + components[i];
separator = '.';
}
return result;
}
function previousLineIs(type, line_no) {
if (line_no > 0) {
return lines[line_no - 1].type == type;
} else {
return false;
}
}
function nextLineIs(type, line_no) {
if (line_no < lines.length - 1) {
return lines[line_no + 1].type == type;
} else {
return false;
}
}
function getPreviousLineIndent(line_no) {
if (line_no > 0) {
return lines[line_no - 1].indent;
} else {
return 0;
}
}
function getNextLineIndent(line_no) {
if (line_no < lines.length - 1) {
return lines[line_no + 1].indent;
} else {
return 0;
}
}
let html = "";
for (let line_no = 0; line_no < lines.length; line_no++) {
const line = lines[line_no];
switch (line.type) {
case "h1":
case "h2":
case "h3":
case "h4":
case "h5":
case "h6":
html +=
"<" +
line.type +
">" +
markdownInlines(line.text, contextType) +
"" +
line.type +
">\n";
break;
case "ul":
case "ol":
if (
!previousLineIs(line.type, line_no) ||
getPreviousLineIndent(line_no) < line.indent
) {
html += "<" + line.type + ">\n";
}
html += "" + markdownInlines(line.text, contextType) + "\n";
if (
!nextLineIs(line.type, line_no) ||
getNextLineIndent(line_no) < line.indent
) {
html += "" + line.type + ">\n";
}
break;
case "p":
if (!previousLineIs("p", line_no)) {
html += "\n";
}
html += markdownInlines(line.text, contextType) + "\n";
if (!nextLineIs("p", line_no)) {
html += "
\n";
}
break;
case "code":
if (!previousLineIs("code", line_no)) {
html += "";
}
html += escapeHtml(line.text) + "\n";
if (!nextLineIs("code", line_no)) {
html += "
\n";
}
break;
}
}
return html;
}
function activateSelectedResult() {
if (domSectSearchResults.classList.contains("hidden")) {
return;
}
let liDom = domListSearchResults.children[curSearchIndex];
if (liDom == null && domListSearchResults.children.length !== 0) {
liDom = domListSearchResults.children[0];
}
if (liDom != null) {
let aDom = liDom.children[0];
location.href = aDom.getAttribute("href");
curSearchIndex = -1;
}
domSearch.blur();
}
// hide the modal if it's visible or return to the previous result page and unfocus the search
function onEscape(ev) {
if (!domHelpModal.classList.contains("hidden")) {
domHelpModal.classList.add("hidden");
ev.preventDefault();
ev.stopPropagation();
} else {
domSearch.value = "";
domSearch.blur();
domSearchPlaceholder.classList.remove("hidden");
curSearchIndex = -1;
ev.preventDefault();
ev.stopPropagation();
startSearch();
}
}
function onSearchKeyDown(ev) {
switch (getKeyString(ev)) {
case "Enter":
// detect if this search changes anything
let terms1 = getSearchTerms();
startSearch();
updateCurNav();
let terms2 = getSearchTerms();
// we might have to wait for onHashChange to trigger
imFeelingLucky = terms1.join(" ") !== terms2.join(" ");
if (!imFeelingLucky) activateSelectedResult();
ev.preventDefault();
ev.stopPropagation();
return;
case "Esc":
onEscape(ev);
return
case "Up":
moveSearchCursor(-1);
ev.preventDefault();
ev.stopPropagation();
return;
case "Down":
// TODO: make the page scroll down if the search cursor is out of the screen
moveSearchCursor(1);
ev.preventDefault();
ev.stopPropagation();
return;
default:
if (ev.shiftKey || ev.ctrlKey || ev.altKey) return;
curSearchIndex = -1;
ev.stopPropagation();
startAsyncSearch();
return;
}
}
function moveSearchCursor(dir) {
if (
curSearchIndex < 0 ||
curSearchIndex >= domListSearchResults.children.length
) {
if (dir > 0) {
curSearchIndex = -1 + dir;
} else if (dir < 0) {
curSearchIndex = domListSearchResults.children.length + dir;
}
} else {
curSearchIndex += dir;
}
if (curSearchIndex < 0) {
curSearchIndex = 0;
}
if (curSearchIndex >= domListSearchResults.children.length) {
curSearchIndex = domListSearchResults.children.length - 1;
}
renderSearchCursor();
}
function getKeyString(ev) {
let name;
let ignoreShift = false;
switch (ev.which) {
case 13:
name = "Enter";
break;
case 27:
name = "Esc";
break;
case 38:
name = "Up";
break;
case 40:
name = "Down";
break;
default:
ignoreShift = true;
name =
ev.key != null
? ev.key
: String.fromCharCode(ev.charCode || ev.keyCode);
}
if (!ignoreShift && ev.shiftKey) name = "Shift+" + name;
if (ev.altKey) name = "Alt+" + name;
if (ev.ctrlKey) name = "Ctrl+" + name;
return name;
}
function onWindowKeyDown(ev) {
switch (getKeyString(ev)) {
case "Esc":
onEscape(ev);
break;
case "s":
if (domHelpModal.classList.contains("hidden")) {
if (ev.target == domSearch) break;
domSearch.focus();
domSearch.select();
domDocs.scrollTo(0, 0);
ev.preventDefault();
ev.stopPropagation();
startAsyncSearch();
}
break;
case "?":
ev.preventDefault();
ev.stopPropagation();
showHelpModal();
break;
}
}
function showHelpModal() {
domHelpModal.classList.remove("hidden");
domHelpModal.style.left =
window.innerWidth / 2 - domHelpModal.clientWidth / 2 + "px";
domHelpModal.style.top =
window.innerHeight / 2 - domHelpModal.clientHeight / 2 + "px";
domHelpModal.focus();
domSearch.blur();
}
function clearAsyncSearch() {
if (searchTimer != null) {
clearTimeout(searchTimer);
searchTimer = null;
}
}
function startAsyncSearch() {
clearAsyncSearch();
searchTimer = setTimeout(startSearch, 100);
}
function startSearch() {
clearAsyncSearch();
let oldHash = location.hash;
let parts = oldHash.split("?");
// TODO: make a tooltip that shows the user that we've replaced their dots
let box_text = domSearch.value.replaceAll(".", " ");
let newPart2 = box_text === "" ? "" : "?" + box_text;
location.replace(parts.length === 1 ? oldHash + newPart2 : parts[0] + newPart2);
}
function getSearchTerms() {
let list = curNavSearch.trim().split(/[ \r\n\t]+/);
return list;
}
function renderSearchGuides() {
const searchTrimmed = false;
let ignoreCase = curNavSearch.toLowerCase() === curNavSearch;
let terms = getSearchTerms();
let matchedItems = new Set();
for (let i = 0; i < terms.length; i += 1) {
const nodes = guidesSearchIndex[terms[i]];
if (nodes) {
for (const n of nodes) {
matchedItems.add(n);
}
}
}
if (matchedItems.size !== 0) {
// Build up the list of search results
let matchedItemsHTML = "";
for (const node of matchedItems) {
const text = node.literal;
const href = "";
matchedItemsHTML += "" + text + "";
}
// Replace the search results using our newly constructed HTML string
domListSearchResults.innerHTML = matchedItemsHTML;
if (searchTrimmed) {
domSectSearchAllResultsLink.classList.remove("hidden");
}
renderSearchCursor();
domSectSearchResults.classList.remove("hidden");
} else {
domSectSearchNoResults.classList.remove("hidden");
}
}
function renderSearchAPI(){
if (canonDeclPaths == null) {
canonDeclPaths = computeCanonDeclPaths();
}
let declSet = new Set();
let otherDeclSet = new Set(); // for low quality results
let declScores = {};
let ignoreCase = curNavSearch.toLowerCase() === curNavSearch;
let term_list = getSearchTerms();
for (let i = 0; i < term_list.length; i += 1) {
let term = term_list[i];
let result = declSearchIndex.search(term.toLowerCase());
if (result == null) {
domSectSearchNoResults.classList.remove("hidden");
domSectSearchResults.classList.add("hidden");
return;
}
let termSet = new Set();
let termOtherSet = new Set();
for (let list of [result.full, result.partial]) {
for (let r of list) {
const d = r.declIndex;
const decl = getDecl(d);
const canonPath = getCanonDeclPath(d);
// collect unconditionally for the first term
if (i == 0) {
declSet.add(d);
} else {
// path intersection for subsequent terms
let found = false;
for (let p of canonPath.declIndexes) {
if (declSet.has(p)) {
found = true;
break;
}
}
if (!found) {
otherDeclSet.add(d);
} else {
termSet.add(d);
}
}
if (declScores[d] == undefined) declScores[d] = 0;
// scores (lower is better)
let decl_name = decl.name;
if (ignoreCase) decl_name = decl_name.toLowerCase();
// shallow path are preferable
const path_depth = canonPath.declNames.length * 50;
// matching the start of a decl name is good
const match_from_start = decl_name.startsWith(term) ? -term.length * (1 -ignoreCase) : (decl_name.length - term.length) + 1;
// being a perfect match is good
const is_full_match = (list == result.full) ? -decl_name.length * (2 - ignoreCase) : decl_name.length - term.length;
// matching the end of a decl name is good
const matches_the_end = decl_name.endsWith(term) ? -term.length * (1 - ignoreCase) : (decl_name.length - term.length) + 1;
// explicitly penalizing scream case decls
const decl_is_scream_case = decl.name.toUpperCase() != decl.name ? 0 : decl.name.length;
const score = path_depth
+ match_from_start
+ is_full_match
+ matches_the_end
+ decl_is_scream_case;
declScores[d] += score;
}
}
if (i != 0) {
for (let d of declSet) {
if (termSet.has(d)) continue;
let found = false;
for (let p of getCanonDeclPath(d).declIndexes) {
if (termSet.has(p) || otherDeclSet.has(p)) {
found = true;
break;
}
}
if (found) {
declScores[d] = declScores[d] / term_list.length;
}
termOtherSet.add(d);
}
declSet = termSet;
for (let d of termOtherSet) {
otherDeclSet.add(d);
}
}
}
let matchedItems = {
high_quality: [],
low_quality: [],
};
for (let idx of declSet) {
matchedItems.high_quality.push({points: declScores[idx], declIndex: idx})
}
for (let idx of otherDeclSet) {
matchedItems.low_quality.push({points: declScores[idx], declIndex: idx})
}
matchedItems.high_quality.sort(function (a, b) {
let cmp = operatorCompare(a.points, b.points);
return cmp;
});
matchedItems.low_quality.sort(function (a, b) {
let cmp = operatorCompare(a.points, b.points);
return cmp;
});
// Build up the list of search results
let matchedItemsHTML = "";
for (let list of [matchedItems.high_quality, matchedItems.low_quality]) {
if (list == matchedItems.low_quality && list.length > 0) {
matchedItemsHTML += "
"
}
for (let result of list) {
const points = result.points;
const match = result.declIndex;
let canonPath = getCanonDeclPath(match);
if (canonPath == null) continue;
let lastModName = canonPath.modNames[canonPath.modNames.length - 1];
let text = lastModName + "." + canonPath.declNames.join(".");
const href = navLink(canonPath.modNames, canonPath.declNames);
matchedItemsHTML += "" + text + "";
}
}
// Replace the search results using our newly constructed HTML string
domListSearchResults.innerHTML = matchedItemsHTML;
renderSearchCursor();
domSectSearchResults.classList.remove("hidden");
}
function renderSearchAPIOld() {
let matchedItems = [];
let ignoreCase = curNavSearch.toLowerCase() === curNavSearch;
let terms = getSearchTerms();
decl_loop: for (
let declIndex = 0;
declIndex < zigAnalysis.decls.length;
declIndex += 1
) {
let canonPath = getCanonDeclPath(declIndex);
if (canonPath == null) continue;
let decl = getDecl(declIndex);
let lastModName = canonPath.modNames[canonPath.modNames.length - 1];
let fullPathSearchText =
lastModName + "." + canonPath.declNames.join(".");
let astNode = getAstNode(decl.src);
let fileAndDocs = ""; //zigAnalysis.files[astNode.file];
// TODO: understand what this piece of code is trying to achieve
// also right now `files` are expressed as a hashmap.
if (astNode.docs != null) {
fileAndDocs += "\n" + astNode.docs;
}
let fullPathSearchTextLower = fullPathSearchText;
if (ignoreCase) {
fullPathSearchTextLower = fullPathSearchTextLower.toLowerCase();
fileAndDocs = fileAndDocs.toLowerCase();
}
let points = 0;
for (let termIndex = 0; termIndex < terms.length; termIndex += 1) {
let term = terms[termIndex];
// exact, case sensitive match of full decl path
if (fullPathSearchText === term) {
points += 4;
continue;
}
// exact, case sensitive match of just decl name
if (decl.name == term) {
points += 3;
continue;
}
// substring, case insensitive match of full decl path
if (fullPathSearchTextLower.indexOf(term) >= 0) {
points += 2;
continue;
}
if (fileAndDocs.indexOf(term) >= 0) {
points += 1;
continue;
}
continue decl_loop;
}
matchedItems.push({
decl: decl,
path: canonPath,
points: points,
});
}
if (matchedItems.length !== 0) {
matchedItems.sort(function (a, b) {
let cmp = operatorCompare(b.points, a.points);
if (cmp != 0) return cmp;
return operatorCompare(a.decl.name, b.decl.name);
});
let searchTrimmed = false;
const searchTrimResultsMaxItems = 60;
if (searchTrimResults && matchedItems.length > searchTrimResultsMaxItems) {
matchedItems = matchedItems.slice(0, searchTrimResultsMaxItems);
searchTrimmed = true;
}
// Build up the list of search results
let matchedItemsHTML = "";
for (let i = 0; i < matchedItems.length; i += 1) {
const match = matchedItems[i];
const lastModName = match.path.modNames[match.path.modNames.length - 1];
const text = lastModName + "." + match.path.declNames.join(".");
const href = navLink(match.path.modNames, match.path.declNames);
matchedItemsHTML += "" + text + "";
}
// Replace the search results using our newly constructed HTML string
domListSearchResults.innerHTML = matchedItemsHTML;
if (searchTrimmed) {
domSectSearchAllResultsLink.classList.remove("hidden");
}
renderSearchCursor();
domSectSearchResults.classList.remove("hidden");
} else {
domSectSearchNoResults.classList.remove("hidden");
}
}
function renderSearchCursor() {
for (let i = 0; i < domListSearchResults.children.length; i += 1) {
let liDom = domListSearchResults.children[i];
if (curSearchIndex === i) {
liDom.classList.add("selected");
} else {
liDom.classList.remove("selected");
}
}
}
// function indexNodesToCalls() {
// let map = {};
// for (let i = 0; i < zigAnalysis.calls.length; i += 1) {
// let call = zigAnalysis.calls[i];
// let fn = zigAnalysis.fns[call.fn];
// if (map[fn.src] == null) {
// map[fn.src] = [i];
// } else {
// map[fn.src].push(i);
// }
// }
// return map;
// }
function byNameProperty(a, b) {
return operatorCompare(a.name, b.name);
}
function getDecl(idx) {
const decl = zigAnalysis.decls[idx];
return {
name: decl[0],
kind: decl[1],
src: decl[2],
value: decl[3],
decltest: decl[4],
is_uns: decl[5],
parent_container: decl[6],
};
}
function getAstNode(idx) {
const ast = zigAnalysis.astNodes[idx];
return {
file: ast[0],
line: ast[1],
col: ast[2],
name: ast[3],
code: ast[4],
docs: ast[5],
fields: ast[6],
comptime: ast[7],
};
}
function getFile(idx) {
const file = zigAnalysis.files[idx];
return {
name: file[0],
modIndex: file[1],
};
}
function getType(idx) {
const ty = zigAnalysis.types[idx];
switch (ty[0]) {
default:
throw "unhandled type kind!";
case 0: // Unanalyzed
throw "unanalyzed type!";
case 1: // Type
case 2: // Void
case 3: // Bool
case 4: // NoReturn
case 5: // Int
case 6: // Float
return { kind: ty[0], name: ty[1] };
case 7: // Pointer
return {
kind: ty[0],
size: ty[1],
child: ty[2],
sentinel: ty[3],
align: ty[4],
address_space: ty[5],
bit_start: ty[6],
host_size: ty[7],
is_ref: ty[8],
is_allowzero: ty[9],
is_mutable: ty[10],
is_volatile: ty[11],
has_sentinel: ty[12],
has_align: ty[13],
has_addrspace: ty[14],
has_bit_range: ty[15],
};
case 8: // Array
return {
kind: ty[0],
len: ty[1],
child: ty[2],
sentinel: ty[3],
};
case 9: // Struct
return {
kind: ty[0],
name: ty[1],
src: ty[2],
privDecls: ty[3],
pubDecls: ty[4],
field_types: ty[5],
field_defaults: ty[6],
is_tuple: ty[7],
line_number: ty[8],
parent_container: ty[9],
};
case 10: // ComptimeExpr
case 11: // ComptimeFloat
case 12: // ComptimeInt
case 13: // Undefined
case 14: // Null
return { kind: ty[0], name: ty[1] };
case 15: // Optional
return {
kind: ty[0],
name: ty[1],
child: ty[2],
};
case 16: // ErrorUnion
return {
kind: ty[0],
lhs: ty[1],
rhs: ty[2],
};
case 17: // InferredErrorUnion
return {
kind: ty[0],
payload: ty[1],
};
case 18: // ErrorSet
return {
kind: ty[0],
name: ty[1],
fields: ty[2],
};
case 19: // Enum
return {
kind: ty[0],
name: ty[1],
src: ty[2],
privDecls: ty[3],
pubDecls: ty[4],
tag: ty[5],
values: ty[6],
nonexhaustive: ty[7],
parent_container: ty[8],
};
case 20: // Union
return {
kind: ty[0],
name: ty[1],
src: ty[2],
privDecls: ty[3],
pubDecls: ty[4],
field_types: ty[5],
tag: ty[6],
auto_tag: ty[7],
parent_container: ty[8],
};
case 21: // Fn
return {
kind: ty[0],
name: ty[1],
src: ty[2],
ret: ty[3],
generic_ret: ty[4],
params: ty[5],
lib_name: ty[6],
is_var_args: ty[7],
is_inferred_error: ty[8],
has_lib_name: ty[9],
has_cc: ty[10],
cc: ty[11],
align: ty[12],
has_align: ty[13],
is_test: ty[14],
is_extern: ty[15],
};
case 22: // BoundFn
return { kind: ty[0], name: ty[1] };
case 23: // Opaque
return {
kind: ty[0],
name: ty[1],
src: ty[2],
privDecls: ty[3],
pubDecls: ty[4],
parent_container: ty[5],
};
case 24: // Frame
case 25: // AnyFrame
case 26: // Vector
case 27: // EnumLiteral
return { kind: ty[0], name: ty[1] };
}
}
})();
function toggleExpand(event) {
const parent = event.target.parentElement;
parent.toggleAttribute("open");
if (!parent.open && parent.getBoundingClientRect().top < 0) {
parent.parentElement.parentElement.scrollIntoView(true);
}
}
function RadixTree() {
this.root = null;
RadixTree.prototype.search = function (query) {
return this.root.search(query);
}
RadixTree.prototype.add = function (declName, value) {
if (this.root == null) {
this.root = new Node(declName.toLowerCase(), null, [value]);
} else {
this.root.add(declName.toLowerCase(), value);
}
const not_scream_case = declName.toUpperCase() != declName;
let found_separator = false;
for (let i = 1; i < declName.length; i +=1) {
if (declName[i] == '_' || declName[i] == '.') {
found_separator = true;
continue;
}
if (found_separator || (declName[i].toLowerCase() !== declName[i])) {
if (declName.length > i+1
&& declName[i+1].toLowerCase() != declName[i+1]) continue;
let suffix = declName.slice(i);
this.root.add(suffix.toLowerCase(), value);
found_separator = false;
}
}
}
function Node(labels, next, values) {
this.labels = labels;
this.next = next;
this.values = values;
}
Node.prototype.isCompressed = function () {
return !Array.isArray(this.next);
}
Node.prototype.search = function (word) {
let full_matches = [];
let partial_matches = [];
let subtree_root = null;
let cn = this;
char_loop: for (let i = 0; i < word.length;) {
if (cn.isCompressed()) {
for (let j = 0; j < cn.labels.length; j += 1) {
let current_idx = i+j;
if (current_idx == word.length) {
partial_matches = cn.values;
subtree_root = cn.next;
break char_loop;
}
if (word[current_idx] != cn.labels[j]) return null;
}
// the full label matched
let new_idx = i + cn.labels.length;
if (new_idx == word.length) {
full_matches = cn.values;
subtree_root = cn.next;
break char_loop;
}
i = new_idx;
cn = cn.next;
continue;
} else {
for (let j = 0; j < cn.labels.length; j += 1) {
if (word[i] == cn.labels[j]) {
if (i == word.length - 1) {
full_matches = cn.values[j];
subtree_root = cn.next[j];
break char_loop;
}
let next = cn.next[j];
if (next == null) return null;
cn = next;
i += 1;
continue char_loop;
}
}
// didn't find a match
return null;
}
}
// Match was found, let's collect all other
// partial matches from the subtree
let stack = [subtree_root];
let node;
while (node = stack.pop()) {
if (node.isCompressed()) {
partial_matches = partial_matches.concat(node.values);
if (node.next != null) {
stack.push(node.next);
}
} else {
for (let v of node.values) {
partial_matches = partial_matches.concat(v);
}
for (let n of node.next) {
if (n != null) stack.push(n);
}
}
}
return {full: full_matches, partial: partial_matches};
}
Node.prototype.add = function (word, value) {
let cn = this;
char_loop: for (let i = 0; i < word.length;) {
if (cn.isCompressed()) {
for(let j = 0; j < cn.labels.length; j += 1) {
let current_idx = i+j;
if (current_idx == word.length) {
if (j < cn.labels.length - 1) {
let node = new Node(cn.labels.slice(j), cn.next, cn.values);
cn.labels = cn.labels.slice(0, j);
cn.next = node;
cn.values = [];
}
cn.values.push(value);
return;
}
if (word[current_idx] == cn.labels[j]) continue;
// if we're here, a mismatch was found
if (j != cn.labels.length - 1) {
// create a suffix node
const label_suffix = cn.labels.slice(j+1);
let node = new Node(label_suffix, cn.next, [...cn.values]);
cn.next = node;
cn.values = [];
}
// turn current node into a split node
let node = null;
let word_values = [];
if (current_idx == word.length - 1) {
// mismatch happened in the last character of word
// meaning that the current node should hold its value
word_values.push(value);
} else {
node = new Node(word.slice(current_idx+1), null, [value]);
}
cn.labels = cn.labels[j] + word[current_idx];
cn.next = [cn.next, node];
cn.values = [cn.values, word_values];
if (j != 0) {
// current node must be turned into a prefix node
let splitNode = new Node(cn.labels, cn.next, cn.values);
cn.labels = word.slice(i, current_idx);
cn.next = splitNode;
cn.values = [];
}
return;
}
// label matched fully with word, are there any more chars?
const new_idx = i + cn.labels.length;
if (new_idx == word.length) {
cn.values.push(value);
return;
} else {
if (cn.next == null) {
let node = new Node(word.slice(new_idx), null, [value]);
cn.next = node;
return;
} else {
cn = cn.next;
i = new_idx;
continue;
}
}
} else { // node is not compressed
let letter = word[i];
for (let j = 0; j < cn.labels.length; j += 1) {
if (letter == cn.labels[j]) {
if (i == word.length - 1) {
cn.values[j].push(value);
return;
}
if (cn.next[j] == null) {
let node = new Node(word.slice(i+1), null, [value]);
cn.next[j] = node;
return;
} else {
cn = cn.next[j];
i += 1;
continue char_loop;
}
}
}
// if we're here we didn't find a match
cn.labels += letter;
if (i == word.length - 1) {
cn.next.push(null);
cn.values.push([value]);
} else {
let node = new Node(word.slice(i+1), null, [value]);
cn.next.push(node);
cn.values.push([]);
}
return;
}
}
}
}
// RADIX TREE:
// apple
// appliance
// "appl" => [
// 'e', => $
// 'i' => "ance" => $
// ]
// OUR STUFF:
// AutoHashMap
// AutoArrayHashMap
// "Auto" => [
// 'A', => "rrayHashMap" => $
// 'H' => "ashMap" => $
// ]
// BUT!
// We want to be able to search "Hash", for example!
function findDeclByName(name) {
for (let i = 0; i < zigAnalysis.decls.length; i += 1) {
let decl = zigAnalysis.decls[i];
if(decl[0] == name) return decl;
}
}