From 0142d93b52350b6539bd7907b339bdc37d78e693 Mon Sep 17 00:00:00 2001 From: Dmitry Stoletov Date: Mon, 25 Jan 2021 19:24:28 +0300 Subject: [PATCH] add type unknow for prefixexp Signed-off-by: Dmitry Stoletov --- formatter/body.go | 2 +- formatter/parser.go | 19 +++++++++++++++++-- formatter/prefixexp.go | 9 ++++----- formatter/prefixexp_test.go | 3 +++ formatter/statement_assigment_test.go | 3 +++ formatter/statement_for_test.go | 3 +++ formatter/statement_funccall_test.go | 2 ++ formatter/statement_if_test.go | 8 ++++++++ formatter/statement_table_test.go | 4 ++++ 9 files changed, 45 insertions(+), 8 deletions(-) diff --git a/formatter/body.go b/formatter/body.go index 3d82651..4bc2cc0 100644 --- a/formatter/body.go +++ b/formatter/body.go @@ -79,7 +79,7 @@ func (b *body) GetStatement(prev, cur *element) statement { switch cur.Token.Type { case nID: - return &prefixexpStatement{IsUnknow: false} + return &prefixexpStatement{IsUnknow: true} case nFor: return &forStatement{} diff --git a/formatter/parser.go b/formatter/parser.go index 524f220..5dc4b5b 100644 --- a/formatter/parser.go +++ b/formatter/parser.go @@ -52,6 +52,10 @@ func parse(code []byte) (*document, error) { // 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 cs == nil { currentBody = doc.Body @@ -102,11 +106,22 @@ func parse(code []byte) (*document, error) { } if currentStatement.TypeOf() == tsPrefixexpStatement && st.TypeOf() == tsFuncCallStatement { - st.AppendStatement(chainSt.ExctractStatement(tsPrefixexpStatement)) + extracted := chainSt.ExctractStatement(tsUnknow) + if extracted == nil { + extracted = chainSt.ExctractStatement(tsPrefixexpStatement) + } + + st.AppendStatement(extracted) + chainSt.Prev().AppendStatement(st) + } else if currentStatement.TypeOf() == tsUnknow && st.TypeOf() == tsFuncCallStatement { + st.AppendStatement(chainSt.ExctractStatement(tsUnknow)) chainSt.Prev().AppendStatement(st) } else if st.TypeOf() == tsAssignment { isPrefixexpConvertAssignment = true - currentStatement = chainSt.ExctractStatement(tsPrefixexpStatement) + currentStatement = chainSt.ExctractStatement(tsUnknow) + if currentStatement == nil { + currentStatement = chainSt.ExctractStatement(tsPrefixexpStatement) + } chainSt.Prev().AppendStatement(st) } else { currentStatement.AppendStatement(st) diff --git a/formatter/prefixexp.go b/formatter/prefixexp.go index b81e697..7dee93d 100644 --- a/formatter/prefixexp.go +++ b/formatter/prefixexp.go @@ -56,9 +56,9 @@ func (s *prefixexpStatement) InnerStatement(prev, cur *element) (bool, statement } func (s *prefixexpStatement) TypeOf() typeStatement { - // if s.IsUnknow { - // return tsUnknow - // } + if s.IsUnknow { + return tsUnknow + } return tsPrefixexpStatement } @@ -99,8 +99,7 @@ func (s *prefixexpStatement) IsEnd(prev, cur *element) (bool, bool) { } if cur.Token.Type == nComma { // assignment statement - // return false, true /////////// - return false, false + return false, true } if cur.Token.Type == nDot { // prefixexpStatement diff --git a/formatter/prefixexp_test.go b/formatter/prefixexp_test.go index c16d4ec..5369e6e 100644 --- a/formatter/prefixexp_test.go +++ b/formatter/prefixexp_test.go @@ -46,6 +46,7 @@ func TestParsePrefixexp(t *testing.T) { }, 1: &funcCallStatement{ Prefixexp: &prefixexpStatement{ + IsUnknow: true, Element: &element{ Token: &lexmachine.Token{ Type: nID, @@ -376,6 +377,7 @@ r["ff"]["dd"].name.name2["ee"]() Blocks: map[uint64]statement{ 0: &funcCallStatement{ Prefixexp: &prefixexpStatement{ + IsUnknow: true, Element: &element{ Token: &lexmachine.Token{ Type: nID, @@ -494,6 +496,7 @@ a["bb"]["cc"].dd.ee["ff"], g["hh"] = 1, 2 List: []*exp{ { Prefixexp: &prefixexpStatement{ + IsUnknow: true, Element: &element{ Token: &lexmachine.Token{ Type: nID, diff --git a/formatter/statement_assigment_test.go b/formatter/statement_assigment_test.go index 58901e5..c19f37e 100644 --- a/formatter/statement_assigment_test.go +++ b/formatter/statement_assigment_test.go @@ -168,6 +168,7 @@ myvar = function() end List: []*exp{ { Prefixexp: &prefixexpStatement{ + IsUnknow: true, Element: &element{ Token: &lexmachine.Token{ Type: nID, @@ -226,6 +227,7 @@ myvar = 1 List: []*exp{ { Prefixexp: &prefixexpStatement{ + IsUnknow: true, Element: &element{ Token: &lexmachine.Token{ Type: nID, @@ -286,6 +288,7 @@ a, b = 1, 2 List: []*exp{ { Prefixexp: &prefixexpStatement{ + IsUnknow: true, Element: &element{ Token: &lexmachine.Token{ Type: nID, diff --git a/formatter/statement_for_test.go b/formatter/statement_for_test.go index a390c82..b0d55d1 100644 --- a/formatter/statement_for_test.go +++ b/formatter/statement_for_test.go @@ -208,6 +208,7 @@ end Blocks: map[uint64]statement{ 0: &funcCallStatement{ Prefixexp: &prefixexpStatement{ + IsUnknow: true, Element: &element{ Token: &lexmachine.Token{ Type: nID, @@ -360,6 +361,7 @@ for i=10,1,-1 do print(i) end Blocks: map[uint64]statement{ 0: &funcCallStatement{ Prefixexp: &prefixexpStatement{ + IsUnknow: true, Element: &element{ Token: &lexmachine.Token{ Type: nID, @@ -504,6 +506,7 @@ for i, pkg in ipairs(packages) do print(i) end Blocks: map[uint64]statement{ 0: &funcCallStatement{ Prefixexp: &prefixexpStatement{ + IsUnknow: true, Element: &element{ Token: &lexmachine.Token{ Type: nID, diff --git a/formatter/statement_funccall_test.go b/formatter/statement_funccall_test.go index ba31ccd..c9d3919 100644 --- a/formatter/statement_funccall_test.go +++ b/formatter/statement_funccall_test.go @@ -44,6 +44,7 @@ funccall() EndColumn: 8, }, }, + IsUnknow: true, }, Explist: &explist{ List: []*exp{ @@ -83,6 +84,7 @@ funccall "literal" EndColumn: 8, }, }, + IsUnknow: true, }, Explist: &explist{ List: []*exp{ diff --git a/formatter/statement_if_test.go b/formatter/statement_if_test.go index 20d91ec..95c8bdb 100644 --- a/formatter/statement_if_test.go +++ b/formatter/statement_if_test.go @@ -174,6 +174,7 @@ print(c) List: []*exp{ { Prefixexp: &prefixexpStatement{ + IsUnknow: true, Element: &element{ Token: &lexmachine.Token{ Type: nID, @@ -283,6 +284,7 @@ print(c) List: []*exp{ { Prefixexp: &prefixexpStatement{ + IsUnknow: true, Element: &element{ Token: &lexmachine.Token{ Type: nID, @@ -392,6 +394,7 @@ print(c) List: []*exp{ { Prefixexp: &prefixexpStatement{ + IsUnknow: true, Element: &element{ Token: &lexmachine.Token{ Type: nID, @@ -462,6 +465,7 @@ print(c) List: []*exp{ { Prefixexp: &prefixexpStatement{ + IsUnknow: true, Element: &element{ Token: &lexmachine.Token{ Type: nID, @@ -528,6 +532,7 @@ print(c) List: []*exp{ { Prefixexp: &prefixexpStatement{ + IsUnknow: true, Element: &element{ Token: &lexmachine.Token{ Type: nID, @@ -577,6 +582,7 @@ print(c) }, 4: &funcCallStatement{ Prefixexp: &prefixexpStatement{ + IsUnknow: true, Element: &element{ Token: &lexmachine.Token{ Type: nID, @@ -746,6 +752,7 @@ end Blocks: map[uint64]statement{ 0: &funcCallStatement{ Prefixexp: &prefixexpStatement{ + IsUnknow: true, Element: &element{ Token: &lexmachine.Token{ Type: nID, @@ -780,6 +787,7 @@ end }, 1: &funcCallStatement{ Prefixexp: &prefixexpStatement{ + IsUnknow: true, Element: &element{ Token: &lexmachine.Token{ Type: nID, diff --git a/formatter/statement_table_test.go b/formatter/statement_table_test.go index 5b3648a..c98c620 100644 --- a/formatter/statement_table_test.go +++ b/formatter/statement_table_test.go @@ -39,6 +39,7 @@ table = { List: []*exp{ { Prefixexp: &prefixexpStatement{ + IsUnknow: true, Element: &element{ Token: &lexmachine.Token{ Type: nID, @@ -190,6 +191,7 @@ table = { List: []*exp{ { Prefixexp: &prefixexpStatement{ + IsUnknow: true, Element: &element{ Token: &lexmachine.Token{ Type: nID, @@ -371,6 +373,7 @@ table = { -- 1 List: []*exp{ { Prefixexp: &prefixexpStatement{ + IsUnknow: true, Element: &element{ Token: &lexmachine.Token{ Type: nID, @@ -570,6 +573,7 @@ table = { -- 0.1 List: []*exp{ { Prefixexp: &prefixexpStatement{ + IsUnknow: true, Element: &element{ Token: &lexmachine.Token{ Type: nID,