@libreflare/worker-runtime 0.8.2 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +234 -23
- package/dist/log-handler.d.ts.map +1 -1
- package/dist/log-handler.js +12 -6
- package/dist/log-handler.js.map +1 -1
- package/dist/rule-evaluator/access-values.d.ts +19 -6
- package/dist/rule-evaluator/access-values.d.ts.map +1 -1
- package/dist/rule-evaluator/access-values.js +33 -8
- package/dist/rule-evaluator/access-values.js.map +1 -1
- package/dist/rule-evaluator/analysis.d.ts +63 -11
- package/dist/rule-evaluator/analysis.d.ts.map +1 -1
- package/dist/rule-evaluator/analysis.js +60 -43
- package/dist/rule-evaluator/analysis.js.map +1 -1
- package/dist/rule-evaluator/ast.d.ts +31 -18
- package/dist/rule-evaluator/ast.d.ts.map +1 -1
- package/dist/rule-evaluator/ast.js +3 -4
- package/dist/rule-evaluator/ast.js.map +1 -1
- package/dist/rule-evaluator/constants.d.ts +1 -1
- package/dist/rule-evaluator/constants.d.ts.map +1 -1
- package/dist/rule-evaluator/constants.js +3 -0
- package/dist/rule-evaluator/constants.js.map +1 -1
- package/dist/rule-evaluator/country-codes.generated.js +1 -1
- package/dist/rule-evaluator/custom-lists.d.ts +0 -6
- package/dist/rule-evaluator/custom-lists.d.ts.map +1 -1
- package/dist/rule-evaluator/custom-lists.js +1 -12
- package/dist/rule-evaluator/custom-lists.js.map +1 -1
- package/dist/rule-evaluator/diagnostics.d.ts +1 -1
- package/dist/rule-evaluator/diagnostics.d.ts.map +1 -1
- package/dist/rule-evaluator/diagnostics.js +18 -0
- package/dist/rule-evaluator/diagnostics.js.map +1 -1
- package/dist/rule-evaluator/engine-observations.generated.d.ts +20 -0
- package/dist/rule-evaluator/engine-observations.generated.d.ts.map +1 -1
- package/dist/rule-evaluator/engine-observations.generated.js +5 -0
- package/dist/rule-evaluator/engine-observations.generated.js.map +1 -1
- package/dist/rule-evaluator/evaluator.d.ts +4 -2
- package/dist/rule-evaluator/evaluator.d.ts.map +1 -1
- package/dist/rule-evaluator/evaluator.js +5 -4
- package/dist/rule-evaluator/evaluator.js.map +1 -1
- package/dist/rule-evaluator/explain.d.ts +2 -1
- package/dist/rule-evaluator/explain.d.ts.map +1 -1
- package/dist/rule-evaluator/explain.js +49 -7
- package/dist/rule-evaluator/explain.js.map +1 -1
- package/dist/rule-evaluator/field-descriptions.generated.d.ts +13 -0
- package/dist/rule-evaluator/field-descriptions.generated.d.ts.map +1 -1
- package/dist/rule-evaluator/field-descriptions.generated.js +33 -0
- package/dist/rule-evaluator/field-descriptions.generated.js.map +1 -1
- package/dist/rule-evaluator/fields.d.ts +13 -1
- package/dist/rule-evaluator/fields.d.ts.map +1 -1
- package/dist/rule-evaluator/fields.js +71 -17
- package/dist/rule-evaluator/fields.js.map +1 -1
- package/dist/rule-evaluator/function-descriptions.d.ts +2 -0
- package/dist/rule-evaluator/function-descriptions.d.ts.map +1 -1
- package/dist/rule-evaluator/function-descriptions.generated.d.ts +66 -10
- package/dist/rule-evaluator/function-descriptions.generated.d.ts.map +1 -1
- package/dist/rule-evaluator/function-descriptions.generated.js +129 -11
- package/dist/rule-evaluator/function-descriptions.generated.js.map +1 -1
- package/dist/rule-evaluator/function-descriptions.js.map +1 -1
- package/dist/rule-evaluator/function-signature-validation.d.ts +2 -0
- package/dist/rule-evaluator/function-signature-validation.d.ts.map +1 -1
- package/dist/rule-evaluator/function-signature-validation.js +169 -19
- package/dist/rule-evaluator/function-signature-validation.js.map +1 -1
- package/dist/rule-evaluator/functions.d.ts +24 -1
- package/dist/rule-evaluator/functions.d.ts.map +1 -1
- package/dist/rule-evaluator/functions.js +423 -95
- package/dist/rule-evaluator/functions.js.map +1 -1
- package/dist/rule-evaluator/ip-literals.d.ts +56 -0
- package/dist/rule-evaluator/ip-literals.d.ts.map +1 -0
- package/dist/rule-evaluator/ip-literals.js +162 -0
- package/dist/rule-evaluator/ip-literals.js.map +1 -0
- package/dist/rule-evaluator/log-sample.d.ts.map +1 -1
- package/dist/rule-evaluator/log-sample.js +14 -2
- package/dist/rule-evaluator/log-sample.js.map +1 -1
- package/dist/rule-evaluator/match-chain.d.ts +2 -1
- package/dist/rule-evaluator/match-chain.d.ts.map +1 -1
- package/dist/rule-evaluator/match-chain.js +12 -17
- package/dist/rule-evaluator/match-chain.js.map +1 -1
- package/dist/rule-evaluator/minifier.d.ts +1 -1
- package/dist/rule-evaluator/minifier.d.ts.map +1 -1
- package/dist/rule-evaluator/minifier.js +15 -14
- package/dist/rule-evaluator/minifier.js.map +1 -1
- package/dist/rule-evaluator/normalizer.d.ts +1 -1
- package/dist/rule-evaluator/normalizer.d.ts.map +1 -1
- package/dist/rule-evaluator/normalizer.js +4 -5
- package/dist/rule-evaluator/normalizer.js.map +1 -1
- package/dist/rule-evaluator/operators.d.ts +4 -0
- package/dist/rule-evaluator/operators.d.ts.map +1 -1
- package/dist/rule-evaluator/operators.js +26 -98
- package/dist/rule-evaluator/operators.js.map +1 -1
- package/dist/rule-evaluator/outcome.d.ts +2 -0
- package/dist/rule-evaluator/outcome.d.ts.map +1 -1
- package/dist/rule-evaluator/outcome.js +0 -6
- package/dist/rule-evaluator/outcome.js.map +1 -1
- package/dist/rule-evaluator/parse-cache.d.ts +9 -2
- package/dist/rule-evaluator/parse-cache.d.ts.map +1 -1
- package/dist/rule-evaluator/parse-cache.js +10 -4
- package/dist/rule-evaluator/parse-cache.js.map +1 -1
- package/dist/rule-evaluator/parser.d.ts +181 -21
- package/dist/rule-evaluator/parser.d.ts.map +1 -1
- package/dist/rule-evaluator/parser.js +561 -93
- package/dist/rule-evaluator/parser.js.map +1 -1
- package/dist/rule-evaluator/quantifier.d.ts +96 -2
- package/dist/rule-evaluator/quantifier.d.ts.map +1 -1
- package/dist/rule-evaluator/quantifier.js +263 -22
- package/dist/rule-evaluator/quantifier.js.map +1 -1
- package/dist/rule-evaluator/query-args.d.ts +23 -0
- package/dist/rule-evaluator/query-args.d.ts.map +1 -0
- package/dist/rule-evaluator/query-args.js +46 -0
- package/dist/rule-evaluator/query-args.js.map +1 -0
- package/dist/rule-evaluator/rule-language-types.d.ts +28 -17
- package/dist/rule-evaluator/rule-language-types.d.ts.map +1 -1
- package/dist/rule-evaluator/rule-language-types.js +117 -42
- package/dist/rule-evaluator/rule-language-types.js.map +1 -1
- package/dist/rule-evaluator/string-literals.d.ts +20 -0
- package/dist/rule-evaluator/string-literals.d.ts.map +1 -0
- package/dist/rule-evaluator/string-literals.js +44 -0
- package/dist/rule-evaluator/string-literals.js.map +1 -0
- package/dist/rule-evaluator/tokenizer.d.ts +34 -5
- package/dist/rule-evaluator/tokenizer.d.ts.map +1 -1
- package/dist/rule-evaluator/tokenizer.js +320 -74
- package/dist/rule-evaluator/tokenizer.js.map +1 -1
- package/dist/rule-evaluator/transformation.d.ts +2 -2
- package/dist/rule-evaluator/transformation.d.ts.map +1 -1
- package/dist/rule-evaluator/transformation.js +4 -5
- package/dist/rule-evaluator/transformation.js.map +1 -1
- package/dist/rule-evaluator/validation.d.ts.map +1 -1
- package/dist/rule-evaluator/validation.js +261 -122
- package/dist/rule-evaluator/validation.js.map +1 -1
- package/dist/rule-evaluator.d.ts +4 -2
- package/dist/rule-evaluator.d.ts.map +1 -1
- package/dist/rule-evaluator.js +3 -1
- package/dist/rule-evaluator.js.map +1 -1
- package/dist/runtime-config.d.ts.map +1 -1
- package/dist/runtime-config.js +4 -3
- package/dist/runtime-config.js.map +1 -1
- package/dist/tools/explain-rule-miss.js +4 -6
- package/dist/tools/explain-rule-miss.js.map +1 -1
- package/package.json +2 -2
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
import { RULE_EXPRESSION_FUNCTIONS } from './constants.js';
|
|
2
|
-
import { isRuleExpressionIpAddress, isRuleExpressionIpLiteral } from './custom-lists.js';
|
|
3
2
|
import { KNOWN_AVAILABLE_FIELDS, KNOWN_UNAVAILABLE_FIELDS } from './fields.js';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
3
|
+
import { formatIpAddressValue, readIpAddress, readIpListMember } from './ip-literals.js';
|
|
4
|
+
import { acceptsComparisonArgument, canReturnBoolean, inferRuleExpressionArgument, integerLiteralValue, isArrayExpandedNode, isComparableValue, parameterDataType, ruleExpressionComparisonTypeFamily, } from './rule-language-types.js';
|
|
5
|
+
import { isDynamicValueNode, unhandledNode, } from './ast.js';
|
|
6
|
+
import { stringLineBreak, tokenize, tokenLength, trimRuleExpression, } from './tokenizer.js';
|
|
7
|
+
/**
|
|
8
|
+
* The type to read a literal compared with a value of a type as; see
|
|
9
|
+
* LiteralType. An array's is its elements', which any() and all() compare
|
|
10
|
+
* one by one, as in `any(cf.waf.content_scan.obj_sizes[*] in {10-20})`.
|
|
11
|
+
*/
|
|
12
|
+
export function literalTypeOf(type) {
|
|
13
|
+
const family = ruleExpressionComparisonTypeFamily(type.kind === 'array' ? type.element : type);
|
|
14
|
+
return family === 'number' || family === 'ip' ? family : null;
|
|
15
|
+
}
|
|
7
16
|
const VALUE_CONTEXT_LOCATIONS = {
|
|
8
17
|
operator: 'on right side of operator',
|
|
9
18
|
function: 'as function argument',
|
|
@@ -194,8 +203,9 @@ function callName(tokens, partners, index) {
|
|
|
194
203
|
* Whether the call whose `(` is at `opener` can take the comparison at an
|
|
195
204
|
* index in the value argument it reads, as far as the tokens tell: not one
|
|
196
205
|
* right after a literal argument, which Cloudflare never compares, and one
|
|
197
|
-
* that callTakesComparison() allows
|
|
198
|
-
* see
|
|
206
|
+
* that callTakesComparison() allows, over each element where the argument's
|
|
207
|
+
* access path has `[*]`. The parser decides the rest from types; see
|
|
208
|
+
* takesComparison().
|
|
199
209
|
*/
|
|
200
210
|
function readsComparison(tokens, partners, opener, index, name, argumentIndex) {
|
|
201
211
|
const end = indexBefore(tokens, partners, index);
|
|
@@ -205,7 +215,18 @@ function readsComparison(tokens, partners, opener, index, name, argumentIndex) {
|
|
|
205
215
|
const before = indexBefore(tokens, partners, start);
|
|
206
216
|
if (literal && (before === opener || tokens[before].kind === 'Comma'))
|
|
207
217
|
return false;
|
|
208
|
-
return callTakesComparison(name, argumentIndex, []);
|
|
218
|
+
return callTakesComparison(name, argumentIndex, [], expandsAt(tokens, partners, end), tokens[index]);
|
|
219
|
+
}
|
|
220
|
+
/** Whether the access path that ends at an index has a `[*]`, as the tokens show it. */
|
|
221
|
+
function expandsAt(tokens, partners, end) {
|
|
222
|
+
let index = end;
|
|
223
|
+
while (tokens[index]?.kind === 'RBracket' && partners[index] >= 0) {
|
|
224
|
+
const opener = partners[index];
|
|
225
|
+
if (opener === index - 2 && tokens[index - 1].kind === 'Star')
|
|
226
|
+
return true;
|
|
227
|
+
index = opener - 1;
|
|
228
|
+
}
|
|
229
|
+
return false;
|
|
209
230
|
}
|
|
210
231
|
/** Whether the token at an index is a `[` that starts an access: one after a name, or after what ends one. */
|
|
211
232
|
function startsAccess(tokens, index) {
|
|
@@ -290,21 +311,30 @@ function endsOperand(token) {
|
|
|
290
311
|
* can take one; see callTakesComparison(). Otherwise the comparison compares
|
|
291
312
|
* the call's result.
|
|
292
313
|
*/
|
|
293
|
-
function takesComparison(call) {
|
|
314
|
+
function takesComparison(call, operator) {
|
|
294
315
|
const value = call.argumentValue;
|
|
295
316
|
if (value === 'literal' || (value !== null && !isComparableValue(value)))
|
|
296
317
|
return false;
|
|
297
|
-
return callTakesComparison(call.functionName, call.argumentIndex, call.args ?? []);
|
|
318
|
+
return callTakesComparison(call.functionName, call.argumentIndex, call.args ?? [], value !== null && isArrayExpandedNode(value), operator);
|
|
298
319
|
}
|
|
299
320
|
/**
|
|
300
321
|
* Whether a call without its `)` can take a comparison as its argument at an
|
|
301
322
|
* index: the parameter accepts one, with T fixed by the arguments before it
|
|
302
323
|
* (see acceptsComparisonArgument()), and the call's result can be a Boolean.
|
|
303
324
|
* A call that returns no Boolean, as to_string() does, could stand only where
|
|
304
|
-
* a value does, and is compared far more often than given a comparison.
|
|
325
|
+
* a value does, and is compared far more often than given a comparison. A
|
|
326
|
+
* comparison over each element, `elementwise`, is an array of Booleans,
|
|
327
|
+
* which such a call takes too, as `len()` does, unless the `operator` orders:
|
|
328
|
+
* a count is ordered far more often than elements are, as in
|
|
329
|
+
* `any(len(http.request.headers[*])[*] gt 1)`.
|
|
305
330
|
*/
|
|
306
|
-
function callTakesComparison(name, index, before) {
|
|
307
|
-
|
|
331
|
+
function callTakesComparison(name, index, before, elementwise, operator) {
|
|
332
|
+
if (canReturnBoolean(name))
|
|
333
|
+
return acceptsComparisonArgument(name, index, before, elementwise);
|
|
334
|
+
return elementwise && !isOrderingOperator(operator) && acceptsComparisonArgument(name, index, before, true);
|
|
335
|
+
}
|
|
336
|
+
function isOrderingOperator(token) {
|
|
337
|
+
return token.kind === 'Operator' && (token.value === 'lt' || token.value === 'le' || token.value === 'gt' || token.value === 'ge');
|
|
308
338
|
}
|
|
309
339
|
const NO_OPERATOR_CHARACTERS = new Set();
|
|
310
340
|
/**
|
|
@@ -362,7 +392,7 @@ function isUnknownCharacter(token) {
|
|
|
362
392
|
}
|
|
363
393
|
/** Whether a token is malformed: the tokenizer reported it already. */
|
|
364
394
|
function isMalformed(token) {
|
|
365
|
-
return token.kind === 'Invalid' || token.closed === false;
|
|
395
|
+
return token.kind === 'Invalid' || token.closed === false || token.invalidEscape === true;
|
|
366
396
|
}
|
|
367
397
|
const CLOSER_TEXT = { group: ')', call: ')', quantifier: ')', access: ']', list: '}' };
|
|
368
398
|
function readHiddenClosers(text) {
|
|
@@ -381,6 +411,42 @@ const MAX_ARRAY_INDEX = 0xffff_ffff;
|
|
|
381
411
|
/** Cloudflare stores integers as signed 64-bit values. */
|
|
382
412
|
const MIN_INTEGER = -(2n ** 63n);
|
|
383
413
|
const MAX_INTEGER = 2n ** 63n - 1n;
|
|
414
|
+
/** Number literal text that suggests an IP address when no type is expected: a `:`, a `/`, or three dotted parts. */
|
|
415
|
+
const IP_ADDRESS_LIKE = /[:/]|\d\.\d+\.\d/;
|
|
416
|
+
/** The same for a list member, where an IPv4 address may have fewer parts. */
|
|
417
|
+
const IP_MEMBER_LIKE = /[:/]|\d\.\d/;
|
|
418
|
+
/** An integer literal's value, when it is one in the 64-bit range. */
|
|
419
|
+
function int64LiteralValue(text) {
|
|
420
|
+
const value = integerLiteralValue(text);
|
|
421
|
+
return value !== null && value >= MIN_INTEGER && value <= MAX_INTEGER ? value : null;
|
|
422
|
+
}
|
|
423
|
+
/** The bounds of a list member that is a valid integer or integer range. */
|
|
424
|
+
function integerRangeBounds(text) {
|
|
425
|
+
const rangeAt = text.indexOf('..');
|
|
426
|
+
const first = int64LiteralValue(rangeAt < 0 ? text : text.slice(0, rangeAt));
|
|
427
|
+
const last = rangeAt < 0 ? first : int64LiteralValue(text.slice(rangeAt + 2));
|
|
428
|
+
return first !== null && last !== null && first <= last ? [first, last] : null;
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* The list members a number token holds on an Integer side. Two-digit
|
|
432
|
+
* numbers that `-` joins, which the tokenizer reads as hex bytes, are each a
|
|
433
|
+
* member there, as Cloudflare reads `10-20` as 10 and -20.
|
|
434
|
+
*/
|
|
435
|
+
function integerSideMembers(token) {
|
|
436
|
+
if (!/^\d{2}(?:-\d{2})+$/.test(token.value))
|
|
437
|
+
return [token];
|
|
438
|
+
let pos = token.pos;
|
|
439
|
+
return token.value.match(/-?\d{2}/g).map(value => {
|
|
440
|
+
const member = { kind: 'Number', value, pos, length: value.length };
|
|
441
|
+
pos += value.length;
|
|
442
|
+
return member;
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
/** The type a literal compared with a left side is read as; see LiteralType. */
|
|
446
|
+
function leftLiteralType(left) {
|
|
447
|
+
const info = inferRuleExpressionArgument(left);
|
|
448
|
+
return info.invalid ? null : literalTypeOf(info.type);
|
|
449
|
+
}
|
|
384
450
|
export class Parser {
|
|
385
451
|
source;
|
|
386
452
|
diagnostics;
|
|
@@ -402,11 +468,34 @@ export class Parser {
|
|
|
402
468
|
* The access paths where recovery in an access without its `]` skipped
|
|
403
469
|
* past the token with the error. What it skipped may hold the `]` and a
|
|
404
470
|
* comparison operator, which would make the field or call before the path
|
|
405
|
-
* a comparison's left side; see InvalidNode.operand.
|
|
406
|
-
*
|
|
407
|
-
*
|
|
471
|
+
* a comparison's left side; see InvalidNode.operand. isCutShort() looks up
|
|
472
|
+
* the path of the node it gets, so the mark reaches it however the node
|
|
473
|
+
* does, as a function argument or a value read on its own too.
|
|
408
474
|
*/
|
|
409
475
|
cutShortAccessPaths = new WeakSet();
|
|
476
|
+
/**
|
|
477
|
+
* The calls without their `)` whose last argument recovery cut short (see
|
|
478
|
+
* ParsedArguments.cutShort), kept as cutShortAccessPaths is. A copy of a
|
|
479
|
+
* call node is not in the set.
|
|
480
|
+
*/
|
|
481
|
+
cutShortCalls = new WeakSet();
|
|
482
|
+
/** Whether the input holds a line break, which only then a string can span; see reportMultilineStrings(). */
|
|
483
|
+
multiline;
|
|
484
|
+
/** Whether the input has a syntax error, from the parser, or from the tokenizer in input over lines. */
|
|
485
|
+
syntaxError;
|
|
486
|
+
/**
|
|
487
|
+
* The quoted strings it read as a value the other way than the tokenizer
|
|
488
|
+
* did, by position, with whether each is the value of `matches`; see
|
|
489
|
+
* parseSource().
|
|
490
|
+
*/
|
|
491
|
+
misreadStrings;
|
|
492
|
+
/**
|
|
493
|
+
* The left side of the comparison whose value parseComparisonValue()
|
|
494
|
+
* reads, kept here rather than passed, so that the frame each nested
|
|
495
|
+
* comparison adds stays small. Nothing that runs before it reads a literal
|
|
496
|
+
* value parses another comparison, so it is still that comparison's.
|
|
497
|
+
*/
|
|
498
|
+
comparisonLeft = null;
|
|
410
499
|
constructor(tokens, source, diagnostics) {
|
|
411
500
|
this.source = source;
|
|
412
501
|
this.diagnostics = diagnostics;
|
|
@@ -414,7 +503,10 @@ export class Parser {
|
|
|
414
503
|
this.tokens = read.tokens;
|
|
415
504
|
this.operatorCharacters = read.operatorCharacters;
|
|
416
505
|
this.partners = pairDelimiters(this.tokens);
|
|
417
|
-
this.hiddenClosers = readHiddenClosers(this.hiddenText(
|
|
506
|
+
this.hiddenClosers = readHiddenClosers(this.hiddenText());
|
|
507
|
+
this.multiline = /[\r\n]/.test(source);
|
|
508
|
+
// Most input is one line, which needs no scan.
|
|
509
|
+
this.syntaxError = this.multiline && tokens.some(isMalformed);
|
|
418
510
|
}
|
|
419
511
|
parse() {
|
|
420
512
|
const node = this.parseOr();
|
|
@@ -422,8 +514,108 @@ export class Parser {
|
|
|
422
514
|
// Recovery at the top level runs to the end, so this is only a safety net.
|
|
423
515
|
this.reportAtCurrent('unexpected tokens after expression end', { id: 'syntax.unexpected_tokens_after_expression_end' });
|
|
424
516
|
}
|
|
517
|
+
this.reportMultilineStrings();
|
|
425
518
|
return node;
|
|
426
519
|
}
|
|
520
|
+
/**
|
|
521
|
+
* Parses the input as exactly one value. A condition in its place reads as
|
|
522
|
+
* invalid, with an error. What follows the value is read as after an
|
|
523
|
+
* operand at the top level, and a field or call that recovery cuts short
|
|
524
|
+
* is kept as fieldCondition() keeps it, since the text may be a comparison.
|
|
525
|
+
* `literalType` is the type of the side the value is for; see LiteralType.
|
|
526
|
+
*/
|
|
527
|
+
parseValueExpression(afterMatches = false, literalType = null) {
|
|
528
|
+
const node = this.readValueExpression(afterMatches, literalType);
|
|
529
|
+
this.reportMultilineStrings();
|
|
530
|
+
return node;
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Warns at each closed quoted string whose text holds a line break, when
|
|
534
|
+
* the input has a syntax error. A string may hold one, but a missing
|
|
535
|
+
* closing quote takes in the lines after it, up to the next `"`, so the
|
|
536
|
+
* errors land after the mistake, and the warning points back at it. A line
|
|
537
|
+
* break in an escape, or in a regex class, can't come from a missing quote;
|
|
538
|
+
* see stringLineBreak().
|
|
539
|
+
*/
|
|
540
|
+
reportMultilineStrings() {
|
|
541
|
+
if (!this.multiline || !this.syntaxError)
|
|
542
|
+
return;
|
|
543
|
+
for (const token of this.tokens) {
|
|
544
|
+
if (token.kind !== 'String' || token.closed === false)
|
|
545
|
+
continue;
|
|
546
|
+
const lineBreak = stringLineBreak(this.source, token);
|
|
547
|
+
if (lineBreak < 0)
|
|
548
|
+
continue;
|
|
549
|
+
this.diagnostics?.push({
|
|
550
|
+
severity: 'warning',
|
|
551
|
+
message: 'string literal spans lines; check for a missing closing quote',
|
|
552
|
+
pos: token.pos,
|
|
553
|
+
length: lineBreak - token.pos,
|
|
554
|
+
id: 'syntax.multiline_string_literal',
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
readValueExpression(afterMatches, literalType) {
|
|
559
|
+
if (startsArgument(this.peekPastStrayClosers(0)))
|
|
560
|
+
this.skipStrayClosers();
|
|
561
|
+
const start = this.peek();
|
|
562
|
+
if (start.kind === 'LParen' || (start.kind === 'Operator' && start.value === 'not'))
|
|
563
|
+
return this.conditionInsteadOfValue(start.pos);
|
|
564
|
+
if (!startsValue(start)) {
|
|
565
|
+
// A malformed token has its own error.
|
|
566
|
+
if (start.kind !== 'Invalid')
|
|
567
|
+
this.reportUnexpectedToken(start);
|
|
568
|
+
if (afterMatches)
|
|
569
|
+
this.noteMatchesValueAfterNoise();
|
|
570
|
+
return this.readRest(start.pos);
|
|
571
|
+
}
|
|
572
|
+
const value = this.parseLoneValue(afterMatches, literalType);
|
|
573
|
+
this.skipStrayClosers();
|
|
574
|
+
if (isComparisonStart(this.tokens, this.pos))
|
|
575
|
+
return this.conditionInsteadOfValue(start.pos);
|
|
576
|
+
const recoveryCutsShort = this.finishOperand();
|
|
577
|
+
if (isLogicalOperator(this.peek()))
|
|
578
|
+
return this.conditionInsteadOfValue(start.pos);
|
|
579
|
+
if (!isDynamicValueNode(value) || !this.isCutShort(value, recoveryCutsShort))
|
|
580
|
+
return value;
|
|
581
|
+
return this.invalidNode(value.pos, value.length, value);
|
|
582
|
+
}
|
|
583
|
+
/** The value at the current token. A name is a field, since a value read on its own has no side. */
|
|
584
|
+
parseLoneValue(afterMatches, literalType) {
|
|
585
|
+
const token = this.peek();
|
|
586
|
+
// No name but a list's reaches parseValue() here, so its context goes unused.
|
|
587
|
+
if (token.kind !== 'Identifier' || token.value.startsWith('$'))
|
|
588
|
+
return this.parseValue('operator', afterMatches, literalType);
|
|
589
|
+
if (isFunctionName(token.value) && this.peekPastStrayClosers(1).kind !== 'LParen')
|
|
590
|
+
return this.missingOpeningParenthesis();
|
|
591
|
+
return this.tryParseDynamicValue() ?? this.parseFieldRef();
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Notes the string after the noise and unknown characters at the current
|
|
595
|
+
* token, which starts no value, as the value of `matches`: it is once they
|
|
596
|
+
* are gone, even where recovery reads them as the value instead. Unlike
|
|
597
|
+
* skipNoise(), it reads past an unknown character.
|
|
598
|
+
*/
|
|
599
|
+
noteMatchesValueAfterNoise() {
|
|
600
|
+
let index = this.pos;
|
|
601
|
+
while (isUnknownCharacter(this.tokens[index]) || this.isNoiseBeforeComparisonValue(index))
|
|
602
|
+
index++;
|
|
603
|
+
this.noteReading(this.tokenAt(index), true);
|
|
604
|
+
}
|
|
605
|
+
/** Reports a condition where a value belongs, and reads it as invalid. */
|
|
606
|
+
conditionInsteadOfValue(startPos) {
|
|
607
|
+
const invalid = this.readRest(startPos);
|
|
608
|
+
this.reportAtSpan('expected one value, found a condition', startPos, invalid.length ?? 0, { id: 'syntax.condition_instead_of_value' });
|
|
609
|
+
return invalid;
|
|
610
|
+
}
|
|
611
|
+
/** Skips the rest of the input, and returns an InvalidNode from `startPos` through the last token. */
|
|
612
|
+
readRest(startPos) {
|
|
613
|
+
const end = this.tokens.length - 1;
|
|
614
|
+
if (this.pos < end)
|
|
615
|
+
this.lastRead = end - 1;
|
|
616
|
+
this.pos = end;
|
|
617
|
+
return this.invalidNode(startPos, this.fieldLength(startPos));
|
|
618
|
+
}
|
|
427
619
|
peek(offset = 0) {
|
|
428
620
|
return this.tokenAt(this.pos + offset);
|
|
429
621
|
}
|
|
@@ -453,22 +645,20 @@ export class Parser {
|
|
|
453
645
|
* delimiter, or '' for any other. What was meant to follow such a token
|
|
454
646
|
* may be in it, but only where its text holds it: in
|
|
455
647
|
* `coalesce(http.host eq "x, ssl)` the string holds the `)`, and in
|
|
456
|
-
* `lower(http.host eq "ab` it holds nothing.
|
|
457
|
-
*
|
|
458
|
-
* reads the tokenizer's tokens, since an unknown character that the
|
|
459
|
-
* parser reads past still follows the string, on a later line.
|
|
648
|
+
* `lower(http.host eq "ab` it holds nothing. Every token without its
|
|
649
|
+
* closing delimiter runs to the end of the input.
|
|
460
650
|
*/
|
|
461
|
-
hiddenText(
|
|
462
|
-
const token = tokens.at(-2);
|
|
463
|
-
if (token?.closed !== false || tokens.at(-1)?.kind !== 'EOF')
|
|
651
|
+
hiddenText() {
|
|
652
|
+
const token = this.tokens.at(-2);
|
|
653
|
+
if (token?.closed !== false || this.tokens.at(-1)?.kind !== 'EOF')
|
|
464
654
|
return '';
|
|
465
655
|
return this.source.slice(token.pos, token.pos + tokenLength(token));
|
|
466
656
|
}
|
|
467
657
|
rawSpan(startPos) {
|
|
468
658
|
const lastToken = this.tokens[this.lastRead];
|
|
469
659
|
if (!lastToken)
|
|
470
|
-
return this.source.slice(startPos)
|
|
471
|
-
return this.source.slice(startPos, lastToken.pos + tokenLength(lastToken))
|
|
660
|
+
return trimRuleExpression(this.source.slice(startPos));
|
|
661
|
+
return trimRuleExpression(this.source.slice(startPos, lastToken.pos + tokenLength(lastToken)));
|
|
472
662
|
}
|
|
473
663
|
// or_expr = and_expr ("or" and_expr)*
|
|
474
664
|
parseOr(first) {
|
|
@@ -612,7 +802,7 @@ export class Parser {
|
|
|
612
802
|
return invalid;
|
|
613
803
|
}
|
|
614
804
|
else {
|
|
615
|
-
if (!isComparisonStart(this.tokens, this.pos) || !this.endsAtComparison())
|
|
805
|
+
if (!isComparisonStart(this.tokens, this.pos) || !this.endsAtComparison(this.pos))
|
|
616
806
|
this.reportUnexpectedToken(token);
|
|
617
807
|
if (this.handlerOf(this.pos) !== null && !isLogicalOperator(token)) {
|
|
618
808
|
invalid = this.emptyInvalid(token);
|
|
@@ -687,7 +877,7 @@ export class Parser {
|
|
|
687
877
|
* InvalidNode.operand.
|
|
688
878
|
*/
|
|
689
879
|
fieldCondition(field, recoveryCutsShort) {
|
|
690
|
-
const cutShort =
|
|
880
|
+
const cutShort = this.isCutShort(field, recoveryCutsShort);
|
|
691
881
|
if (field.accessPath.length === 0 && !cutShort) {
|
|
692
882
|
return { kind: 'BoolField', name: field.name, raw: this.source.slice(field.pos, field.pos + field.length), pos: field.pos, length: field.length };
|
|
693
883
|
}
|
|
@@ -699,6 +889,11 @@ export class Parser {
|
|
|
699
889
|
}
|
|
700
890
|
return this.invalidNode(field.pos, field.length, field);
|
|
701
891
|
}
|
|
892
|
+
/** Whether recovery cut a field or call short, so that it may be a comparison's left side; see InvalidNode.operand. */
|
|
893
|
+
isCutShort(node, recoveryCutsShort) {
|
|
894
|
+
const marked = this.cutShortAccessPaths.has(node.accessPath) || (node.kind === 'Func' && this.cutShortCalls.has(node));
|
|
895
|
+
return (recoveryCutsShort || marked) && isComparableValue(node);
|
|
896
|
+
}
|
|
702
897
|
parseFieldRef() {
|
|
703
898
|
const start = this.peek();
|
|
704
899
|
const name = this.consume().value; // Identifier
|
|
@@ -709,6 +904,7 @@ export class Parser {
|
|
|
709
904
|
const accessPath = [];
|
|
710
905
|
for (this.skipStrayClosers(); this.peek().kind === 'LBracket'; this.skipStrayClosers()) {
|
|
711
906
|
const start = this.peek();
|
|
907
|
+
this.reportSpaceBeforeAccess(start);
|
|
712
908
|
const access = this.open('access');
|
|
713
909
|
const value = this.parseAccessValue(start);
|
|
714
910
|
this.finishOperand();
|
|
@@ -722,6 +918,22 @@ export class Parser {
|
|
|
722
918
|
}
|
|
723
919
|
return accessPath;
|
|
724
920
|
}
|
|
921
|
+
/**
|
|
922
|
+
* Reports a space, or a comment, before the `[` of an access. Cloudflare
|
|
923
|
+
* reads an access only right after its field, call, or previous access, and
|
|
924
|
+
* ends the value at the space. The access is read as written all the same.
|
|
925
|
+
* After a stray closer, or an access without its `]`, the space is part of
|
|
926
|
+
* that error.
|
|
927
|
+
*/
|
|
928
|
+
reportSpaceBeforeAccess(start) {
|
|
929
|
+
const before = this.tokens[this.pos - 1];
|
|
930
|
+
if (before !== this.tokens[this.lastRead] || !(before.kind === 'Identifier' || isCloser(before)))
|
|
931
|
+
return;
|
|
932
|
+
const end = before.pos + tokenLength(before);
|
|
933
|
+
if (end === start.pos)
|
|
934
|
+
return;
|
|
935
|
+
this.reportAtSpan('space before \'[\' in an access; Cloudflare reads an access only right after its field, call, or previous access', end, start.pos - end, { id: 'syntax.space_before_access' });
|
|
936
|
+
}
|
|
725
937
|
/**
|
|
726
938
|
* The value of an access, or null when it is not a valid one, after
|
|
727
939
|
* reporting it. A number or string touching an unknown character is the
|
|
@@ -753,6 +965,19 @@ export class Parser {
|
|
|
753
965
|
}
|
|
754
966
|
if (value.kind === 'String') {
|
|
755
967
|
this.consume();
|
|
968
|
+
this.noteReading(value, false);
|
|
969
|
+
// A bad escape, or a missing closing quote, has its own error.
|
|
970
|
+
if (value.invalidEscape || value.closed === false)
|
|
971
|
+
return null;
|
|
972
|
+
if (value.format === 'raw') {
|
|
973
|
+
this.reportAtToken('map key must be a quoted string, not a raw string', value, { id: 'syntax.raw_string_map_key' });
|
|
974
|
+
return null;
|
|
975
|
+
}
|
|
976
|
+
// A key that is not UTF-8 holds a lone surrogate; see byte-strings.ts.
|
|
977
|
+
if (!value.value.isWellFormed()) {
|
|
978
|
+
this.reportAtToken('map key must be valid UTF-8', value, { id: 'syntax.non_utf8_map_key' });
|
|
979
|
+
return null;
|
|
980
|
+
}
|
|
756
981
|
return { kind: 'MapKeyAccess', key: value.value, pos: start.pos };
|
|
757
982
|
}
|
|
758
983
|
if (value.kind === 'Invalid') {
|
|
@@ -774,9 +999,15 @@ export class Parser {
|
|
|
774
999
|
parseComparisonValue(op, insertionPos) {
|
|
775
1000
|
this.skipNoise(this.isNoiseBeforeComparisonValue, startsValue);
|
|
776
1001
|
const token = this.peek();
|
|
1002
|
+
if (op === 'matches' && !startsValue(token))
|
|
1003
|
+
this.noteMatchesValueAfterNoise();
|
|
777
1004
|
// An unknown character starts recovery, since what follows it may belong to it.
|
|
778
1005
|
if (token.kind === 'Invalid') {
|
|
1006
|
+
const start = this.pos;
|
|
779
1007
|
const invalid = this.readInvalid();
|
|
1008
|
+
// A string an unknown character touches is part of the value, as in `@"\d"`.
|
|
1009
|
+
for (let index = start; index <= this.lastRead; index++)
|
|
1010
|
+
this.noteReading(this.tokens[index], op === 'matches');
|
|
780
1011
|
this.recover();
|
|
781
1012
|
return invalid;
|
|
782
1013
|
}
|
|
@@ -791,7 +1022,18 @@ export class Parser {
|
|
|
791
1022
|
this.reportAtSpan(`missing right-hand value for operator '${op}'`, insertionPos, 0, { id: 'syntax.missing_operator_value', params: { operator: op } });
|
|
792
1023
|
return undefined;
|
|
793
1024
|
}
|
|
794
|
-
return this.tryParseDynamicValue() ?? this.
|
|
1025
|
+
return this.tryParseDynamicValue() ?? this.parseLiteralComparisonValue(op);
|
|
1026
|
+
}
|
|
1027
|
+
/**
|
|
1028
|
+
* A comparison's literal value, with a number literal read as the type of
|
|
1029
|
+
* `comparisonLeft` first; see LiteralType. Its own method, so that the
|
|
1030
|
+
* frame each nested comparison adds stays small.
|
|
1031
|
+
*/
|
|
1032
|
+
parseLiteralComparisonValue(op) {
|
|
1033
|
+
// Only a number or a list reads its literals as a type, so the type is looked up for no other value.
|
|
1034
|
+
const kind = this.peek().kind;
|
|
1035
|
+
const literalType = kind === 'Number' || kind === 'LBrace' ? leftLiteralType(this.comparisonLeft) : null;
|
|
1036
|
+
return this.parseValue('operator', op === 'matches', literalType);
|
|
795
1037
|
}
|
|
796
1038
|
// dynamic_value = func_call | field_ref
|
|
797
1039
|
// Unknown identifiers stay literal so unquoted strings keep their diagnostic.
|
|
@@ -848,27 +1090,42 @@ export class Parser {
|
|
|
848
1090
|
this.consume(); // 'wildcard'
|
|
849
1091
|
return 'strict_wildcard';
|
|
850
1092
|
}
|
|
851
|
-
/**
|
|
852
|
-
|
|
1093
|
+
/**
|
|
1094
|
+
* Notes a quoted string read as a value the other way than the tokenizer
|
|
1095
|
+
* did: as the value of `matches`, or as any other value.
|
|
1096
|
+
*/
|
|
1097
|
+
noteReading(token, matchesValue) {
|
|
1098
|
+
if (token.kind !== 'String' || token.format === 'raw' || (token.format === 'regex') === matchesValue)
|
|
1099
|
+
return;
|
|
1100
|
+
(this.misreadStrings ??= new Map()).set(token.pos, matchesValue);
|
|
1101
|
+
}
|
|
1102
|
+
/**
|
|
1103
|
+
* A literal value, or an InvalidNode for one that is not valid, such as an
|
|
1104
|
+
* unquoted name, after reporting it. A number literal is read as
|
|
1105
|
+
* `literalType` first; see LiteralType.
|
|
1106
|
+
*/
|
|
1107
|
+
parseValue(context, matchesValue = false, literalType = null) {
|
|
853
1108
|
const t = this.peek();
|
|
854
1109
|
if (t.kind === 'String') {
|
|
855
1110
|
this.consume();
|
|
1111
|
+
this.noteReading(t, matchesValue);
|
|
1112
|
+
// A bad escape has its own error.
|
|
1113
|
+
if (t.invalidEscape)
|
|
1114
|
+
return this.invalidNode(t.pos, tokenLength(t));
|
|
856
1115
|
return { kind: 'StringLiteral', value: t.value, pos: t.pos, length: tokenLength(t) };
|
|
857
1116
|
}
|
|
858
1117
|
if (t.kind === 'Number') {
|
|
859
1118
|
this.consume();
|
|
860
1119
|
if (this.reportHexBytesLiteral(t))
|
|
861
1120
|
return this.invalidNode(t.pos, tokenLength(t));
|
|
862
|
-
|
|
863
|
-
return { kind: 'IpLiteral', value: t.value, pos: t.pos, length: tokenLength(t) };
|
|
864
|
-
return this.numberLiteral(t);
|
|
1121
|
+
return this.numberValue(t, context === 'function', literalType);
|
|
865
1122
|
}
|
|
866
1123
|
if (t.kind === 'Boolean') {
|
|
867
1124
|
this.consume();
|
|
868
1125
|
return { kind: 'BoolLiteral', value: t.value === 'true', pos: t.pos, length: tokenLength(t) };
|
|
869
1126
|
}
|
|
870
1127
|
if (t.kind === 'LBrace')
|
|
871
|
-
return this.parseSetLiteral();
|
|
1128
|
+
return this.parseSetLiteral(literalType);
|
|
872
1129
|
if (t.kind === 'Identifier' && t.value.startsWith('$')) {
|
|
873
1130
|
this.consume();
|
|
874
1131
|
return { kind: 'ListRef', name: t.value.slice(1), pos: t.pos, length: tokenLength(t) };
|
|
@@ -900,42 +1157,73 @@ export class Parser {
|
|
|
900
1157
|
this.pos = partner;
|
|
901
1158
|
this.consume();
|
|
902
1159
|
}
|
|
903
|
-
/**
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
1160
|
+
/**
|
|
1161
|
+
* A number token as one value: an integer, with its exact `integer` value,
|
|
1162
|
+
* or one IP address. Text that neither reads gets the error of the type
|
|
1163
|
+
* its place expects, or with none, of the type its text suggests. A literal
|
|
1164
|
+
* that only the other type reads stays one, for the type check to report,
|
|
1165
|
+
* as in `ip.src eq 1`. Cloudflare reads a function argument as an IP
|
|
1166
|
+
* address, then an integer, whatever its parameter, so the parameter's
|
|
1167
|
+
* type picks only the error. An argument can't be hex, which Cloudflare
|
|
1168
|
+
* reads as a name there.
|
|
1169
|
+
*/
|
|
1170
|
+
numberValue(token, argument, literalType) {
|
|
1171
|
+
const text = token.value;
|
|
1172
|
+
if (argument && /^0[xX]/.test(text)) {
|
|
1173
|
+
this.reportAtToken(`hex integer literal '${text}' is read as a name in a function argument; write it in decimal`, token, { id: 'syntax.hex_integer_argument', params: { literal: text } });
|
|
907
1174
|
return this.invalidNode(token.pos, tokenLength(token));
|
|
908
|
-
|
|
1175
|
+
}
|
|
1176
|
+
const integer = int64LiteralValue(text);
|
|
1177
|
+
if (integer !== null)
|
|
1178
|
+
return { kind: 'NumberLiteral', value: Number(integer), integer, raw: text, pos: token.pos, length: tokenLength(token) };
|
|
1179
|
+
if (readIpAddress(text) !== null)
|
|
1180
|
+
return { kind: 'IpLiteral', value: text, pos: token.pos, length: tokenLength(token) };
|
|
1181
|
+
const expected = argument ? this.argumentLiteralType() : literalType;
|
|
1182
|
+
if (expected === 'ip' || (expected === null && IP_ADDRESS_LIKE.test(text))) {
|
|
1183
|
+
const member = readIpListMember(text);
|
|
1184
|
+
// An argument can't be the list a range needs.
|
|
1185
|
+
if (!argument && 'range' in member && (text.includes('/') || text.includes('..'))) {
|
|
1186
|
+
this.reportAtToken(`IP range '${text}' is valid only in an inline list, as in 'in {${text}}'`, token, { id: 'syntax.ip_range_requires_list', params: { literal: text } });
|
|
1187
|
+
}
|
|
1188
|
+
else {
|
|
1189
|
+
this.reportInvalidIpAddress(token);
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
else {
|
|
1193
|
+
this.readInteger(token, text);
|
|
1194
|
+
}
|
|
1195
|
+
return this.invalidNode(token.pos, tokenLength(token));
|
|
1196
|
+
}
|
|
1197
|
+
/** The type the innermost call's parameter for the argument read declares, as a LiteralType. */
|
|
1198
|
+
argumentLiteralType() {
|
|
1199
|
+
const call = this.constructs.at(-1);
|
|
1200
|
+
return literalTypeOf(parameterDataType(call.functionName, call.argumentIndex));
|
|
909
1201
|
}
|
|
910
|
-
parseSetLiteral() {
|
|
1202
|
+
parseSetLiteral(literalType) {
|
|
911
1203
|
const start = this.peek();
|
|
912
1204
|
const list = this.open('list');
|
|
913
1205
|
const elements = [];
|
|
914
1206
|
// A list with its `}` owns every token up to it; one without ends where an enclosing construct resumes.
|
|
915
1207
|
while (this.handlerOf(this.pos) === null) {
|
|
916
1208
|
const t = this.peek();
|
|
917
|
-
const unquotedType = t.kind === 'Number' ? unquotedSetElementType(t.value) : null;
|
|
918
1209
|
if (t.kind === 'String') {
|
|
919
|
-
elements.push({ type: 'string', value: t.value });
|
|
920
|
-
this.consume();
|
|
921
|
-
}
|
|
922
|
-
else if (unquotedType === 'integer') {
|
|
923
|
-
const [first, last = first] = t.value.split('..').map(bound => this.readInteger(t, bound));
|
|
924
1210
|
this.consume();
|
|
925
|
-
|
|
926
|
-
|
|
1211
|
+
this.noteReading(t, false);
|
|
1212
|
+
// A bad escape has its own error.
|
|
1213
|
+
elements.push(t.invalidEscape
|
|
927
1214
|
? this.invalidMember(t)
|
|
928
|
-
: { type: '
|
|
1215
|
+
: { type: 'string', value: t.value, pos: t.pos, length: tokenLength(t) });
|
|
929
1216
|
}
|
|
930
|
-
else if (
|
|
931
|
-
elements.push({ type: 'ip', value: t.value });
|
|
1217
|
+
else if (t.kind === 'Number') {
|
|
932
1218
|
this.consume();
|
|
1219
|
+
for (const member of literalType === 'number' ? integerSideMembers(t) : [t])
|
|
1220
|
+
elements.push(this.setMember(member, literalType));
|
|
933
1221
|
}
|
|
934
1222
|
else {
|
|
935
1223
|
if (t.kind === 'Identifier')
|
|
936
1224
|
this.reportUnquotedName(t, 'set');
|
|
937
1225
|
// A malformed token has its own error.
|
|
938
|
-
else if (
|
|
1226
|
+
else if (t.kind !== 'Invalid') {
|
|
939
1227
|
this.reportAtToken(`unexpected token in set literal: ${t.kind} "${t.value}"`, t, { id: 'syntax.unexpected_token_in_set_literal', params: { tokenKind: t.kind, tokenValue: t.value } });
|
|
940
1228
|
}
|
|
941
1229
|
this.skipInvalidValue();
|
|
@@ -945,27 +1233,119 @@ export class Parser {
|
|
|
945
1233
|
this.close(list, () => this.reportAtCurrent('missing closing brace', { id: 'syntax.missing_closing_brace' }));
|
|
946
1234
|
return { kind: 'SetLiteral', elements, pos: start.pos, length: this.fieldLength(start.pos) };
|
|
947
1235
|
}
|
|
948
|
-
/**
|
|
949
|
-
|
|
950
|
-
|
|
1236
|
+
/**
|
|
1237
|
+
* An unquoted list member, read as `literalType` first, as Cloudflare
|
|
1238
|
+
* reads each member as the type of the list's left side: an integer or
|
|
1239
|
+
* integer range, or an IP address, CIDR, or IP range. A member that only
|
|
1240
|
+
* the other type reads stays one, for the type check to report. One that
|
|
1241
|
+
* neither reads gets the error of the type its place expects, or with
|
|
1242
|
+
* none, of the type its text suggests.
|
|
1243
|
+
*/
|
|
1244
|
+
setMember(token, literalType) {
|
|
1245
|
+
// A member spans its token, which may be one that integerSideMembers() split off, as `08` is from `08-10`.
|
|
1246
|
+
const length = tokenLength(token);
|
|
1247
|
+
if (this.reportHexBytesLiteral(token))
|
|
1248
|
+
return this.invalidMember(token, length);
|
|
1249
|
+
const text = token.value;
|
|
1250
|
+
const bounds = integerRangeBounds(text);
|
|
1251
|
+
const integer = bounds && { type: 'integer', value: text.includes('..') ? text : Number(bounds[0]), raw: text, bounds };
|
|
1252
|
+
// Digits alone are an IP address, such as `10` for 10.0.0.0, only where one is expected.
|
|
1253
|
+
const ipMember = literalType === 'ip' || IP_MEMBER_LIKE.test(text) ? readIpListMember(text) : null;
|
|
1254
|
+
const ip = ipMember && 'range' in ipMember ? { type: 'ip', value: text, range: ipMember.range } : null;
|
|
1255
|
+
const member = literalType === 'ip' ? ip ?? integer : integer ?? ip;
|
|
1256
|
+
if (member) {
|
|
1257
|
+
if (member === ip && literalType !== 'number' && ipMember && 'range' in ipMember && ipMember.shortFormPrefix !== null) {
|
|
1258
|
+
this.warnShortIpv4Address(token, ipMember.range.first, ipMember.shortFormPrefix);
|
|
1259
|
+
}
|
|
1260
|
+
return member;
|
|
1261
|
+
}
|
|
1262
|
+
if (literalType !== 'number' && ipMember && 'error' in ipMember)
|
|
1263
|
+
this.reportInvalidIpMember(token, ipMember);
|
|
1264
|
+
else
|
|
1265
|
+
this.reportInvalidIntegerMember(token);
|
|
1266
|
+
return this.invalidMember(token, length);
|
|
1267
|
+
}
|
|
1268
|
+
/** Reports an integer list member that is not valid: a bound that is not an integer, a missing bound, or reversed bounds. */
|
|
1269
|
+
reportInvalidIntegerMember(token) {
|
|
1270
|
+
const text = token.value;
|
|
1271
|
+
const bounds = text.split('..');
|
|
1272
|
+
if (bounds.length > 2 || bounds.includes('')) {
|
|
1273
|
+
this.reportAtToken(`number literal '${text}' must be an integer or an integer range`, token, { id: 'syntax.invalid_integer_literal', params: { literal: text } });
|
|
1274
|
+
return;
|
|
1275
|
+
}
|
|
1276
|
+
const [first, last = first] = bounds.map(bound => this.readInteger(token, bound));
|
|
1277
|
+
if (first === null || last === null)
|
|
1278
|
+
return;
|
|
1279
|
+
this.reportReversedRange(token, bounds[0], bounds[1]);
|
|
1280
|
+
}
|
|
1281
|
+
/** Reports why an IP list member is not valid. */
|
|
1282
|
+
reportInvalidIpMember(token, member) {
|
|
1283
|
+
const text = token.value;
|
|
1284
|
+
switch (member.error) {
|
|
1285
|
+
case 'address':
|
|
1286
|
+
this.reportInvalidIpAddress(token);
|
|
1287
|
+
return;
|
|
1288
|
+
case 'prefix':
|
|
1289
|
+
this.reportAtToken(`CIDR '${text}' needs a decimal prefix length from 0 to ${member.max}`, token, { id: 'syntax.invalid_cidr_prefix', params: { literal: text, prefix: member.prefix, max: member.max } });
|
|
1290
|
+
return;
|
|
1291
|
+
case 'host_bits':
|
|
1292
|
+
this.reportAtToken(`CIDR '${text}' has bits set past its prefix; write ${member.network}`, token, { id: 'syntax.cidr_host_bits', params: { literal: text, network: member.network } });
|
|
1293
|
+
return;
|
|
1294
|
+
case 'reversed':
|
|
1295
|
+
this.reportReversedRange(token, member.first, member.last);
|
|
1296
|
+
return;
|
|
1297
|
+
case 'mixed_versions':
|
|
1298
|
+
this.reportAtToken(`IP range '${text}' has an IPv4 and an IPv6 bound`, token, { id: 'syntax.mixed_ip_range', params: { literal: text } });
|
|
1299
|
+
return;
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
reportReversedRange(token, first, last) {
|
|
1303
|
+
this.reportAtToken(`range '${token.value}' starts after it ends; write the lower bound first`, token, { id: 'syntax.reversed_range', params: { literal: token.value, first, last } });
|
|
1304
|
+
}
|
|
1305
|
+
reportInvalidIpAddress(token) {
|
|
1306
|
+
this.reportAtToken(`'${token.value}' is not a valid IP address`, token, { id: 'syntax.invalid_ip_address', params: { literal: token.value } });
|
|
1307
|
+
}
|
|
1308
|
+
/**
|
|
1309
|
+
* Warns at an IPv4 list member with fewer than four parts and no prefix,
|
|
1310
|
+
* which is one address, such as 10.1.0.0 for `10.1`, while the network,
|
|
1311
|
+
* 10.1.0.0/16, is a likely meaning. It is no syntax error.
|
|
1312
|
+
*/
|
|
1313
|
+
warnShortIpv4Address(token, address, prefix) {
|
|
1314
|
+
const written = formatIpAddressValue({ version: 4, value: address });
|
|
1315
|
+
this.diagnostics?.push({
|
|
1316
|
+
severity: 'warning',
|
|
1317
|
+
message: `'${token.value}' is the one address ${written}; write ${written}/${prefix} for the network`,
|
|
1318
|
+
pos: token.pos,
|
|
1319
|
+
length: tokenLength(token),
|
|
1320
|
+
id: 'inline_list.short_ipv4_address',
|
|
1321
|
+
params: { literal: token.value, address: written, network: `${written}/${prefix}` },
|
|
1322
|
+
});
|
|
1323
|
+
}
|
|
1324
|
+
/** A list member that is not a valid element, from its token to the last token read, or of the length given. */
|
|
1325
|
+
invalidMember(token, length = this.fieldLength(token.pos)) {
|
|
1326
|
+
return { ...this.invalidNode(token.pos, length), type: 'invalid' };
|
|
951
1327
|
}
|
|
952
1328
|
// func_call = funcName "(" args ")"
|
|
953
1329
|
parseFuncCall(startPos) {
|
|
954
|
-
|
|
955
|
-
|
|
1330
|
+
if (this.peekPastStrayClosers(1).kind !== 'LParen')
|
|
1331
|
+
return this.missingOpeningParenthesis();
|
|
1332
|
+
const name = this.consume().value;
|
|
956
1333
|
this.skipStrayClosers();
|
|
957
|
-
if (this.peek().kind !== 'LParen') {
|
|
958
|
-
// Like a missing closer, a missing opener leaves the parser without its place.
|
|
959
|
-
this.reportAtCurrent(`missing opening parenthesis for function '${name}'`, { id: 'syntax.missing_function_opening_parenthesis', params: { functionName: name } });
|
|
960
|
-
this.recover();
|
|
961
|
-
return this.invalidNode(startPos, tokenLength(nameToken));
|
|
962
|
-
}
|
|
963
1334
|
if (name === 'any' || name === 'all') {
|
|
964
1335
|
return this.parseAnyAll(name, startPos);
|
|
965
1336
|
}
|
|
966
1337
|
const parsed = this.parseArguments(name);
|
|
967
1338
|
return this.parseScalarFunctionComparison(name, parsed, startPos);
|
|
968
1339
|
}
|
|
1340
|
+
/** Reads a function's name that no `(` follows, reports it, and recovers; the name stands as invalid. */
|
|
1341
|
+
missingOpeningParenthesis() {
|
|
1342
|
+
const nameToken = this.consume();
|
|
1343
|
+
this.skipStrayClosers();
|
|
1344
|
+
// Like a missing closer, a missing opener leaves the parser without its place.
|
|
1345
|
+
this.reportAtCurrent(`missing opening parenthesis for function '${nameToken.value}'`, { id: 'syntax.missing_function_opening_parenthesis', params: { functionName: nameToken.value } });
|
|
1346
|
+
this.recover();
|
|
1347
|
+
return this.invalidNode(nameToken.pos, tokenLength(nameToken));
|
|
1348
|
+
}
|
|
969
1349
|
/** A function call as a value; the caller has checked that `(` follows its name. */
|
|
970
1350
|
parseFunctionValue(startPos) {
|
|
971
1351
|
const name = this.consume().value;
|
|
@@ -978,7 +1358,7 @@ export class Parser {
|
|
|
978
1358
|
}
|
|
979
1359
|
funcNode(name, parsed, accessPath, startPos) {
|
|
980
1360
|
const raw = this.rawSpan(startPos);
|
|
981
|
-
|
|
1361
|
+
const node = {
|
|
982
1362
|
kind: 'Func',
|
|
983
1363
|
name,
|
|
984
1364
|
args: parsed.args,
|
|
@@ -991,6 +1371,9 @@ export class Parser {
|
|
|
991
1371
|
...(parsed.openInCall ? { openInCall: true } : {}),
|
|
992
1372
|
...(parsed.strayArgument ? { strayArgument: true } : {}),
|
|
993
1373
|
};
|
|
1374
|
+
if (parsed.cutShort)
|
|
1375
|
+
this.cutShortCalls.add(node);
|
|
1376
|
+
return node;
|
|
994
1377
|
}
|
|
995
1378
|
/**
|
|
996
1379
|
* Parses a call's argument list, from its `(` through its `)`, with the
|
|
@@ -1044,7 +1427,7 @@ export class Parser {
|
|
|
1044
1427
|
break;
|
|
1045
1428
|
// A call that ends at a comparison operator ends before it, and the comparison then compares its
|
|
1046
1429
|
// result; see parseArgumentComparison().
|
|
1047
|
-
if (isComparisonStart(this.tokens, this.pos) && this.endsAtComparison())
|
|
1430
|
+
if (isComparisonStart(this.tokens, this.pos) && this.endsAtComparison(this.pos))
|
|
1048
1431
|
break;
|
|
1049
1432
|
// What follows a complete argument starts the next one. It may be a stray token instead, which may
|
|
1050
1433
|
// belong to the argument before it, so that argument and the ones after it are uncertain, and
|
|
@@ -1080,12 +1463,18 @@ export class Parser {
|
|
|
1080
1463
|
const cutShort = this.finishOperand();
|
|
1081
1464
|
if (!isLogicalOperator(this.peek()))
|
|
1082
1465
|
return argument;
|
|
1083
|
-
// Cloudflare needs parentheses to combine conditions here; read the rest as if they were there.
|
|
1084
1466
|
const condition = this.asCondition(argument, cutShort);
|
|
1467
|
+
this.constructs.at(-1).readingCondition = true;
|
|
1468
|
+
return this.parseUnparenthesizedLogicalRest(condition, startPos);
|
|
1469
|
+
}
|
|
1470
|
+
/**
|
|
1471
|
+
* Reads the logical operators after an argument's first condition as if
|
|
1472
|
+
* parentheses were around them, which Cloudflare needs, and reports them.
|
|
1473
|
+
*/
|
|
1474
|
+
parseUnparenthesizedLogicalRest(first, startPos) {
|
|
1085
1475
|
const operator = this.peek().value;
|
|
1086
1476
|
this.reportAtCurrent(`conditions combined with '${operator}' in a function argument need parentheses`, { id: 'syntax.unparenthesized_logical_argument', params: { operator } });
|
|
1087
|
-
this.
|
|
1088
|
-
return this.parseOr({ node: condition, pos: startPos });
|
|
1477
|
+
return this.parseOr({ node: first, pos: startPos });
|
|
1089
1478
|
}
|
|
1090
1479
|
/**
|
|
1091
1480
|
* Reports and skips each token that stands for nothing before what
|
|
@@ -1107,13 +1496,16 @@ export class Parser {
|
|
|
1107
1496
|
}
|
|
1108
1497
|
/**
|
|
1109
1498
|
* Skips the tokens that stand for nothing before an argument; see
|
|
1110
|
-
* skipNoise().
|
|
1111
|
-
*
|
|
1112
|
-
* the
|
|
1499
|
+
* skipNoise(). Before a quantifier's predicate, `starts` is
|
|
1500
|
+
* startsCondition(), since a value there is no predicate: the tokens
|
|
1501
|
+
* before one stay, and the first of them gets the error. A comparison
|
|
1502
|
+
* operator among them may have lost its left side, which would make the
|
|
1503
|
+
* argument a comparison, so that argument and the ones after it are
|
|
1504
|
+
* uncertain.
|
|
1113
1505
|
*/
|
|
1114
|
-
skipNoiseBeforeArgument() {
|
|
1506
|
+
skipNoiseBeforeArgument(starts = startsArgument) {
|
|
1115
1507
|
const start = this.pos;
|
|
1116
|
-
this.skipNoise(this.isNoiseBeforeArgument,
|
|
1508
|
+
this.skipNoise(this.isNoiseBeforeArgument, starts);
|
|
1117
1509
|
for (let index = start; index < this.pos; index++) {
|
|
1118
1510
|
if (!isComparisonStart(this.tokens, index))
|
|
1119
1511
|
continue;
|
|
@@ -1204,7 +1596,7 @@ export class Parser {
|
|
|
1204
1596
|
case 'BoolLiteral':
|
|
1205
1597
|
case 'SetLiteral':
|
|
1206
1598
|
case 'ListRef':
|
|
1207
|
-
return this.invalidNode(argument.pos, argument.length
|
|
1599
|
+
return this.invalidNode(argument.pos, argument.length);
|
|
1208
1600
|
default:
|
|
1209
1601
|
return unhandledNode(argument);
|
|
1210
1602
|
}
|
|
@@ -1230,7 +1622,7 @@ export class Parser {
|
|
|
1230
1622
|
nearest: {
|
|
1231
1623
|
closed: closer !== null ? depth : outside.closed,
|
|
1232
1624
|
call: kind === 'call' ? depth : outside.call,
|
|
1233
|
-
logical: kind === 'group' || kind === 'call' ? depth : outside.logical,
|
|
1625
|
+
logical: kind === 'group' || kind === 'call' || (kind === 'quantifier' && closer !== null) ? depth : outside.logical,
|
|
1234
1626
|
access: closer === null && kind === 'access' ? depth : outside.access,
|
|
1235
1627
|
},
|
|
1236
1628
|
};
|
|
@@ -1295,7 +1687,7 @@ export class Parser {
|
|
|
1295
1687
|
handler = Math.max(handler, nearest.logical);
|
|
1296
1688
|
if (argumentStarts && startsArgument(token))
|
|
1297
1689
|
handler = Math.max(handler, nearest.call);
|
|
1298
|
-
if (isComparisonStart(this.tokens, index) && this.endsAtComparison())
|
|
1690
|
+
if (isComparisonStart(this.tokens, index) && this.endsAtComparison(index))
|
|
1299
1691
|
handler = Math.max(handler, this.constructs.length - 1);
|
|
1300
1692
|
if (token.kind === 'LBracket')
|
|
1301
1693
|
handler = Math.max(handler, nearest.access);
|
|
@@ -1306,19 +1698,19 @@ export class Parser {
|
|
|
1306
1698
|
return isLogicalOperator(token) ? -1 : null;
|
|
1307
1699
|
}
|
|
1308
1700
|
/**
|
|
1309
|
-
* Whether the innermost construct ends at
|
|
1310
|
-
* without its `]`, or a call without its `)` that reads a
|
|
1311
|
-
* the comparison cannot be; see takesComparison(). A
|
|
1312
|
-
* does not: the comparison belongs to the innermost
|
|
1313
|
-
* one holds, such as a condition.
|
|
1701
|
+
* Whether the innermost construct ends at the comparison operator at an
|
|
1702
|
+
* index: an access without its `]`, or a call without its `)` that reads a
|
|
1703
|
+
* value argument the comparison cannot be; see takesComparison(). A
|
|
1704
|
+
* construct outside it does not: the comparison belongs to the innermost
|
|
1705
|
+
* one, or to what that one holds, such as a condition.
|
|
1314
1706
|
*/
|
|
1315
|
-
endsAtComparison() {
|
|
1707
|
+
endsAtComparison(operator) {
|
|
1316
1708
|
const construct = this.constructs.at(-1);
|
|
1317
1709
|
if (construct === undefined || construct.closer !== null)
|
|
1318
1710
|
return false;
|
|
1319
1711
|
if (construct.kind === 'access')
|
|
1320
1712
|
return true;
|
|
1321
|
-
return construct.kind === 'call' && !construct.readingCondition && !takesComparison(construct);
|
|
1713
|
+
return construct.kind === 'call' && !construct.readingCondition && !takesComparison(construct, this.tokens[operator]);
|
|
1322
1714
|
}
|
|
1323
1715
|
/** Whether a construct outside the one at `depth` resumes at the current token. */
|
|
1324
1716
|
resumesOutside(depth) {
|
|
@@ -1451,7 +1843,7 @@ export class Parser {
|
|
|
1451
1843
|
* parseFunctionArg() for a logical operator after it.
|
|
1452
1844
|
*/
|
|
1453
1845
|
parseArgumentComparison(left, startPos) {
|
|
1454
|
-
if (this.endsAtComparison())
|
|
1846
|
+
if (this.endsAtComparison(this.pos))
|
|
1455
1847
|
return null;
|
|
1456
1848
|
return this.parseComparisonRest(left, startPos);
|
|
1457
1849
|
}
|
|
@@ -1461,6 +1853,7 @@ export class Parser {
|
|
|
1461
1853
|
const op = this.tryParseCompOp();
|
|
1462
1854
|
if (op === null)
|
|
1463
1855
|
return null;
|
|
1856
|
+
this.comparisonLeft = left;
|
|
1464
1857
|
const right = this.parseComparisonValue(op, this.previousTokenEnd());
|
|
1465
1858
|
const raw = this.rawSpan(startPos);
|
|
1466
1859
|
return { kind: 'Comparison', left, op, right, raw, pos: startPos, length: raw.length };
|
|
@@ -1476,15 +1869,15 @@ export class Parser {
|
|
|
1476
1869
|
parseScalarFunctionComparison(name, parsed, startPos) {
|
|
1477
1870
|
const funcNode = this.funcNode(name, parsed, this.parseAccessPath(), startPos);
|
|
1478
1871
|
const comparison = name === 'starts_with' || name === 'ends_with' ? null : this.parseComparisonRest(funcNode, startPos);
|
|
1479
|
-
return comparison ?? this.functionCondition(funcNode,
|
|
1872
|
+
return comparison ?? this.functionCondition(funcNode, this.cutsShort());
|
|
1480
1873
|
}
|
|
1481
1874
|
/** A call without a comparison, as a condition; see fieldCondition(). */
|
|
1482
|
-
functionCondition(funcNode,
|
|
1483
|
-
if (!
|
|
1875
|
+
functionCondition(funcNode, recoveryCutsShort) {
|
|
1876
|
+
if (!this.isCutShort(funcNode, recoveryCutsShort))
|
|
1484
1877
|
return funcNode;
|
|
1485
|
-
return this.invalidNode(funcNode.pos, funcNode.length
|
|
1878
|
+
return this.invalidNode(funcNode.pos, funcNode.length, funcNode);
|
|
1486
1879
|
}
|
|
1487
|
-
// any(
|
|
1880
|
+
// any(predicate) or all(predicate); see parseQuantifierPredicate()
|
|
1488
1881
|
parseAnyAll(name, startPos) {
|
|
1489
1882
|
const quantifier = this.open('quantifier');
|
|
1490
1883
|
this.skipStrayClosers();
|
|
@@ -1502,12 +1895,49 @@ export class Parser {
|
|
|
1502
1895
|
this.reportAtSpan(`missing argument for function '${name}'`, startPos, Math.max(raw.length, name.length + 2), { id: 'syntax.missing_function_argument', params: { functionName: name } });
|
|
1503
1896
|
return this.funcNode(name, quantifierArguments([], undefined, false), [], startPos);
|
|
1504
1897
|
}
|
|
1505
|
-
const predicate = this.
|
|
1506
|
-
this.finishOperand();
|
|
1898
|
+
const predicate = this.parseQuantifierPredicate();
|
|
1507
1899
|
const closing = this.close(quantifier, () => this.reportMissingCallCloser(name));
|
|
1508
1900
|
const parsed = quantifierArguments([predicate], quantifier.uncertainFrom ?? (closing === 'read' ? undefined : 1), quantifier.recoveredIn !== null || closing !== 'read');
|
|
1509
1901
|
return this.funcNode(name, parsed, this.parseAccessPath(), startPos);
|
|
1510
1902
|
}
|
|
1903
|
+
/**
|
|
1904
|
+
* Parses the argument of any() or all() as Cloudflare does, as a function
|
|
1905
|
+
* argument is read: one that starts with `(`, `not`, any() or all() is a
|
|
1906
|
+
* whole logical expression, and any other is one condition, which needs
|
|
1907
|
+
* parentheses to combine with others. Without its `)`, the quantifier
|
|
1908
|
+
* leaves a logical operator after such a condition to the condition around
|
|
1909
|
+
* it; see NearestConstructs.
|
|
1910
|
+
*/
|
|
1911
|
+
parseQuantifierPredicate() {
|
|
1912
|
+
const closed = this.constructs.at(-1).closer !== null;
|
|
1913
|
+
this.skipNoiseBeforeArgument(startsCondition);
|
|
1914
|
+
const combines = this.startsLogicalArgument();
|
|
1915
|
+
if (combines && closed) {
|
|
1916
|
+
const condition = this.parseOr();
|
|
1917
|
+
if (condition.kind === 'Func')
|
|
1918
|
+
condition.condition = true;
|
|
1919
|
+
return condition;
|
|
1920
|
+
}
|
|
1921
|
+
const startPos = this.conditionStart();
|
|
1922
|
+
const condition = isLogicalOperator(this.peek()) ? this.invalidLogicalPredicate() : this.parseNot();
|
|
1923
|
+
this.finishOperand();
|
|
1924
|
+
if (!isLogicalOperator(this.peek()) || (!closed && !combines)) {
|
|
1925
|
+
// As in a function argument, a lone call that starts a logical argument is a condition.
|
|
1926
|
+
if (combines && condition.kind === 'Func')
|
|
1927
|
+
condition.condition = true;
|
|
1928
|
+
return condition;
|
|
1929
|
+
}
|
|
1930
|
+
// The `)` may be missing before the operator instead, which would end the predicate there.
|
|
1931
|
+
this.leaveArgumentUncertain();
|
|
1932
|
+
return combines ? this.parseOr({ node: condition, pos: startPos }) : this.parseUnparenthesizedLogicalRest(condition, startPos);
|
|
1933
|
+
}
|
|
1934
|
+
/** A logical operator that starts a predicate combines nothing, so it is the predicate, invalid; see invalidArgument(). */
|
|
1935
|
+
invalidLogicalPredicate() {
|
|
1936
|
+
this.reportUnexpectedToken(this.peek());
|
|
1937
|
+
const invalid = this.readInvalid();
|
|
1938
|
+
this.consume();
|
|
1939
|
+
return invalid;
|
|
1940
|
+
}
|
|
1511
1941
|
/**
|
|
1512
1942
|
* Reads an integer literal, reporting one Cloudflare rejects: one with a
|
|
1513
1943
|
* fraction, an octal one with the digit 8 or 9, or one outside the 64-bit
|
|
@@ -1536,7 +1966,7 @@ export class Parser {
|
|
|
1536
1966
|
* package does not decode. Text that is also an IPv6 address stays an address.
|
|
1537
1967
|
*/
|
|
1538
1968
|
reportHexBytesLiteral(token) {
|
|
1539
|
-
if (!/^[\da-fA-F]{2}(?:[:-][\da-fA-F]{2})+$/.test(token.value) ||
|
|
1969
|
+
if (!/^[\da-fA-F]{2}(?:[:-][\da-fA-F]{2})+$/.test(token.value) || 'range' in readIpListMember(token.value))
|
|
1540
1970
|
return false;
|
|
1541
1971
|
this.reportAtToken(`hex byte literal '${token.value}' is not supported; write the value as a quoted string`, token, { id: 'syntax.unsupported_hex_bytes_literal', params: { literal: token.value } });
|
|
1542
1972
|
return true;
|
|
@@ -1569,7 +1999,45 @@ export class Parser {
|
|
|
1569
1999
|
});
|
|
1570
2000
|
}
|
|
1571
2001
|
report(detail) {
|
|
2002
|
+
this.syntaxError = true;
|
|
1572
2003
|
this.diagnostics?.push(detail);
|
|
1573
2004
|
}
|
|
1574
2005
|
}
|
|
2006
|
+
/** How many times parseSource() reads the input at most. */
|
|
2007
|
+
const MAX_PASSES = 3;
|
|
2008
|
+
/**
|
|
2009
|
+
* Tokenizes and parses the input. The tokenizer reads a quoted string right
|
|
2010
|
+
* after `matches` as a regex literal, which is always the value of `matches`
|
|
2011
|
+
* in valid input. Recovery may read a string in input with errors the other
|
|
2012
|
+
* way, as in `http.host matches , "\d"`, so each string the parser read as a
|
|
2013
|
+
* value the other way is read again its way, and the input parsed again; see
|
|
2014
|
+
* docs/string-literal-alignment.md. Each pass writes into the caller's
|
|
2015
|
+
* diagnostics and comments, and a pass that is redone takes its own back.
|
|
2016
|
+
* The tokenizer's diagnostics come first; `tokenizerDiagnosticCount` says
|
|
2017
|
+
* how many of the last pass's there are. `afterMatches` says that the input
|
|
2018
|
+
* follows `matches`; see InternalTokenizeOptions.
|
|
2019
|
+
*/
|
|
2020
|
+
export function parseSource(source, diagnostics, options, metadata, parse, afterMatches = false) {
|
|
2021
|
+
const diagnosticCount = diagnostics?.length ?? 0;
|
|
2022
|
+
const commentCount = metadata.comments?.length ?? 0;
|
|
2023
|
+
// Only the public option is copied, so that a caller's options object can't set the internal ones.
|
|
2024
|
+
const tokenizeOptions = { comments: options.comments, afterMatches };
|
|
2025
|
+
let regexStrings;
|
|
2026
|
+
for (let pass = 1;; pass++) {
|
|
2027
|
+
const tokens = tokenize(source, diagnostics, regexStrings ? { ...tokenizeOptions, regexStrings } : tokenizeOptions, metadata);
|
|
2028
|
+
const tokenizerDiagnosticCount = (diagnostics?.length ?? 0) - diagnosticCount;
|
|
2029
|
+
const parser = new Parser(tokens, source, diagnostics);
|
|
2030
|
+
const result = parse(parser);
|
|
2031
|
+
const misread = parser.misreadStrings;
|
|
2032
|
+
// A string read back the way an earlier pass read it would only repeat that pass.
|
|
2033
|
+
if (misread === undefined || pass === MAX_PASSES || [...misread.keys()].some(pos => regexStrings?.has(pos))) {
|
|
2034
|
+
return { tokens, result, tokenizerDiagnosticCount };
|
|
2035
|
+
}
|
|
2036
|
+
if (diagnostics)
|
|
2037
|
+
diagnostics.length = diagnosticCount;
|
|
2038
|
+
if (metadata.comments)
|
|
2039
|
+
metadata.comments.length = commentCount;
|
|
2040
|
+
regexStrings = new Map([...regexStrings ?? [], ...misread]);
|
|
2041
|
+
}
|
|
2042
|
+
}
|
|
1575
2043
|
//# sourceMappingURL=parser.js.map
|