Skip to content

Commit

Permalink
add type unknow for prefixexp
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Stoletov <[email protected]>
  • Loading branch information
irvis committed Jan 25, 2021
1 parent 52654e8 commit 0142d93
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion formatter/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
19 changes: 17 additions & 2 deletions formatter/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions formatter/prefixexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions formatter/prefixexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestParsePrefixexp(t *testing.T) {
},
1: &funcCallStatement{
Prefixexp: &prefixexpStatement{
IsUnknow: true,
Element: &element{
Token: &lexmachine.Token{
Type: nID,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions formatter/statement_assigment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ myvar = function() end
List: []*exp{
{
Prefixexp: &prefixexpStatement{
IsUnknow: true,
Element: &element{
Token: &lexmachine.Token{
Type: nID,
Expand Down Expand Up @@ -226,6 +227,7 @@ myvar = 1
List: []*exp{
{
Prefixexp: &prefixexpStatement{
IsUnknow: true,
Element: &element{
Token: &lexmachine.Token{
Type: nID,
Expand Down Expand Up @@ -286,6 +288,7 @@ a, b = 1, 2
List: []*exp{
{
Prefixexp: &prefixexpStatement{
IsUnknow: true,
Element: &element{
Token: &lexmachine.Token{
Type: nID,
Expand Down
3 changes: 3 additions & 0 deletions formatter/statement_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ end
Blocks: map[uint64]statement{
0: &funcCallStatement{
Prefixexp: &prefixexpStatement{
IsUnknow: true,
Element: &element{
Token: &lexmachine.Token{
Type: nID,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions formatter/statement_funccall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ funccall()
EndColumn: 8,
},
},
IsUnknow: true,
},
Explist: &explist{
List: []*exp{
Expand Down Expand Up @@ -83,6 +84,7 @@ funccall "literal"
EndColumn: 8,
},
},
IsUnknow: true,
},
Explist: &explist{
List: []*exp{
Expand Down
8 changes: 8 additions & 0 deletions formatter/statement_if_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ print(c)
List: []*exp{
{
Prefixexp: &prefixexpStatement{
IsUnknow: true,
Element: &element{
Token: &lexmachine.Token{
Type: nID,
Expand Down Expand Up @@ -283,6 +284,7 @@ print(c)
List: []*exp{
{
Prefixexp: &prefixexpStatement{
IsUnknow: true,
Element: &element{
Token: &lexmachine.Token{
Type: nID,
Expand Down Expand Up @@ -392,6 +394,7 @@ print(c)
List: []*exp{
{
Prefixexp: &prefixexpStatement{
IsUnknow: true,
Element: &element{
Token: &lexmachine.Token{
Type: nID,
Expand Down Expand Up @@ -462,6 +465,7 @@ print(c)
List: []*exp{
{
Prefixexp: &prefixexpStatement{
IsUnknow: true,
Element: &element{
Token: &lexmachine.Token{
Type: nID,
Expand Down Expand Up @@ -528,6 +532,7 @@ print(c)
List: []*exp{
{
Prefixexp: &prefixexpStatement{
IsUnknow: true,
Element: &element{
Token: &lexmachine.Token{
Type: nID,
Expand Down Expand Up @@ -577,6 +582,7 @@ print(c)
},
4: &funcCallStatement{
Prefixexp: &prefixexpStatement{
IsUnknow: true,
Element: &element{
Token: &lexmachine.Token{
Type: nID,
Expand Down Expand Up @@ -746,6 +752,7 @@ end
Blocks: map[uint64]statement{
0: &funcCallStatement{
Prefixexp: &prefixexpStatement{
IsUnknow: true,
Element: &element{
Token: &lexmachine.Token{
Type: nID,
Expand Down Expand Up @@ -780,6 +787,7 @@ end
},
1: &funcCallStatement{
Prefixexp: &prefixexpStatement{
IsUnknow: true,
Element: &element{
Token: &lexmachine.Token{
Type: nID,
Expand Down
4 changes: 4 additions & 0 deletions formatter/statement_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ table = {
List: []*exp{
{
Prefixexp: &prefixexpStatement{
IsUnknow: true,
Element: &element{
Token: &lexmachine.Token{
Type: nID,
Expand Down Expand Up @@ -190,6 +191,7 @@ table = {
List: []*exp{
{
Prefixexp: &prefixexpStatement{
IsUnknow: true,
Element: &element{
Token: &lexmachine.Token{
Type: nID,
Expand Down Expand Up @@ -371,6 +373,7 @@ table = { -- 1
List: []*exp{
{
Prefixexp: &prefixexpStatement{
IsUnknow: true,
Element: &element{
Token: &lexmachine.Token{
Type: nID,
Expand Down Expand Up @@ -570,6 +573,7 @@ table = { -- 0.1
List: []*exp{
{
Prefixexp: &prefixexpStatement{
IsUnknow: true,
Element: &element{
Token: &lexmachine.Token{
Type: nID,
Expand Down

0 comments on commit 0142d93

Please sign in to comment.