( text: string, fail: (message: string) => never )
| 933 | |
| 934 | // Splits the node properties, an anchor and a tag in either order, from the start of `text`. |
| 935 | const nodeProperties = ( |
| 936 | text: string, |
| 937 | fail: (message: string) => never |
| 938 | ): { readonly anchor: string | undefined; readonly tag: string | undefined; readonly rest: string } => { |
| 939 | // Neither an anchor nor a tag, the common case: the checks below would all fail. |
| 940 | const first = text.charCodeAt(0) |
| 941 | if (first !== 38 && first !== 33) return { anchor: undefined, tag: undefined, rest: text } |
| 942 | let anchor: string | undefined |
| 943 | let tag: string | undefined |
| 944 | let rest = text |
| 945 | while (true) { |
| 946 | // `s`: U+2028 and U+2029 are content, not line breaks. |
| 947 | const property = anchor === undefined ? /^&([^ \t,[\]{}]+)(?:[ \t]+|$)/s.exec(rest) : null |
| 948 | if (property !== null) { |
| 949 | anchor = property[1] |
| 950 | rest = rest.slice(property[0].length) |
| 951 | } else if (tag === undefined && rest.startsWith("!")) { |
| 952 | tag = readTag(rest, fail) |
| 953 | rest = rest.slice(tag.length) |
| 954 | if (rest.length > 0 && !isWhiteCode(rest.charCodeAt(0))) fail(`Expected white space after the YAML tag ${tag}`) |
| 955 | rest = trimWhiteStart(rest) |
| 956 | } else { |
| 957 | // A node has at most one anchor and one tag. |
| 958 | if (tag !== undefined && rest.startsWith("!")) fail("Multiple tags on one node") |
| 959 | if (anchor !== undefined && /^&[^ \t,[\]{}]/.test(rest)) fail("Multiple anchors on one node") |
| 960 | // YAML 1.2.2 §§5.3 and 6.9.2: `&` cannot start a plain scalar, so it must start a named anchor. |
| 961 | if (rest.startsWith("&")) fail("Invalid anchor") |
| 962 | return { anchor, tag, rest } |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | const throwSyntaxError = (message: string): never => { |
| 968 | throw new SyntaxError(message) |
no test coverage detected