Skip to content

Commit

Permalink
syntax: make StmtList a Node
Browse files Browse the repository at this point in the history
It's not particularly useful yet, but it will be useful very soon to
clean up IfClause.

We also did have Pos and End methods for this type, though we hadn't
exported them. Removing the type or the methods would be a net loss of
simplicity in the syntax tree type definitions, so properly expose the
type and methods.

It's the first node whose Pos and End methods don't take pointer
receivers. That's fine, as it is often used as an anonymous field.
  • Loading branch information
mvdan committed Dec 9, 2018
1 parent fdb7c61 commit 230fc13
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
13 changes: 5 additions & 8 deletions syntax/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Node interface {
type File struct {
Name string

StmtList
StmtList // exposing its Pos and End methods
}

// StmtList is a list of statements with any number of trailing comments. Both
Expand All @@ -33,7 +33,7 @@ type StmtList struct {
Last []Comment
}

func (s StmtList) pos() Pos {
func (s StmtList) Pos() Pos {
if len(s.Stmts) > 0 {
s := s.Stmts[0]
sPos := s.Pos()
Expand All @@ -50,7 +50,7 @@ func (s StmtList) pos() Pos {
return Pos{}
}

func (s StmtList) end() Pos {
func (s StmtList) End() Pos {
if len(s.Last) > 0 {
return s.Last[len(s.Last)-1].End()
}
Expand Down Expand Up @@ -100,9 +100,6 @@ func (p Pos) IsValid() bool { return p.line > 0 }
// version of p.Offset() > p2.Offset().
func (p Pos) After(p2 Pos) bool { return p.offs > p2.offs }

func (f *File) Pos() Pos { return f.StmtList.pos() }
func (f *File) End() Pos { return f.StmtList.end() }

func posAddCol(p Pos, n int) Pos {
p.col += uint16(n)
p.offs += uint32(n)
Expand Down Expand Up @@ -288,7 +285,7 @@ func (s *Subshell) Pos() Pos { return s.Lparen }
func (s *Subshell) End() Pos { return posAddCol(s.Rparen, 1) }

// Block represents a series of commands that should be executed in a nested
// scope.
// scope. It is essentially a StmtList within curly braces.
type Block struct {
Lbrace, Rbrace Pos
StmtList
Expand Down Expand Up @@ -690,7 +687,7 @@ func (c *CaseItem) End() Pos {
if c.OpPos.IsValid() {
return posAddCol(c.OpPos, len(c.Op.String()))
}
return c.StmtList.end()
return c.StmtList.End()
}

// TestClause represents a Bash extended test clause.
Expand Down
8 changes: 4 additions & 4 deletions syntax/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ func (p *Printer) command(cmd Command, redirs []*Redirect) (startRedirs int) {
case *Subshell:
p.WriteByte('(')
p.wantSpace = len(x.Stmts) > 0 && startsWithLparen(x.Stmts[0])
p.spacePad(x.StmtList.pos())
p.spacePad(x.StmtList.Pos())
p.nestedStmts(x.StmtList, x.Rparen)
p.wantSpace = false
p.spacePad(x.Rparen)
Expand Down Expand Up @@ -985,8 +985,8 @@ func (p *Printer) command(cmd Command, redirs []*Redirect) (startRedirs int) {
p.casePatternJoin(ci.Patterns)
p.WriteByte(')')
p.wantSpace = !p.minify
sep := len(ci.Stmts) > 1 || ci.StmtList.pos().Line() > p.line ||
(!ci.StmtList.empty() && ci.OpPos.Line() > ci.StmtList.end().Line())
sep := len(ci.Stmts) > 1 || ci.StmtList.Pos().Line() > p.line ||
(!ci.StmtList.empty() && ci.OpPos.Line() > ci.StmtList.End().Line())
p.nestedStmts(ci.StmtList, ci.OpPos)
p.level++
if !p.minify || i != len(x.Items)-1 {
Expand Down Expand Up @@ -1262,7 +1262,7 @@ func (p *Printer) nestedStmts(sl StmtList, closing Pos) {
// { stmt; stmt; }
p.wantNewline = true
case closing.Line() > p.line && len(sl.Stmts) > 0 &&
sl.end().Line() < closing.Line():
sl.End().Line() < closing.Line():
// Force a newline if we find:
// { stmt
// }
Expand Down

0 comments on commit 230fc13

Please sign in to comment.