* Greatly improve the rustdoc search parser source code

* Move all functions outside parseQuery
This commit is contained in:
Guillaume Gomez 2022-01-04 15:44:00 +01:00
parent 99c5394ecc
commit 264064df36
16 changed files with 563 additions and 510 deletions

View File

@ -8,7 +8,6 @@ function initSearch(searchIndex){}
/**
* @typedef {{
* isExact: boolean,
* name: string,
* fullPath: Array<string>,
* pathWithoutLast: Array<string>,
@ -18,20 +17,25 @@ function initSearch(searchIndex){}
*/
var QueryElement;
/**
* @typedef {{
* pos: number,
* totalElems: number,
* typeFilter: (null|string),
* userQuery: string,
* }}
*/
var ParserState;
/**
* @typedef {{
* original: string,
* userQuery: string,
* length: number,
* pos: number,
* typeFilter: number,
* elems: Array<QueryElement>,
* elemName: (string|null),
* args: Array<QueryElement>,
* returned: Array<QueryElement>,
* foundElems: number,
* id: string,
* nameSplit: (string|null),
* }}
*/
var ParsedQuery;
@ -50,3 +54,31 @@ var ParsedQuery;
* }}
*/
var Row;
/**
* @typedef {{
* in_args: Array<Object>,
* returned: Array<Object>,
* others: Array<Object>,
* query: ParsedQuery,
* }}
*/
var ResultsTable;
/**
* @typedef {{
* crate: "std"
* desc: string,
* displayPath: string,
* fullPath: string,
* href: string,
* id: number,
* lev: number,
* name: string,
* normalizedName: string,
* parent: (Object|undefined),
* path: string,
* ty: number,
* }}
*/
var Results;

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
// The goal of this test is to ensure the color of the text is the one expected.
goto: file://|DOC_PATH|/test_docs/index.html?search=cook
goto: file://|DOC_PATH|/test_docs/index.html?search=coo
// This is needed so that the text color is computed.
show-text: true

View File

@ -3,66 +3,51 @@ const QUERY = ['<"P">', '"P" "P"', 'P "P"', '"p" p', '"const": p'];
const PARSED = [
{
args: [],
elemName: null,
elems: [],
foundElems: 0,
id: "<\"P\">",
nameSplit: null,
original: "<\"P\">",
returned: [],
typeFilter: null,
typeFilter: -1,
userQuery: "<\"p\">",
error: "`\"` cannot be used in generics",
},
{
args: [],
elemName: null,
elems: [],
foundElems: 0,
id: "\"P\" \"P\"",
nameSplit: null,
original: "\"P\" \"P\"",
returned: [],
typeFilter: null,
typeFilter: -1,
userQuery: "\"p\" \"p\"",
error: "Cannot have more than one literal search element",
},
{
args: [],
elemName: null,
elems: [],
foundElems: 0,
id: "P \"P\"",
nameSplit: null,
original: "P \"P\"",
returned: [],
typeFilter: null,
typeFilter: -1,
userQuery: "p \"p\"",
error: "Cannot use literal search when there is more than one element",
},
{
args: [],
elemName: null,
elems: [],
foundElems: 0,
id: "\"p\" p",
nameSplit: null,
original: "\"p\" p",
returned: [],
typeFilter: null,
typeFilter: -1,
userQuery: "\"p\" p",
error: "You cannot have more than one element if you use quotes",
},
{
args: [],
elemName: null,
elems: [],
foundElems: 0,
id: "\"const\": p",
nameSplit: null,
original: "\"const\": p",
returned: [],
typeFilter: null,
typeFilter: -1,
userQuery: "\"const\": p",
error: "You cannot use quotes on type filter",
},

View File

@ -3,7 +3,6 @@ const QUERY = ['fn:foo', 'enum : foo', 'macro<f>:foo'];
const PARSED = [
{
args: [],
elemName: null,
elems: [{
name: "foo",
fullPath: ["foo"],
@ -12,8 +11,6 @@ const PARSED = [
generics: [],
}],
foundElems: 1,
id: "fn:foo",
nameSplit: null,
original: "fn:foo",
returned: [],
typeFilter: 5,
@ -22,7 +19,6 @@ const PARSED = [
},
{
args: [],
elemName: null,
elems: [{
name: "foo",
fullPath: ["foo"],
@ -31,8 +27,6 @@ const PARSED = [
generics: [],
}],
foundElems: 1,
id: "enum : foo",
nameSplit: null,
original: "enum : foo",
returned: [],
typeFilter: 4,
@ -41,7 +35,6 @@ const PARSED = [
},
{
args: [],
elemName: null,
elems: [{
name: "foo",
fullPath: ["foo"],
@ -50,8 +43,6 @@ const PARSED = [
generics: [],
}],
foundElems: 1,
id: "macro<f>:foo",
nameSplit: null,
original: "macro<f>:foo",
returned: [],
typeFilter: 14,

View File

@ -1,9 +1,8 @@
const QUERY = ['<P>', 'A<B<C<D>, E>'];
const QUERY = ['<P>', 'A<B<C<D>, E>', 'p<> u8'];
const PARSED = [
{
args: [],
elemName: null,
elems: [{
name: "",
fullPath: [""],
@ -20,8 +19,6 @@ const PARSED = [
],
}],
foundElems: 1,
id: "<P>",
nameSplit: null,
original: "<P>",
returned: [],
typeFilter: -1,
@ -30,7 +27,6 @@ const PARSED = [
},
{
args: [],
elemName: null,
elems: [{
name: "a",
fullPath: ["a"],
@ -70,12 +66,35 @@ const PARSED = [
],
}],
foundElems: 1,
id: 'A<B<C<D>, E>',
nameSplit: null,
original: 'A<B<C<D>, E>',
returned: [],
typeFilter: -1,
userQuery: 'a<b<c<d>, e>',
error: null,
}
},
{
args: [],
elems: [
{
name: "p",
fullPath: ["p"],
pathWithoutLast: [],
pathLast: "p",
generics: [],
},
{
name: "u8",
fullPath: ["u8"],
pathWithoutLast: [],
pathLast: "u8",
generics: [],
},
],
foundElems: 2,
original: "p<> u8",
returned: [],
typeFilter: -1,
userQuery: "p<> u8",
error: null,
},
];

View File

@ -9,11 +9,8 @@ const PARSED = [
pathLast: "whatever",
generics: [],
}],
elemName: null,
elems: [],
foundElems: 1,
id: "(whatever)",
nameSplit: null,
original: "(whatever)",
returned: [],
typeFilter: -1,
@ -36,11 +33,8 @@ const PARSED = [
},
],
}],
elemName: null,
elems: [],
foundElems: 1,
id: "(<P>)",
nameSplit: null,
original: "(<P>)",
returned: [],
typeFilter: -1,

View File

@ -6,11 +6,8 @@ const QUERY = ['-> <P> (p2)', '(p -> p2', 'a b', 'a,b(c)'];
const PARSED = [
{
args: [],
elemName: null,
elems: [],
foundElems: 2,
id: "-> <P> (p2)",
nameSplit: null,
original: "-> <P> (p2)",
returned: [
{
@ -57,11 +54,8 @@ const PARSED = [
generics: [],
},
],
elemName: null,
elems: [],
foundElems: 2,
id: "(p -> p2",
nameSplit: null,
original: "(p -> p2",
returned: [],
typeFilter: -1,
@ -70,7 +64,6 @@ const PARSED = [
},
{
args: [],
elemName: null,
elems: [
{
name: "a b",
@ -81,8 +74,6 @@ const PARSED = [
},
],
foundElems: 1,
id: "a b",
nameSplit: null,
original: "a b",
returned: [],
typeFilter: -1,
@ -99,7 +90,6 @@ const PARSED = [
generics: [],
},
],
elemName: null,
elems: [
{
name: "a",
@ -117,8 +107,6 @@ const PARSED = [
},
],
foundElems: 3,
id: "a,b(c)",
nameSplit: null,
original: "a,b(c)",
returned: [],
typeFilter: -1,

View File

@ -3,7 +3,6 @@ const QUERY = ['R<P>'];
const PARSED = [
{
args: [],
elemName: null,
elems: [{
name: "r",
fullPath: ["r"],
@ -20,8 +19,6 @@ const PARSED = [
],
}],
foundElems: 1,
id: "R<P>",
nameSplit: null,
original: "R<P>",
returned: [],
typeFilter: -1,

View File

@ -3,7 +3,6 @@ const QUERY = ['A::B', '::A::B', 'A::B::,C', 'A::B::<f>,C'];
const PARSED = [
{
args: [],
elemName: null,
elems: [{
name: "a::b",
fullPath: ["a", "b"],
@ -12,8 +11,6 @@ const PARSED = [
generics: [],
}],
foundElems: 1,
id: "A::B",
nameSplit: null,
original: "A::B",
returned: [],
typeFilter: -1,
@ -22,7 +19,6 @@ const PARSED = [
},
{
args: [],
elemName: null,
elems: [{
name: "::a::b",
fullPath: ["a", "b"],
@ -31,8 +27,6 @@ const PARSED = [
generics: [],
}],
foundElems: 1,
id: '::A::B',
nameSplit: null,
original: '::A::B',
returned: [],
typeFilter: -1,
@ -41,7 +35,6 @@ const PARSED = [
},
{
args: [],
elemName: null,
elems: [
{
name: "a::b::",
@ -59,8 +52,6 @@ const PARSED = [
},
],
foundElems: 2,
id: 'A::B::,C',
nameSplit: null,
original: 'A::B::,C',
returned: [],
typeFilter: -1,
@ -69,7 +60,6 @@ const PARSED = [
},
{
args: [],
elemName: null,
elems: [
{
name: "a::b::",
@ -95,8 +85,6 @@ const PARSED = [
},
],
foundElems: 2,
id: 'A::B::<f>,C',
nameSplit: null,
original: 'A::B::<f>,C',
returned: [],
typeFilter: -1,

View File

@ -3,11 +3,8 @@ const QUERY = ['-> "p"', '("p")'];
const PARSED = [
{
args: [],
elemName: null,
elems: [],
foundElems: 1,
id: "-> \"p\"",
nameSplit: null,
original: "-> \"p\"",
returned: [{
name: "p",
@ -28,11 +25,8 @@ const PARSED = [
pathLast: "p",
generics: [],
}],
elemName: null,
elems: [],
foundElems: 1,
id: "(\"p\")",
nameSplit: null,
original: "(\"p\")",
returned: [],
typeFilter: -1,

View File

@ -3,11 +3,8 @@ const QUERY = ['-> <P>', '-> P'];
const PARSED = [
{
args: [],
elemName: null,
elems: [],
foundElems: 1,
id: "-> <P>",
nameSplit: null,
original: "-> <P>",
returned: [{
name: "",
@ -30,11 +27,8 @@ const PARSED = [
},
{
args: [],
elemName: null,
elems: [],
foundElems: 1,
id: "-> P",
nameSplit: null,
original: "-> P",
returned: [{
name: "p",

View File

@ -4,6 +4,6 @@ const EXPECTED = {
'others': [
{ 'path': 'std::vec::Vec', 'name': 'new' },
{ 'path': 'std::vec::Vec', 'name': 'ne' },
{ 'path': 'std::rc::Rc', 'name': 'ne' },
{ 'path': 'alloc::vec::Vec', 'name': 'ne' },
],
};

View File

@ -10,7 +10,7 @@ const EXPECTED = [
{
'path': 'doc_alias_whitespace',
'name': 'Struct',
'alias': 'demon lord',
'alias': 'Demon Lord',
'href': '../doc_alias_whitespace/struct.Struct.html',
'is_alias': true
},

View File

@ -32,7 +32,7 @@ const EXPECTED = [
{
'path': 'doc_alias',
'name': 'Struct',
'alias': 'structitem',
'alias': 'StructItem',
'href': '../doc_alias/struct.Struct.html',
'is_alias': true
},
@ -44,7 +44,7 @@ const EXPECTED = [
{
'path': 'doc_alias::Struct',
'name': 'field',
'alias': 'structfielditem',
'alias': 'StructFieldItem',
'href': '../doc_alias/struct.Struct.html#structfield.field',
'is_alias': true
},
@ -56,7 +56,7 @@ const EXPECTED = [
{
'path': 'doc_alias::Struct',
'name': 'method',
'alias': 'structmethoditem',
'alias': 'StructMethodItem',
'href': '../doc_alias/struct.Struct.html#method.method',
'is_alias': true
},
@ -72,7 +72,7 @@ const EXPECTED = [
{
'path': 'doc_alias::Struct',
'name': 'ImplConstItem',
'alias': 'structimplconstitem',
'alias': 'StructImplConstItem',
'href': '../doc_alias/struct.Struct.html#associatedconstant.ImplConstItem',
'is_alias': true
},
@ -84,7 +84,7 @@ const EXPECTED = [
{
'path': 'doc_alias::Struct',
'name': 'function',
'alias': 'impltraitfunction',
'alias': 'ImplTraitFunction',
'href': '../doc_alias/struct.Struct.html#method.function',
'is_alias': true
},
@ -96,7 +96,7 @@ const EXPECTED = [
{
'path': 'doc_alias',
'name': 'Enum',
'alias': 'enumitem',
'alias': 'EnumItem',
'href': '../doc_alias/enum.Enum.html',
'is_alias': true
},
@ -108,7 +108,7 @@ const EXPECTED = [
{
'path': 'doc_alias::Enum',
'name': 'Variant',
'alias': 'variantitem',
'alias': 'VariantItem',
'href': '../doc_alias/enum.Enum.html#variant.Variant',
'is_alias': true
},
@ -120,7 +120,7 @@ const EXPECTED = [
{
'path': 'doc_alias::Enum',
'name': 'method',
'alias': 'enummethoditem',
'alias': 'EnumMethodItem',
'href': '../doc_alias/enum.Enum.html#method.method',
'is_alias': true
},
@ -132,7 +132,7 @@ const EXPECTED = [
{
'path': 'doc_alias',
'name': 'Typedef',
'alias': 'typedefitem',
'alias': 'TypedefItem',
'href': '../doc_alias/type.Typedef.html',
'is_alias': true
},
@ -144,7 +144,7 @@ const EXPECTED = [
{
'path': 'doc_alias',
'name': 'Trait',
'alias': 'traititem',
'alias': 'TraitItem',
'href': '../doc_alias/trait.Trait.html',
'is_alias': true
},
@ -156,7 +156,7 @@ const EXPECTED = [
{
'path': 'doc_alias::Trait',
'name': 'Target',
'alias': 'traittypeitem',
'alias': 'TraitTypeItem',
'href': '../doc_alias/trait.Trait.html#associatedtype.Target',
'is_alias': true
},
@ -168,7 +168,7 @@ const EXPECTED = [
{
'path': 'doc_alias::Trait',
'name': 'AssociatedConst',
'alias': 'associatedconstitem',
'alias': 'AssociatedConstItem',
'href': '../doc_alias/trait.Trait.html#associatedconstant.AssociatedConst',
'is_alias': true
},
@ -180,7 +180,7 @@ const EXPECTED = [
{
'path': 'doc_alias::Trait',
'name': 'function',
'alias': 'traitfunctionitem',
'alias': 'TraitFunctionItem',
'href': '../doc_alias/trait.Trait.html#tymethod.function',
'is_alias': true
},
@ -192,7 +192,7 @@ const EXPECTED = [
{
'path': 'doc_alias',
'name': 'function',
'alias': 'functionitem',
'alias': 'FunctionItem',
'href': '../doc_alias/fn.function.html',
'is_alias': true
},
@ -204,7 +204,7 @@ const EXPECTED = [
{
'path': 'doc_alias',
'name': 'Module',
'alias': 'moduleitem',
'alias': 'ModuleItem',
'href': '../doc_alias/Module/index.html',
'is_alias': true
},
@ -216,7 +216,7 @@ const EXPECTED = [
{
'path': 'doc_alias',
'name': 'Const',
'alias': 'constitem',
'alias': 'ConstItem',
'href': '../doc_alias/constant.Const.html',
'is_alias': true
},
@ -232,7 +232,7 @@ const EXPECTED = [
{
'path': 'doc_alias',
'name': 'Static',
'alias': 'staticitem',
'alias': 'StaticItem',
'href': '../doc_alias/static.Static.html',
'is_alias': true
},
@ -244,7 +244,7 @@ const EXPECTED = [
{
'path': 'doc_alias',
'name': 'Union',
'alias': 'unionitem',
'alias': 'UnionItem',
'href': '../doc_alias/union.Union.html',
'is_alias': true
},
@ -262,7 +262,7 @@ const EXPECTED = [
{
'path': 'doc_alias::Union',
'name': 'union_item',
'alias': 'unionfielditem',
'alias': 'UnionFieldItem',
'href': '../doc_alias/union.Union.html#structfield.union_item',
'is_alias': true
},
@ -274,7 +274,7 @@ const EXPECTED = [
{
'path': 'doc_alias::Union',
'name': 'method',
'alias': 'unionmethoditem',
'alias': 'UnionMethodItem',
'href': '../doc_alias/union.Union.html#method.method',
'is_alias': true
},
@ -286,7 +286,7 @@ const EXPECTED = [
{
'path': 'doc_alias',
'name': 'Macro',
'alias': 'macroitem',
'alias': 'MacroItem',
'href': '../doc_alias/macro.Macro.html',
'is_alias': true
},

View File

@ -270,7 +270,12 @@ function loadSearchJsAndIndex(searchJs, searchIndex, storageJs, crate) {
// execQuery last parameter is built in buildIndex.
// buildIndex requires the hashmap from search-index.
var functionsToLoad = ["buildHrefAndPath", "pathSplitter", "levenshtein", "validateResult",
"buildIndex", "execQuery", "parseQuery", "createQueryResults"];
"buildIndex", "execQuery", "parseQuery", "createQueryResults",
"isWhitespace", "isSpecialStartCharacter", "isStopCharacter",
"removeEmptyStringsFromArray", "parseInput", "getItemsBefore",
"getNextElem", "createQueryElement", "isReturnArrow", "isPathStart",
"skipWhitespaces", "getStringElem", "itemTypeFromName",
"newParsedQuery"];
const functions = ["hasOwnPropertyRustdoc", "onEach"];
ALIASES = {};
@ -286,13 +291,12 @@ function loadSearchJsAndIndex(searchJs, searchIndex, storageJs, crate) {
return [loaded, index];
}
function checkFieldNeededFields(fullPath, expected, error_text, queryName, position) {
// This function checks if `expected` has all the required fields needed for the checks.
function checkNeededFields(fullPath, expected, error_text, queryName, position) {
let fieldsToCheck;
if (fullPath.length === 0) {
fieldsToCheck = [
"foundElems",
"id",
"nameSplit",
"original",
"returned",
"typeFilter",
@ -328,7 +332,7 @@ function checkFieldNeededFields(fullPath, expected, error_text, queryName, posit
function valueCheck(fullPath, expected, result, error_text, queryName) {
if (Array.isArray(expected)) {
for (var i = 0; i < expected.length; ++i) {
checkFieldNeededFields(fullPath, expected[i], error_text, queryName, i);
checkNeededFields(fullPath, expected[i], error_text, queryName, i);
if (i >= result.length) {
error_text.push(`${queryName}==> EXPECTED has extra value in array from field ` +
`\`${fullPath}\` (position ${i}): \`${JSON.stringify(expected[i])}\``);
@ -367,7 +371,7 @@ function valueCheck(fullPath, expected, result, error_text, queryName) {
function runParser(query, expected, loaded, loadedFile, queryName) {
var error_text = [];
checkFieldNeededFields("", expected, error_text, queryName, null);
checkNeededFields("", expected, error_text, queryName, null);
if (error_text.length === 0) {
valueCheck('', expected, loaded.parseQuery(query), error_text, queryName);
}