forked from salesforce/agentscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse-sequence.ts
More file actions
133 lines (121 loc) · 4.11 KB
/
parse-sequence.ts
File metadata and controls
133 lines (121 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Copyright (c) 2026, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* For full license text, see the LICENSE file in the repo root or https://www.apache.org/licenses/LICENSE-2.0
*/
/**
* Sequence parsing functions extracted from Parser class.
*
* Each function takes a ParserContext as its first parameter, following
* the same free-function pattern as parse-mapping.ts and parse-templates.ts.
*/
import { TokenKind } from './token.js';
import { CSTNode } from './cst-node.js';
import { makeErrorNode } from './errors.js';
import { synchronize, skipNewlines, isAtEnd } from './recovery.js';
import {
parseMapping,
parseMappingItem,
tryParseColinearValue,
isColinearMappingElement,
parseColinearMappingElement,
} from './parse-mapping.js';
import type { ParserContext } from './parser.js';
/**
* Parse a YAML-style sequence (list of `- item` entries).
*
* Exported for use by parser.ts dispatch and by parse-mapping.ts
* via ParseSequenceFn callback.
*/
export function parseSequence(ctx: ParserContext): CSTNode {
const startTok = ctx.peek();
const node = ctx.startNode('sequence');
while (ctx.peekKind() === TokenKind.DASH_SPACE) {
const elem = parseSequenceElement(ctx);
if (elem) node.appendChild(elem);
skipNewlines(ctx);
}
// Non-sequence items remaining at same indent → wrap in ERROR inside sequence
while (
!isAtEnd(ctx) &&
ctx.peekKind() !== TokenKind.DEDENT &&
ctx.peekKind() !== TokenKind.DASH_SPACE
) {
skipNewlines(ctx);
if (isAtEnd(ctx) || ctx.peekKind() === TokenKind.DEDENT) break;
// Try to parse as mapping item and wrap in ERROR
const parseSeq = (_ctx: ParserContext) => parseSequence(_ctx);
const item = parseMappingItem(ctx, parseSeq);
if (item) {
const errNode = makeErrorNode(
ctx.source,
[item],
item.startOffset,
item.endOffset,
item.startPosition,
item.endPosition
);
node.appendChild(errNode);
} else {
const err = synchronize(ctx);
if (err) {
node.appendChild(err);
} else {
ctx.consume();
}
}
}
ctx.finishNode(node, startTok);
return node;
}
/**
* Parse a single sequence element: `- <value>`.
*/
function parseSequenceElement(ctx: ParserContext): CSTNode {
const startTok = ctx.peek();
const node = ctx.startNode('sequence_element');
// Consume "- " or "-"
ctx.addAnonymousChild(node, ctx.consume());
const parseSeq = (_ctx: ParserContext) => parseSequence(_ctx);
// Check for colinear mapping element (key: value on same line)
if (isColinearMappingElement(ctx)) {
const mappingElem = parseColinearMappingElement(ctx);
if (mappingElem) node.appendChild(mappingElem, 'colinear_mapping_element');
// Optional block value (indented mapping below)
if (ctx.peekKind() === TokenKind.NEWLINE) ctx.consume();
if (ctx.peekKind() === TokenKind.INDENT) {
ctx.consume();
const blockValue = parseMapping(ctx, parseSeq);
if (blockValue) node.appendChild(blockValue, 'block_value');
if (ctx.peekKind() === TokenKind.DEDENT) ctx.consume();
}
} else if (
ctx.peekKind() === TokenKind.NEWLINE ||
ctx.peekKind() === TokenKind.EOF ||
ctx.peekKind() === TokenKind.INDENT
) {
// Bare dash with optional block value below (or immediately indented)
if (ctx.peekKind() === TokenKind.NEWLINE) ctx.consume();
if (ctx.peekKind() === TokenKind.INDENT) {
ctx.consume();
const blockValue = parseMapping(ctx, parseSeq);
if (blockValue) node.appendChild(blockValue, 'block_value');
if (ctx.peekKind() === TokenKind.DEDENT) ctx.consume();
}
} else {
// Colinear value
const colinear = tryParseColinearValue(ctx);
if (colinear) {
if (colinear.errorPrefix) node.appendChild(colinear.errorPrefix);
node.appendChild(colinear.value, 'colinear_value');
}
// Inline comment after value
if (ctx.peekKind() === TokenKind.COMMENT) {
node.appendChild(ctx.consumeNamed('comment'));
}
if (ctx.peekKind() === TokenKind.NEWLINE) ctx.consume();
}
ctx.finishNode(node, startTok);
return node;
}