Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 4929733

Browse files
Simplify parser syntax
1 parent 99d5520 commit 4929733

File tree

11 files changed

+50
-205
lines changed

11 files changed

+50
-205
lines changed

src/librustdoc/html/static/js/search.js

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,21 @@ window.initSearch = function(rawSearchIndex) {
129129
}
130130

131131
function isSpecialStartCharacter(c) {
132-
return "(<\"".indexOf(c) !== -1;
132+
return "<\"".indexOf(c) !== -1;
133133
}
134134

135135
function isEndCharacter(c) {
136-
return "),>-".indexOf(c) !== -1;
136+
return ",>-".indexOf(c) !== -1;
137137
}
138138

139139
function isStopCharacter(c) {
140140
return isWhitespace(c) || isEndCharacter(c);
141141
}
142142

143+
function isErrorCharacter(c) {
144+
return "()".indexOf(c) !== -1;
145+
}
146+
143147
function itemTypeFromName(typename) {
144148
for (var i = 0, len = itemTypes.length; i < len; ++i) {
145149
if (itemTypes[i] === typename) {
@@ -267,7 +271,9 @@ window.initSearch = function(rawSearchIndex) {
267271
} else {
268272
while (parserState.pos < parserState.length) {
269273
var c = parserState.userQuery[parserState.pos];
270-
if (isStopCharacter(c) || isSpecialStartCharacter(c)) {
274+
if (isErrorCharacter(c)) {
275+
throw new Error(`Unexpected \`${c}\``);
276+
} else if (isStopCharacter(c) || isSpecialStartCharacter(c)) {
271277
break;
272278
}
273279
// If we allow paths ("str::string" for example).
@@ -285,6 +291,9 @@ window.initSearch = function(rawSearchIndex) {
285291
if (parserState.pos < parserState.length &&
286292
parserState.userQuery[parserState.pos] === "<")
287293
{
294+
if (isInGenerics) {
295+
throw new Error("Unexpected `<` after `<`");
296+
}
288297
parserState.pos += 1;
289298
getItemsBefore(query, parserState, generics, ">");
290299
}
@@ -317,12 +326,10 @@ window.initSearch = function(rawSearchIndex) {
317326
continue;
318327
} else if (c === ":" && isPathStart(parserState)) {
319328
throw new Error("Unexpected `::`: paths cannot start with `::`");
320-
} else if (c === "(" || c === ":" || isEndCharacter(c)) {
329+
} else if (c === ":" || isEndCharacter(c)) {
321330
var extra = "";
322331
if (limit === ">") {
323332
extra = "`<`";
324-
} else if (limit === ")") {
325-
extra = "`(`";
326333
} else if (limit === "") {
327334
extra = "`->`";
328335
}
@@ -356,8 +363,6 @@ window.initSearch = function(rawSearchIndex) {
356363
} else if (c === "-" && isReturnArrow(parserState)) {
357364
break;
358365
}
359-
} else if (c == "(") {
360-
break;
361366
} else if (c === ":" &&
362367
parserState.typeFilter === null &&
363368
!isPathStart(parserState))
@@ -391,11 +396,7 @@ window.initSearch = function(rawSearchIndex) {
391396
}
392397
while (parserState.pos < parserState.length) {
393398
c = parserState.userQuery[parserState.pos];
394-
if (query.args.length === 0 && c === "(") {
395-
parserState.pos += 1;
396-
// Check for function/method arguments.
397-
getItemsBefore(query, parserState, query.args, ")");
398-
} else if (isReturnArrow(parserState)) {
399+
if (isReturnArrow(parserState)) {
399400
parserState.pos += 2;
400401
// Get returned elements.
401402
getItemsBefore(query, parserState, query.returned, "");
@@ -419,7 +420,6 @@ window.initSearch = function(rawSearchIndex) {
419420
userQuery: userQuery.toLowerCase(),
420421
typeFilter: NO_TYPE_FILTER,
421422
elems: [],
422-
args: [],
423423
returned: [],
424424
// Total number of "top" elements (does not include generics).
425425
foundElems: 0,
@@ -466,19 +466,19 @@ window.initSearch = function(rawSearchIndex) {
466466
*
467467
* The supported syntax by this parser is as follow:
468468
*
469-
* ident = *1(ALPHA / DIGIT)
470-
* path = ident *WS *(DOUBLE-COLON *WS ident)
471-
* arg = path *WS [generics]
469+
* ident = *(ALPHA / DIGIT)
470+
* path = ident *(DOUBLE-COLON ident)
471+
* arg = path [generics]
472+
* arg-without-generic = path
472473
* nonempty-arg-list = arg *WS *(COMMA *WS arg)
473-
* generics = OPEN-ANGLE-BRACKET *WS nonempty-arg-list *WS CLOSE-ANGLE-BRACKET
474-
* function-args = OPEN-PAREN *WS [nonempty-arg-list] *WS END-PAREN
475-
* return-args = RETURN-ARROW *WS function-args
474+
* nonempty-arg-list-without-generics = arg-without-generic *WS *(COMMA *WS arg-without-generic)
475+
* generics = OPEN-ANGLE-BRACKET *WS nonempty-arg-list-without-generics *WS CLOSE-ANGLE-BRACKET
476+
* return-args = RETURN-ARROW *WS nonempty-arg-list
476477
*
477478
* exact-search = [type-filter *WS COLON] *WS QUOTE ident QUOTE *WS [generics]
478479
* type-search = [type-filter *WS COLON] *WS path *WS generics
479-
* function-search = path *WS function-args *WS [return-args]
480480
*
481-
* query = *WS (exact-search / type-search / function-search / return-args) *WS
481+
* query = *WS (exact-search / type-search / return-args) *WS
482482
*
483483
* type-filter = (
484484
* "mod" /
@@ -510,8 +510,6 @@ window.initSearch = function(rawSearchIndex) {
510510
*
511511
* OPEN-ANGLE-BRACKET = "<"
512512
* CLOSE-ANGLE-BRACKET = ">"
513-
* OPEN-PAREN = "("
514-
* END-PAREN = ")"
515513
* COLON = ":"
516514
* DOUBLE-COLON = "::"
517515
* QUOTE = %x22
@@ -554,7 +552,7 @@ window.initSearch = function(rawSearchIndex) {
554552
// case.
555553
query.literalSearch = parserState.totalElems > 1;
556554
}
557-
query.foundElems = query.elems.length + query.args.length + query.returned.length;
555+
query.foundElems = query.elems.length + query.returned.length;
558556
if (query.foundElems === 0 && parserState.length !== 0) {
559557
// In this case, we'll simply keep whatever was entered by the user...
560558
createQueryElement(query, parserState, query.elems, userQuery, []);
@@ -743,6 +741,11 @@ window.initSearch = function(rawSearchIndex) {
743741
function checkGenerics(row, elem, defaultLev) {
744742
if (row.length <= GENERICS_DATA || row[GENERICS_DATA].length === 0) {
745743
return elem.generics.length === 0 ? defaultLev : MAX_LEV_DISTANCE + 1;
744+
} else if (row[GENERICS_DATA].length > 0 && row[GENERICS_DATA][0][NAME] === "") {
745+
if (row.length > GENERICS_DATA) {
746+
return checkGenerics(row[GENERICS_DATA][0], elem, defaultLev);
747+
}
748+
return elem.generics.length === 0 ? defaultLev : MAX_LEV_DISTANCE + 1;
746749
}
747750
// The names match, but we need to be sure that all generics kinda
748751
// match as well.
@@ -751,7 +754,15 @@ window.initSearch = function(rawSearchIndex) {
751754
var elems = {};
752755
for (var x = 0, length = row[GENERICS_DATA].length; x < length; ++x) {
753756
elem_name = row[GENERICS_DATA][x][NAME];
754-
if (!elems[elem_name]) {
757+
if (elem_name === "") {
758+
// Pure generic, needs to check into it.
759+
if (checkGenerics(
760+
row[GENERICS_DATA][x], elem, MAX_LEV_DISTANCE + 1) !== 0) {
761+
return MAX_LEV_DISTANCE + 1;
762+
}
763+
continue;
764+
}
765+
if (elems[elem_name] === undefined) {
755766
elems[elem_name] = 0;
756767
}
757768
elems[elem_name] += 1;
@@ -1216,9 +1227,6 @@ window.initSearch = function(rawSearchIndex) {
12161227
if (!checkArgs(parsedQuery.elems, findArg)) {
12171228
return;
12181229
}
1219-
if (!checkArgs(parsedQuery.args, findArg)) {
1220-
return;
1221-
}
12221230
if (!checkArgs(parsedQuery.returned, checkReturned)) {
12231231
return;
12241232
}
@@ -1231,7 +1239,7 @@ window.initSearch = function(rawSearchIndex) {
12311239
}
12321240

12331241
function innerRunQuery() {
1234-
var elem, i, nSearchWords, in_args, in_returned, row;
1242+
var elem, i, nSearchWords, in_returned, row;
12351243

12361244
if (parsedQuery.foundElems === 1) {
12371245
if (parsedQuery.elems.length === 1) {
@@ -1241,14 +1249,6 @@ window.initSearch = function(rawSearchIndex) {
12411249
// returned).
12421250
handleSingleArg(searchIndex[i], i, elem);
12431251
}
1244-
} else if (parsedQuery.args.length === 1) {
1245-
// We received one argument to check, so looking into args.
1246-
elem = parsedQuery.args[0];
1247-
for (i = 0, nSearchWords = searchWords.length; i < nSearchWords; ++i) {
1248-
row = searchIndex[i];
1249-
in_args = findArg(row, elem, parsedQuery.typeFilter);
1250-
addIntoResults(results_in_args, row.id, i, -1, in_args);
1251-
}
12521252
} else if (parsedQuery.returned.length === 1) {
12531253
// We received one returned argument to check, so looking into returned values.
12541254
elem = parsedQuery.returned[0];
@@ -1262,9 +1262,7 @@ window.initSearch = function(rawSearchIndex) {
12621262
var container = results_others;
12631263
// In the special case where only a "returned" information is available, we want to
12641264
// put the information into the "results_returned" dict.
1265-
if (parsedQuery.returned.length !== 0 && parsedQuery.args.length === 0 &&
1266-
parsedQuery.elems.length === 0)
1267-
{
1265+
if (parsedQuery.returned.length !== 0 && parsedQuery.elems.length === 0) {
12681266
container = results_returned;
12691267
}
12701268
for (i = 0, nSearchWords = searchWords.length; i < nSearchWords; ++i) {

src/test/rustdoc-js-std/parser-errors.js

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const QUERY = [
1919

2020
const PARSED = [
2121
{
22-
args: [],
2322
elems: [],
2423
foundElems: 0,
2524
original: "<\"P\">",
@@ -29,7 +28,6 @@ const PARSED = [
2928
error: "`\"` cannot be used in generics",
3029
},
3130
{
32-
args: [],
3331
elems: [],
3432
foundElems: 0,
3533
original: "\"P\" \"P\"",
@@ -39,7 +37,6 @@ const PARSED = [
3937
error: "Cannot have more than one literal search element",
4038
},
4139
{
42-
args: [],
4340
elems: [],
4441
foundElems: 0,
4542
original: "P \"P\"",
@@ -49,7 +46,6 @@ const PARSED = [
4946
error: "Cannot use literal search when there is more than one element",
5047
},
5148
{
52-
args: [],
5349
elems: [],
5450
foundElems: 0,
5551
original: "\"p\" p",
@@ -59,7 +55,6 @@ const PARSED = [
5955
error: "You cannot have more than one element if you use quotes",
6056
},
6157
{
62-
args: [],
6358
elems: [],
6459
foundElems: 0,
6560
original: "\"const\": p",
@@ -69,7 +64,6 @@ const PARSED = [
6964
error: "You cannot use quotes on type filter",
7065
},
7166
{
72-
args: [],
7367
elems: [],
7468
foundElems: 0,
7569
original: "<:a>",
@@ -79,7 +73,6 @@ const PARSED = [
7973
error: "Unexpected `:` after `<`",
8074
},
8175
{
82-
args: [],
8376
elems: [],
8477
foundElems: 0,
8578
original: "<::a>",
@@ -89,17 +82,15 @@ const PARSED = [
8982
error: "Unexpected `::`: paths cannot start with `::`",
9083
},
9184
{
92-
args: [],
9385
elems: [],
9486
foundElems: 0,
9587
original: "((a))",
9688
returned: [],
9789
typeFilter: -1,
9890
userQuery: "((a))",
99-
error: "Unexpected `(` after `(`",
91+
error: "Unexpected `(`",
10092
},
10193
{
102-
args: [],
10394
elems: [],
10495
foundElems: 0,
10596
original: "->,a",
@@ -109,17 +100,15 @@ const PARSED = [
109100
error: "Unexpected `,` after `->`",
110101
},
111102
{
112-
args: [],
113103
elems: [],
114104
foundElems: 0,
115105
original: "(p -> p",
116106
returned: [],
117107
typeFilter: -1,
118108
userQuery: "(p -> p",
119-
error: "Unexpected `-` after `(`",
109+
error: "Unexpected `(`",
120110
},
121111
{
122-
args: [],
123112
elems: [],
124113
foundElems: 0,
125114
original: "::a::b",
@@ -129,7 +118,6 @@ const PARSED = [
129118
error: "Paths cannot start with `::`",
130119
},
131120
{
132-
args: [],
133121
elems: [],
134122
foundElems: 0,
135123
original: "a::::b",
@@ -139,7 +127,6 @@ const PARSED = [
139127
error: "Unexpected `::::`",
140128
},
141129
{
142-
args: [],
143130
elems: [],
144131
foundElems: 0,
145132
original: "a::b::",
@@ -149,7 +136,6 @@ const PARSED = [
149136
error: "Paths cannot end with `::`",
150137
},
151138
{
152-
args: [],
153139
elems: [],
154140
foundElems: 0,
155141
original: ":a",
@@ -159,7 +145,6 @@ const PARSED = [
159145
error: "Expected type filter before `:`",
160146
},
161147
{
162-
args: [],
163148
elems: [],
164149
foundElems: 0,
165150
original: "a b:",
@@ -169,17 +154,15 @@ const PARSED = [
169154
error: "Unexpected `:`",
170155
},
171156
{
172-
args: [],
173157
elems: [],
174158
foundElems: 0,
175159
original: "a (b:",
176160
returned: [],
177161
typeFilter: -1,
178162
userQuery: "a (b:",
179-
error: "Unexpected `:` after `(`",
163+
error: "Unexpected `(`",
180164
},
181165
{
182-
args: [],
183166
elems: [],
184167
foundElems: 0,
185168
original: "{:",

src/test/rustdoc-js-std/parser-filter.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const QUERY = ['fn:foo', 'enum : foo', 'macro<f>:foo'];
22

33
const PARSED = [
44
{
5-
args: [],
65
elems: [{
76
name: "foo",
87
fullPath: ["foo"],
@@ -18,7 +17,6 @@ const PARSED = [
1817
error: null,
1918
},
2019
{
21-
args: [],
2220
elems: [{
2321
name: "foo",
2422
fullPath: ["foo"],
@@ -34,7 +32,6 @@ const PARSED = [
3432
error: null,
3533
},
3634
{
37-
args: [],
3835
elems: [],
3936
foundElems: 0,
4037
original: "macro<f>:foo",

0 commit comments

Comments
 (0)