@@ -2257,12 +2257,14 @@ func (p *parser) checkExprOrType(x ast.Expr) ast.Expr {
2257
2257
}
2258
2258
2259
2259
// If lhs is set and the result is an identifier, it is not resolved.
2260
- func (p * parser ) parsePrimaryExpr (lhs , allowTuple , allowCmd bool ) (x ast.Expr , isTuple bool ) {
2260
+ func (p * parser ) parsePrimaryExpr (iden * ast. Ident , lhs , allowTuple , allowCmd bool ) (x ast.Expr , isTuple bool ) {
2261
2261
if p .trace {
2262
2262
defer un (trace (p , "PrimaryExpr" ))
2263
2263
}
2264
2264
2265
- if x , isTuple = p .parseOperand (lhs , allowTuple , allowCmd ); isTuple {
2265
+ if iden != nil {
2266
+ x = iden
2267
+ } else if x , isTuple = p .parseOperand (lhs , allowTuple , allowCmd ); isTuple {
2266
2268
return
2267
2269
}
2268
2270
L:
@@ -2384,7 +2386,7 @@ func (p *parser) checkCmd() bool {
2384
2386
2385
2387
// parseErrWrapExpr: expr! expr? expr?:defval
2386
2388
func (p * parser ) parseErrWrapExpr (lhs , allowTuple , allowCmd bool ) (x ast.Expr , isTuple bool ) {
2387
- if x , isTuple = p .parsePrimaryExpr (lhs , allowTuple , allowCmd ); isTuple {
2389
+ if x , isTuple = p .parsePrimaryExpr (nil , lhs , allowTuple , allowCmd ); isTuple {
2388
2390
return
2389
2391
}
2390
2392
if expr , ok := x .(* ast.ErrWrapExpr ); ok {
@@ -2674,8 +2676,14 @@ const (
2674
2676
// of a range clause (with mode == rangeOk). The returned statement is an
2675
2677
// assignment with a right-hand side that is a single unary expression of
2676
2678
// the form "range x". No guarantees are given for the left-hand side.
2677
- func (p * parser ) parseSimpleStmt (mode int , allowCmd bool ) (ast.Stmt , bool ) {
2678
- return p .parseSimpleStmtEx (mode , allowCmd , false )
2679
+ func (p * parser ) parseSimpleStmt (mode int , allowCmd bool ) ast.Stmt {
2680
+ ss , _ /* isRange */ := p .parseSimpleStmtEx (mode , allowCmd , false )
2681
+ return ss
2682
+ }
2683
+
2684
+ func (p * parser ) parseBranchCmdStmt (iden * ast.Ident ) ast.Stmt { // Go+: goto as command
2685
+ x , _ := p .parsePrimaryExpr (iden , false , false , true )
2686
+ return & ast.ExprStmt {X : x }
2679
2687
}
2680
2688
2681
2689
func (p * parser ) parseSimpleStmtEx (mode int , allowCmd , allowRangeExpr bool ) (ast.Stmt , bool ) {
@@ -2833,20 +2841,29 @@ func (p *parser) parseBranchStmt(tok token.Token) ast.Stmt {
2833
2841
2834
2842
oldpos , oldlit := p .pos , p .lit // Go+: save token to allow goto() as a function
2835
2843
pos := p .expect (tok )
2836
- if p .tok == token .LPAREN { // Go+: allow goto() as a function
2844
+ next := p .tok
2845
+ if next != token .IDENT && next != token .SEMICOLON { // Go+: allow goto() as a function
2837
2846
p .unget (oldpos , token .IDENT , oldlit )
2838
- s , _ := p .parseSimpleStmt (basic , false )
2847
+ s := p .parseSimpleStmt (basic , true )
2839
2848
p .expectSemi ()
2840
2849
return s
2841
2850
}
2842
2851
2843
2852
var label * ast.Ident
2844
- if tok != token .FALLTHROUGH && p . tok == token .IDENT {
2853
+ if tok != token .FALLTHROUGH && next == token .IDENT {
2845
2854
label = p .parseIdent ()
2846
2855
// add to list of unresolved targets
2847
2856
n := len (p .targetStack ) - 1
2848
2857
p .targetStack [n ] = append (p .targetStack [n ], label )
2849
2858
}
2859
+ if p .tok != token .SEMICOLON { // Go+: goto command
2860
+ if label != nil {
2861
+ p .unget (label .NamePos , token .IDENT , label .Name )
2862
+ }
2863
+ s := p .parseBranchCmdStmt (& ast.Ident {NamePos : oldpos , Name : oldlit })
2864
+ p .expectSemi ()
2865
+ return s
2866
+ }
2850
2867
p .expectSemi ()
2851
2868
2852
2869
return & ast.BranchStmt {TokPos : pos , Tok : tok , Label : label }
@@ -2887,7 +2904,7 @@ func (p *parser) parseIfHeader() (init ast.Stmt, cond ast.Expr) {
2887
2904
p .next ()
2888
2905
p .error (p .pos , "var declaration not allowed in 'IF' initializer" )
2889
2906
}
2890
- init , _ = p .parseSimpleStmt (basic , false )
2907
+ init = p .parseSimpleStmt (basic , false )
2891
2908
}
2892
2909
2893
2910
var condStmt ast.Stmt
@@ -2904,7 +2921,7 @@ func (p *parser) parseIfHeader() (init ast.Stmt, cond ast.Expr) {
2904
2921
p .expect (token .SEMICOLON )
2905
2922
}
2906
2923
if p .tok != token .LBRACE {
2907
- condStmt , _ = p .parseSimpleStmt (basic , false )
2924
+ condStmt = p .parseSimpleStmt (basic , false )
2908
2925
}
2909
2926
} else {
2910
2927
condStmt = init
@@ -2951,7 +2968,7 @@ func (p *parser) parseForPhraseCond() (init ast.Stmt, cond ast.Expr) {
2951
2968
p .next ()
2952
2969
p .error (p .pos , "var declaration not allowed in 'IF' initializer" )
2953
2970
}
2954
- init , _ = p .parseSimpleStmt (basic , false )
2971
+ init = p .parseSimpleStmt (basic , false )
2955
2972
}
2956
2973
2957
2974
var condStmt ast.Stmt
@@ -2968,7 +2985,7 @@ func (p *parser) parseForPhraseCond() (init ast.Stmt, cond ast.Expr) {
2968
2985
p .expect (token .SEMICOLON )
2969
2986
}
2970
2987
if ! isForPhraseCondEnd (p .tok ) {
2971
- condStmt , _ = p .parseSimpleStmt (basic , false )
2988
+ condStmt = p .parseSimpleStmt (basic , false )
2972
2989
}
2973
2990
} else {
2974
2991
condStmt = init
@@ -3102,7 +3119,7 @@ func (p *parser) parseSwitchStmt() ast.Stmt {
3102
3119
prevLev := p .exprLev
3103
3120
p .exprLev = - 1
3104
3121
if p .tok != token .SEMICOLON {
3105
- s2 , _ = p .parseSimpleStmt (basic , false )
3122
+ s2 = p .parseSimpleStmt (basic , false )
3106
3123
}
3107
3124
if p .tok == token .SEMICOLON {
3108
3125
p .next ()
@@ -3123,7 +3140,7 @@ func (p *parser) parseSwitchStmt() ast.Stmt {
3123
3140
// Having the extra nested but empty scope won't affect it.
3124
3141
p .openScope ()
3125
3142
defer p .closeScope ()
3126
- s2 , _ = p .parseSimpleStmt (basic , false )
3143
+ s2 = p .parseSimpleStmt (basic , false )
3127
3144
}
3128
3145
}
3129
3146
p .exprLev = prevLev
@@ -3329,11 +3346,11 @@ func (p *parser) parseForStmt() ast.Stmt {
3329
3346
s1 = s2
3330
3347
s2 = nil
3331
3348
if p .tok != token .SEMICOLON {
3332
- s2 , _ = p .parseSimpleStmt (basic , false )
3349
+ s2 = p .parseSimpleStmt (basic , false )
3333
3350
}
3334
3351
p .expectSemi ()
3335
3352
if p .tok != token .LBRACE {
3336
- s3 , _ = p .parseSimpleStmt (basic , false )
3353
+ s3 = p .parseSimpleStmt (basic , false )
3337
3354
}
3338
3355
}
3339
3356
p .exprLev = prevLev
@@ -3412,7 +3429,7 @@ func (p *parser) parseStmt(allowCmd bool) (s ast.Stmt) {
3412
3429
allowCmd = false
3413
3430
fallthrough
3414
3431
case token .IDENT , token .MAP : // operands
3415
- s , _ = p .parseSimpleStmt (labelOk , allowCmd )
3432
+ s = p .parseSimpleStmt (labelOk , allowCmd )
3416
3433
// because of the required look-ahead, labeled statements are
3417
3434
// parsed by parseSimpleStmt - don't expect a semicolon after
3418
3435
// them
@@ -3684,7 +3701,7 @@ func (p *parser) parseOverloadFunc() (ast.Expr, bool) {
3684
3701
case token .FUNC :
3685
3702
return p .parseFuncTypeOrLit (), true
3686
3703
case token .LPAREN :
3687
- x , _ := p .parsePrimaryExpr (false , false , false )
3704
+ x , _ := p .parsePrimaryExpr (nil , false , false , false )
3688
3705
return x , true
3689
3706
}
3690
3707
return nil , false
0 commit comments