Skip to content

Commit

Permalink
Merge pull request #1493 from visualfc/parser_lit
Browse files Browse the repository at this point in the history
parser: parsePrimaryExpr fix LiteralValue check
  • Loading branch information
xushiwei authored Oct 26, 2023
2 parents 09e2200 + 3363ba1 commit 15ad484
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 16 deletions.
6 changes: 6 additions & 0 deletions parser/_instance/instance5/cmd.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var a [2]int
if 0 < a[0] {
println a
println T[int]{1}
println (T[int]){a:1,b:2}
}
115 changes: 115 additions & 0 deletions parser/_instance/instance5/parser.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package main

file cmd.gop
noEntrypoint
ast.GenDecl:
Tok: var
Specs:
ast.ValueSpec:
Names:
ast.Ident:
Name: a
Type:
ast.ArrayType:
Len:
ast.BasicLit:
Kind: INT
Value: 2
Elt:
ast.Ident:
Name: int
ast.FuncDecl:
Name:
ast.Ident:
Name: main
Type:
ast.FuncType:
Params:
ast.FieldList:
Body:
ast.BlockStmt:
List:
ast.IfStmt:
Cond:
ast.BinaryExpr:
X:
ast.BasicLit:
Kind: INT
Value: 0
Op: <
Y:
ast.IndexExpr:
X:
ast.Ident:
Name: a
Index:
ast.BasicLit:
Kind: INT
Value: 0
Body:
ast.BlockStmt:
List:
ast.ExprStmt:
X:
ast.CallExpr:
Fun:
ast.Ident:
Name: println
Args:
ast.Ident:
Name: a
ast.ExprStmt:
X:
ast.CallExpr:
Fun:
ast.Ident:
Name: println
Args:
ast.CompositeLit:
Type:
ast.IndexExpr:
X:
ast.Ident:
Name: T
Index:
ast.Ident:
Name: int
Elts:
ast.BasicLit:
Kind: INT
Value: 1
ast.ExprStmt:
X:
ast.CallExpr:
Fun:
ast.Ident:
Name: println
Args:
ast.CompositeLit:
Type:
ast.ParenExpr:
X:
ast.IndexExpr:
X:
ast.Ident:
Name: T
Index:
ast.Ident:
Name: int
Elts:
ast.KeyValueExpr:
Key:
ast.Ident:
Name: a
Value:
ast.BasicLit:
Kind: INT
Value: 1
ast.KeyValueExpr:
Key:
ast.Ident:
Name: b
Value:
ast.BasicLit:
Kind: INT
Value: 2
18 changes: 2 additions & 16 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2068,23 +2068,9 @@ func (p *parser) checkExpr(x ast.Expr) ast.Expr {
return x
}

// isTypeName reports whether x is a (qualified) TypeName.
func isTypeName(x ast.Expr) bool {
switch t := x.(type) {
case *ast.BadExpr:
case *ast.Ident:
case *ast.SelectorExpr:
_, isIdent := t.X.(*ast.Ident)
return isIdent
default:
return false // all other nodes are not type names
}
return true
}

// isLiteralType reports whether x is a legal composite literal type.
func isLiteralType(x ast.Expr) bool {
switch t := x.(type) {
switch t := unparen(x).(type) {
case *ast.BadExpr:
case *ast.Ident:
case *ast.IndexExpr:
Expand Down Expand Up @@ -2188,7 +2174,7 @@ L:
case token.LBRACE: // {
if allowCmd && p.isCmd(x) { // println {...}
x = p.parseCallOrConversion(p.checkExprOrType(x), true)
} else if isLiteralType(x) && (p.exprLev >= 0 || !isTypeName(x)) {
} else if isLiteralType(x) && p.exprLev >= 0 {
if lhs {
p.resolve(x)
}
Expand Down

0 comments on commit 15ad484

Please sign in to comment.