Debian Codegraph @

internal/libyaml/yaml.go

raw symbols SCIP
1 // Copyright 2006-2010 Kirill Simonov
2 // Copyright 2011-2019 Canonical Ltd
3 // Copyright 2025 The go-yaml Project Contributors
4 // SPDX-License-Identifier: Apache-2.0 AND MIT
5
6 // Core libyaml types and structures.
7 // Defines Parser, Emitter, Event, Token, and related constants for YAML
8 // processing.
9
10 package libyaml
11
12 import (
13 "fmt"
14 "strings"
15 )
16
17 // VersionDirective holds the YAML version directive data.
18 type VersionDirective struct {
19 major int8 // The major version number.
20 minor int8 // The minor version number.
21 }
22
23 // Major returns the major version number.
24 func (v *VersionDirective) Major() int { return int(v.major) }
25
26 // Minor returns the minor version number.
27 func (v *VersionDirective) Minor() int { return int(v.minor) }
28
29 // TagDirective holds the YAML tag directive data.
30 type TagDirective struct {
31 handle []byte // The tag handle.
32 prefix []byte // The tag prefix.
33 }
34
35 // GetHandle returns the tag handle.
36 func (t *TagDirective) GetHandle() string { return string(t.handle) }
37
38 // GetPrefix returns the tag prefix.
39 func (t *TagDirective) GetPrefix() string { return string(t.prefix) }
40
41 // Encoding represents the character encoding of a YAML stream.
42 type Encoding int
43
44 // The stream encoding.
45 const (
46 // Let the parser choose the encoding.
47 ANY_ENCODING Encoding = iota
48
49 UTF8_ENCODING // The default UTF-8 encoding.
50 UTF16LE_ENCODING // The UTF-16-LE encoding with BOM.
51 UTF16BE_ENCODING // The UTF-16-BE encoding with BOM.
52 )
53
54 // LineBreak represents the line break style used in YAML output.
55 type LineBreak int
56
57 // Line break types.
58 const (
59 // Let the parser choose the break type.
60 ANY_BREAK LineBreak = iota
61
62 CR_BREAK // Use CR for line breaks (Mac style).
63 LN_BREAK // Use LN for line breaks (Unix style).
64 CRLN_BREAK // Use CR LN for line breaks (DOS style).
65 )
66
67 // QuoteStyle represents the preferred quote style for scalar values.
68 type QuoteStyle int
69
70 // Quote style types for required quoting.
71 const (
72 QuoteSingle QuoteStyle = iota // Prefer single quotes when quoting is required.
73 QuoteDouble // Prefer double quotes when quoting is required.
74 QuoteLegacy // Legacy behavior: double in representer, single in emitter.
75 )
76
77 // ScalarStyle returns the scalar style for this quote preference in the
78 // representer/serializer context.
79 // In this context, both QuoteDouble and QuoteLegacy use double quotes.
80 func (q QuoteStyle) ScalarStyle() ScalarStyle {
81 if q == QuoteDouble || q == QuoteLegacy {
82 return DOUBLE_QUOTED_SCALAR_STYLE
83 }
84 return SINGLE_QUOTED_SCALAR_STYLE
85 }
86
87 // ErrorType represents the category of error that occurred during processing.
88 type ErrorType int
89
90 // Many bad things could happen with the parser and emitter.
91 const (
92 // No error is produced.
93 NO_ERROR ErrorType = iota
94
95 MEMORY_ERROR // Cannot allocate or reallocate a block of memory.
96 READER_ERROR // Cannot read or decode the input stream.
97 SCANNER_ERROR // Cannot scan the input stream.
98 PARSER_ERROR // Cannot parse the input stream.
99 COMPOSER_ERROR // Cannot compose a YAML document.
100 WRITER_ERROR // Cannot write to the output stream.
101 EMITTER_ERROR // Cannot emit a YAML stream.
102 )
103
104 // Mark holds the pointer position.
105 type Mark struct {
106 Index int // The position index.
107 Line int // The position line (1-indexed; 0 means unknown).
108 Column int // The position column (1-indexed; 0 means unknown).
109 }
110
111 // String returns a human-readable string representation of the position mark.
112 func (m Mark) String() string {
113 var builder strings.Builder
114 if m.Line == 0 {
115 return "<unknown position>"
116 }
117
118 fmt.Fprintf(&builder, "line %d", m.Line)
119 if m.Column > 0 {
120 fmt.Fprintf(&builder, ", column %d", m.Column)
121 }
122
123 return builder.String()
124 }
125
126 // shortString returns a compact position string.
127 // Returns "<unknown position>" when Line is 0 (position not known).
128 // When Column is 0 (unknown), it is omitted from output ("L{line}");
129 // otherwise it is displayed as "L{line}.C{col}".
130 func (m Mark) shortString() string {
131 if m.Line == 0 {
132 return "<unknown position>"
133 }
134 if m.Column > 0 {
135 return fmt.Sprintf("L%d.C%d", m.Line, m.Column)
136 }
137 return fmt.Sprintf("L%d", m.Line)
138 }
139
140 // rangeString formats a position range from start mark m to end mark.
141 // Both marks use shortString for their individual display.
142 // When marks are on the same line:
143 // - Both Column==0: just "L2" (unknown columns, no range shown)
144 // - Both Column>0: "L2.C6-C7" (compact column range)
145 // - Mixed columns: "L1.C4-L1" (full start with line-only end)
146 //
147 // When marks are on different lines: "L1.C8-L2.C3"
148 func (m Mark) rangeString(end Mark) string {
149 start := m.shortString()
150 if m.Line == end.Line {
151 if m.Column == 0 && end.Column == 0 {
152 // Same line, unknown columns: just "L2"
153 return start
154 }
155 if m.Column > 0 && end.Column > 0 {
156 if m.Column == end.Column {
157 // Same position: just "L2.C6"
158 return start
159 }
160 // Same line with columns: "L2.C6-C7"
161 return fmt.Sprintf("%s-C%d", start, end.Column)
162 }
163 }
164 return fmt.Sprintf("%s-%s", start, end.shortString())
165 }
166
167 // Node Styles
168
169 // styleInt is the underlying type for style constants.
170 type styleInt int8
171
172 // ScalarStyle represents the formatting style of a scalar value.
173 type ScalarStyle styleInt
174
175 // Scalar styles.
176 const (
177 // Let the emitter choose the style.
178 ANY_SCALAR_STYLE ScalarStyle = 0
179
180 PLAIN_SCALAR_STYLE ScalarStyle = 1 << iota // The plain scalar style.
181 SINGLE_QUOTED_SCALAR_STYLE // The single-quoted scalar style.
182 DOUBLE_QUOTED_SCALAR_STYLE // The double-quoted scalar style.
183 LITERAL_SCALAR_STYLE // The literal scalar style.
184 FOLDED_SCALAR_STYLE // The folded scalar style.
185 )
186
187 // String returns a string representation of a [ScalarStyle].
188 func (style ScalarStyle) String() string {
189 switch style {
190 case PLAIN_SCALAR_STYLE:
191 return "Plain"
192 case SINGLE_QUOTED_SCALAR_STYLE:
193 return "Single"
194 case DOUBLE_QUOTED_SCALAR_STYLE:
195 return "Double"
196 case LITERAL_SCALAR_STYLE:
197 return "Literal"
198 case FOLDED_SCALAR_STYLE:
199 return "Folded"
200 default:
201 return ""
202 }
203 }
204
205 // SequenceStyle represents the formatting style of a sequence node.
206 type SequenceStyle styleInt
207
208 // Sequence styles.
209 const (
210 // Let the emitter choose the style.
211 ANY_SEQUENCE_STYLE SequenceStyle = iota
212
213 BLOCK_SEQUENCE_STYLE // The block sequence style.
214 FLOW_SEQUENCE_STYLE // The flow sequence style.
215 )
216
217 // MappingStyle represents the formatting style of a mapping node.
218 type MappingStyle styleInt
219
220 // Mapping styles.
221 const (
222 // Let the emitter choose the style.
223 ANY_MAPPING_STYLE MappingStyle = iota
224
225 BLOCK_MAPPING_STYLE // The block mapping style.
226 FLOW_MAPPING_STYLE // The flow mapping style.
227 )
228
229 // Tokens
230
231 // TokenType represents the type of a scanned token.
232 type TokenType int
233
234 // Token types.
235 const (
236 // An empty token.
237 NO_TOKEN TokenType = iota
238
239 STREAM_START_TOKEN // A STREAM-START token.
240 STREAM_END_TOKEN // A STREAM-END token.
241
242 VERSION_DIRECTIVE_TOKEN // A VERSION-DIRECTIVE token.
243 TAG_DIRECTIVE_TOKEN // A TAG-DIRECTIVE token.
244 DOCUMENT_START_TOKEN // A DOCUMENT-START token.
245 DOCUMENT_END_TOKEN // A DOCUMENT-END token.
246
247 BLOCK_SEQUENCE_START_TOKEN // A BLOCK-SEQUENCE-START token.
248 BLOCK_MAPPING_START_TOKEN // A BLOCK-SEQUENCE-END token.
249 BLOCK_END_TOKEN // A BLOCK-END token.
250
251 FLOW_SEQUENCE_START_TOKEN // A FLOW-SEQUENCE-START token.
252 FLOW_SEQUENCE_END_TOKEN // A FLOW-SEQUENCE-END token.
253 FLOW_MAPPING_START_TOKEN // A FLOW-MAPPING-START token.
254 FLOW_MAPPING_END_TOKEN // A FLOW-MAPPING-END token.
255
256 BLOCK_ENTRY_TOKEN // A BLOCK-ENTRY token.
257 FLOW_ENTRY_TOKEN // A FLOW-ENTRY token.
258 KEY_TOKEN // A KEY token.
259 VALUE_TOKEN // A VALUE token.
260
261 ALIAS_TOKEN // An ALIAS token.
262 ANCHOR_TOKEN // An ANCHOR token.
263 TAG_TOKEN // A TAG token.
264 SCALAR_TOKEN // A SCALAR token.
265 COMMENT_TOKEN // A COMMENT token.
266 )
267
268 // String returns a string representation of the token type.
269 func (tt TokenType) String() string {
270 switch tt {
271 case NO_TOKEN:
272 return "NO_TOKEN"
273 case STREAM_START_TOKEN:
274 return "STREAM_START_TOKEN"
275 case STREAM_END_TOKEN:
276 return "STREAM_END_TOKEN"
277 case VERSION_DIRECTIVE_TOKEN:
278 return "VERSION_DIRECTIVE_TOKEN"
279 case TAG_DIRECTIVE_TOKEN:
280 return "TAG_DIRECTIVE_TOKEN"
281 case DOCUMENT_START_TOKEN:
282 return "DOCUMENT_START_TOKEN"
283 case DOCUMENT_END_TOKEN:
284 return "DOCUMENT_END_TOKEN"
285 case BLOCK_SEQUENCE_START_TOKEN:
286 return "BLOCK_SEQUENCE_START_TOKEN"
287 case BLOCK_MAPPING_START_TOKEN:
288 return "BLOCK_MAPPING_START_TOKEN"
289 case BLOCK_END_TOKEN:
290 return "BLOCK_END_TOKEN"
291 case FLOW_SEQUENCE_START_TOKEN:
292 return "FLOW_SEQUENCE_START_TOKEN"
293 case FLOW_SEQUENCE_END_TOKEN:
294 return "FLOW_SEQUENCE_END_TOKEN"
295 case FLOW_MAPPING_START_TOKEN:
296 return "FLOW_MAPPING_START_TOKEN"
297 case FLOW_MAPPING_END_TOKEN:
298 return "FLOW_MAPPING_END_TOKEN"
299 case BLOCK_ENTRY_TOKEN:
300 return "BLOCK_ENTRY_TOKEN"
301 case FLOW_ENTRY_TOKEN:
302 return "FLOW_ENTRY_TOKEN"
303 case KEY_TOKEN:
304 return "KEY_TOKEN"
305 case VALUE_TOKEN:
306 return "VALUE_TOKEN"
307 case ALIAS_TOKEN:
308 return "ALIAS_TOKEN"
309 case ANCHOR_TOKEN:
310 return "ANCHOR_TOKEN"
311 case TAG_TOKEN:
312 return "TAG_TOKEN"
313 case SCALAR_TOKEN:
314 return "SCALAR_TOKEN"
315 case COMMENT_TOKEN:
316 return "COMMENT_TOKEN"
317 }
318 return "<unknown token>"
319 }
320
321 // Token holds information about a scanning token.
322 type Token struct {
323 // The token type.
324 Type TokenType
325
326 // The start/end of the token.
327 StartMark, EndMark Mark
328
329 // The stream encoding (for STREAM_START_TOKEN).
330 encoding Encoding
331
332 // The alias/anchor/scalar Value or tag/tag directive handle
333 // (for ALIAS_TOKEN, ANCHOR_TOKEN, SCALAR_TOKEN, TAG_TOKEN, TAG_DIRECTIVE_TOKEN).
334 Value []byte
335
336 // The tag suffix (for TAG_TOKEN).
337 suffix []byte
338
339 // The tag directive prefix (for TAG_DIRECTIVE_TOKEN).
340 prefix []byte
341
342 // The scalar Style (for SCALAR_TOKEN).
343 Style ScalarStyle
344
345 // The version directive major/minor (for VERSION_DIRECTIVE_TOKEN).
346 major, minor int8
347 }
348
349 // Events
350
351 // EventType represents the type of a parsing or emitting event.
352 type EventType int8
353
354 // Event types.
355 const (
356 // An empty event.
357 NO_EVENT EventType = iota
358
359 STREAM_START_EVENT // A STREAM-START event.
360 STREAM_END_EVENT // A STREAM-END event.
361 DOCUMENT_START_EVENT // A DOCUMENT-START event.
362 DOCUMENT_END_EVENT // A DOCUMENT-END event.
363 ALIAS_EVENT // An ALIAS event.
364 SCALAR_EVENT // A SCALAR event.
365 SEQUENCE_START_EVENT // A SEQUENCE-START event.
366 SEQUENCE_END_EVENT // A SEQUENCE-END event.
367 MAPPING_START_EVENT // A MAPPING-START event.
368 MAPPING_END_EVENT // A MAPPING-END event.
369 TAIL_COMMENT_EVENT
370 )
371
372 // eventStrings maps EventType constants to their string representations.
373 var eventStrings = []string{
374 NO_EVENT: "none",
375 STREAM_START_EVENT: "stream start",
376 STREAM_END_EVENT: "stream end",
377 DOCUMENT_START_EVENT: "document start",
378 DOCUMENT_END_EVENT: "document end",
379 ALIAS_EVENT: "alias",
380 SCALAR_EVENT: "scalar",
381 SEQUENCE_START_EVENT: "sequence start",
382 SEQUENCE_END_EVENT: "sequence end",
383 MAPPING_START_EVENT: "mapping start",
384 MAPPING_END_EVENT: "mapping end",
385 TAIL_COMMENT_EVENT: "tail comment",
386 }
387
388 // String returns a string representation of the event type.
389 func (e EventType) String() string {
390 if e < 0 || int(e) >= len(eventStrings) {
391 return fmt.Sprintf("unknown event %d", e)
392 }
393 return eventStrings[e]
394 }
395
396 // Event holds information about a parsing or emitting event.
397 type Event struct {
398 // The event type.
399 Type EventType
400
401 // The start and end of the event.
402 StartMark, EndMark Mark
403
404 // The document encoding (for STREAM_START_EVENT).
405 encoding Encoding
406
407 // The version directive (for DOCUMENT_START_EVENT).
408 versionDirective *VersionDirective
409
410 // The list of tag directives (for DOCUMENT_START_EVENT).
411 tagDirectives []TagDirective
412
413 // The comments
414 HeadComment []byte
415 LineComment []byte
416 FootComment []byte
417 TailComment []byte
418
419 // The Anchor (for SCALAR_EVENT, SEQUENCE_START_EVENT, MAPPING_START_EVENT, ALIAS_EVENT).
420 Anchor []byte
421
422 // The Tag (for SCALAR_EVENT, SEQUENCE_START_EVENT, MAPPING_START_EVENT).
423 Tag []byte
424
425 // The scalar Value (for SCALAR_EVENT).
426 Value []byte
427
428 // Is the document start/end indicator Implicit, or the tag optional?
429 // (for DOCUMENT_START_EVENT, DOCUMENT_END_EVENT, SEQUENCE_START_EVENT, MAPPING_START_EVENT, SCALAR_EVENT).
430 Implicit bool
431
432 // Is the tag optional for any non-plain style? (for SCALAR_EVENT).
433 quoted_implicit bool
434
435 // The Style (for SCALAR_EVENT, SEQUENCE_START_EVENT, MAPPING_START_EVENT).
436 Style Style
437 }
438
439 // ScalarStyle returns the style of a scalar event.
440 func (e *Event) ScalarStyle() ScalarStyle { return ScalarStyle(e.Style) }
441
442 // SequenceStyle returns the style of a sequence event.
443 func (e *Event) SequenceStyle() SequenceStyle { return SequenceStyle(e.Style) }
444
445 // MappingStyle returns the style of a mapping event.
446 func (e *Event) MappingStyle() MappingStyle { return MappingStyle(e.Style) }
447
448 // GetEncoding returns the stream encoding (for STREAM_START_EVENT).
449 func (e *Event) GetEncoding() Encoding { return e.encoding }
450
451 // GetVersionDirective returns the version directive (for DOCUMENT_START_EVENT).
452 func (e *Event) GetVersionDirective() *VersionDirective { return e.versionDirective }
453
454 // GetTagDirectives returns the tag directives (for DOCUMENT_START_EVENT).
455 func (e *Event) GetTagDirectives() []TagDirective { return e.tagDirectives }
456
457 // Nodes
458 const (
459 NULL_TAG = "tag:yaml.org,2002:null" // The tag !!null with the only possible value: null.
460 BOOL_TAG = "tag:yaml.org,2002:bool" // The tag !!bool with the values: true and false.
461 STR_TAG = "tag:yaml.org,2002:str" // The tag !!str for string values.
462 INT_TAG = "tag:yaml.org,2002:int" // The tag !!int for integer values.
463 FLOAT_TAG = "tag:yaml.org,2002:float" // The tag !!float for float values.
464 TIMESTAMP_TAG = "tag:yaml.org,2002:timestamp" // The tag !!timestamp for date and time values.
465
466 SEQ_TAG = "tag:yaml.org,2002:seq" // The tag !!seq is used to denote sequences.
467 MAP_TAG = "tag:yaml.org,2002:map" // The tag !!map is used to denote mapping.
468
469 // Not in original libyaml.
470 BINARY_TAG = "tag:yaml.org,2002:binary"
471 MERGE_TAG = "tag:yaml.org,2002:merge"
472
473 DEFAULT_SCALAR_TAG = STR_TAG // The default scalar tag is !!str.
474 DEFAULT_SEQUENCE_TAG = SEQ_TAG // The default sequence tag is !!seq.
475 DEFAULT_MAPPING_TAG = MAP_TAG // The default mapping tag is !!map.
476 )