1
Fork 0

Parse idents the same way in both quote string elements and "normal" elements

This commit is contained in:
Guillaume Gomez 2022-04-16 16:33:42 +02:00
parent a6051c76db
commit 6f35475ba5
2 changed files with 45 additions and 33 deletions

View file

@ -176,13 +176,11 @@ window.initSearch = function(rawSearchIndex) {
throw new Error("Cannot use literal search when there is more than one element");
}
parserState.pos += 1;
while (parserState.pos < parserState.length &&
parserState.userQuery[parserState.pos] !== "\"")
{
parserState.pos += 1;
}
var end = getIdentEndPosition(parserState);
if (parserState.pos >= parserState.length) {
throw new Error("Unclosed `\"`");
} else if (parserState.userQuery[end] !== "\"") {
throw new Error(`Unexpected \`${parserState.userQuery[end]}\` in a string element`);
}
// To skip the quote at the end.
parserState.pos += 1;
@ -285,22 +283,15 @@ window.initSearch = function(rawSearchIndex) {
}
/**
* @param {ParsedQuery} query
* This function goes through all characters until it reaches an invalid ident character or the
* end of the query. It returns the position of the last character of the ident.
*
* @param {ParserState} parserState
* @param {Array<QueryElement>} elems - This is where the new {QueryElement} will be added.
* @param {boolean} isInGenerics
*
* @return {integer}
*/
function getNextElem(query, parserState, elems, isInGenerics) {
var generics = [];
var start = parserState.pos;
var end = start;
// We handle the strings on their own mostly to make code easier to follow.
if (parserState.userQuery[parserState.pos] === "\"") {
start += 1;
getStringElem(query, parserState, isInGenerics);
end = parserState.pos - 1;
} else {
function getIdentEndPosition(parserState) {
var end = parserState.pos;
while (parserState.pos < parserState.length) {
var c = parserState.userQuery[parserState.pos];
if (!isIdentCharacter(c)) {
@ -327,6 +318,27 @@ window.initSearch = function(rawSearchIndex) {
parserState.pos += 1;
end = parserState.pos;
}
return end;
}
/**
* @param {ParsedQuery} query
* @param {ParserState} parserState
* @param {Array<QueryElement>} elems - This is where the new {QueryElement} will be added.
* @param {boolean} isInGenerics
*/
function getNextElem(query, parserState, elems, isInGenerics) {
var generics = [];
var start = parserState.pos;
var end;
// We handle the strings on their own mostly to make code easier to follow.
if (parserState.userQuery[parserState.pos] === "\"") {
start += 1;
getStringElem(query, parserState, isInGenerics);
end = parserState.pos - 1;
} else {
end = getIdentEndPosition(parserState);
}
if (parserState.pos < parserState.length &&
parserState.userQuery[parserState.pos] === "<")

View file

@ -275,7 +275,7 @@ function loadSearchJsAndIndex(searchJs, searchIndex, storageJs, crate) {
"parseInput", "getItemsBefore", "getNextElem", "createQueryElement",
"isReturnArrow", "isPathStart", "getStringElem", "newParsedQuery",
"itemTypeFromName", "isEndCharacter", "isErrorCharacter",
"isIdentCharacter", "isSeparatorCharacter"];
"isIdentCharacter", "isSeparatorCharacter", "getIdentEndPosition"];
const functions = ["hasOwnPropertyRustdoc", "onEach"];
ALIASES = {};