authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2023-07-14 16:27:09+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-07-14 16:27:09+02:00
loga187141056687231c7fd2c333280e6fc20062d11
tree6ebfe705527d02500129628d3998cc0d9310fa16
parent3022c525ec87c391842ec339916037e12b3a7b5c
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Autodoc tokenizer (#16409)

* autodoc: init work to refactor exprName * autodoc: Implement more expressions in exprName refactor * autodoc: more work * autodoc: More exprName to ex refactoring * autodoc: Remove whitespace flag from renderer; Add pre tags in value and variable drawing in renderContainer * autodoc: add inline styling to pre blocks * autodoc: move renderer code to main.js * autodoc: More exprName to ex refactoring; Fn signatures rendered with new code * autodoc: Fix function rendering. Add more things to ex * autodoc: nuke exprName --------- Co-authored-by: Krzysztof Wolicki <der.teufel.mail@gmail.com>

4 files changed, 1376 insertions(+), 1542 deletions(-)

lib/docs/index.html+34-10
...@@ -297,6 +297,13 @@...@@ -297,6 +297,13 @@
297 overflow-x: auto;297 overflow-x: auto;
298 }298 }
299299
300 .docs pre.inline {
301 background-color: var(--bg-color);
302 padding: 0;
303 display: inline;
304 }
305
306
300 .docs code {307 .docs code {
301 font-family: var(--mono);308 font-family: var(--mono);
302 font-size: 1em;309 font-size: 1em;
...@@ -685,15 +692,23 @@...@@ -685,15 +692,23 @@
685 </style>692 </style>
686693
687 <style>694 <style>
688 pre{695 pre {
689 --zig-keyword: #333;696 --zig-keyword: #333;
690 --zig-identifier: 'black'; 697 --zig-builtin: #0086b3;
698 --zig-identifier: 'black';
699 --zig-string-literal: #d14;
700 --zig-type: #458;
701 --zig-fn: #900;
691 }702 }
692 703
693 @media (prefers-color-scheme: dark) { 704 @media (prefers-color-scheme: dark) {
694 pre {705 pre {
695 --zig-keyword: 'white';706 --zig-keyword: #eee;
696 --zig-identifier: 'purple'; 707 --zig-builtin: #ff894c;
708 --zig-identifier: 'purple';
709 --zig-string-literal: #2e5;
710 --zig-type: #68f;
711 --zig-fn: #e33;
697 }712 }
698 }713 }
699 714
...@@ -748,7 +763,7 @@...@@ -748,7 +763,7 @@
748 .zig_keyword_anytype,763 .zig_keyword_anytype,
749 .zig_keyword_fn764 .zig_keyword_fn
750 {765 {
751 color: #333;766 color: var(--zig-keyword);
752 font-weight: bold;767 font-weight: bold;
753 }768 }
754769
...@@ -757,12 +772,12 @@...@@ -757,12 +772,12 @@
757 .zig_multiline_string_literal_line,772 .zig_multiline_string_literal_line,
758 .zig_char_literal773 .zig_char_literal
759 {774 {
760 color: #d14;775 color: var(--zig-string-literal);
761 }776 }
762777
763 .zig_builtin778 .zig_builtin
764 {779 {
765 color: #0086b3;780 color: var(--zig-builtin);
766 }781 }
767782
768 .zig_doc_comment,783 .zig_doc_comment,
...@@ -772,15 +787,24 @@...@@ -772,15 +787,24 @@
772 }787 }
773788
774 .zig_identifier {789 .zig_identifier {
775 color: white;790 color: var(--zig-identifier);
776 font-weight: bold;791 font-weight: bold;
777 }792 }
778793
779 .zig_number_literal {794 .zig_number_literal,
780 color: #008080;795 .zig_special {
796 color: #ff8080;
781 }797 }
782798
799 .zig_type {
800 color: var(--zig-type);
801 font-weight: bold;
802 }
783803
804 .zig_fn {
805 color: var(--zig-fn);
806 font-weight: bold;
807 }
784 808
785 </style>809 </style>
786 </head>810 </head>
lib/docs/main.js+1110-1336
...@@ -7,7 +7,7 @@ const NAV_MODES = {...@@ -7,7 +7,7 @@ const NAV_MODES = {
7 GUIDES: "#G;",7 GUIDES: "#G;",
8};8};
99
10(function () {10(function() {
11 const domBanner = document.getElementById("banner");11 const domBanner = document.getElementById("banner");
12 const domMain = document.getElementById("main");12 const domMain = document.getElementById("main");
13 const domStatus = document.getElementById("status");13 const domStatus = document.getElementById("status");
...@@ -59,7 +59,7 @@ const NAV_MODES = {...@@ -59,7 +59,7 @@ const NAV_MODES = {
59 const domDocs = document.getElementById("docs");59 const domDocs = document.getElementById("docs");
60 const domGuidesSection = document.getElementById("guides");60 const domGuidesSection = document.getElementById("guides");
61 const domActiveGuide = document.getElementById("activeGuide");61 const domActiveGuide = document.getElementById("activeGuide");
62 62
63 const domListSearchResults = document.getElementById("listSearchResults");63 const domListSearchResults = document.getElementById("listSearchResults");
64 const domSectSearchNoResults = document.getElementById("sectSearchNoResults");64 const domSectSearchNoResults = document.getElementById("sectSearchNoResults");
65 const domSectInfo = document.getElementById("sectInfo");65 const domSectInfo = document.getElementById("sectInfo");
...@@ -133,7 +133,7 @@ const NAV_MODES = {...@@ -133,7 +133,7 @@ const NAV_MODES = {
133 // map of decl index to list of comptime fn calls133 // map of decl index to list of comptime fn calls
134 // let nodesToCallsMap = indexNodesToCalls();134 // let nodesToCallsMap = indexNodesToCalls();
135135
136 let guidesSearchIndex = {}; 136 let guidesSearchIndex = {};
137 window.guideSearch = guidesSearchIndex;137 window.guideSearch = guidesSearchIndex;
138 parseGuides();138 parseGuides();
139139
...@@ -248,7 +248,7 @@ const NAV_MODES = {...@@ -248,7 +248,7 @@ const NAV_MODES = {
248 } else if (type.kind === typeKinds.Union) {248 } else if (type.kind === typeKinds.Union) {
249 name = "union";249 name = "union";
250 } else {250 } else {
251 console.log("TODO: unhalndled case in typeShortName");251 console.log("TODO: unhandled case in typeShortName");
252 return null;252 return null;
253 }253 }
254254
...@@ -313,8 +313,8 @@ const NAV_MODES = {...@@ -313,8 +313,8 @@ const NAV_MODES = {
313313
314 function resolveGenericRet(genericFunc) {314 function resolveGenericRet(genericFunc) {
315 if (genericFunc.generic_ret == null) return null;315 if (genericFunc.generic_ret == null) return null;
316 let result = resolveValue({expr: genericFunc.generic_ret});316 let result = resolveValue({ expr: genericFunc.generic_ret });
317 317
318 let i = 0;318 let i = 0;
319 while (true) {319 while (true) {
320 i += 1;320 i += 1;
...@@ -437,32 +437,32 @@ const NAV_MODES = {...@@ -437,32 +437,32 @@ const NAV_MODES = {
437 const section_list = zigAnalysis.guide_sections;437 const section_list = zigAnalysis.guide_sections;
438 resizeDomList(domGuidesList, section_list.length, '<div><h2><span></span></h2><ul class="modules"></ul></div>');438 resizeDomList(domGuidesList, section_list.length, '<div><h2><span></span></h2><ul class="modules"></ul></div>');
439 for (let j = 0; j < section_list.length; j += 1) {439 for (let j = 0; j < section_list.length; j += 1) {
440 const section = section_list[j];440 const section = section_list[j];
441 const domSectionName = domGuidesList.children[j].children[0].children[0];441 const domSectionName = domGuidesList.children[j].children[0].children[0];
442 const domGuides = domGuidesList.children[j].children[1];442 const domGuides = domGuidesList.children[j].children[1];
443 domSectionName.textContent = section.name;443 domSectionName.textContent = section.name;
444 resizeDomList(domGuides, section.guides.length, '<li><a href="#"></a></li>');444 resizeDomList(domGuides, section.guides.length, '<li><a href="#"></a></li>');
445 for (let i = 0; i < section.guides.length; i += 1) {445 for (let i = 0; i < section.guides.length; i += 1) {
446 const guide = section.guides[i];446 const guide = section.guides[i];
447 let liDom = domGuides.children[i];447 let liDom = domGuides.children[i];
448 let aDom = liDom.children[0];448 let aDom = liDom.children[0];
449 aDom.textContent = guide.title;449 aDom.textContent = guide.title;
450 aDom.setAttribute("href", NAV_MODES.GUIDES + guide.name);450 aDom.setAttribute("href", NAV_MODES.GUIDES + guide.name);
451 if (guide.name === curNav.activeGuide) {451 if (guide.name === curNav.activeGuide) {
452 aDom.classList.add("active");452 aDom.classList.add("active");
453 } else {453 } else {
454 aDom.classList.remove("active");454 aDom.classList.remove("active");
455 }
456 }455 }
456 }
457 }457 }
458 458
459 if (section_list.length > 0) {459 if (section_list.length > 0) {
460 domGuidesMenu.classList.remove("hidden");460 domGuidesMenu.classList.remove("hidden");
461 }461 }
462462
463 463
464 if (curNavSearch !== "") {464 if (curNavSearch !== "") {
465 return renderSearchGuides();465 return renderSearchGuides();
466 }466 }
467467
468 // main content468 // main content
...@@ -476,8 +476,8 @@ const NAV_MODES = {...@@ -476,8 +476,8 @@ const NAV_MODES = {
476 break outer;476 break outer;
477 }477 }
478 }478 }
479 } 479 }
480 480
481 if (activeGuide == undefined) {481 if (activeGuide == undefined) {
482 const root_file_idx = zigAnalysis.modules[zigAnalysis.rootMod].file;482 const root_file_idx = zigAnalysis.modules[zigAnalysis.rootMod].file;
483 const root_file_name = getFile(root_file_idx).name;483 const root_file_name = getFile(root_file_idx).name;
...@@ -564,7 +564,7 @@ Happy writing!...@@ -564,7 +564,7 @@ Happy writing!
564 renderModList();564 renderModList();
565565
566 if (curNavSearch !== "") {566 if (curNavSearch !== "") {
567 return renderSearchAPI();567 return renderSearchAPI();
568 }568 }
569569
570 let rootMod = zigAnalysis.modules[zigAnalysis.rootMod];570 let rootMod = zigAnalysis.modules[zigAnalysis.rootMod];
...@@ -660,7 +660,8 @@ Happy writing!...@@ -660,7 +660,8 @@ Happy writing!
660 if (!decl.decltest) return;660 if (!decl.decltest) return;
661 const astNode = getAstNode(decl.decltest);661 const astNode = getAstNode(decl.decltest);
662 domSectDocTests.classList.remove("hidden");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 }
665666
666 function renderUnknownDecl(decl) {667 function renderUnknownDecl(decl) {
...@@ -707,11 +708,7 @@ Happy writing!...@@ -707,11 +708,7 @@ Happy writing!
707 console.assert("type" in value.expr);708 console.assert("type" in value.expr);
708 let typeObj = getType(value.expr.type);709 let typeObj = getType(value.expr.type);
709710
710 domFnProtoCode.innerHTML = exprName(value.expr, {711 domFnProtoCode.innerHTML = renderTokens(ex(value.expr, { fnDecl: fnDecl }));
711 wantHtml: true,
712 wantLink: true,
713 fnDecl,
714 });
715712
716 domFnSourceLink.innerHTML = "[<a target=\"_blank\" href=\"" + sourceFileLink(fnDecl) + "\">src</a>]";713 domFnSourceLink.innerHTML = "[<a target=\"_blank\" href=\"" + sourceFileLink(fnDecl) + "\">src</a>]";
717714
...@@ -840,15 +837,21 @@ Happy writing!...@@ -840,15 +837,21 @@ Happy writing!
840837
841 let value = typeObj.params[i];838 let value = typeObj.params[i];
842 let preClass = docsNonEmpty ? ' class="fieldHasDocs"' : "";839 let preClass = docsNonEmpty ? ' class="fieldHasDocs"' : "";
843 let html = "<pre" + preClass + ">" + escapeHtml(fieldNode.name) + ": ";840 let html = "<pre" + preClass + ">" + renderTokens((function*() {
844 if (isVarArgs && i === typeObj.params.length - 1) {841 yield Tok.identifier(fieldNode.name);
845 html += "...";842 yield Tok.colon;
846 } else {843 yield Tok.space;
847 let name = exprName(value, { wantHtml: false, wantLink: false });844 if (isVarArgs && i === typeObj.params.length - 1) {
848 html += '<span class="tok-kw">' + name + "</span>";845 yield Tok.period;
849 }846 yield Tok.period;
847 yield Tok.period;
848 } else {
849 yield* ex(value, {});
850 }
851 yield Tok.comma;
852 }()));
850853
851 html += ",</pre>";854 html += "</pre>";
852855
853 if (docsNonEmpty) {856 if (docsNonEmpty) {
854 html += '<div class="fieldDocs">' + markdown(docs) + "</div>";857 html += '<div class="fieldDocs">' + markdown(docs) + "</div>";
...@@ -932,7 +935,7 @@ Happy writing!...@@ -932,7 +935,7 @@ Happy writing!
932 domSectMainMod.classList.remove("hidden");935 domSectMainMod.classList.remove("hidden");
933 }936 }
934937
935 list.sort(function (a, b) {938 list.sort(function(a, b) {
936 return operatorCompare(a.name.toLowerCase(), b.name.toLowerCase());939 return operatorCompare(a.name.toLowerCase(), b.name.toLowerCase());
937 });940 });
938941
...@@ -1054,823 +1057,578 @@ Happy writing!...@@ -1054,823 +1057,578 @@ Happy writing!
1054 return walkResultTypeRef(resolved);1057 return walkResultTypeRef(resolved);
1055 }1058 }
10561059
1057 function exprName(expr, opts) {1060 function* DecoratedTokenizer(src, context) {
1058 switch (Object.keys(expr)[0]) {1061 let tok_it = Tokenizer(src);
1059 default:1062 for (let t of tok_it) {
1060 throw "this expression is not implemented yet";1063 if (t.tag == Tag.identifier) {
1061 case "bool": {1064 const link = detectDeclPath(t.src, context);
1062 if (expr.bool) {1065 if (link) {
1063 return "true";1066 t.link = link;
1064 }1067 }
1065 return "false";
1066 }
1067 case "&": {
1068 return "&" + exprName(zigAnalysis.exprs[expr["&"]], opts);
1069 }1068 }
1070 case "compileError": {1069
1071 let compileError = expr.compileError;1070 yield t;
1072 return "@compileError(" + exprName(zigAnalysis.exprs[compileError], opts) + ")";1071 }
1073 }1072 }
1074 case "enumLiteral": {1073
1075 let literal = expr.enumLiteral;1074
1076 return "." + literal;1075 function renderSingleToken(t) {
1077 }1076
1078 case "void": {1077 if (t.tag == Tag.whitespace) {
1079 return "void";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": {1131 case "declName": {
1082 return "unreachable";1132 yield { src: expr.declName, tag: Tag.identifier };
1133 return;
1083 }1134 }
1084 case "slice": {1135 case "declRef": {
1085 let payloadHtml = "";1136 const name = getDecl(expr.declRef).name;
1086 const lhsExpr = zigAnalysis.exprs[expr.slice.lhs];1137 const canonPath = getCanonDeclPath(expr.declRef);
1087 const startExpr = zigAnalysis.exprs[expr.slice.start];1138 if (canonPath) {
1088 let decl = exprName(lhsExpr, opts);1139 const link = navLink(canonPath.modNames, canonPath.declNames);
1089 let start = exprName(startExpr, opts);1140 yield { src: name, tag: Tag.identifier, link };
1090 let end = "";1141 } else {
1091 let sentinel = "";1142 yield { src: name, tag: Tag.identifier };
1092 if (expr.slice["end"]) {
1093 const endExpr = zigAnalysis.exprs[expr.slice.end];
1094 let end_ = exprName(endExpr, opts);
1095 end += end_;
1096 }1143 }
1097 if (expr.slice["sentinel"]) {1144 return;
1098 const sentinelExpr = zigAnalysis.exprs[expr.slice.sentinel];1145 }
1099 let sentinel_ = exprName(sentinelExpr, opts);1146 case "refPath": {
1100 sentinel += " :" + sentinel_;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 + "]";1151 return;
1103 return payloadHtml;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": {1161 case "bool": {
1106 let payloadHtml = "";1162 if (expr.bool) {
1107 const lhsExpr = zigAnalysis.exprs[expr.sliceLength.lhs];1163 yield { src: "true", tag: Tag.identifier };
1108 const startExpr = zigAnalysis.exprs[expr.sliceLength.start];1164 return;
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_;
1118 }1165 }
1119 payloadHtml += decl + "[" + start + "..][0.." + len + sentinel + "]";1166 yield { src: "false", tag: Tag.identifier };
1120 return payloadHtml;1167 return;
1121 }1168 }
1122 case "sliceIndex": {1169 case "&": {
1123 const sliceIndex = zigAnalysis.exprs[expr.sliceIndex];1170 yield { src: "&", tag: Tag.ampersand };
1124 return exprName(sliceIndex, opts, opts);1171 yield* ex(zigAnalysis.exprs[expr["&"]], opts);
1172 return;
1125 }1173 }
1126 case "cmpxchg": {1174 case "call": {
1127 const typeIndex = zigAnalysis.exprs[expr.cmpxchg.type];1175
1128 const ptrIndex = zigAnalysis.exprs[expr.cmpxchg.ptr];1176 let call = zigAnalysis.calls[expr.call];
1129 const expectedValueIndex =1177
1130 zigAnalysis.exprs[expr.cmpxchg.expected_value];1178 switch (Object.keys(call.func)[0]) {
1131 const newValueIndex = zigAnalysis.exprs[expr.cmpxchg.new_value];1179 default:
1132 const successOrderIndex = zigAnalysis.exprs[expr.cmpxchg.success_order];1180 throw "TODO";
1133 const failureOrderIndex = zigAnalysis.exprs[expr.cmpxchg.failure_order];1181 case "declRef":
11341182 case "refPath": {
1135 const type = exprName(typeIndex, opts);1183 yield* ex(call.func, 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";
1151 break;1184 break;
1152 }1185 }
1153 default: {1186 }
1154 console.log("There's only cmpxchg_strong and cmpxchg_weak");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 }
11571196
1158 return (1197 yield Tok.r_paren;
1159 fnName +1198 return;
1160 "(" +
1161 type +
1162 ", " +
1163 ptr +
1164 ", " +
1165 expectedValue +
1166 ", " +
1167 newValue +
1168 ", " +
1169 "." +
1170 successOrder +
1171 ", " +
1172 "." +
1173 failureOrder +
1174 ")"
1175 );
1176 }1199 }
1177 case "cmpxchgIndex": {1200 case "sizeOf": {
1178 const cmpxchgIndex = zigAnalysis.exprs[expr.cmpxchgIndex];1201 const sizeOf = zigAnalysis.exprs[expr.sizeOf];
1179 return exprName(cmpxchgIndex, opts);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": {1208
1182 let condExpr = zigAnalysis.exprs[expr.switchOp.cond_index];1209 case "as": {
1183 let ast = getAstNode(expr.switchOp.src);1210 const exprArg = zigAnalysis.exprs[expr.as.exprArg];
1184 let file_name = expr.switchOp.file_name;1211 yield* ex(exprArg, opts);
1185 let outer_decl_index = expr.switchOp.outer_decl;1212 return;
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;
1223 }1213 }
1224 case "switchIndex": {1214
1225 const switchIndex = zigAnalysis.exprs[expr.switchIndex];1215 case "int": {
1226 return exprName(switchIndex, opts);1216 yield { src: expr.int, tag: Tag.number_literal };
1217 return;
1227 }1218 }
1228 case "fieldRef": {1219
1229 const field_idx = expr.fieldRef.index;1220 case "int_big": {
1230 const type = getType(expr.fieldRef.type);1221 if (expr.int_big.negated) {
1231 const field = getAstNode(type.src).fields[field_idx];1222 yield { src: "-", tag: Tag.minus };
1232 const name = getAstNode(field).name;1223 }
1233 return name;1224 yield { src: expr.int_big.value, tag: Tag.number_literal };
1225 return;
1234 }1226 }
1235 case "intFromEnum": {1227
1236 const intFromEnum = zigAnalysis.exprs[expr.intFromEnum];1228 case "float": {
1237 return "@intFromEnum(" + exprName(intFromEnum, opts) + ")";1229 yield { src: expr.float, tag: Tag.number_literal };
1230 return;
1238 }1231 }
1239 case "bitSizeOf": {1232
1240 const bitSizeOf = zigAnalysis.exprs[expr.bitSizeOf];1233 case "float128": {
1241 return "@bitSizeOf(" + exprName(bitSizeOf, opts) + ")";1234 yield { src: expr.float128, tag: Tag.number_literal };
1235 return;
1242 }1236 }
1243 case "sizeOf": {1237
1244 const sizeOf = zigAnalysis.exprs[expr.sizeOf];1238 case "array": {
1245 return "@sizeOf(" + exprName(sizeOf, opts) + ")";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": {1252
1248 const builtinIndex = zigAnalysis.exprs[expr.builtinIndex];1253 case "compileError": {
1249 return exprName(builtinIndex, opts);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": {1260
1252 const param_expr = zigAnalysis.exprs[expr.builtin.param];1261 case "string": {
1253 let param = exprName(param_expr, opts);1262 yield { src: '"' + expr.string + '"', tag: Tag.string_literal };
12541263 return;
1255 let payloadHtml = "@";1264 }
1256 switch (expr.builtin.name) {1265
1257 case "align_of": {1266 case "struct": {
1258 payloadHtml += "alignOf";1267 yield Tok.period;
1259 break;1268 yield Tok.l_brace;
1260 }1269 yield Tok.space;
1261 case "int_from_bool": {1270
1262 payloadHtml += "intFromBool";1271 for (let i = 0; i < expr.struct.length; i++) {
1263 break;1272 const fv = expr.struct[i];
1264 }1273 const field_name = fv.name;
1265 case "embed_file": {1274 const field_value = ex(fv.val.expr, opts);
1266 payloadHtml += "embedFile";1275 yield Tok.period;
1267 break;1276 yield { src: field_name, tag: Tag.identifier };
1268 }1277 yield Tok.space;
1269 case "error_name": {1278 yield Tok.eql;
1270 payloadHtml += "errorName";1279 yield Tok.space;
1271 break;1280 yield* field_value;
1272 }1281 if (i !== expr.struct.length - 1) {
1273 case "panic": {1282 yield Tok.comma;
1274 payloadHtml += "panic";1283 yield Tok.space;
1275 break;1284 } else {
1276 }1285 yield Tok.space;
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;
1288 }1286 }
1289 case "cos": {1287 }
1290 payloadHtml += "cos";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 break;1315 break;
1292 }1316 }
1293 case "tan": {1317 case "addwrap": {
1294 payloadHtml += "tan";1318 yield { src: "+%", tag: Tag.plus_percent };
1295 break;1319 break;
1296 }1320 }
1297 case "exp": {1321 case "add_sat": {
1298 payloadHtml += "exp";1322 yield { src: "+|", tag: Tag.plus_pipe };
1299 break;1323 break;
1300 }1324 }
1301 case "exp2": {1325 case "sub": {
1302 payloadHtml += "exp2";1326 yield { src: "-", tag: Tag.minus };
1303 break;1327 break;
1304 }1328 }
1305 case "log": {1329 case "subwrap": {
1306 payloadHtml += "log";1330 yield { src: "-%", tag: Tag.minus_percent };
1307 break;1331 break;
1308 }1332 }
1309 case "log2": {1333 case "sub_sat": {
1310 payloadHtml += "log2";1334 yield { src: "-|", tag: Tag.minus_pipe };
1311 break;1335 break;
1312 }1336 }
1313 case "log10": {1337 case "mul": {
1314 payloadHtml += "log10";1338 yield { src: "*", tag: Tag.asterisk };
1315 break;1339 break;
1316 }1340 }
1317 case "fabs": {1341 case "mulwrap": {
1318 payloadHtml += "fabs";1342 yield { src: "*%", tag: Tag.asterisk_percent };
1319 break;1343 break;
1320 }1344 }
1321 case "floor": {1345 case "mul_sat": {
1322 payloadHtml += "floor";1346 yield { src: "*|", tag: Tag.asterisk_pipe };
1323 break;1347 break;
1324 }1348 }
1325 case "ceil": {1349 case "div": {
1326 payloadHtml += "ceil";1350 yield { src: "/", tag: Tag.slash };
1327 break;1351 break;
1328 }1352 }
1329 case "trunc": {1353 case "shl": {
1330 payloadHtml += "trunc";1354 yield { src: "<<", tag: Tag.angle_bracket_angle_bracket_left };
1331 break;1355 break;
1332 }1356 }
1333 case "round": {1357 case "shl_sat": {
1334 payloadHtml += "round";1358 yield { src: "<<|", tag: Tag.angle_bracket_angle_bracket_left_pipe };
1335 break;1359 break;
1336 }1360 }
1337 case "tag_name": {1361 case "shr": {
1338 payloadHtml += "tagName";1362 yield { src: ">>", tag: Tag.angle_bracket_angle_bracket_right };
1339 break;1363 break;
1340 }1364 }
1341 case "reify": {1365 case "bit_or": {
1342 payloadHtml += "Type";1366 yield { src: "|", tag: Tag.pipe };
1343 break;1367 break;
1344 }1368 }
1345 case "type_name": {1369 case "bit_and": {
1346 payloadHtml += "typeName";1370 yield { src: "&", tag: Tag.ampersand };
1347 break;1371 break;
1348 }1372 }
1349 case "frame_type": {1373 case "array_cat": {
1350 payloadHtml += "Frame";1374 yield { src: "++", tag: Tag.plus_plus };
1351 break;1375 break;
1352 }1376 }
1353 case "frame_size": {1377 case "array_mul": {
1354 payloadHtml += "frameSize";1378 yield { src: "**", tag: Tag.asterisk_asterisk };
1355 break;1379 break;
1356 }1380 }
1357 case "work_item_id": {1381 case "cmp_eq": {
1358 payloadHtml += "workItemId";1382 yield { src: "==", tag: Tag.equal_equal };
1359 break;1383 break;
1360 }1384 }
1361 case "work_group_size": {1385 case "cmp_neq": {
1362 payloadHtml += "workGroupSize";1386 yield { src: "!=", tag: Tag.bang_equal };
1363 break;1387 break;
1364 }1388 }
1365 case "work_group_id": {1389 case "cmp_gt": {
1366 payloadHtml += "workGroupId";1390 yield { src: ">", tag: Tag.angle_bracket_right };
1367 break;1391 break;
1368 }1392 }
1369 case "int_from_ptr": {1393 case "cmp_gte": {
1370 payloadHtml += "intFromPtr";1394 yield { src: ">=", tag: Tag.angle_bracket_right_equal };
1371 break;1395 break;
1372 }1396 }
1373 case "int_from_error": {1397 case "cmp_lt": {
1374 payloadHtml += "intFromError";1398 yield { src: "<", tag: Tag.angle_bracket_left };
1375 break;1399 break;
1376 }1400 }
1377 case "error_to_int": {1401 case "cmp_lte": {
1378 payloadHtml += "errorFromInt";1402 yield { src: "<=", tag: Tag.angle_bracket_left_equal };
1379 break;1403 break;
1380 }1404 }
1381 case "max": {1405 case "bool_br_and": {
1382 payloadHtml += "max";1406 yield { src: "and", tag: Tag.keyword_and };
1383 break;1407 break;
1384 }1408 }
1385 case "min": {1409 case "bool_br_or": {
1386 payloadHtml += "min";1410 yield { src: "or", tag: Tag.keyword_or };
1387 break;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 default: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 case "builtinBinIndex": {1429 case "builtinBinIndex": {
1419 const builtinBinIndex = zigAnalysis.exprs[expr.builtinBinIndex];1430 const builtinBinIndex = zigAnalysis.exprs[expr.builtinBinIndex];
1420 return exprName(builtinBinIndex, opts);1431 yield* ex(builtinBinIndex, opts);
1432 return;
1421 }1433 }
1434
1422 case "builtinBin": {1435 case "builtinBin": {
1423 const lhsOp = zigAnalysis.exprs[expr.builtinBin.lhs];1436 const lhsOp = zigAnalysis.exprs[expr.builtinBin.lhs];
1424 const rhsOp = zigAnalysis.exprs[expr.builtinBin.rhs];1437 const rhsOp = zigAnalysis.exprs[expr.builtinBin.rhs];
1425 let lhs = exprName(lhsOp, opts);
1426 let rhs = exprName(rhsOp, opts);
14271438
1428 let payloadHtml = "@";1439 let builtinName = "@";
1429 switch (expr.builtinBin.name) {1440 switch (expr.builtinBin.name) {
1430 case "int_from_float": {1441 case "int_from_float": {
1431 payloadHtml += "intFromFloat";1442 builtinName += "intFromFloat";
1432 break;1443 break;
1433 }1444 }
1434 case "float_from_int": {1445 case "float_from_int": {
1435 payloadHtml += "floatFromInt";1446 builtinName += "floatFromInt";
1436 break;1447 break;
1437 }1448 }
1438 case "ptr_from_int": {1449 case "ptr_from_int": {
1439 payloadHtml += "ptrFromInt";1450 builtinName += "ptrFromInt";
1440 break;1451 break;
1441 }1452 }
1442 case "enum_from_int": {1453 case "enum_from_int": {
1443 payloadHtml += "enumFromInt";1454 builtinName += "enumFromInt";
1444 break;1455 break;
1445 }1456 }
1446 case "float_cast": {1457 case "float_cast": {
1447 payloadHtml += "floatCast";1458 builtinName += "floatCast";
1448 break;1459 break;
1449 }1460 }
1450 case "int_cast": {1461 case "int_cast": {
1451 payloadHtml += "intCast";1462 builtinName += "intCast";
1452 break;1463 break;
1453 }1464 }
1454 case "ptr_cast": {1465 case "ptr_cast": {
1455 payloadHtml += "ptrCast";1466 builtinName += "ptrCast";
1456 break;1467 break;
1457 }1468 }
1458 case "const_cast": {1469 case "const_cast": {
1459 payloadHtml += "constCast";1470 builtinName += "constCast";
1460 break;1471 break;
1461 }1472 }
1462 case "volatile_cast": {1473 case "volatile_cast": {
1463 payloadHtml += "volatileCast";1474 builtinName += "volatileCast";
1464 break;1475 break;
1465 }1476 }
1466 case "truncate": {1477 case "truncate": {
1467 payloadHtml += "truncate";1478 builtinName += "truncate";
1468 break;1479 break;
1469 }1480 }
1470 case "has_decl": {1481 case "has_decl": {
1471 payloadHtml += "hasDecl";1482 builtinName += "hasDecl";
1472 break;1483 break;
1473 }1484 }
1474 case "has_field": {1485 case "has_field": {
1475 payloadHtml += "hasField";1486 builtinName += "hasField";
1476 break;1487 break;
1477 }1488 }
1478 case "bit_reverse": {1489 case "bit_reverse": {
1479 payloadHtml += "bitReverse";1490 builtinName += "bitReverse";
1480 break;1491 break;
1481 }1492 }
1482 case "div_exact": {1493 case "div_exact": {
1483 payloadHtml += "divExact";1494 builtinName += "divExact";
1484 break;1495 break;
1485 }1496 }
1486 case "div_floor": {1497 case "div_floor": {
1487 payloadHtml += "divFloor";1498 builtinName += "divFloor";
1488 break;1499 break;
1489 }1500 }
1490 case "div_trunc": {1501 case "div_trunc": {
1491 payloadHtml += "divTrunc";1502 builtinName += "divTrunc";
1492 break;1503 break;
1493 }1504 }
1494 case "mod": {1505 case "mod": {
1495 payloadHtml += "mod";1506 builtinName += "mod";
1496 break;1507 break;
1497 }1508 }
1498 case "rem": {1509 case "rem": {
1499 payloadHtml += "rem";1510 builtinName += "rem";
1500 break;1511 break;
1501 }1512 }
1502 case "mod_rem": {1513 case "mod_rem": {
1503 payloadHtml += "rem";1514 builtinName += "rem";
1504 break;1515 break;
1505 }1516 }
1506 case "shl_exact": {1517 case "shl_exact": {
1507 payloadHtml += "shlExact";1518 builtinName += "shlExact";
1508 break;1519 break;
1509 }1520 }
1510 case "shr_exact": {1521 case "shr_exact": {
1511 payloadHtml += "shrExact";1522 builtinName += "shrExact";
1512 break;1523 break;
1513 }1524 }
1514 case "bitcast": {1525 case "bitcast": {
1515 payloadHtml += "bitCast";1526 builtinName += "bitCast";
1516 break;1527 break;
1517 }1528 }
1518 case "align_cast": {1529 case "align_cast": {
1519 payloadHtml += "alignCast";1530 builtinName += "alignCast";
1520 break;1531 break;
1521 }1532 }
1522 case "vector_type": {1533 case "vector_type": {
1523 payloadHtml += "Vector";1534 builtinName += "Vector";
1524 break;1535 break;
1525 }1536 }
1526 case "reduce": {1537 case "reduce": {
1527 payloadHtml += "reduce";1538 builtinName += "reduce";
1528 break;1539 break;
1529 }1540 }
1530 case "splat": {1541 case "splat": {
1531 payloadHtml += "splat";1542 builtinName += "splat";
1532 break;1543 break;
1533 }1544 }
1534 case "offset_of": {1545 case "offset_of": {
1535 payloadHtml += "offsetOf";1546 builtinName += "offsetOf";
1536 break;1547 break;
1537 }1548 }
1538 case "bit_offset_of": {1549 case "bit_offset_of": {
1539 payloadHtml += "bitOffsetOf";1550 builtinName += "bitOffsetOf";
1540 break;1551 break;
1541 }1552 }
1542 default:1553 default:
1543 console.log("builtin function not handled yet or doesn't exist!");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 = "";
15591556
1560 if (lhsOp["binOpIndex"]) {1557 yield { src: builtinName, tag: Tag.builtin };
1561 print_lhs = "(" + lhs + ")";1558 yield Tok.l_paren;
1562 } else {1559 yield* ex(lhsOp, opts);
1563 print_lhs = lhs;1560 yield Tok.comma;
1564 }1561 yield Tok.space;
1565 if (rhsOp["binOpIndex"]) {1562 yield* ex(rhsOp, opts);
1566 print_rhs = "(" + rhs + ")";1563 yield Tok.r_paren;
1567 } else {1564 return;
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;
1739 }1565 }
1740 case "typeOf": {1566
1741 const typeRefArg = zigAnalysis.exprs[expr.typeOf];1567 case "enumLiteral": {
1742 let payloadHtml =1568 let literal = expr.enumLiteral;
1743 "@TypeOf(" +1569 yield Tok.period;
1744 exprName(typeRefArg, { wantHtml: true, wantLink: true }) +1570 yield { src: literal, tag: Tag.identifier };
1745 ")";1571 return;
1746 return payloadHtml;
1747 }1572 }
1748 case "typeInfo": {1573
1749 const typeRefArg = zigAnalysis.exprs[expr.typeInfo];1574 case "void": {
1750 let payloadHtml =1575 yield { src: "void", tag: Tag.identifier };
1751 "@typeInfo(" +1576 return;
1752 exprName(typeRefArg, { wantHtml: true, wantLink: true }) +
1753 ")";
1754 return payloadHtml;
1755 }1577 }
1578
1756 case "null": {1579 case "null": {
1757 if (opts.wantHtml) {1580 yield { src: "null", tag: Tag.identifier };
1758 return '<span class="tok-null">null</span>';1581 return;
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);
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 += "(";
17891583
1790 for (let i = 0; i < call.args.length; i++) {1584 case "undefined": {
1791 if (i != 0) payloadHtml += ", ";1585 yield { src: "undefined", tag: Tag.identifier };
1792 payloadHtml += exprName(call.args[i], opts);1586 return;
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);
1805 }1587 }
1806 case "declRef": {
1807 const name = getDecl(expr.declRef).name;
18081588
1809 if (opts.wantHtml) {1589 case "anytype": {
1810 let payloadHtml = "";1590 yield { src: "anytype", tag: Tag.keyword_anytype };
1811 if (opts.wantLink) {1591 return;
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);
1856 }1592 }
1857 case "undefined": {1593
1858 return "undefined";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": {1600
1861 return '"' + escapeHtml(expr.string) + '"';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 }
18631609
1864 case "int_big": {1610 case "switchIndex": {
1865 return (expr.int_big.negated ? "-" : "") + expr.int_big.value;1611 const switchIndex = zigAnalysis.exprs[expr.switchIndex];
1612 yield* ex(switchIndex, opts);
1613 return;
1866 }1614 }
18671615
1868 case "anytype": {1616 case "errorSets": {
1869 return "anytype";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 }
18711625
1872 case "this": {1626 case "errorUnion": {
1873 return "@This()";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 }
18751633
1876 case "type": {1634 case "type": {
...@@ -1880,416 +1638,389 @@ Happy writing!...@@ -1880,416 +1638,389 @@ Happy writing!
1880 if (typeof typeObj === "number") typeObj = getType(typeObj);1638 if (typeof typeObj === "number") typeObj = getType(typeObj);
1881 switch (typeObj.kind) {1639 switch (typeObj.kind) {
1882 default: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 case typeKinds.Struct: {1766 case typeKinds.Struct: {
1885 let structObj = typeObj;1767 let structObj = typeObj;
1886 let name = "";
1887 let layout = "";
1888 if (structObj.layout !== null) {1768 if (structObj.layout !== null) {
1889 switch (structObj.layout.enumLiteral) {1769 switch (structObj.layout.enumLiteral) {
1890 case "Packed": {1770 case "Packed": {
1891 layout = "packed ";1771 yield { src: "packed", tag: Tag.keyword_packed };
1892 break;1772 break;
1893 }1773 }
1894 case "Extern": {1774 case "Extern": {
1895 layout = "extern ";1775 yield { src: "extern", tag: Tag.keyword_extern };
1896 break;1776 break;
1897 }1777 }
1898 }1778 }
1779 yield Tok.space;
1899 }1780 }
1900 if (opts.wantHtml) {1781 yield { src: "struct", tag: Tag.keyword_struct };
1901 name = "<span class='tok-kw'>" + layout + "struct</span>";
1902 } else {
1903 name = layout + "struct";
1904 }
1905 if (structObj.backing_int !== null) {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 += " { ";1787 yield Tok.space;
1909 if (structObj.field_types.length > 1 && opts.wantHtml) { name += "</br>"; }1788 yield Tok.l_brace;
1910 let indent = "";1789
1911 if (structObj.field_types.length > 1 && opts.wantHtml) {1790 if (structObj.field_types.length > 1) {
1912 indent = "&nbsp;&nbsp;&nbsp;&nbsp;"1791 yield Tok.enter;
1792 } else {
1793 yield Tok.space;
1913 }1794 }
1914 if (opts.indent && structObj.field_types.length > 1) {1795
1915 indent = opts.indent + indent;1796 let indent = 0;
1797 if (structObj.field_types.length > 1) {
1798 indent = 1;
1916 }1799 }
1917 let structNode = getAstNode(structObj.src);1800 if (opts.indent && structObj.field_types.length > 1) {
1918 let field_end = ",";1801 indent += opts.ident;
1919 if (structObj.field_types.length > 1 && opts.wantHtml) {
1920 field_end += "</br>";
1921 } else {
1922 field_end += " ";
1923 }1802 }
19241803
1804 let structNode = getAstNode(structObj.src);
1925 for (let i = 0; i < structObj.field_types.length; i += 1) {1805 for (let i = 0; i < structObj.field_types.length; i += 1) {
1926 let fieldNode = getAstNode(structNode.fields[i]);1806 let fieldNode = getAstNode(structNode.fields[i]);
1927 let fieldName = fieldNode.name;1807 let fieldName = fieldNode.name;
1928 let html = indent;1808
1929 if (!structObj.is_tuple) {1809 for (let j = 0; j < indent; j += 1) {
1930 html += escapeHtml(fieldName);1810 yield Tok.tab;
1931 }1811 }
19321812
1933 let fieldTypeExpr = structObj.field_types[i];1813 if (!typeObj.is_tuple) {
1934 if (!structObj.is_tuple) {1814 yield { src: fieldName, tag: Tag.identifier };
1935 html += ": ";
1936 }1815 }
19371816
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 });
19391823
1940 if (structObj.field_defaults[i] !== null) {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 }
19431830
1944 html += field_end;1831 if (structObj.field_types.length > 1) {
19451832 yield Tok.comma;
1946 name += html;1833 yield Tok.enter;
1947 }1834 } else {
1948 if (opts.indent && structObj.field_types.length > 1) {1835 yield Tok.space;
1949 name += opts.indent;1836 }
1950 }1837 }
1951 name += "}";1838 yield Tok.r_brace;
1952 return name;1839 return;
1953 }1840 }
1954 case typeKinds.Enum: {1841 case typeKinds.Enum: {
1955 let enumObj = typeObj;1842 let enumObj = typeObj;
1956 let name = "";1843 yield { src: "enum", tag: Tag.keyword_enum };
1957 if (opts.wantHtml) {
1958 name = "<span class='tok-kw'>enum</span>";
1959 } else {
1960 name = "enum";
1961 }
1962 if (enumObj.tag) {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 let enumNode = getAstNode(enumObj.src);1852 let enumNode = getAstNode(enumObj.src);
1967 let fields_len = enumNode.fields.length;1853 let fields_len = enumNode.fields.length;
1968 if (enumObj.nonexhaustive) {1854 if (enumObj.nonexhaustive) {
1969 fields_len += 1;1855 fields_len += 1;
1970 }1856 }
1971 if (fields_len > 1 && opts.wantHtml) { name += "</br>"; }1857
1972 let indent = "";1858 if (fields_len > 1) {
1859 yield Tok.enter;
1860 } else {
1861 yield Tok.space;
1862 }
1863
1864 let indent = 0;
1973 if (fields_len > 1) {1865 if (fields_len > 1) {
1974 if (opts.wantHtml) {1866 indent = 1;
1975 indent = "&nbsp;&nbsp;&nbsp;&nbsp;";
1976 } else {
1977 indent = " ";
1978 }
1979 }1867 }
1980 if (opts.indent) {1868 if (opts.indent) {
1981 indent = opts.indent + indent;1869 indent += opts.indent;
1982 }
1983 let field_end = ",";
1984 if (fields_len > 1 && opts.wantHtml) {
1985 field_end += "</br>";
1986 } else {
1987 field_end += " ";
1988 }1870 }
1871
1989 for (let i = 0; i < enumNode.fields.length; i += 1) {1872 for (let i = 0; i < enumNode.fields.length; i += 1) {
1990 let fieldNode = getAstNode(enumNode.fields[i]);1873 let fieldNode = getAstNode(enumNode.fields[i]);
1991 let fieldName = fieldNode.name;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 };
19931878
1994 if (enumObj.values[i] !== null) {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 }
19971885
1998 html += field_end;1886 if (fields_len > 1) {
19991887 yield Tok.comma;
2000 name += html;1888 yield Tok.enter;
1889 }
2001 }1890 }
2002 if (enumObj.nonexhaustive) {1891 for (let j = 0; j < indent; j += 1) yield Tok.tab;
2003 name += indent + "_" + field_end;1892 yield { src: "_", tag: Tag.identifier };
1893 if (fields_len > 1) {
1894 yield Tok.comma;
1895 yield Tok.enter;
2004 }1896 }
2005 if (opts.indent) {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 += "}";1900 yield Tok.r_brace;
2009 return name;1901 return;
2010 }1902 }
2011 case typeKinds.Union: {1903 case typeKinds.Union: {
2012 let unionObj = typeObj;1904 let unionObj = typeObj;
2013 let name = "";
2014 let layout = "";
2015 if (unionObj.layout !== null) {1905 if (unionObj.layout !== null) {
2016 switch (unionObj.layout.enumLiteral) {1906 switch (unionObj.layout.enumLiteral) {
2017 case "Packed": {1907 case "Packed": {
2018 layout = "packed ";1908 yield { src: "packed", tag: Tag.keyword_packed };
2019 break;1909 break;
2020 }1910 }
2021 case "Extern": {1911 case "Extern": {
2022 layout = "extern ";1912 yield { src: "extern", tag: Tag.keyword_extern };
2023 break;1913 break;
2024 }1914 }
2025 }1915 }
1916 yield Tok.space;
2026 }1917 }
2027 if (opts.wantHtml) {1918 yield { src: "union", tag: Tag.keyword_union };
2028 name = "<span class='tok-kw'>" + layout + "union</span>";
2029 } else {
2030 name = layout + "union";
2031 }
2032 if (unionObj.auto_tag) {1919 if (unionObj.auto_tag) {
2033 if (opts.wantHtml) {1920 yield Tok.l_paren;
2034 name += "(<span class='tok-kw'>enum</span>";1921 yield { src: "enum", tag: Tag.keyword_enum };
2035 } else {
2036 name += "(enum";
2037 }
2038 if (unionObj.tag) {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 } else {1927 } else {
2041 name += ")";1928 yield Tok.r_paren;
2042 }1929 }
2043 } else if (unionObj.tag) {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 += " { ";1935 yield Tok.space;
2047 if (unionObj.field_types.length > 1 && opts.wantHtml) {1936 yield Tok.l_brace;
2048 name += "</br>";1937 if (unionObj.field_types.length > 1) {
1938 yield Tok.enter;
1939 } else {
1940 yield Tok.space;
2049 }1941 }
2050 let indent = "";1942 let indent = 0;
2051 if (unionObj.field_types.length > 1 && opts.wantHtml) {1943 if (unionObj.field_types.length > 1) {
2052 indent = "&nbsp;&nbsp;&nbsp;&nbsp;"1944 indent = 1;
2053 }1945 }
2054 if (opts.indent) {1946 if (opts.indent) {
2055 indent = opts.indent + indent;1947 indent += opts.indent;
2056 }1948 }
2057 let unionNode = getAstNode(unionObj.src);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 for (let i = 0; i < unionObj.field_types.length; i += 1) {1950 for (let i = 0; i < unionObj.field_types.length; i += 1) {
2065 let fieldNode = getAstNode(unionNode.fields[i]);1951 let fieldNode = getAstNode(unionNode.fields[i]);
2066 let fieldName = fieldNode.name;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 };
20681955
2069 let fieldTypeExpr = unionObj.field_types[i];1956 let fieldTypeExpr = unionObj.field_types[i];
2070 html += ": ";1957 yield Tok.colon;
1958 yield Tok.space;
20711959
2072 html += exprName(fieldTypeExpr, { ...opts, indent: indent });1960 yield* ex(fieldTypeExpr, { ...opts, indent: indent });
20731961
2074 html += field_end;1962 if (unionObj.field_types.length > 1) {
20751963 yield Tok.comma;
2076 name += html;1964 yield Tok.enter;
1965 } else {
1966 yield Tok.space;
1967 }
2077 }1968 }
2078 if (opts.indent) {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 += "}";1972 yield Tok.r_brace;
2082 return name;1973 return;
2083 }1974 }
2084 case typeKinds.Opaque: {1975 case typeKinds.Opaque: {
2085 let opaqueObj = typeObj;1976 yield { src: "opaque", tag: Tag.keyword_opaque };
2086 return opaqueObj;1977 yield Tok.space;
2087 }1978 yield Tok.l_brace;
2088 case typeKinds.ComptimeExpr: {1979 yield Tok.r_brace;
2089 return "anyopaque";1980 return;
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 }
2214 }1981 }
2215 case typeKinds.Int: {1982 case typeKinds.EnumLiteral: {
2216 let intObj = typeObj;1983 yield { src: "(enum literal)", tag: Tag.identifier };
2217 let name = intObj.name;1984 return;
2218 if (opts.wantHtml) {
2219 return '<span class="tok-type">' + name + "</span>";
2220 } else {
2221 return name;
2222 }
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 case typeKinds.ErrorSet: {1986 case typeKinds.ErrorSet: {
2267 let errSetObj = typeObj;1987 let errSetObj = typeObj;
2268 if (errSetObj.fields == null) {1988 if (errSetObj.fields === null) {
2269 return '<span class="tok-type">anyerror</span>';1989 yield { src: "anyerror", tag: Tag.identifier };
2270 } else if (errSetObj.fields.length == 0) {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 } else if (errSetObj.fields.length == 1) {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 } else {1999 } else {
2275 // throw "TODO";2000 yield { src: "error", tag: Tag.keyword_error };
2276 let html = "error{ " + errSetObj.fields[0].name;2001 yield Tok.l_brace;
2277 for (let i = 1; i < errSetObj.fields.length; i++) html += ", " + errSetObj.fields[i].name;2002 yield { src: errSetObj.fields[0].name, tag: Tag.identifier };
2278 html += " }";2003 for (let i = 1; i < errSetObj.fields.length; i++) {
2279 return html;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 case typeKinds.ErrorUnion: {2012 case typeKinds.ErrorUnion: {
2284 let errUnionObj = typeObj;2013 let errUnionObj = typeObj;
2285 let lhs = exprName(errUnionObj.lhs, opts);2014 yield* ex(errUnionObj.lhs, opts);
2286 let rhs = exprName(errUnionObj.rhs, opts);2015 yield { src: "!", tag: Tag.bang };
2287 return lhs + "!" + rhs;2016 yield* ex(errUnionObj.rhs, opts);
2017 return;
2288 }2018 }
2289 case typeKinds.InferredErrorUnion: {2019 case typeKinds.InferredErrorUnion: {
2290 let errUnionObj = typeObj;2020 let errUnionObj = typeObj;
2291 let payload = exprName(errUnionObj.payload, opts);2021 yield { src: "!", tag: Tag.bang };
2292 return "!" + payload;2022 yield* ex(errUnionObj.payload, opts);
2023 return;
2293 }2024 }
2294 case typeKinds.Fn: {2025 case typeKinds.Fn: {
2295 let fnObj = typeObj;2026 let fnObj = typeObj;
...@@ -2299,45 +2030,32 @@ Happy writing!...@@ -2299,45 +2030,32 @@ Happy writing!
2299 opts.linkFnNameDecl = null;2030 opts.linkFnNameDecl = null;
2300 let payloadHtml = "";2031 let payloadHtml = "";
2301 if (opts.addParensIfFnSignature && fnObj.src == 0) {2032 if (opts.addParensIfFnSignature && fnObj.src == 0) {
2302 payloadHtml += "(";2033 yield Tok.l_paren;
2303 }2034 }
2304 if (fnObj.is_extern) {2035 if (fnObj.is_extern) {
2305 if (opts.wantHtml) {2036 yield { src: "extern", tag: Tag.keyword_extern };
2306 payloadHtml += '<span class="tok-kw">extern </span>';2037 yield Tok.space;
2307 } else {
2308 payloadHtml += "extern ";
2309 }
2310 } else if (fnObj.has_cc) {2038 } else if (fnObj.has_cc) {
2311 let cc_expr = zigAnalysis.exprs[fnObj.cc];2039 let cc_expr = zigAnalysis.exprs[fnObj.cc];
2312 if (cc_expr.enumLiteral === "Inline") {2040 if (cc_expr.enumLiteral === "Inline") {
2313 if(opts.wantHtml) {2041 yield { src: "inline", tag: Tag.keyword_inline };
2314 payloadHtml += '<span class="tok-kw">inline </span>'2042 yield Tok.space;
2315 } else {
2316 payloadHtml += "inline "
2317 }
2318 }2043 }
2319 }2044 }
2320 if (fnObj.has_lib_name) {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) {2049 yield { src: "fn", tag: Tag.keyword_fn };
2324 payloadHtml += '<span class="tok-kw">fn </span>';2050 yield Tok.space;
2325 if (fnDecl) {2051 if (fnDecl) {
2326 payloadHtml += '<span class="tok-fn">';2052 if (linkFnNameDecl) {
2327 if (linkFnNameDecl) {2053 yield { src: fnDecl.name, tag: Tag.identifier, link: linkFnNameDecl, fnDecl: false };
2328 payloadHtml +=2054 } else {
2329 '<a href="' + linkFnNameDecl + '">' +2055 yield { src: fnDecl.name, tag: Tag.identifier, fnDecl: true };
2330 escapeHtml(fnDecl.name) +
2331 "</a>";
2332 } else {
2333 payloadHtml += escapeHtml(fnDecl.name);
2334 }
2335 payloadHtml += "</span>";
2336 }2056 }
2337 } else {
2338 payloadHtml += "fn ";
2339 }2057 }
2340 payloadHtml += "(";2058 yield Tok.l_paren;
2341 if (fnObj.params) {2059 if (fnObj.params) {
2342 let fields = null;2060 let fields = null;
2343 let isVarArgs = false;2061 let isVarArgs = false;
...@@ -2349,13 +2067,10 @@ Happy writing!...@@ -2349,13 +2067,10 @@ Happy writing!
23492067
2350 for (let i = 0; i < fnObj.params.length; i += 1) {2068 for (let i = 0; i < fnObj.params.length; i += 1) {
2351 if (i != 0) {2069 if (i != 0) {
2352 payloadHtml += ", ";2070 yield Tok.comma;
2071 yield Tok.space;
2353 }2072 }
23542073
2355 if (opts.wantHtml) {
2356 payloadHtml +=
2357 "<span class='argBreaker'><br>&nbsp;&nbsp;&nbsp;&nbsp;</span>";
2358 }
2359 let value = fnObj.params[i];2074 let value = fnObj.params[i];
2360 let paramValue = resolveValue({ expr: value });2075 let paramValue = resolveValue({ expr: value });
23612076
...@@ -2363,24 +2078,20 @@ Happy writing!...@@ -2363,24 +2078,20 @@ Happy writing!
2363 let paramNode = getAstNode(fields[i]);2078 let paramNode = getAstNode(fields[i]);
23642079
2365 if (paramNode.varArgs) {2080 if (paramNode.varArgs) {
2366 payloadHtml += "...";2081 yield Tok.period;
2082 yield Tok.period;
2083 yield Tok.period;
2367 continue;2084 continue;
2368 }2085 }
23692086
2370 if (paramNode.noalias) {2087 if (paramNode.noalias) {
2371 if (opts.wantHtml) {2088 yield { src: "noalias", tag: Tag.keyword_noalias };
2372 payloadHtml += '<span class="tok-kw">noalias</span> ';2089 yield Tok.space;
2373 } else {
2374 payloadHtml += "noalias ";
2375 }
2376 }2090 }
23772091
2378 if (paramNode.comptime) {2092 if (paramNode.comptime) {
2379 if (opts.wantHtml) {2093 yield { src: "comptime", tag: Tag.keyword_comptime };
2380 payloadHtml += '<span class="tok-kw">comptime</span> ';2094 yield Tok.space;
2381 } else {
2382 payloadHtml += "comptime ";
2383 }
2384 }2095 }
23852096
2386 let paramName = paramNode.name;2097 let paramName = paramNode.name;
...@@ -2390,94 +2101,102 @@ Happy writing!...@@ -2390,94 +2101,102 @@ Happy writing!
2390 if (paramName === "") {2101 if (paramName === "") {
2391 paramName = "_";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 }
23972110
2398 if (isVarArgs && i === fnObj.params.length - 1) {2111 if (isVarArgs && i === fnObj.params.length - 1) {
2399 payloadHtml += "...";2112 yield Tok.period;
2113 yield Tok.period;
2114 yield Tok.period;
2400 } else if ("alignOf" in value) {2115 } else if ("alignOf" in value) {
2401 payloadHtml += exprName(value, opts);2116 yield* ex(value, opts);
2402 } else if ("typeOf" in value) {2117 } else if ("typeOf" in value) {
2403 payloadHtml += exprName(value, opts);2118 yield* ex(value, opts);
2404 } else if ("typeOf_peer" in value) {2119 } else if ("typeOf_peer" in value) {
2405 payloadHtml += exprName(value, opts);2120 yield* ex(value, opts);
2406 } else if ("declRef" in value) {2121 } else if ("declRef" in value) {
2407 payloadHtml += exprName(value, opts);2122 yield* ex(value, opts);
2408 } else if ("call" in value) {2123 } else if ("call" in value) {
2409 payloadHtml += exprName(value, opts);2124 yield* ex(value, opts);
2410 } else if ("refPath" in value) {2125 } else if ("refPath" in value) {
2411 payloadHtml += exprName(value, opts);2126 yield* ex(value, opts);
2412 } else if ("type" in value) {2127 } else if ("type" in value) {
2413 payloadHtml += exprName(value, opts);2128 yield* ex(value, opts);
2414 //payloadHtml += '<span class="tok-kw">' + name + "</span>";2129 //payloadHtml += '<span class="tok-kw">' + name + "</span>";
2415 } else if ("binOpIndex" in value) {2130 } else if ("binOpIndex" in value) {
2416 payloadHtml += exprName(value, opts);2131 yield* ex(value, opts);
2417 } else if ("comptimeExpr" in value) {2132 } else if ("comptimeExpr" in value) {
2418 let comptimeExpr =2133 let comptimeExpr =
2419 zigAnalysis.comptimeExprs[value.comptimeExpr].code;2134 zigAnalysis.comptimeExprs[value.comptimeExpr].code;
2420 if (opts.wantHtml) {2135 yield* Tokenizer(comptimeExpr);
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>';
2428 } else {2136 } else {
2429 payloadHtml += "anytype";2137 yield { src: "anytype", tag: Tag.keyword_anytype };
2430 }2138 }
2431 }2139 }
2432 }2140 }
24332141
2434 if (opts.wantHtml) {2142 yield Tok.r_paren;
2435 payloadHtml += "<span class='argBreaker'>,<br></span>";2143 yield Tok.space;
2436 }
2437 payloadHtml += ") ";
24382144
2439 if (fnObj.has_align) {2145 if (fnObj.has_align) {
2440 let align = zigAnalysis.exprs[fnObj.align];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 if (fnObj.has_cc) {2153 if (fnObj.has_cc) {
2444 let cc = zigAnalysis.exprs[fnObj.cc];2154 let cc = zigAnalysis.exprs[fnObj.cc];
2445 if (cc) {2155 if (cc) {
2446 if (cc.enumLiteral !== "Inline") {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 }
24512165
2452 if (fnObj.is_inferred_error) {2166 if (fnObj.is_inferred_error) {
2453 payloadHtml += "!";2167 yield { src: "!", tag: Tag.bang };
2454 }2168 }
2455 if (fnObj.ret != null) {2169 if (fnObj.ret != null) {
2456 payloadHtml += exprName(fnObj.ret, {2170 yield* ex(fnObj.ret, {
2457 ...opts,2171 ...opts,
2458 addParensIfFnSignature: true,2172 addParensIfFnSignature: true,
2459 });2173 });
2460 } else if (opts.wantHtml) {
2461 payloadHtml += '<span class="tok-kw">anytype</span>';
2462 } else {2174 } else {
2463 payloadHtml += "anytype";2175 yield { src: "anytype", tag: Tag.keyword_anytype };
2464 }2176 }
24652177
2466 if (opts.addParensIfFnSignature && fnObj.src == 0) {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 }
24802197
2198
2199
2481 function shouldSkipParamName(typeRef, paramName) {2200 function shouldSkipParamName(typeRef, paramName) {
2482 let resolvedTypeRef = resolveValue({ expr: typeRef });2201 let resolvedTypeRef = resolveValue({ expr: typeRef });
2483 if ("type" in resolvedTypeRef) {2202 if ("type" in resolvedTypeRef) {
...@@ -2504,13 +2223,13 @@ Happy writing!...@@ -2504,13 +2223,13 @@ Happy writing!
2504 typeObj ===2223 typeObj ===
2505 getType(zigAnalysis.modules[zigAnalysis.rootMod].main)2224 getType(zigAnalysis.modules[zigAnalysis.rootMod].main)
2506 ) {2225 ) {
2507 name = "std";2226 name = renderSingleToken(Tok.identifier("std"));
2508 } else {2227 } else {
2509 name = exprName({ type: typeObj }, { wantHtml: false, wantLink: false });2228 name = renderTokens(ex({ type: typeObj }));
2510 }2229 }
2511 if (name != null && name != "") {2230 if (name != null && name != "") {
2512 domHdrName.innerText =2231 domHdrName.innerHTML = "<pre class='inline'>" + name + "</pre> ("
2513 name + " (" + zigAnalysis.typeKinds[typeObj.kind] + ")";2232 + zigAnalysis.typeKinds[typeObj.kind] + ")";
2514 domHdrName.classList.remove("hidden");2233 domHdrName.classList.remove("hidden");
2515 }2234 }
2516 if (typeObj.kind == typeKinds.ErrorSet) {2235 if (typeObj.kind == typeKinds.ErrorSet) {
...@@ -2528,7 +2247,7 @@ Happy writing!...@@ -2528,7 +2247,7 @@ Happy writing!
2528 //let srcObj = zigAnalysis.astNodes[errObj.src];2247 //let srcObj = zigAnalysis.astNodes[errObj.src];
2529 errorList.push(errObj);2248 errorList.push(errObj);
2530 }2249 }
2531 errorList.sort(function (a, b) {2250 errorList.sort(function(a, b) {
2532 return operatorCompare(a.name.toLowerCase(), b.name.toLowerCase());2251 return operatorCompare(a.name.toLowerCase(), b.name.toLowerCase());
2533 });2252 });
25342253
...@@ -2624,55 +2343,80 @@ Happy writing!...@@ -2624,55 +2343,80 @@ Happy writing!
2624 if (resolvedValue.expr.fieldRef) {2343 if (resolvedValue.expr.fieldRef) {
2625 const declRef = decl.value.expr.refPath[0].declRef;2344 const declRef = decl.value.expr.refPath[0].declRef;
2626 const type = getDecl(declRef);2345 const type = getDecl(declRef);
2627 domFnProtoCode.innerHTML =2346
2628 '<span class="tok-kw">const</span> ' +2347 domFnProtoCode.innerHTML = renderTokens(
2629 escapeHtml(decl.name) +2348 (function*() {
2630 ": " +2349 yield Tok.const;
2631 type.name +2350 yield Tok.space;
2632 " = " +2351 yield Tok.identifier(decl.name);
2633 exprName(decl.value.expr, { wantHtml: true, wantLink: true }) +2352 yield Tok.colon;
2634 ";";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 } else if (2361 } else if (
2636 resolvedValue.expr.string !== undefined ||2362 resolvedValue.expr.string !== undefined ||
2637 resolvedValue.expr.call !== undefined ||2363 resolvedValue.expr.call !== undefined ||
2638 resolvedValue.expr.comptimeExpr !== undefined2364 resolvedValue.expr.comptimeExpr !== undefined
2639 ) {2365 ) {
2640 let typeRef = null;2366 domFnProtoCode.innerHTML = renderTokens(
2641 if (resolvedValue.typeRef !== undefined) {2367 (function*() {
2642 typeRef = resolvedValue.typeRef;2368 yield Tok.const;
2643 }2369 yield Tok.space;
2644 domFnProtoCode.innerHTML =2370 yield Tok.identifier(decl.name);
2645 '<span class="tok-kw">const</span> ' +2371 if (decl.value.typeRef) {
2646 escapeHtml(decl.name) +2372 yield Tok.colon;
2647 ": " +2373 yield Tok.space;
2648 exprName(typeRef !== null ? typeRef : resolvedValue.expr, { wantHtml: true, wantLink: true }) +2374 yield* ex(decl.value.typeRef, {});
2649 " = " +2375 }
2650 exprName(decl.value.expr, { wantHtml: true, wantLink: true }) +2376 yield Tok.space;
2651 ";";2377 yield Tok.eql;
2378 yield Tok.space;
2379 yield* ex(decl.value.expr, {});
2380 yield Tok.semi;
2381 })());
2652 } else if (resolvedValue.expr.compileError) {2382 } else if (resolvedValue.expr.compileError) {
2653 domFnProtoCode.innerHTML =2383 domFnProtoCode.innerHTML = renderTokens(
2654 '<span class="tok-kw">const</span> ' +2384 (function*() {
2655 escapeHtml(decl.name) +2385 yield Tok.const;
2656 " = " +2386 yield Tok.space;
2657 exprName(decl.value.expr, { wantHtml: true, wantLink: true }) +2387 yield Tok.identifier(decl.name);
2658 ";";2388 yield Tok.space;
2389 yield Tok.eql;
2390 yield Tok.space;
2391 yield* ex(decl.value.expr, {});
2392 yield Tok.semi;
2393 })());
2659 } else {2394 } else {
2660 domFnProtoCode.innerHTML =2395 const parent = getType(decl.parent_container);
2661 '<span class="tok-kw">const</span> ' +2396 domFnProtoCode.innerHTML = renderTokens(
2662 escapeHtml(decl.name) +2397 (function*() {
2663 ": " +2398 yield Tok.const;
2664 exprName(resolvedValue.typeRef, { wantHtml: true, wantLink: true }) +2399 yield Tok.space;
2665 " = " +2400 yield Tok.identifier(decl.name);
2666 exprName(decl.value.expr, { wantHtml: true, wantLink: true }) +2401 if (decl.value.typeRef !== null) {
2667 ";";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 }
26692413
2670 let docs = getAstNode(decl.src).docs;2414 let docs = getAstNode(decl.src).docs;
2671 if (docs != null) {2415 if (docs != null) {
2672 // TODO: it shouldn't just be decl.parent_container, but rather 2416 // TODO: it shouldn't just be decl.parent_container, but rather
2673 // the type that the decl holds (if the value is a type)2417 // the type that the decl holds (if the value is a type)
2674 domTldDocs.innerHTML = markdown(docs, getType(decl.parent_container));2418 domTldDocs.innerHTML = markdown(docs, decl);
2675 2419
2676 domTldDocs.classList.remove("hidden");2420 domTldDocs.classList.remove("hidden");
2677 }2421 }
26782422
...@@ -2685,43 +2429,68 @@ Happy writing!...@@ -2685,43 +2429,68 @@ Happy writing!
2685 if (resolvedVar.expr.fieldRef) {2429 if (resolvedVar.expr.fieldRef) {
2686 const declRef = decl.value.expr.refPath[0].declRef;2430 const declRef = decl.value.expr.refPath[0].declRef;
2687 const type = getDecl(declRef);2431 const type = getDecl(declRef);
2688 domFnProtoCode.innerHTML =2432 domFnProtoCode.innerHTML = renderTokens(
2689 '<span class="tok-kw">var</span> ' +2433 (function*() {
2690 escapeHtml(decl.name) +2434 yield Tok.var;
2691 ": " +2435 yield Tok.space;
2692 type.name +2436 yield Tok.identifier(decl.name);
2693 " = " +2437 yield Tok.colon;
2694 exprName(decl.value.expr, { wantHtml: true, wantLink: true }) +2438 yield Tok.space;
2695 ";";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 } else if (2446 } else if (
2697 resolvedVar.expr.string !== undefined ||2447 resolvedVar.expr.string !== undefined ||
2698 resolvedVar.expr.call !== undefined ||2448 resolvedVar.expr.call !== undefined ||
2699 resolvedVar.expr.comptimeExpr !== undefined2449 resolvedVar.expr.comptimeExpr !== undefined
2700 ) {2450 ) {
2701 domFnProtoCode.innerHTML =2451 domFnProtoCode.innerHTML = renderTokens(
2702 '<span class="tok-kw">var</span> ' +2452 (function*() {
2703 escapeHtml(decl.name) +2453 yield Tok.var;
2704 ": " +2454 yield Tok.space;
2705 exprName(resolvedVar.typeRef !== null ? resolvedVar.typeRef : resolvedVar.expr, { wantHtml: true, wantLink: true }) +2455 yield Tok.identifier(decl.name);
2706 " = " +2456 if (decl.value.typeRef) {
2707 exprName(decl.value.expr, { wantHtml: true, wantLink: true }) +2457 yield Tok.colon;
2708 ";";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 } else if (resolvedVar.expr.compileError) {2467 } else if (resolvedVar.expr.compileError) {
2710 domFnProtoCode.innerHTML =2468 domFnProtoCode.innerHTML = renderTokens(
2711 '<span class="tok-kw">var</span> ' +2469 (function*() {
2712 escapeHtml(decl.name) +2470 yield Tok.var;
2713 " = " +2471 yield Tok.space;
2714 exprName(decl.value.expr, { wantHtml: true, wantLink: true }) +2472 yield Tok.identifier(decl.name);
2715 ";";2473 yield Tok.space;
2474 yield Tok.eql;
2475 yield Tok.space;
2476 yield* ex(decl.value.expr, {});
2477 yield Tok.semi;
2478 })());
2716 } else {2479 } else {
2717 domFnProtoCode.innerHTML =2480 domFnProtoCode.innerHTML = renderTokens(
2718 '<span class="tok-kw">var</span> ' +2481 (function*() {
2719 escapeHtml(decl.name) +2482 yield Tok.var;
2720 ": " +2483 yield Tok.space;
2721 exprName(resolvedVar.typeRef, { wantHtml: true, wantLink: true }) +2484 yield Tok.identifier(decl.name);
2722 " = " +2485 yield Tok.colon;
2723 exprName(decl.value.expr, { wantHtml: true, wantLink: true }) +2486 yield Tok.space;
2724 ";";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 }
27262495
2727 let docs = getAstNode(decl.src).docs;2496 let docs = getAstNode(decl.src).docs;
...@@ -2955,7 +2724,7 @@ Happy writing!...@@ -2955,7 +2724,7 @@ Happy writing!
2955 resizeDomList(2724 resizeDomList(
2956 domListFns,2725 domListFns,
2957 fnsList.length,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 );
29602729
2961 for (let i = 0; i < fnsList.length; i += 1) {2730 for (let i = 0; i < fnsList.length; i += 1) {
...@@ -2968,12 +2737,10 @@ Happy writing!...@@ -2968,12 +2737,10 @@ Happy writing!
29682737
2969 let declType = resolveValue(decl.value);2738 let declType = resolveValue(decl.value);
2970 console.assert("type" in declType.expr);2739 console.assert("type" in declType.expr);
2971 tdFnSignature.innerHTML = exprName(declType.expr, {2740 tdFnSignature.innerHTML = renderTokens(ex(declType.expr, {
2972 wantHtml: true,
2973 wantLink: true,
2974 fnDecl: decl,2741 fnDecl: decl,
2975 linkFnNameDecl: navLinkDecl(decl.name),2742 linkFnNameDecl: navLinkDecl(decl.name),
2976 });2743 }));
2977 tdFnSrc.innerHTML = "<a style=\"float: right;\" target=\"_blank\" href=\"" +2744 tdFnSrc.innerHTML = "<a style=\"float: right;\" target=\"_blank\" href=\"" +
2978 sourceFileLink(decl) + "\">[src]</a>";2745 sourceFileLink(decl) + "\">[src]</a>";
29792746
...@@ -3018,14 +2785,22 @@ Happy writing!...@@ -3018,14 +2785,22 @@ Happy writing!
3018 if (container.kind === typeKinds.Enum) {2785 if (container.kind === typeKinds.Enum) {
3019 let value = container.values[i];2786 let value = container.values[i];
3020 if (value !== null) {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 } else {2795 } else {
3024 let fieldTypeExpr = container.field_types[i];2796 let fieldTypeExpr = container.field_types[i];
3025 if (container.kind !== typeKinds.Struct || !container.is_tuple) {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 let tsn = typeShorthandName(fieldTypeExpr);2804 let tsn = typeShorthandName(fieldTypeExpr);
3030 if (tsn) {2805 if (tsn) {
3031 html += "<span> (" + tsn + ")</span>";2806 html += "<span> (" + tsn + ")</span>";
...@@ -3033,7 +2808,12 @@ Happy writing!...@@ -3033,7 +2808,12 @@ Happy writing!
3033 if (container.kind === typeKinds.Struct && !container.is_tuple) {2808 if (container.kind === typeKinds.Struct && !container.is_tuple) {
3034 let defaultInitExpr = container.field_defaults[i];2809 let defaultInitExpr = container.field_defaults[i];
3035 if (defaultInitExpr !== null) {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,7 +2832,7 @@ Happy writing!
3052 resizeDomList(2832 resizeDomList(
3053 domListGlobalVars,2833 domListGlobalVars,
3054 varsList.length,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 for (let i = 0; i < varsList.length; i += 1) {2837 for (let i = 0; i < varsList.length; i += 1) {
3058 let decl = varsList[i];2838 let decl = varsList[i];
...@@ -3061,15 +2841,13 @@ Happy writing!...@@ -3061,15 +2841,13 @@ Happy writing!
3061 let tdName = trDom.children[0];2841 let tdName = trDom.children[0];
3062 let tdNameA = tdName.children[0];2842 let tdNameA = tdName.children[0];
3063 let tdType = trDom.children[1];2843 let tdType = trDom.children[1];
2844 let preType = tdType.children[0];
3064 let tdDesc = trDom.children[2];2845 let tdDesc = trDom.children[2];
30652846
3066 tdNameA.setAttribute("href", navLinkDecl(decl.name));2847 tdNameA.setAttribute("href", navLinkDecl(decl.name));
3067 tdNameA.textContent = decl.name;2848 tdNameA.textContent = decl.name;
30682849
3069 tdType.innerHTML = exprName(walkResultTypeRef(decl.value), {2850 preType.innerHTML = renderTokens(ex(walkResultTypeRef(decl.value), {}));
3070 wantHtml: true,
3071 wantLink: true,
3072 });
30732851
3074 let docs = getAstNode(decl.src).docs;2852 let docs = getAstNode(decl.src).docs;
3075 if (docs != null) {2853 if (docs != null) {
...@@ -3085,7 +2863,7 @@ Happy writing!...@@ -3085,7 +2863,7 @@ Happy writing!
3085 resizeDomList(2863 resizeDomList(
3086 domListValues,2864 domListValues,
3087 valsList.length,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 for (let i = 0; i < valsList.length; i += 1) {2868 for (let i = 0; i < valsList.length; i += 1) {
3091 let decl = valsList[i];2869 let decl = valsList[i];
...@@ -3094,15 +2872,13 @@ Happy writing!...@@ -3094,15 +2872,13 @@ Happy writing!
3094 let tdName = trDom.children[0];2872 let tdName = trDom.children[0];
3095 let tdNameA = tdName.children[0];2873 let tdNameA = tdName.children[0];
3096 let tdType = trDom.children[1];2874 let tdType = trDom.children[1];
2875 let preType = tdType.children[0];
3097 let tdDesc = trDom.children[2];2876 let tdDesc = trDom.children[2];
30982877
3099 tdNameA.setAttribute("href", navLinkDecl(decl.name));2878 tdNameA.setAttribute("href", navLinkDecl(decl.name));
3100 tdNameA.textContent = decl.name;2879 tdNameA.textContent = decl.name;
31012880
3102 tdType.innerHTML = exprName(walkResultTypeRef(decl.value), {2881 preType.innerHTML = renderTokens(ex(walkResultTypeRef(decl.value), {}));
3103 wantHtml: true,
3104 wantLink: true,
3105 });
31062882
3107 let docs = getAstNode(decl.src).docs;2883 let docs = getAstNode(decl.src).docs;
3108 if (docs != null) {2884 if (docs != null) {
...@@ -3118,24 +2894,21 @@ Happy writing!...@@ -3118,24 +2894,21 @@ Happy writing!
3118 resizeDomList(2894 resizeDomList(
3119 domListTests,2895 domListTests,
3120 testsList.length,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 for (let i = 0; i < testsList.length; i += 1) {2899 for (let i = 0; i < testsList.length; i += 1) {
3124 let decl = testsList[i];2900 let decl = testsList[i];
3125 let trDom = domListTests.children[i];2901 let trDom = domListTests.children[i];
31262902
3127 let tdName = trDom.children[0];2903 let tdName = trDom.children[0];
3128 let tdNameA = tdName.children[0];2904 let tdNamePre = tdName.children[0];
3129 let tdType = trDom.children[1];2905 let tdType = trDom.children[1];
2906 let tdTypePre = tdType.children[0];
3130 let tdDesc = trDom.children[2];2907 let tdDesc = trDom.children[2];
31312908
3132 tdNameA.setAttribute("href", navLinkDecl(decl.name));2909 tdNamePre.innerHTML = renderSingleToken(Tok.identifier(decl.name));
3133 tdNameA.textContent = decl.name;
31342910
3135 tdType.innerHTML = exprName(walkResultTypeRef(decl.value), {2911 tdTypePre.innerHTML = ex(walkResultTypeRef(decl.value), {});
3136 wantHtml: true,
3137 wantLink: true,
3138 });
31392912
3140 let docs = getAstNode(decl.src).docs;2913 let docs = getAstNode(decl.src).docs;
3141 if (docs != null) {2914 if (docs != null) {
...@@ -3260,7 +3033,7 @@ Happy writing!...@@ -3260,7 +3033,7 @@ Happy writing!
32603033
3261 return;3034 return;
3262 case NAV_MODES.GUIDES:3035 case NAV_MODES.GUIDES:
3263 3036
3264 const sections = zigAnalysis.guide_sections;3037 const sections = zigAnalysis.guide_sections;
3265 if (sections.length != 0 && sections[0].guides.length != 0 && nonSearchPart == "") {3038 if (sections.length != 0 && sections[0].guides.length != 0 && nonSearchPart == "") {
3266 location.hash = NAV_MODES.GUIDES + sections[0].guides[0].name;3039 location.hash = NAV_MODES.GUIDES + sections[0].guides[0].name;
...@@ -3428,10 +3201,10 @@ Happy writing!...@@ -3428,10 +3201,10 @@ Happy writing!
3428 if (list[declIndex] != null) continue;3201 if (list[declIndex] != null) continue;
34293202
3430 let decl = getDecl(declIndex);3203 let decl = getDecl(declIndex);
3431 3204
3432 if (decl.is_uns) {3205 if (decl.is_uns) {
3433 let unsDeclList = [decl];3206 let unsDeclList = [decl];
3434 while(unsDeclList.length != 0) {3207 while (unsDeclList.length != 0) {
3435 let unsDecl = unsDeclList.pop();3208 let unsDecl = unsDeclList.pop();
3436 let unsDeclVal = resolveValue(unsDecl.value);3209 let unsDeclVal = resolveValue(unsDecl.value);
3437 if (!("type" in unsDeclVal.expr)) continue;3210 if (!("type" in unsDeclVal.expr)) continue;
...@@ -3441,7 +3214,7 @@ Happy writing!...@@ -3441,7 +3214,7 @@ Happy writing!
3441 for (let unsDeclI = 0; unsDeclI < unsPubDeclLen; unsDeclI += 1) {3214 for (let unsDeclI = 0; unsDeclI < unsPubDeclLen; unsDeclI += 1) {
3442 let childDeclIndex = unsType.pubDecls[unsDeclI];3215 let childDeclIndex = unsType.pubDecls[unsDeclI];
3443 let childDecl = getDecl(childDeclIndex);3216 let childDecl = getDecl(childDeclIndex);
3444 3217
3445 if (childDecl.is_uns) {3218 if (childDecl.is_uns) {
3446 unsDeclList.push(childDecl);3219 unsDeclList.push(childDecl);
3447 } else {3220 } else {
...@@ -3460,54 +3233,54 @@ Happy writing!...@@ -3460,54 +3233,54 @@ Happy writing!
3460 return list;3233 return list;
3461 }3234 }
34623235
3463function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {3236 function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
3464 let declVal = resolveValue(decl.value);3237 let declVal = resolveValue(decl.value);
3465 let declNames = item.declNames.concat([decl.name]);3238 let declNames = item.declNames.concat([decl.name]);
3466 let declIndexes = item.declIndexes.concat([declIndex]);3239 let declIndexes = item.declIndexes.concat([declIndex]);
34673240
3468 if (list[declIndex] != null) return;3241 if (list[declIndex] != null) return;
3469 list[declIndex] = {3242 list[declIndex] = {
3470 modNames: modNames,3243 modNames: modNames,
3471 declNames: declNames,3244 declNames: declNames,
3472 declIndexes: declIndexes,3245 declIndexes: declIndexes,
3473 };3246 };
34743247
3475 // add to search index3248 // add to search index
3476 {3249 {
3477 declSearchIndex.add(decl.name, {declIndex});3250 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;
3485 }3251 }
34863252
3487 if (isContainerType(value)) {
3488 stack.push({
3489 declNames: declNames,
3490 declIndexes: declIndexes,
3491 type: value,
3492 });
3493 }
34943253
3495 // Generic function3254 if ("type" in declVal.expr) {
3496 if (typeIsGenericFn(declVal.expr.type)) {3255 let value = getType(declVal.expr.type);
3497 let ret = resolveGenericRet(value);3256 if (declCanRepresentTypeKind(value.kind)) {
3498 if (ret != null && "type" in ret.expr) {3257 canonTypeDecls[declVal.type] = declIndex;
3499 let generic_type = getType(ret.expr.type);3258 }
3500 if (isContainerType(generic_type)) {3259
3501 stack.push({3260 if (isContainerType(value)) {
3502 declNames: declNames,3261 stack.push({
3503 declIndexes: declIndexes,3262 declNames: declNames,
3504 type: generic_type,3263 declIndexes: declIndexes,
3505 });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}
35113284
3512 function getCanonDeclPath(index) {3285 function getCanonDeclPath(index) {
3513 if (canonDeclPaths == null) {3286 if (canonDeclPaths == null) {
...@@ -3524,7 +3297,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {...@@ -3524,7 +3297,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
3524 }3297 }
35253298
3526 function escapeHtml(text) {3299 function escapeHtml(text) {
3527 return text.replace(/[&"<>]/g, function (m) {3300 return text.replace(/[&"<>]/g, function(m) {
3528 return escapeHtmlReplacements[m];3301 return escapeHtmlReplacements[m];
3529 });3302 });
3530 }3303 }
...@@ -3553,13 +3326,13 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {...@@ -3553,13 +3326,13 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
3553 }3326 }
35543327
3555 function parseGuides() {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 const section = zigAnalysis.guide_sections[j];3330 const section = zigAnalysis.guide_sections[j];
3558 for (let i = 0; i < section.guides.length; i+=1){3331 for (let i = 0; i < section.guides.length; i += 1) {
3559 let reader = new commonmark.Parser({smart: true});3332 let reader = new commonmark.Parser({ smart: true });
3560 const guide = section.guides[i];3333 const guide = section.guides[i];
3561 const ast = reader.parse(guide.body); 3334 const ast = reader.parse(guide.body);
3562 3335
3563 // Find the first text thing to use as a sidebar title3336 // Find the first text thing to use as a sidebar title
3564 guide.title = "[empty guide]";3337 guide.title = "[empty guide]";
3565 {3338 {
...@@ -3571,7 +3344,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {...@@ -3571,7 +3344,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
3571 guide.title = node.literal;3344 guide.title = node.literal;
3572 break;3345 break;
3573 }3346 }
3574 } 3347 }
3575 }3348 }
3576 // Index this guide3349 // Index this guide
3577 {3350 {
...@@ -3580,24 +3353,24 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {...@@ -3580,24 +3353,24 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
3580 while ((event = walker.next())) {3353 while ((event = walker.next())) {
3581 node = event.node;3354 node = event.node;
3582 if (event.entering == true && node.type === 'text') {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 }
35903363
3591 function indexTextForGuide(section_idx, guide_idx, node){3364 function indexTextForGuide(section_idx, guide_idx, node) {
3592 const terms = node.literal.split(" ");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 const t = terms[i];3367 const t = terms[i];
3595 if (!guidesSearchIndex[t]) guidesSearchIndex[t] = new Set();3368 if (!guidesSearchIndex[t]) guidesSearchIndex[t] = new Set();
3596 node.guide = {section_idx, guide_idx};3369 node.guide = { section_idx, guide_idx };
3597 guidesSearchIndex[t].add(node);3370 guidesSearchIndex[t].add(node);
3598 } 3371 }
3599 }3372 }
3600 3373
36013374
3602 function markdown(input, contextType) {3375 function markdown(input, contextType) {
3603 const parsed = new commonmark.Parser({ smart: true }).parse(input);3376 const parsed = new commonmark.Parser({ smart: true }).parse(input);
...@@ -3620,70 +3393,71 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {...@@ -3620,70 +3393,71 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
36203393
3621 return new commonmark.HtmlRenderer({ safe: true }).render(parsed);3394 return new commonmark.HtmlRenderer({ safe: true }).render(parsed);
36223395
3623 function detectDeclPath(text, context) {3396 }
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 }
36453397
3646 if (curContext == context) {3398 function detectDeclPath(text, context) {
3647 separator = '.';3399 let result = "";
3648 result = location.hash + separator + components[0];3400 let separator = ":";
3649 } else {3401 const components = text.split(".");
3650 // We had to go up, which means we need a new path!3402 let curDeclOrType = undefined;
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 }
36603403
3661 break;3404 let curContext = context;
3662 } 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]);
36633414
3664 if (!curDeclOrType) {3415 if (!curDeclOrType) {
3665 for (let i = 0; i < zigAnalysis.modules.length; i += 1){3416 if (curContext.parent_container == null) break;
3666 const p = zigAnalysis.modules[i];3417 curContext = getType(curContext.parent_container);
3667 if (p.name == components[0]) {3418 continue;
3668 curDeclOrType = getType(p.main);
3669 result += "#A;" + components[0];
3670 break;
3671 }
3672 }
3673 }3419 }
36743420
3675 if (!curDeclOrType) return null;3421 if (curContext == context) {
3676 3422 separator = '.';
3677 for (let i = 1; i < components.length; i += 1) {3423 result = location.hash + separator + components[0];
3678 curDeclOrType = findSubDecl(curDeclOrType, components[i]);3424 } else {
3679 if (!curDeclOrType) return null;3425 // We had to go up, which means we need a new path!
3680 result += separator + components[i];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 separator = '.';3432 separator = '.';
3433 result = "#A;" + fullPath;
3682 }3434 }
36833435
3684 return result;3436 break;
3685 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 }
36883462
3689 function activateSelectedResult() {3463 function activateSelectedResult() {
...@@ -3723,7 +3497,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {...@@ -3723,7 +3497,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
3723 startSearch();3497 startSearch();
3724 }3498 }
3725 }3499 }
3726 3500
37273501
3728 function onSearchKeyDown(ev) {3502 function onSearchKeyDown(ev) {
3729 switch (getKeyString(ev)) {3503 switch (getKeyString(ev)) {
...@@ -3824,7 +3598,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {...@@ -3824,7 +3598,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
3824 break;3598 break;
3825 case "/":3599 case "/":
3826 if (!getPrefSlashSearch()) break;3600 if (!getPrefSlashSearch()) break;
3827 // fallthrough3601 // fallthrough
3828 case "s":3602 case "s":
3829 if (!isModalVisible(domHelpModal) && !isModalVisible(domPrefsModal)) {3603 if (!isModalVisible(domHelpModal) && !isModalVisible(domPrefsModal)) {
3830 if (ev.target == domSearch) break;3604 if (ev.target == domSearch) break;
...@@ -3846,9 +3620,9 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {...@@ -3846,9 +3620,9 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
38463620
3847 // toggle the help modal3621 // toggle the help modal
3848 if (isModalVisible(domHelpModal)) {3622 if (isModalVisible(domHelpModal)) {
3849 hideModal(domHelpModal);3623 hideModal(domHelpModal);
3850 } else {3624 } else {
3851 showModal(domHelpModal);3625 showModal(domHelpModal);
3852 }3626 }
3853 ev.preventDefault();3627 ev.preventDefault();
3854 ev.stopPropagation();3628 ev.stopPropagation();
...@@ -3927,21 +3701,21 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {...@@ -3927,21 +3701,21 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
3927 function renderSearchGuides() {3701 function renderSearchGuides() {
3928 const searchTrimmed = false;3702 const searchTrimmed = false;
3929 let ignoreCase = curNavSearch.toLowerCase() === curNavSearch;3703 let ignoreCase = curNavSearch.toLowerCase() === curNavSearch;
3930 3704
3931 let terms = getSearchTerms();3705 let terms = getSearchTerms();
3932 let matchedItems = new Set();3706 let matchedItems = new Set();
39333707
3934 for (let i = 0; i < terms.length; i += 1) {3708 for (let i = 0; i < terms.length; i += 1) {
3935 const nodes = guidesSearchIndex[terms[i]];3709 const nodes = guidesSearchIndex[terms[i]];
3936 if (nodes) {3710 if (nodes) {
3937 for (const n of nodes) { 3711 for (const n of nodes) {
3938 matchedItems.add(n);3712 matchedItems.add(n);
3939 }3713 }
3940 }3714 }
3941 }3715 }
39423716
3943 3717
3944 3718
3945 if (matchedItems.size !== 0) {3719 if (matchedItems.size !== 0) {
3946 // Build up the list of search results3720 // Build up the list of search results
3947 let matchedItemsHTML = "";3721 let matchedItemsHTML = "";
...@@ -3966,14 +3740,14 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {...@@ -3966,14 +3740,14 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
3966 }3740 }
3967 }3741 }
39683742
3969 function renderSearchAPI(){3743 function renderSearchAPI() {
3970 if (canonDeclPaths == null) {3744 if (canonDeclPaths == null) {
3971 canonDeclPaths = computeCanonDeclPaths();3745 canonDeclPaths = computeCanonDeclPaths();
3972 }3746 }
3973 let declSet = new Set();3747 let declSet = new Set();
3974 let otherDeclSet = new Set(); // for low quality results3748 let otherDeclSet = new Set(); // for low quality results
3975 let declScores = {};3749 let declScores = {};
3976 3750
3977 let ignoreCase = curNavSearch.toLowerCase() === curNavSearch;3751 let ignoreCase = curNavSearch.toLowerCase() === curNavSearch;
3978 let term_list = getSearchTerms();3752 let term_list = getSearchTerms();
3979 for (let i = 0; i < term_list.length; i += 1) {3753 for (let i = 0; i < term_list.length; i += 1) {
...@@ -4004,7 +3778,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {...@@ -4004,7 +3778,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
4004 if (declSet.has(p)) {3778 if (declSet.has(p)) {
4005 found = true;3779 found = true;
4006 break;3780 break;
4007 } 3781 }
4008 }3782 }
4009 if (!found) {3783 if (!found) {
4010 otherDeclSet.add(d);3784 otherDeclSet.add(d);
...@@ -4012,9 +3786,9 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {...@@ -4012,9 +3786,9 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
4012 termSet.add(d);3786 termSet.add(d);
4013 }3787 }
4014 }3788 }
4015 3789
4016 if (declScores[d] == undefined) declScores[d] = 0;3790 if (declScores[d] == undefined) declScores[d] = 0;
4017 3791
4018 // scores (lower is better)3792 // scores (lower is better)
4019 let decl_name = decl.name;3793 let decl_name = decl.name;
4020 if (ignoreCase) decl_name = decl_name.toLowerCase();3794 if (ignoreCase) decl_name = decl_name.toLowerCase();
...@@ -4022,21 +3796,21 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {...@@ -4022,21 +3796,21 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
4022 // shallow path are preferable3796 // shallow path are preferable
4023 const path_depth = canonPath.declNames.length * 50;3797 const path_depth = canonPath.declNames.length * 50;
4024 // matching the start of a decl name is good3798 // 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 // being a perfect match is good3800 // being a perfect match is good
4027 const is_full_match = (decl_name === term) ? -decl_name.length * (1 - ignoreCase) : Math.abs(decl_name.length - term.length);3801 const is_full_match = (decl_name === term) ? -decl_name.length * (1 - ignoreCase) : Math.abs(decl_name.length - term.length);
4028 // matching the end of a decl name is good3802 // matching the end of a decl name is good
4029 const matches_the_end = decl_name.endsWith(term) ? -term.length * (1 - ignoreCase) : (decl_name.length - term.length) + 1;3803 const matches_the_end = decl_name.endsWith(term) ? -term.length * (1 - ignoreCase) : (decl_name.length - term.length) + 1;
4030 // explicitly penalizing scream case decls3804 // explicitly penalizing scream case decls
4031 const decl_is_scream_case = decl.name.toUpperCase() != decl.name ? 0 : decl.name.length; 3805 const decl_is_scream_case = decl.name.toUpperCase() != decl.name ? 0 : decl.name.length;
4032 3806
4033 const score = path_depth 3807 const score = path_depth
4034 + match_from_start 3808 + match_from_start
4035 + is_full_match 3809 + is_full_match
4036 + matches_the_end 3810 + matches_the_end
4037 + decl_is_scream_case;3811 + decl_is_scream_case;
40383812
4039 declScores[d] += score; 3813 declScores[d] += score;
4040 }3814 }
4041 }3815 }
4042 if (i != 0) {3816 if (i != 0) {
...@@ -4047,19 +3821,19 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {...@@ -4047,19 +3821,19 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
4047 if (termSet.has(p) || otherDeclSet.has(p)) {3821 if (termSet.has(p) || otherDeclSet.has(p)) {
4048 found = true;3822 found = true;
4049 break;3823 break;
4050 } 3824 }
4051 }3825 }
4052 if (found) {3826 if (found) {
4053 declScores[d] = declScores[d] / term_list.length;3827 declScores[d] = declScores[d] / term_list.length;
4054 }3828 }
4055 3829
4056 termOtherSet.add(d);3830 termOtherSet.add(d);
4057 }3831 }
4058 declSet = termSet;3832 declSet = termSet;
4059 for (let d of termOtherSet) {3833 for (let d of termOtherSet) {
4060 otherDeclSet.add(d);3834 otherDeclSet.add(d);
4061 }3835 }
4062 3836
4063 }3837 }
4064 }3838 }
40653839
...@@ -4068,21 +3842,21 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {...@@ -4068,21 +3842,21 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
4068 low_quality: [],3842 low_quality: [],
4069 };3843 };
4070 for (let idx of declSet) {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 for (let idx of otherDeclSet) {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 3850
4077 matchedItems.high_quality.sort(function (a, b) {3851 matchedItems.high_quality.sort(function(a, b) {
4078 let cmp = operatorCompare(a.points, b.points);3852 let cmp = operatorCompare(a.points, b.points);
4079 return cmp;3853 return cmp;
4080 });3854 });
4081 matchedItems.low_quality.sort(function (a, b) {3855 matchedItems.low_quality.sort(function(a, b) {
4082 let cmp = operatorCompare(a.points, b.points);3856 let cmp = operatorCompare(a.points, b.points);
4083 return cmp;3857 return cmp;
4084 });3858 });
4085 3859
4086 // Build up the list of search results3860 // Build up the list of search results
4087 let matchedItemsHTML = "";3861 let matchedItemsHTML = "";
40883862
...@@ -4099,7 +3873,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {...@@ -4099,7 +3873,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
40993873
4100 let lastModName = canonPath.modNames[canonPath.modNames.length - 1];3874 let lastModName = canonPath.modNames[canonPath.modNames.length - 1];
4101 let text = lastModName + "." + canonPath.declNames.join(".");3875 let text = lastModName + "." + canonPath.declNames.join(".");
4102 3876
41033877
4104 const href = navLink(canonPath.modNames, canonPath.declNames);3878 const href = navLink(canonPath.modNames, canonPath.declNames);
41053879
...@@ -4113,7 +3887,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {...@@ -4113,7 +3887,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
41133887
4114 domSectSearchResults.classList.remove("hidden");3888 domSectSearchResults.classList.remove("hidden");
4115 }3889 }
4116 3890
4117 function renderSearchAPIOld() {3891 function renderSearchAPIOld() {
4118 let matchedItems = [];3892 let matchedItems = [];
4119 let ignoreCase = curNavSearch.toLowerCase() === curNavSearch;3893 let ignoreCase = curNavSearch.toLowerCase() === curNavSearch;
...@@ -4179,7 +3953,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {...@@ -4179,7 +3953,7 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
4179 }3953 }
41803954
4181 if (matchedItems.length !== 0) {3955 if (matchedItems.length !== 0) {
4182 matchedItems.sort(function (a, b) {3956 matchedItems.sort(function(a, b) {
4183 let cmp = operatorCompare(b.points, a.points);3957 let cmp = operatorCompare(b.points, a.points);
4184 if (cmp != 0) return cmp;3958 if (cmp != 0) return cmp;
4185 return operatorCompare(a.decl.name, b.decl.name);3959 return operatorCompare(a.decl.name, b.decl.name);
...@@ -4485,12 +4259,12 @@ function toggleExpand(event) {...@@ -4485,12 +4259,12 @@ function toggleExpand(event) {
4485function RadixTree() {4259function RadixTree() {
4486 this.root = null;4260 this.root = null;
44874261
4488 RadixTree.prototype.search = function (query) {4262 RadixTree.prototype.search = function(query) {
4489 return this.root.search(query);4263 return this.root.search(query);
4490 4264
4491 }4265 }
44924266
4493 RadixTree.prototype.add = function (declName, value) {4267 RadixTree.prototype.add = function(declName, value) {
4494 if (this.root == null) {4268 if (this.root == null) {
4495 this.root = new Node(declName.toLowerCase(), null, [value]);4269 this.root = new Node(declName.toLowerCase(), null, [value]);
4496 } else {4270 } else {
...@@ -4499,7 +4273,7 @@ function RadixTree() {...@@ -4499,7 +4273,7 @@ function RadixTree() {
44994273
4500 const not_scream_case = declName.toUpperCase() != declName;4274 const not_scream_case = declName.toUpperCase() != declName;
4501 let found_separator = false;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 if (declName[i] == '_' || declName[i] == '.') {4277 if (declName[i] == '_' || declName[i] == '.') {
4504 found_separator = true;4278 found_separator = true;
4505 continue;4279 continue;
...@@ -4507,42 +4281,42 @@ function RadixTree() {...@@ -4507,42 +4281,42 @@ function RadixTree() {
45074281
45084282
4509 if (found_separator || (declName[i].toLowerCase() !== declName[i])) {4283 if (found_separator || (declName[i].toLowerCase() !== declName[i])) {
4510 if (declName.length > i+1 4284 if (declName.length > i + 1
4511 && declName[i+1].toLowerCase() != declName[i+1]) continue;4285 && declName[i + 1].toLowerCase() != declName[i + 1]) continue;
4512 let suffix = declName.slice(i);4286 let suffix = declName.slice(i);
4513 this.root.add(suffix.toLowerCase(), value);4287 this.root.add(suffix.toLowerCase(), value);
4514 found_separator = false;4288 found_separator = false;
4515 }4289 }
4516 }4290 }
4517 }4291 }
4518 4292
4519 function Node(labels, next, values) {4293 function Node(labels, next, values) {
4520 this.labels = labels; 4294 this.labels = labels;
4521 this.next = next;4295 this.next = next;
4522 this.values = values;4296 this.values = values;
4523 }4297 }
45244298
4525 Node.prototype.isCompressed = function () {4299 Node.prototype.isCompressed = function() {
4526 return !Array.isArray(this.next);4300 return !Array.isArray(this.next);
4527 }4301 }
45284302
4529 Node.prototype.search = function (word) {4303 Node.prototype.search = function(word) {
4530 let full_matches = [];4304 let full_matches = [];
4531 let partial_matches = [];4305 let partial_matches = [];
4532 let subtree_root = null;4306 let subtree_root = null;
4533 4307
4534 let cn = this;4308 let cn = this;
4535 char_loop: for (let i = 0; i < word.length;) {4309 char_loop: for (let i = 0; i < word.length;) {
4536 if (cn.isCompressed()) {4310 if (cn.isCompressed()) {
4537 for (let j = 0; j < cn.labels.length; j += 1) {4311 for (let j = 0; j < cn.labels.length; j += 1) {
4538 let current_idx = i+j;4312 let current_idx = i + j;
45394313
4540 if (current_idx == word.length) {4314 if (current_idx == word.length) {
4541 partial_matches = cn.values;4315 partial_matches = cn.values;
4542 subtree_root = cn.next;4316 subtree_root = cn.next;
4543 break char_loop; 4317 break char_loop;
4544 }4318 }
4545 4319
4546 if (word[current_idx] != cn.labels[j]) return null;4320 if (word[current_idx] != cn.labels[j]) return null;
4547 }4321 }
45484322
...@@ -4553,33 +4327,33 @@ function RadixTree() {...@@ -4553,33 +4327,33 @@ function RadixTree() {
4553 subtree_root = cn.next;4327 subtree_root = cn.next;
4554 break char_loop;4328 break char_loop;
4555 }4329 }
4556 4330
4557 4331
4558 i = new_idx;4332 i = new_idx;
4559 cn = cn.next;4333 cn = cn.next;
4560 continue;4334 continue;
4561 } else {4335 } else {
4562 for (let j = 0; j < cn.labels.length; j += 1) {4336 for (let j = 0; j < cn.labels.length; j += 1) {
4563 if (word[i] == cn.labels[j]) {4337 if (word[i] == cn.labels[j]) {
4564 if (i == word.length - 1) { 4338 if (i == word.length - 1) {
4565 full_matches = cn.values[j];4339 full_matches = cn.values[j];
4566 subtree_root = cn.next[j];4340 subtree_root = cn.next[j];
4567 break char_loop;4341 break char_loop;
4568 }4342 }
4569 4343
4570 let next = cn.next[j];4344 let next = cn.next[j];
4571 if (next == null) return null;4345 if (next == null) return null;
4572 cn = next;4346 cn = next;
4573 i += 1;4347 i += 1;
4574 continue char_loop; 4348 continue char_loop;
4575 } 4349 }
4576 }4350 }
4577 4351
4578 // didn't find a match4352 // didn't find a match
4579 return null;4353 return null;
4580 }4354 }
4581 }4355 }
4582 4356
4583 // Match was found, let's collect all other 4357 // Match was found, let's collect all other
4584 // partial matches from the subtree4358 // partial matches from the subtree
4585 let stack = [subtree_root];4359 let stack = [subtree_root];
...@@ -4594,22 +4368,22 @@ function RadixTree() {...@@ -4594,22 +4368,22 @@ function RadixTree() {
4594 for (let v of node.values) {4368 for (let v of node.values) {
4595 partial_matches = partial_matches.concat(v);4369 partial_matches = partial_matches.concat(v);
4596 }4370 }
4597 4371
4598 for (let n of node.next) {4372 for (let n of node.next) {
4599 if (n != null) stack.push(n);4373 if (n != null) stack.push(n);
4600 }4374 }
4601 }4375 }
4602 }4376 }
46034377
4604 return {full: full_matches, partial: partial_matches};4378 return { full: full_matches, partial: partial_matches };
4605 }4379 }
46064380
4607 Node.prototype.add = function (word, value) {4381 Node.prototype.add = function(word, value) {
4608 let cn = this;4382 let cn = this;
4609 char_loop: for (let i = 0; i < word.length;) {4383 char_loop: for (let i = 0; i < word.length;) {
4610 if (cn.isCompressed()) {4384 if (cn.isCompressed()) {
4611 for(let j = 0; j < cn.labels.length; j += 1) {4385 for (let j = 0; j < cn.labels.length; j += 1) {
4612 let current_idx = i+j;4386 let current_idx = i + j;
46134387
4614 if (current_idx == word.length) {4388 if (current_idx == word.length) {
4615 if (j < cn.labels.length - 1) {4389 if (j < cn.labels.length - 1) {
...@@ -4621,13 +4395,13 @@ function RadixTree() {...@@ -4621,13 +4395,13 @@ function RadixTree() {
4621 cn.values.push(value);4395 cn.values.push(value);
4622 return;4396 return;
4623 }4397 }
4624 4398
4625 if (word[current_idx] == cn.labels[j]) continue;4399 if (word[current_idx] == cn.labels[j]) continue;
4626 4400
4627 // if we're here, a mismatch was found4401 // if we're here, a mismatch was found
4628 if (j != cn.labels.length - 1) {4402 if (j != cn.labels.length - 1) {
4629 // create a suffix node4403 // create a suffix node
4630 const label_suffix = cn.labels.slice(j+1);4404 const label_suffix = cn.labels.slice(j + 1);
4631 let node = new Node(label_suffix, cn.next, [...cn.values]);4405 let node = new Node(label_suffix, cn.next, [...cn.values]);
4632 cn.next = node;4406 cn.next = node;
4633 cn.values = [];4407 cn.values = [];
...@@ -4641,11 +4415,11 @@ function RadixTree() {...@@ -4641,11 +4415,11 @@ function RadixTree() {
4641 // meaning that the current node should hold its value4415 // meaning that the current node should hold its value
4642 word_values.push(value);4416 word_values.push(value);
4643 } else {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 cn.labels = cn.labels[j] + word[current_idx];4421 cn.labels = cn.labels[j] + word[current_idx];
4648 cn.next = [cn.next, node]; 4422 cn.next = [cn.next, node];
4649 cn.values = [cn.values, word_values];4423 cn.values = [cn.values, word_values];
46504424
4651 if (j != 0) {4425 if (j != 0) {
...@@ -4657,7 +4431,7 @@ function RadixTree() {...@@ -4657,7 +4431,7 @@ function RadixTree() {
4657 }4431 }
46584432
4659 return;4433 return;
4660 } 4434 }
4661 // label matched fully with word, are there any more chars?4435 // label matched fully with word, are there any more chars?
4662 const new_idx = i + cn.labels.length;4436 const new_idx = i + cn.labels.length;
4663 if (new_idx == word.length) {4437 if (new_idx == word.length) {
...@@ -4673,7 +4447,7 @@ function RadixTree() {...@@ -4673,7 +4447,7 @@ function RadixTree() {
4673 i = new_idx;4447 i = new_idx;
4674 continue;4448 continue;
4675 }4449 }
4676 } 4450 }
4677 } else { // node is not compressed4451 } else { // node is not compressed
4678 let letter = word[i];4452 let letter = word[i];
4679 for (let j = 0; j < cn.labels.length; j += 1) {4453 for (let j = 0; j < cn.labels.length; j += 1) {
...@@ -4683,7 +4457,7 @@ function RadixTree() {...@@ -4683,7 +4457,7 @@ function RadixTree() {
4683 return;4457 return;
4684 }4458 }
4685 if (cn.next[j] == null) {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 cn.next[j] = node;4461 cn.next[j] = node;
4688 return;4462 return;
4689 } else {4463 } else {
...@@ -4700,7 +4474,7 @@ function RadixTree() {...@@ -4700,7 +4474,7 @@ function RadixTree() {
4700 cn.next.push(null);4474 cn.next.push(null);
4701 cn.values.push([value]);4475 cn.values.push([value]);
4702 } else {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 cn.next.push(node);4478 cn.next.push(node);
4705 cn.values.push([]);4479 cn.values.push([]);
4706 }4480 }
lib/docs/ziglexer.js+225-190
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1'use strict';1'use strict';
22
3const Tag = {3const Tag = {
4 whitespace: "whitespace",
4 invalid: "invalid",5 invalid: "invalid",
5 identifier: "identifier",6 identifier: "identifier",
6 string_literal: "string_literal",7 string_literal: "string_literal",
...@@ -125,6 +126,27 @@ const Tag = {...@@ -125,6 +126,27 @@ const Tag = {
125 keyword_while: "keyword_while"126 keyword_while: "keyword_while"
126}127}
127128
129const Tok = {
130 const: { src: "const", tag: Tag.keyword_const },
131 var: { src: "var", tag: Tag.keyword_var },
132 colon: { src: ":", tag: Tag.colon },
133 eql: { src: "=", tag: Tag.equals },
134 space: { src: " ", tag: Tag.whitespace },
135 tab: { src: " ", tag: Tag.whitespace },
136 enter: { src: "\n", tag: Tag.whitespace },
137 semi: { src: ";", tag: Tag.semicolon },
138 l_bracket: { src: "[", tag: Tag.l_bracket },
139 r_bracket: { src: "]", tag: Tag.r_bracket },
140 l_brace: { src: "{", tag: Tag.l_brace },
141 r_brace: { src: "}", tag: Tag.r_brace },
142 l_paren: { src: "(", tag: Tag.l_paren },
143 r_paren: { src: ")", tag: Tag.r_paren },
144 period: { src: ".", tag: Tag.period },
145 comma: { src: ",", tag: Tag.comma },
146 identifier: (name) => { return { src: name, tag: Tag.identifier } },
147};
148
149
128const State = {150const State = {
129 start: 0,151 start: 0,
130 identifier: 1,152 identifier: 1,
...@@ -175,6 +197,7 @@ const State = {...@@ -175,6 +197,7 @@ const State = {
175 period_2: 46,197 period_2: 46,
176 period_asterisk: 47,198 period_asterisk: 47,
177 saw_at_sign: 48,199 saw_at_sign: 48,
200 whitespace: 49,
178}201}
179202
180const keywords = {203const keywords = {
...@@ -256,25 +279,36 @@ function dump_tokens(tokens, raw_source) {...@@ -256,25 +279,36 @@ function dump_tokens(tokens, raw_source) {
256 }279 }
257}280}
258281
282function* Tokenizer(raw_source) {
283 let tokenizer = new InnerTokenizer(raw_source);
284 while (true) {
285 let t = tokenizer.next();
286 if (t.tag == Tag.eof)
287 return;
288
289 t.src = raw_source.slice(t.loc.start, t.loc.end);
290
291 yield t;
292 }
259293
294}
295function InnerTokenizer(raw_source) {
296 this.index = 0;
297 this.flag = false;
260298
261function tokenize_zig_source(raw_source) {299 this.seen_escape_digits = undefined;
262300 this.remaining_code_units = undefined;
263 var index = 0;
264 var flag = false;
265
266 let seen_escape_digits = undefined;
267 let remaining_code_units = undefined;
268301
269 const next = () => {302 this.next = () => {
270 let state = State.start;303 let state = State.start;
271304
272 var result = {305 var result = {
273 tag: -1,306 tag: -1,
274 loc: {307 loc: {
275 start: index,308 start: this.index,
276 end: undefined,309 end: undefined,
277 },310 },
311 src: undefined,
278 };312 };
279313
280 //having a while (true) loop seems like a bad idea the loop should never314 //having a while (true) loop seems like a bad idea the loop should never
...@@ -284,37 +318,39 @@ function tokenize_zig_source(raw_source) {...@@ -284,37 +318,39 @@ function tokenize_zig_source(raw_source) {
284318
285 while (iterations <= MAX_ITERATIONS) {319 while (iterations <= MAX_ITERATIONS) {
286320
287 if (flag) {321 if (this.flag) {
288 return make_token(Tag.eof, index - 2, index - 2);322 return make_token(Tag.eof, this.index - 2, this.index - 2);
289 }323 }
290 iterations += 1; // avoid death loops324 iterations += 1; // avoid death loops
291325
292 var c = raw_source[index];326 var c = raw_source[this.index];
293327
294 if (c === undefined) {328 if (c === undefined) {
295 c = ' '; // push the last token329 c = ' '; // push the last token
296 flag = true;330 this.flag = true;
297 }331 }
298332
299 switch (state) {333 switch (state) {
300 case State.start:334 case State.start:
301 switch (c) {335 switch (c) {
302 case 0: {336 case 0: {
303 if (index != raw_source.length) {337 if (this.index != raw_source.length) {
304 result.tag = Tag.invalid;338 result.tag = Tag.invalid;
305 result.loc.start = index;339 result.loc.start = this.index;
306 index += 1;340 this.index += 1;
307 result.loc.end = index;341 result.loc.end = this.index;
308 return result;342 return result;
309 }343 }
310 result.loc.end = index;344 result.loc.end = this.index;
311 return result;345 return result;
312 }346 }
313 case ' ':347 case ' ':
314 case '\n':348 case '\n':
315 case '\t':349 case '\t':
316 case '\r': {350 case '\r': {
317 result.loc.start = index + 1;351 state = State.whitespace;
352 result.tag = Tag.whitespace;
353 result.loc.start = this.index;
318 break;354 break;
319 }355 }
320 case '"': {356 case '"': {
...@@ -401,51 +437,51 @@ function tokenize_zig_source(raw_source) {...@@ -401,51 +437,51 @@ function tokenize_zig_source(raw_source) {
401 }437 }
402 case '(': {438 case '(': {
403 result.tag = Tag.l_paren;439 result.tag = Tag.l_paren;
404 index += 1;440 this.index += 1;
405 result.loc.end = index;441 result.loc.end = this.index;
406442
407 return result;443 return result;
408 444
409 }445 }
410 case ')': {446 case ')': {
411 result.tag = Tag.r_paren;447 result.tag = Tag.r_paren;
412 index += 1; result.loc.end = index;448 this.index += 1; result.loc.end = this.index;
413 return result;449 return result;
414 450
415 }451 }
416 case '[': {452 case '[': {
417 result.tag = Tag.l_bracket;453 result.tag = Tag.l_bracket;
418 index += 1; result.loc.end = index;454 this.index += 1; result.loc.end = this.index;
419 return result;455 return result;
420 456
421 }457 }
422 case ']': {458 case ']': {
423 result.tag = Tag.r_bracket;459 result.tag = Tag.r_bracket;
424 index += 1; result.loc.end = index;460 this.index += 1; result.loc.end = this.index;
425 return result;461 return result;
426 462
427 }463 }
428 case ';': {464 case ';': {
429 result.tag = Tag.semicolon;465 result.tag = Tag.semicolon;
430 index += 1; result.loc.end = index;466 this.index += 1; result.loc.end = this.index;
431 return result;467 return result;
432 468
433 }469 }
434 case ',': {470 case ',': {
435 result.tag = Tag.comma;471 result.tag = Tag.comma;
436 index += 1; result.loc.end = index;472 this.index += 1; result.loc.end = this.index;
437 return result;473 return result;
438 474
439 }475 }
440 case '?': {476 case '?': {
441 result.tag = Tag.question_mark;477 result.tag = Tag.question_mark;
442 index += 1; result.loc.end = index;478 this.index += 1; result.loc.end = this.index;
443 return result;479 return result;
444 480
445 }481 }
446 case ':': {482 case ':': {
447 result.tag = Tag.colon;483 result.tag = Tag.colon;
448 index += 1; result.loc.end = index;484 this.index += 1; result.loc.end = this.index;
449 return result;485 return result;
450 486
451 }487 }
...@@ -473,19 +509,19 @@ function tokenize_zig_source(raw_source) {...@@ -473,19 +509,19 @@ function tokenize_zig_source(raw_source) {
473 }509 }
474 case '{': {510 case '{': {
475 result.tag = Tag.l_brace;511 result.tag = Tag.l_brace;
476 index += 1; result.loc.end = index;512 this.index += 1; result.loc.end = this.index;
477 return result;513 return result;
478 514
479 }515 }
480 case '}': {516 case '}': {
481 result.tag = Tag.r_brace;517 result.tag = Tag.r_brace;
482 index += 1; result.loc.end = index;518 this.index += 1; result.loc.end = this.index;
483 return result;519 return result;
484 520
485 }521 }
486 case '~': {522 case '~': {
487 result.tag = Tag.tilde;523 result.tag = Tag.tilde;
488 index += 1; result.loc.end = index;524 this.index += 1; result.loc.end = this.index;
489 return result;525 return result;
490 526
491 }527 }
...@@ -517,8 +553,8 @@ function tokenize_zig_source(raw_source) {...@@ -517,8 +553,8 @@ function tokenize_zig_source(raw_source) {
517 }553 }
518 default: {554 default: {
519 result.tag = Tag.invalid;555 result.tag = Tag.invalid;
520 result.loc.end = index;556 result.loc.end = this.index;
521 index += 1;557 this.index += 1;
522 return result;558 return result;
523 }559 }
524 }560 }
...@@ -588,7 +624,7 @@ function tokenize_zig_source(raw_source) {...@@ -588,7 +624,7 @@ function tokenize_zig_source(raw_source) {
588 }624 }
589 default: {625 default: {
590 result.tag = Tag.invalid;626 result.tag = Tag.invalid;
591 result.loc.end = index;627 result.loc.end = this.index;
592 return result;628 return result;
593 }629 }
594 }630 }
...@@ -597,11 +633,11 @@ function tokenize_zig_source(raw_source) {...@@ -597,11 +633,11 @@ function tokenize_zig_source(raw_source) {
597 switch (c) {633 switch (c) {
598 case '=': {634 case '=': {
599 result.tag = Tag.ampersand_equal;635 result.tag = Tag.ampersand_equal;
600 index += 1; result.loc.end = index;636 this.index += 1; result.loc.end = this.index;
601 return result;637 return result;
602 }638 }
603 default: {639 default: {
604 result.tag = Tag.ampersand; result.loc.end = index;640 result.tag = Tag.ampersand; result.loc.end = this.index;
605 return result;641 return result;
606 }642 }
607 }643 }
...@@ -609,12 +645,12 @@ function tokenize_zig_source(raw_source) {...@@ -609,12 +645,12 @@ function tokenize_zig_source(raw_source) {
609 case State.asterisk: switch (c) {645 case State.asterisk: switch (c) {
610 case '=': {646 case '=': {
611 result.tag = Tag.asterisk_equal;647 result.tag = Tag.asterisk_equal;
612 index += 1; result.loc.end = index;648 this.index += 1; result.loc.end = this.index;
613 return result;649 return result;
614 }650 }
615 case '*': {651 case '*': {
616 result.tag = Tag.asterisk_asterisk;652 result.tag = Tag.asterisk_asterisk;
617 index += 1; result.loc.end = index;653 this.index += 1; result.loc.end = this.index;
618 return result;654 return result;
619 }655 }
620 case '%': {656 case '%': {
...@@ -625,7 +661,7 @@ function tokenize_zig_source(raw_source) {...@@ -625,7 +661,7 @@ function tokenize_zig_source(raw_source) {
625 }661 }
626 default: {662 default: {
627 result.tag = Tag.asterisk;663 result.tag = Tag.asterisk;
628 result.loc.end = index;664 result.loc.end = this.index;
629 return result;665 return result;
630 }666 }
631 }667 }
...@@ -634,12 +670,12 @@ function tokenize_zig_source(raw_source) {...@@ -634,12 +670,12 @@ function tokenize_zig_source(raw_source) {
634 switch (c) {670 switch (c) {
635 case '=': {671 case '=': {
636 result.tag = Tag.asterisk_percent_equal;672 result.tag = Tag.asterisk_percent_equal;
637 index += 1; result.loc.end = index;673 this.index += 1; result.loc.end = this.index;
638 return result;674 return result;
639 }675 }
640 default: {676 default: {
641 result.tag = Tag.asterisk_percent;677 result.tag = Tag.asterisk_percent;
642 result.loc.end = index;678 result.loc.end = this.index;
643 return result; 679 return result;
644 }680 }
645 }681 }
...@@ -648,11 +684,11 @@ function tokenize_zig_source(raw_source) {...@@ -648,11 +684,11 @@ function tokenize_zig_source(raw_source) {
648 switch (c) {684 switch (c) {
649 case '=': {685 case '=': {
650 result.tag = Tag.asterisk_pipe_equal;686 result.tag = Tag.asterisk_pipe_equal;
651 index += 1; result.loc.end = index;687 this.index += 1; result.loc.end = this.index;
652 return result;688 return result;
653 }689 }
654 default: {690 default: {
655 result.tag = Tag.asterisk_pipe; result.loc.end = index;691 result.tag = Tag.asterisk_pipe; result.loc.end = this.index;
656 return result;692 return result;
657 }693 }
658 }694 }
...@@ -661,11 +697,11 @@ function tokenize_zig_source(raw_source) {...@@ -661,11 +697,11 @@ function tokenize_zig_source(raw_source) {
661 switch (c) {697 switch (c) {
662 case '=': {698 case '=': {
663 result.tag = Tag.percent_equal;699 result.tag = Tag.percent_equal;
664 index += 1; result.loc.end = index;700 this.index += 1; result.loc.end = this.index;
665 return result;701 return result;
666 }702 }
667 default: {703 default: {
668 result.tag = Tag.percent; result.loc.end = index;704 result.tag = Tag.percent; result.loc.end = this.index;
669 return result;705 return result;
670 }706 }
671 }707 }
...@@ -674,12 +710,12 @@ function tokenize_zig_source(raw_source) {...@@ -674,12 +710,12 @@ function tokenize_zig_source(raw_source) {
674 switch (c) {710 switch (c) {
675 case '=': {711 case '=': {
676 result.tag = Tag.plus_equal;712 result.tag = Tag.plus_equal;
677 index += 1; result.loc.end = index;713 this.index += 1; result.loc.end = this.index;
678 return result;714 return result;
679 }715 }
680 case '+': {716 case '+': {
681 result.tag = Tag.plus_plus;717 result.tag = Tag.plus_plus;
682 index += 1; result.loc.end = index;718 this.index += 1; result.loc.end = this.index;
683 return result;719 return result;
684 }720 }
685 case '%': {721 case '%': {
...@@ -689,7 +725,7 @@ function tokenize_zig_source(raw_source) {...@@ -689,7 +725,7 @@ function tokenize_zig_source(raw_source) {
689 state = State.plus_pipe; break;725 state = State.plus_pipe; break;
690 }726 }
691 default: {727 default: {
692 result.tag = Tag.plus; result.loc.end = index;728 result.tag = Tag.plus; result.loc.end = this.index;
693 return result;729 return result;
694 }730 }
695 }731 }
...@@ -698,11 +734,11 @@ function tokenize_zig_source(raw_source) {...@@ -698,11 +734,11 @@ function tokenize_zig_source(raw_source) {
698 switch (c) {734 switch (c) {
699 case '=': {735 case '=': {
700 result.tag = Tag.plus_percent_equal;736 result.tag = Tag.plus_percent_equal;
701 index += 1; result.loc.end = index;737 this.index += 1; result.loc.end = this.index;
702 return result;738 return result;
703 }739 }
704 default: {740 default: {
705 result.tag = Tag.plus_percent; result.loc.end = index;741 result.tag = Tag.plus_percent; result.loc.end = this.index;
706 return result;742 return result;
707 }743 }
708 }744 }
...@@ -711,11 +747,11 @@ function tokenize_zig_source(raw_source) {...@@ -711,11 +747,11 @@ function tokenize_zig_source(raw_source) {
711 switch (c) {747 switch (c) {
712 case '=': {748 case '=': {
713 result.tag = Tag.plus_pipe_equal;749 result.tag = Tag.plus_pipe_equal;
714 index += 1; result.loc.end = index;750 this.index += 1; result.loc.end = this.index;
715 return result;751 return result;
716 }752 }
717 default: {753 default: {
718 result.tag = Tag.plus_pipe; result.loc.end = index;754 result.tag = Tag.plus_pipe; result.loc.end = this.index;
719 return result;755 return result;
720 }756 }
721 }757 }
...@@ -724,11 +760,11 @@ function tokenize_zig_source(raw_source) {...@@ -724,11 +760,11 @@ function tokenize_zig_source(raw_source) {
724 switch (c) {760 switch (c) {
725 case '=': {761 case '=': {
726 result.tag = Tag.caret_equal;762 result.tag = Tag.caret_equal;
727 index += 1; result.loc.end = index;763 this.index += 1; result.loc.end = this.index;
728 return result;764 return result;
729 }765 }
730 default: {766 default: {
731 result.tag = Tag.caret; result.loc.end = index;767 result.tag = Tag.caret; result.loc.end = this.index;
732 return result;768 return result;
733 }769 }
734 }770 }
...@@ -799,12 +835,12 @@ function tokenize_zig_source(raw_source) {...@@ -799,12 +835,12 @@ function tokenize_zig_source(raw_source) {
799 case '8':835 case '8':
800 case '9': break;836 case '9': break;
801 default: {837 default: {
802 // if (Token.getKeyword(buffer[result.loc.start..index])) | tag | {838 // if (Token.getKeyword(buffer[result.loc.start..this.index])) | tag | {
803 const z = raw_source.substring(result.loc.start, index);839 const z = raw_source.substring(result.loc.start, this.index);
804 if (z in keywords) {840 if (z in keywords) {
805 result.tag = keywords[z];841 result.tag = keywords[z];
806 }842 }
807 result.loc.end = index;843 result.loc.end = this.index;
808 return result; 844 return result;
809 }845 }
810846
...@@ -875,7 +911,7 @@ function tokenize_zig_source(raw_source) {...@@ -875,7 +911,7 @@ function tokenize_zig_source(raw_source) {
875 case '7':911 case '7':
876 case '8':912 case '8':
877 case '9': break;913 case '9': break;
878 default: result.loc.end = index;914 default: result.loc.end = this.index;
879 return result;915 return result;
880 }916 }
881 break;917 break;
...@@ -887,7 +923,7 @@ function tokenize_zig_source(raw_source) {...@@ -887,7 +923,7 @@ function tokenize_zig_source(raw_source) {
887 }923 }
888 default: {924 default: {
889 result.tag = Tag.invalid;925 result.tag = Tag.invalid;
890 result.loc.end = index;926 result.loc.end = this.index;
891 return result;927 return result;
892 }928 }
893 }929 }
...@@ -898,25 +934,25 @@ function tokenize_zig_source(raw_source) {...@@ -898,25 +934,25 @@ function tokenize_zig_source(raw_source) {
898 state = State.string_literal_backslash; break;934 state = State.string_literal_backslash; break;
899 }935 }
900 case '"': {936 case '"': {
901 index += 1;937 this.index += 1;
902 result.loc.end = index;938 result.loc.end = this.index;
903939
904 return result; 940 return result;
905 }941 }
906 case 0: {942 case 0: {
907 //TODO: PORT943 //TODO: PORT
908 // if (index == buffer.len) {944 // if (this.index == buffer.len) {
909 // result.tag = .invalid;945 // result.tag = .invalid;
910 // break;946 // break;
911 // } else {947 // } else {
912 // checkLiteralCharacter();948 // checkLiteralCharacter();
913 // }949 // }
914 result.loc.end = index;950 result.loc.end = this.index;
915 return result; 951 return result;
916 }952 }
917 case '\n': {953 case '\n': {
918 result.tag = Tag.invalid;954 result.tag = Tag.invalid;
919 result.loc.end = index;955 result.loc.end = this.index;
920 return result; 956 return result;
921 }957 }
922 //TODO: PORT958 //TODO: PORT
...@@ -928,7 +964,7 @@ function tokenize_zig_source(raw_source) {...@@ -928,7 +964,7 @@ function tokenize_zig_source(raw_source) {
928 case 0:964 case 0:
929 case '\n': {965 case '\n': {
930 result.tag = Tag.invalid;966 result.tag = Tag.invalid;
931 result.loc.end = index;967 result.loc.end = this.index;
932 return result; 968 return result;
933 }969 }
934 default: {970 default: {
...@@ -939,7 +975,7 @@ function tokenize_zig_source(raw_source) {...@@ -939,7 +975,7 @@ function tokenize_zig_source(raw_source) {
939 case State.char_literal: switch (c) {975 case State.char_literal: switch (c) {
940 case 0: {976 case 0: {
941 result.tag = Tag.invalid;977 result.tag = Tag.invalid;
942 result.loc.end = index;978 result.loc.end = this.index;
943 return result; 979 return result;
944 }980 }
945 case '\\': {981 case '\\': {
...@@ -952,15 +988,15 @@ function tokenize_zig_source(raw_source) {...@@ -952,15 +988,15 @@ function tokenize_zig_source(raw_source) {
952 // break;988 // break;
953 // },989 // },
954 // 0xc0...0xdf => { // 110xxxxx990 // 0xc0...0xdf => { // 110xxxxx
955 // remaining_code_units = 1;991 // this.remaining_code_units = 1;
956 // state = .char_literal_unicode;992 // state = .char_literal_unicode;
957 // },993 // },
958 // 0xe0...0xef => { // 1110xxxx994 // 0xe0...0xef => { // 1110xxxx
959 // remaining_code_units = 2;995 // this.remaining_code_units = 2;
960 // state = .char_literal_unicode;996 // state = .char_literal_unicode;
961 // },997 // },
962 // 0xf0...0xf7 => { // 11110xxx998 // 0xf0...0xf7 => { // 11110xxx
963 // remaining_code_units = 3;999 // this.remaining_code_units = 3;
964 // state = .char_literal_unicode;1000 // state = .char_literal_unicode;
965 // },1001 // },
9661002
...@@ -1070,7 +1106,7 @@ function tokenize_zig_source(raw_source) {...@@ -1070,7 +1106,7 @@ function tokenize_zig_source(raw_source) {
1070 // case 0xdd:1106 // case 0xdd:
1071 // case 0xde:1107 // case 0xde:
1072 // case 0xdf:1108 // case 0xdf:
1073 // remaining_code_units = 1;1109 // this.remaining_code_units = 1;
1074 // state = .char_literal_unicode;1110 // state = .char_literal_unicode;
1075 // case 0xe0:1111 // case 0xe0:
1076 // case 0xe1:1112 // case 0xe1:
...@@ -1088,7 +1124,7 @@ function tokenize_zig_source(raw_source) {...@@ -1088,7 +1124,7 @@ function tokenize_zig_source(raw_source) {
1088 // case 0xed:1124 // case 0xed:
1089 // case 0xee:1125 // case 0xee:
1090 // case 0xef:1126 // case 0xef:
1091 // remaining_code_units = 2;1127 // this.remaining_code_units = 2;
1092 // state = .char_literal_unicode;1128 // state = .char_literal_unicode;
1093 // case 0xf0:1129 // case 0xf0:
1094 // case 0xf1:1130 // case 0xf1:
...@@ -1098,12 +1134,12 @@ function tokenize_zig_source(raw_source) {...@@ -1098,12 +1134,12 @@ function tokenize_zig_source(raw_source) {
1098 // case 0xf5:1134 // case 0xf5:
1099 // case 0xf6:1135 // case 0xf6:
1100 // case 0xf7:1136 // case 0xf7:
1101 // remaining_code_units = 3;1137 // this.remaining_code_units = 3;
1102 // state = .char_literal_unicode;1138 // state = .char_literal_unicode;
11031139
1104 case '\n': {1140 case '\n': {
1105 result.tag = Tag.invalid;1141 result.tag = Tag.invalid;
1106 result.loc.end = index;1142 result.loc.end = this.index;
1107 return result; 1143 return result;
1108 }1144 }
1109 default: {1145 default: {
...@@ -1116,12 +1152,12 @@ function tokenize_zig_source(raw_source) {...@@ -1116,12 +1152,12 @@ function tokenize_zig_source(raw_source) {
1116 case 0:1152 case 0:
1117 case '\n': {1153 case '\n': {
1118 result.tag = Tag.invalid;1154 result.tag = Tag.invalid;
1119 result.loc.end = index;1155 result.loc.end = this.index;
1120 return result; 1156 return result;
1121 }1157 }
1122 case 'x': {1158 case 'x': {
1123 state = State.char_literal_hex_escape;1159 state = State.char_literal_hex_escape;
1124 seen_escape_digits = 0; break;1160 this.seen_escape_digits = 0; break;
1125 }1161 }
1126 case 'u': {1162 case 'u': {
1127 state = State.char_literal_unicode_escape_saw_u; break;1163 state = State.char_literal_unicode_escape_saw_u; break;
...@@ -1155,14 +1191,14 @@ function tokenize_zig_source(raw_source) {...@@ -1155,14 +1191,14 @@ function tokenize_zig_source(raw_source) {
1155 case 'D':1191 case 'D':
1156 case 'E':1192 case 'E':
1157 case 'F': {1193 case 'F': {
1158 seen_escape_digits += 1;1194 this.seen_escape_digits += 1;
1159 if (seen_escape_digits == 2) {1195 if (this.seen_escape_digits == 2) {
1160 state = State.char_literal_end;1196 state = State.char_literal_end;
1161 } break;1197 } break;
1162 }1198 }
1163 default: {1199 default: {
1164 result.tag = Tag.invalid;1200 result.tag = Tag.invalid;
1165 esult.loc.end = index;1201 esult.loc.end = this.index;
1166 return result;1202 return result;
1167 }1203 }
1168 }1204 }
...@@ -1171,7 +1207,7 @@ function tokenize_zig_source(raw_source) {...@@ -1171,7 +1207,7 @@ function tokenize_zig_source(raw_source) {
1171 switch (c) {1207 switch (c) {
1172 case 0: {1208 case 0: {
1173 result.tag = Tag.invalid;1209 result.tag = Tag.invalid;
1174 result.loc.end = index;1210 result.loc.end = this.index;
1175 return result;1211 return result;
1176 }1212 }
1177 case '{': {1213 case '{': {
...@@ -1187,7 +1223,7 @@ function tokenize_zig_source(raw_source) {...@@ -1187,7 +1223,7 @@ function tokenize_zig_source(raw_source) {
1187 switch (c) {1223 switch (c) {
1188 case 0: {1224 case 0: {
1189 result.tag = Tag.invalid;1225 result.tag = Tag.invalid;
1190 result.loc.end = index;1226 result.loc.end = this.index;
1191 return result;1227 return result;
1192 }1228 }
1193 case '0':1229 case '0':
...@@ -1297,13 +1333,13 @@ function tokenize_zig_source(raw_source) {...@@ -1297,13 +1333,13 @@ function tokenize_zig_source(raw_source) {
1297 switch (c) {1333 switch (c) {
1298 case '\'': {1334 case '\'': {
1299 result.tag = Tag.char_literal;1335 result.tag = Tag.char_literal;
1300 index += 1;1336 this.index += 1;
1301 result.loc.end = index;1337 result.loc.end = this.index;
1302 return result; 1338 return result;
1303 }1339 }
1304 default: {1340 default: {
1305 result.tag = Tag.invalid;1341 result.tag = Tag.invalid;
1306 result.loc.end = index;1342 result.loc.end = this.index;
1307 return result; 1343 return result;
1308 }1344 }
1309 }1345 }
...@@ -1311,14 +1347,14 @@ function tokenize_zig_source(raw_source) {...@@ -1311,14 +1347,14 @@ function tokenize_zig_source(raw_source) {
1311 case State.char_literal_unicode:1347 case State.char_literal_unicode:
1312 switch (c) {1348 switch (c) {
1313 // 0x80...0xbf => {1349 // 0x80...0xbf => {
1314 // remaining_code_units -= 1;1350 // this.remaining_code_units -= 1;
1315 // if (remaining_code_units == 0) {1351 // if (this.remaining_code_units == 0) {
1316 // state = .char_literal_end;1352 // state = .char_literal_end;
1317 // }1353 // }
1318 // },1354 // },
1319 default: {1355 default: {
1320 result.tag = Tag.invalid;1356 result.tag = Tag.invalid;
1321 result.loc.end = index;1357 result.loc.end = this.index;
1322 return result; 1358 return result;
1323 }1359 }
1324 }1360 }
...@@ -1326,12 +1362,12 @@ function tokenize_zig_source(raw_source) {...@@ -1326,12 +1362,12 @@ function tokenize_zig_source(raw_source) {
1326 case State.multiline_string_literal_line:1362 case State.multiline_string_literal_line:
1327 switch (c) {1363 switch (c) {
1328 case 0:1364 case 0:
1329 result.loc.end = index;1365 result.loc.end = this.index;
1330 return result;1366 return result;
1331 case '\n': {1367 case '\n': {
13321368
1333 index += 1;1369 this.index += 1;
1334 result.loc.end = index;1370 result.loc.end = this.index;
1335 return result;1371 return result;
1336 }1372 }
1337 case '\t': break;1373 case '\t': break;
...@@ -1344,13 +1380,13 @@ function tokenize_zig_source(raw_source) {...@@ -1344,13 +1380,13 @@ function tokenize_zig_source(raw_source) {
1344 switch (c) {1380 switch (c) {
1345 case '=': {1381 case '=': {
1346 result.tag = Tag.bang_equal;1382 result.tag = Tag.bang_equal;
1347 index += 1;1383 this.index += 1;
1348 result.loc.end = index;1384 result.loc.end = this.index;
1349 return result;1385 return result;
1350 }1386 }
1351 default: {1387 default: {
1352 result.tag = Tag.bang;1388 result.tag = Tag.bang;
1353 result.loc.end = index;1389 result.loc.end = this.index;
1354 return result;1390 return result;
1355 }1391 }
1356 }1392 }
...@@ -1359,19 +1395,19 @@ function tokenize_zig_source(raw_source) {...@@ -1359,19 +1395,19 @@ function tokenize_zig_source(raw_source) {
1359 switch (c) {1395 switch (c) {
1360 case '=': {1396 case '=': {
1361 result.tag = Tag.pipe_equal;1397 result.tag = Tag.pipe_equal;
1362 index += 1;1398 this.index += 1;
1363 result.loc.end = index;1399 result.loc.end = this.index;
1364 return result;1400 return result;
1365 }1401 }
1366 case '|': {1402 case '|': {
1367 result.tag = Tag.pipe_pipe;1403 result.tag = Tag.pipe_pipe;
1368 index += 1;1404 this.index += 1;
1369 result.loc.end = index;1405 result.loc.end = this.index;
1370 return result;1406 return result;
1371 }1407 }
1372 default: {1408 default: {
1373 result.tag = Tag.pipe;1409 result.tag = Tag.pipe;
1374 result.loc.end = index;1410 result.loc.end = this.index;
1375 return result;1411 return result;
1376 }1412 }
1377 }1413 }
...@@ -1379,19 +1415,19 @@ function tokenize_zig_source(raw_source) {...@@ -1379,19 +1415,19 @@ function tokenize_zig_source(raw_source) {
1379 case State.equal: switch (c) {1415 case State.equal: switch (c) {
1380 case '=': {1416 case '=': {
1381 result.tag = Tag.equal_equal;1417 result.tag = Tag.equal_equal;
1382 index += 1;1418 this.index += 1;
1383 result.loc.end = index;1419 result.loc.end = this.index;
1384 return result;1420 return result;
1385 }1421 }
1386 case '>': {1422 case '>': {
1387 result.tag = Tag.equal_angle_bracket_right;1423 result.tag = Tag.equal_angle_bracket_right;
1388 index += 1;1424 this.index += 1;
1389 result.loc.end = index;1425 result.loc.end = this.index;
1390 return result;1426 return result;
1391 }1427 }
1392 default: {1428 default: {
1393 result.tag = Tag.equal;1429 result.tag = Tag.equal;
1394 result.loc.end = index;1430 result.loc.end = this.index;
1395 return result;1431 return result;
1396 }1432 }
1397 }1433 }
...@@ -1399,14 +1435,14 @@ function tokenize_zig_source(raw_source) {...@@ -1399,14 +1435,14 @@ function tokenize_zig_source(raw_source) {
1399 case State.minus: switch (c) {1435 case State.minus: switch (c) {
1400 case '>': {1436 case '>': {
1401 result.tag = Tag.arrow;1437 result.tag = Tag.arrow;
1402 index += 1;1438 this.index += 1;
1403 result.loc.end = index;1439 result.loc.end = this.index;
1404 return result;1440 return result;
1405 }1441 }
1406 case '=': {1442 case '=': {
1407 result.tag = Tag.minus_equal;1443 result.tag = Tag.minus_equal;
1408 index += 1;1444 this.index += 1;
1409 result.loc.end = index;1445 result.loc.end = this.index;
1410 return result;1446 return result;
1411 }1447 }
1412 case '%': {1448 case '%': {
...@@ -1417,7 +1453,7 @@ function tokenize_zig_source(raw_source) {...@@ -1417,7 +1453,7 @@ function tokenize_zig_source(raw_source) {
1417 }1453 }
1418 default: {1454 default: {
1419 result.tag = Tag.minus;1455 result.tag = Tag.minus;
1420 result.loc.end = index;1456 result.loc.end = this.index;
1421 return result;1457 return result;
1422 }1458 }
1423 }1459 }
...@@ -1426,13 +1462,13 @@ function tokenize_zig_source(raw_source) {...@@ -1426,13 +1462,13 @@ function tokenize_zig_source(raw_source) {
1426 switch (c) {1462 switch (c) {
1427 case '=': {1463 case '=': {
1428 result.tag = Tag.minus_percent_equal;1464 result.tag = Tag.minus_percent_equal;
1429 index += 1;1465 this.index += 1;
1430 result.loc.end = index;1466 result.loc.end = this.index;
1431 return result;1467 return result;
1432 }1468 }
1433 default: {1469 default: {
1434 result.tag = Tag.minus_percent;1470 result.tag = Tag.minus_percent;
1435 result.loc.end = index;1471 result.loc.end = this.index;
1436 return result;1472 return result;
1437 }1473 }
1438 }1474 }
...@@ -1441,13 +1477,13 @@ function tokenize_zig_source(raw_source) {...@@ -1441,13 +1477,13 @@ function tokenize_zig_source(raw_source) {
1441 switch (c) {1477 switch (c) {
1442 case '=': {1478 case '=': {
1443 result.tag = Tag.minus_pipe_equal;1479 result.tag = Tag.minus_pipe_equal;
1444 index += 1;1480 this.index += 1;
1445 result.loc.end = index;1481 result.loc.end = this.index;
1446 return result;1482 return result;
1447 }1483 }
1448 default: {1484 default: {
1449 result.tag = Tag.minus_pipe;1485 result.tag = Tag.minus_pipe;
1450 result.loc.end = index;1486 result.loc.end = this.index;
1451 return result;1487 return result;
1452 }1488 }
1453 }1489 }
...@@ -1459,13 +1495,13 @@ function tokenize_zig_source(raw_source) {...@@ -1459,13 +1495,13 @@ function tokenize_zig_source(raw_source) {
1459 }1495 }
1460 case '=': {1496 case '=': {
1461 result.tag = Tag.angle_bracket_left_equal;1497 result.tag = Tag.angle_bracket_left_equal;
1462 index += 1;1498 this.index += 1;
1463 result.loc.end = index;1499 result.loc.end = this.index;
1464 return result;1500 return result;
1465 }1501 }
1466 default: {1502 default: {
1467 result.tag = Tag.angle_bracket_left;1503 result.tag = Tag.angle_bracket_left;
1468 result.loc.end = index;1504 result.loc.end = this.index;
1469 return result;1505 return result;
1470 }1506 }
1471 }1507 }
...@@ -1474,8 +1510,8 @@ function tokenize_zig_source(raw_source) {...@@ -1474,8 +1510,8 @@ function tokenize_zig_source(raw_source) {
1474 switch (c) {1510 switch (c) {
1475 case '=': {1511 case '=': {
1476 result.tag = Tag.angle_bracket_angle_bracket_left_equal;1512 result.tag = Tag.angle_bracket_angle_bracket_left_equal;
1477 index += 1;1513 this.index += 1;
1478 result.loc.end = index;1514 result.loc.end = this.index;
1479 return result;1515 return result;
1480 }1516 }
1481 case '|': {1517 case '|': {
...@@ -1483,7 +1519,7 @@ function tokenize_zig_source(raw_source) {...@@ -1483,7 +1519,7 @@ function tokenize_zig_source(raw_source) {
1483 }1519 }
1484 default: {1520 default: {
1485 result.tag = Tag.angle_bracket_angle_bracket_left;1521 result.tag = Tag.angle_bracket_angle_bracket_left;
1486 result.loc.end = index;1522 result.loc.end = this.index;
1487 return result;1523 return result;
1488 }1524 }
1489 }1525 }
...@@ -1492,13 +1528,13 @@ function tokenize_zig_source(raw_source) {...@@ -1492,13 +1528,13 @@ function tokenize_zig_source(raw_source) {
1492 switch (c) {1528 switch (c) {
1493 case '=': {1529 case '=': {
1494 result.tag = Tag.angle_bracket_angle_bracket_left_pipe_equal;1530 result.tag = Tag.angle_bracket_angle_bracket_left_pipe_equal;
1495 index += 1;1531 this.index += 1;
1496 result.loc.end = index;1532 result.loc.end = this.index;
1497 return result;1533 return result;
1498 }1534 }
1499 default: {1535 default: {
1500 result.tag = Tag.angle_bracket_angle_bracket_left_pipe;1536 result.tag = Tag.angle_bracket_angle_bracket_left_pipe;
1501 result.loc.end = index;1537 result.loc.end = this.index;
1502 return result;1538 return result;
1503 }1539 }
1504 }1540 }
...@@ -1510,13 +1546,13 @@ function tokenize_zig_source(raw_source) {...@@ -1510,13 +1546,13 @@ function tokenize_zig_source(raw_source) {
1510 }1546 }
1511 case '=': {1547 case '=': {
1512 result.tag = Tag.angle_bracket_right_equal;1548 result.tag = Tag.angle_bracket_right_equal;
1513 index += 1;1549 this.index += 1;
1514 result.loc.end = index;1550 result.loc.end = this.index;
1515 return result;1551 return result;
1516 }1552 }
1517 default: {1553 default: {
1518 result.tag = Tag.angle_bracket_right;1554 result.tag = Tag.angle_bracket_right;
1519 result.loc.end = index;1555 result.loc.end = this.index;
1520 return result;1556 return result;
1521 }1557 }
1522 }1558 }
...@@ -1525,13 +1561,13 @@ function tokenize_zig_source(raw_source) {...@@ -1525,13 +1561,13 @@ function tokenize_zig_source(raw_source) {
1525 switch (c) {1561 switch (c) {
1526 case '=': {1562 case '=': {
1527 result.tag = Tag.angle_bracket_angle_bracket_right_equal;1563 result.tag = Tag.angle_bracket_angle_bracket_right_equal;
1528 index += 1;1564 this.index += 1;
1529 result.loc.end = index;1565 result.loc.end = this.index;
1530 return result;1566 return result;
1531 }1567 }
1532 default: {1568 default: {
1533 result.tag = Tag.angle_bracket_angle_bracket_right;1569 result.tag = Tag.angle_bracket_angle_bracket_right;
1534 result.loc.end = index;1570 result.loc.end = this.index;
1535 return result;1571 return result;
1536 }1572 }
1537 }1573 }
...@@ -1546,7 +1582,7 @@ function tokenize_zig_source(raw_source) {...@@ -1546,7 +1582,7 @@ function tokenize_zig_source(raw_source) {
1546 }1582 }
1547 default: {1583 default: {
1548 result.tag = Tag.period;1584 result.tag = Tag.period;
1549 result.loc.end = index;1585 result.loc.end = this.index;
1550 return result;1586 return result;
1551 }1587 }
1552 }1588 }
...@@ -1555,13 +1591,13 @@ function tokenize_zig_source(raw_source) {...@@ -1555,13 +1591,13 @@ function tokenize_zig_source(raw_source) {
1555 switch (c) {1591 switch (c) {
1556 case '.': {1592 case '.': {
1557 result.tag = Tag.ellipsis3;1593 result.tag = Tag.ellipsis3;
1558 index += 1;1594 this.index += 1;
1559 result.loc.end = index;1595 result.loc.end = this.index;
1560 return result;1596 return result;
1561 }1597 }
1562 default: {1598 default: {
1563 result.tag = Tag.ellipsis2;1599 result.tag = Tag.ellipsis2;
1564 result.loc.end = index;1600 result.loc.end = this.index;
1565 return result;1601 return result;
1566 }1602 }
1567 }1603 }
...@@ -1570,12 +1606,12 @@ function tokenize_zig_source(raw_source) {...@@ -1570,12 +1606,12 @@ function tokenize_zig_source(raw_source) {
1570 switch (c) {1606 switch (c) {
1571 case '*': {1607 case '*': {
1572 result.tag = Tag.invalid_periodasterisks;1608 result.tag = Tag.invalid_periodasterisks;
1573 result.loc.end = index;1609 result.loc.end = this.index;
1574 return result;1610 return result;
1575 }1611 }
1576 default: {1612 default: {
1577 result.tag = Tag.period_asterisk;1613 result.tag = Tag.period_asterisk;
1578 result.loc.end = index;1614 result.loc.end = this.index;
1579 return result;1615 return result;
1580 }1616 }
1581 }1617 }
...@@ -1588,24 +1624,24 @@ function tokenize_zig_source(raw_source) {...@@ -1588,24 +1624,24 @@ function tokenize_zig_source(raw_source) {
1588 }1624 }
1589 case '=': {1625 case '=': {
1590 result.tag = Tag.slash_equal;1626 result.tag = Tag.slash_equal;
1591 index += 1;1627 this.index += 1;
1592 result.loc.end = index;1628 result.loc.end = this.index;
1593 return result;1629 return result;
1594 }1630 }
1595 default: {1631 default: {
1596 result.tag = Tag.slash;1632 result.tag = Tag.slash;
1597 result.loc.end = index;1633 result.loc.end = this.index;
1598 return result;1634 return result;
1599 }1635 }
1600 } break;1636 } break;
1601 case State.line_comment_start:1637 case State.line_comment_start:
1602 switch (c) {1638 switch (c) {
1603 case 0: {1639 case 0: {
1604 if (index != raw_source.length) {1640 if (this.index != raw_source.length) {
1605 result.tag = Tag.invalid;1641 result.tag = Tag.invalid;
1606 index += 1;1642 this.index += 1;
1607 }1643 }
1608 result.loc.end = index;1644 result.loc.end = this.index;
1609 return result;1645 return result;
1610 }1646 }
1611 case '/': {1647 case '/': {
...@@ -1617,7 +1653,7 @@ function tokenize_zig_source(raw_source) {...@@ -1617,7 +1653,7 @@ function tokenize_zig_source(raw_source) {
1617 }1653 }
1618 case '\n': {1654 case '\n': {
1619 state = State.start;1655 state = State.start;
1620 result.loc.start = index + 1; break;1656 result.loc.start = this.index + 1; break;
1621 }1657 }
1622 case '\t':1658 case '\t':
1623 state = State.line_comment; break;1659 state = State.line_comment; break;
...@@ -1637,7 +1673,7 @@ function tokenize_zig_source(raw_source) {...@@ -1637,7 +1673,7 @@ function tokenize_zig_source(raw_source) {
1637 case '\n':1673 case '\n':
1638 {1674 {
1639 result.tag = Tag.doc_comment;1675 result.tag = Tag.doc_comment;
1640 result.loc.end = index;1676 result.loc.end = this.index;
1641 return result;1677 return result;
1642 }1678 }
1643 case '\t': {1679 case '\t': {
...@@ -1655,16 +1691,16 @@ function tokenize_zig_source(raw_source) {...@@ -1655,16 +1691,16 @@ function tokenize_zig_source(raw_source) {
1655 case State.line_comment:1691 case State.line_comment:
1656 switch (c) {1692 switch (c) {
1657 case 0: {1693 case 0: {
1658 if (index != raw_source.length) {1694 if (this.index != raw_source.length) {
1659 result.tag = Tag.invalid;1695 result.tag = Tag.invalid;
1660 index += 1;1696 this.index += 1;
1661 }1697 }
1662 result.loc.end = index;1698 result.loc.end = this.index;
1663 return result;1699 return result;
1664 }1700 }
1665 case '\n': {1701 case '\n': {
1666 state = State.start;1702 state = State.start;
1667 result.loc.start = index + 1;1703 result.loc.start = this.index + 1;
1668 break;1704 break;
1669 }1705 }
1670 case '\t': break;1706 case '\t': break;
...@@ -1675,7 +1711,7 @@ function tokenize_zig_source(raw_source) {...@@ -1675,7 +1711,7 @@ function tokenize_zig_source(raw_source) {
1675 switch (c) {1711 switch (c) {
1676 case 0://1712 case 0://
1677 case '\n':1713 case '\n':
1678 result.loc.end = index;1714 result.loc.end = this.index;
1679 return result;1715 return result;
1680 case '\t': break;1716 case '\t': break;
1681 //TODOL PORT1717 //TODOL PORT
...@@ -1754,7 +1790,7 @@ function tokenize_zig_source(raw_source) {...@@ -1754,7 +1790,7 @@ function tokenize_zig_source(raw_source) {
1754 case 'P':1790 case 'P':
1755 state = State.int_exponent;1791 state = State.int_exponent;
1756 break;1792 break;
1757 default: result.loc.end = index;1793 default: result.loc.end = this.index;
1758 return result;1794 return result;
1759 } break;1795 } break;
1760 case State.int_exponent:1796 case State.int_exponent:
...@@ -1766,7 +1802,7 @@ function tokenize_zig_source(raw_source) {...@@ -1766,7 +1802,7 @@ function tokenize_zig_source(raw_source) {
1766 state = State.float; break;1802 state = State.float; break;
1767 }1803 }
1768 default: {1804 default: {
1769 index -= 1;1805 this.index -= 1;
1770 state = State.int; break;1806 state = State.int; break;
1771 }1807 }
1772 } break;1808 } break;
...@@ -1838,8 +1874,8 @@ function tokenize_zig_source(raw_source) {...@@ -1838,8 +1874,8 @@ function tokenize_zig_source(raw_source) {
1838 case 'P':1874 case 'P':
1839 state = State.float_exponent; break;1875 state = State.float_exponent; break;
1840 default: {1876 default: {
1841 index -= 1;1877 this.index -= 1;
1842 result.loc.end = index;1878 result.loc.end = this.index;
1843 return result;1879 return result;
1844 }1880 }
1845 } break;1881 } break;
...@@ -1911,7 +1947,7 @@ function tokenize_zig_source(raw_source) {...@@ -1911,7 +1947,7 @@ function tokenize_zig_source(raw_source) {
1911 case 'p':1947 case 'p':
1912 case 'P':1948 case 'P':
1913 state = State.float_exponent; break;1949 state = State.float_exponent; break;
1914 default: result.loc.end = index;1950 default: result.loc.end = this.index;
1915 return result;1951 return result;
1916 } break;1952 } break;
1917 case State.float_exponent:1953 case State.float_exponent:
...@@ -1920,13 +1956,27 @@ function tokenize_zig_source(raw_source) {...@@ -1920,13 +1956,27 @@ function tokenize_zig_source(raw_source) {
1920 case '+':1956 case '+':
1921 state = State.float; break;1957 state = State.float; break;
1922 default: {1958 default: {
1923 index -= 1;1959 this.index -= 1;
1924 state = State.float; break;1960 state = State.float; break;
1925 }1961 }
1926 }1962 }
1927 break;1963 break;
1964
1965 case State.whitespace:
1966 switch(c) {
1967 case ' ':
1968 case '\n':
1969 case '\t':
1970 case '\r': {
1971 break;
1972 }
1973 default: {
1974 result.loc.end = this.index;
1975 return result;
1976 }
1977 }
1928 }1978 }
1929 index += 1;1979 this.index += 1;
1930 }1980 }
19311981
1932 //TODO: PORT1982 //TODO: PORT
...@@ -1938,52 +1988,37 @@ function tokenize_zig_source(raw_source) {...@@ -1938,52 +1988,37 @@ function tokenize_zig_source(raw_source) {
1938 // result.loc.start = sindex;1988 // result.loc.start = sindex;
1939 // }1989 // }
19401990
1941 result.loc.end = index;1991 result.loc.end = this.index;
1942 return result;1992 return result;
19431993
1944 }1994 }
1945
1946 let toks = []
1947
1948 for (let i = 0; i < raw_source.length * 2; i++) {
1949 const tok = next();
1950 toks.push(tok);
1951
1952 if (tok.tag == Tag.eof) {
1953 break;
1954 }
1955 }
1956
1957 return toks;
1958}1995}
19591996
19601997
1961function generate_html_for_src(src) {1998const builtin_types = [
1962 var toks = tokenize_zig_source(src);1999 "f16", "f32", "f64", "f80", "f128",
1963 var html = [];2000 "c_longdouble", "c_short", "c_ushort", "c_int", "c_uint",
19642001 "c_long", "c_ulong", "c_longlong", "c_ulonglong", "c_char",
1965 let offset = 0;2002 "anyopaque", "void", "bool", "isize", "usize",
1966 for (let z = 0; z < toks.length; z++) {2003 "noreturn", "type", "anyerror", "comptime_int", "comptime_float",
1967 const t = toks[z];2004];
19682005
1969 if(t.tag == Tag.eof)2006function isSimpleType(typeName) {
1970 break;2007 return builtin_types.includes(typeName) || isIntType(typeName);
19712008}
1972 const spanStart = `<span class="zig_${t.tag}">`
1973 const spanEnd = `</span>`
1974
1975 src = `${src.slice(0, t.loc.start + offset)}` + spanStart + `${src.slice(t.loc.start + offset)}`;
1976 offset += spanStart.length;
19772009
1978 src = `${src.slice(0, t.loc.end + offset)}` + spanEnd + `${src.slice(t.loc.end + offset)}`;2010function isIntType(typeName) {
1979 offset += spanEnd.length;2011 if (typeName[0] != 'u' && typeName[0] != 'i') return false;
2012 let i = 1;
2013 if (i == typeName.length) return false;
2014 for (; i < typeName.length; i += 1) {
2015 if (typeName[i] < '0' || typeName[i] > '9') return false;
1980 }2016 }
2017 return true;
2018}
19812019
19822020function isSpecialIndentifier(identifier) {
1983 html.push(src);2021 return ["null", "true", "false", ,"undefined"].includes(identifier);
1984
1985 return html.join("");
1986
1987}2022}
19882023
1989//const fs = require('fs');2024//const fs = require('fs');
src/Autodoc.zig+7-6
...@@ -814,6 +814,7 @@ const DocData = struct {...@@ -814,6 +814,7 @@ const DocData = struct {
814 this: usize, // index in `types`814 this: usize, // index in `types`
815 declRef: *Scope.DeclStatus,815 declRef: *Scope.DeclStatus,
816 declIndex: usize, // index into `decls`, alternative repr for `declRef`816 declIndex: usize, // index into `decls`, alternative repr for `declRef`
817 declName: []const u8, // unresolved decl name
817 builtinField: enum { len, ptr },818 builtinField: enum { len, ptr },
818 fieldRef: FieldRef,819 fieldRef: FieldRef,
819 refPath: []Expr,820 refPath: []Expr,
...@@ -2282,7 +2283,7 @@ fn walkInstruction(...@@ -2282,7 +2283,7 @@ fn walkInstruction(
22822283
2283 var path: std.ArrayListUnmanaged(DocData.Expr) = .{};2284 var path: std.ArrayListUnmanaged(DocData.Expr) = .{};
2284 try path.append(self.arena, .{2285 try path.append(self.arena, .{
2285 .string = file.zir.nullTerminatedString(extra.data.field_name_start),2286 .declName = file.zir.nullTerminatedString(extra.data.field_name_start),
2286 });2287 });
22872288
2288 // Put inside path the starting index of each decl name that2289 // Put inside path the starting index of each decl name that
...@@ -2304,7 +2305,7 @@ fn walkInstruction(...@@ -2304,7 +2305,7 @@ fn walkInstruction(
2304 );2305 );
23052306
2306 try path.append(self.arena, .{2307 try path.append(self.arena, .{
2307 .string = file.zir.nullTerminatedString(lhs_extra.data.field_name_start),2308 .declName = file.zir.nullTerminatedString(lhs_extra.data.field_name_start),
2308 });2309 });
2309 }2310 }
2310 };2311 };
...@@ -3665,7 +3666,7 @@ fn tryResolveRefPath(...@@ -3665,7 +3666,7 @@ fn tryResolveRefPath(
3665 var i: usize = 0;3666 var i: usize = 0;
3666 outer: while (i < path.len - 1) : (i += 1) {3667 outer: while (i < path.len - 1) : (i += 1) {
3667 const parent = path[i];3668 const parent = path[i];
3668 const child_string = path[i + 1].string; // we expect to find a string union case3669 const child_string = path[i + 1].declName; // we expect to find an unsolved decl
36693670
3670 var resolved_parent = parent;3671 var resolved_parent = parent;
3671 var j: usize = 0;3672 var j: usize = 0;
...@@ -3738,14 +3739,14 @@ fn tryResolveRefPath(...@@ -3738,14 +3739,14 @@ fn tryResolveRefPath(
3738 return;3739 return;
3739 }3740 }
37403741
3741 // If the last element is a string or a CTE, then we give up,3742 // If the last element is a declName or a CTE, then we give up,
3742 // otherwise we resovle the parent to it and loop again.3743 // otherwise we resovle the parent to it and loop again.
3743 // NOTE: we assume that if we find a string, it's because of3744 // NOTE: we assume that if we find a string, it's because of
3744 // a CTE component somewhere in the path. We know that the path3745 // a CTE component somewhere in the path. We know that the path
3745 // is not pending futher evaluation because we just checked!3746 // is not pending futher evaluation because we just checked!
3746 const last = rp[rp.len - 1];3747 const last = rp[rp.len - 1];
3747 switch (last) {3748 switch (last) {
3748 .comptimeExpr, .string => break :outer,3749 .comptimeExpr, .declName => break :outer,
3749 else => {3750 else => {
3750 resolved_parent = last;3751 resolved_parent = last;
3751 continue;3752 continue;
...@@ -3772,7 +3773,7 @@ fn tryResolveRefPath(...@@ -3772,7 +3773,7 @@ fn tryResolveRefPath(
3772 "TODO: handle `{s}`in tryResolveRefPath\nInfo: {}",3773 "TODO: handle `{s}`in tryResolveRefPath\nInfo: {}",
3773 .{ @tagName(resolved_parent), resolved_parent },3774 .{ @tagName(resolved_parent), resolved_parent },
3774 );3775 );
3775 path[i + 1] = (try self.cteTodo("<match failure>")).expr;3776 // path[i + 1] = (try self.cteTodo("<match failure>")).expr;
3776 continue :outer;3777 continue :outer;
3777 },3778 },
3778 .comptimeExpr, .call, .typeOf => {3779 .comptimeExpr, .call, .typeOf => {