Skip to content

Commit a5e2c95

Browse files
authored
Merge pull request #1846 from xushiwei/q
parser: _testdata/matrix2 (support row...)
2 parents 2b397eb + 884b222 commit a5e2c95

File tree

6 files changed

+94
-0
lines changed

6 files changed

+94
-0
lines changed

ast/ast_gop.go

+20
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,26 @@ func (*MatrixLit) exprNode() {}
159159

160160
// -----------------------------------------------------------------------------
161161

162+
// A ElemEllipsis node represents a matrix row elements.
163+
type ElemEllipsis struct {
164+
Elt Expr // ellipsis element
165+
Ellipsis token.Pos // position of "..."
166+
}
167+
168+
// Pos - position of first character belonging to the node.
169+
func (p *ElemEllipsis) Pos() token.Pos {
170+
return p.Elt.Pos()
171+
}
172+
173+
// End - position of first character immediately after the node.
174+
func (p *ElemEllipsis) End() token.Pos {
175+
return p.Ellipsis + 3
176+
}
177+
178+
func (*ElemEllipsis) exprNode() {}
179+
180+
// -----------------------------------------------------------------------------
181+
162182
// ErrWrapExpr represents `expr!`, `expr?` or `expr?: defaultValue`.
163183
type ErrWrapExpr struct {
164184
X Expr

parser/_testdata/matrix2/matrix.gop

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
echo [
2+
1, 2, 3
3+
row...
4+
7, 8, 9
5+
]
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
file matrix.gop
4+
noEntrypoint
5+
ast.FuncDecl:
6+
Name:
7+
ast.Ident:
8+
Name: main
9+
Type:
10+
ast.FuncType:
11+
Params:
12+
ast.FieldList:
13+
Body:
14+
ast.BlockStmt:
15+
List:
16+
ast.ExprStmt:
17+
X:
18+
ast.CallExpr:
19+
Fun:
20+
ast.Ident:
21+
Name: echo
22+
Args:
23+
ast.MatrixLit:
24+
Elts:
25+
ast.BasicLit:
26+
Kind: INT
27+
Value: 1
28+
ast.BasicLit:
29+
Kind: INT
30+
Value: 2
31+
ast.BasicLit:
32+
Kind: INT
33+
Value: 3
34+
ast.ElemEllipsis:
35+
Elt:
36+
ast.Ident:
37+
Name: row
38+
ast.BasicLit:
39+
Kind: INT
40+
Value: 7
41+
ast.BasicLit:
42+
Kind: INT
43+
Value: 8
44+
ast.BasicLit:
45+
Kind: INT
46+
Value: 9
47+
NElt: 3

parser/parser.go

+6
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,11 @@ func (p *parser) parseSliceOrMatrixLit(lbrack token.Pos, first ast.Expr) ast.Exp
899899
case token.SEMICOLON:
900900
mat = append(mat, elts)
901901
elts = make([]ast.Expr, 0, len(elts))
902+
case token.ELLIPSIS:
903+
n := len(elts)
904+
elts[n-1] = &ast.ElemEllipsis{Ellipsis: p.pos, Elt: elts[n-1]}
905+
p.next()
906+
continue
902907
default:
903908
goto done
904909
}
@@ -2195,6 +2200,7 @@ func (p *parser) checkExpr(x ast.Expr) ast.Expr {
21952200
p.error(v.opening, msgTupleNotSupported)
21962201
x = &ast.BadExpr{From: v.opening, To: v.closing}
21972202
case *ast.EnvExpr:
2203+
case *ast.ElemEllipsis:
21982204
default:
21992205
// all other nodes are not proper expressions
22002206
p.errorExpected(x.Pos(), "expression", 3)

parser/parser_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"io/fs"
2121
"testing"
2222

23+
"github.com/goplus/gop/ast"
2324
"github.com/goplus/gop/token"
2425
fsx "github.com/qiniu/x/http/fs"
2526
)
@@ -313,4 +314,15 @@ func TestParseFieldDecl(t *testing.T) {
313314
p.parseFieldDecl(nil)
314315
}
315316

317+
func TestCheckExpr(t *testing.T) {
318+
var p parser
319+
p.init(token.NewFileSet(), "/foo/bar.gop", []byte(``), 0)
320+
p.checkExpr(&ast.Ellipsis{})
321+
p.checkExpr(&ast.ElemEllipsis{})
322+
p.checkExpr(&ast.StarExpr{})
323+
p.checkExpr(&ast.IndexListExpr{})
324+
p.checkExpr(&ast.FuncType{})
325+
p.checkExpr(&ast.FuncLit{})
326+
}
327+
316328
// -----------------------------------------------------------------------------

printer/nodes.go

+4
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,10 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
11271127
p.print(x.Name)
11281128
}
11291129

1130+
case *ast.ElemEllipsis:
1131+
p.expr(x.Elt)
1132+
p.print(token.ELLIPSIS)
1133+
11301134
default:
11311135
log.Fatalf("unreachable %T\n", x)
11321136
}

0 commit comments

Comments
 (0)