[d24f17c] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | function resolveProps(tokens, { flow, indicator, next, offset, onError, startOnNewline }) {
|
---|
| 4 | let spaceBefore = false;
|
---|
| 5 | let atNewline = startOnNewline;
|
---|
| 6 | let hasSpace = startOnNewline;
|
---|
| 7 | let comment = '';
|
---|
| 8 | let commentSep = '';
|
---|
| 9 | let hasNewline = false;
|
---|
| 10 | let hasNewlineAfterProp = false;
|
---|
| 11 | let reqSpace = false;
|
---|
| 12 | let anchor = null;
|
---|
| 13 | let tag = null;
|
---|
| 14 | let comma = null;
|
---|
| 15 | let found = null;
|
---|
| 16 | let start = null;
|
---|
| 17 | for (const token of tokens) {
|
---|
| 18 | if (reqSpace) {
|
---|
| 19 | if (token.type !== 'space' &&
|
---|
| 20 | token.type !== 'newline' &&
|
---|
| 21 | token.type !== 'comma')
|
---|
| 22 | onError(token.offset, 'MISSING_CHAR', 'Tags and anchors must be separated from the next token by white space');
|
---|
| 23 | reqSpace = false;
|
---|
| 24 | }
|
---|
| 25 | switch (token.type) {
|
---|
| 26 | case 'space':
|
---|
| 27 | // At the doc level, tabs at line start may be parsed
|
---|
| 28 | // as leading white space rather than indentation.
|
---|
| 29 | // In a flow collection, only the parser handles indent.
|
---|
| 30 | if (!flow &&
|
---|
| 31 | atNewline &&
|
---|
| 32 | indicator !== 'doc-start' &&
|
---|
| 33 | token.source[0] === '\t')
|
---|
| 34 | onError(token, 'TAB_AS_INDENT', 'Tabs are not allowed as indentation');
|
---|
| 35 | hasSpace = true;
|
---|
| 36 | break;
|
---|
| 37 | case 'comment': {
|
---|
| 38 | if (!hasSpace)
|
---|
| 39 | onError(token, 'MISSING_CHAR', 'Comments must be separated from other tokens by white space characters');
|
---|
| 40 | const cb = token.source.substring(1) || ' ';
|
---|
| 41 | if (!comment)
|
---|
| 42 | comment = cb;
|
---|
| 43 | else
|
---|
| 44 | comment += commentSep + cb;
|
---|
| 45 | commentSep = '';
|
---|
| 46 | atNewline = false;
|
---|
| 47 | break;
|
---|
| 48 | }
|
---|
| 49 | case 'newline':
|
---|
| 50 | if (atNewline) {
|
---|
| 51 | if (comment)
|
---|
| 52 | comment += token.source;
|
---|
| 53 | else
|
---|
| 54 | spaceBefore = true;
|
---|
| 55 | }
|
---|
| 56 | else
|
---|
| 57 | commentSep += token.source;
|
---|
| 58 | atNewline = true;
|
---|
| 59 | hasNewline = true;
|
---|
| 60 | if (anchor || tag)
|
---|
| 61 | hasNewlineAfterProp = true;
|
---|
| 62 | hasSpace = true;
|
---|
| 63 | break;
|
---|
| 64 | case 'anchor':
|
---|
| 65 | if (anchor)
|
---|
| 66 | onError(token, 'MULTIPLE_ANCHORS', 'A node can have at most one anchor');
|
---|
| 67 | if (token.source.endsWith(':'))
|
---|
| 68 | onError(token.offset + token.source.length - 1, 'BAD_ALIAS', 'Anchor ending in : is ambiguous', true);
|
---|
| 69 | anchor = token;
|
---|
| 70 | if (start === null)
|
---|
| 71 | start = token.offset;
|
---|
| 72 | atNewline = false;
|
---|
| 73 | hasSpace = false;
|
---|
| 74 | reqSpace = true;
|
---|
| 75 | break;
|
---|
| 76 | case 'tag': {
|
---|
| 77 | if (tag)
|
---|
| 78 | onError(token, 'MULTIPLE_TAGS', 'A node can have at most one tag');
|
---|
| 79 | tag = token;
|
---|
| 80 | if (start === null)
|
---|
| 81 | start = token.offset;
|
---|
| 82 | atNewline = false;
|
---|
| 83 | hasSpace = false;
|
---|
| 84 | reqSpace = true;
|
---|
| 85 | break;
|
---|
| 86 | }
|
---|
| 87 | case indicator:
|
---|
| 88 | // Could here handle preceding comments differently
|
---|
| 89 | if (anchor || tag)
|
---|
| 90 | onError(token, 'BAD_PROP_ORDER', `Anchors and tags must be after the ${token.source} indicator`);
|
---|
| 91 | if (found)
|
---|
| 92 | onError(token, 'UNEXPECTED_TOKEN', `Unexpected ${token.source} in ${flow ?? 'collection'}`);
|
---|
| 93 | found = token;
|
---|
| 94 | atNewline = false;
|
---|
| 95 | hasSpace = false;
|
---|
| 96 | break;
|
---|
| 97 | case 'comma':
|
---|
| 98 | if (flow) {
|
---|
| 99 | if (comma)
|
---|
| 100 | onError(token, 'UNEXPECTED_TOKEN', `Unexpected , in ${flow}`);
|
---|
| 101 | comma = token;
|
---|
| 102 | atNewline = false;
|
---|
| 103 | hasSpace = false;
|
---|
| 104 | break;
|
---|
| 105 | }
|
---|
| 106 | // else fallthrough
|
---|
| 107 | default:
|
---|
| 108 | onError(token, 'UNEXPECTED_TOKEN', `Unexpected ${token.type} token`);
|
---|
| 109 | atNewline = false;
|
---|
| 110 | hasSpace = false;
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | const last = tokens[tokens.length - 1];
|
---|
| 114 | const end = last ? last.offset + last.source.length : offset;
|
---|
| 115 | if (reqSpace &&
|
---|
| 116 | next &&
|
---|
| 117 | next.type !== 'space' &&
|
---|
| 118 | next.type !== 'newline' &&
|
---|
| 119 | next.type !== 'comma' &&
|
---|
| 120 | (next.type !== 'scalar' || next.source !== ''))
|
---|
| 121 | onError(next.offset, 'MISSING_CHAR', 'Tags and anchors must be separated from the next token by white space');
|
---|
| 122 | return {
|
---|
| 123 | comma,
|
---|
| 124 | found,
|
---|
| 125 | spaceBefore,
|
---|
| 126 | comment,
|
---|
| 127 | hasNewline,
|
---|
| 128 | hasNewlineAfterProp,
|
---|
| 129 | anchor,
|
---|
| 130 | tag,
|
---|
| 131 | end,
|
---|
| 132 | start: start ?? end
|
---|
| 133 | };
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | exports.resolveProps = resolveProps;
|
---|