Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Parse indented newlines correctly #448

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ a: 0 - 1
a: 0 - 1
`,
},
{`
{
`
- a:
b: c
d: e
Expand Down Expand Up @@ -456,7 +457,7 @@ d: eeeeeeeeeeeeeeeee
},
{
`
a: b
a: b
c
`,
`
Expand All @@ -465,7 +466,7 @@ a: b c
},
{
`
a:
a:
b: c
`,
`
Expand All @@ -475,7 +476,7 @@ a:
},
{
`
a: b
a: b
c: d
`,
`
Expand Down Expand Up @@ -625,6 +626,20 @@ b: 1
}
}

func TestIndentedNewLine(t *testing.T) {
ast, err := parser.ParseFile(filepath.Join("testdata", "indented_new_line.yml"), 0)
if err != nil {
t.Fatalf("%+v", err)
}
actual := fmt.Sprintf("%v", ast)
expect := `a: b
c: d
`
if expect != actual {
t.Fatalf("Expected:\n%s\n\nActual:\n%s\n", expect, actual)
}
}

func TestSyntaxError(t *testing.T) {
tests := []struct {
source string
Expand Down Expand Up @@ -999,8 +1014,7 @@ func (c *pathCapturer) Visit(node ast.Node) ast.Visitor {
return c
}

type Visitor struct {
}
type Visitor struct{}

func (v *Visitor) Visit(node ast.Node) ast.Visitor {
tk := node.GetToken()
Expand Down
4 changes: 4 additions & 0 deletions parser/testdata/indented_new_line.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# The line following a:b has 3 spaces
a: b

c: d
23 changes: 15 additions & 8 deletions scanner/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ type Context struct {
literalOpt string
}

var (
ctxPool = sync.Pool{
New: func() interface{} {
return createContext()
},
}
)
var ctxPool = sync.Pool{
New: func() interface{} {
return createContext()
},
}

func createContext() *Context {
return &Context{
Expand Down Expand Up @@ -101,7 +99,7 @@ func (c *Context) addBuf(r rune) {

func (c *Context) addOriginBuf(r rune) {
c.obuf = append(c.obuf, r)
if r != ' ' && r != '\t' {
if r != ' ' && r != '\t' && r != '\n' {
c.notSpaceOrgCharPos = len(c.obuf)
}
}
Expand All @@ -110,6 +108,15 @@ func (c *Context) removeRightSpaceFromBuf() int {
trimmedBuf := c.obuf[:c.notSpaceOrgCharPos]
buflen := len(trimmedBuf)
diff := len(c.obuf) - buflen

// only calculate the space chopped up to the first newline
for i := c.notSpaceOrgCharPos; i < len(c.obuf); i++ {
if c.obuf[i] == '\n' || c.obuf[i] == '\r' {
diff = i - c.notSpaceOrgCharPos
break
}
}

if diff > 0 {
c.obuf = c.obuf[:buflen]
c.buf = c.bufferedSrc()
Expand Down
Loading