Skip to content

Commit

Permalink
refactor variable name
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy committed Oct 28, 2024
1 parent 313328c commit 9e880ea
Showing 1 changed file with 49 additions and 32 deletions.
81 changes: 49 additions & 32 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,24 @@ const (
// Scanner holds the scanner's internal state while processing a given text.
// It can be allocated as part of another data structure but must be initialized via Init before use.
type Scanner struct {
source []rune
sourcePos int
sourceSize int
line int
column int
offset int
prevIndentNum int
prevIndentColumn int
docStartColumn int
source []rune
sourcePos int
sourceSize int
// line number. This number starts from 1.
line int
// column number. This number starts from 1.
column int
// offset represents the offset from the beginning of the source.
offset int
// lastDelimColumn is the last column needed to compare indent is retained.
lastDelimColumn int
// indentNum indicates the number of spaces used for indentation.
indentNum int
// prevLineIndentNum indicates the number of spaces used for indentation at previous line.
prevLineIndentNum int
// indentLevel indicates the level of indent depth. This value does not match the column value.
indentLevel int
indentNum int
docStartColumn int
isFirstCharAtLine bool
isAnchor bool
startedFlowSequenceNum int
Expand Down Expand Up @@ -90,7 +97,7 @@ func (s *Scanner) progressColumn(ctx *Context, num int) {
}

func (s *Scanner) progressLine(ctx *Context) {
s.prevIndentNum = s.indentNum
s.prevLineIndentNum = s.indentNum
s.column = 1
s.line++
s.offset++
Expand Down Expand Up @@ -128,29 +135,39 @@ func (s *Scanner) newLineCount(src []rune) int {
return cnt
}

func (s *Scanner) updateIndentState(ctx *Context) {
var indentNumBasedIndentState IndentState
if s.prevIndentNum < s.indentNum {
func (s *Scanner) updateIndentLevel() {
if s.prevLineIndentNum < s.indentNum {
s.indentLevel++
indentNumBasedIndentState = IndentStateUp
} else if s.prevIndentNum == s.indentNum {
indentNumBasedIndentState = IndentStateEqual
} else {
indentNumBasedIndentState = IndentStateDown
} else if s.prevLineIndentNum > s.indentNum {
if s.indentLevel > 0 {
s.indentLevel--
}
}
}

func (s *Scanner) indentStateFromIndentNumDifference() IndentState {
switch {
case s.prevLineIndentNum < s.indentNum:
return IndentStateUp
case s.prevLineIndentNum == s.indentNum:
return IndentStateEqual
default:
return IndentStateDown
}
}

func (s *Scanner) updateIndentState(ctx *Context) {
s.updateIndentLevel()

if s.prevIndentColumn > 0 {
if s.prevIndentColumn < s.column {
if s.lastDelimColumn > 0 {
if s.lastDelimColumn < s.column {
s.indentState = IndentStateUp
} else if s.prevIndentColumn != s.column || s.prevIndentNum != s.indentNum {
} else if s.lastDelimColumn != s.column || s.prevLineIndentNum != s.indentNum {
// The following case ( current position is 'd' ), some variables becomes like here
// - prevIndentColumn: 1 of 'a'
// - lastDelimColumn: 1 of 'a'
// - indentNumBasedIndentState: IndentStateDown because d's indentNum(1) is less than c's indentNum(3).
// Therefore, s.prevIndentColumn(1) == s.column(1) is true, but we want to treat this as IndentStateDown.
// So, we look also current indentState value by the above prevIndentNum based logic, and determines finally indentState.
// Therefore, s.lastDelimColumn(1) == s.column(1) is true, but we want to treat this as IndentStateDown.
// So, we look also current indentState value by the above prevLineIndentNum based logic, and determines finally indentState.
// ---
// a:
// b
Expand All @@ -162,7 +179,7 @@ func (s *Scanner) updateIndentState(ctx *Context) {
s.indentState = IndentStateEqual
}
} else {
s.indentState = indentNumBasedIndentState
s.indentState = s.indentStateFromIndentNumDifference()
}
}

Expand Down Expand Up @@ -662,7 +679,7 @@ func (s *Scanner) scan(ctx *Context) (pos int) {
}
case '<':
if s.isMergeKey(ctx) {
s.prevIndentColumn = s.column
s.lastDelimColumn = s.column
ctx.addToken(token.MergeKey(string(ctx.obuf)+"<<", s.pos()))
s.progressColumn(ctx, 1)
pos++
Expand Down Expand Up @@ -696,7 +713,7 @@ func (s *Scanner) scan(ctx *Context) (pos int) {
s.addBufferedTokenIfExists(ctx)
ctx.addOriginBuf(c)
tk := token.SequenceEntry(string(ctx.obuf), s.pos())
s.prevIndentColumn = tk.Position.Column
s.lastDelimColumn = tk.Position.Column
ctx.addToken(tk)
s.progressColumn(ctx, 1)
return
Expand Down Expand Up @@ -732,13 +749,13 @@ func (s *Scanner) scan(ctx *Context) (pos int) {
// mapping value
tk := s.bufferedToken(ctx)
if tk != nil {
s.prevIndentColumn = tk.Position.Column
s.lastDelimColumn = tk.Position.Column
ctx.addToken(tk)
} else if tk := ctx.lastToken(); tk != nil {
// If the map key is quote, the buffer does not exist because it has already been cut into tokens.
// Therefore, we need to check the last token.
if tk.Indicator == token.QuotedScalarIndicator {
s.prevIndentColumn = tk.Position.Column
s.lastDelimColumn = tk.Position.Column
}
}
ctx.addToken(token.MappingValue(s.pos()))
Expand Down Expand Up @@ -856,8 +873,8 @@ func (s *Scanner) Init(text string) {
s.line = 1
s.column = 1
s.offset = 1
s.prevIndentNum = 0
s.prevIndentColumn = 0
s.prevLineIndentNum = 0
s.lastDelimColumn = 0
s.indentLevel = 0
s.indentNum = 0
s.isFirstCharAtLine = true
Expand Down

0 comments on commit 9e880ea

Please sign in to comment.