| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package main |
| 8 |
|
| 9 |
import ( |
| 10 |
"errors" |
| 11 |
"fmt" |
| 12 |
"io" |
| 13 |
|
| 14 |
"go.yaml.in/yaml/v4/internal/libyaml" |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
type Parser struct { |
| 19 |
parser libyaml.Parser |
| 20 |
done bool |
| 21 |
pendingTokens []*Token |
| 22 |
commentsHead int |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
func NewParser(reader io.Reader) (*Parser, error) { |
| 27 |
p := &Parser{ |
| 28 |
parser: libyaml.NewParser(), |
| 29 |
} |
| 30 |
p.parser.SetInputReader(reader) |
| 31 |
return p, nil |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
func (p *Parser) Next() (*Token, error) { |
| 36 |
|
| 37 |
if len(p.pendingTokens) > 0 { |
| 38 |
token := p.pendingTokens[0] |
| 39 |
p.pendingTokens = p.pendingTokens[1:] |
| 40 |
return token, nil |
| 41 |
} |
| 42 |
|
| 43 |
if p.done { |
| 44 |
return nil, nil |
| 45 |
} |
| 46 |
|
| 47 |
var yamlToken libyaml.Token |
| 48 |
if err := p.parser.Scan(&yamlToken); err != nil { |
| 49 |
if !errors.Is(err, io.EOF) { |
| 50 |
return nil, fmt.Errorf("parser error: %w", err) |
| 51 |
} |
| 52 |
p.done = true |
| 53 |
return nil, nil |
| 54 |
} |
| 55 |
|
| 56 |
token := &Token{ |
| 57 |
StartLine: yamlToken.StartMark.Line, |
| 58 |
StartColumn: yamlToken.StartMark.Column, |
| 59 |
EndLine: yamlToken.EndMark.Line, |
| 60 |
EndColumn: yamlToken.EndMark.Column, |
| 61 |
} |
| 62 |
|
| 63 |
switch yamlToken.Type { |
| 64 |
case libyaml.STREAM_START_TOKEN: |
| 65 |
token.Type = "STREAM-START" |
| 66 |
case libyaml.STREAM_END_TOKEN: |
| 67 |
token.Type = "STREAM-END" |
| 68 |
p.done = true |
| 69 |
case libyaml.DOCUMENT_START_TOKEN: |
| 70 |
token.Type = "DOCUMENT-START" |
| 71 |
case libyaml.DOCUMENT_END_TOKEN: |
| 72 |
token.Type = "DOCUMENT-END" |
| 73 |
case libyaml.BLOCK_SEQUENCE_START_TOKEN: |
| 74 |
token.Type = "BLOCK-SEQUENCE-START" |
| 75 |
case libyaml.BLOCK_MAPPING_START_TOKEN: |
| 76 |
token.Type = "BLOCK-MAPPING-START" |
| 77 |
case libyaml.BLOCK_END_TOKEN: |
| 78 |
token.Type = "BLOCK-END" |
| 79 |
case libyaml.FLOW_SEQUENCE_START_TOKEN: |
| 80 |
token.Type = "FLOW-SEQUENCE-START" |
| 81 |
case libyaml.FLOW_SEQUENCE_END_TOKEN: |
| 82 |
token.Type = "FLOW-SEQUENCE-END" |
| 83 |
case libyaml.FLOW_MAPPING_START_TOKEN: |
| 84 |
token.Type = "FLOW-MAPPING-START" |
| 85 |
case libyaml.FLOW_MAPPING_END_TOKEN: |
| 86 |
token.Type = "FLOW-MAPPING-END" |
| 87 |
case libyaml.BLOCK_ENTRY_TOKEN: |
| 88 |
token.Type = "BLOCK-ENTRY" |
| 89 |
case libyaml.FLOW_ENTRY_TOKEN: |
| 90 |
token.Type = "FLOW-ENTRY" |
| 91 |
case libyaml.KEY_TOKEN: |
| 92 |
token.Type = "KEY" |
| 93 |
case libyaml.VALUE_TOKEN: |
| 94 |
token.Type = "VALUE" |
| 95 |
case libyaml.ALIAS_TOKEN: |
| 96 |
token.Type = "ALIAS" |
| 97 |
token.Value = string(yamlToken.Value) |
| 98 |
case libyaml.ANCHOR_TOKEN: |
| 99 |
token.Type = "ANCHOR" |
| 100 |
token.Value = string(yamlToken.Value) |
| 101 |
case libyaml.TAG_TOKEN: |
| 102 |
token.Type = "TAG" |
| 103 |
token.Value = string(yamlToken.Value) |
| 104 |
case libyaml.SCALAR_TOKEN: |
| 105 |
token.Type = "SCALAR" |
| 106 |
token.Value = string(yamlToken.Value) |
| 107 |
token.Style = yamlToken.Style.String() |
| 108 |
case libyaml.VERSION_DIRECTIVE_TOKEN: |
| 109 |
token.Type = "VERSION-DIRECTIVE" |
| 110 |
case libyaml.TAG_DIRECTIVE_TOKEN: |
| 111 |
token.Type = "TAG-DIRECTIVE" |
| 112 |
default: |
| 113 |
token.Type = "UNKNOWN" |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
p.processComments(&yamlToken, token) |
| 118 |
|
| 119 |
|
| 120 |
if len(p.pendingTokens) > 0 { |
| 121 |
|
| 122 |
p.pendingTokens = append(p.pendingTokens, token) |
| 123 |
|
| 124 |
result := p.pendingTokens[0] |
| 125 |
p.pendingTokens = p.pendingTokens[1:] |
| 126 |
return result, nil |
| 127 |
} |
| 128 |
|
| 129 |
return token, nil |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
func (p *Parser) processComments(yamlToken *libyaml.Token, mainToken *Token) { |
| 134 |
comments := p.parser.GetPendingComments() |
| 135 |
|
| 136 |
for p.commentsHead < len(comments) { |
| 137 |
comment := &comments[p.commentsHead] |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
if yamlToken.StartMark.Index < comment.TokenMark.Index { |
| 142 |
|
| 143 |
break |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
p.appendCommentTokenIfNotEmpty(comment.Head, "head", comment) |
| 148 |
p.appendCommentTokenIfNotEmpty(comment.Line, "line", comment) |
| 149 |
p.appendCommentTokenIfNotEmpty(comment.Foot, "foot", comment) |
| 150 |
|
| 151 |
p.commentsHead++ |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
func (p *Parser) appendCommentTokenIfNotEmpty(value []byte, commentType string, comment *libyaml.Comment) { |
| 157 |
if len(value) > 0 { |
| 158 |
commentToken := &Token{ |
| 159 |
Type: "COMMENT", |
| 160 |
Value: string(value), |
| 161 |
CommentType: commentType, |
| 162 |
StartLine: comment.StartMark.Line, |
| 163 |
StartColumn: comment.StartMark.Column, |
| 164 |
EndLine: comment.EndMark.Line, |
| 165 |
EndColumn: comment.EndMark.Column, |
| 166 |
} |
| 167 |
p.pendingTokens = append(p.pendingTokens, commentToken) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
func (p *Parser) Close() { |
| 173 |
p.parser.Delete() |
| 174 |
} |