Skip to content
Open
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
27 changes: 27 additions & 0 deletions formatter/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package formatter

import (
"errors"
"io"
)

Expand Down Expand Up @@ -70,6 +71,28 @@ func (b *body) AppendStatement(st statement) {
b.Qty++
}

var errStatementNotExist = errors.New("statement not exist")

func (b *body) RemoveStatement(st statement) error {
for i := len(b.Blocks) - 1; i >= 0; i-- {
if b.Blocks[uint64(i)] == st {
b.Blocks[uint64(i)] = nil
return nil
}
}

return errStatementNotExist
}

func bodyRemoveStatement(haystack, needle statement) error {
v, ok := haystack.(*body)
if !ok {
return errCastingType
}

return v.RemoveStatement(needle)
}

func (b *body) GetStatement(prev, cur *element) statement {
if prev != nil && prev.Token.Type == nLocal {
if cur.Token.Type == nID {
Expand Down Expand Up @@ -131,6 +154,10 @@ func (b *body) Format(c *Config, p printer, w io.Writer) error {
for i := 0; i < int(b.Qty); i++ {
st := b.Blocks[uint64(i)]

if st == nil {
continue
}

if !p.IgnoreFirstPad {
_, newlineOk := st.(*newlineStatement)
commentSt, commentOk := st.(*commentStatement)
Expand Down
12 changes: 8 additions & 4 deletions formatter/exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (exp) InnerStatement(prev, cur *element) (bool, statement) {
}

return false, &prefixexpStatement{Enclosed: true}

case nID:
//z = a().b()
return true, &prefixexpStatement{}
}

return false, nil
Expand Down Expand Up @@ -364,10 +368,10 @@ func (s *exp) AppendStatement(st statement) {
s.Prefixexp = &prefixexpStatement{FuncCall: v} // a = func[0].call{}

case *prefixexpStatement:
if s.Element != nil {
v.Element = s.Element
s.Element = nil
}
// if s.Element != nil {
// v.Element = s.Element
// s.Element = nil
// }

s.Prefixexp = v
}
Expand Down
28 changes: 23 additions & 5 deletions formatter/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package formatter

import "fmt"

// Parse code.
func parse(code []byte) (*document, error) {
var (
Expand All @@ -37,6 +39,8 @@ func parse(code []byte) (*document, error) {
currentBody = b
currentStatement = b

logging := true

for s.Next() {
el, err := s.Scan()
if err != nil {
Expand All @@ -45,18 +49,23 @@ func parse(code []byte) (*document, error) {

curElement = &el

// if prevElement != nil {
// fmt.Printf("%s%s %s%s = ", mMagenta, TokenIDs[prevElement.Token.Type], prevElement.Token.Value, defaultStyle)
// }
if logging {
if prevElement != nil {
fmt.Printf("%s%s %s%s = ", mMagenta, TokenIDs[prevElement.Token.Type], prevElement.Token.Value, defaultStyle)
}

// fmt.Printf("%s%s %s%s\n", mMagenta, TokenIDs[el.Token.Type], el.Token.Value, defaultStyle)
fmt.Printf("%s%s %s%s\n", mMagenta, TokenIDs[el.Token.Type], el.Token.Value, defaultStyle)
}

for isBlockEnd, ok := currentStatement.IsEnd(prevElement, curElement); ok; isBlockEnd, ok = currentStatement.IsEnd(prevElement, curElement) {
if currentStatement.TypeOf() == tsUnknow && curElement.Token.Type == nComma {
break
}

cs := chainSt.ExtractPrev()
if logging {
fmt.Printf("-- ex %p %#v\n", cs, cs)
}
if cs == nil {
currentBody = doc.Body
chainSt.Append(currentBody)
Expand All @@ -82,6 +91,9 @@ func parse(code []byte) (*document, error) {
}

if st := currentStatement.GetStatement(prevElement, curElement); st != nil {
if logging {
fmt.Printf("-- st %p %#v\n", st, st)
}
var assignmentWithOneVar statement

isPrefixexpConvertAssignment := false
Expand Down Expand Up @@ -112,6 +124,10 @@ func parse(code []byte) (*document, error) {

st.AppendStatement(extracted)
chainSt.Prev().AppendStatement(st)
} else if currentStatement.TypeOf() == tsFuncCallStatement && st.TypeOf() == tsPrefixexpStatement {
extracted := chainSt.ExctractStatement(tsFuncCallStatement)
bodyRemoveStatement(chainSt.GetLastBody(), extracted)
chainSt.GetLastBody().AppendStatement(st)
} else if currentStatement.TypeOf() == tsUnknow && st.TypeOf() == tsFuncCallStatement {
st.AppendStatement(chainSt.ExctractStatement(tsUnknow))
chainSt.Prev().AppendStatement(st)
Expand All @@ -133,7 +149,9 @@ func parse(code []byte) (*document, error) {
st.AppendStatement(inner)
chainSt.Append(inner)
// }

if logging {
fmt.Printf("-- in %p %#v\n", inner, inner)
}
st = inner

if isBreak {
Expand Down
54 changes: 40 additions & 14 deletions formatter/prefixexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *prefixexpStatement) InnerStatement(prev, cur *element) (bool, statement

if cur.Token.Type == nParentheses {
if prev != nil && prev.Token.Type == nID {
return false, &funcCallStatement{}
return false, &funcCallStatement{} // Element: cur
}

s.Enclosed = true
Expand All @@ -52,6 +52,18 @@ func (s *prefixexpStatement) InnerStatement(prev, cur *element) (bool, statement
return false, &funcCallStatement{}
}

if cur.Token.Type == nDot {
if prev != nil && prev.Token.Type == nClosingParentheses {
return true, &prefixexpStatement{}
}
}

if cur.Token.Type == nColon {
if prev != nil && prev.Token.Type == nClosingParentheses {
return true, &prefixexpStatement{}
}
}

return false, nil
}

Expand Down Expand Up @@ -98,6 +110,10 @@ func (s *prefixexpStatement) IsEnd(prev, cur *element) (bool, bool) {
return false, false // .id
}

if prev != nil && prev.Token.Type == nColon && cur.Token.Type == nID { // function call
return false, false // :id
}

if cur.Token.Type == nComma { // assignment statement
return false, true
}
Expand Down Expand Up @@ -149,7 +165,7 @@ func (s *prefixexpStatement) Append(el *element) {
return
}

if el.Token.Type == nDot {
if el.Token.Type == nDot || el.Token.Type == nColon {
s.FieldAccessor = el

return
Expand All @@ -159,8 +175,16 @@ func (s *prefixexpStatement) Append(el *element) {
return
}

if s.FieldAccessor != nil && s.FieldAccessor.Token.Type == nDot { // .id
s.FieldAccessor = el
if s.FieldAccessor != nil {
if s.FieldAccessor.Token.Type == nDot { // .id
s.Element = s.FieldAccessor
s.FieldAccessor = el
}

if s.FieldAccessor.Token.Type == nColon { // :id
s.Element = s.FieldAccessor
s.FieldAccessor = el
}

return
}
Expand Down Expand Up @@ -197,7 +221,9 @@ func (s *prefixexpStatement) GetStatement(prev, cur *element) statement {
}

if cur.Token.Type == nParentheses || cur.Token.Type == nString {
return &funcCallStatement{}
return &funcCallStatement{
// TODO Prefixexp: s,
}
}

if cur.Token.Type == nCurlyBracket {
Expand Down Expand Up @@ -232,6 +258,12 @@ func (s *prefixexpStatement) Format(c *Config, p printer, w io.Writer) error {
}
}

if st := s.FuncCall; st != nil {
if err := st.Format(c, p, w); err != nil {
return err
}
}

if st := s.Element; st != nil {
if err := st.Format(c, p, w); err != nil {
return err
Expand Down Expand Up @@ -263,9 +295,9 @@ func (s *prefixexpStatement) Format(c *Config, p printer, w io.Writer) error {
}

if st := s.FieldAccessor; st != nil {
if _, err := w.Write([]byte(".")); err != nil {
return err
}
// if _, err := w.Write([]byte(".")); err != nil {
// return err
// }

if err := st.Format(c, p, w); err != nil {
return err
Expand All @@ -278,11 +310,5 @@ func (s *prefixexpStatement) Format(c *Config, p printer, w io.Writer) error {
}
}

if st := s.FuncCall; st != nil {
if err := st.Format(c, p, w); err != nil {
return err
}
}

return nil
}
6 changes: 3 additions & 3 deletions formatter/statement_assigment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ myvar = function() end
List: []*exp{{}},
},
},
Body: &body{
Blocks: make(map[uint64]statement),
},
// Body: &body{
// Blocks: make(map[uint64]statement),
// },
},
},
},
Expand Down
14 changes: 7 additions & 7 deletions formatter/statement_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (s *functionStatement) AppendStatement(st statement) {
}

func (s *functionStatement) GetBody(prevSt statement, cur *element) statement {
if cur.Token.Type != nClosingParentheses {
if s.FuncCall == nil {
return prevSt
}

Expand Down Expand Up @@ -123,13 +123,13 @@ func (s *functionStatement) Format(c *Config, p printer, w io.Writer) error {
if err := st.Format(c, inner, w); err != nil {
return err
}
}

if st.Qty > 0 {
// a = function()
// end
if err := newLine(w); err != nil {
return err
if st.Qty > 0 {
// a = function()
// end
if err := newLine(w); err != nil {
return err
}
}
}

Expand Down
24 changes: 12 additions & 12 deletions formatter/statement_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ end
List: []*exp{{}},
},
},
Body: &body{
Blocks: make(map[uint64]statement),
},
// Body: &body{
// Blocks: make(map[uint64]statement),
// },
},
},
Qty: 1,
Expand Down Expand Up @@ -95,9 +95,9 @@ end
List: []*exp{{}},
},
},
Body: &body{
Blocks: make(map[uint64]statement),
},
// Body: &body{
// Blocks: make(map[uint64]statement),
// },
},
},
Qty: 1,
Expand Down Expand Up @@ -141,9 +141,9 @@ end
List: []*exp{{}},
},
},
Body: &body{
Blocks: make(map[uint64]statement),
},
// Body: &body{
// Blocks: make(map[uint64]statement),
// },
},
1: &newlineStatement{},
2: &functionStatement{
Expand All @@ -166,9 +166,9 @@ end
List: []*exp{{}},
},
},
Body: &body{
Blocks: make(map[uint64]statement),
},
// Body: &body{
// Blocks: make(map[uint64]statement),
// },
},
},
Qty: 3,
Expand Down
10 changes: 8 additions & 2 deletions formatter/statement_funccall.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func (funcCallStatement) TypeOf() typeStatement {
}

func (s *funcCallStatement) IsEnd(prev, cur *element) (bool, bool) {
if cur.Token.Type == nClosingParentheses {
return true, true
if cur.Token.Type == nClosingParentheses || cur.Token.Type == nDot || cur.Token.Type == nColon {
return false, false
}

return false, true
Expand All @@ -56,6 +56,12 @@ func (s *funcCallStatement) GetBody(prevSt statement, cur *element) statement {
}

func (s *funcCallStatement) GetStatement(prev, cur *element) statement {
if cur.Token.Type == nDot || cur.Token.Type == nColon {
return &prefixexpStatement{
FuncCall: s,
}
}

return nil
}

Expand Down
Loading