1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
|
package scanner
import (
"errors"
"strconv"
"strings"
"sync"
"github.com/goccy/go-yaml/token"
)
// Context context at scanning
type Context struct {
idx int
size int
notSpaceCharPos int
notSpaceOrgCharPos int
src []rune
buf []rune
obuf []rune
tokens token.Tokens
mstate *MultiLineState
}
type MultiLineState struct {
opt string
firstLineIndentColumn int
prevLineIndentColumn int
lineIndentColumn int
lastNotSpaceOnlyLineIndentColumn int
spaceOnlyIndentColumn int
foldedNewLine bool
isRawFolded bool
isLiteral bool
isFolded bool
}
var (
ctxPool = sync.Pool{
New: func() interface{} {
return createContext()
},
}
)
func createContext() *Context {
return &Context{
idx: 0,
tokens: token.Tokens{},
}
}
func newContext(src []rune) *Context {
ctx, _ := ctxPool.Get().(*Context)
ctx.reset(src)
return ctx
}
func (c *Context) release() {
ctxPool.Put(c)
}
func (c *Context) clear() {
c.resetBuffer()
c.mstate = nil
}
func (c *Context) reset(src []rune) {
c.idx = 0
c.size = len(src)
c.src = src
c.tokens = c.tokens[:0]
c.resetBuffer()
c.mstate = nil
}
func (c *Context) resetBuffer() {
c.buf = c.buf[:0]
c.obuf = c.obuf[:0]
c.notSpaceCharPos = 0
c.notSpaceOrgCharPos = 0
}
func (c *Context) breakMultiLine() {
c.mstate = nil
}
func (c *Context) getMultiLineState() *MultiLineState {
return c.mstate
}
func (c *Context) setLiteral(lastDelimColumn int, opt string) {
mstate := &MultiLineState{
isLiteral: true,
opt: opt,
}
indent := firstLineIndentColumnByOpt(opt)
if indent > 0 {
mstate.firstLineIndentColumn = lastDelimColumn + indent
}
c.mstate = mstate
}
func (c *Context) setFolded(lastDelimColumn int, opt string) {
mstate := &MultiLineState{
isFolded: true,
opt: opt,
}
indent := firstLineIndentColumnByOpt(opt)
if indent > 0 {
mstate.firstLineIndentColumn = lastDelimColumn + indent
}
c.mstate = mstate
}
func (c *Context) setRawFolded(column int) {
mstate := &MultiLineState{
isRawFolded: true,
}
mstate.updateIndentColumn(column)
c.mstate = mstate
}
func firstLineIndentColumnByOpt(opt string) int {
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)
}
func (s *MultiLineState) lastDelimColumn() int {
if s.firstLineIndentColumn == 0 {
return 0
}
return s.firstLineIndentColumn - 1
}
func (s *MultiLineState) updateIndentColumn(column int) {
if s.firstLineIndentColumn == 0 {
s.firstLineIndentColumn = column
}
if s.lineIndentColumn == 0 {
s.lineIndentColumn = column
}
}
func (s *MultiLineState) updateSpaceOnlyIndentColumn(column int) {
if s.firstLineIndentColumn != 0 {
return
}
s.spaceOnlyIndentColumn = column
}
func (s *MultiLineState) validateIndentAfterSpaceOnly(column int) error {
if s.firstLineIndentColumn != 0 {
return nil
}
if s.spaceOnlyIndentColumn > column {
return errors.New("invalid number of indent is specified after space only")
}
return nil
}
func (s *MultiLineState) validateIndentColumn() error {
if firstLineIndentColumnByOpt(s.opt) == 0 {
return nil
}
if s.firstLineIndentColumn > s.lineIndentColumn {
return errors.New("invalid number of indent is specified in the multi-line header")
}
return nil
}
func (s *MultiLineState) updateNewLineState() {
s.prevLineIndentColumn = s.lineIndentColumn
if s.lineIndentColumn != 0 {
s.lastNotSpaceOnlyLineIndentColumn = s.lineIndentColumn
}
s.foldedNewLine = true
s.lineIndentColumn = 0
}
func (s *MultiLineState) isIndentColumn(column int) bool {
if s.firstLineIndentColumn == 0 {
return column == 1
}
return s.firstLineIndentColumn > column
}
func (s *MultiLineState) addIndent(ctx *Context, column int) {
if s.firstLineIndentColumn == 0 {
return
}
// If the first line of the document has already been evaluated, the number is treated as the threshold, since the `firstLineIndentColumn` is a positive number.
if column < s.firstLineIndentColumn {
return
}
// `c.foldedNewLine` is a variable that is set to true for every newline.
if !s.isLiteral && s.foldedNewLine {
s.foldedNewLine = false
}
// Since addBuf ignore space character, add to the buffer directly.
ctx.buf = append(ctx.buf, ' ')
ctx.notSpaceCharPos = len(ctx.buf)
}
// updateNewLineInFolded if Folded or RawFolded context and the content on the current line starts at the same column as the previous line,
// treat the new-line-char as a space.
func (s *MultiLineState) updateNewLineInFolded(ctx *Context, column int) {
if s.isLiteral {
return
}
// Folded or RawFolded.
if !s.foldedNewLine {
return
}
var (
lastChar rune
prevLastChar rune
)
if len(ctx.buf) != 0 {
lastChar = ctx.buf[len(ctx.buf)-1]
}
if len(ctx.buf) > 1 {
prevLastChar = ctx.buf[len(ctx.buf)-2]
}
if s.lineIndentColumn == s.prevLineIndentColumn {
// ---
// >
// a
// b
if lastChar == '\n' {
ctx.buf[len(ctx.buf)-1] = ' '
}
} else if s.prevLineIndentColumn == 0 && s.lastNotSpaceOnlyLineIndentColumn == column {
// if previous line is indent-space and new-line-char only, prevLineIndentColumn is zero.
// In this case, last new-line-char is removed.
// ---
// >
// a
//
// b
if lastChar == '\n' && prevLastChar == '\n' {
ctx.buf = ctx.buf[:len(ctx.buf)-1]
ctx.notSpaceCharPos = len(ctx.buf)
}
}
s.foldedNewLine = false
}
func (s *MultiLineState) hasTrimAllEndNewlineOpt() bool {
return strings.HasPrefix(s.opt, "-") || strings.HasSuffix(s.opt, "-") || s.isRawFolded
}
func (s *MultiLineState) hasKeepAllEndNewlineOpt() bool {
return strings.HasPrefix(s.opt, "+") || strings.HasSuffix(s.opt, "+")
}
func (c *Context) addToken(tk *token.Token) {
if tk == nil {
return
}
c.tokens = append(c.tokens, tk)
}
func (c *Context) addBuf(r rune) {
if len(c.buf) == 0 && (r == ' ' || r == '\t') {
return
}
c.buf = append(c.buf, r)
if r != ' ' && r != '\t' {
c.notSpaceCharPos = len(c.buf)
}
}
func (c *Context) addBufWithTab(r rune) {
if len(c.buf) == 0 && r == ' ' {
return
}
c.buf = append(c.buf, r)
if r != ' ' {
c.notSpaceCharPos = len(c.buf)
}
}
func (c *Context) addOriginBuf(r rune) {
c.obuf = append(c.obuf, r)
if r != ' ' && r != '\t' {
c.notSpaceOrgCharPos = len(c.obuf)
}
}
func (c *Context) removeRightSpaceFromBuf() {
trimmedBuf := c.obuf[:c.notSpaceOrgCharPos]
buflen := len(trimmedBuf)
diff := len(c.obuf) - buflen
if diff > 0 {
c.obuf = c.obuf[:buflen]
c.buf = c.bufferedSrc()
}
}
func (c *Context) isEOS() bool {
return len(c.src)-1 <= c.idx
}
func (c *Context) isNextEOS() bool {
return len(c.src) <= c.idx+1
}
func (c *Context) next() bool {
return c.idx < c.size
}
func (c *Context) source(s, e int) string {
return string(c.src[s:e])
}
func (c *Context) previousChar() rune {
if c.idx > 0 {
return c.src[c.idx-1]
}
return rune(0)
}
func (c *Context) currentChar() rune {
if c.size > c.idx {
return c.src[c.idx]
}
return rune(0)
}
func (c *Context) nextChar() rune {
if c.size > c.idx+1 {
return c.src[c.idx+1]
}
return rune(0)
}
func (c *Context) repeatNum(r rune) int {
cnt := 0
for i := c.idx; i < c.size; i++ {
if c.src[i] == r {
cnt++
} else {
break
}
}
return cnt
}
func (c *Context) progress(num int) {
c.idx += num
}
func (c *Context) existsBuffer() bool {
return len(c.bufferedSrc()) != 0
}
func (c *Context) isMultiLine() bool {
return c.mstate != nil
}
func (c *Context) bufferedSrc() []rune {
src := c.buf[:c.notSpaceCharPos]
if c.isMultiLine() {
mstate := c.getMultiLineState()
// remove end '\n' character and trailing empty lines.
// https://yaml.org/spec/1.2.2/#8112-block-chomping-indicator
if mstate.hasTrimAllEndNewlineOpt() {
// If the '-' flag is specified, all trailing newline characters will be removed.
src = []rune(strings.TrimRight(string(src), "\n"))
} else if !mstate.hasKeepAllEndNewlineOpt() {
// Normally, all but one of the trailing newline characters are removed.
var newLineCharCount int
for i := len(src) - 1; i >= 0; i-- {
if src[i] == '\n' {
newLineCharCount++
continue
}
break
}
removedNewLineCharCount := newLineCharCount - 1
for removedNewLineCharCount > 0 {
src = []rune(strings.TrimSuffix(string(src), "\n"))
removedNewLineCharCount--
}
}
// If the text ends with a space character, remove all of them.
if mstate.hasTrimAllEndNewlineOpt() {
src = []rune(strings.TrimRight(string(src), " "))
}
if string(src) == "\n" {
// If the content consists only of a newline,
// it can be considered as the document ending without any specified value,
// so it is treated as an empty string.
src = []rune{}
}
if mstate.hasKeepAllEndNewlineOpt() && len(src) == 0 {
src = []rune{'\n'}
}
}
return src
}
func (c *Context) bufferedToken(pos *token.Position) *token.Token {
if c.idx == 0 {
return nil
}
source := c.bufferedSrc()
if len(source) == 0 {
c.buf = c.buf[:0] // clear value's buffer only.
return nil
}
var tk *token.Token
if c.isMultiLine() {
tk = token.String(string(source), string(c.obuf), pos)
} else {
tk = token.New(string(source), string(c.obuf), pos)
}
c.setTokenTypeByPrevTag(tk)
c.resetBuffer()
return tk
}
func (c *Context) setTokenTypeByPrevTag(tk *token.Token) {
lastTk := c.lastToken()
if lastTk == nil {
return
}
if lastTk.Type != token.TagType {
return
}
tag := token.ReservedTagKeyword(lastTk.Value)
if _, exists := token.ReservedTagKeywordMap[tag]; !exists {
tk.Type = token.StringType
}
}
func (c *Context) lastToken() *token.Token {
if len(c.tokens) != 0 {
return c.tokens[len(c.tokens)-1]
}
return nil
}
|