Skip to content

Commit

Permalink
MatrixLit: ast.ElemEllipsis (support row...)
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Apr 7, 2024
1 parent 3eaba8a commit 97f29f8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
20 changes: 20 additions & 0 deletions ast/ast_gop.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,26 @@ func (*MatrixLit) exprNode() {}

// -----------------------------------------------------------------------------

// A ElemEllipsis node represents a matrix row elements.
type ElemEllipsis struct {
Elt Expr // ellipsis element
Ellipsis token.Pos // position of "..."
}

// Pos - position of first character belonging to the node.
func (p *ElemEllipsis) Pos() token.Pos {
return p.Elt.Pos()
}

// End - position of first character immediately after the node.
func (p *ElemEllipsis) End() token.Pos {
return p.Ellipsis + 3
}

func (*ElemEllipsis) exprNode() {}

// -----------------------------------------------------------------------------

// ErrWrapExpr represents `expr!`, `expr?` or `expr?: defaultValue`.
type ErrWrapExpr struct {
X Expr
Expand Down
2 changes: 1 addition & 1 deletion parser/_testdata/matrix2/matrix.gop
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
echo [
1, 2, 3
row...
7, 8, 9
7, 8, 9
]
2 changes: 1 addition & 1 deletion parser/_testdata/matrix2/parser.expect
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ ast.FuncDecl:
ast.BasicLit:
Kind: INT
Value: 3
ast.Ellipsis:
ast.ElemEllipsis:
Elt:
ast.Ident:
Name: row
Expand Down
3 changes: 2 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ func (p *parser) parseSliceOrMatrixLit(lbrack token.Pos, first ast.Expr) ast.Exp
elts = make([]ast.Expr, 0, len(elts))
case token.ELLIPSIS:
n := len(elts)
elts[n-1] = &ast.Ellipsis{Ellipsis: p.pos, Elt: elts[n-1]}
elts[n-1] = &ast.ElemEllipsis{Ellipsis: p.pos, Elt: elts[n-1]}
p.next()
continue
default:
Expand Down Expand Up @@ -2200,6 +2200,7 @@ func (p *parser) checkExpr(x ast.Expr) ast.Expr {
p.error(v.opening, msgTupleNotSupported)
x = &ast.BadExpr{From: v.opening, To: v.closing}
case *ast.EnvExpr:
case *ast.ElemEllipsis:
default:
// all other nodes are not proper expressions
p.errorExpected(x.Pos(), "expression", 3)
Expand Down
4 changes: 4 additions & 0 deletions printer/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,10 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
p.print(x.Name)
}

case *ast.ElemEllipsis:
p.expr(x.Elt)
p.print(token.ELLIPSIS)

default:
log.Fatalf("unreachable %T\n", x)
}
Expand Down

0 comments on commit 97f29f8

Please sign in to comment.