(input: string)
| 167 | } |
| 168 | |
| 169 | export function parseSyntaxToAst(input: string): ParseResult { |
| 170 | const errors: SyntaxError[] = []; |
| 171 | const root: ObjectNode = createObjectNode(0); |
| 172 | const stack: StackFrame[] = [ |
| 173 | { indent: -1, node: root, parent: null, key: null }, |
| 174 | ]; |
| 175 | |
| 176 | const lines = input.split(/\r?\n/); |
| 177 | lines.forEach((line, index) => { |
| 178 | const lineNumber = index + 1; |
| 179 | if (!line.trim()) return; |
| 180 | const { indent, content } = getIndentInfo(line); |
| 181 | if (isCommentLine(content) || isCodeFenceLine(content)) return; |
| 182 | const stripped = stripComments(content); |
| 183 | if (!stripped.trim()) return; |
| 184 | |
| 185 | while (stack.length > 1 && indent <= stack[stack.length - 1].indent) { |
| 186 | stack.pop(); |
| 187 | } |
| 188 | |
| 189 | const parentFrame = stack[stack.length - 1]; |
| 190 | let parentNode = parentFrame.node; |
| 191 | |
| 192 | const trimmed = stripped.trim(); |
| 193 | if ( |
| 194 | trimmed.startsWith('-') && |
| 195 | (trimmed.length === 1 || isWhitespace(trimmed[1])) |
| 196 | ) { |
| 197 | if (parentNode.kind !== 'array') { |
| 198 | if ( |
| 199 | parentNode.kind === 'object' && |
| 200 | Object.keys(parentNode.entries).length === 0 && |
| 201 | parentNode.value === undefined && |
| 202 | parentFrame.parent && |
| 203 | parentFrame.key |
| 204 | ) { |
| 205 | const arrayNode = createArrayNode(parentNode.line); |
| 206 | if (parentFrame.parent.kind === 'object') { |
| 207 | parentFrame.parent.entries[parentFrame.key] = arrayNode; |
| 208 | } else if (parentFrame.parent.kind === 'array') { |
| 209 | const indexInParent = parentFrame.parent.items.indexOf(parentNode); |
| 210 | if (indexInParent >= 0) |
| 211 | parentFrame.parent.items[indexInParent] = arrayNode; |
| 212 | } |
| 213 | parentFrame.node = arrayNode; |
| 214 | parentNode = arrayNode; |
| 215 | } else { |
| 216 | errors.push({ |
| 217 | path: '', |
| 218 | line: lineNumber, |
| 219 | code: 'bad_list', |
| 220 | message: 'List item is not under an array container.', |
| 221 | raw: trimmed, |
| 222 | }); |
| 223 | return; |
| 224 | } |
| 225 | } |
| 226 |
no test coverage detected
searching dependent graphs…