diff --git a/yaml/_loader_state.ts b/yaml/_loader_state.ts index b358198cedf0..9c55124abadf 100644 --- a/yaml/_loader_state.ts +++ b/yaml/_loader_state.ts @@ -919,6 +919,13 @@ export class LoaderState { "Cannot read flow collection: missing comma between flow collection entries", ); } + if (ch === COMMA) { + // "flow collection entries can never be completely empty", + // as per YAML 1.2, section 7.4 + throw this.#createError( + "Cannot read flow collection: expected the node content, but found ','", + ); + } keyTag = keyNode = valueNode = null; isPair = isExplicitPair = false; @@ -1515,8 +1522,11 @@ export class LoaderState { resolveTag(state: State) { switch (state.tag) { case null: - case "!": + case "!": { + const { anchor } = state; + if (anchor !== null) this.anchorMap.set(anchor, state.result); return state; + } case "?": { for (const type of this.implicitTypes) { // Implicit resolving is not allowed for non-scalar types, and '?' diff --git a/yaml/_type/int.ts b/yaml/_type/int.ts index 800bb2cf3d11..8436b8a11e3f 100644 --- a/yaml/_type/int.ts +++ b/yaml/_type/int.ts @@ -27,6 +27,8 @@ function isDecCode(c: number): boolean { } function resolveYamlInteger(data: string): boolean { + if (data === null) return false; + const max = data.length; let index = 0; let hasDigits = false; @@ -73,6 +75,19 @@ function resolveYamlInteger(data: string): boolean { return hasDigits && ch !== "_"; } + if (ch === "o") { + // base 8 + index++; + + for (; index < max; index++) { + ch = data[index]; + if (ch === "_") continue; + if (!isOctCode(data.charCodeAt(index))) return false; + hasDigits = true; + } + return hasDigits && ch !== "_"; + } + // base 8 for (; index < max; index++) { ch = data[index]; @@ -83,7 +98,7 @@ function resolveYamlInteger(data: string): boolean { return hasDigits && ch !== "_"; } - // base 10 (except 0) or base 60 + // base 10 // value should not start with `_`; if (ch === "_") return false; @@ -100,8 +115,7 @@ function resolveYamlInteger(data: string): boolean { // Should have digits and should not end with `_` if (!hasDigits || ch === "_") return false; - // base60 almost not used, no needs to optimize - return /^(:[0-5]?[0-9])+$/.test(data.slice(index)); + return true; } function constructYamlInteger(data: string): number { @@ -124,6 +138,7 @@ function constructYamlInteger(data: string): number { if (ch === "0") { if (value[1] === "b") return sign * parseInt(value.slice(2), 2); if (value[1] === "x") return sign * parseInt(value, 16); + if (value[1] === "o") return sign * parseInt(value.slice(2), 8); return sign * parseInt(value, 8); } diff --git a/yaml/_type/nil.ts b/yaml/_type/nil.ts index a43fab534967..d1f5e5704d1c 100644 --- a/yaml/_type/nil.ts +++ b/yaml/_type/nil.ts @@ -12,6 +12,7 @@ export const nil: Type<"scalar", null> = { predicate: (object: unknown): object is null => object === null, construct: () => null, resolve: (data: string): boolean => { + if (data === null) return true; return ( data === "~" || data === "null" || diff --git a/yaml/_type/omap.ts b/yaml/_type/omap.ts index effeebc39c5f..0c473c8a454e 100644 --- a/yaml/_type/omap.ts +++ b/yaml/_type/omap.ts @@ -7,6 +7,8 @@ import type { Type } from "../_type.ts"; import { isPlainObject } from "../_utils.ts"; function resolveYamlOmap(data: Record[]): boolean { + if (data === null) return true; + const objectKeys = new Set(); for (const object of data) { if (!isPlainObject(object)) return false; @@ -25,6 +27,6 @@ export const omap: Type<"sequence", Record[]> = { kind: "sequence", resolve: resolveYamlOmap, construct(data) { - return data; + return data !== null ? data : []; }, }; diff --git a/yaml/parse_test.ts b/yaml/parse_test.ts index c9ecd3da6a4f..63166fecf4c6 100644 --- a/yaml/parse_test.ts +++ b/yaml/parse_test.ts @@ -380,8 +380,24 @@ Deno.test({ - 012_34 - 0x1A - 0x1A_2B + - 0o17 + - 0o777 `), - [10, 170, 10, 668, 26, 6699], + [10, 170, 10, 668, 26, 6699, 15, 511], + ); + + // explicit !!int tag on resolvable scalars + assertEquals( + parse(` + - !!int 42 + - !!int -42 + - !!int "42" + - !!int 0b1010 + - !!int 012 + - !!int 0x1A + - !!int 0o17 + `), + [42, -42, 42, 10, 10, 26, 15], ); // empty string is invalid integer @@ -422,6 +438,13 @@ Deno.test({ SyntaxError, "Cannot resolve a node with ! explicit tag at line 2, column 1:\n \n ^", ); + + // empty value with !!int tag fails with a syntax error, not a TypeError + assertThrows( + () => parse("x: !!int\n"), + SyntaxError, + "Cannot resolve a node with ! explicit tag", + ); }, }); @@ -499,6 +522,22 @@ Deno.test({ SyntaxError, "Cannot resolve a node with ! explicit tag", ); + + // empty omap resolves to an empty sequence + assertEquals(parse("x: !!omap\n"), { x: [] }); + }, +}); + +Deno.test({ + name: "parse() handles null type", + fn() { + assertEquals(parse("x: null\ny: ~\nz:\n"), { + x: null, + y: null, + z: null, + }); + // explicit !!null tag on an empty value resolves to null + assertEquals(parse("x: !!null\n"), { x: null }); }, }); @@ -597,6 +636,32 @@ Deno.test("parse() handles anchors and aliases", () => { new Uint8Array([72, 101, 108, 108, 111]), ], ); + // anchor on the key line binds the indented scalar value + assertEquals( + parse(`a: &anchor + value +ref: *anchor`), + { a: "value", ref: "value" }, + ); + assertEquals( + parse(`a: &anchor + "quoted" +ref: *anchor`), + { a: "quoted", ref: "quoted" }, + ); + assertEquals( + parse(`a: &anchor + 42 +ref: *anchor`), + { a: 42, ref: 42 }, + ); + // anchor with empty value binds null + assertEquals( + parse(`a: &anchor + +ref: *anchor`), + { a: null, ref: null }, + ); assertThrows( () => parse(`- &anchor Foo @@ -613,6 +678,19 @@ Deno.test("parse() handles anchors and aliases", () => { ); }); +Deno.test("parse() rejects empty flow collection entries", () => { + // YAML 1.2, section 7.4: flow collection entries can never be empty + for (const doc of ["[, 4]", "[1, , 3]", "[, ]", "{, a: 1}"]) { + assertThrows( + () => parse(doc), + SyntaxError, + "expected the node content, but found ','", + ); + } + // a trailing comma is allowed + assertEquals(parse("[a,]"), ["a"]); +}); + Deno.test("parse() handles escaped strings in double quotes", () => { assertEquals(parse('"\\"bar\\""'), '"bar"'); assertEquals(parse('"\\x30\\x31"'), "01");