Debian Codegraph @

cmd/go-yaml/event.go

raw symbols SCIP
1 // Copyright 2025 The go-yaml Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Package main provides YAML event formatting utilities for the go-yaml tool.
5
6 package main
7
8 import (
9 "bytes"
10 "fmt"
11 "io"
12
13 "go.yaml.in/yaml/v4"
14 "go.yaml.in/yaml/v4/internal/libyaml"
15 )
16
17 // EventType represents the type of a YAML event
18 type EventType string
19
20 // Event type constants for CLI output formatting.
21 const (
22 EventDocumentStart EventType = "DOCUMENT-START"
23 EventDocumentEnd EventType = "DOCUMENT-END"
24 EventScalar EventType = "SCALAR"
25 EventSequenceStart EventType = "SEQUENCE-START"
26 EventSequenceEnd EventType = "SEQUENCE-END"
27 EventMappingStart EventType = "MAPPING-START"
28 EventMappingEnd EventType = "MAPPING-END"
29 )
30
31 // Event represents a YAML event
32 type Event struct {
33 Type EventType
34 Value string
35 Anchor string
36 Tag string
37 Style string
38 Implicit bool
39 StartLine int
40 StartColumn int
41 EndLine int
42 EndColumn int
43 HeadComment string
44 LineComment string
45 FootComment string
46 }
47
48 // EventInfo represents the information about a YAML event for YAML encoding
49 type EventInfo struct {
50 Event string `yaml:"event"`
51 Value string `yaml:"value,omitempty"`
52 Style string `yaml:"style,omitempty"`
53 Tag string `yaml:"tag,omitempty"`
54 Anchor string `yaml:"anchor,omitempty"`
55 Implicit *bool `yaml:"implicit,omitempty"`
56 Explicit *bool `yaml:"explicit,omitempty"`
57 Head string `yaml:"head,omitempty"`
58 Line string `yaml:"line,omitempty"`
59 Foot string `yaml:"foot,omitempty"`
60 Pos string `yaml:"pos,omitempty"`
61 }
62
63 // ProcessEvents reads YAML from reader and outputs event information
64 func ProcessEvents(reader io.Reader, profuse, compact, unmarshal bool) error {
65 if unmarshal {
66 return processEventsUnmarshal(reader, profuse, compact)
67 }
68 return processEventsDecode(reader, profuse, compact)
69 }
70
71 // processEventsDecode uses libyaml.Parser.Parse for YAML processing
72 func processEventsDecode(reader io.Reader, profuse, compact bool) error {
73 // Read all input from reader
74 input, err := io.ReadAll(reader)
75 if err != nil {
76 return fmt.Errorf("failed to read input: %w", err)
77 }
78
79 // Get events from parser directly
80 events, err := getEventsFromParser(input, profuse)
81 if err != nil {
82 return err
83 }
84
85 if compact {
86 // For compact mode, output each event as a flow style mapping in a sequence
87 for _, event := range events {
88 info := formatEventInfo(event, profuse)
89
90 // Create a YAML node with flow style for the mapping
91 compactNode := &yaml.Node{
92 Kind: yaml.MappingNode,
93 Style: yaml.FlowStyle,
94 }
95
96 // Add the Event field
97 compactNode.Content = append(compactNode.Content,
98 &yaml.Node{Kind: yaml.ScalarNode, Value: "event"},
99 &yaml.Node{Kind: yaml.ScalarNode, Value: info.Event})
100
101 // Add other fields if they exist
102 if info.Value != "" {
103 compactNode.Content = append(compactNode.Content,
104 &yaml.Node{Kind: yaml.ScalarNode, Value: "value"},
105 &yaml.Node{Kind: yaml.ScalarNode, Value: info.Value})
106 }
107 if info.Style != "" {
108 compactNode.Content = append(compactNode.Content,
109 &yaml.Node{Kind: yaml.ScalarNode, Value: "style"},
110 &yaml.Node{Kind: yaml.ScalarNode, Value: info.Style})
111 }
112 if info.Tag != "" {
113 compactNode.Content = append(compactNode.Content,
114 &yaml.Node{Kind: yaml.ScalarNode, Value: "tag"},
115 &yaml.Node{Kind: yaml.ScalarNode, Value: info.Tag})
116 }
117 if info.Anchor != "" {
118 compactNode.Content = append(compactNode.Content,
119 &yaml.Node{Kind: yaml.ScalarNode, Value: "anchor"},
120 &yaml.Node{Kind: yaml.ScalarNode, Value: info.Anchor})
121 }
122 if info.Implicit != nil {
123 compactNode.Content = append(compactNode.Content,
124 &yaml.Node{Kind: yaml.ScalarNode, Value: "implicit"},
125 &yaml.Node{Kind: yaml.ScalarNode, Value: fmt.Sprintf("%t", *info.Implicit)})
126 }
127 if info.Explicit != nil {
128 compactNode.Content = append(compactNode.Content,
129 &yaml.Node{Kind: yaml.ScalarNode, Value: "explicit"},
130 &yaml.Node{Kind: yaml.ScalarNode, Value: fmt.Sprintf("%t", *info.Explicit)})
131 }
132 if info.Head != "" {
133 compactNode.Content = append(compactNode.Content,
134 &yaml.Node{Kind: yaml.ScalarNode, Value: "head"},
135 &yaml.Node{Kind: yaml.ScalarNode, Value: info.Head})
136 }
137 if info.Line != "" {
138 compactNode.Content = append(compactNode.Content,
139 &yaml.Node{Kind: yaml.ScalarNode, Value: "line"},
140 &yaml.Node{Kind: yaml.ScalarNode, Value: info.Line})
141 }
142 if info.Foot != "" {
143 compactNode.Content = append(compactNode.Content,
144 &yaml.Node{Kind: yaml.ScalarNode, Value: "foot"},
145 &yaml.Node{Kind: yaml.ScalarNode, Value: info.Foot})
146 }
147 if info.Pos != "" {
148 compactNode.Content = append(compactNode.Content,
149 &yaml.Node{Kind: yaml.ScalarNode, Value: "pos"},
150 &yaml.Node{Kind: yaml.ScalarNode, Value: info.Pos})
151 }
152
153 var buf bytes.Buffer
154 enc, err := yaml.NewDumper(&buf)
155 if err != nil {
156 return fmt.Errorf("failed to create dumper: %w", err)
157 }
158 if err := enc.Dump([]*yaml.Node{compactNode}); err != nil {
159 enc.Close()
160 return fmt.Errorf("failed to dump compact event info: %w", err)
161 }
162 if err := enc.Close(); err != nil {
163 return fmt.Errorf("failed to close dumper: %w", err)
164 }
165 fmt.Print(buf.String())
166 }
167 } else {
168 // For non-compact mode, output each event as a separate mapping
169 for _, event := range events {
170 info := formatEventInfo(event, profuse)
171
172 var buf bytes.Buffer
173 enc, err := yaml.NewDumper(&buf)
174 if err != nil {
175 return fmt.Errorf("failed to create dumper: %w", err)
176 }
177 if err := enc.Dump([]*EventInfo{info}); err != nil {
178 enc.Close()
179 return fmt.Errorf("failed to dump event info: %w", err)
180 }
181 if err := enc.Close(); err != nil {
182 return fmt.Errorf("failed to close dumper: %w", err)
183 }
184 fmt.Print(buf.String())
185 }
186 }
187
188 return nil
189 }
190
191 // processEventsUnmarshal uses libyaml.Parser.Parse for YAML processing
192 func processEventsUnmarshal(reader io.Reader, profuse, compact bool) error {
193 // Read all input from reader
194 input, err := io.ReadAll(reader)
195 if err != nil {
196 return fmt.Errorf("failed to read input: %w", err)
197 }
198
199 // Get events from parser directly
200 events, err := getEventsFromParser(input, profuse)
201 if err != nil {
202 return err
203 }
204
205 if compact {
206 // For compact mode, output each event as a flow style mapping in a sequence
207 for _, event := range events {
208 info := formatEventInfo(event, profuse)
209
210 // Create a YAML node with flow style for the mapping
211 compactNode := &yaml.Node{
212 Kind: yaml.MappingNode,
213 Style: yaml.FlowStyle,
214 }
215
216 // Add the Event field
217 compactNode.Content = append(compactNode.Content,
218 &yaml.Node{Kind: yaml.ScalarNode, Value: "event"},
219 &yaml.Node{Kind: yaml.ScalarNode, Value: info.Event})
220
221 // Add other fields if they exist
222 if info.Value != "" {
223 compactNode.Content = append(compactNode.Content,
224 &yaml.Node{Kind: yaml.ScalarNode, Value: "value"},
225 &yaml.Node{Kind: yaml.ScalarNode, Value: info.Value})
226 }
227 if info.Style != "" {
228 compactNode.Content = append(compactNode.Content,
229 &yaml.Node{Kind: yaml.ScalarNode, Value: "style"},
230 &yaml.Node{Kind: yaml.ScalarNode, Value: info.Style})
231 }
232 if info.Tag != "" {
233 compactNode.Content = append(compactNode.Content,
234 &yaml.Node{Kind: yaml.ScalarNode, Value: "tag"},
235 &yaml.Node{Kind: yaml.ScalarNode, Value: info.Tag})
236 }
237 if info.Anchor != "" {
238 compactNode.Content = append(compactNode.Content,
239 &yaml.Node{Kind: yaml.ScalarNode, Value: "anchor"},
240 &yaml.Node{Kind: yaml.ScalarNode, Value: info.Anchor})
241 }
242 if info.Implicit != nil {
243 compactNode.Content = append(compactNode.Content,
244 &yaml.Node{Kind: yaml.ScalarNode, Value: "implicit"},
245 &yaml.Node{Kind: yaml.ScalarNode, Value: fmt.Sprintf("%t", *info.Implicit)})
246 }
247 if info.Explicit != nil {
248 compactNode.Content = append(compactNode.Content,
249 &yaml.Node{Kind: yaml.ScalarNode, Value: "explicit"},
250 &yaml.Node{Kind: yaml.ScalarNode, Value: fmt.Sprintf("%t", *info.Explicit)})
251 }
252 if info.Head != "" {
253 compactNode.Content = append(compactNode.Content,
254 &yaml.Node{Kind: yaml.ScalarNode, Value: "head"},
255 &yaml.Node{Kind: yaml.ScalarNode, Value: info.Head})
256 }
257 if info.Line != "" {
258 compactNode.Content = append(compactNode.Content,
259 &yaml.Node{Kind: yaml.ScalarNode, Value: "line"},
260 &yaml.Node{Kind: yaml.ScalarNode, Value: info.Line})
261 }
262 if info.Foot != "" {
263 compactNode.Content = append(compactNode.Content,
264 &yaml.Node{Kind: yaml.ScalarNode, Value: "foot"},
265 &yaml.Node{Kind: yaml.ScalarNode, Value: info.Foot})
266 }
267 if info.Pos != "" {
268 compactNode.Content = append(compactNode.Content,
269 &yaml.Node{Kind: yaml.ScalarNode, Value: "pos"},
270 &yaml.Node{Kind: yaml.ScalarNode, Value: info.Pos})
271 }
272
273 var buf bytes.Buffer
274 enc, err := yaml.NewDumper(&buf)
275 if err != nil {
276 return fmt.Errorf("failed to create dumper: %w", err)
277 }
278 if err := enc.Dump([]*yaml.Node{compactNode}); err != nil {
279 enc.Close()
280 return fmt.Errorf("failed to dump compact event info: %w", err)
281 }
282 if err := enc.Close(); err != nil {
283 return fmt.Errorf("failed to close dumper: %w", err)
284 }
285 fmt.Print(buf.String())
286 }
287 } else {
288 // For non-compact mode, output each event as a separate mapping
289 for _, event := range events {
290 info := formatEventInfo(event, profuse)
291
292 var buf bytes.Buffer
293 enc, err := yaml.NewDumper(&buf)
294 if err != nil {
295 return fmt.Errorf("failed to create dumper: %w", err)
296 }
297 if err := enc.Dump([]*EventInfo{info}); err != nil {
298 enc.Close()
299 return fmt.Errorf("failed to dump event info: %w", err)
300 }
301 if err := enc.Close(); err != nil {
302 return fmt.Errorf("failed to close dumper: %w", err)
303 }
304 fmt.Print(buf.String())
305 }
306 }
307
308 return nil
309 }
310
311 // formatEventInfo converts an Event to an EventInfo struct for YAML encoding
312 func formatEventInfo(event *Event, profuse bool) *EventInfo {
313 info := &EventInfo{
314 Event: string(event.Type),
315 }
316
317 if event.Value != "" {
318 info.Value = event.Value
319 }
320 if event.Style != "" {
321 info.Style = event.Style
322 }
323 if event.Tag != "" {
324 info.Tag = event.Tag
325 }
326 if event.Anchor != "" {
327 info.Anchor = event.Anchor
328 }
329 if event.HeadComment != "" {
330 info.Head = event.HeadComment
331 }
332 if event.LineComment != "" {
333 info.Line = event.LineComment
334 }
335 if event.FootComment != "" {
336 info.Foot = event.FootComment
337 }
338 if profuse {
339 if event.StartLine == event.EndLine && event.StartColumn == event.EndColumn {
340 // Single position
341 info.Pos = fmt.Sprintf("%d:%d", event.StartLine, event.StartColumn)
342 } else if event.StartLine == event.EndLine {
343 // Range on same line
344 info.Pos = fmt.Sprintf("%d:%d-%d", event.StartLine, event.StartColumn, event.EndColumn)
345 } else {
346 // Range across different lines
347 info.Pos = fmt.Sprintf("%d:%d-%d:%d", event.StartLine, event.StartColumn, event.EndLine, event.EndColumn)
348 }
349 }
350
351 // Handle implicit/explicit for document start/end events
352 if event.Type == "DOCUMENT-START" || event.Type == "DOCUMENT-END" {
353 if profuse {
354 // For -E mode: show implicit: true when implicit
355 if event.Implicit {
356 trueVal := true
357 info.Implicit = &trueVal
358 }
359 } else {
360 // For -e mode: show explicit: true when NOT implicit
361 if !event.Implicit {
362 trueVal := true
363 info.Explicit = &trueVal
364 }
365 }
366 }
367
368 return info
369 }
370
371 // getEventsFromParser parses YAML input and extracts events with implicit field information
372 func getEventsFromParser(input []byte, profuse bool) ([]*Event, error) {
373 p := libyaml.NewParser()
374 if len(input) == 0 {
375 input = []byte{'\n'}
376 }
377 p.SetInputString(input)
378
379 var events []*Event
380 var ev libyaml.Event
381
382 for {
383 if err := p.Parse(&ev); err != nil {
384 return nil, fmt.Errorf("failed to parse YAML: %w", err)
385 }
386
387 event := convertLibyamlEvent(&ev, profuse)
388 if event != nil {
389 events = append(events, event)
390 }
391
392 if ev.Type == libyaml.STREAM_END_EVENT {
393 ev.Delete()
394 break
395 }
396 ev.Delete()
397 }
398
399 return events, nil
400 }
401
402 // convertLibyamlEvent converts a libyaml event to our Event struct
403 func convertLibyamlEvent(ev *libyaml.Event, profuse bool) *Event {
404 // Skip stream events
405 if ev.Type == libyaml.STREAM_START_EVENT || ev.Type == libyaml.STREAM_END_EVENT {
406 return nil
407 }
408
409 event := &Event{
410 StartLine: ev.StartMark.Line,
411 StartColumn: ev.StartMark.Column,
412 EndLine: ev.EndMark.Line,
413 EndColumn: ev.EndMark.Column,
414 HeadComment: string(ev.HeadComment),
415 LineComment: string(ev.LineComment),
416 FootComment: string(ev.FootComment),
417 }
418
419 switch ev.Type {
420 case libyaml.DOCUMENT_START_EVENT:
421 event.Type = "DOCUMENT-START"
422 event.Implicit = ev.Implicit
423 case libyaml.DOCUMENT_END_EVENT:
424 event.Type = "DOCUMENT-END"
425 event.Implicit = ev.Implicit
426 case libyaml.MAPPING_START_EVENT:
427 event.Type = "MAPPING-START"
428 event.Anchor = string(ev.Anchor)
429 event.Tag = string(ev.Tag)
430 // Style handling for mapping
431 if ev.MappingStyle() == libyaml.FLOW_MAPPING_STYLE {
432 event.Style = "Flow"
433 }
434 case libyaml.MAPPING_END_EVENT:
435 event.Type = "MAPPING-END"
436 case libyaml.SEQUENCE_START_EVENT:
437 event.Type = "SEQUENCE-START"
438 event.Anchor = string(ev.Anchor)
439 event.Tag = string(ev.Tag)
440 // Style handling for sequence
441 if ev.SequenceStyle() == libyaml.FLOW_SEQUENCE_STYLE {
442 event.Style = "Flow"
443 }
444 case libyaml.SEQUENCE_END_EVENT:
445 event.Type = "SEQUENCE-END"
446 case libyaml.SCALAR_EVENT:
447 event.Type = "SCALAR"
448 event.Value = string(ev.Value)
449 event.Anchor = string(ev.Anchor)
450 event.Tag = string(ev.Tag)
451 event.Implicit = ev.Implicit
452 // Style handling for scalar
453 switch ev.ScalarStyle() {
454 case libyaml.PLAIN_SCALAR_STYLE:
455 if profuse {
456 event.Style = "Plain"
457 }
458 case libyaml.DOUBLE_QUOTED_SCALAR_STYLE:
459 event.Style = "Double"
460 case libyaml.SINGLE_QUOTED_SCALAR_STYLE:
461 event.Style = "Single"
462 case libyaml.LITERAL_SCALAR_STYLE:
463 event.Style = "Literal"
464 case libyaml.FOLDED_SCALAR_STYLE:
465 event.Style = "Folded"
466 }
467 case libyaml.ALIAS_EVENT:
468 event.Type = "ALIAS"
469 event.Anchor = string(ev.Anchor)
470 }
471
472 return event
473 }