From 7edcf34f121bcbdb542667ddd330625feca8f4ed Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Tue, 5 Nov 2024 15:25:58 +0900 Subject: [PATCH] support suffix keyword for document header option (#506) --- lexer/lexer_test.go | 23 +++++++++++++++++++++++ scanner/context.go | 11 +++++++---- scanner/scanner.go | 7 ++++--- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/lexer/lexer_test.go b/lexer/lexer_test.go index d4eec75..81b8be9 100644 --- a/lexer/lexer_test.go +++ b/lexer/lexer_test.go @@ -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) { diff --git a/scanner/context.go b/scanner/context.go index 24b96c4..415cf07 100644 --- a/scanner/context.go +++ b/scanner/context.go @@ -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) } @@ -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 { diff --git a/scanner/scanner.go b/scanner/scanner.go index 371bd8a..e89290a 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -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 }