Skip to content

Commit

Permalink
Merge pull request #55 from GuanceCloud/slice-expr
Browse files Browse the repository at this point in the history
Adjust `SliceExpr` struct
  • Loading branch information
vircoys authored Dec 4, 2024
2 parents f84a9b0 + 9bad7d7 commit 42809b6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 53 deletions.
4 changes: 2 additions & 2 deletions pkg/ast/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ type SliceExpr struct {
Start *Node
End *Node
Step *Node
Colon2 *Node
Colon2 bool
LBracket token.LnColPos
RBracket token.LnColPos
}
Expand All @@ -341,7 +341,7 @@ func (e *SliceExpr) String() string {
stepStr := ""
if e.Step != nil {
stepStr = ":" + e.Step.String()
} else if e.Colon2 != nil {
} else if e.Colon2 {
stepStr = ":"
}
return fmt.Sprintf("%s[%s:%s%s]", e.Obj.String(), startStr, endStr, stepStr)
Expand Down
48 changes: 24 additions & 24 deletions pkg/parser/gram.y
Original file line number Diff line number Diff line change
Expand Up @@ -546,99 +546,99 @@ slice_expr_start

slice_expr: identifier LEFT_BRACKET SPACE_EOLS expr COLON SPACE_EOLS expr COLON SPACE_EOLS expr RIGHT_BRACKET //a[1:3:2]
{
$$ = yylex.(*parser).newSliceExpr($1, $4, $7, $10, $1, $2, $11)
$$ = yylex.(*parser).newSliceExpr($1, $4, $7, $10, true, $2, $11)
}
| identifier LEFT_BRACKET SPACE_EOLS expr COLON SPACE_EOLS expr COLON SPACE_EOLS RIGHT_BRACKET //a[1:3:]
{
$$ = yylex.(*parser).newSliceExpr($1, $4, $7, nil, $1, $2, $10)
$$ = yylex.(*parser).newSliceExpr($1, $4, $7, nil, true, $2, $10)
}
| identifier LEFT_BRACKET SPACE_EOLS expr COLON SPACE_EOLS COLON SPACE_EOLS expr RIGHT_BRACKET //a[1::2]
{
$$ = yylex.(*parser).newSliceExpr($1, $4, nil, $9, $1, $2, $10)
$$ = yylex.(*parser).newSliceExpr($1, $4, nil, $9, true, $2, $10)
}
| identifier LEFT_BRACKET SPACE_EOLS expr COLON SPACE_EOLS COLON SPACE_EOLS RIGHT_BRACKET //a[1::]
{
$$ = yylex.(*parser).newSliceExpr($1, $4, nil, nil, $1, $2, $9)
$$ = yylex.(*parser).newSliceExpr($1, $4, nil, nil, true, $2, $9)
}
| identifier LEFT_BRACKET SPACE_EOLS COLON SPACE_EOLS expr COLON SPACE_EOLS expr RIGHT_BRACKET //a[:3:2]
{
$$ = yylex.(*parser).newSliceExpr($1, nil, $6, $9, $1, $2, $10)
$$ = yylex.(*parser).newSliceExpr($1, nil, $6, $9, true, $2, $10)
}
| identifier LEFT_BRACKET SPACE_EOLS COLON SPACE_EOLS expr COLON SPACE_EOLS RIGHT_BRACKET //a[:3:]
{
$$ = yylex.(*parser).newSliceExpr($1, nil, $6, nil, $1, $2, $9)
$$ = yylex.(*parser).newSliceExpr($1, nil, $6, nil, true, $2, $9)
}
| identifier LEFT_BRACKET SPACE_EOLS COLON SPACE_EOLS COLON SPACE_EOLS expr RIGHT_BRACKET //a[::2]
{
$$ = yylex.(*parser).newSliceExpr($1, nil, nil, $8, $1, $2, $9)
$$ = yylex.(*parser).newSliceExpr($1, nil, nil, $8, true, $2, $9)
}
| identifier LEFT_BRACKET SPACE_EOLS COLON SPACE_EOLS COLON SPACE_EOLS RIGHT_BRACKET //a[::]
{
$$ = yylex.(*parser).newSliceExpr($1, nil, nil, nil, $1, $2, $8)
$$ = yylex.(*parser).newSliceExpr($1, nil, nil, nil, true, $2, $8)
}
| identifier LEFT_BRACKET SPACE_EOLS expr COLON SPACE_EOLS expr RIGHT_BRACKET //a[1:3]
{
$$ = yylex.(*parser).newSliceExpr($1, $4, $7, nil, nil, $2, $8)
$$ = yylex.(*parser).newSliceExpr($1, $4, $7, nil, false, $2, $8)
}
| identifier LEFT_BRACKET SPACE_EOLS COLON SPACE_EOLS expr RIGHT_BRACKET //a[:3]
{
$$ = yylex.(*parser).newSliceExpr($1, nil, $6, nil, nil, $2, $7)
$$ = yylex.(*parser).newSliceExpr($1, nil, $6, nil, false, $2, $7)
}
| identifier LEFT_BRACKET SPACE_EOLS expr COLON SPACE_EOLS RIGHT_BRACKET //a[1:]
{
$$ = yylex.(*parser).newSliceExpr($1, $4, nil, nil, nil, $2, $7)
$$ = yylex.(*parser).newSliceExpr($1, $4, nil, nil, false, $2, $7)
}
| identifier LEFT_BRACKET SPACE_EOLS COLON SPACE_EOLS RIGHT_BRACKET //a[:]
{
$$ = yylex.(*parser).newSliceExpr($1, nil, nil, nil, nil, $2, $6)
$$ = yylex.(*parser).newSliceExpr($1, nil, nil, nil, false, $2, $6)
}
| slice_expr_start LEFT_BRACKET SPACE_EOLS expr COLON SPACE_EOLS expr COLON SPACE_EOLS expr RIGHT_BRACKET
{
$$ = yylex.(*parser).newSliceExpr($1, $4, $7, $10, $1, $2, $11)
$$ = yylex.(*parser).newSliceExpr($1, $4, $7, $10, true, $2, $11)
}
| slice_expr_start LEFT_BRACKET SPACE_EOLS expr COLON SPACE_EOLS expr COLON SPACE_EOLS RIGHT_BRACKET
{
$$ = yylex.(*parser).newSliceExpr($1, $4, $7, nil, $1, $2, $10)
$$ = yylex.(*parser).newSliceExpr($1, $4, $7, nil, true, $2, $10)
}
| slice_expr_start LEFT_BRACKET SPACE_EOLS expr COLON SPACE_EOLS COLON SPACE_EOLS expr RIGHT_BRACKET
{
$$ = yylex.(*parser).newSliceExpr($1, $4, nil, $9, $1, $2, $10)
$$ = yylex.(*parser).newSliceExpr($1, $4, nil, $9, true, $2, $10)
}
| slice_expr_start LEFT_BRACKET SPACE_EOLS expr COLON SPACE_EOLS COLON SPACE_EOLS RIGHT_BRACKET
{
$$ = yylex.(*parser).newSliceExpr($1, $4, nil, nil, $1, $2, $9)
$$ = yylex.(*parser).newSliceExpr($1, $4, nil, nil, true, $2, $9)
}
| slice_expr_start LEFT_BRACKET SPACE_EOLS COLON SPACE_EOLS expr COLON SPACE_EOLS expr RIGHT_BRACKET
{
$$ = yylex.(*parser).newSliceExpr($1, nil, $6, $9,$1, $2, $10)
$$ = yylex.(*parser).newSliceExpr($1, nil, $6, $9, true, $2, $10)
}
| slice_expr_start LEFT_BRACKET SPACE_EOLS COLON SPACE_EOLS expr COLON SPACE_EOLS RIGHT_BRACKET
{
$$ = yylex.(*parser).newSliceExpr($1, nil, $6, nil, $1, $2, $9)
$$ = yylex.(*parser).newSliceExpr($1, nil, $6, nil, true, $2, $9)
}
| slice_expr_start LEFT_BRACKET SPACE_EOLS COLON SPACE_EOLS COLON SPACE_EOLS expr RIGHT_BRACKET
{
$$ = yylex.(*parser).newSliceExpr($1, nil, nil, $8, $1, $2, $9)
$$ = yylex.(*parser).newSliceExpr($1, nil, nil, $8, true, $2, $9)
}
| slice_expr_start LEFT_BRACKET SPACE_EOLS COLON SPACE_EOLS COLON SPACE_EOLS RIGHT_BRACKET
{
$$ = yylex.(*parser).newSliceExpr($1, nil, nil, nil, $1, $2, $8)
$$ = yylex.(*parser).newSliceExpr($1, nil, nil, nil, true, $2, $8)
}
| slice_expr_start LEFT_BRACKET SPACE_EOLS expr COLON SPACE_EOLS expr RIGHT_BRACKET
{
$$ = yylex.(*parser).newSliceExpr($1, $4, $7, nil, nil, $2, $8)
$$ = yylex.(*parser).newSliceExpr($1, $4, $7, nil, false, $2, $8)
}
| slice_expr_start LEFT_BRACKET SPACE_EOLS COLON SPACE_EOLS expr RIGHT_BRACKET
{
$$ = yylex.(*parser).newSliceExpr($1, nil, $6, nil, nil, $2, $7)
$$ = yylex.(*parser).newSliceExpr($1, nil, $6, nil, false, $2, $7)
}
| slice_expr_start LEFT_BRACKET SPACE_EOLS expr COLON SPACE_EOLS RIGHT_BRACKET
{
$$ = yylex.(*parser).newSliceExpr($1, $4, nil, nil, nil, $2, $7)
$$ = yylex.(*parser).newSliceExpr($1, $4, nil, nil, false, $2, $7)
}
| slice_expr_start LEFT_BRACKET SPACE_EOLS COLON SPACE_EOLS RIGHT_BRACKET
{
$$ = yylex.(*parser).newSliceExpr($1, nil, nil, nil, nil, $2, $6)
$$ = yylex.(*parser).newSliceExpr($1, nil, nil, nil, false, $2, $6)
}
;

Expand Down
48 changes: 24 additions & 24 deletions pkg/parser/gram_y.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ func (p *parser) newCallExpr(fn *ast.Node, args []*ast.Node, lParen, rParen Item

return ast.WrapCallExpr(f)
}
func (p *parser) newSliceExpr(obj *ast.Node, start *ast.Node, end *ast.Node, step *ast.Node, Colon2 *ast.Node, lbracket Item, rbracket Item) *ast.Node {
func (p *parser) newSliceExpr(obj *ast.Node, start *ast.Node, end *ast.Node, step *ast.Node, colon2 bool, lbracket Item, rbracket Item) *ast.Node {
if obj == nil {
p.addParseErrf(p.yyParser.lval.item.PositionRange(), "invalid slice expression: object is nil")
return nil
Expand Down Expand Up @@ -567,7 +567,7 @@ func (p *parser) newSliceExpr(obj *ast.Node, start *ast.Node, end *ast.Node, ste
Start: start,
End: end,
Step: step,
Colon2: Colon2,
Colon2: colon2,
LBracket: p.posCache.LnCol(lbracket.Pos),
RBracket: p.posCache.LnCol(rbracket.Pos),
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1533,7 +1533,7 @@ multiline-string
Start: nil,
End: nil,
Step: nil,
Colon2: ast.WrapIdentifier(&ast.Identifier{Name: "a"}),
Colon2: true,
}),
},
},
Expand Down

0 comments on commit 42809b6

Please sign in to comment.