refactor common pattern into a function

master
xackus 2020-04-07 22:56:00 +02:00
parent bf46117f13
commit 721c76b53c
1 changed files with 42 additions and 111 deletions

View File

@ -47,6 +47,16 @@
var typeKinds = indexTypeKinds();
var typeTypeId = findTypeTypeId();
var pointerSizeEnum = { One: 0, Many: 1, Slice: 2, C: 3 };
var tokenKinds = {
Keyword: 'tok-kw',
String: 'tok-str',
Builtin: 'tok-builtin',
Comment: 'tok-comment',
Function: 'tok-fn',
Null: 'tok-null',
Number: 'tok-number',
Type: 'tok-type',
};
// for each package, is an array with packages to get to this one
var canonPkgPaths = computeCanonicalPackagePaths();
@ -537,18 +547,10 @@
var fnObj = zigAnalysis.fns[value];
return typeIndexName(fnObj.type, wantHtml, wantLink);
case typeKinds.Int:
if (wantHtml) {
return '<span class="tok-number">' + value + '</span>';
} else {
return value + "";
}
return token(value, tokenKinds.Number, wantHtml);
case typeKinds.Optional:
if(value === 'null'){
if (wantHtml) {
return '<span class="tok-null">' + value + '</span>';
} else {
return value + "";
}
return token(value, tokenKinds.Null, wantHtml);
} else {
console.trace("TODO non-null optional value printing");
return "TODO";
@ -563,11 +565,7 @@
switch (typeObj.kind) {
case typeKinds.Array:
var name = "[";
if (wantHtml) {
name += '<span class="tok-number">' + typeObj.len + '</span>';
} else {
name += typeObj.len;
}
name += token(typeObj.len, tokenKinds.Number, wantHtml);
name += "]";
name += typeIndexName(typeObj.elem, wantHtml, wantSubLink, null);
return name;
@ -591,111 +589,48 @@
break;
}
if (typeObj['const']) {
if (wantHtml) {
name += '<span class="tok-kw">const</span> ';
} else {
name += "const ";
}
name += token('const', tokenKinds.Keyword, wantHtml) + ' ';
}
if (typeObj['volatile']) {
if (wantHtml) {
name += '<span class="tok-kw">volatile</span> ';
} else {
name += "volatile ";
}
name += token('volatile', tokenKinds.Keyword, wantHtml) + ' ';
}
if (typeObj.align != null) {
if (wantHtml) {
name += '<span class="tok-kw">align</span>(';
} else {
name += "align(";
}
if (wantHtml) {
name += '<span class="tok-number">' + typeObj.align + '</span>';
} else {
name += typeObj.align;
}
name += token('align', tokenKinds.Keyword, wantHtml) + '(';
name += token(typeObj.align, tokenKinds.Number, wantHtml);
if (typeObj.hostIntBytes != null) {
name += ":";
if (wantHtml) {
name += '<span class="tok-number">' + typeObj.bitOffsetInHost + '</span>';
} else {
name += typeObj.bitOffsetInHost;
}
name += token(typeObj.bitOffsetInHost, tokenKinds.Number, wantHtml);
name += ":";
if (wantHtml) {
name += '<span class="tok-number">' + typeObj.hostIntBytes + '</span>';
} else {
name += typeObj.hostIntBytes;
}
name += token(typeObj.hostIntBytes, tokenKinds.Number, wantHtml);
}
name += ") ";
}
name += typeIndexName(typeObj.elem, wantHtml, wantSubLink, null);
return name;
case typeKinds.Float:
if (wantHtml) {
return '<span class="tok-type">f' + typeObj.bits + '</span>';
} else {
return "f" + typeObj.bits;
}
return token('f' + typeObj.bits, tokenKinds.Type, wantHtml);
case typeKinds.Int:
var signed = (typeObj.i != null) ? 'i' : 'u';
var bits = typeObj[signed];
if (wantHtml) {
return '<span class="tok-type">' + signed + bits + '</span>';
} else {
return signed + bits;
}
return token(signed + bits, tokenKinds.Type, wantHtml);
case typeKinds.ComptimeInt:
if (wantHtml) {
return '<span class="tok-type">comptime_int</span>';
} else {
return "comptime_int";
}
return token('comptime_int', tokenKinds.Type, wantHtml);
case typeKinds.ComptimeFloat:
if (wantHtml) {
return '<span class="tok-type">comptime_float</span>';
} else {
return "comptime_float";
}
return token('comptime_float', tokenKinds.Type, wantHtml);
case typeKinds.Type:
if (wantHtml) {
return '<span class="tok-type">type</span>';
} else {
return "type";
}
return token('type', tokenKinds.Type, wantHtml);
case typeKinds.Bool:
if (wantHtml) {
return '<span class="tok-type">bool</span>';
} else {
return "bool";
}
return token('bool', tokenKinds.Type, wantHtml);
case typeKinds.Void:
if (wantHtml) {
return '<span class="tok-type">void</span>';
} else {
return "void";
}
return token('void', tokenKinds.Type, wantHtml);
case typeKinds.EnumLiteral:
if (wantHtml) {
return '<span class="tok-type">(enum literal)</span>';
} else {
return "(enum literal)";
}
return token('(enum literal)', tokenKinds.Type, wantHtml);
case typeKinds.NoReturn:
if (wantHtml) {
return '<span class="tok-type">noreturn</span>';
} else {
return "noreturn";
}
return token('noreturn', tokenKinds.Type, wantHtml);
case typeKinds.ErrorSet:
if (typeObj.errors == null) {
if (wantHtml) {
return '<span class="tok-type">anyerror</span>';
} else {
return "anyerror";
}
return token('anyerror', tokenKinds.Type, wantHtml);
} else {
if (wantHtml) {
return escapeHtml(typeObj.name);
@ -756,19 +691,11 @@
}
if (paramNode.noalias) {
if (wantHtml) {
payloadHtml += '<span class="tok-kw">noalias</span> ';
} else {
payloadHtml += 'noalias ';
}
payloadHtml += token('noalias', tokenKinds.Keyword, wantHtml) + ' ';
}
if (paramNode.comptime) {
if (wantHtml) {
payloadHtml += '<span class="tok-kw">comptime</span> ';
} else {
payloadHtml += 'comptime ';
}
payloadHtml += token('comptime', tokenKinds.Keyword, wantHtml) + ' ';
}
var paramName = paramNode.name;
@ -784,10 +711,8 @@
payloadHtml += '...';
} else if (argTypeIndex != null) {
payloadHtml += typeIndexName(argTypeIndex, wantHtml, wantSubLink);
} else if (wantHtml) {
payloadHtml += '<span class="tok-kw">var</span>';
} else {
payloadHtml += 'var';
payloadHtml += token('var', tokenKinds.Keyword, wantHtml);
}
}
}
@ -795,10 +720,8 @@
payloadHtml += ') ';
if (typeObj.ret != null) {
payloadHtml += typeIndexName(typeObj.ret, wantHtml, wantSubLink, fnDecl);
} else if (wantHtml) {
payloadHtml += '<span class="tok-kw">var</span>';
} else {
payloadHtml += 'var';
payloadHtml += token('var', tokenKinds.Keyword, wantHtml);
}
return payloadHtml;
default:
@ -1973,4 +1896,12 @@
return key;
}
}
function token(value, tokenClass, wantHtml){
if(wantHtml){
return '<span class="' + tokenClass + '">' + value + '</span>';
} else {
return value + '';
}
}
})();