Skip to content

Commit 558921a

Browse files
authored
Refactor indent state (#481)
* refactor indent state * refactor function position * remove unnecessary function
1 parent 56f8b29 commit 558921a

File tree

3 files changed

+35
-44
lines changed

3 files changed

+35
-44
lines changed

lexer/lexer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestTokenize(t *testing.T) {
2222
CharacterType: token.CharacterTypeMiscellaneous,
2323
Indicator: token.NotIndicator,
2424
Value: "null",
25-
Origin: "null\n",
25+
Origin: "null\n\t\t",
2626
},
2727
},
2828
},
@@ -828,7 +828,7 @@ func TestTokenize(t *testing.T) {
828828
CharacterType: token.CharacterTypeMiscellaneous,
829829
Indicator: token.NotIndicator,
830830
Value: "123",
831-
Origin: "123\n",
831+
Origin: "123\n\t\t",
832832
},
833833
},
834834
},

parser/parser_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,20 @@ a:
439439
piyo
440440
`,
441441
},
442+
{
443+
`
444+
v: |
445+
a
446+
b
447+
c`,
448+
`
449+
v: |
450+
a
451+
b
452+
c
453+
`,
454+
},
455+
442456
{
443457
`
444458
a: |

scanner/scanner.go

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -145,44 +145,31 @@ func (s *Scanner) updateIndentLevel() {
145145
}
146146
}
147147

148-
func (s *Scanner) indentStateFromIndentNumDifference() IndentState {
149-
switch {
150-
case s.prevLineIndentNum < s.indentNum:
151-
return IndentStateUp
152-
case s.prevLineIndentNum == s.indentNum:
153-
return IndentStateEqual
154-
default:
155-
return IndentStateDown
156-
}
157-
}
158-
159148
func (s *Scanner) updateIndentState(ctx *Context) {
160-
s.updateIndentLevel()
161-
162149
if s.lastDelimColumn > 0 {
163150
if s.lastDelimColumn < s.column {
164151
s.indentState = IndentStateUp
165-
} else if s.lastDelimColumn != s.column || s.prevLineIndentNum != s.indentNum {
166-
// The following case ( current position is 'd' ), some variables becomes like here
167-
// - lastDelimColumn: 1 of 'a'
168-
// - indentNumBasedIndentState: IndentStateDown because d's indentNum(1) is less than c's indentNum(3).
169-
// Therefore, s.lastDelimColumn(1) == s.column(1) is true, but we want to treat this as IndentStateDown.
170-
// So, we look also current indentState value by the above prevLineIndentNum based logic, and determines finally indentState.
171-
// ---
172-
// a:
173-
// b
174-
// c
175-
// d: e
176-
// ^
177-
s.indentState = IndentStateDown
178152
} else {
179-
s.indentState = IndentStateEqual
153+
// If lastDelimColumn and s.column are the same,
154+
// treat as Down state since it is the same column as delimiter.
155+
s.indentState = IndentStateDown
180156
}
181157
} else {
182158
s.indentState = s.indentStateFromIndentNumDifference()
183159
}
184160
}
185161

162+
func (s *Scanner) indentStateFromIndentNumDifference() IndentState {
163+
switch {
164+
case s.prevLineIndentNum < s.indentNum:
165+
return IndentStateUp
166+
case s.prevLineIndentNum == s.indentNum:
167+
return IndentStateEqual
168+
default:
169+
return IndentStateDown
170+
}
171+
}
172+
186173
func (s *Scanner) updateIndent(ctx *Context, c rune) {
187174
if s.isFirstCharAtLine && s.isNewLineChar(c) && ctx.isDocument() {
188175
return
@@ -195,6 +182,7 @@ func (s *Scanner) updateIndent(ctx *Context, c rune) {
195182
s.indentState = IndentStateKeep
196183
return
197184
}
185+
s.updateIndentLevel()
198186
s.updateIndentState(ctx)
199187
s.isFirstCharAtLine = false
200188
}
@@ -207,10 +195,6 @@ func (s *Scanner) isChangedToIndentStateUp() bool {
207195
return s.indentState == IndentStateUp
208196
}
209197

210-
func (s *Scanner) isChangedToIndentStateEqual() bool {
211-
return s.indentState == IndentStateEqual
212-
}
213-
214198
func (s *Scanner) addBufferedTokenIfExists(ctx *Context) {
215199
ctx.addToken(s.bufferedToken(ctx))
216200
}
@@ -634,23 +618,16 @@ func (s *Scanner) scan(ctx *Context) (pos int) {
634618
pos = ctx.nextPos()
635619
c := ctx.currentChar()
636620
s.updateIndent(ctx, c)
621+
if s.isChangedToIndentStateDown() {
622+
s.addBufferedTokenIfExists(ctx)
623+
}
637624
if ctx.isDocument() {
638-
if (s.indentNum == 0 && s.isChangedToIndentStateEqual()) ||
639-
s.isChangedToIndentStateDown() {
640-
s.addBufferedTokenIfExists(ctx)
625+
if s.isChangedToIndentStateDown() {
641626
s.breakLiteral(ctx)
642627
} else {
643628
s.scanLiteral(ctx, c)
644629
continue
645630
}
646-
} else if s.isChangedToIndentStateDown() {
647-
s.addBufferedTokenIfExists(ctx)
648-
} else if s.isChangedToIndentStateEqual() {
649-
// if first character is new line character, buffer expect to raw folded literal
650-
if len(ctx.obuf) > 0 && s.newLineCount(ctx.obuf) <= 1 {
651-
// doesn't raw folded literal
652-
s.addBufferedTokenIfExists(ctx)
653-
}
654631
}
655632
switch c {
656633
case '{':

0 commit comments

Comments
 (0)