Skip to content

Commit

Permalink
support suffix keyword for document header option (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy authored Nov 5, 2024
1 parent 97070fb commit 7edcf34
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
23 changes: 23 additions & 0 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2149,6 +2149,29 @@ s: >-3
},
},
},
{
YAML: `
|2-
text
`,
Tokens: token.Tokens{
{
Type: token.LiteralType,
CharacterType: token.CharacterTypeIndicator,
Indicator: token.BlockScalarIndicator,
Value: "|2-",
Origin: "\n|2-\n",
},
{
Type: token.StringType,
CharacterType: token.CharacterTypeMiscellaneous,
Indicator: token.NotIndicator,
Value: "\n text",
Origin: "\n text\n",
},
},
},
}
for _, test := range tests {
t.Run(test.YAML, func(t *testing.T) {
Expand Down
11 changes: 7 additions & 4 deletions scanner/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,12 @@ func (c *Context) updateDocumentIndentColumn() {
}

func (c *Context) docFirstLineIndentColumnByDocOpt() int {
trimmed := strings.TrimPrefix(c.docOpt, "-")
trimmed = strings.TrimPrefix(trimmed, "+")
i, _ := strconv.ParseInt(trimmed, 10, 64)
opt := c.docOpt
opt = strings.TrimPrefix(opt, "-")
opt = strings.TrimPrefix(opt, "+")
opt = strings.TrimSuffix(opt, "-")
opt = strings.TrimSuffix(opt, "+")
i, _ := strconv.ParseInt(opt, 10, 64)
return int(i)
}

Expand Down Expand Up @@ -270,7 +273,7 @@ func (c *Context) existsBuffer() bool {

func (c *Context) bufferedSrc() []rune {
src := c.buf[:c.notSpaceCharPos]
if c.isDocument() && strings.HasPrefix(c.docOpt, "-") {
if c.isDocument() && (strings.HasPrefix(c.docOpt, "-") || strings.HasSuffix(c.docOpt, "-")) {
// remove end '\n' character and trailing empty lines
// https://yaml.org/spec/1.2.2/#8112-block-chomping-indicator
for {
Expand Down
7 changes: 4 additions & 3 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,9 +833,10 @@ func (s *Scanner) validateDocumentHeaderOption(opt string) error {
if len(opt) == 0 {
return nil
}
if opt[0] == '+' || opt[0] == '-' {
opt = opt[1:]
}
opt = strings.TrimPrefix(opt, "-")
opt = strings.TrimPrefix(opt, "+")
opt = strings.TrimSuffix(opt, "-")
opt = strings.TrimSuffix(opt, "+")
if len(opt) == 0 {
return nil
}
Expand Down

0 comments on commit 7edcf34

Please sign in to comment.