Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser: _testdata/matrix2 (support row...) #1846

Merged
merged 3 commits into from
Apr 7, 2024
Merged
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
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
5 changes: 5 additions & 0 deletions parser/_testdata/matrix2/matrix.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
echo [
1, 2, 3
row...
7, 8, 9
]
47 changes: 47 additions & 0 deletions parser/_testdata/matrix2/parser.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

file matrix.gop
noEntrypoint
ast.FuncDecl:
Name:
ast.Ident:
Name: main
Type:
ast.FuncType:
Params:
ast.FieldList:
Body:
ast.BlockStmt:
List:
ast.ExprStmt:
X:
ast.CallExpr:
Fun:
ast.Ident:
Name: echo
Args:
ast.MatrixLit:
Elts:
ast.BasicLit:
Kind: INT
Value: 1
ast.BasicLit:
Kind: INT
Value: 2
ast.BasicLit:
Kind: INT
Value: 3
ast.ElemEllipsis:
Elt:
ast.Ident:
Name: row
ast.BasicLit:
Kind: INT
Value: 7
ast.BasicLit:
Kind: INT
Value: 8
ast.BasicLit:
Kind: INT
Value: 9
NElt: 3
6 changes: 6 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,11 @@ func (p *parser) parseSliceOrMatrixLit(lbrack token.Pos, first ast.Expr) ast.Exp
case token.SEMICOLON:
mat = append(mat, elts)
elts = make([]ast.Expr, 0, len(elts))
case token.ELLIPSIS:
n := len(elts)
elts[n-1] = &ast.ElemEllipsis{Ellipsis: p.pos, Elt: elts[n-1]}
p.next()
continue
default:
goto done
}
Expand Down Expand Up @@ -2195,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
12 changes: 12 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io/fs"
"testing"

"github.com/goplus/gop/ast"
"github.com/goplus/gop/token"
fsx "github.com/qiniu/x/http/fs"
)
Expand Down Expand Up @@ -313,4 +314,15 @@ func TestParseFieldDecl(t *testing.T) {
p.parseFieldDecl(nil)
}

func TestCheckExpr(t *testing.T) {
var p parser
p.init(token.NewFileSet(), "/foo/bar.gop", []byte(``), 0)
p.checkExpr(&ast.Ellipsis{})
p.checkExpr(&ast.ElemEllipsis{})
p.checkExpr(&ast.StarExpr{})
p.checkExpr(&ast.IndexListExpr{})
p.checkExpr(&ast.FuncType{})
p.checkExpr(&ast.FuncLit{})
}

// -----------------------------------------------------------------------------
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
Loading