| ... | ... | @@ -7,7 +7,7 @@ const NAV_MODES = { |
| 7 | 7 | GUIDES: "#G;", |
| 8 | 8 | }; |
| 9 | 9 | |
| 10 | | (function () { |
| 10 | (function() { |
| 11 | 11 | const domBanner = document.getElementById("banner"); |
| 12 | 12 | const domMain = document.getElementById("main"); |
| 13 | 13 | const domStatus = document.getElementById("status"); |
| ... | ... | @@ -59,7 +59,7 @@ const NAV_MODES = { |
| 59 | 59 | const domDocs = document.getElementById("docs"); |
| 60 | 60 | const domGuidesSection = document.getElementById("guides"); |
| 61 | 61 | const domActiveGuide = document.getElementById("activeGuide"); |
| 62 | | |
| 62 | |
| 63 | 63 | const domListSearchResults = document.getElementById("listSearchResults"); |
| 64 | 64 | const domSectSearchNoResults = document.getElementById("sectSearchNoResults"); |
| 65 | 65 | const domSectInfo = document.getElementById("sectInfo"); |
| ... | ... | @@ -133,7 +133,7 @@ const NAV_MODES = { |
| 133 | 133 | // map of decl index to list of comptime fn calls |
| 134 | 134 | // let nodesToCallsMap = indexNodesToCalls(); |
| 135 | 135 | |
| 136 | | let guidesSearchIndex = {}; |
| 136 | let guidesSearchIndex = {}; |
| 137 | 137 | window.guideSearch = guidesSearchIndex; |
| 138 | 138 | parseGuides(); |
| 139 | 139 | |
| ... | ... | @@ -248,7 +248,7 @@ const NAV_MODES = { |
| 248 | 248 | } else if (type.kind === typeKinds.Union) { |
| 249 | 249 | name = "union"; |
| 250 | 250 | } else { |
| 251 | | console.log("TODO: unhalndled case in typeShortName"); |
| 251 | console.log("TODO: unhandled case in typeShortName"); |
| 252 | 252 | return null; |
| 253 | 253 | } |
| 254 | 254 | |
| ... | ... | @@ -313,8 +313,8 @@ const NAV_MODES = { |
| 313 | 313 | |
| 314 | 314 | function resolveGenericRet(genericFunc) { |
| 315 | 315 | if (genericFunc.generic_ret == null) return null; |
| 316 | | let result = resolveValue({expr: genericFunc.generic_ret}); |
| 317 | | |
| 316 | let result = resolveValue({ expr: genericFunc.generic_ret }); |
| 317 | |
| 318 | 318 | let i = 0; |
| 319 | 319 | while (true) { |
| 320 | 320 | i += 1; |
| ... | ... | @@ -437,32 +437,32 @@ const NAV_MODES = { |
| 437 | 437 | const section_list = zigAnalysis.guide_sections; |
| 438 | 438 | resizeDomList(domGuidesList, section_list.length, '<div><h2><span></span></h2><ul class="modules"></ul></div>'); |
| 439 | 439 | for (let j = 0; j < section_list.length; j += 1) { |
| 440 | | const section = section_list[j]; |
| 441 | | const domSectionName = domGuidesList.children[j].children[0].children[0]; |
| 442 | | const domGuides = domGuidesList.children[j].children[1]; |
| 443 | | domSectionName.textContent = section.name; |
| 444 | | resizeDomList(domGuides, section.guides.length, '<li><a href="#"></a></li>'); |
| 445 | | for (let i = 0; i < section.guides.length; i += 1) { |
| 446 | | const guide = section.guides[i]; |
| 447 | | let liDom = domGuides.children[i]; |
| 448 | | let aDom = liDom.children[0]; |
| 449 | | aDom.textContent = guide.title; |
| 450 | | aDom.setAttribute("href", NAV_MODES.GUIDES + guide.name); |
| 451 | | if (guide.name === curNav.activeGuide) { |
| 452 | | aDom.classList.add("active"); |
| 453 | | } else { |
| 454 | | aDom.classList.remove("active"); |
| 455 | | } |
| 440 | const section = section_list[j]; |
| 441 | const domSectionName = domGuidesList.children[j].children[0].children[0]; |
| 442 | const domGuides = domGuidesList.children[j].children[1]; |
| 443 | domSectionName.textContent = section.name; |
| 444 | resizeDomList(domGuides, section.guides.length, '<li><a href="#"></a></li>'); |
| 445 | for (let i = 0; i < section.guides.length; i += 1) { |
| 446 | const guide = section.guides[i]; |
| 447 | let liDom = domGuides.children[i]; |
| 448 | let aDom = liDom.children[0]; |
| 449 | aDom.textContent = guide.title; |
| 450 | aDom.setAttribute("href", NAV_MODES.GUIDES + guide.name); |
| 451 | if (guide.name === curNav.activeGuide) { |
| 452 | aDom.classList.add("active"); |
| 453 | } else { |
| 454 | aDom.classList.remove("active"); |
| 456 | 455 | } |
| 456 | } |
| 457 | 457 | } |
| 458 | | |
| 458 | |
| 459 | 459 | if (section_list.length > 0) { |
| 460 | 460 | domGuidesMenu.classList.remove("hidden"); |
| 461 | 461 | } |
| 462 | 462 | |
| 463 | | |
| 463 | |
| 464 | 464 | if (curNavSearch !== "") { |
| 465 | | return renderSearchGuides(); |
| 465 | return renderSearchGuides(); |
| 466 | 466 | } |
| 467 | 467 | |
| 468 | 468 | // main content |
| ... | ... | @@ -476,8 +476,8 @@ const NAV_MODES = { |
| 476 | 476 | break outer; |
| 477 | 477 | } |
| 478 | 478 | } |
| 479 | | } |
| 480 | | |
| 479 | } |
| 480 | |
| 481 | 481 | if (activeGuide == undefined) { |
| 482 | 482 | const root_file_idx = zigAnalysis.modules[zigAnalysis.rootMod].file; |
| 483 | 483 | const root_file_name = getFile(root_file_idx).name; |
| ... | ... | @@ -564,7 +564,7 @@ Happy writing! |
| 564 | 564 | renderModList(); |
| 565 | 565 | |
| 566 | 566 | if (curNavSearch !== "") { |
| 567 | | return renderSearchAPI(); |
| 567 | return renderSearchAPI(); |
| 568 | 568 | } |
| 569 | 569 | |
| 570 | 570 | let rootMod = zigAnalysis.modules[zigAnalysis.rootMod]; |
| ... | ... | @@ -660,7 +660,8 @@ Happy writing! |
| 660 | 660 | if (!decl.decltest) return; |
| 661 | 661 | const astNode = getAstNode(decl.decltest); |
| 662 | 662 | domSectDocTests.classList.remove("hidden"); |
| 663 | | domDocTestsCode.innerHTML = generate_html_for_src(astNode.code); |
| 663 | domDocTestsCode.innerHTML = renderTokens( |
| 664 | DecoratedTokenizer(astNode.code, decl)); |
| 664 | 665 | } |
| 665 | 666 | |
| 666 | 667 | function renderUnknownDecl(decl) { |
| ... | ... | @@ -707,11 +708,7 @@ Happy writing! |
| 707 | 708 | console.assert("type" in value.expr); |
| 708 | 709 | let typeObj = getType(value.expr.type); |
| 709 | 710 | |
| 710 | | domFnProtoCode.innerHTML = exprName(value.expr, { |
| 711 | | wantHtml: true, |
| 712 | | wantLink: true, |
| 713 | | fnDecl, |
| 714 | | }); |
| 711 | domFnProtoCode.innerHTML = renderTokens(ex(value.expr, { fnDecl: fnDecl })); |
| 715 | 712 | |
| 716 | 713 | domFnSourceLink.innerHTML = "[<a target=\"_blank\" href=\"" + sourceFileLink(fnDecl) + "\">src</a>]"; |
| 717 | 714 | |
| ... | ... | @@ -840,15 +837,21 @@ Happy writing! |
| 840 | 837 | |
| 841 | 838 | let value = typeObj.params[i]; |
| 842 | 839 | let preClass = docsNonEmpty ? ' class="fieldHasDocs"' : ""; |
| 843 | | let html = "<pre" + preClass + ">" + escapeHtml(fieldNode.name) + ": "; |
| 844 | | if (isVarArgs && i === typeObj.params.length - 1) { |
| 845 | | html += "..."; |
| 846 | | } else { |
| 847 | | let name = exprName(value, { wantHtml: false, wantLink: false }); |
| 848 | | html += '<span class="tok-kw">' + name + "</span>"; |
| 849 | | } |
| 840 | let html = "<pre" + preClass + ">" + renderTokens((function*() { |
| 841 | yield Tok.identifier(fieldNode.name); |
| 842 | yield Tok.colon; |
| 843 | yield Tok.space; |
| 844 | if (isVarArgs && i === typeObj.params.length - 1) { |
| 845 | yield Tok.period; |
| 846 | yield Tok.period; |
| 847 | yield Tok.period; |
| 848 | } else { |
| 849 | yield* ex(value, {}); |
| 850 | } |
| 851 | yield Tok.comma; |
| 852 | }())); |
| 850 | 853 | |
| 851 | | html += ",</pre>"; |
| 854 | html += "</pre>"; |
| 852 | 855 | |
| 853 | 856 | if (docsNonEmpty) { |
| 854 | 857 | html += '<div class="fieldDocs">' + markdown(docs) + "</div>"; |
| ... | ... | @@ -932,7 +935,7 @@ Happy writing! |
| 932 | 935 | domSectMainMod.classList.remove("hidden"); |
| 933 | 936 | } |
| 934 | 937 | |
| 935 | | list.sort(function (a, b) { |
| 938 | list.sort(function(a, b) { |
| 936 | 939 | return operatorCompare(a.name.toLowerCase(), b.name.toLowerCase()); |
| 937 | 940 | }); |
| 938 | 941 | |
| ... | ... | @@ -1054,823 +1057,578 @@ Happy writing! |
| 1054 | 1057 | return walkResultTypeRef(resolved); |
| 1055 | 1058 | } |
| 1056 | 1059 | |
| 1057 | | function exprName(expr, opts) { |
| 1058 | | switch (Object.keys(expr)[0]) { |
| 1059 | | default: |
| 1060 | | throw "this expression is not implemented yet"; |
| 1061 | | case "bool": { |
| 1062 | | if (expr.bool) { |
| 1063 | | return "true"; |
| 1060 | function* DecoratedTokenizer(src, context) { |
| 1061 | let tok_it = Tokenizer(src); |
| 1062 | for (let t of tok_it) { |
| 1063 | if (t.tag == Tag.identifier) { |
| 1064 | const link = detectDeclPath(t.src, context); |
| 1065 | if (link) { |
| 1066 | t.link = link; |
| 1064 | 1067 | } |
| 1065 | | return "false"; |
| 1066 | | } |
| 1067 | | case "&": { |
| 1068 | | return "&" + exprName(zigAnalysis.exprs[expr["&"]], opts); |
| 1069 | 1068 | } |
| 1070 | | case "compileError": { |
| 1071 | | let compileError = expr.compileError; |
| 1072 | | return "@compileError(" + exprName(zigAnalysis.exprs[compileError], opts) + ")"; |
| 1073 | | } |
| 1074 | | case "enumLiteral": { |
| 1075 | | let literal = expr.enumLiteral; |
| 1076 | | return "." + literal; |
| 1077 | | } |
| 1078 | | case "void": { |
| 1079 | | return "void"; |
| 1069 | |
| 1070 | yield t; |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | |
| 1075 | function renderSingleToken(t) { |
| 1076 | |
| 1077 | if (t.tag == Tag.whitespace) { |
| 1078 | return t.src; |
| 1079 | } |
| 1080 | |
| 1081 | let src = t.src; |
| 1082 | // if (t.tag == Tag.identifier) { |
| 1083 | // src = escapeHtml(src); |
| 1084 | // } |
| 1085 | let result = ""; |
| 1086 | if (t.tag == Tag.identifier && isSimpleType(t.src)) { |
| 1087 | result = `<span class="zig_type">${src}</span>`; |
| 1088 | } else if (t.tag == Tag.identifier && isSpecialIndentifier(t.src)) { |
| 1089 | result = `<span class="zig_special">${src}</span>`; |
| 1090 | } else if (t.tag == Tag.identifier && t.fnDecl) { |
| 1091 | result = `<span class="zig_fn">${src}</span>`; |
| 1092 | } else { |
| 1093 | result = `<span class="zig_${t.tag}">${src}</span>`; |
| 1094 | } |
| 1095 | |
| 1096 | if (t.link) { |
| 1097 | result = `<a href="${t.link}">` + result + "</a>"; |
| 1098 | } |
| 1099 | |
| 1100 | return result; |
| 1101 | } |
| 1102 | |
| 1103 | function renderTokens(tok_it) { |
| 1104 | var html = []; |
| 1105 | |
| 1106 | const max_iter = 100000; |
| 1107 | let i = 0; |
| 1108 | for (const t of tok_it) { |
| 1109 | i += 1; |
| 1110 | if (i > max_iter) |
| 1111 | throw "too many iterations"; |
| 1112 | |
| 1113 | if (t.tag == Tag.eof) |
| 1114 | break; |
| 1115 | |
| 1116 | html.push(renderSingleToken(t)); |
| 1117 | } |
| 1118 | |
| 1119 | return html.join(""); |
| 1120 | } |
| 1121 | |
| 1122 | function* ex(expr, opts) { |
| 1123 | switch (Object.keys(expr)[0]) { |
| 1124 | default: |
| 1125 | throw "this expression is not implemented yet: " + Object.keys(expr)[0]; |
| 1126 | case "comptimeExpr": { |
| 1127 | const src = zigAnalysis.comptimeExprs[expr.comptimeExpr].code; |
| 1128 | yield* DecoratedTokenizer(src); |
| 1129 | return; |
| 1080 | 1130 | } |
| 1081 | | case "unreachable": { |
| 1082 | | return "unreachable"; |
| 1131 | case "declName": { |
| 1132 | yield { src: expr.declName, tag: Tag.identifier }; |
| 1133 | return; |
| 1083 | 1134 | } |
| 1084 | | case "slice": { |
| 1085 | | let payloadHtml = ""; |
| 1086 | | const lhsExpr = zigAnalysis.exprs[expr.slice.lhs]; |
| 1087 | | const startExpr = zigAnalysis.exprs[expr.slice.start]; |
| 1088 | | let decl = exprName(lhsExpr, opts); |
| 1089 | | let start = exprName(startExpr, opts); |
| 1090 | | let end = ""; |
| 1091 | | let sentinel = ""; |
| 1092 | | if (expr.slice["end"]) { |
| 1093 | | const endExpr = zigAnalysis.exprs[expr.slice.end]; |
| 1094 | | let end_ = exprName(endExpr, opts); |
| 1095 | | end += end_; |
| 1135 | case "declRef": { |
| 1136 | const name = getDecl(expr.declRef).name; |
| 1137 | const canonPath = getCanonDeclPath(expr.declRef); |
| 1138 | if (canonPath) { |
| 1139 | const link = navLink(canonPath.modNames, canonPath.declNames); |
| 1140 | yield { src: name, tag: Tag.identifier, link }; |
| 1141 | } else { |
| 1142 | yield { src: name, tag: Tag.identifier }; |
| 1096 | 1143 | } |
| 1097 | | if (expr.slice["sentinel"]) { |
| 1098 | | const sentinelExpr = zigAnalysis.exprs[expr.slice.sentinel]; |
| 1099 | | let sentinel_ = exprName(sentinelExpr, opts); |
| 1100 | | sentinel += " :" + sentinel_; |
| 1144 | return; |
| 1145 | } |
| 1146 | case "refPath": { |
| 1147 | for (let i = 0; i < expr.refPath.length; i += 1) { |
| 1148 | if (i > 0) yield Tok.period; |
| 1149 | yield* ex(expr.refPath[i]); |
| 1101 | 1150 | } |
| 1102 | | payloadHtml += decl + "[" + start + ".." + end + sentinel + "]"; |
| 1103 | | return payloadHtml; |
| 1151 | return; |
| 1152 | } |
| 1153 | case "fieldRef": { |
| 1154 | const field_idx = expr.fieldRef.index; |
| 1155 | const type = getType(expr.fieldRef.type); |
| 1156 | const field = getAstNode(type.src).fields[field_idx]; |
| 1157 | const name = getAstNode(field).name; |
| 1158 | yield { src: name, tag: Tag.identifier }; |
| 1159 | return; |
| 1104 | 1160 | } |
| 1105 | | case "sliceLength": { |
| 1106 | | let payloadHtml = ""; |
| 1107 | | const lhsExpr = zigAnalysis.exprs[expr.sliceLength.lhs]; |
| 1108 | | const startExpr = zigAnalysis.exprs[expr.sliceLength.start]; |
| 1109 | | const lenExpr = zigAnalysis.exprs[expr.sliceLength.len]; |
| 1110 | | let decl = exprName(lhsExpr, opts); |
| 1111 | | let start = exprName(startExpr, opts); |
| 1112 | | let len = exprName(lenExpr, opts); |
| 1113 | | let sentinel = ""; |
| 1114 | | if (expr.sliceLength["sentinel"]) { |
| 1115 | | const sentinelExpr = zigAnalysis.exprs[expr.sliceLength.sentinel]; |
| 1116 | | let sentinel_ = exprName(sentinelExpr, options); |
| 1117 | | sentinel += " :" + sentinel_; |
| 1161 | case "bool": { |
| 1162 | if (expr.bool) { |
| 1163 | yield { src: "true", tag: Tag.identifier }; |
| 1164 | return; |
| 1118 | 1165 | } |
| 1119 | | payloadHtml += decl + "[" + start + "..][0.." + len + sentinel + "]"; |
| 1120 | | return payloadHtml; |
| 1166 | yield { src: "false", tag: Tag.identifier }; |
| 1167 | return; |
| 1121 | 1168 | } |
| 1122 | | case "sliceIndex": { |
| 1123 | | const sliceIndex = zigAnalysis.exprs[expr.sliceIndex]; |
| 1124 | | return exprName(sliceIndex, opts, opts); |
| 1169 | case "&": { |
| 1170 | yield { src: "&", tag: Tag.ampersand }; |
| 1171 | yield* ex(zigAnalysis.exprs[expr["&"]], opts); |
| 1172 | return; |
| 1125 | 1173 | } |
| 1126 | | case "cmpxchg": { |
| 1127 | | const typeIndex = zigAnalysis.exprs[expr.cmpxchg.type]; |
| 1128 | | const ptrIndex = zigAnalysis.exprs[expr.cmpxchg.ptr]; |
| 1129 | | const expectedValueIndex = |
| 1130 | | zigAnalysis.exprs[expr.cmpxchg.expected_value]; |
| 1131 | | const newValueIndex = zigAnalysis.exprs[expr.cmpxchg.new_value]; |
| 1132 | | const successOrderIndex = zigAnalysis.exprs[expr.cmpxchg.success_order]; |
| 1133 | | const failureOrderIndex = zigAnalysis.exprs[expr.cmpxchg.failure_order]; |
| 1134 | | |
| 1135 | | const type = exprName(typeIndex, opts); |
| 1136 | | const ptr = exprName(ptrIndex, opts); |
| 1137 | | const expectedValue = exprName(expectedValueIndex, opts); |
| 1138 | | const newValue = exprName(newValueIndex, opts); |
| 1139 | | const successOrder = exprName(successOrderIndex, opts); |
| 1140 | | const failureOrder = exprName(failureOrderIndex, opts); |
| 1141 | | |
| 1142 | | let fnName = "@"; |
| 1143 | | |
| 1144 | | switch (expr.cmpxchg.name) { |
| 1145 | | case "cmpxchg_strong": { |
| 1146 | | fnName += "cmpxchgStrong"; |
| 1147 | | break; |
| 1148 | | } |
| 1149 | | case "cmpxchg_weak": { |
| 1150 | | fnName += "cmpxchgWeak"; |
| 1174 | case "call": { |
| 1175 | |
| 1176 | let call = zigAnalysis.calls[expr.call]; |
| 1177 | |
| 1178 | switch (Object.keys(call.func)[0]) { |
| 1179 | default: |
| 1180 | throw "TODO"; |
| 1181 | case "declRef": |
| 1182 | case "refPath": { |
| 1183 | yield* ex(call.func, opts); |
| 1151 | 1184 | break; |
| 1152 | 1185 | } |
| 1153 | | default: { |
| 1154 | | console.log("There's only cmpxchg_strong and cmpxchg_weak"); |
| 1186 | } |
| 1187 | yield Tok.l_paren; |
| 1188 | |
| 1189 | for (let i = 0; i < call.args.length; i++) { |
| 1190 | if (i != 0) { |
| 1191 | yield Tok.comma; |
| 1192 | yield Tok.space; |
| 1155 | 1193 | } |
| 1194 | yield* ex(call.args[i], opts); |
| 1156 | 1195 | } |
| 1157 | 1196 | |
| 1158 | | return ( |
| 1159 | | fnName + |
| 1160 | | "(" + |
| 1161 | | type + |
| 1162 | | ", " + |
| 1163 | | ptr + |
| 1164 | | ", " + |
| 1165 | | expectedValue + |
| 1166 | | ", " + |
| 1167 | | newValue + |
| 1168 | | ", " + |
| 1169 | | "." + |
| 1170 | | successOrder + |
| 1171 | | ", " + |
| 1172 | | "." + |
| 1173 | | failureOrder + |
| 1174 | | ")" |
| 1175 | | ); |
| 1197 | yield Tok.r_paren; |
| 1198 | return; |
| 1176 | 1199 | } |
| 1177 | | case "cmpxchgIndex": { |
| 1178 | | const cmpxchgIndex = zigAnalysis.exprs[expr.cmpxchgIndex]; |
| 1179 | | return exprName(cmpxchgIndex, opts); |
| 1200 | case "sizeOf": { |
| 1201 | const sizeOf = zigAnalysis.exprs[expr.sizeOf]; |
| 1202 | yield { src: "@sizeOf", tag: Tag.builtin }; |
| 1203 | yield { src: "(", tag: Tag.l_paren }; |
| 1204 | yield* ex(sizeOf, opts); |
| 1205 | yield { src: ")", tag: Tag.r_paren }; |
| 1206 | return; |
| 1180 | 1207 | } |
| 1181 | | case "switchOp": { |
| 1182 | | let condExpr = zigAnalysis.exprs[expr.switchOp.cond_index]; |
| 1183 | | let ast = getAstNode(expr.switchOp.src); |
| 1184 | | let file_name = expr.switchOp.file_name; |
| 1185 | | let outer_decl_index = expr.switchOp.outer_decl; |
| 1186 | | let outer_decl = getType(outer_decl_index); |
| 1187 | | let line = 0; |
| 1188 | | // console.log(expr.switchOp) |
| 1189 | | // console.log(outer_decl) |
| 1190 | | while (outer_decl_index !== 0 && outer_decl.line_number > 0) { |
| 1191 | | line += outer_decl.line_number; |
| 1192 | | outer_decl_index = outer_decl.outer_decl; |
| 1193 | | outer_decl = getType(outer_decl_index); |
| 1194 | | // console.log(outer_decl) |
| 1195 | | } |
| 1196 | | line += ast.line + 1; |
| 1197 | | let payloadHtml = ""; |
| 1198 | | let cond = exprName(condExpr, opts); |
| 1199 | | |
| 1200 | | payloadHtml += |
| 1201 | | "</br>" + |
| 1202 | | "node_name: " + |
| 1203 | | ast.name + |
| 1204 | | "</br>" + |
| 1205 | | "file: " + |
| 1206 | | file_name + |
| 1207 | | "</br>" + |
| 1208 | | "line: " + |
| 1209 | | line + |
| 1210 | | "</br>"; |
| 1211 | | payloadHtml += |
| 1212 | | "switch(" + |
| 1213 | | cond + |
| 1214 | | ") {" + |
| 1215 | | '<a href="/src/' + |
| 1216 | | file_name + |
| 1217 | | "#L" + |
| 1218 | | line + |
| 1219 | | '">' + |
| 1220 | | "..." + |
| 1221 | | "</a>}"; |
| 1222 | | return payloadHtml; |
| 1208 | |
| 1209 | case "as": { |
| 1210 | const exprArg = zigAnalysis.exprs[expr.as.exprArg]; |
| 1211 | yield* ex(exprArg, opts); |
| 1212 | return; |
| 1223 | 1213 | } |
| 1224 | | case "switchIndex": { |
| 1225 | | const switchIndex = zigAnalysis.exprs[expr.switchIndex]; |
| 1226 | | return exprName(switchIndex, opts); |
| 1214 | |
| 1215 | case "int": { |
| 1216 | yield { src: expr.int, tag: Tag.number_literal }; |
| 1217 | return; |
| 1227 | 1218 | } |
| 1228 | | case "fieldRef": { |
| 1229 | | const field_idx = expr.fieldRef.index; |
| 1230 | | const type = getType(expr.fieldRef.type); |
| 1231 | | const field = getAstNode(type.src).fields[field_idx]; |
| 1232 | | const name = getAstNode(field).name; |
| 1233 | | return name; |
| 1219 | |
| 1220 | case "int_big": { |
| 1221 | if (expr.int_big.negated) { |
| 1222 | yield { src: "-", tag: Tag.minus }; |
| 1223 | } |
| 1224 | yield { src: expr.int_big.value, tag: Tag.number_literal }; |
| 1225 | return; |
| 1234 | 1226 | } |
| 1235 | | case "intFromEnum": { |
| 1236 | | const intFromEnum = zigAnalysis.exprs[expr.intFromEnum]; |
| 1237 | | return "@intFromEnum(" + exprName(intFromEnum, opts) + ")"; |
| 1227 | |
| 1228 | case "float": { |
| 1229 | yield { src: expr.float, tag: Tag.number_literal }; |
| 1230 | return; |
| 1238 | 1231 | } |
| 1239 | | case "bitSizeOf": { |
| 1240 | | const bitSizeOf = zigAnalysis.exprs[expr.bitSizeOf]; |
| 1241 | | return "@bitSizeOf(" + exprName(bitSizeOf, opts) + ")"; |
| 1232 | |
| 1233 | case "float128": { |
| 1234 | yield { src: expr.float128, tag: Tag.number_literal }; |
| 1235 | return; |
| 1242 | 1236 | } |
| 1243 | | case "sizeOf": { |
| 1244 | | const sizeOf = zigAnalysis.exprs[expr.sizeOf]; |
| 1245 | | return "@sizeOf(" + exprName(sizeOf, opts) + ")"; |
| 1237 | |
| 1238 | case "array": { |
| 1239 | yield { src: ".", tag: Tag.period }; |
| 1240 | yield Tok.l_brace; |
| 1241 | for (let i = 0; i < expr.array.length; i++) { |
| 1242 | if (i != 0) { |
| 1243 | yield { src: ",", tag: Tag.comma }; |
| 1244 | yield Tok.space; |
| 1245 | } |
| 1246 | let elem = zigAnalysis.exprs[expr.array[i]]; |
| 1247 | yield* ex(elem, opts); |
| 1248 | } |
| 1249 | yield Tok.r_brace; |
| 1250 | return; |
| 1246 | 1251 | } |
| 1247 | | case "builtinIndex": { |
| 1248 | | const builtinIndex = zigAnalysis.exprs[expr.builtinIndex]; |
| 1249 | | return exprName(builtinIndex, opts); |
| 1252 | |
| 1253 | case "compileError": { |
| 1254 | yield { src: "@compileError", tag: Tag.builtin }; |
| 1255 | yield Tok.l_paren; |
| 1256 | yield* ex(zigAnalysis.exprs[expr.compileError], opts); |
| 1257 | yield Tok.r_paren; |
| 1258 | return; |
| 1250 | 1259 | } |
| 1251 | | case "builtin": { |
| 1252 | | const param_expr = zigAnalysis.exprs[expr.builtin.param]; |
| 1253 | | let param = exprName(param_expr, opts); |
| 1254 | | |
| 1255 | | let payloadHtml = "@"; |
| 1256 | | switch (expr.builtin.name) { |
| 1257 | | case "align_of": { |
| 1258 | | payloadHtml += "alignOf"; |
| 1259 | | break; |
| 1260 | | } |
| 1261 | | case "int_from_bool": { |
| 1262 | | payloadHtml += "intFromBool"; |
| 1263 | | break; |
| 1264 | | } |
| 1265 | | case "embed_file": { |
| 1266 | | payloadHtml += "embedFile"; |
| 1267 | | break; |
| 1268 | | } |
| 1269 | | case "error_name": { |
| 1270 | | payloadHtml += "errorName"; |
| 1271 | | break; |
| 1272 | | } |
| 1273 | | case "panic": { |
| 1274 | | payloadHtml += "panic"; |
| 1275 | | break; |
| 1276 | | } |
| 1277 | | case "set_runtime_safety": { |
| 1278 | | payloadHtml += "setRuntimeSafety"; |
| 1279 | | break; |
| 1280 | | } |
| 1281 | | case "sqrt": { |
| 1282 | | payloadHtml += "sqrt"; |
| 1283 | | break; |
| 1284 | | } |
| 1285 | | case "sin": { |
| 1286 | | payloadHtml += "sin"; |
| 1287 | | break; |
| 1260 | |
| 1261 | case "string": { |
| 1262 | yield { src: '"' + expr.string + '"', tag: Tag.string_literal }; |
| 1263 | return; |
| 1264 | } |
| 1265 | |
| 1266 | case "struct": { |
| 1267 | yield Tok.period; |
| 1268 | yield Tok.l_brace; |
| 1269 | yield Tok.space; |
| 1270 | |
| 1271 | for (let i = 0; i < expr.struct.length; i++) { |
| 1272 | const fv = expr.struct[i]; |
| 1273 | const field_name = fv.name; |
| 1274 | const field_value = ex(fv.val.expr, opts); |
| 1275 | yield Tok.period; |
| 1276 | yield { src: field_name, tag: Tag.identifier }; |
| 1277 | yield Tok.space; |
| 1278 | yield Tok.eql; |
| 1279 | yield Tok.space; |
| 1280 | yield* field_value; |
| 1281 | if (i !== expr.struct.length - 1) { |
| 1282 | yield Tok.comma; |
| 1283 | yield Tok.space; |
| 1284 | } else { |
| 1285 | yield Tok.space; |
| 1288 | 1286 | } |
| 1289 | | case "cos": { |
| 1290 | | payloadHtml += "cos"; |
| 1287 | } |
| 1288 | yield Tok.r_brace; |
| 1289 | return; |
| 1290 | } |
| 1291 | |
| 1292 | case "binOpIndex": { |
| 1293 | const binOp = zigAnalysis.exprs[expr.binOpIndex]; |
| 1294 | yield* ex(binOp, opts); |
| 1295 | return; |
| 1296 | } |
| 1297 | |
| 1298 | case "binOp": { |
| 1299 | const lhsOp = zigAnalysis.exprs[expr.binOp.lhs]; |
| 1300 | const rhsOp = zigAnalysis.exprs[expr.binOp.rhs]; |
| 1301 | |
| 1302 | if (lhsOp["binOpIndex"] !== undefined) { |
| 1303 | yield Tok.l_paren; |
| 1304 | yield* ex(lhsOp, opts); |
| 1305 | yield Tok.r_paren; |
| 1306 | } else { |
| 1307 | yield* ex(lhsOp, opts); |
| 1308 | } |
| 1309 | |
| 1310 | yield Tok.space; |
| 1311 | |
| 1312 | switch (expr.binOp.name) { |
| 1313 | case "add": { |
| 1314 | yield { src: "+", tag: Tag.plus }; |
| 1291 | 1315 | break; |
| 1292 | 1316 | } |
| 1293 | | case "tan": { |
| 1294 | | payloadHtml += "tan"; |
| 1317 | case "addwrap": { |
| 1318 | yield { src: "+%", tag: Tag.plus_percent }; |
| 1295 | 1319 | break; |
| 1296 | 1320 | } |
| 1297 | | case "exp": { |
| 1298 | | payloadHtml += "exp"; |
| 1321 | case "add_sat": { |
| 1322 | yield { src: "+|", tag: Tag.plus_pipe }; |
| 1299 | 1323 | break; |
| 1300 | 1324 | } |
| 1301 | | case "exp2": { |
| 1302 | | payloadHtml += "exp2"; |
| 1325 | case "sub": { |
| 1326 | yield { src: "-", tag: Tag.minus }; |
| 1303 | 1327 | break; |
| 1304 | 1328 | } |
| 1305 | | case "log": { |
| 1306 | | payloadHtml += "log"; |
| 1329 | case "subwrap": { |
| 1330 | yield { src: "-%", tag: Tag.minus_percent }; |
| 1307 | 1331 | break; |
| 1308 | 1332 | } |
| 1309 | | case "log2": { |
| 1310 | | payloadHtml += "log2"; |
| 1333 | case "sub_sat": { |
| 1334 | yield { src: "-|", tag: Tag.minus_pipe }; |
| 1311 | 1335 | break; |
| 1312 | 1336 | } |
| 1313 | | case "log10": { |
| 1314 | | payloadHtml += "log10"; |
| 1337 | case "mul": { |
| 1338 | yield { src: "*", tag: Tag.asterisk }; |
| 1315 | 1339 | break; |
| 1316 | 1340 | } |
| 1317 | | case "fabs": { |
| 1318 | | payloadHtml += "fabs"; |
| 1341 | case "mulwrap": { |
| 1342 | yield { src: "*%", tag: Tag.asterisk_percent }; |
| 1319 | 1343 | break; |
| 1320 | 1344 | } |
| 1321 | | case "floor": { |
| 1322 | | payloadHtml += "floor"; |
| 1345 | case "mul_sat": { |
| 1346 | yield { src: "*|", tag: Tag.asterisk_pipe }; |
| 1323 | 1347 | break; |
| 1324 | 1348 | } |
| 1325 | | case "ceil": { |
| 1326 | | payloadHtml += "ceil"; |
| 1349 | case "div": { |
| 1350 | yield { src: "/", tag: Tag.slash }; |
| 1327 | 1351 | break; |
| 1328 | 1352 | } |
| 1329 | | case "trunc": { |
| 1330 | | payloadHtml += "trunc"; |
| 1353 | case "shl": { |
| 1354 | yield { src: "<<", tag: Tag.angle_bracket_angle_bracket_left }; |
| 1331 | 1355 | break; |
| 1332 | 1356 | } |
| 1333 | | case "round": { |
| 1334 | | payloadHtml += "round"; |
| 1357 | case "shl_sat": { |
| 1358 | yield { src: "<<|", tag: Tag.angle_bracket_angle_bracket_left_pipe }; |
| 1335 | 1359 | break; |
| 1336 | 1360 | } |
| 1337 | | case "tag_name": { |
| 1338 | | payloadHtml += "tagName"; |
| 1361 | case "shr": { |
| 1362 | yield { src: ">>", tag: Tag.angle_bracket_angle_bracket_right }; |
| 1339 | 1363 | break; |
| 1340 | 1364 | } |
| 1341 | | case "reify": { |
| 1342 | | payloadHtml += "Type"; |
| 1365 | case "bit_or": { |
| 1366 | yield { src: "|", tag: Tag.pipe }; |
| 1343 | 1367 | break; |
| 1344 | 1368 | } |
| 1345 | | case "type_name": { |
| 1346 | | payloadHtml += "typeName"; |
| 1369 | case "bit_and": { |
| 1370 | yield { src: "&", tag: Tag.ampersand }; |
| 1347 | 1371 | break; |
| 1348 | 1372 | } |
| 1349 | | case "frame_type": { |
| 1350 | | payloadHtml += "Frame"; |
| 1373 | case "array_cat": { |
| 1374 | yield { src: "++", tag: Tag.plus_plus }; |
| 1351 | 1375 | break; |
| 1352 | 1376 | } |
| 1353 | | case "frame_size": { |
| 1354 | | payloadHtml += "frameSize"; |
| 1377 | case "array_mul": { |
| 1378 | yield { src: "**", tag: Tag.asterisk_asterisk }; |
| 1355 | 1379 | break; |
| 1356 | 1380 | } |
| 1357 | | case "work_item_id": { |
| 1358 | | payloadHtml += "workItemId"; |
| 1381 | case "cmp_eq": { |
| 1382 | yield { src: "==", tag: Tag.equal_equal }; |
| 1359 | 1383 | break; |
| 1360 | 1384 | } |
| 1361 | | case "work_group_size": { |
| 1362 | | payloadHtml += "workGroupSize"; |
| 1385 | case "cmp_neq": { |
| 1386 | yield { src: "!=", tag: Tag.bang_equal }; |
| 1363 | 1387 | break; |
| 1364 | 1388 | } |
| 1365 | | case "work_group_id": { |
| 1366 | | payloadHtml += "workGroupId"; |
| 1389 | case "cmp_gt": { |
| 1390 | yield { src: ">", tag: Tag.angle_bracket_right }; |
| 1367 | 1391 | break; |
| 1368 | 1392 | } |
| 1369 | | case "int_from_ptr": { |
| 1370 | | payloadHtml += "intFromPtr"; |
| 1393 | case "cmp_gte": { |
| 1394 | yield { src: ">=", tag: Tag.angle_bracket_right_equal }; |
| 1371 | 1395 | break; |
| 1372 | 1396 | } |
| 1373 | | case "int_from_error": { |
| 1374 | | payloadHtml += "intFromError"; |
| 1397 | case "cmp_lt": { |
| 1398 | yield { src: "<", tag: Tag.angle_bracket_left }; |
| 1375 | 1399 | break; |
| 1376 | 1400 | } |
| 1377 | | case "error_to_int": { |
| 1378 | | payloadHtml += "errorFromInt"; |
| 1401 | case "cmp_lte": { |
| 1402 | yield { src: "<=", tag: Tag.angle_bracket_left_equal }; |
| 1379 | 1403 | break; |
| 1380 | 1404 | } |
| 1381 | | case "max": { |
| 1382 | | payloadHtml += "max"; |
| 1405 | case "bool_br_and": { |
| 1406 | yield { src: "and", tag: Tag.keyword_and }; |
| 1383 | 1407 | break; |
| 1384 | 1408 | } |
| 1385 | | case "min": { |
| 1386 | | payloadHtml += "min"; |
| 1409 | case "bool_br_or": { |
| 1410 | yield { src: "or", tag: Tag.keyword_or }; |
| 1387 | 1411 | break; |
| 1388 | 1412 | } |
| 1389 | | case "bit_not": { |
| 1390 | | return "~" + param; |
| 1391 | | } |
| 1392 | | case "bool_not": { |
| 1393 | | return "!" + param; |
| 1394 | | } |
| 1395 | | case "clz": { |
| 1396 | | return "@clz(T" + ", " + param + ")"; |
| 1397 | | } |
| 1398 | | case "ctz": { |
| 1399 | | return "@ctz(T" + ", " + param + ")"; |
| 1400 | | } |
| 1401 | | case "pop_count": { |
| 1402 | | return "@popCount(T" + ", " + param + ")"; |
| 1403 | | } |
| 1404 | | case "byte_swap": { |
| 1405 | | return "@byteSwap(T" + ", " + param + ")"; |
| 1406 | | } |
| 1407 | | case "bit_reverse": { |
| 1408 | | return "@bitReverse(T" + ", " + param + ")"; |
| 1409 | | } |
| 1410 | | case "truncate": { |
| 1411 | | return "@truncate(" + param + ")"; |
| 1412 | | } |
| 1413 | 1413 | default: |
| 1414 | | console.log("builtin function not handled yet or doesn't exist!"); |
| 1414 | console.log("operator not handled yet or doesn't exist!"); |
| 1415 | 1415 | } |
| 1416 | | return payloadHtml + "(" + param + ")"; |
| 1416 | |
| 1417 | yield Tok.space; |
| 1418 | |
| 1419 | if (rhsOp["binOpIndex"] !== undefined) { |
| 1420 | yield Tok.l_paren; |
| 1421 | yield* ex(rhsOp, opts); |
| 1422 | yield Tok.r_paren; |
| 1423 | } else { |
| 1424 | yield* ex(rhsOp, opts); |
| 1425 | } |
| 1426 | return; |
| 1417 | 1427 | } |
| 1428 | |
| 1418 | 1429 | case "builtinBinIndex": { |
| 1419 | 1430 | const builtinBinIndex = zigAnalysis.exprs[expr.builtinBinIndex]; |
| 1420 | | return exprName(builtinBinIndex, opts); |
| 1431 | yield* ex(builtinBinIndex, opts); |
| 1432 | return; |
| 1421 | 1433 | } |
| 1434 | |
| 1422 | 1435 | case "builtinBin": { |
| 1423 | 1436 | const lhsOp = zigAnalysis.exprs[expr.builtinBin.lhs]; |
| 1424 | 1437 | const rhsOp = zigAnalysis.exprs[expr.builtinBin.rhs]; |
| 1425 | | let lhs = exprName(lhsOp, opts); |
| 1426 | | let rhs = exprName(rhsOp, opts); |
| 1427 | 1438 | |
| 1428 | | let payloadHtml = "@"; |
| 1439 | let builtinName = "@"; |
| 1429 | 1440 | switch (expr.builtinBin.name) { |
| 1430 | 1441 | case "int_from_float": { |
| 1431 | | payloadHtml += "intFromFloat"; |
| 1442 | builtinName += "intFromFloat"; |
| 1432 | 1443 | break; |
| 1433 | 1444 | } |
| 1434 | 1445 | case "float_from_int": { |
| 1435 | | payloadHtml += "floatFromInt"; |
| 1446 | builtinName += "floatFromInt"; |
| 1436 | 1447 | break; |
| 1437 | 1448 | } |
| 1438 | 1449 | case "ptr_from_int": { |
| 1439 | | payloadHtml += "ptrFromInt"; |
| 1450 | builtinName += "ptrFromInt"; |
| 1440 | 1451 | break; |
| 1441 | 1452 | } |
| 1442 | 1453 | case "enum_from_int": { |
| 1443 | | payloadHtml += "enumFromInt"; |
| 1454 | builtinName += "enumFromInt"; |
| 1444 | 1455 | break; |
| 1445 | 1456 | } |
| 1446 | 1457 | case "float_cast": { |
| 1447 | | payloadHtml += "floatCast"; |
| 1458 | builtinName += "floatCast"; |
| 1448 | 1459 | break; |
| 1449 | 1460 | } |
| 1450 | 1461 | case "int_cast": { |
| 1451 | | payloadHtml += "intCast"; |
| 1462 | builtinName += "intCast"; |
| 1452 | 1463 | break; |
| 1453 | 1464 | } |
| 1454 | 1465 | case "ptr_cast": { |
| 1455 | | payloadHtml += "ptrCast"; |
| 1466 | builtinName += "ptrCast"; |
| 1456 | 1467 | break; |
| 1457 | 1468 | } |
| 1458 | 1469 | case "const_cast": { |
| 1459 | | payloadHtml += "constCast"; |
| 1470 | builtinName += "constCast"; |
| 1460 | 1471 | break; |
| 1461 | 1472 | } |
| 1462 | 1473 | case "volatile_cast": { |
| 1463 | | payloadHtml += "volatileCast"; |
| 1474 | builtinName += "volatileCast"; |
| 1464 | 1475 | break; |
| 1465 | 1476 | } |
| 1466 | 1477 | case "truncate": { |
| 1467 | | payloadHtml += "truncate"; |
| 1478 | builtinName += "truncate"; |
| 1468 | 1479 | break; |
| 1469 | 1480 | } |
| 1470 | 1481 | case "has_decl": { |
| 1471 | | payloadHtml += "hasDecl"; |
| 1482 | builtinName += "hasDecl"; |
| 1472 | 1483 | break; |
| 1473 | 1484 | } |
| 1474 | 1485 | case "has_field": { |
| 1475 | | payloadHtml += "hasField"; |
| 1486 | builtinName += "hasField"; |
| 1476 | 1487 | break; |
| 1477 | 1488 | } |
| 1478 | 1489 | case "bit_reverse": { |
| 1479 | | payloadHtml += "bitReverse"; |
| 1490 | builtinName += "bitReverse"; |
| 1480 | 1491 | break; |
| 1481 | 1492 | } |
| 1482 | 1493 | case "div_exact": { |
| 1483 | | payloadHtml += "divExact"; |
| 1494 | builtinName += "divExact"; |
| 1484 | 1495 | break; |
| 1485 | 1496 | } |
| 1486 | 1497 | case "div_floor": { |
| 1487 | | payloadHtml += "divFloor"; |
| 1498 | builtinName += "divFloor"; |
| 1488 | 1499 | break; |
| 1489 | 1500 | } |
| 1490 | 1501 | case "div_trunc": { |
| 1491 | | payloadHtml += "divTrunc"; |
| 1502 | builtinName += "divTrunc"; |
| 1492 | 1503 | break; |
| 1493 | 1504 | } |
| 1494 | 1505 | case "mod": { |
| 1495 | | payloadHtml += "mod"; |
| 1506 | builtinName += "mod"; |
| 1496 | 1507 | break; |
| 1497 | 1508 | } |
| 1498 | 1509 | case "rem": { |
| 1499 | | payloadHtml += "rem"; |
| 1510 | builtinName += "rem"; |
| 1500 | 1511 | break; |
| 1501 | 1512 | } |
| 1502 | 1513 | case "mod_rem": { |
| 1503 | | payloadHtml += "rem"; |
| 1514 | builtinName += "rem"; |
| 1504 | 1515 | break; |
| 1505 | 1516 | } |
| 1506 | 1517 | case "shl_exact": { |
| 1507 | | payloadHtml += "shlExact"; |
| 1518 | builtinName += "shlExact"; |
| 1508 | 1519 | break; |
| 1509 | 1520 | } |
| 1510 | 1521 | case "shr_exact": { |
| 1511 | | payloadHtml += "shrExact"; |
| 1522 | builtinName += "shrExact"; |
| 1512 | 1523 | break; |
| 1513 | 1524 | } |
| 1514 | 1525 | case "bitcast": { |
| 1515 | | payloadHtml += "bitCast"; |
| 1526 | builtinName += "bitCast"; |
| 1516 | 1527 | break; |
| 1517 | 1528 | } |
| 1518 | 1529 | case "align_cast": { |
| 1519 | | payloadHtml += "alignCast"; |
| 1530 | builtinName += "alignCast"; |
| 1520 | 1531 | break; |
| 1521 | 1532 | } |
| 1522 | 1533 | case "vector_type": { |
| 1523 | | payloadHtml += "Vector"; |
| 1534 | builtinName += "Vector"; |
| 1524 | 1535 | break; |
| 1525 | 1536 | } |
| 1526 | 1537 | case "reduce": { |
| 1527 | | payloadHtml += "reduce"; |
| 1538 | builtinName += "reduce"; |
| 1528 | 1539 | break; |
| 1529 | 1540 | } |
| 1530 | 1541 | case "splat": { |
| 1531 | | payloadHtml += "splat"; |
| 1542 | builtinName += "splat"; |
| 1532 | 1543 | break; |
| 1533 | 1544 | } |
| 1534 | 1545 | case "offset_of": { |
| 1535 | | payloadHtml += "offsetOf"; |
| 1546 | builtinName += "offsetOf"; |
| 1536 | 1547 | break; |
| 1537 | 1548 | } |
| 1538 | 1549 | case "bit_offset_of": { |
| 1539 | | payloadHtml += "bitOffsetOf"; |
| 1550 | builtinName += "bitOffsetOf"; |
| 1540 | 1551 | break; |
| 1541 | 1552 | } |
| 1542 | 1553 | default: |
| 1543 | 1554 | console.log("builtin function not handled yet or doesn't exist!"); |
| 1544 | 1555 | } |
| 1545 | | return payloadHtml + "(" + lhs + ", " + rhs + ")"; |
| 1546 | | } |
| 1547 | | case "binOpIndex": { |
| 1548 | | const binOpIndex = zigAnalysis.exprs[expr.binOpIndex]; |
| 1549 | | return exprName(binOpIndex, opts); |
| 1550 | | } |
| 1551 | | case "binOp": { |
| 1552 | | const lhsOp = zigAnalysis.exprs[expr.binOp.lhs]; |
| 1553 | | const rhsOp = zigAnalysis.exprs[expr.binOp.rhs]; |
| 1554 | | let lhs = exprName(lhsOp, opts); |
| 1555 | | let rhs = exprName(rhsOp, opts); |
| 1556 | | |
| 1557 | | let print_lhs = ""; |
| 1558 | | let print_rhs = ""; |
| 1559 | 1556 | |
| 1560 | | if (lhsOp["binOpIndex"]) { |
| 1561 | | print_lhs = "(" + lhs + ")"; |
| 1562 | | } else { |
| 1563 | | print_lhs = lhs; |
| 1564 | | } |
| 1565 | | if (rhsOp["binOpIndex"]) { |
| 1566 | | print_rhs = "(" + rhs + ")"; |
| 1567 | | } else { |
| 1568 | | print_rhs = rhs; |
| 1569 | | } |
| 1570 | | |
| 1571 | | let operator = ""; |
| 1572 | | |
| 1573 | | switch (expr.binOp.name) { |
| 1574 | | case "add": { |
| 1575 | | operator += "+"; |
| 1576 | | break; |
| 1577 | | } |
| 1578 | | case "addwrap": { |
| 1579 | | operator += "+%"; |
| 1580 | | break; |
| 1581 | | } |
| 1582 | | case "add_sat": { |
| 1583 | | operator += "+|"; |
| 1584 | | break; |
| 1585 | | } |
| 1586 | | case "sub": { |
| 1587 | | operator += "-"; |
| 1588 | | break; |
| 1589 | | } |
| 1590 | | case "subwrap": { |
| 1591 | | operator += "-%"; |
| 1592 | | break; |
| 1593 | | } |
| 1594 | | case "sub_sat": { |
| 1595 | | operator += "-|"; |
| 1596 | | break; |
| 1597 | | } |
| 1598 | | case "mul": { |
| 1599 | | operator += "*"; |
| 1600 | | break; |
| 1601 | | } |
| 1602 | | case "mulwrap": { |
| 1603 | | operator += "*%"; |
| 1604 | | break; |
| 1605 | | } |
| 1606 | | case "mul_sat": { |
| 1607 | | operator += "*|"; |
| 1608 | | break; |
| 1609 | | } |
| 1610 | | case "div": { |
| 1611 | | operator += "/"; |
| 1612 | | break; |
| 1613 | | } |
| 1614 | | case "shl": { |
| 1615 | | operator += "<<"; |
| 1616 | | break; |
| 1617 | | } |
| 1618 | | case "shl_sat": { |
| 1619 | | operator += "<<|"; |
| 1620 | | break; |
| 1621 | | } |
| 1622 | | case "shr": { |
| 1623 | | operator += ">>"; |
| 1624 | | break; |
| 1625 | | } |
| 1626 | | case "bit_or": { |
| 1627 | | operator += "|"; |
| 1628 | | break; |
| 1629 | | } |
| 1630 | | case "bit_and": { |
| 1631 | | operator += "&"; |
| 1632 | | break; |
| 1633 | | } |
| 1634 | | case "array_cat": { |
| 1635 | | operator += "++"; |
| 1636 | | break; |
| 1637 | | } |
| 1638 | | case "array_mul": { |
| 1639 | | operator += "**"; |
| 1640 | | break; |
| 1641 | | } |
| 1642 | | case "cmp_eq": { |
| 1643 | | operator += "=="; |
| 1644 | | break; |
| 1645 | | } |
| 1646 | | case "cmp_neq": { |
| 1647 | | operator += "!="; |
| 1648 | | break; |
| 1649 | | } |
| 1650 | | case "cmp_gt": { |
| 1651 | | operator += ">"; |
| 1652 | | break; |
| 1653 | | } |
| 1654 | | case "cmp_gte": { |
| 1655 | | operator += ">="; |
| 1656 | | break; |
| 1657 | | } |
| 1658 | | case "cmp_lt": { |
| 1659 | | operator += "<"; |
| 1660 | | break; |
| 1661 | | } |
| 1662 | | case "cmp_lte": { |
| 1663 | | operator += "<="; |
| 1664 | | break; |
| 1665 | | } |
| 1666 | | case "bool_br_and": { |
| 1667 | | operator += "and"; |
| 1668 | | break; |
| 1669 | | } |
| 1670 | | case "bool_br_or": { |
| 1671 | | operator += "or"; |
| 1672 | | break; |
| 1673 | | } |
| 1674 | | default: |
| 1675 | | console.log("operator not handled yet or doesn't exist!"); |
| 1676 | | } |
| 1677 | | |
| 1678 | | return print_lhs + " " + operator + " " + print_rhs; |
| 1679 | | } |
| 1680 | | case "errorSets": { |
| 1681 | | const errUnionObj = getType(expr.errorSets); |
| 1682 | | let lhs = exprName(errUnionObj.lhs, opts); |
| 1683 | | let rhs = exprName(errUnionObj.rhs, opts); |
| 1684 | | return lhs + " || " + rhs; |
| 1685 | | } |
| 1686 | | case "errorUnion": { |
| 1687 | | const errUnionObj = getType(expr.errorUnion); |
| 1688 | | let lhs = exprName(errUnionObj.lhs, opts); |
| 1689 | | let rhs = exprName(errUnionObj.rhs, opts); |
| 1690 | | return lhs + "!" + rhs; |
| 1691 | | } |
| 1692 | | case "struct": { |
| 1693 | | // const struct_name = |
| 1694 | | // zigAnalysis.decls[expr.struct[0].val.typeRef.refPath[0].declRef].name; |
| 1695 | | const struct_name = "."; |
| 1696 | | let struct_body = ""; |
| 1697 | | struct_body += struct_name + "{ "; |
| 1698 | | for (let i = 0; i < expr.struct.length; i++) { |
| 1699 | | const fv = expr.struct[i]; |
| 1700 | | const field_name = fv.name; |
| 1701 | | const field_value = exprName(fv.val.expr, opts); |
| 1702 | | // TODO: commented out because it seems not needed. if it deals |
| 1703 | | // with a corner case, please add a comment when re-enabling it. |
| 1704 | | // let field_value = exprArg[Object.keys(exprArg)[0]]; |
| 1705 | | // if (field_value instanceof Object) { |
| 1706 | | // value_field = exprName(value_field) |
| 1707 | | // zigAnalysis.decls[value_field[0].val.typeRef.refPath[0].declRef] |
| 1708 | | // .name; |
| 1709 | | // } |
| 1710 | | struct_body += "." + field_name + " = " + field_value; |
| 1711 | | if (i !== expr.struct.length - 1) { |
| 1712 | | struct_body += ", "; |
| 1713 | | } else { |
| 1714 | | struct_body += " "; |
| 1715 | | } |
| 1716 | | } |
| 1717 | | struct_body += "}"; |
| 1718 | | return struct_body; |
| 1719 | | } |
| 1720 | | case "typeOf_peer": { |
| 1721 | | let payloadHtml = "@TypeOf("; |
| 1722 | | for (let i = 0; i < expr.typeOf_peer.length; i++) { |
| 1723 | | let elem = zigAnalysis.exprs[expr.typeOf_peer[i]]; |
| 1724 | | payloadHtml += exprName(elem, { wantHtml: true, wantLink: true }); |
| 1725 | | if (i !== expr.typeOf_peer.length - 1) { |
| 1726 | | payloadHtml += ", "; |
| 1727 | | } |
| 1728 | | } |
| 1729 | | payloadHtml += ")"; |
| 1730 | | return payloadHtml; |
| 1731 | | } |
| 1732 | | case "alignOf": { |
| 1733 | | const alignRefArg = zigAnalysis.exprs[expr.alignOf]; |
| 1734 | | let payloadHtml = |
| 1735 | | "@alignOf(" + |
| 1736 | | exprName(alignRefArg, { wantHtml: true, wantLink: true }) + |
| 1737 | | ")"; |
| 1738 | | return payloadHtml; |
| 1557 | yield { src: builtinName, tag: Tag.builtin }; |
| 1558 | yield Tok.l_paren; |
| 1559 | yield* ex(lhsOp, opts); |
| 1560 | yield Tok.comma; |
| 1561 | yield Tok.space; |
| 1562 | yield* ex(rhsOp, opts); |
| 1563 | yield Tok.r_paren; |
| 1564 | return; |
| 1739 | 1565 | } |
| 1740 | | case "typeOf": { |
| 1741 | | const typeRefArg = zigAnalysis.exprs[expr.typeOf]; |
| 1742 | | let payloadHtml = |
| 1743 | | "@TypeOf(" + |
| 1744 | | exprName(typeRefArg, { wantHtml: true, wantLink: true }) + |
| 1745 | | ")"; |
| 1746 | | return payloadHtml; |
| 1566 | |
| 1567 | case "enumLiteral": { |
| 1568 | let literal = expr.enumLiteral; |
| 1569 | yield Tok.period; |
| 1570 | yield { src: literal, tag: Tag.identifier }; |
| 1571 | return; |
| 1747 | 1572 | } |
| 1748 | | case "typeInfo": { |
| 1749 | | const typeRefArg = zigAnalysis.exprs[expr.typeInfo]; |
| 1750 | | let payloadHtml = |
| 1751 | | "@typeInfo(" + |
| 1752 | | exprName(typeRefArg, { wantHtml: true, wantLink: true }) + |
| 1753 | | ")"; |
| 1754 | | return payloadHtml; |
| 1573 | |
| 1574 | case "void": { |
| 1575 | yield { src: "void", tag: Tag.identifier }; |
| 1576 | return; |
| 1755 | 1577 | } |
| 1578 | |
| 1756 | 1579 | case "null": { |
| 1757 | | if (opts.wantHtml) { |
| 1758 | | return '<span class="tok-null">null</span>'; |
| 1759 | | } else { |
| 1760 | | return "null"; |
| 1761 | | } |
| 1762 | | } |
| 1763 | | case "array": { |
| 1764 | | let payloadHtml = ".{"; |
| 1765 | | for (let i = 0; i < expr.array.length; i++) { |
| 1766 | | if (i != 0) payloadHtml += ", "; |
| 1767 | | let elem = zigAnalysis.exprs[expr.array[i]]; |
| 1768 | | payloadHtml += exprName(elem, opts); |
| 1769 | | } |
| 1770 | | return payloadHtml + "}"; |
| 1771 | | } |
| 1772 | | case "comptimeExpr": { |
| 1773 | | return generate_html_for_src(zigAnalysis.comptimeExprs[expr.comptimeExpr].code); |
| 1580 | yield { src: "null", tag: Tag.identifier }; |
| 1581 | return; |
| 1774 | 1582 | } |
| 1775 | | case "call": { |
| 1776 | | let call = zigAnalysis.calls[expr.call]; |
| 1777 | | let payloadHtml = ""; |
| 1778 | | |
| 1779 | | switch (Object.keys(call.func)[0]) { |
| 1780 | | default: |
| 1781 | | throw "TODO"; |
| 1782 | | case "declRef": |
| 1783 | | case "refPath": { |
| 1784 | | payloadHtml += exprName(call.func, opts); |
| 1785 | | break; |
| 1786 | | } |
| 1787 | | } |
| 1788 | | payloadHtml += "("; |
| 1789 | 1583 | |
| 1790 | | for (let i = 0; i < call.args.length; i++) { |
| 1791 | | if (i != 0) payloadHtml += ", "; |
| 1792 | | payloadHtml += exprName(call.args[i], opts); |
| 1793 | | } |
| 1794 | | |
| 1795 | | payloadHtml += ")"; |
| 1796 | | return payloadHtml; |
| 1797 | | } |
| 1798 | | case "as": { |
| 1799 | | // @Check : this should be done in backend because there are legit @as() calls |
| 1800 | | // const typeRefArg = zigAnalysis.exprs[expr.as.typeRefArg]; |
| 1801 | | const exprArg = zigAnalysis.exprs[expr.as.exprArg]; |
| 1802 | | // return "@as(" + exprName(typeRefArg, opts) + |
| 1803 | | // ", " + exprName(exprArg, opts) + ")"; |
| 1804 | | return exprName(exprArg, opts); |
| 1584 | case "undefined": { |
| 1585 | yield { src: "undefined", tag: Tag.identifier }; |
| 1586 | return; |
| 1805 | 1587 | } |
| 1806 | | case "declRef": { |
| 1807 | | const name = getDecl(expr.declRef).name; |
| 1808 | 1588 | |
| 1809 | | if (opts.wantHtml) { |
| 1810 | | let payloadHtml = ""; |
| 1811 | | if (opts.wantLink) { |
| 1812 | | payloadHtml += '<a href="' + findDeclNavLink(name) + '">'; |
| 1813 | | } |
| 1814 | | payloadHtml += |
| 1815 | | '<span class="tok-kw" style="color:lightblue;">' + |
| 1816 | | name + |
| 1817 | | "</span>"; |
| 1818 | | if (opts.wantLink) payloadHtml += "</a>"; |
| 1819 | | return payloadHtml; |
| 1820 | | } else { |
| 1821 | | return name; |
| 1822 | | } |
| 1823 | | } |
| 1824 | | case "refPath": { |
| 1825 | | let firstComponent = expr.refPath[0]; |
| 1826 | | let name = exprName(firstComponent, opts); |
| 1827 | | let url = undefined; |
| 1828 | | if (opts.wantLink && "declRef" in firstComponent) { |
| 1829 | | url = findDeclNavLink(getDecl(firstComponent.declRef).name); |
| 1830 | | } |
| 1831 | | for (let i = 1; i < expr.refPath.length; i++) { |
| 1832 | | let component = undefined; |
| 1833 | | if ("string" in expr.refPath[i]) { |
| 1834 | | component = expr.refPath[i].string; |
| 1835 | | } else { |
| 1836 | | component = exprName(expr.refPath[i], { ...opts, wantLink: false }); |
| 1837 | | if (opts.wantLink && "declRef" in expr.refPath[i]) { |
| 1838 | | url += "." + getDecl(expr.refPath[i].declRef).name; |
| 1839 | | component = '<a href="' + url + '">' + |
| 1840 | | component + |
| 1841 | | "</a>"; |
| 1842 | | } |
| 1843 | | } |
| 1844 | | name += "." + component; |
| 1845 | | } |
| 1846 | | return name; |
| 1847 | | } |
| 1848 | | case "int": { |
| 1849 | | return "" + expr.int; |
| 1850 | | } |
| 1851 | | case "float": { |
| 1852 | | return "" + expr.float.toFixed(2); |
| 1853 | | } |
| 1854 | | case "float128": { |
| 1855 | | return "" + expr.float128.toFixed(2); |
| 1589 | case "anytype": { |
| 1590 | yield { src: "anytype", tag: Tag.keyword_anytype }; |
| 1591 | return; |
| 1856 | 1592 | } |
| 1857 | | case "undefined": { |
| 1858 | | return "undefined"; |
| 1593 | |
| 1594 | case "this": { |
| 1595 | yield { src: "@This", tag: Tag.builtin }; |
| 1596 | yield Tok.l_paren; |
| 1597 | yield Tok.r_paren; |
| 1598 | return; |
| 1859 | 1599 | } |
| 1860 | | case "string": { |
| 1861 | | return '"' + escapeHtml(expr.string) + '"'; |
| 1600 | |
| 1601 | case "typeInfo": { |
| 1602 | const arg = zigAnalysis.exprs[expr.typeInfo]; |
| 1603 | yield { src: "@typeInfo", tag: Tag.builtin }; |
| 1604 | yield Tok.l_paren; |
| 1605 | yield* ex(arg, opts); |
| 1606 | yield Tok.r_paren; |
| 1607 | return; |
| 1862 | 1608 | } |
| 1863 | 1609 | |
| 1864 | | case "int_big": { |
| 1865 | | return (expr.int_big.negated ? "-" : "") + expr.int_big.value; |
| 1610 | case "switchIndex": { |
| 1611 | const switchIndex = zigAnalysis.exprs[expr.switchIndex]; |
| 1612 | yield* ex(switchIndex, opts); |
| 1613 | return; |
| 1866 | 1614 | } |
| 1867 | 1615 | |
| 1868 | | case "anytype": { |
| 1869 | | return "anytype"; |
| 1616 | case "errorSets": { |
| 1617 | const errSetsObj = getType(expr.errorSets); |
| 1618 | yield* ex(errSetsObj.lhs, opts); |
| 1619 | yield Tok.space; |
| 1620 | yield { src: "||", tag: Tag.pipe_pipe }; |
| 1621 | yield Tok.space; |
| 1622 | yield* ex(errSetsObj.rhs, opts); |
| 1623 | return; |
| 1870 | 1624 | } |
| 1871 | 1625 | |
| 1872 | | case "this": { |
| 1873 | | return "@This()"; |
| 1626 | case "errorUnion": { |
| 1627 | const errUnionObj = getType(expr.errorUnion); |
| 1628 | yield* ex(errUnionObj.lhs, opts); |
| 1629 | yield { src: "!", tag: Tag.bang }; |
| 1630 | yield* ex(errUnionObj.rhs, opts); |
| 1631 | return; |
| 1874 | 1632 | } |
| 1875 | 1633 | |
| 1876 | 1634 | case "type": { |
| ... | ... | @@ -1880,416 +1638,389 @@ Happy writing! |
| 1880 | 1638 | if (typeof typeObj === "number") typeObj = getType(typeObj); |
| 1881 | 1639 | switch (typeObj.kind) { |
| 1882 | 1640 | default: |
| 1883 | | throw "TODO"; |
| 1641 | throw "TODO: " + typeObj.kind; |
| 1642 | case typeKinds.Type: { |
| 1643 | yield { src: typeObj.name, tag: Tag.identifier }; |
| 1644 | return; |
| 1645 | } |
| 1646 | case typeKinds.Void: { |
| 1647 | yield { src: "void", tag: Tag.identifier }; |
| 1648 | return; |
| 1649 | } |
| 1650 | case typeKinds.NoReturn: { |
| 1651 | yield { src: "noreturn", tag: Tag.identifier }; |
| 1652 | return; |
| 1653 | } |
| 1654 | case typeKinds.ComptimeExpr: { |
| 1655 | yield { src: "anyopaque", tag: Tag.identifier }; |
| 1656 | return; |
| 1657 | } |
| 1658 | case typeKinds.Bool: { |
| 1659 | yield { src: "bool", tag: Tag.identifier }; |
| 1660 | return; |
| 1661 | } |
| 1662 | case typeKinds.ComptimeInt: { |
| 1663 | yield { src: "comptime_int", tag: Tag.identifier }; |
| 1664 | return; |
| 1665 | } |
| 1666 | case typeKinds.ComptimeFloat: { |
| 1667 | yield { src: "comptime_float", tag: Tag.identifier }; |
| 1668 | return; |
| 1669 | } |
| 1670 | case typeKinds.Int: { |
| 1671 | yield { src: typeObj.name, tag: Tag.identifier }; |
| 1672 | return; |
| 1673 | } |
| 1674 | case typeKinds.Float: { |
| 1675 | yield { src: typeObj.name, tag: Tag.identifier }; |
| 1676 | return; |
| 1677 | } |
| 1678 | case typeKinds.Array: { |
| 1679 | yield Tok.l_bracket; |
| 1680 | yield* ex(typeObj.len, opts); |
| 1681 | if (typeObj.sentinel) { |
| 1682 | yield Tok.colon; |
| 1683 | yield* ex(typeObj.sentinel, opts); |
| 1684 | } |
| 1685 | yield Tok.r_bracket; |
| 1686 | yield* ex(typeObj.child, opts); |
| 1687 | return; |
| 1688 | } |
| 1689 | case typeKinds.Optional: { |
| 1690 | yield { src: "?", tag: Tag.question_mark }; |
| 1691 | yield* ex(typeObj.child, opts); |
| 1692 | return; |
| 1693 | } |
| 1694 | case typeKinds.Pointer: { |
| 1695 | let ptrObj = typeObj; |
| 1696 | switch (ptrObj.size) { |
| 1697 | default: |
| 1698 | console.log("TODO: implement unhandled pointer size case"); |
| 1699 | case pointerSizeEnum.One: |
| 1700 | yield { src: "*", tag: Tag.asterisk }; |
| 1701 | break; |
| 1702 | case pointerSizeEnum.Many: |
| 1703 | yield Tok.l_bracket; |
| 1704 | yield { src: "*", tag: Tag.asterisk }; |
| 1705 | if (ptrObj.sentinel !== null) { |
| 1706 | yield Tok.colon; |
| 1707 | yield* ex(ptrObj.sentinel, opts); |
| 1708 | } |
| 1709 | yield Tok.r_bracket; |
| 1710 | break; |
| 1711 | case pointerSizeEnum.Slice: |
| 1712 | if (ptrObj.is_ref) { |
| 1713 | yield { src: "*", tag: Tag.asterisk }; |
| 1714 | } |
| 1715 | yield Tok.l_bracket; |
| 1716 | if (ptrObj.sentinel !== null) { |
| 1717 | yield Tok.colon; |
| 1718 | yield* ex(ptrObj.sentinel, opts); |
| 1719 | } |
| 1720 | yield Tok.r_bracket; |
| 1721 | break; |
| 1722 | case pointerSizeEnum.C: |
| 1723 | yield Tok.l_bracket; |
| 1724 | yield { src: "*", tag: Tag.asterisk }; |
| 1725 | yield { src: "c", tag: Tag.identifier }; |
| 1726 | if (typeObj.sentinel !== null) { |
| 1727 | yield Tok.colon; |
| 1728 | yield* ex(ptrObj.sentinel, opts); |
| 1729 | } |
| 1730 | yield Tok.r_bracket; |
| 1731 | break; |
| 1732 | } |
| 1733 | if (!ptrObj.is_mutable) { |
| 1734 | yield Tok.const; |
| 1735 | yield Tok.space; |
| 1736 | } |
| 1737 | if (ptrObj.is_allowzero) { |
| 1738 | yield { src: "allowzero", tag: Tag.keyword_allowzero }; |
| 1739 | yield Tok.space; |
| 1740 | } |
| 1741 | if (ptrObj.is_volatile) { |
| 1742 | yield { src: "volatile", tag: Tag.keyword_volatile }; |
| 1743 | } |
| 1744 | if (ptrObj.has_addrspace) { |
| 1745 | yield { src: "addrspace", tag: Tag.keyword_addrspace }; |
| 1746 | yield Tok.l_paren; |
| 1747 | yield Tok.period; |
| 1748 | yield Tok.r_paren; |
| 1749 | } |
| 1750 | if (ptrObj.has_align) { |
| 1751 | yield { src: "align", tag: Tag.keyword_align }; |
| 1752 | yield Tok.l_paren; |
| 1753 | yield* ex(ptrObj.align, opts); |
| 1754 | if (ptrObj.hostIntBytes !== undefined && ptrObj.hostIntBytes !== null) { |
| 1755 | yield Tok.colon; |
| 1756 | yield* ex(ptrObj.bitOffsetInHost, opts); |
| 1757 | yield Tok.colon; |
| 1758 | yield* ex(ptrObj.hostIntBytes, opts); |
| 1759 | } |
| 1760 | yield Tok.r_paren; |
| 1761 | yield Tok.space; |
| 1762 | } |
| 1763 | yield* ex(ptrObj.child, opts); |
| 1764 | return; |
| 1765 | } |
| 1884 | 1766 | case typeKinds.Struct: { |
| 1885 | 1767 | let structObj = typeObj; |
| 1886 | | let name = ""; |
| 1887 | | let layout = ""; |
| 1888 | 1768 | if (structObj.layout !== null) { |
| 1889 | 1769 | switch (structObj.layout.enumLiteral) { |
| 1890 | 1770 | case "Packed": { |
| 1891 | | layout = "packed "; |
| 1771 | yield { src: "packed", tag: Tag.keyword_packed }; |
| 1892 | 1772 | break; |
| 1893 | 1773 | } |
| 1894 | 1774 | case "Extern": { |
| 1895 | | layout = "extern "; |
| 1775 | yield { src: "extern", tag: Tag.keyword_extern }; |
| 1896 | 1776 | break; |
| 1897 | 1777 | } |
| 1898 | 1778 | } |
| 1779 | yield Tok.space; |
| 1899 | 1780 | } |
| 1900 | | if (opts.wantHtml) { |
| 1901 | | name = "<span class='tok-kw'>" + layout + "struct</span>"; |
| 1902 | | } else { |
| 1903 | | name = layout + "struct"; |
| 1904 | | } |
| 1781 | yield { src: "struct", tag: Tag.keyword_struct }; |
| 1905 | 1782 | if (structObj.backing_int !== null) { |
| 1906 | | name += "(" + exprName(structObj.backing_int, opts) + ")"; |
| 1783 | yield Tok.l_paren; |
| 1784 | yield* ex(structObj.backing_int, opts); |
| 1785 | yield Tok.r_paren; |
| 1907 | 1786 | } |
| 1908 | | name += " { "; |
| 1909 | | if (structObj.field_types.length > 1 && opts.wantHtml) { name += "</br>"; } |
| 1910 | | let indent = ""; |
| 1911 | | if (structObj.field_types.length > 1 && opts.wantHtml) { |
| 1912 | | indent = "&nbsp;&nbsp;&nbsp;&nbsp;" |
| 1787 | yield Tok.space; |
| 1788 | yield Tok.l_brace; |
| 1789 | |
| 1790 | if (structObj.field_types.length > 1) { |
| 1791 | yield Tok.enter; |
| 1792 | } else { |
| 1793 | yield Tok.space; |
| 1913 | 1794 | } |
| 1914 | | if (opts.indent && structObj.field_types.length > 1) { |
| 1915 | | indent = opts.indent + indent; |
| 1795 | |
| 1796 | let indent = 0; |
| 1797 | if (structObj.field_types.length > 1) { |
| 1798 | indent = 1; |
| 1916 | 1799 | } |
| 1917 | | let structNode = getAstNode(structObj.src); |
| 1918 | | let field_end = ","; |
| 1919 | | if (structObj.field_types.length > 1 && opts.wantHtml) { |
| 1920 | | field_end += "</br>"; |
| 1921 | | } else { |
| 1922 | | field_end += " "; |
| 1800 | if (opts.indent && structObj.field_types.length > 1) { |
| 1801 | indent += opts.ident; |
| 1923 | 1802 | } |
| 1924 | 1803 | |
| 1804 | let structNode = getAstNode(structObj.src); |
| 1925 | 1805 | for (let i = 0; i < structObj.field_types.length; i += 1) { |
| 1926 | 1806 | let fieldNode = getAstNode(structNode.fields[i]); |
| 1927 | 1807 | let fieldName = fieldNode.name; |
| 1928 | | let html = indent; |
| 1929 | | if (!structObj.is_tuple) { |
| 1930 | | html += escapeHtml(fieldName); |
| 1808 | |
| 1809 | for (let j = 0; j < indent; j += 1) { |
| 1810 | yield Tok.tab; |
| 1931 | 1811 | } |
| 1932 | 1812 | |
| 1933 | | let fieldTypeExpr = structObj.field_types[i]; |
| 1934 | | if (!structObj.is_tuple) { |
| 1935 | | html += ": "; |
| 1813 | if (!typeObj.is_tuple) { |
| 1814 | yield { src: fieldName, tag: Tag.identifier }; |
| 1936 | 1815 | } |
| 1937 | 1816 | |
| 1938 | | html += exprName(fieldTypeExpr, { ...opts, indent: indent }); |
| 1817 | let fieldTypeExpr = structObj.field_types[i]; |
| 1818 | if (!typeObj.is_tuple) { |
| 1819 | yield Tok.colon; |
| 1820 | yield Tok.space; |
| 1821 | } |
| 1822 | yield* ex(fieldTypeExpr, { ...opts, indent: indent }); |
| 1939 | 1823 | |
| 1940 | 1824 | if (structObj.field_defaults[i] !== null) { |
| 1941 | | html += " = " + exprName(structObj.field_defaults[i], opts); |
| 1825 | yield Tok.space; |
| 1826 | yield Tok.eql; |
| 1827 | yield Tok.space; |
| 1828 | yield* ex(structObj.field_defaults[i], opts); |
| 1942 | 1829 | } |
| 1943 | 1830 | |
| 1944 | | html += field_end; |
| 1945 | | |
| 1946 | | name += html; |
| 1947 | | } |
| 1948 | | if (opts.indent && structObj.field_types.length > 1) { |
| 1949 | | name += opts.indent; |
| 1831 | if (structObj.field_types.length > 1) { |
| 1832 | yield Tok.comma; |
| 1833 | yield Tok.enter; |
| 1834 | } else { |
| 1835 | yield Tok.space; |
| 1836 | } |
| 1950 | 1837 | } |
| 1951 | | name += "}"; |
| 1952 | | return name; |
| 1838 | yield Tok.r_brace; |
| 1839 | return; |
| 1953 | 1840 | } |
| 1954 | 1841 | case typeKinds.Enum: { |
| 1955 | 1842 | let enumObj = typeObj; |
| 1956 | | let name = ""; |
| 1957 | | if (opts.wantHtml) { |
| 1958 | | name = "<span class='tok-kw'>enum</span>"; |
| 1959 | | } else { |
| 1960 | | name = "enum"; |
| 1961 | | } |
| 1843 | yield { src: "enum", tag: Tag.keyword_enum }; |
| 1962 | 1844 | if (enumObj.tag) { |
| 1963 | | name += "(" + exprName(enumObj.tag, opts) + ")"; |
| 1845 | yield Tok.l_paren; |
| 1846 | yield* ex(enumObj.tag, opts); |
| 1847 | yield Tok.r_paren; |
| 1964 | 1848 | } |
| 1965 | | name += " { "; |
| 1849 | yield Tok.space; |
| 1850 | yield Tok.l_brace; |
| 1851 | |
| 1966 | 1852 | let enumNode = getAstNode(enumObj.src); |
| 1967 | 1853 | let fields_len = enumNode.fields.length; |
| 1968 | 1854 | if (enumObj.nonexhaustive) { |
| 1969 | 1855 | fields_len += 1; |
| 1970 | 1856 | } |
| 1971 | | if (fields_len > 1 && opts.wantHtml) { name += "</br>"; } |
| 1972 | | let indent = ""; |
| 1857 | |
| 1858 | if (fields_len > 1) { |
| 1859 | yield Tok.enter; |
| 1860 | } else { |
| 1861 | yield Tok.space; |
| 1862 | } |
| 1863 | |
| 1864 | let indent = 0; |
| 1973 | 1865 | if (fields_len > 1) { |
| 1974 | | if (opts.wantHtml) { |
| 1975 | | indent = "&nbsp;&nbsp;&nbsp;&nbsp;"; |
| 1976 | | } else { |
| 1977 | | indent = " "; |
| 1978 | | } |
| 1866 | indent = 1; |
| 1979 | 1867 | } |
| 1980 | 1868 | if (opts.indent) { |
| 1981 | | indent = opts.indent + indent; |
| 1982 | | } |
| 1983 | | let field_end = ","; |
| 1984 | | if (fields_len > 1 && opts.wantHtml) { |
| 1985 | | field_end += "</br>"; |
| 1986 | | } else { |
| 1987 | | field_end += " "; |
| 1869 | indent += opts.indent; |
| 1988 | 1870 | } |
| 1871 | |
| 1989 | 1872 | for (let i = 0; i < enumNode.fields.length; i += 1) { |
| 1990 | 1873 | let fieldNode = getAstNode(enumNode.fields[i]); |
| 1991 | 1874 | let fieldName = fieldNode.name; |
| 1992 | | let html = indent + escapeHtml(fieldName); |
| 1875 | |
| 1876 | for (let j = 0; j < indent; j += 1) yield Tok.tab; |
| 1877 | yield { src: fieldName, tag: Tag.identifier }; |
| 1993 | 1878 | |
| 1994 | 1879 | if (enumObj.values[i] !== null) { |
| 1995 | | html += " = " + exprName(enumObj.values[i], opts); |
| 1880 | yield Tok.space; |
| 1881 | yield Tok.eql; |
| 1882 | yield Tok.space; |
| 1883 | yield* ex(enumObj.values[i], opts); |
| 1996 | 1884 | } |
| 1997 | 1885 | |
| 1998 | | html += field_end; |
| 1999 | | |
| 2000 | | name += html; |
| 1886 | if (fields_len > 1) { |
| 1887 | yield Tok.comma; |
| 1888 | yield Tok.enter; |
| 1889 | } |
| 2001 | 1890 | } |
| 2002 | | if (enumObj.nonexhaustive) { |
| 2003 | | name += indent + "_" + field_end; |
| 1891 | for (let j = 0; j < indent; j += 1) yield Tok.tab; |
| 1892 | yield { src: "_", tag: Tag.identifier }; |
| 1893 | if (fields_len > 1) { |
| 1894 | yield Tok.comma; |
| 1895 | yield Tok.enter; |
| 2004 | 1896 | } |
| 2005 | 1897 | if (opts.indent) { |
| 2006 | | name += opts.indent; |
| 1898 | for (let j = 0; j < opts.indent; j += 1) yield Tok.tab; |
| 2007 | 1899 | } |
| 2008 | | name += "}"; |
| 2009 | | return name; |
| 1900 | yield Tok.r_brace; |
| 1901 | return; |
| 2010 | 1902 | } |
| 2011 | 1903 | case typeKinds.Union: { |
| 2012 | 1904 | let unionObj = typeObj; |
| 2013 | | let name = ""; |
| 2014 | | let layout = ""; |
| 2015 | 1905 | if (unionObj.layout !== null) { |
| 2016 | 1906 | switch (unionObj.layout.enumLiteral) { |
| 2017 | 1907 | case "Packed": { |
| 2018 | | layout = "packed "; |
| 1908 | yield { src: "packed", tag: Tag.keyword_packed }; |
| 2019 | 1909 | break; |
| 2020 | 1910 | } |
| 2021 | 1911 | case "Extern": { |
| 2022 | | layout = "extern "; |
| 1912 | yield { src: "extern", tag: Tag.keyword_extern }; |
| 2023 | 1913 | break; |
| 2024 | 1914 | } |
| 2025 | 1915 | } |
| 1916 | yield Tok.space; |
| 2026 | 1917 | } |
| 2027 | | if (opts.wantHtml) { |
| 2028 | | name = "<span class='tok-kw'>" + layout + "union</span>"; |
| 2029 | | } else { |
| 2030 | | name = layout + "union"; |
| 2031 | | } |
| 1918 | yield { src: "union", tag: Tag.keyword_union }; |
| 2032 | 1919 | if (unionObj.auto_tag) { |
| 2033 | | if (opts.wantHtml) { |
| 2034 | | name += "(<span class='tok-kw'>enum</span>"; |
| 2035 | | } else { |
| 2036 | | name += "(enum"; |
| 2037 | | } |
| 1920 | yield Tok.l_paren; |
| 1921 | yield { src: "enum", tag: Tag.keyword_enum }; |
| 2038 | 1922 | if (unionObj.tag) { |
| 2039 | | name += "(" + exprName(unionObj.tag, opts) + "))"; |
| 1923 | yield Tok.l_paren; |
| 1924 | yield* ex(unionObj.tag, opts); |
| 1925 | yield Tok.r_paren; |
| 1926 | yield Tok.r_paren; |
| 2040 | 1927 | } else { |
| 2041 | | name += ")"; |
| 1928 | yield Tok.r_paren; |
| 2042 | 1929 | } |
| 2043 | 1930 | } else if (unionObj.tag) { |
| 2044 | | name += "(" + exprName(unionObj.tag, opts) + ")"; |
| 1931 | yield Tok.l_paren; |
| 1932 | yield* ex(unionObj.tag, opts); |
| 1933 | yield Tok.r_paren; |
| 2045 | 1934 | } |
| 2046 | | name += " { "; |
| 2047 | | if (unionObj.field_types.length > 1 && opts.wantHtml) { |
| 2048 | | name += "</br>"; |
| 1935 | yield Tok.space; |
| 1936 | yield Tok.l_brace; |
| 1937 | if (unionObj.field_types.length > 1) { |
| 1938 | yield Tok.enter; |
| 1939 | } else { |
| 1940 | yield Tok.space; |
| 2049 | 1941 | } |
| 2050 | | let indent = ""; |
| 2051 | | if (unionObj.field_types.length > 1 && opts.wantHtml) { |
| 2052 | | indent = "&nbsp;&nbsp;&nbsp;&nbsp;" |
| 1942 | let indent = 0; |
| 1943 | if (unionObj.field_types.length > 1) { |
| 1944 | indent = 1; |
| 2053 | 1945 | } |
| 2054 | 1946 | if (opts.indent) { |
| 2055 | | indent = opts.indent + indent; |
| 1947 | indent += opts.indent; |
| 2056 | 1948 | } |
| 2057 | 1949 | let unionNode = getAstNode(unionObj.src); |
| 2058 | | let field_end = ","; |
| 2059 | | if (unionObj.field_types.length > 1 && opts.wantHtml) { |
| 2060 | | field_end += "</br>"; |
| 2061 | | } else { |
| 2062 | | field_end += " "; |
| 2063 | | } |
| 2064 | 1950 | for (let i = 0; i < unionObj.field_types.length; i += 1) { |
| 2065 | 1951 | let fieldNode = getAstNode(unionNode.fields[i]); |
| 2066 | 1952 | let fieldName = fieldNode.name; |
| 2067 | | let html = indent + escapeHtml(fieldName); |
| 1953 | for (let j = 0; j < indent; j += 1) yield Tok.tab; |
| 1954 | yield { src: fieldName, tag: Tag.identifier }; |
| 2068 | 1955 | |
| 2069 | 1956 | let fieldTypeExpr = unionObj.field_types[i]; |
| 2070 | | html += ": "; |
| 1957 | yield Tok.colon; |
| 1958 | yield Tok.space; |
| 2071 | 1959 | |
| 2072 | | html += exprName(fieldTypeExpr, { ...opts, indent: indent }); |
| 1960 | yield* ex(fieldTypeExpr, { ...opts, indent: indent }); |
| 2073 | 1961 | |
| 2074 | | html += field_end; |
| 2075 | | |
| 2076 | | name += html; |
| 1962 | if (unionObj.field_types.length > 1) { |
| 1963 | yield Tok.comma; |
| 1964 | yield Tok.enter; |
| 1965 | } else { |
| 1966 | yield Tok.space; |
| 1967 | } |
| 2077 | 1968 | } |
| 2078 | 1969 | if (opts.indent) { |
| 2079 | | name += opts.indent; |
| 1970 | for (let j = 0; j < opts.indent; j += 1) yield Tok.tab; |
| 2080 | 1971 | } |
| 2081 | | name += "}"; |
| 2082 | | return name; |
| 1972 | yield Tok.r_brace; |
| 1973 | return; |
| 2083 | 1974 | } |
| 2084 | 1975 | case typeKinds.Opaque: { |
| 2085 | | let opaqueObj = typeObj; |
| 2086 | | return opaqueObj; |
| 2087 | | } |
| 2088 | | case typeKinds.ComptimeExpr: { |
| 2089 | | return "anyopaque"; |
| 2090 | | } |
| 2091 | | case typeKinds.Array: { |
| 2092 | | let arrayObj = typeObj; |
| 2093 | | let name = "["; |
| 2094 | | let lenName = exprName(arrayObj.len, opts); |
| 2095 | | let sentinel = arrayObj.sentinel |
| 2096 | | ? ":" + exprName(arrayObj.sentinel, opts) |
| 2097 | | : ""; |
| 2098 | | // let is_mutable = arrayObj.is_multable ? "const " : ""; |
| 2099 | | |
| 2100 | | if (opts.wantHtml) { |
| 2101 | | name += |
| 2102 | | '<span class="tok-number">' + lenName + sentinel + "</span>"; |
| 2103 | | } else { |
| 2104 | | name += lenName + sentinel; |
| 2105 | | } |
| 2106 | | name += "]"; |
| 2107 | | // name += is_mutable; |
| 2108 | | name += exprName(arrayObj.child, opts); |
| 2109 | | return name; |
| 2110 | | } |
| 2111 | | case typeKinds.Optional: |
| 2112 | | return "?" + exprName(typeObj.child, opts); |
| 2113 | | case typeKinds.Pointer: { |
| 2114 | | let ptrObj = typeObj; |
| 2115 | | let sentinel = ptrObj.sentinel |
| 2116 | | ? ":" + exprName(ptrObj.sentinel, opts) |
| 2117 | | : ""; |
| 2118 | | let is_mutable = !ptrObj.is_mutable ? "const " : ""; |
| 2119 | | let name = ""; |
| 2120 | | switch (ptrObj.size) { |
| 2121 | | default: |
| 2122 | | console.log("TODO: implement unhandled pointer size case"); |
| 2123 | | case pointerSizeEnum.One: |
| 2124 | | name += "*"; |
| 2125 | | name += is_mutable; |
| 2126 | | break; |
| 2127 | | case pointerSizeEnum.Many: |
| 2128 | | name += "[*"; |
| 2129 | | name += sentinel; |
| 2130 | | name += "]"; |
| 2131 | | name += is_mutable; |
| 2132 | | break; |
| 2133 | | case pointerSizeEnum.Slice: |
| 2134 | | if (ptrObj.is_ref) { |
| 2135 | | name += "*"; |
| 2136 | | } |
| 2137 | | name += "["; |
| 2138 | | name += sentinel; |
| 2139 | | name += "]"; |
| 2140 | | name += is_mutable; |
| 2141 | | break; |
| 2142 | | case pointerSizeEnum.C: |
| 2143 | | name += "[*c"; |
| 2144 | | name += sentinel; |
| 2145 | | name += "]"; |
| 2146 | | name += is_mutable; |
| 2147 | | break; |
| 2148 | | } |
| 2149 | | // @check: after the major changes in arrays the consts are came from switch above |
| 2150 | | // if (!ptrObj.is_mutable) { |
| 2151 | | // if (opts.wantHtml) { |
| 2152 | | // name += '<span class="tok-kw">const</span> '; |
| 2153 | | // } else { |
| 2154 | | // name += "const "; |
| 2155 | | // } |
| 2156 | | // } |
| 2157 | | if (ptrObj.is_allowzero) { |
| 2158 | | name += "allowzero "; |
| 2159 | | } |
| 2160 | | if (ptrObj.is_volatile) { |
| 2161 | | name += "volatile "; |
| 2162 | | } |
| 2163 | | if (ptrObj.has_addrspace) { |
| 2164 | | name += "addrspace("; |
| 2165 | | name += "." + ""; |
| 2166 | | name += ") "; |
| 2167 | | } |
| 2168 | | if (ptrObj.has_align) { |
| 2169 | | let align = exprName(ptrObj.align, opts); |
| 2170 | | if (opts.wantHtml) { |
| 2171 | | name += '<span class="tok-kw">align</span>('; |
| 2172 | | } else { |
| 2173 | | name += "align("; |
| 2174 | | } |
| 2175 | | if (opts.wantHtml) { |
| 2176 | | name += '<span class="tok-number">' + align + "</span>"; |
| 2177 | | } else { |
| 2178 | | name += align; |
| 2179 | | } |
| 2180 | | if (ptrObj.hostIntBytes != null) { |
| 2181 | | name += ":"; |
| 2182 | | if (opts.wantHtml) { |
| 2183 | | name += |
| 2184 | | '<span class="tok-number">' + |
| 2185 | | ptrObj.bitOffsetInHost + |
| 2186 | | "</span>"; |
| 2187 | | } else { |
| 2188 | | name += ptrObj.bitOffsetInHost; |
| 2189 | | } |
| 2190 | | name += ":"; |
| 2191 | | if (opts.wantHtml) { |
| 2192 | | name += |
| 2193 | | '<span class="tok-number">' + |
| 2194 | | ptrObj.hostIntBytes + |
| 2195 | | "</span>"; |
| 2196 | | } else { |
| 2197 | | name += ptrObj.hostIntBytes; |
| 2198 | | } |
| 2199 | | } |
| 2200 | | name += ") "; |
| 2201 | | } |
| 2202 | | //name += typeValueName(ptrObj.child, wantHtml, wantSubLink, null); |
| 2203 | | name += exprName(ptrObj.child, opts); |
| 2204 | | return name; |
| 2205 | | } |
| 2206 | | case typeKinds.Float: { |
| 2207 | | let floatObj = typeObj; |
| 2208 | | |
| 2209 | | if (opts.wantHtml) { |
| 2210 | | return '<span class="tok-type">' + floatObj.name + "</span>"; |
| 2211 | | } else { |
| 2212 | | return floatObj.name; |
| 2213 | | } |
| 1976 | yield { src: "opaque", tag: Tag.keyword_opaque }; |
| 1977 | yield Tok.space; |
| 1978 | yield Tok.l_brace; |
| 1979 | yield Tok.r_brace; |
| 1980 | return; |
| 2214 | 1981 | } |
| 2215 | | case typeKinds.Int: { |
| 2216 | | let intObj = typeObj; |
| 2217 | | let name = intObj.name; |
| 2218 | | if (opts.wantHtml) { |
| 2219 | | return '<span class="tok-type">' + name + "</span>"; |
| 2220 | | } else { |
| 2221 | | return name; |
| 2222 | | } |
| 1982 | case typeKinds.EnumLiteral: { |
| 1983 | yield { src: "(enum literal)", tag: Tag.identifier }; |
| 1984 | return; |
| 2223 | 1985 | } |
| 2224 | | case typeKinds.ComptimeInt: |
| 2225 | | if (opts.wantHtml) { |
| 2226 | | return '<span class="tok-type">comptime_int</span>'; |
| 2227 | | } else { |
| 2228 | | return "comptime_int"; |
| 2229 | | } |
| 2230 | | case typeKinds.ComptimeFloat: |
| 2231 | | if (opts.wantHtml) { |
| 2232 | | return '<span class="tok-type">comptime_float</span>'; |
| 2233 | | } else { |
| 2234 | | return "comptime_float"; |
| 2235 | | } |
| 2236 | | case typeKinds.Type: |
| 2237 | | if (opts.wantHtml) { |
| 2238 | | return '<span class="tok-type">type</span>'; |
| 2239 | | } else { |
| 2240 | | return "type"; |
| 2241 | | } |
| 2242 | | case typeKinds.Bool: |
| 2243 | | if (opts.wantHtml) { |
| 2244 | | return '<span class="tok-type">bool</span>'; |
| 2245 | | } else { |
| 2246 | | return "bool"; |
| 2247 | | } |
| 2248 | | case typeKinds.Void: |
| 2249 | | if (opts.wantHtml) { |
| 2250 | | return '<span class="tok-type">void</span>'; |
| 2251 | | } else { |
| 2252 | | return "void"; |
| 2253 | | } |
| 2254 | | case typeKinds.EnumLiteral: |
| 2255 | | if (opts.wantHtml) { |
| 2256 | | return '<span class="tok-type">(enum literal)</span>'; |
| 2257 | | } else { |
| 2258 | | return "(enum literal)"; |
| 2259 | | } |
| 2260 | | case typeKinds.NoReturn: |
| 2261 | | if (opts.wantHtml) { |
| 2262 | | return '<span class="tok-type">noreturn</span>'; |
| 2263 | | } else { |
| 2264 | | return "noreturn"; |
| 2265 | | } |
| 2266 | 1986 | case typeKinds.ErrorSet: { |
| 2267 | 1987 | let errSetObj = typeObj; |
| 2268 | | if (errSetObj.fields == null) { |
| 2269 | | return '<span class="tok-type">anyerror</span>'; |
| 1988 | if (errSetObj.fields === null) { |
| 1989 | yield { src: "anyerror", tag: Tag.identifier }; |
| 2270 | 1990 | } else if (errSetObj.fields.length == 0) { |
| 2271 | | return "error{}"; |
| 1991 | yield { src: "error", tag: Tag.keyword_error }; |
| 1992 | yield Tok.l_brace; |
| 1993 | yield Tok.r_brace; |
| 2272 | 1994 | } else if (errSetObj.fields.length == 1) { |
| 2273 | | return "error{" + errSetObj.fields[0].name + "}"; |
| 1995 | yield { src: "error", tag: Tag.keyword_error }; |
| 1996 | yield Tok.l_brace; |
| 1997 | yield { src: errSetObj.fields[0].name, tag: Tag.identifier }; |
| 1998 | yield Tok.r_brace; |
| 2274 | 1999 | } else { |
| 2275 | | // throw "TODO"; |
| 2276 | | let html = "error{ " + errSetObj.fields[0].name; |
| 2277 | | for (let i = 1; i < errSetObj.fields.length; i++) html += ", " + errSetObj.fields[i].name; |
| 2278 | | html += " }"; |
| 2279 | | return html; |
| 2000 | yield { src: "error", tag: Tag.keyword_error }; |
| 2001 | yield Tok.l_brace; |
| 2002 | yield { src: errSetObj.fields[0].name, tag: Tag.identifier }; |
| 2003 | for (let i = 1; i < errSetObj.fields.length; i++) { |
| 2004 | yield Tok.comma; |
| 2005 | yield Tok.space; |
| 2006 | yield { src: errSetObj.fields[i].name, tag: Tag.identifier }; |
| 2007 | } |
| 2008 | yield Tok.r_brace; |
| 2280 | 2009 | } |
| 2010 | return; |
| 2281 | 2011 | } |
| 2282 | | |
| 2283 | 2012 | case typeKinds.ErrorUnion: { |
| 2284 | 2013 | let errUnionObj = typeObj; |
| 2285 | | let lhs = exprName(errUnionObj.lhs, opts); |
| 2286 | | let rhs = exprName(errUnionObj.rhs, opts); |
| 2287 | | return lhs + "!" + rhs; |
| 2014 | yield* ex(errUnionObj.lhs, opts); |
| 2015 | yield { src: "!", tag: Tag.bang }; |
| 2016 | yield* ex(errUnionObj.rhs, opts); |
| 2017 | return; |
| 2288 | 2018 | } |
| 2289 | 2019 | case typeKinds.InferredErrorUnion: { |
| 2290 | 2020 | let errUnionObj = typeObj; |
| 2291 | | let payload = exprName(errUnionObj.payload, opts); |
| 2292 | | return "!" + payload; |
| 2021 | yield { src: "!", tag: Tag.bang }; |
| 2022 | yield* ex(errUnionObj.payload, opts); |
| 2023 | return; |
| 2293 | 2024 | } |
| 2294 | 2025 | case typeKinds.Fn: { |
| 2295 | 2026 | let fnObj = typeObj; |
| ... | ... | @@ -2299,45 +2030,32 @@ Happy writing! |
| 2299 | 2030 | opts.linkFnNameDecl = null; |
| 2300 | 2031 | let payloadHtml = ""; |
| 2301 | 2032 | if (opts.addParensIfFnSignature && fnObj.src == 0) { |
| 2302 | | payloadHtml += "("; |
| 2033 | yield Tok.l_paren; |
| 2303 | 2034 | } |
| 2304 | 2035 | if (fnObj.is_extern) { |
| 2305 | | if (opts.wantHtml) { |
| 2306 | | payloadHtml += '<span class="tok-kw">extern </span>'; |
| 2307 | | } else { |
| 2308 | | payloadHtml += "extern "; |
| 2309 | | } |
| 2036 | yield { src: "extern", tag: Tag.keyword_extern }; |
| 2037 | yield Tok.space; |
| 2310 | 2038 | } else if (fnObj.has_cc) { |
| 2311 | 2039 | let cc_expr = zigAnalysis.exprs[fnObj.cc]; |
| 2312 | 2040 | if (cc_expr.enumLiteral === "Inline") { |
| 2313 | | if(opts.wantHtml) { |
| 2314 | | payloadHtml += '<span class="tok-kw">inline </span>' |
| 2315 | | } else { |
| 2316 | | payloadHtml += "inline " |
| 2317 | | } |
| 2041 | yield { src: "inline", tag: Tag.keyword_inline }; |
| 2042 | yield Tok.space; |
| 2318 | 2043 | } |
| 2319 | 2044 | } |
| 2320 | 2045 | if (fnObj.has_lib_name) { |
| 2321 | | payloadHtml += '"' + fnObj.lib_name + '" '; |
| 2046 | yield { src: '"' + fnObj.lib_name + '"', tag: Tag.string_literal }; |
| 2047 | yield Tok.space; |
| 2322 | 2048 | } |
| 2323 | | if (opts.wantHtml) { |
| 2324 | | payloadHtml += '<span class="tok-kw">fn </span>'; |
| 2325 | | if (fnDecl) { |
| 2326 | | payloadHtml += '<span class="tok-fn">'; |
| 2327 | | if (linkFnNameDecl) { |
| 2328 | | payloadHtml += |
| 2329 | | '<a href="' + linkFnNameDecl + '">' + |
| 2330 | | escapeHtml(fnDecl.name) + |
| 2331 | | "</a>"; |
| 2332 | | } else { |
| 2333 | | payloadHtml += escapeHtml(fnDecl.name); |
| 2334 | | } |
| 2335 | | payloadHtml += "</span>"; |
| 2049 | yield { src: "fn", tag: Tag.keyword_fn }; |
| 2050 | yield Tok.space; |
| 2051 | if (fnDecl) { |
| 2052 | if (linkFnNameDecl) { |
| 2053 | yield { src: fnDecl.name, tag: Tag.identifier, link: linkFnNameDecl, fnDecl: false }; |
| 2054 | } else { |
| 2055 | yield { src: fnDecl.name, tag: Tag.identifier, fnDecl: true }; |
| 2336 | 2056 | } |
| 2337 | | } else { |
| 2338 | | payloadHtml += "fn "; |
| 2339 | 2057 | } |
| 2340 | | payloadHtml += "("; |
| 2058 | yield Tok.l_paren; |
| 2341 | 2059 | if (fnObj.params) { |
| 2342 | 2060 | let fields = null; |
| 2343 | 2061 | let isVarArgs = false; |
| ... | ... | @@ -2349,13 +2067,10 @@ Happy writing! |
| 2349 | 2067 | |
| 2350 | 2068 | for (let i = 0; i < fnObj.params.length; i += 1) { |
| 2351 | 2069 | if (i != 0) { |
| 2352 | | payloadHtml += ", "; |
| 2070 | yield Tok.comma; |
| 2071 | yield Tok.space; |
| 2353 | 2072 | } |
| 2354 | 2073 | |
| 2355 | | if (opts.wantHtml) { |
| 2356 | | payloadHtml += |
| 2357 | | "<span class='argBreaker'><br>&nbsp;&nbsp;&nbsp;&nbsp;</span>"; |
| 2358 | | } |
| 2359 | 2074 | let value = fnObj.params[i]; |
| 2360 | 2075 | let paramValue = resolveValue({ expr: value }); |
| 2361 | 2076 | |
| ... | ... | @@ -2363,24 +2078,20 @@ Happy writing! |
| 2363 | 2078 | let paramNode = getAstNode(fields[i]); |
| 2364 | 2079 | |
| 2365 | 2080 | if (paramNode.varArgs) { |
| 2366 | | payloadHtml += "..."; |
| 2081 | yield Tok.period; |
| 2082 | yield Tok.period; |
| 2083 | yield Tok.period; |
| 2367 | 2084 | continue; |
| 2368 | 2085 | } |
| 2369 | 2086 | |
| 2370 | 2087 | if (paramNode.noalias) { |
| 2371 | | if (opts.wantHtml) { |
| 2372 | | payloadHtml += '<span class="tok-kw">noalias</span> '; |
| 2373 | | } else { |
| 2374 | | payloadHtml += "noalias "; |
| 2375 | | } |
| 2088 | yield { src: "noalias", tag: Tag.keyword_noalias }; |
| 2089 | yield Tok.space; |
| 2376 | 2090 | } |
| 2377 | 2091 | |
| 2378 | 2092 | if (paramNode.comptime) { |
| 2379 | | if (opts.wantHtml) { |
| 2380 | | payloadHtml += '<span class="tok-kw">comptime</span> '; |
| 2381 | | } else { |
| 2382 | | payloadHtml += "comptime "; |
| 2383 | | } |
| 2093 | yield { src: "comptime", tag: Tag.keyword_comptime }; |
| 2094 | yield Tok.space; |
| 2384 | 2095 | } |
| 2385 | 2096 | |
| 2386 | 2097 | let paramName = paramNode.name; |
| ... | ... | @@ -2390,94 +2101,102 @@ Happy writing! |
| 2390 | 2101 | if (paramName === "") { |
| 2391 | 2102 | paramName = "_"; |
| 2392 | 2103 | } |
| 2393 | | payloadHtml += paramName + ": "; |
| 2104 | yield { src: paramName, tag: Tag.identifier }; |
| 2105 | yield Tok.colon; |
| 2106 | yield Tok.space; |
| 2394 | 2107 | } |
| 2395 | 2108 | } |
| 2396 | 2109 | } |
| 2397 | 2110 | |
| 2398 | 2111 | if (isVarArgs && i === fnObj.params.length - 1) { |
| 2399 | | payloadHtml += "..."; |
| 2112 | yield Tok.period; |
| 2113 | yield Tok.period; |
| 2114 | yield Tok.period; |
| 2400 | 2115 | } else if ("alignOf" in value) { |
| 2401 | | payloadHtml += exprName(value, opts); |
| 2116 | yield* ex(value, opts); |
| 2402 | 2117 | } else if ("typeOf" in value) { |
| 2403 | | payloadHtml += exprName(value, opts); |
| 2118 | yield* ex(value, opts); |
| 2404 | 2119 | } else if ("typeOf_peer" in value) { |
| 2405 | | payloadHtml += exprName(value, opts); |
| 2120 | yield* ex(value, opts); |
| 2406 | 2121 | } else if ("declRef" in value) { |
| 2407 | | payloadHtml += exprName(value, opts); |
| 2122 | yield* ex(value, opts); |
| 2408 | 2123 | } else if ("call" in value) { |
| 2409 | | payloadHtml += exprName(value, opts); |
| 2124 | yield* ex(value, opts); |
| 2410 | 2125 | } else if ("refPath" in value) { |
| 2411 | | payloadHtml += exprName(value, opts); |
| 2126 | yield* ex(value, opts); |
| 2412 | 2127 | } else if ("type" in value) { |
| 2413 | | payloadHtml += exprName(value, opts); |
| 2128 | yield* ex(value, opts); |
| 2414 | 2129 | //payloadHtml += '<span class="tok-kw">' + name + "</span>"; |
| 2415 | 2130 | } else if ("binOpIndex" in value) { |
| 2416 | | payloadHtml += exprName(value, opts); |
| 2131 | yield* ex(value, opts); |
| 2417 | 2132 | } else if ("comptimeExpr" in value) { |
| 2418 | 2133 | let comptimeExpr = |
| 2419 | 2134 | zigAnalysis.comptimeExprs[value.comptimeExpr].code; |
| 2420 | | if (opts.wantHtml) { |
| 2421 | | payloadHtml += |
| 2422 | | '<span class="tok-kw">' + comptimeExpr + "</span>"; |
| 2423 | | } else { |
| 2424 | | payloadHtml += comptimeExpr; |
| 2425 | | } |
| 2426 | | } else if (opts.wantHtml) { |
| 2427 | | payloadHtml += '<span class="tok-kw">anytype</span>'; |
| 2135 | yield* Tokenizer(comptimeExpr); |
| 2428 | 2136 | } else { |
| 2429 | | payloadHtml += "anytype"; |
| 2137 | yield { src: "anytype", tag: Tag.keyword_anytype }; |
| 2430 | 2138 | } |
| 2431 | 2139 | } |
| 2432 | 2140 | } |
| 2433 | 2141 | |
| 2434 | | if (opts.wantHtml) { |
| 2435 | | payloadHtml += "<span class='argBreaker'>,<br></span>"; |
| 2436 | | } |
| 2437 | | payloadHtml += ") "; |
| 2142 | yield Tok.r_paren; |
| 2143 | yield Tok.space; |
| 2438 | 2144 | |
| 2439 | 2145 | if (fnObj.has_align) { |
| 2440 | 2146 | let align = zigAnalysis.exprs[fnObj.align]; |
| 2441 | | payloadHtml += "align(" + exprName(align, opts) + ") "; |
| 2147 | yield { src: "align", tag: Tag.keyword_align }; |
| 2148 | yield Tok.l_paren; |
| 2149 | yield* ex(align, opts); |
| 2150 | yield Tok.r_paren; |
| 2151 | yield Tok.space; |
| 2442 | 2152 | } |
| 2443 | 2153 | if (fnObj.has_cc) { |
| 2444 | 2154 | let cc = zigAnalysis.exprs[fnObj.cc]; |
| 2445 | 2155 | if (cc) { |
| 2446 | 2156 | if (cc.enumLiteral !== "Inline") { |
| 2447 | | payloadHtml += "callconv(" + exprName(cc, opts) + ") "; |
| 2157 | yield { src: "collconv", tag: Tag.keyword_callconv }; |
| 2158 | yield Tok.l_paren; |
| 2159 | yield* ex(cc, opts); |
| 2160 | yield Tok.r_paren; |
| 2161 | yield Tok.space; |
| 2448 | 2162 | } |
| 2449 | 2163 | } |
| 2450 | 2164 | } |
| 2451 | 2165 | |
| 2452 | 2166 | if (fnObj.is_inferred_error) { |
| 2453 | | payloadHtml += "!"; |
| 2167 | yield { src: "!", tag: Tag.bang }; |
| 2454 | 2168 | } |
| 2455 | 2169 | if (fnObj.ret != null) { |
| 2456 | | payloadHtml += exprName(fnObj.ret, { |
| 2170 | yield* ex(fnObj.ret, { |
| 2457 | 2171 | ...opts, |
| 2458 | 2172 | addParensIfFnSignature: true, |
| 2459 | 2173 | }); |
| 2460 | | } else if (opts.wantHtml) { |
| 2461 | | payloadHtml += '<span class="tok-kw">anytype</span>'; |
| 2462 | 2174 | } else { |
| 2463 | | payloadHtml += "anytype"; |
| 2175 | yield { src: "anytype", tag: Tag.keyword_anytype }; |
| 2464 | 2176 | } |
| 2465 | 2177 | |
| 2466 | 2178 | if (opts.addParensIfFnSignature && fnObj.src == 0) { |
| 2467 | | payloadHtml += ")"; |
| 2179 | yield Tok.r_paren; |
| 2468 | 2180 | } |
| 2469 | | return payloadHtml; |
| 2181 | return; |
| 2470 | 2182 | } |
| 2471 | | // if (wantHtml) { |
| 2472 | | // return escapeHtml(typeObj.name); |
| 2473 | | // } else { |
| 2474 | | // return typeObj.name; |
| 2475 | | // } |
| 2476 | 2183 | } |
| 2477 | 2184 | } |
| 2185 | case "typeOf": { |
| 2186 | const typeRefArg = zigAnalysis.exprs[expr.typeOf]; |
| 2187 | yield { src: "@TypeOf", tag: Tag.builtin }; |
| 2188 | yield Tok.l_paren; |
| 2189 | yield* ex(typeRefArg, opts); |
| 2190 | yield Tok.r_paren; |
| 2191 | return; |
| 2192 | } |
| 2478 | 2193 | } |
| 2194 | |
| 2195 | |
| 2479 | 2196 | } |
| 2480 | 2197 | |
| 2198 | |
| 2199 | |
| 2481 | 2200 | function shouldSkipParamName(typeRef, paramName) { |
| 2482 | 2201 | let resolvedTypeRef = resolveValue({ expr: typeRef }); |
| 2483 | 2202 | if ("type" in resolvedTypeRef) { |
| ... | ... | @@ -2504,13 +2223,13 @@ Happy writing! |
| 2504 | 2223 | typeObj === |
| 2505 | 2224 | getType(zigAnalysis.modules[zigAnalysis.rootMod].main) |
| 2506 | 2225 | ) { |
| 2507 | | name = "std"; |
| 2226 | name = renderSingleToken(Tok.identifier("std")); |
| 2508 | 2227 | } else { |
| 2509 | | name = exprName({ type: typeObj }, { wantHtml: false, wantLink: false }); |
| 2228 | name = renderTokens(ex({ type: typeObj })); |
| 2510 | 2229 | } |
| 2511 | 2230 | if (name != null && name != "") { |
| 2512 | | domHdrName.innerText = |
| 2513 | | name + " (" + zigAnalysis.typeKinds[typeObj.kind] + ")"; |
| 2231 | domHdrName.innerHTML = "<pre class='inline'>" + name + "</pre> (" |
| 2232 | + zigAnalysis.typeKinds[typeObj.kind] + ")"; |
| 2514 | 2233 | domHdrName.classList.remove("hidden"); |
| 2515 | 2234 | } |
| 2516 | 2235 | if (typeObj.kind == typeKinds.ErrorSet) { |
| ... | ... | @@ -2528,7 +2247,7 @@ Happy writing! |
| 2528 | 2247 | //let srcObj = zigAnalysis.astNodes[errObj.src]; |
| 2529 | 2248 | errorList.push(errObj); |
| 2530 | 2249 | } |
| 2531 | | errorList.sort(function (a, b) { |
| 2250 | errorList.sort(function(a, b) { |
| 2532 | 2251 | return operatorCompare(a.name.toLowerCase(), b.name.toLowerCase()); |
| 2533 | 2252 | }); |
| 2534 | 2253 | |
| ... | ... | @@ -2624,55 +2343,80 @@ Happy writing! |
| 2624 | 2343 | if (resolvedValue.expr.fieldRef) { |
| 2625 | 2344 | const declRef = decl.value.expr.refPath[0].declRef; |
| 2626 | 2345 | const type = getDecl(declRef); |
| 2627 | | domFnProtoCode.innerHTML = |
| 2628 | | '<span class="tok-kw">const</span> ' + |
| 2629 | | escapeHtml(decl.name) + |
| 2630 | | ": " + |
| 2631 | | type.name + |
| 2632 | | " = " + |
| 2633 | | exprName(decl.value.expr, { wantHtml: true, wantLink: true }) + |
| 2634 | | ";"; |
| 2346 | |
| 2347 | domFnProtoCode.innerHTML = renderTokens( |
| 2348 | (function*() { |
| 2349 | yield Tok.const; |
| 2350 | yield Tok.space; |
| 2351 | yield Tok.identifier(decl.name); |
| 2352 | yield Tok.colon; |
| 2353 | yield Tok.space; |
| 2354 | yield Tok.identifier(type.name); |
| 2355 | yield Tok.space; |
| 2356 | yield Tok.eql; |
| 2357 | yield Tok.space; |
| 2358 | yield* ex(decl.value.expr, {}); |
| 2359 | yield Tok.semi; |
| 2360 | })()); |
| 2635 | 2361 | } else if ( |
| 2636 | 2362 | resolvedValue.expr.string !== undefined || |
| 2637 | 2363 | resolvedValue.expr.call !== undefined || |
| 2638 | 2364 | resolvedValue.expr.comptimeExpr !== undefined |
| 2639 | 2365 | ) { |
| 2640 | | let typeRef = null; |
| 2641 | | if (resolvedValue.typeRef !== undefined) { |
| 2642 | | typeRef = resolvedValue.typeRef; |
| 2643 | | } |
| 2644 | | domFnProtoCode.innerHTML = |
| 2645 | | '<span class="tok-kw">const</span> ' + |
| 2646 | | escapeHtml(decl.name) + |
| 2647 | | ": " + |
| 2648 | | exprName(typeRef !== null ? typeRef : resolvedValue.expr, { wantHtml: true, wantLink: true }) + |
| 2649 | | " = " + |
| 2650 | | exprName(decl.value.expr, { wantHtml: true, wantLink: true }) + |
| 2651 | | ";"; |
| 2366 | domFnProtoCode.innerHTML = renderTokens( |
| 2367 | (function*() { |
| 2368 | yield Tok.const; |
| 2369 | yield Tok.space; |
| 2370 | yield Tok.identifier(decl.name); |
| 2371 | if (decl.value.typeRef) { |
| 2372 | yield Tok.colon; |
| 2373 | yield Tok.space; |
| 2374 | yield* ex(decl.value.typeRef, {}); |
| 2375 | } |
| 2376 | yield Tok.space; |
| 2377 | yield Tok.eql; |
| 2378 | yield Tok.space; |
| 2379 | yield* ex(decl.value.expr, {}); |
| 2380 | yield Tok.semi; |
| 2381 | })()); |
| 2652 | 2382 | } else if (resolvedValue.expr.compileError) { |
| 2653 | | domFnProtoCode.innerHTML = |
| 2654 | | '<span class="tok-kw">const</span> ' + |
| 2655 | | escapeHtml(decl.name) + |
| 2656 | | " = " + |
| 2657 | | exprName(decl.value.expr, { wantHtml: true, wantLink: true }) + |
| 2658 | | ";"; |
| 2383 | domFnProtoCode.innerHTML = renderTokens( |
| 2384 | (function*() { |
| 2385 | yield Tok.const; |
| 2386 | yield Tok.space; |
| 2387 | yield Tok.identifier(decl.name); |
| 2388 | yield Tok.space; |
| 2389 | yield Tok.eql; |
| 2390 | yield Tok.space; |
| 2391 | yield* ex(decl.value.expr, {}); |
| 2392 | yield Tok.semi; |
| 2393 | })()); |
| 2659 | 2394 | } else { |
| 2660 | | domFnProtoCode.innerHTML = |
| 2661 | | '<span class="tok-kw">const</span> ' + |
| 2662 | | escapeHtml(decl.name) + |
| 2663 | | ": " + |
| 2664 | | exprName(resolvedValue.typeRef, { wantHtml: true, wantLink: true }) + |
| 2665 | | " = " + |
| 2666 | | exprName(decl.value.expr, { wantHtml: true, wantLink: true }) + |
| 2667 | | ";"; |
| 2395 | const parent = getType(decl.parent_container); |
| 2396 | domFnProtoCode.innerHTML = renderTokens( |
| 2397 | (function*() { |
| 2398 | yield Tok.const; |
| 2399 | yield Tok.space; |
| 2400 | yield Tok.identifier(decl.name); |
| 2401 | if (decl.value.typeRef !== null) { |
| 2402 | yield Tok.colon; |
| 2403 | yield Tok.space; |
| 2404 | yield* ex(decl.value.typeRef, {}); |
| 2405 | } |
| 2406 | yield Tok.space; |
| 2407 | yield Tok.eql; |
| 2408 | yield Tok.space; |
| 2409 | yield* ex(decl.value.expr, {}); |
| 2410 | yield Tok.semi; |
| 2411 | })()); |
| 2668 | 2412 | } |
| 2669 | 2413 | |
| 2670 | 2414 | let docs = getAstNode(decl.src).docs; |
| 2671 | 2415 | if (docs != null) { |
| 2672 | 2416 | // TODO: it shouldn't just be decl.parent_container, but rather |
| 2673 | 2417 | // the type that the decl holds (if the value is a type) |
| 2674 | | domTldDocs.innerHTML = markdown(docs, getType(decl.parent_container)); |
| 2675 | | |
| 2418 | domTldDocs.innerHTML = markdown(docs, decl); |
| 2419 | |
| 2676 | 2420 | domTldDocs.classList.remove("hidden"); |
| 2677 | 2421 | } |
| 2678 | 2422 | |
| ... | ... | @@ -2685,43 +2429,68 @@ Happy writing! |
| 2685 | 2429 | if (resolvedVar.expr.fieldRef) { |
| 2686 | 2430 | const declRef = decl.value.expr.refPath[0].declRef; |
| 2687 | 2431 | const type = getDecl(declRef); |
| 2688 | | domFnProtoCode.innerHTML = |
| 2689 | | '<span class="tok-kw">var</span> ' + |
| 2690 | | escapeHtml(decl.name) + |
| 2691 | | ": " + |
| 2692 | | type.name + |
| 2693 | | " = " + |
| 2694 | | exprName(decl.value.expr, { wantHtml: true, wantLink: true }) + |
| 2695 | | ";"; |
| 2432 | domFnProtoCode.innerHTML = renderTokens( |
| 2433 | (function*() { |
| 2434 | yield Tok.var; |
| 2435 | yield Tok.space; |
| 2436 | yield Tok.identifier(decl.name); |
| 2437 | yield Tok.colon; |
| 2438 | yield Tok.space; |
| 2439 | yield Tok.identifier(type.name); |
| 2440 | yield Tok.space; |
| 2441 | yield Tok.eql; |
| 2442 | yield Tok.space; |
| 2443 | yield* ex(decl.value.expr, {}); |
| 2444 | yield Tok.semi; |
| 2445 | })()); |
| 2696 | 2446 | } else if ( |
| 2697 | 2447 | resolvedVar.expr.string !== undefined || |
| 2698 | 2448 | resolvedVar.expr.call !== undefined || |
| 2699 | 2449 | resolvedVar.expr.comptimeExpr !== undefined |
| 2700 | 2450 | ) { |
| 2701 | | domFnProtoCode.innerHTML = |
| 2702 | | '<span class="tok-kw">var</span> ' + |
| 2703 | | escapeHtml(decl.name) + |
| 2704 | | ": " + |
| 2705 | | exprName(resolvedVar.typeRef !== null ? resolvedVar.typeRef : resolvedVar.expr, { wantHtml: true, wantLink: true }) + |
| 2706 | | " = " + |
| 2707 | | exprName(decl.value.expr, { wantHtml: true, wantLink: true }) + |
| 2708 | | ";"; |
| 2451 | domFnProtoCode.innerHTML = renderTokens( |
| 2452 | (function*() { |
| 2453 | yield Tok.var; |
| 2454 | yield Tok.space; |
| 2455 | yield Tok.identifier(decl.name); |
| 2456 | if (decl.value.typeRef) { |
| 2457 | yield Tok.colon; |
| 2458 | yield Tok.space; |
| 2459 | yield* ex(decl.value.typeRef, {}); |
| 2460 | } |
| 2461 | yield Tok.space; |
| 2462 | yield Tok.eql; |
| 2463 | yield Tok.space; |
| 2464 | yield* ex(decl.value.expr, {}); |
| 2465 | yield Tok.semi; |
| 2466 | })()); |
| 2709 | 2467 | } else if (resolvedVar.expr.compileError) { |
| 2710 | | domFnProtoCode.innerHTML = |
| 2711 | | '<span class="tok-kw">var</span> ' + |
| 2712 | | escapeHtml(decl.name) + |
| 2713 | | " = " + |
| 2714 | | exprName(decl.value.expr, { wantHtml: true, wantLink: true }) + |
| 2715 | | ";"; |
| 2468 | domFnProtoCode.innerHTML = renderTokens( |
| 2469 | (function*() { |
| 2470 | yield Tok.var; |
| 2471 | yield Tok.space; |
| 2472 | yield Tok.identifier(decl.name); |
| 2473 | yield Tok.space; |
| 2474 | yield Tok.eql; |
| 2475 | yield Tok.space; |
| 2476 | yield* ex(decl.value.expr, {}); |
| 2477 | yield Tok.semi; |
| 2478 | })()); |
| 2716 | 2479 | } else { |
| 2717 | | domFnProtoCode.innerHTML = |
| 2718 | | '<span class="tok-kw">var</span> ' + |
| 2719 | | escapeHtml(decl.name) + |
| 2720 | | ": " + |
| 2721 | | exprName(resolvedVar.typeRef, { wantHtml: true, wantLink: true }) + |
| 2722 | | " = " + |
| 2723 | | exprName(decl.value.expr, { wantHtml: true, wantLink: true }) + |
| 2724 | | ";"; |
| 2480 | domFnProtoCode.innerHTML = renderTokens( |
| 2481 | (function*() { |
| 2482 | yield Tok.var; |
| 2483 | yield Tok.space; |
| 2484 | yield Tok.identifier(decl.name); |
| 2485 | yield Tok.colon; |
| 2486 | yield Tok.space; |
| 2487 | yield* ex(resolvedVar.typeRef, {}); |
| 2488 | yield Tok.space; |
| 2489 | yield Tok.eql; |
| 2490 | yield Tok.space; |
| 2491 | yield* ex(decl.value.expr, {}); |
| 2492 | yield Tok.semi; |
| 2493 | })()); |
| 2725 | 2494 | } |
| 2726 | 2495 | |
| 2727 | 2496 | let docs = getAstNode(decl.src).docs; |
| ... | ... | @@ -2955,7 +2724,7 @@ Happy writing! |
| 2955 | 2724 | resizeDomList( |
| 2956 | 2725 | domListFns, |
| 2957 | 2726 | fnsList.length, |
| 2958 | | "<div><dt><div class=\"fnSignature\"></div><div></div></dt><dd></dd></div>" |
| 2727 | '<div><dt><pre class="inline fnSignature"></pre><div></div></dt><dd></dd></div>' |
| 2959 | 2728 | ); |
| 2960 | 2729 | |
| 2961 | 2730 | for (let i = 0; i < fnsList.length; i += 1) { |
| ... | ... | @@ -2968,12 +2737,10 @@ Happy writing! |
| 2968 | 2737 | |
| 2969 | 2738 | let declType = resolveValue(decl.value); |
| 2970 | 2739 | console.assert("type" in declType.expr); |
| 2971 | | tdFnSignature.innerHTML = exprName(declType.expr, { |
| 2972 | | wantHtml: true, |
| 2973 | | wantLink: true, |
| 2740 | tdFnSignature.innerHTML = renderTokens(ex(declType.expr, { |
| 2974 | 2741 | fnDecl: decl, |
| 2975 | 2742 | linkFnNameDecl: navLinkDecl(decl.name), |
| 2976 | | }); |
| 2743 | })); |
| 2977 | 2744 | tdFnSrc.innerHTML = "<a style=\"float: right;\" target=\"_blank\" href=\"" + |
| 2978 | 2745 | sourceFileLink(decl) + "\">[src]</a>"; |
| 2979 | 2746 | |
| ... | ... | @@ -3018,14 +2785,22 @@ Happy writing! |
| 3018 | 2785 | if (container.kind === typeKinds.Enum) { |
| 3019 | 2786 | let value = container.values[i]; |
| 3020 | 2787 | if (value !== null) { |
| 3021 | | html += " = " + exprName(value, { wantHtml: true, wantLink: true }); |
| 2788 | html += renderTokens((function*() { |
| 2789 | yield Tok.space; |
| 2790 | yield Tok.eql; |
| 2791 | yield Tok.space; |
| 2792 | yield* ex(value, {}); |
| 2793 | })()); |
| 3022 | 2794 | } |
| 3023 | 2795 | } else { |
| 3024 | 2796 | let fieldTypeExpr = container.field_types[i]; |
| 3025 | 2797 | if (container.kind !== typeKinds.Struct || !container.is_tuple) { |
| 3026 | | html += ": "; |
| 2798 | html += renderTokens((function*() { |
| 2799 | yield Tok.colon; |
| 2800 | yield Tok.space; |
| 2801 | })()); |
| 3027 | 2802 | } |
| 3028 | | html += exprName(fieldTypeExpr, { wantHtml: true, wantLink: true }); |
| 2803 | html += renderTokens(ex(fieldTypeExpr, {})); |
| 3029 | 2804 | let tsn = typeShorthandName(fieldTypeExpr); |
| 3030 | 2805 | if (tsn) { |
| 3031 | 2806 | html += "<span> (" + tsn + ")</span>"; |
| ... | ... | @@ -3033,7 +2808,12 @@ Happy writing! |
| 3033 | 2808 | if (container.kind === typeKinds.Struct && !container.is_tuple) { |
| 3034 | 2809 | let defaultInitExpr = container.field_defaults[i]; |
| 3035 | 2810 | if (defaultInitExpr !== null) { |
| 3036 | | html += " = " + exprName(defaultInitExpr, { wantHtml: true, wantLink: true }); |
| 2811 | html += renderTokens((function*() { |
| 2812 | yield Tok.space; |
| 2813 | yield Tok.eql; |
| 2814 | yield Tok.space; |
| 2815 | yield* ex(defaultInitExpr, {}); |
| 2816 | })()); |
| 3037 | 2817 | } |
| 3038 | 2818 | } |
| 3039 | 2819 | } |
| ... | ... | @@ -3052,7 +2832,7 @@ Happy writing! |
| 3052 | 2832 | resizeDomList( |
| 3053 | 2833 | domListGlobalVars, |
| 3054 | 2834 | varsList.length, |
| 3055 | | '<tr><td><a href="#"></a></td><td></td><td></td></tr>' |
| 2835 | '<tr><td><a href="#"></a></td><td><pre class="inline"></pre></td><td></td></tr>' |
| 3056 | 2836 | ); |
| 3057 | 2837 | for (let i = 0; i < varsList.length; i += 1) { |
| 3058 | 2838 | let decl = varsList[i]; |
| ... | ... | @@ -3061,15 +2841,13 @@ Happy writing! |
| 3061 | 2841 | let tdName = trDom.children[0]; |
| 3062 | 2842 | let tdNameA = tdName.children[0]; |
| 3063 | 2843 | let tdType = trDom.children[1]; |
| 2844 | let preType = tdType.children[0]; |
| 3064 | 2845 | let tdDesc = trDom.children[2]; |
| 3065 | 2846 | |
| 3066 | 2847 | tdNameA.setAttribute("href", navLinkDecl(decl.name)); |
| 3067 | 2848 | tdNameA.textContent = decl.name; |
| 3068 | 2849 | |
| 3069 | | tdType.innerHTML = exprName(walkResultTypeRef(decl.value), { |
| 3070 | | wantHtml: true, |
| 3071 | | wantLink: true, |
| 3072 | | }); |
| 2850 | preType.innerHTML = renderTokens(ex(walkResultTypeRef(decl.value), {})); |
| 3073 | 2851 | |
| 3074 | 2852 | let docs = getAstNode(decl.src).docs; |
| 3075 | 2853 | if (docs != null) { |
| ... | ... | @@ -3085,7 +2863,7 @@ Happy writing! |
| 3085 | 2863 | resizeDomList( |
| 3086 | 2864 | domListValues, |
| 3087 | 2865 | valsList.length, |
| 3088 | | '<tr><td><a href="#"></a></td><td></td><td></td></tr>' |
| 2866 | '<tr><td><a href="#"></a></td><td><pre class="inline"></pre></td><td></td></tr>' |
| 3089 | 2867 | ); |
| 3090 | 2868 | for (let i = 0; i < valsList.length; i += 1) { |
| 3091 | 2869 | let decl = valsList[i]; |
| ... | ... | @@ -3094,15 +2872,13 @@ Happy writing! |
| 3094 | 2872 | let tdName = trDom.children[0]; |
| 3095 | 2873 | let tdNameA = tdName.children[0]; |
| 3096 | 2874 | let tdType = trDom.children[1]; |
| 2875 | let preType = tdType.children[0]; |
| 3097 | 2876 | let tdDesc = trDom.children[2]; |
| 3098 | 2877 | |
| 3099 | 2878 | tdNameA.setAttribute("href", navLinkDecl(decl.name)); |
| 3100 | 2879 | tdNameA.textContent = decl.name; |
| 3101 | 2880 | |
| 3102 | | tdType.innerHTML = exprName(walkResultTypeRef(decl.value), { |
| 3103 | | wantHtml: true, |
| 3104 | | wantLink: true, |
| 3105 | | }); |
| 2881 | preType.innerHTML = renderTokens(ex(walkResultTypeRef(decl.value), {})); |
| 3106 | 2882 | |
| 3107 | 2883 | let docs = getAstNode(decl.src).docs; |
| 3108 | 2884 | if (docs != null) { |
| ... | ... | @@ -3118,24 +2894,21 @@ Happy writing! |
| 3118 | 2894 | resizeDomList( |
| 3119 | 2895 | domListTests, |
| 3120 | 2896 | testsList.length, |
| 3121 | | '<tr><td><a href="#"></a></td><td></td><td></td></tr>' |
| 2897 | '<tr><td><pre class="inline"></pre></td><td><pre class="inline"></pre></td><td></td></tr>' |
| 3122 | 2898 | ); |
| 3123 | 2899 | for (let i = 0; i < testsList.length; i += 1) { |
| 3124 | 2900 | let decl = testsList[i]; |
| 3125 | 2901 | let trDom = domListTests.children[i]; |
| 3126 | 2902 | |
| 3127 | 2903 | let tdName = trDom.children[0]; |
| 3128 | | let tdNameA = tdName.children[0]; |
| 2904 | let tdNamePre = tdName.children[0]; |
| 3129 | 2905 | let tdType = trDom.children[1]; |
| 2906 | let tdTypePre = tdType.children[0]; |
| 3130 | 2907 | let tdDesc = trDom.children[2]; |
| 3131 | 2908 | |
| 3132 | | tdNameA.setAttribute("href", navLinkDecl(decl.name)); |
| 3133 | | tdNameA.textContent = decl.name; |
| 2909 | tdNamePre.innerHTML = renderSingleToken(Tok.identifier(decl.name)); |
| 3134 | 2910 | |
| 3135 | | tdType.innerHTML = exprName(walkResultTypeRef(decl.value), { |
| 3136 | | wantHtml: true, |
| 3137 | | wantLink: true, |
| 3138 | | }); |
| 2911 | tdTypePre.innerHTML = ex(walkResultTypeRef(decl.value), {}); |
| 3139 | 2912 | |
| 3140 | 2913 | let docs = getAstNode(decl.src).docs; |
| 3141 | 2914 | if (docs != null) { |
| ... | ... | @@ -3260,7 +3033,7 @@ Happy writing! |
| 3260 | 3033 | |
| 3261 | 3034 | return; |
| 3262 | 3035 | case NAV_MODES.GUIDES: |
| 3263 | | |
| 3036 | |
| 3264 | 3037 | const sections = zigAnalysis.guide_sections; |
| 3265 | 3038 | if (sections.length != 0 && sections[0].guides.length != 0 && nonSearchPart == "") { |
| 3266 | 3039 | location.hash = NAV_MODES.GUIDES + sections[0].guides[0].name; |
| ... | ... | @@ -3428,10 +3201,10 @@ Happy writing! |
| 3428 | 3201 | if (list[declIndex] != null) continue; |
| 3429 | 3202 | |
| 3430 | 3203 | let decl = getDecl(declIndex); |
| 3431 | | |
| 3204 | |
| 3432 | 3205 | if (decl.is_uns) { |
| 3433 | 3206 | let unsDeclList = [decl]; |
| 3434 | | while(unsDeclList.length != 0) { |
| 3207 | while (unsDeclList.length != 0) { |
| 3435 | 3208 | let unsDecl = unsDeclList.pop(); |
| 3436 | 3209 | let unsDeclVal = resolveValue(unsDecl.value); |
| 3437 | 3210 | if (!("type" in unsDeclVal.expr)) continue; |
| ... | ... | @@ -3441,7 +3214,7 @@ Happy writing! |
| 3441 | 3214 | for (let unsDeclI = 0; unsDeclI < unsPubDeclLen; unsDeclI += 1) { |
| 3442 | 3215 | let childDeclIndex = unsType.pubDecls[unsDeclI]; |
| 3443 | 3216 | let childDecl = getDecl(childDeclIndex); |
| 3444 | | |
| 3217 | |
| 3445 | 3218 | if (childDecl.is_uns) { |
| 3446 | 3219 | unsDeclList.push(childDecl); |
| 3447 | 3220 | } else { |
| ... | ... | @@ -3460,54 +3233,54 @@ Happy writing! |
| 3460 | 3233 | return list; |
| 3461 | 3234 | } |
| 3462 | 3235 | |
| 3463 | | function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) { |
| 3464 | | let declVal = resolveValue(decl.value); |
| 3465 | | let declNames = item.declNames.concat([decl.name]); |
| 3466 | | let declIndexes = item.declIndexes.concat([declIndex]); |
| 3236 | function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) { |
| 3237 | let declVal = resolveValue(decl.value); |
| 3238 | let declNames = item.declNames.concat([decl.name]); |
| 3239 | let declIndexes = item.declIndexes.concat([declIndex]); |
| 3467 | 3240 | |
| 3468 | | if (list[declIndex] != null) return; |
| 3469 | | list[declIndex] = { |
| 3470 | | modNames: modNames, |
| 3471 | | declNames: declNames, |
| 3472 | | declIndexes: declIndexes, |
| 3473 | | }; |
| 3241 | if (list[declIndex] != null) return; |
| 3242 | list[declIndex] = { |
| 3243 | modNames: modNames, |
| 3244 | declNames: declNames, |
| 3245 | declIndexes: declIndexes, |
| 3246 | }; |
| 3474 | 3247 | |
| 3475 | | // add to search index |
| 3476 | | { |
| 3477 | | declSearchIndex.add(decl.name, {declIndex}); |
| 3478 | | } |
| 3479 | | |
| 3480 | | |
| 3481 | | if ("type" in declVal.expr) { |
| 3482 | | let value = getType(declVal.expr.type); |
| 3483 | | if (declCanRepresentTypeKind(value.kind)) { |
| 3484 | | canonTypeDecls[declVal.type] = declIndex; |
| 3248 | // add to search index |
| 3249 | { |
| 3250 | declSearchIndex.add(decl.name, { declIndex }); |
| 3485 | 3251 | } |
| 3486 | 3252 | |
| 3487 | | if (isContainerType(value)) { |
| 3488 | | stack.push({ |
| 3489 | | declNames: declNames, |
| 3490 | | declIndexes: declIndexes, |
| 3491 | | type: value, |
| 3492 | | }); |
| 3493 | | } |
| 3494 | 3253 | |
| 3495 | | // Generic function |
| 3496 | | if (typeIsGenericFn(declVal.expr.type)) { |
| 3497 | | let ret = resolveGenericRet(value); |
| 3498 | | if (ret != null && "type" in ret.expr) { |
| 3499 | | let generic_type = getType(ret.expr.type); |
| 3500 | | if (isContainerType(generic_type)) { |
| 3501 | | stack.push({ |
| 3502 | | declNames: declNames, |
| 3503 | | declIndexes: declIndexes, |
| 3504 | | type: generic_type, |
| 3505 | | }); |
| 3254 | if ("type" in declVal.expr) { |
| 3255 | let value = getType(declVal.expr.type); |
| 3256 | if (declCanRepresentTypeKind(value.kind)) { |
| 3257 | canonTypeDecls[declVal.type] = declIndex; |
| 3258 | } |
| 3259 | |
| 3260 | if (isContainerType(value)) { |
| 3261 | stack.push({ |
| 3262 | declNames: declNames, |
| 3263 | declIndexes: declIndexes, |
| 3264 | type: value, |
| 3265 | }); |
| 3266 | } |
| 3267 | |
| 3268 | // Generic function |
| 3269 | if (typeIsGenericFn(declVal.expr.type)) { |
| 3270 | let ret = resolveGenericRet(value); |
| 3271 | if (ret != null && "type" in ret.expr) { |
| 3272 | let generic_type = getType(ret.expr.type); |
| 3273 | if (isContainerType(generic_type)) { |
| 3274 | stack.push({ |
| 3275 | declNames: declNames, |
| 3276 | declIndexes: declIndexes, |
| 3277 | type: generic_type, |
| 3278 | }); |
| 3279 | } |
| 3506 | 3280 | } |
| 3507 | 3281 | } |
| 3508 | 3282 | } |
| 3509 | 3283 | } |
| 3510 | | } |
| 3511 | 3284 | |
| 3512 | 3285 | function getCanonDeclPath(index) { |
| 3513 | 3286 | if (canonDeclPaths == null) { |
| ... | ... | @@ -3524,7 +3297,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) { |
| 3524 | 3297 | } |
| 3525 | 3298 | |
| 3526 | 3299 | function escapeHtml(text) { |
| 3527 | | return text.replace(/[&"<>]/g, function (m) { |
| 3300 | return text.replace(/[&"<>]/g, function(m) { |
| 3528 | 3301 | return escapeHtmlReplacements[m]; |
| 3529 | 3302 | }); |
| 3530 | 3303 | } |
| ... | ... | @@ -3553,13 +3326,13 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) { |
| 3553 | 3326 | } |
| 3554 | 3327 | |
| 3555 | 3328 | function parseGuides() { |
| 3556 | | for (let j = 0; j < zigAnalysis.guide_sections.length; j+=1){ |
| 3329 | for (let j = 0; j < zigAnalysis.guide_sections.length; j += 1) { |
| 3557 | 3330 | const section = zigAnalysis.guide_sections[j]; |
| 3558 | | for (let i = 0; i < section.guides.length; i+=1){ |
| 3559 | | let reader = new commonmark.Parser({smart: true}); |
| 3331 | for (let i = 0; i < section.guides.length; i += 1) { |
| 3332 | let reader = new commonmark.Parser({ smart: true }); |
| 3560 | 3333 | const guide = section.guides[i]; |
| 3561 | | const ast = reader.parse(guide.body); |
| 3562 | | |
| 3334 | const ast = reader.parse(guide.body); |
| 3335 | |
| 3563 | 3336 | // Find the first text thing to use as a sidebar title |
| 3564 | 3337 | guide.title = "[empty guide]"; |
| 3565 | 3338 | { |
| ... | ... | @@ -3571,7 +3344,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) { |
| 3571 | 3344 | guide.title = node.literal; |
| 3572 | 3345 | break; |
| 3573 | 3346 | } |
| 3574 | | } |
| 3347 | } |
| 3575 | 3348 | } |
| 3576 | 3349 | // Index this guide |
| 3577 | 3350 | { |
| ... | ... | @@ -3580,24 +3353,24 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) { |
| 3580 | 3353 | while ((event = walker.next())) { |
| 3581 | 3354 | node = event.node; |
| 3582 | 3355 | if (event.entering == true && node.type === 'text') { |
| 3583 | | indexTextForGuide(j, i, node); |
| 3356 | indexTextForGuide(j, i, node); |
| 3584 | 3357 | } |
| 3585 | | } |
| 3358 | } |
| 3586 | 3359 | } |
| 3587 | | } |
| 3360 | } |
| 3588 | 3361 | } |
| 3589 | 3362 | } |
| 3590 | 3363 | |
| 3591 | | function indexTextForGuide(section_idx, guide_idx, node){ |
| 3364 | function indexTextForGuide(section_idx, guide_idx, node) { |
| 3592 | 3365 | const terms = node.literal.split(" "); |
| 3593 | | for (let i = 0; i < terms.length; i += 1){ |
| 3366 | for (let i = 0; i < terms.length; i += 1) { |
| 3594 | 3367 | const t = terms[i]; |
| 3595 | 3368 | if (!guidesSearchIndex[t]) guidesSearchIndex[t] = new Set(); |
| 3596 | | node.guide = {section_idx, guide_idx}; |
| 3369 | node.guide = { section_idx, guide_idx }; |
| 3597 | 3370 | guidesSearchIndex[t].add(node); |
| 3598 | | } |
| 3371 | } |
| 3599 | 3372 | } |
| 3600 | | |
| 3373 | |
| 3601 | 3374 | |
| 3602 | 3375 | function markdown(input, contextType) { |
| 3603 | 3376 | const parsed = new commonmark.Parser({ smart: true }).parse(input); |
| ... | ... | @@ -3620,70 +3393,71 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) { |
| 3620 | 3393 | |
| 3621 | 3394 | return new commonmark.HtmlRenderer({ safe: true }).render(parsed); |
| 3622 | 3395 | |
| 3623 | | function detectDeclPath(text, context) { |
| 3624 | | let result = ""; |
| 3625 | | let separator = ":"; |
| 3626 | | const components = text.split("."); |
| 3627 | | let curDeclOrType = undefined; |
| 3628 | | |
| 3629 | | let curContext = context; |
| 3630 | | let limit = 10000; |
| 3631 | | while (curContext) { |
| 3632 | | limit -= 1; |
| 3633 | | |
| 3634 | | if (limit == 0) { |
| 3635 | | throw "too many iterations"; |
| 3636 | | } |
| 3637 | | |
| 3638 | | curDeclOrType = findSubDecl(curContext, components[0]); |
| 3639 | | |
| 3640 | | if (!curDeclOrType) { |
| 3641 | | if (curContext.parent_container == null) break; |
| 3642 | | curContext = getType(curContext.parent_container); |
| 3643 | | continue; |
| 3644 | | } |
| 3396 | } |
| 3645 | 3397 | |
| 3646 | | if (curContext == context) { |
| 3647 | | separator = '.'; |
| 3648 | | result = location.hash + separator + components[0]; |
| 3649 | | } else { |
| 3650 | | // We had to go up, which means we need a new path! |
| 3651 | | const canonPath = getCanonDeclPath(curDeclOrType.find_subdecl_idx); |
| 3652 | | if (!canonPath) return; |
| 3653 | | |
| 3654 | | let lastModName = canonPath.modNames[canonPath.modNames.length - 1]; |
| 3655 | | let fullPath = lastModName + ":" + canonPath.declNames.join("."); |
| 3656 | | |
| 3657 | | separator = '.'; |
| 3658 | | result = "#A;" + fullPath; |
| 3659 | | } |
| 3398 | function detectDeclPath(text, context) { |
| 3399 | let result = ""; |
| 3400 | let separator = ":"; |
| 3401 | const components = text.split("."); |
| 3402 | let curDeclOrType = undefined; |
| 3660 | 3403 | |
| 3661 | | break; |
| 3662 | | } |
| 3404 | let curContext = context; |
| 3405 | let limit = 10000; |
| 3406 | while (curContext) { |
| 3407 | limit -= 1; |
| 3408 | |
| 3409 | if (limit == 0) { |
| 3410 | throw "too many iterations"; |
| 3411 | } |
| 3412 | |
| 3413 | curDeclOrType = findSubDecl(curContext, components[0]); |
| 3663 | 3414 | |
| 3664 | 3415 | if (!curDeclOrType) { |
| 3665 | | for (let i = 0; i < zigAnalysis.modules.length; i += 1){ |
| 3666 | | const p = zigAnalysis.modules[i]; |
| 3667 | | if (p.name == components[0]) { |
| 3668 | | curDeclOrType = getType(p.main); |
| 3669 | | result += "#A;" + components[0]; |
| 3670 | | break; |
| 3671 | | } |
| 3672 | | } |
| 3416 | if (curContext.parent_container == null) break; |
| 3417 | curContext = getType(curContext.parent_container); |
| 3418 | continue; |
| 3673 | 3419 | } |
| 3674 | 3420 | |
| 3675 | | if (!curDeclOrType) return null; |
| 3676 | | |
| 3677 | | for (let i = 1; i < components.length; i += 1) { |
| 3678 | | curDeclOrType = findSubDecl(curDeclOrType, components[i]); |
| 3679 | | if (!curDeclOrType) return null; |
| 3680 | | result += separator + components[i]; |
| 3421 | if (curContext == context) { |
| 3422 | separator = '.'; |
| 3423 | result = location.hash + separator + components[0]; |
| 3424 | } else { |
| 3425 | // We had to go up, which means we need a new path! |
| 3426 | const canonPath = getCanonDeclPath(curDeclOrType.find_subdecl_idx); |
| 3427 | if (!canonPath) return; |
| 3428 | |
| 3429 | let lastModName = canonPath.modNames[canonPath.modNames.length - 1]; |
| 3430 | let fullPath = lastModName + ":" + canonPath.declNames.join("."); |
| 3431 | |
| 3681 | 3432 | separator = '.'; |
| 3433 | result = "#A;" + fullPath; |
| 3682 | 3434 | } |
| 3683 | 3435 | |
| 3684 | | return result; |
| 3685 | | |
| 3436 | break; |
| 3437 | } |
| 3438 | |
| 3439 | if (!curDeclOrType) { |
| 3440 | for (let i = 0; i < zigAnalysis.modules.length; i += 1) { |
| 3441 | const p = zigAnalysis.modules[i]; |
| 3442 | if (p.name == components[0]) { |
| 3443 | curDeclOrType = getType(p.main); |
| 3444 | result += "#A;" + components[0]; |
| 3445 | break; |
| 3446 | } |
| 3447 | } |
| 3448 | } |
| 3449 | |
| 3450 | if (!curDeclOrType) return null; |
| 3451 | |
| 3452 | for (let i = 1; i < components.length; i += 1) { |
| 3453 | curDeclOrType = findSubDecl(curDeclOrType, components[i]); |
| 3454 | if (!curDeclOrType) return null; |
| 3455 | result += separator + components[i]; |
| 3456 | separator = '.'; |
| 3686 | 3457 | } |
| 3458 | |
| 3459 | return result; |
| 3460 | |
| 3687 | 3461 | } |
| 3688 | 3462 | |
| 3689 | 3463 | function activateSelectedResult() { |
| ... | ... | @@ -3723,7 +3497,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) { |
| 3723 | 3497 | startSearch(); |
| 3724 | 3498 | } |
| 3725 | 3499 | } |
| 3726 | | |
| 3500 | |
| 3727 | 3501 | |
| 3728 | 3502 | function onSearchKeyDown(ev) { |
| 3729 | 3503 | switch (getKeyString(ev)) { |
| ... | ... | @@ -3824,7 +3598,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) { |
| 3824 | 3598 | break; |
| 3825 | 3599 | case "/": |
| 3826 | 3600 | if (!getPrefSlashSearch()) break; |
| 3827 | | // fallthrough |
| 3601 | // fallthrough |
| 3828 | 3602 | case "s": |
| 3829 | 3603 | if (!isModalVisible(domHelpModal) && !isModalVisible(domPrefsModal)) { |
| 3830 | 3604 | if (ev.target == domSearch) break; |
| ... | ... | @@ -3846,9 +3620,9 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) { |
| 3846 | 3620 | |
| 3847 | 3621 | // toggle the help modal |
| 3848 | 3622 | if (isModalVisible(domHelpModal)) { |
| 3849 | | hideModal(domHelpModal); |
| 3623 | hideModal(domHelpModal); |
| 3850 | 3624 | } else { |
| 3851 | | showModal(domHelpModal); |
| 3625 | showModal(domHelpModal); |
| 3852 | 3626 | } |
| 3853 | 3627 | ev.preventDefault(); |
| 3854 | 3628 | ev.stopPropagation(); |
| ... | ... | @@ -3927,21 +3701,21 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) { |
| 3927 | 3701 | function renderSearchGuides() { |
| 3928 | 3702 | const searchTrimmed = false; |
| 3929 | 3703 | let ignoreCase = curNavSearch.toLowerCase() === curNavSearch; |
| 3930 | | |
| 3704 | |
| 3931 | 3705 | let terms = getSearchTerms(); |
| 3932 | 3706 | let matchedItems = new Set(); |
| 3933 | 3707 | |
| 3934 | 3708 | for (let i = 0; i < terms.length; i += 1) { |
| 3935 | 3709 | const nodes = guidesSearchIndex[terms[i]]; |
| 3936 | 3710 | if (nodes) { |
| 3937 | | for (const n of nodes) { |
| 3711 | for (const n of nodes) { |
| 3938 | 3712 | matchedItems.add(n); |
| 3939 | 3713 | } |
| 3940 | 3714 | } |
| 3941 | 3715 | } |
| 3942 | 3716 | |
| 3943 | | |
| 3944 | | |
| 3717 | |
| 3718 | |
| 3945 | 3719 | if (matchedItems.size !== 0) { |
| 3946 | 3720 | // Build up the list of search results |
| 3947 | 3721 | let matchedItemsHTML = ""; |
| ... | ... | @@ -3966,14 +3740,14 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) { |
| 3966 | 3740 | } |
| 3967 | 3741 | } |
| 3968 | 3742 | |
| 3969 | | function renderSearchAPI(){ |
| 3743 | function renderSearchAPI() { |
| 3970 | 3744 | if (canonDeclPaths == null) { |
| 3971 | 3745 | canonDeclPaths = computeCanonDeclPaths(); |
| 3972 | 3746 | } |
| 3973 | 3747 | let declSet = new Set(); |
| 3974 | 3748 | let otherDeclSet = new Set(); // for low quality results |
| 3975 | 3749 | let declScores = {}; |
| 3976 | | |
| 3750 | |
| 3977 | 3751 | let ignoreCase = curNavSearch.toLowerCase() === curNavSearch; |
| 3978 | 3752 | let term_list = getSearchTerms(); |
| 3979 | 3753 | for (let i = 0; i < term_list.length; i += 1) { |
| ... | ... | @@ -4004,7 +3778,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) { |
| 4004 | 3778 | if (declSet.has(p)) { |
| 4005 | 3779 | found = true; |
| 4006 | 3780 | break; |
| 4007 | | } |
| 3781 | } |
| 4008 | 3782 | } |
| 4009 | 3783 | if (!found) { |
| 4010 | 3784 | otherDeclSet.add(d); |
| ... | ... | @@ -4012,9 +3786,9 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) { |
| 4012 | 3786 | termSet.add(d); |
| 4013 | 3787 | } |
| 4014 | 3788 | } |
| 4015 | | |
| 3789 | |
| 4016 | 3790 | if (declScores[d] == undefined) declScores[d] = 0; |
| 4017 | | |
| 3791 | |
| 4018 | 3792 | // scores (lower is better) |
| 4019 | 3793 | let decl_name = decl.name; |
| 4020 | 3794 | if (ignoreCase) decl_name = decl_name.toLowerCase(); |
| ... | ... | @@ -4022,21 +3796,21 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) { |
| 4022 | 3796 | // shallow path are preferable |
| 4023 | 3797 | const path_depth = canonPath.declNames.length * 50; |
| 4024 | 3798 | // matching the start of a decl name is good |
| 4025 | | const match_from_start = decl_name.startsWith(term) ? -term.length * (2 -ignoreCase) : (decl_name.length - term.length) + 1; |
| 3799 | const match_from_start = decl_name.startsWith(term) ? -term.length * (2 - ignoreCase) : (decl_name.length - term.length) + 1; |
| 4026 | 3800 | // being a perfect match is good |
| 4027 | 3801 | const is_full_match = (decl_name === term) ? -decl_name.length * (1 - ignoreCase) : Math.abs(decl_name.length - term.length); |
| 4028 | 3802 | // matching the end of a decl name is good |
| 4029 | 3803 | const matches_the_end = decl_name.endsWith(term) ? -term.length * (1 - ignoreCase) : (decl_name.length - term.length) + 1; |
| 4030 | 3804 | // explicitly penalizing scream case decls |
| 4031 | | const decl_is_scream_case = decl.name.toUpperCase() != decl.name ? 0 : decl.name.length; |
| 4032 | | |
| 4033 | | const score = path_depth |
| 4034 | | + match_from_start |
| 4035 | | + is_full_match |
| 4036 | | + matches_the_end |
| 3805 | const decl_is_scream_case = decl.name.toUpperCase() != decl.name ? 0 : decl.name.length; |
| 3806 | |
| 3807 | const score = path_depth |
| 3808 | + match_from_start |
| 3809 | + is_full_match |
| 3810 | + matches_the_end |
| 4037 | 3811 | + decl_is_scream_case; |
| 4038 | 3812 | |
| 4039 | | declScores[d] += score; |
| 3813 | declScores[d] += score; |
| 4040 | 3814 | } |
| 4041 | 3815 | } |
| 4042 | 3816 | if (i != 0) { |
| ... | ... | @@ -4047,19 +3821,19 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) { |
| 4047 | 3821 | if (termSet.has(p) || otherDeclSet.has(p)) { |
| 4048 | 3822 | found = true; |
| 4049 | 3823 | break; |
| 4050 | | } |
| 3824 | } |
| 4051 | 3825 | } |
| 4052 | 3826 | if (found) { |
| 4053 | 3827 | declScores[d] = declScores[d] / term_list.length; |
| 4054 | 3828 | } |
| 4055 | | |
| 3829 | |
| 4056 | 3830 | termOtherSet.add(d); |
| 4057 | 3831 | } |
| 4058 | 3832 | declSet = termSet; |
| 4059 | 3833 | for (let d of termOtherSet) { |
| 4060 | 3834 | otherDeclSet.add(d); |
| 4061 | 3835 | } |
| 4062 | | |
| 3836 | |
| 4063 | 3837 | } |
| 4064 | 3838 | } |
| 4065 | 3839 | |
| ... | ... | @@ -4068,21 +3842,21 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) { |
| 4068 | 3842 | low_quality: [], |
| 4069 | 3843 | }; |
| 4070 | 3844 | for (let idx of declSet) { |
| 4071 | | matchedItems.high_quality.push({points: declScores[idx], declIndex: idx}) |
| 3845 | matchedItems.high_quality.push({ points: declScores[idx], declIndex: idx }) |
| 4072 | 3846 | } |
| 4073 | 3847 | for (let idx of otherDeclSet) { |
| 4074 | | matchedItems.low_quality.push({points: declScores[idx], declIndex: idx}) |
| 3848 | matchedItems.low_quality.push({ points: declScores[idx], declIndex: idx }) |
| 4075 | 3849 | } |
| 4076 | | |
| 4077 | | matchedItems.high_quality.sort(function (a, b) { |
| 3850 | |
| 3851 | matchedItems.high_quality.sort(function(a, b) { |
| 4078 | 3852 | let cmp = operatorCompare(a.points, b.points); |
| 4079 | 3853 | return cmp; |
| 4080 | 3854 | }); |
| 4081 | | matchedItems.low_quality.sort(function (a, b) { |
| 3855 | matchedItems.low_quality.sort(function(a, b) { |
| 4082 | 3856 | let cmp = operatorCompare(a.points, b.points); |
| 4083 | 3857 | return cmp; |
| 4084 | 3858 | }); |
| 4085 | | |
| 3859 | |
| 4086 | 3860 | // Build up the list of search results |
| 4087 | 3861 | let matchedItemsHTML = ""; |
| 4088 | 3862 | |
| ... | ... | @@ -4099,7 +3873,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) { |
| 4099 | 3873 | |
| 4100 | 3874 | let lastModName = canonPath.modNames[canonPath.modNames.length - 1]; |
| 4101 | 3875 | let text = lastModName + "." + canonPath.declNames.join("."); |
| 4102 | | |
| 3876 | |
| 4103 | 3877 | |
| 4104 | 3878 | const href = navLink(canonPath.modNames, canonPath.declNames); |
| 4105 | 3879 | |
| ... | ... | @@ -4113,7 +3887,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) { |
| 4113 | 3887 | |
| 4114 | 3888 | domSectSearchResults.classList.remove("hidden"); |
| 4115 | 3889 | } |
| 4116 | | |
| 3890 | |
| 4117 | 3891 | function renderSearchAPIOld() { |
| 4118 | 3892 | let matchedItems = []; |
| 4119 | 3893 | let ignoreCase = curNavSearch.toLowerCase() === curNavSearch; |
| ... | ... | @@ -4179,7 +3953,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) { |
| 4179 | 3953 | } |
| 4180 | 3954 | |
| 4181 | 3955 | if (matchedItems.length !== 0) { |
| 4182 | | matchedItems.sort(function (a, b) { |
| 3956 | matchedItems.sort(function(a, b) { |
| 4183 | 3957 | let cmp = operatorCompare(b.points, a.points); |
| 4184 | 3958 | if (cmp != 0) return cmp; |
| 4185 | 3959 | return operatorCompare(a.decl.name, b.decl.name); |
| ... | ... | @@ -4485,12 +4259,12 @@ function toggleExpand(event) { |
| 4485 | 4259 | function RadixTree() { |
| 4486 | 4260 | this.root = null; |
| 4487 | 4261 | |
| 4488 | | RadixTree.prototype.search = function (query) { |
| 4262 | RadixTree.prototype.search = function(query) { |
| 4489 | 4263 | return this.root.search(query); |
| 4490 | | |
| 4264 | |
| 4491 | 4265 | } |
| 4492 | 4266 | |
| 4493 | | RadixTree.prototype.add = function (declName, value) { |
| 4267 | RadixTree.prototype.add = function(declName, value) { |
| 4494 | 4268 | if (this.root == null) { |
| 4495 | 4269 | this.root = new Node(declName.toLowerCase(), null, [value]); |
| 4496 | 4270 | } else { |
| ... | ... | @@ -4499,7 +4273,7 @@ function RadixTree() { |
| 4499 | 4273 | |
| 4500 | 4274 | const not_scream_case = declName.toUpperCase() != declName; |
| 4501 | 4275 | let found_separator = false; |
| 4502 | | for (let i = 1; i < declName.length; i +=1) { |
| 4276 | for (let i = 1; i < declName.length; i += 1) { |
| 4503 | 4277 | if (declName[i] == '_' || declName[i] == '.') { |
| 4504 | 4278 | found_separator = true; |
| 4505 | 4279 | continue; |
| ... | ... | @@ -4507,42 +4281,42 @@ function RadixTree() { |
| 4507 | 4281 | |
| 4508 | 4282 | |
| 4509 | 4283 | if (found_separator || (declName[i].toLowerCase() !== declName[i])) { |
| 4510 | | if (declName.length > i+1 |
| 4511 | | && declName[i+1].toLowerCase() != declName[i+1]) continue; |
| 4284 | if (declName.length > i + 1 |
| 4285 | && declName[i + 1].toLowerCase() != declName[i + 1]) continue; |
| 4512 | 4286 | let suffix = declName.slice(i); |
| 4513 | 4287 | this.root.add(suffix.toLowerCase(), value); |
| 4514 | 4288 | found_separator = false; |
| 4515 | 4289 | } |
| 4516 | 4290 | } |
| 4517 | 4291 | } |
| 4518 | | |
| 4292 | |
| 4519 | 4293 | function Node(labels, next, values) { |
| 4520 | | this.labels = labels; |
| 4294 | this.labels = labels; |
| 4521 | 4295 | this.next = next; |
| 4522 | 4296 | this.values = values; |
| 4523 | 4297 | } |
| 4524 | 4298 | |
| 4525 | | Node.prototype.isCompressed = function () { |
| 4299 | Node.prototype.isCompressed = function() { |
| 4526 | 4300 | return !Array.isArray(this.next); |
| 4527 | 4301 | } |
| 4528 | 4302 | |
| 4529 | | Node.prototype.search = function (word) { |
| 4303 | Node.prototype.search = function(word) { |
| 4530 | 4304 | let full_matches = []; |
| 4531 | 4305 | let partial_matches = []; |
| 4532 | 4306 | let subtree_root = null; |
| 4533 | | |
| 4307 | |
| 4534 | 4308 | let cn = this; |
| 4535 | 4309 | char_loop: for (let i = 0; i < word.length;) { |
| 4536 | 4310 | if (cn.isCompressed()) { |
| 4537 | 4311 | for (let j = 0; j < cn.labels.length; j += 1) { |
| 4538 | | let current_idx = i+j; |
| 4312 | let current_idx = i + j; |
| 4539 | 4313 | |
| 4540 | 4314 | if (current_idx == word.length) { |
| 4541 | 4315 | partial_matches = cn.values; |
| 4542 | 4316 | subtree_root = cn.next; |
| 4543 | | break char_loop; |
| 4317 | break char_loop; |
| 4544 | 4318 | } |
| 4545 | | |
| 4319 | |
| 4546 | 4320 | if (word[current_idx] != cn.labels[j]) return null; |
| 4547 | 4321 | } |
| 4548 | 4322 | |
| ... | ... | @@ -4553,33 +4327,33 @@ function RadixTree() { |
| 4553 | 4327 | subtree_root = cn.next; |
| 4554 | 4328 | break char_loop; |
| 4555 | 4329 | } |
| 4556 | | |
| 4557 | | |
| 4330 | |
| 4331 | |
| 4558 | 4332 | i = new_idx; |
| 4559 | 4333 | cn = cn.next; |
| 4560 | 4334 | continue; |
| 4561 | 4335 | } else { |
| 4562 | 4336 | for (let j = 0; j < cn.labels.length; j += 1) { |
| 4563 | 4337 | if (word[i] == cn.labels[j]) { |
| 4564 | | if (i == word.length - 1) { |
| 4338 | if (i == word.length - 1) { |
| 4565 | 4339 | full_matches = cn.values[j]; |
| 4566 | 4340 | subtree_root = cn.next[j]; |
| 4567 | 4341 | break char_loop; |
| 4568 | 4342 | } |
| 4569 | | |
| 4343 | |
| 4570 | 4344 | let next = cn.next[j]; |
| 4571 | 4345 | if (next == null) return null; |
| 4572 | 4346 | cn = next; |
| 4573 | 4347 | i += 1; |
| 4574 | | continue char_loop; |
| 4575 | | } |
| 4348 | continue char_loop; |
| 4349 | } |
| 4576 | 4350 | } |
| 4577 | | |
| 4351 | |
| 4578 | 4352 | // didn't find a match |
| 4579 | 4353 | return null; |
| 4580 | 4354 | } |
| 4581 | 4355 | } |
| 4582 | | |
| 4356 | |
| 4583 | 4357 | // Match was found, let's collect all other |
| 4584 | 4358 | // partial matches from the subtree |
| 4585 | 4359 | let stack = [subtree_root]; |
| ... | ... | @@ -4594,22 +4368,22 @@ function RadixTree() { |
| 4594 | 4368 | for (let v of node.values) { |
| 4595 | 4369 | partial_matches = partial_matches.concat(v); |
| 4596 | 4370 | } |
| 4597 | | |
| 4371 | |
| 4598 | 4372 | for (let n of node.next) { |
| 4599 | 4373 | if (n != null) stack.push(n); |
| 4600 | 4374 | } |
| 4601 | 4375 | } |
| 4602 | 4376 | } |
| 4603 | 4377 | |
| 4604 | | return {full: full_matches, partial: partial_matches}; |
| 4378 | return { full: full_matches, partial: partial_matches }; |
| 4605 | 4379 | } |
| 4606 | 4380 | |
| 4607 | | Node.prototype.add = function (word, value) { |
| 4381 | Node.prototype.add = function(word, value) { |
| 4608 | 4382 | let cn = this; |
| 4609 | 4383 | char_loop: for (let i = 0; i < word.length;) { |
| 4610 | 4384 | if (cn.isCompressed()) { |
| 4611 | | for(let j = 0; j < cn.labels.length; j += 1) { |
| 4612 | | let current_idx = i+j; |
| 4385 | for (let j = 0; j < cn.labels.length; j += 1) { |
| 4386 | let current_idx = i + j; |
| 4613 | 4387 | |
| 4614 | 4388 | if (current_idx == word.length) { |
| 4615 | 4389 | if (j < cn.labels.length - 1) { |
| ... | ... | @@ -4621,13 +4395,13 @@ function RadixTree() { |
| 4621 | 4395 | cn.values.push(value); |
| 4622 | 4396 | return; |
| 4623 | 4397 | } |
| 4624 | | |
| 4398 | |
| 4625 | 4399 | if (word[current_idx] == cn.labels[j]) continue; |
| 4626 | | |
| 4400 | |
| 4627 | 4401 | // if we're here, a mismatch was found |
| 4628 | 4402 | if (j != cn.labels.length - 1) { |
| 4629 | 4403 | // create a suffix node |
| 4630 | | const label_suffix = cn.labels.slice(j+1); |
| 4404 | const label_suffix = cn.labels.slice(j + 1); |
| 4631 | 4405 | let node = new Node(label_suffix, cn.next, [...cn.values]); |
| 4632 | 4406 | cn.next = node; |
| 4633 | 4407 | cn.values = []; |
| ... | ... | @@ -4641,11 +4415,11 @@ function RadixTree() { |
| 4641 | 4415 | // meaning that the current node should hold its value |
| 4642 | 4416 | word_values.push(value); |
| 4643 | 4417 | } else { |
| 4644 | | node = new Node(word.slice(current_idx+1), null, [value]); |
| 4418 | node = new Node(word.slice(current_idx + 1), null, [value]); |
| 4645 | 4419 | } |
| 4646 | | |
| 4420 | |
| 4647 | 4421 | cn.labels = cn.labels[j] + word[current_idx]; |
| 4648 | | cn.next = [cn.next, node]; |
| 4422 | cn.next = [cn.next, node]; |
| 4649 | 4423 | cn.values = [cn.values, word_values]; |
| 4650 | 4424 | |
| 4651 | 4425 | if (j != 0) { |
| ... | ... | @@ -4657,7 +4431,7 @@ function RadixTree() { |
| 4657 | 4431 | } |
| 4658 | 4432 | |
| 4659 | 4433 | return; |
| 4660 | | } |
| 4434 | } |
| 4661 | 4435 | // label matched fully with word, are there any more chars? |
| 4662 | 4436 | const new_idx = i + cn.labels.length; |
| 4663 | 4437 | if (new_idx == word.length) { |
| ... | ... | @@ -4673,7 +4447,7 @@ function RadixTree() { |
| 4673 | 4447 | i = new_idx; |
| 4674 | 4448 | continue; |
| 4675 | 4449 | } |
| 4676 | | } |
| 4450 | } |
| 4677 | 4451 | } else { // node is not compressed |
| 4678 | 4452 | let letter = word[i]; |
| 4679 | 4453 | for (let j = 0; j < cn.labels.length; j += 1) { |
| ... | ... | @@ -4683,7 +4457,7 @@ function RadixTree() { |
| 4683 | 4457 | return; |
| 4684 | 4458 | } |
| 4685 | 4459 | if (cn.next[j] == null) { |
| 4686 | | let node = new Node(word.slice(i+1), null, [value]); |
| 4460 | let node = new Node(word.slice(i + 1), null, [value]); |
| 4687 | 4461 | cn.next[j] = node; |
| 4688 | 4462 | return; |
| 4689 | 4463 | } else { |
| ... | ... | @@ -4700,7 +4474,7 @@ function RadixTree() { |
| 4700 | 4474 | cn.next.push(null); |
| 4701 | 4475 | cn.values.push([value]); |
| 4702 | 4476 | } else { |
| 4703 | | let node = new Node(word.slice(i+1), null, [value]); |
| 4477 | let node = new Node(word.slice(i + 1), null, [value]); |
| 4704 | 4478 | cn.next.push(node); |
| 4705 | 4479 | cn.values.push([]); |
| 4706 | 4480 | } |