1// Copyright 2009 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// Package parser implements a parser for Go source files. Input may be
6// provided in a variety of forms (see the various Parse* functions); the
7// output is an abstract syntax tree (AST) representing the Go source. The
8// parser is invoked through one of the Parse* functions.
9
10package parser
11
12import (
13	"fmt"
14	"go/ast"
15	"go/scanner"
16	"go/token"
17)
18
19// The mode parameter to the Parse* functions is a set of flags (or 0).
20// They control the amount of source code parsed and other optional
21// parser functionality.
22const (
23	PackageClauseOnly uint = 1 << iota // parsing stops after package clause
24	ImportsOnly                        // parsing stops after import declarations
25	ParseComments                      // parse comments and add them to AST
26	Trace                              // print a trace of parsed productions
27	DeclarationErrors                  // report declaration errors
28)
29
30// The parser structure holds the parser's internal state.
31type parser struct {
32	file *token.File
33	scanner.ErrorVector
34	scanner scanner.Scanner
35
36	// Tracing/debugging
37	mode   uint // parsing mode
38	trace  bool // == (mode & Trace != 0)
39	indent uint // indentation used for tracing output
40
41	// Comments
42	comments    []*ast.CommentGroup
43	leadComment *ast.CommentGroup // last lead comment
44	lineComment *ast.CommentGroup // last line comment
45
46	// Next token
47	pos token.Pos   // token position
48	tok token.Token // one token look-ahead
49	lit string      // token literal
50
51	// Non-syntactic parser control
52	exprLev int // < 0: in control clause, >= 0: in expression
53
54	// Ordinary identifier scopes
55	pkgScope   *ast.Scope        // pkgScope.Outer == nil
56	topScope   *ast.Scope        // top-most scope; may be pkgScope
57	unresolved []*ast.Ident      // unresolved identifiers
58	imports    []*ast.ImportSpec // list of imports
59
60	// Label scope
61	// (maintained by open/close LabelScope)
62	labelScope  *ast.Scope     // label scope for current function
63	targetStack [][]*ast.Ident // stack of unresolved labels
64}
65
66// scannerMode returns the scanner mode bits given the parser's mode bits.
67func scannerMode(mode uint) uint {
68	var m uint = scanner.InsertSemis
69	if mode&ParseComments != 0 {
70		m |= scanner.ScanComments
71	}
72	return m
73}
74
75func (p *parser) init(fset *token.FileSet, filename string, src []byte, mode uint) {
76	p.file = fset.AddFile(filename, fset.Base(), len(src))
77	p.scanner.Init(p.file, src, p, scannerMode(mode))
78
79	p.mode = mode
80	p.trace = mode&Trace != 0 // for convenience (p.trace is used frequently)
81
82	p.next()
83
84	// set up the pkgScope here (as opposed to in parseFile) because
85	// there are other parser entry points (ParseExpr, etc.)
86	p.openScope()
87	p.pkgScope = p.topScope
88
89	// for the same reason, set up a label scope
90	p.openLabelScope()
91}
92
93// ----------------------------------------------------------------------------
94// Scoping support
95
96func (p *parser) openScope() {
97	p.topScope = ast.NewScope(p.topScope)
98}
99
100func (p *parser) closeScope() {
101	p.topScope = p.topScope.Outer
102}
103
104func (p *parser) openLabelScope() {
105	p.labelScope = ast.NewScope(p.labelScope)
106	p.targetStack = append(p.targetStack, nil)
107}
108
109func (p *parser) closeLabelScope() {
110	// resolve labels
111	n := len(p.targetStack) - 1
112	scope := p.labelScope
113	for _, ident := range p.targetStack[n] {
114		ident.Obj = scope.Lookup(ident.Name)
115		if ident.Obj == nil && p.mode&DeclarationErrors != 0 {
116			p.error(ident.Pos(), fmt.Sprintf("label %s undefined", ident.Name))
117		}
118	}
119	// pop label scope
120	p.targetStack = p.targetStack[0:n]
121	p.labelScope = p.labelScope.Outer
122}
123
124func (p *parser) declare(decl any, scope *ast.Scope, kind ast.ObjKind, idents ...*ast.Ident) {
125	for _, ident := range idents {
126		assert(ident.Obj == nil, "identifier already declared or resolved")
127		if ident.Name != "_" {
128			obj := ast.NewObj(kind, ident.Name)
129			// remember the corresponding declaration for redeclaration
130			// errors and global variable resolution/typechecking phase
131			obj.Decl = decl
132			if alt := scope.Insert(obj); alt != nil && p.mode&DeclarationErrors != 0 {
133				prevDecl := ""
134				if pos := alt.Pos(); pos.IsValid() {
135					prevDecl = fmt.Sprintf("\n\tprevious declaration at %s", p.file.Position(pos))
136				}
137				p.error(ident.Pos(), fmt.Sprintf("%s redeclared in this block%s", ident.Name, prevDecl))
138			}
139			ident.Obj = obj
140		}
141	}
142}
143
144func (p *parser) shortVarDecl(idents []*ast.Ident) {
145	// Go spec: A short variable declaration may redeclare variables
146	// provided they were originally declared in the same block with
147	// the same type, and at least one of the non-blank variables is new.
148	n := 0 // number of new variables
149	for _, ident := range idents {
150		assert(ident.Obj == nil, "identifier already declared or resolved")
151		if ident.Name != "_" {
152			obj := ast.NewObj(ast.Var, ident.Name)
153			// short var declarations cannot have redeclaration errors
154			// and are not global => no need to remember the respective
155			// declaration
156			alt := p.topScope.Insert(obj)
157			if alt == nil {
158				n++ // new declaration
159				alt = obj
160			}
161			ident.Obj = alt
162		}
163	}
164	if n == 0 && p.mode&DeclarationErrors != 0 {
165		p.error(idents[0].Pos(), "no new variables on left side of :=")
166	}
167}
168
169// The unresolved object is a sentinel to mark identifiers that have been added
170// to the list of unresolved identifiers. The sentinel is only used for verifying
171// internal consistency.
172var unresolved = new(ast.Object)
173
174func (p *parser) resolve(x ast.Expr) {
175	// nothing to do if x is not an identifier or the blank identifier
176	ident, _ := x.(*ast.Ident)
177	if ident == nil {
178		return
179	}
180	assert(ident.Obj == nil, "identifier already declared or resolved")
181	if ident.Name == "_" {
182		return
183	}
184	// try to resolve the identifier
185	for s := p.topScope; s != nil; s = s.Outer {
186		if obj := s.Lookup(ident.Name); obj != nil {
187			ident.Obj = obj
188			return
189		}
190	}
191	// all local scopes are known, so any unresolved identifier
192	// must be found either in the file scope, package scope
193	// (perhaps in another file), or universe scope --- collect
194	// them so that they can be resolved later
195	ident.Obj = unresolved
196	p.unresolved = append(p.unresolved, ident)
197}
198
199// ----------------------------------------------------------------------------
200// Parsing support
201
202func (p *parser) printTrace(a ...any) {
203	const dots = ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . " +
204		". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . "
205	const n = uint(len(dots))
206	pos := p.file.Position(p.pos)
207	fmt.Printf("%5d:%3d: ", pos.Line, pos.Column)
208	i := 2 * p.indent
209	for ; i > n; i -= n {
210		fmt.Print(dots)
211	}
212	fmt.Print(dots[0:i])
213	fmt.Println(a...)
214}
215
216func trace(p *parser, msg string) *parser {
217	p.printTrace(msg, "(")
218	p.indent++
219	return p
220}
221
222// Usage pattern: defer un(trace(p, "..."));
223func un(p *parser) {
224	p.indent--
225	p.printTrace(")")
226}
227
228// Advance to the next token.
229func (p *parser) next0() {
230	// Because of one-token look-ahead, print the previous token
231	// when tracing as it provides a more readable output. The
232	// very first token (!p.pos.IsValid()) is not initialized
233	// (it is token.ILLEGAL), so don't print it.
234	if p.trace && p.pos.IsValid() {
235		s := p.tok.String()
236		switch {
237		case p.tok.IsLiteral():
238			p.printTrace(s, p.lit)
239		case p.tok.IsOperator(), p.tok.IsKeyword():
240			p.printTrace("\"" + s + "\"")
241		default:
242			p.printTrace(s)
243		}
244	}
245
246	p.pos, p.tok, p.lit = p.scanner.Scan()
247}
248
249// Consume a comment and return it and the line on which it ends.
250func (p *parser) consumeComment() (comment *ast.Comment, endline int) {
251	// /*-style comments may end on a different line than where they start.
252	// Scan the comment for '\n' chars and adjust endline accordingly.
253	endline = p.file.Line(p.pos)
254	if p.lit[1] == '*' {
255		// don't use range here - no need to decode Unicode code points
256		for i := 0; i < len(p.lit); i++ {
257			if p.lit[i] == '\n' {
258				endline++
259			}
260		}
261	}
262
263	comment = &ast.Comment{p.pos, p.lit}
264	p.next0()
265
266	return
267}
268
269// Consume a group of adjacent comments, add it to the parser's
270// comments list, and return it together with the line at which
271// the last comment in the group ends. An empty line or non-comment
272// token terminates a comment group.
273func (p *parser) consumeCommentGroup() (comments *ast.CommentGroup, endline int) {
274	var list []*ast.Comment
275	endline = p.file.Line(p.pos)
276	for p.tok == token.COMMENT && endline+1 >= p.file.Line(p.pos) {
277		var comment *ast.Comment
278		comment, endline = p.consumeComment()
279		list = append(list, comment)
280	}
281
282	// add comment group to the comments list
283	comments = &ast.CommentGroup{list}
284	p.comments = append(p.comments, comments)
285
286	return
287}
288
289// Advance to the next non-comment token. In the process, collect
290// any comment groups encountered, and remember the last lead and
291// line comments.
292//
293// A lead comment is a comment group that starts and ends in a
294// line without any other tokens and that is followed by a non-comment
295// token on the line immediately after the comment group.
296//
297// A line comment is a comment group that follows a non-comment
298// token on the same line, and that has no tokens after it on the line
299// where it ends.
300//
301// Lead and line comments may be considered documentation that is
302// stored in the AST.
303func (p *parser) next() {
304	p.leadComment = nil
305	p.lineComment = nil
306	line := p.file.Line(p.pos) // current line
307	p.next0()
308
309	if p.tok == token.COMMENT {
310		var comment *ast.CommentGroup
311		var endline int
312
313		if p.file.Line(p.pos) == line {
314			// The comment is on same line as the previous token; it
315			// cannot be a lead comment but may be a line comment.
316			comment, endline = p.consumeCommentGroup()
317			if p.file.Line(p.pos) != endline {
318				// The next token is on a different line, thus
319				// the last comment group is a line comment.
320				p.lineComment = comment
321			}
322		}
323
324		// consume successor comments, if any
325		endline = -1
326		for p.tok == token.COMMENT {
327			comment, endline = p.consumeCommentGroup()
328		}
329
330		if endline+1 == p.file.Line(p.pos) {
331			// The next token is following on the line immediately after the
332			// comment group, thus the last comment group is a lead comment.
333			p.leadComment = comment
334		}
335	}
336}
337
338func (p *parser) error(pos token.Pos, msg string) {
339	p.Error(p.file.Position(pos), msg)
340}
341
342func (p *parser) errorExpected(pos token.Pos, msg string) {
343	msg = "expected " + msg
344	if pos == p.pos {
345		// the error happened at the current position;
346		// make the error message more specific
347		if p.tok == token.SEMICOLON && p.lit[0] == '\n' {
348			msg += ", found newline"
349		} else {
350			msg += ", found '" + p.tok.String() + "'"
351			if p.tok.IsLiteral() {
352				msg += " " + p.lit
353			}
354		}
355	}
356	p.error(pos, msg)
357}
358
359func (p *parser) expect(tok token.Token) token.Pos {
360	pos := p.pos
361	if p.tok != tok {
362		p.errorExpected(pos, "'"+tok.String()+"'")
363	}
364	p.next() // make progress
365	return pos
366}
367
368func (p *parser) expectSemi() {
369	if p.tok != token.RPAREN && p.tok != token.RBRACE {
370		p.expect(token.SEMICOLON)
371	}
372}
373
374func assert(cond bool, msg string) {
375	if !cond {
376		panic("go/parser internal error: " + msg)
377	}
378}
379
380// ----------------------------------------------------------------------------
381// Identifiers
382
383func (p *parser) parseIdent() *ast.Ident {
384	pos := p.pos
385	name := "_"
386	if p.tok == token.IDENT {
387		name = p.lit
388		p.next()
389	} else {
390		p.expect(token.IDENT) // use expect() error handling
391	}
392	return &ast.Ident{pos, name, nil}
393}
394
395func (p *parser) parseIdentList() (list []*ast.Ident) {
396	if p.trace {
397		defer un(trace(p, "IdentList"))
398	}
399
400	list = append(list, p.parseIdent())
401	for p.tok == token.COMMA {
402		p.next()
403		list = append(list, p.parseIdent())
404	}
405
406	return
407}
408
409// ----------------------------------------------------------------------------
410// Common productions
411
412// If lhs is set, result list elements which are identifiers are not resolved.
413func (p *parser) parseExprList(lhs bool) (list []ast.Expr) {
414	if p.trace {
415		defer un(trace(p, "ExpressionList"))
416	}
417
418	list = append(list, p.parseExpr(lhs))
419	for p.tok == token.COMMA {
420		p.next()
421		list = append(list, p.parseExpr(lhs))
422	}
423
424	return
425}
426
427func (p *parser) parseLhsList() []ast.Expr {
428	list := p.parseExprList(true)
429	switch p.tok {
430	case token.DEFINE:
431		// lhs of a short variable declaration
432		p.shortVarDecl(p.makeIdentList(list))
433	case token.COLON:
434		// lhs of a label declaration or a communication clause of a select
435		// statement (parseLhsList is not called when parsing the case clause
436		// of a switch statement):
437		// - labels are declared by the caller of parseLhsList
438		// - for communication clauses, if there is a stand-alone identifier
439		//   followed by a colon, we have a syntax error; there is no need
440		//   to resolve the identifier in that case
441	default:
442		// identifiers must be declared elsewhere
443		for _, x := range list {
444			p.resolve(x)
445		}
446	}
447	return list
448}
449
450func (p *parser) parseRhsList() []ast.Expr {
451	return p.parseExprList(false)
452}
453
454// ----------------------------------------------------------------------------
455// Types
456
457func (p *parser) parseType() ast.Expr {
458	if p.trace {
459		defer un(trace(p, "Type"))
460	}
461
462	typ := p.tryType()
463
464	if typ == nil {
465		pos := p.pos
466		p.errorExpected(pos, "type")
467		p.next() // make progress
468		return &ast.BadExpr{pos, p.pos}
469	}
470
471	return typ
472}
473
474// If the result is an identifier, it is not resolved.
475func (p *parser) parseTypeName() ast.Expr {
476	if p.trace {
477		defer un(trace(p, "TypeName"))
478	}
479
480	ident := p.parseIdent()
481	// don't resolve ident yet - it may be a parameter or field name
482
483	if p.tok == token.PERIOD {
484		// ident is a package name
485		p.next()
486		p.resolve(ident)
487		sel := p.parseIdent()
488		return &ast.SelectorExpr{ident, sel}
489	}
490
491	return ident
492}
493
494func (p *parser) parseArrayType(ellipsisOk bool) ast.Expr {
495	if p.trace {
496		defer un(trace(p, "ArrayType"))
497	}
498
499	lbrack := p.expect(token.LBRACK)
500	var len ast.Expr
501	if ellipsisOk && p.tok == token.ELLIPSIS {
502		len = &ast.Ellipsis{p.pos, nil}
503		p.next()
504	} else if p.tok != token.RBRACK {
505		len = p.parseRhs()
506	}
507	p.expect(token.RBRACK)
508	elt := p.parseType()
509
510	return &ast.ArrayType{lbrack, len, elt}
511}
512
513func (p *parser) makeIdentList(list []ast.Expr) []*ast.Ident {
514	idents := make([]*ast.Ident, len(list))
515	for i, x := range list {
516		ident, isIdent := x.(*ast.Ident)
517		if !isIdent {
518			pos := x.(ast.Expr).Pos()
519			p.errorExpected(pos, "identifier")
520			ident = &ast.Ident{pos, "_", nil}
521		}
522		idents[i] = ident
523	}
524	return idents
525}
526
527func (p *parser) parseFieldDecl(scope *ast.Scope) *ast.Field {
528	if p.trace {
529		defer un(trace(p, "FieldDecl"))
530	}
531
532	doc := p.leadComment
533
534	// fields
535	list, typ := p.parseVarList(false)
536
537	// optional tag
538	var tag *ast.BasicLit
539	if p.tok == token.STRING {
540		tag = &ast.BasicLit{p.pos, p.tok, p.lit}
541		p.next()
542	}
543
544	// analyze case
545	var idents []*ast.Ident
546	if typ != nil {
547		// IdentifierList Type
548		idents = p.makeIdentList(list)
549	} else {
550		// ["*"] TypeName (AnonymousField)
551		typ = list[0] // we always have at least one element
552		p.resolve(typ)
553		if n := len(list); n > 1 || !isTypeName(deref(typ)) {
554			pos := typ.Pos()
555			p.errorExpected(pos, "anonymous field")
556			typ = &ast.BadExpr{pos, list[n-1].End()}
557		}
558	}
559
560	p.expectSemi() // call before accessing p.linecomment
561
562	field := &ast.Field{doc, idents, typ, tag, p.lineComment}
563	p.declare(field, scope, ast.Var, idents...)
564
565	return field
566}
567
568func (p *parser) parseStructType() *ast.StructType {
569	if p.trace {
570		defer un(trace(p, "StructType"))
571	}
572
573	pos := p.expect(token.STRUCT)
574	lbrace := p.expect(token.LBRACE)
575	scope := ast.NewScope(nil) // struct scope
576	var list []*ast.Field
577	for p.tok == token.IDENT || p.tok == token.MUL || p.tok == token.LPAREN {
578		// a field declaration cannot start with a '(' but we accept
579		// it here for more robust parsing and better error messages
580		// (parseFieldDecl will check and complain if necessary)
581		list = append(list, p.parseFieldDecl(scope))
582	}
583	rbrace := p.expect(token.RBRACE)
584
585	// TODO(gri): store struct scope in AST
586	return &ast.StructType{pos, &ast.FieldList{lbrace, list, rbrace}, false}
587}
588
589func (p *parser) parsePointerType() *ast.StarExpr {
590	if p.trace {
591		defer un(trace(p, "PointerType"))
592	}
593
594	star := p.expect(token.MUL)
595	base := p.parseType()
596
597	return &ast.StarExpr{star, base}
598}
599
600func (p *parser) tryVarType(isParam bool) ast.Expr {
601	if isParam && p.tok == token.ELLIPSIS {
602		pos := p.pos
603		p.next()
604		typ := p.tryIdentOrType(isParam) // don't use parseType so we can provide better error message
605		if typ == nil {
606			p.error(pos, "'...' parameter is missing type")
607			typ = &ast.BadExpr{pos, p.pos}
608		}
609		if p.tok != token.RPAREN {
610			p.error(pos, "can use '...' with last parameter type only")
611		}
612		return &ast.Ellipsis{pos, typ}
613	}
614	return p.tryIdentOrType(false)
615}
616
617func (p *parser) parseVarType(isParam bool) ast.Expr {
618	typ := p.tryVarType(isParam)
619	if typ == nil {
620		pos := p.pos
621		p.errorExpected(pos, "type")
622		p.next() // make progress
623		typ = &ast.BadExpr{pos, p.pos}
624	}
625	return typ
626}
627
628func (p *parser) parseVarList(isParam bool) (list []ast.Expr, typ ast.Expr) {
629	if p.trace {
630		defer un(trace(p, "VarList"))
631	}
632
633	// a list of identifiers looks like a list of type names
634	for {
635		// parseVarType accepts any type (including parenthesized ones)
636		// even though the syntax does not permit them here: we
637		// accept them all for more robust parsing and complain
638		// afterwards
639		list = append(list, p.parseVarType(isParam))
640		if p.tok != token.COMMA {
641			break
642		}
643		p.next()
644	}
645
646	// if we had a list of identifiers, it must be followed by a type
647	typ = p.tryVarType(isParam)
648	if typ != nil {
649		p.resolve(typ)
650	}
651
652	return
653}
654
655func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params []*ast.Field) {
656	if p.trace {
657		defer un(trace(p, "ParameterList"))
658	}
659
660	list, typ := p.parseVarList(ellipsisOk)
661	if typ != nil {
662		// IdentifierList Type
663		idents := p.makeIdentList(list)
664		field := &ast.Field{nil, idents, typ, nil, nil}
665		params = append(params, field)
666		// Go spec: The scope of an identifier denoting a function
667		// parameter or result variable is the function body.
668		p.declare(field, scope, ast.Var, idents...)
669		if p.tok == token.COMMA {
670			p.next()
671		}
672
673		for p.tok != token.RPAREN && p.tok != token.EOF {
674			idents := p.parseIdentList()
675			typ := p.parseVarType(ellipsisOk)
676			field := &ast.Field{nil, idents, typ, nil, nil}
677			params = append(params, field)
678			// Go spec: The scope of an identifier denoting a function
679			// parameter or result variable is the function body.
680			p.declare(field, scope, ast.Var, idents...)
681			if p.tok != token.COMMA {
682				break
683			}
684			p.next()
685		}
686
687	} else {
688		// Type { "," Type } (anonymous parameters)
689		params = make([]*ast.Field, len(list))
690		for i, x := range list {
691			p.resolve(x)
692			params[i] = &ast.Field{Type: x}
693		}
694	}
695
696	return
697}
698
699func (p *parser) parseParameters(scope *ast.Scope, ellipsisOk bool) *ast.FieldList {
700	if p.trace {
701		defer un(trace(p, "Parameters"))
702	}
703
704	var params []*ast.Field
705	lparen := p.expect(token.LPAREN)
706	if p.tok != token.RPAREN {
707		params = p.parseParameterList(scope, ellipsisOk)
708	}
709	rparen := p.expect(token.RPAREN)
710
711	return &ast.FieldList{lparen, params, rparen}
712}
713
714func (p *parser) parseResult(scope *ast.Scope) *ast.FieldList {
715	if p.trace {
716		defer un(trace(p, "Result"))
717	}
718
719	if p.tok == token.LPAREN {
720		return p.parseParameters(scope, false)
721	}
722
723	typ := p.tryType()
724	if typ != nil {
725		list := make([]*ast.Field, 1)
726		list[0] = &ast.Field{Type: typ}
727		return &ast.FieldList{List: list}
728	}
729
730	return nil
731}
732
733func (p *parser) parseSignature(scope *ast.Scope) (params, results *ast.FieldList) {
734	if p.trace {
735		defer un(trace(p, "Signature"))
736	}
737
738	params = p.parseParameters(scope, true)
739	results = p.parseResult(scope)
740
741	return
742}
743
744func (p *parser) parseFuncType() (*ast.FuncType, *ast.Scope) {
745	if p.trace {
746		defer un(trace(p, "FuncType"))
747	}
748
749	pos := p.expect(token.FUNC)
750	scope := ast.NewScope(p.topScope) // function scope
751	params, results := p.parseSignature(scope)
752
753	return &ast.FuncType{pos, params, results}, scope
754}
755
756func (p *parser) parseMethodSpec(scope *ast.Scope) *ast.Field {
757	if p.trace {
758		defer un(trace(p, "MethodSpec"))
759	}
760
761	doc := p.leadComment
762	var idents []*ast.Ident
763	var typ ast.Expr
764	x := p.parseTypeName()
765	if ident, isIdent := x.(*ast.Ident); isIdent && p.tok == token.LPAREN {
766		// method
767		idents = []*ast.Ident{ident}
768		scope := ast.NewScope(nil) // method scope
769		params, results := p.parseSignature(scope)
770		typ = &ast.FuncType{token.NoPos, params, results}
771	} else {
772		// embedded interface
773		typ = x
774	}
775	p.expectSemi() // call before accessing p.linecomment
776
777	spec := &ast.Field{doc, idents, typ, nil, p.lineComment}
778	p.declare(spec, scope, ast.Fun, idents...)
779
780	return spec
781}
782
783func (p *parser) parseInterfaceType() *ast.InterfaceType {
784	if p.trace {
785		defer un(trace(p, "InterfaceType"))
786	}
787
788	pos := p.expect(token.INTERFACE)
789	lbrace := p.expect(token.LBRACE)
790	scope := ast.NewScope(nil) // interface scope
791	var list []*ast.Field
792	for p.tok == token.IDENT {
793		list = append(list, p.parseMethodSpec(scope))
794	}
795	rbrace := p.expect(token.RBRACE)
796
797	// TODO(gri): store interface scope in AST
798	return &ast.InterfaceType{pos, &ast.FieldList{lbrace, list, rbrace}, false}
799}
800
801func (p *parser) parseMapType() *ast.MapType {
802	if p.trace {
803		defer un(trace(p, "MapType"))
804	}
805
806	pos := p.expect(token.MAP)
807	p.expect(token.LBRACK)
808	key := p.parseType()
809	p.expect(token.RBRACK)
810	value := p.parseType()
811
812	return &ast.MapType{pos, key, value}
813}
814
815func (p *parser) parseChanType() *ast.ChanType {
816	if p.trace {
817		defer un(trace(p, "ChanType"))
818	}
819
820	pos := p.pos
821	dir := ast.SEND | ast.RECV
822	if p.tok == token.CHAN {
823		p.next()
824		if p.tok == token.ARROW {
825			p.next()
826			dir = ast.SEND
827		}
828	} else {
829		p.expect(token.ARROW)
830		p.expect(token.CHAN)
831		dir = ast.RECV
832	}
833	value := p.parseType()
834
835	return &ast.ChanType{pos, dir, value}
836}
837
838// If the result is an identifier, it is not resolved.
839func (p *parser) tryIdentOrType(ellipsisOk bool) ast.Expr {
840	switch p.tok {
841	case token.IDENT:
842		return p.parseTypeName()
843	case token.LBRACK:
844		return p.parseArrayType(ellipsisOk)
845	case token.STRUCT:
846		return p.parseStructType()
847	case token.MUL:
848		return p.parsePointerType()
849	case token.FUNC:
850		typ, _ := p.parseFuncType()
851		return typ
852	case token.INTERFACE:
853		return p.parseInterfaceType()
854	case token.MAP:
855		return p.parseMapType()
856	case token.CHAN, token.ARROW:
857		return p.parseChanType()
858	case token.LPAREN:
859		lparen := p.pos
860		p.next()
861		typ := p.parseType()
862		rparen := p.expect(token.RPAREN)
863		return &ast.ParenExpr{lparen, typ, rparen}
864	}
865
866	// no type found
867	return nil
868}
869
870func (p *parser) tryType() ast.Expr {
871	typ := p.tryIdentOrType(false)
872	if typ != nil {
873		p.resolve(typ)
874	}
875	return typ
876}
877
878// ----------------------------------------------------------------------------
879// Blocks
880
881func (p *parser) parseStmtList() (list []ast.Stmt) {
882	if p.trace {
883		defer un(trace(p, "StatementList"))
884	}
885
886	for p.tok != token.CASE && p.tok != token.DEFAULT && p.tok != token.RBRACE && p.tok != token.EOF {
887		list = append(list, p.parseStmt())
888	}
889
890	return
891}
892
893func (p *parser) parseBody(scope *ast.Scope) *ast.BlockStmt {
894	if p.trace {
895		defer un(trace(p, "Body"))
896	}
897
898	lbrace := p.expect(token.LBRACE)
899	p.topScope = scope // open function scope
900	p.openLabelScope()
901	list := p.parseStmtList()
902	p.closeLabelScope()
903	p.closeScope()
904	rbrace := p.expect(token.RBRACE)
905
906	return &ast.BlockStmt{lbrace, list, rbrace}
907}
908
909func (p *parser) parseBlockStmt() *ast.BlockStmt {
910	if p.trace {
911		defer un(trace(p, "BlockStmt"))
912	}
913
914	lbrace := p.expect(token.LBRACE)
915	p.openScope()
916	list := p.parseStmtList()
917	p.closeScope()
918	rbrace := p.expect(token.RBRACE)
919
920	return &ast.BlockStmt{lbrace, list, rbrace}
921}
922
923// ----------------------------------------------------------------------------
924// Expressions
925
926func (p *parser) parseFuncTypeOrLit() ast.Expr {
927	if p.trace {
928		defer un(trace(p, "FuncTypeOrLit"))
929	}
930
931	typ, scope := p.parseFuncType()
932	if p.tok != token.LBRACE {
933		// function type only
934		return typ
935	}
936
937	p.exprLev++
938	body := p.parseBody(scope)
939	p.exprLev--
940
941	return &ast.FuncLit{typ, body}
942}
943
944// parseOperand may return an expression or a raw type (incl. array
945// types of the form [...]T. Callers must verify the result.
946// If lhs is set and the result is an identifier, it is not resolved.
947func (p *parser) parseOperand(lhs bool) ast.Expr {
948	if p.trace {
949		defer un(trace(p, "Operand"))
950	}
951
952	switch p.tok {
953	case token.IDENT:
954		x := p.parseIdent()
955		if !lhs {
956			p.resolve(x)
957		}
958		return x
959
960	case token.INT, token.FLOAT, token.IMAG, token.CHAR, token.STRING:
961		x := &ast.BasicLit{p.pos, p.tok, p.lit}
962		p.next()
963		return x
964
965	case token.LPAREN:
966		lparen := p.pos
967		p.next()
968		p.exprLev++
969		x := p.parseRhs()
970		p.exprLev--
971		rparen := p.expect(token.RPAREN)
972		return &ast.ParenExpr{lparen, x, rparen}
973
974	case token.FUNC:
975		return p.parseFuncTypeOrLit()
976
977	default:
978		if typ := p.tryIdentOrType(true); typ != nil {
979			// could be type for composite literal or conversion
980			_, isIdent := typ.(*ast.Ident)
981			assert(!isIdent, "type cannot be identifier")
982			return typ
983		}
984	}
985
986	pos := p.pos
987	p.errorExpected(pos, "operand")
988	p.next() // make progress
989	return &ast.BadExpr{pos, p.pos}
990}
991
992func (p *parser) parseSelector(x ast.Expr) ast.Expr {
993	if p.trace {
994		defer un(trace(p, "Selector"))
995	}
996
997	sel := p.parseIdent()
998
999	return &ast.SelectorExpr{x, sel}
1000}
1001
1002func (p *parser) parseTypeAssertion(x ast.Expr) ast.Expr {
1003	if p.trace {
1004		defer un(trace(p, "TypeAssertion"))
1005	}
1006
1007	p.expect(token.LPAREN)
1008	var typ ast.Expr
1009	if p.tok == token.TYPE {
1010		// type switch: typ == nil
1011		p.next()
1012	} else {
1013		typ = p.parseType()
1014	}
1015	p.expect(token.RPAREN)
1016
1017	return &ast.TypeAssertExpr{x, typ}
1018}
1019
1020func (p *parser) parseIndexOrSlice(x ast.Expr) ast.Expr {
1021	if p.trace {
1022		defer un(trace(p, "IndexOrSlice"))
1023	}
1024
1025	lbrack := p.expect(token.LBRACK)
1026	p.exprLev++
1027	var low, high ast.Expr
1028	isSlice := false
1029	if p.tok != token.COLON {
1030		low = p.parseRhs()
1031	}
1032	if p.tok == token.COLON {
1033		isSlice = true
1034		p.next()
1035		if p.tok != token.RBRACK {
1036			high = p.parseRhs()
1037		}
1038	}
1039	p.exprLev--
1040	rbrack := p.expect(token.RBRACK)
1041
1042	if isSlice {
1043		return &ast.SliceExpr{x, lbrack, low, high, rbrack}
1044	}
1045	return &ast.IndexExpr{x, lbrack, low, rbrack}
1046}
1047
1048func (p *parser) parseCallOrConversion(fun ast.Expr) *ast.CallExpr {
1049	if p.trace {
1050		defer un(trace(p, "CallOrConversion"))
1051	}
1052
1053	lparen := p.expect(token.LPAREN)
1054	p.exprLev++
1055	var list []ast.Expr
1056	var ellipsis token.Pos
1057	for p.tok != token.RPAREN && p.tok != token.EOF && !ellipsis.IsValid() {
1058		list = append(list, p.parseRhs())
1059		if p.tok == token.ELLIPSIS {
1060			ellipsis = p.pos
1061			p.next()
1062		}
1063		if p.tok != token.COMMA {
1064			break
1065		}
1066		p.next()
1067	}
1068	p.exprLev--
1069	rparen := p.expect(token.RPAREN)
1070
1071	return &ast.CallExpr{fun, lparen, list, ellipsis, rparen}
1072}
1073
1074func (p *parser) parseElement(keyOk bool) ast.Expr {
1075	if p.trace {
1076		defer un(trace(p, "Element"))
1077	}
1078
1079	if p.tok == token.LBRACE {
1080		return p.parseLiteralValue(nil)
1081	}
1082
1083	x := p.parseExpr(keyOk) // don't resolve if map key
1084	if keyOk {
1085		if p.tok == token.COLON {
1086			colon := p.pos
1087			p.next()
1088			return &ast.KeyValueExpr{x, colon, p.parseElement(false)}
1089		}
1090		p.resolve(x) // not a map key
1091	}
1092
1093	return x
1094}
1095
1096func (p *parser) parseElementList() (list []ast.Expr) {
1097	if p.trace {
1098		defer un(trace(p, "ElementList"))
1099	}
1100
1101	for p.tok != token.RBRACE && p.tok != token.EOF {
1102		list = append(list, p.parseElement(true))
1103		if p.tok != token.COMMA {
1104			break
1105		}
1106		p.next()
1107	}
1108
1109	return
1110}
1111
1112func (p *parser) parseLiteralValue(typ ast.Expr) ast.Expr {
1113	if p.trace {
1114		defer un(trace(p, "LiteralValue"))
1115	}
1116
1117	lbrace := p.expect(token.LBRACE)
1118	var elts []ast.Expr
1119	p.exprLev++
1120	if p.tok != token.RBRACE {
1121		elts = p.parseElementList()
1122	}
1123	p.exprLev--
1124	rbrace := p.expect(token.RBRACE)
1125	return &ast.CompositeLit{typ, lbrace, elts, rbrace}
1126}
1127
1128// checkExpr checks that x is an expression (and not a type).
1129func (p *parser) checkExpr(x ast.Expr) ast.Expr {
1130	switch t := ast.Unparen(x).(type) {
1131	case *ast.BadExpr:
1132	case *ast.Ident:
1133	case *ast.BasicLit:
1134	case *ast.FuncLit:
1135	case *ast.CompositeLit:
1136	case *ast.ParenExpr:
1137		panic("unreachable")
1138	case *ast.SelectorExpr:
1139	case *ast.IndexExpr:
1140	case *ast.SliceExpr:
1141	case *ast.TypeAssertExpr:
1142		if t.Type == nil {
1143			// the form X.(type) is only allowed in type switch expressions
1144			p.errorExpected(x.Pos(), "expression")
1145			x = &ast.BadExpr{x.Pos(), x.End()}
1146		}
1147	case *ast.CallExpr:
1148	case *ast.StarExpr:
1149	case *ast.UnaryExpr:
1150		if t.Op == token.RANGE {
1151			// the range operator is only allowed at the top of a for statement
1152			p.errorExpected(x.Pos(), "expression")
1153			x = &ast.BadExpr{x.Pos(), x.End()}
1154		}
1155	case *ast.BinaryExpr:
1156	default:
1157		// all other nodes are not proper expressions
1158		p.errorExpected(x.Pos(), "expression")
1159		x = &ast.BadExpr{x.Pos(), x.End()}
1160	}
1161	return x
1162}
1163
1164// isTypeName reports whether x is a (qualified) TypeName.
1165func isTypeName(x ast.Expr) bool {
1166	switch t := x.(type) {
1167	case *ast.BadExpr:
1168	case *ast.Ident:
1169	case *ast.SelectorExpr:
1170		_, isIdent := t.X.(*ast.Ident)
1171		return isIdent
1172	default:
1173		return false // all other nodes are not type names
1174	}
1175	return true
1176}
1177
1178// isLiteralType reports whether x is a legal composite literal type.
1179func isLiteralType(x ast.Expr) bool {
1180	switch t := x.(type) {
1181	case *ast.BadExpr:
1182	case *ast.Ident:
1183	case *ast.SelectorExpr:
1184		_, isIdent := t.X.(*ast.Ident)
1185		return isIdent
1186	case *ast.ArrayType:
1187	case *ast.StructType:
1188	case *ast.MapType:
1189	default:
1190		return false // all other nodes are not legal composite literal types
1191	}
1192	return true
1193}
1194
1195// If x is of the form *T, deref returns T, otherwise it returns x.
1196func deref(x ast.Expr) ast.Expr {
1197	if p, isPtr := x.(*ast.StarExpr); isPtr {
1198		x = p.X
1199	}
1200	return x
1201}
1202
1203// checkExprOrType checks that x is an expression or a type
1204// (and not a raw type such as [...]T).
1205func (p *parser) checkExprOrType(x ast.Expr) ast.Expr {
1206	switch t := ast.Unparen(x).(type) {
1207	case *ast.ParenExpr:
1208		panic("unreachable")
1209	case *ast.UnaryExpr:
1210		if t.Op == token.RANGE {
1211			// the range operator is only allowed at the top of a for statement
1212			p.errorExpected(x.Pos(), "expression")
1213			x = &ast.BadExpr{x.Pos(), x.End()}
1214		}
1215	case *ast.ArrayType:
1216		if len, isEllipsis := t.Len.(*ast.Ellipsis); isEllipsis {
1217			p.error(len.Pos(), "expected array length, found '...'")
1218			x = &ast.BadExpr{x.Pos(), x.End()}
1219		}
1220	}
1221
1222	// all other nodes are expressions or types
1223	return x
1224}
1225
1226// If lhs is set and the result is an identifier, it is not resolved.
1227func (p *parser) parsePrimaryExpr(lhs bool) ast.Expr {
1228	if p.trace {
1229		defer un(trace(p, "PrimaryExpr"))
1230	}
1231
1232	x := p.parseOperand(lhs)
1233L:
1234	for {
1235		switch p.tok {
1236		case token.PERIOD:
1237			p.next()
1238			if lhs {
1239				p.resolve(x)
1240			}
1241			switch p.tok {
1242			case token.IDENT:
1243				x = p.parseSelector(p.checkExpr(x))
1244			case token.LPAREN:
1245				x = p.parseTypeAssertion(p.checkExpr(x))
1246			default:
1247				pos := p.pos
1248				p.next() // make progress
1249				p.errorExpected(pos, "selector or type assertion")
1250				x = &ast.BadExpr{pos, p.pos}
1251			}
1252		case token.LBRACK:
1253			if lhs {
1254				p.resolve(x)
1255			}
1256			x = p.parseIndexOrSlice(p.checkExpr(x))
1257		case token.LPAREN:
1258			if lhs {
1259				p.resolve(x)
1260			}
1261			x = p.parseCallOrConversion(p.checkExprOrType(x))
1262		case token.LBRACE:
1263			if isLiteralType(x) && (p.exprLev >= 0 || !isTypeName(x)) {
1264				if lhs {
1265					p.resolve(x)
1266				}
1267				x = p.parseLiteralValue(x)
1268			} else {
1269				break L
1270			}
1271		default:
1272			break L
1273		}
1274		lhs = false // no need to try to resolve again
1275	}
1276
1277	return x
1278}
1279
1280// If lhs is set and the result is an identifier, it is not resolved.
1281func (p *parser) parseUnaryExpr(lhs bool) ast.Expr {
1282	if p.trace {
1283		defer un(trace(p, "UnaryExpr"))
1284	}
1285
1286	switch p.tok {
1287	case token.ADD, token.SUB, token.NOT, token.XOR, token.AND, token.RANGE:
1288		pos, op := p.pos, p.tok
1289		p.next()
1290		x := p.parseUnaryExpr(false)
1291		return &ast.UnaryExpr{pos, op, p.checkExpr(x)}
1292
1293	case token.ARROW:
1294		// channel type or receive expression
1295		pos := p.pos
1296		p.next()
1297		if p.tok == token.CHAN {
1298			p.next()
1299			value := p.parseType()
1300			return &ast.ChanType{pos, ast.RECV, value}
1301		}
1302
1303		x := p.parseUnaryExpr(false)
1304		return &ast.UnaryExpr{pos, token.ARROW, p.checkExpr(x)}
1305
1306	case token.MUL:
1307		// pointer type or unary "*" expression
1308		pos := p.pos
1309		p.next()
1310		x := p.parseUnaryExpr(false)
1311		return &ast.StarExpr{pos, p.checkExprOrType(x)}
1312	}
1313
1314	return p.parsePrimaryExpr(lhs)
1315}
1316
1317// If lhs is set and the result is an identifier, it is not resolved.
1318func (p *parser) parseBinaryExpr(lhs bool, prec1 int) ast.Expr {
1319	if p.trace {
1320		defer un(trace(p, "BinaryExpr"))
1321	}
1322
1323	x := p.parseUnaryExpr(lhs)
1324	for prec := p.tok.Precedence(); prec >= prec1; prec-- {
1325		for p.tok.Precedence() == prec {
1326			pos, op := p.pos, p.tok
1327			p.next()
1328			if lhs {
1329				p.resolve(x)
1330				lhs = false
1331			}
1332			y := p.parseBinaryExpr(false, prec+1)
1333			x = &ast.BinaryExpr{p.checkExpr(x), pos, op, p.checkExpr(y)}
1334		}
1335	}
1336
1337	return x
1338}
1339
1340// If lhs is set and the result is an identifier, it is not resolved.
1341// TODO(gri): parseExpr may return a type or even a raw type ([..]int) -
1342// should reject when a type/raw type is obviously not allowed
1343func (p *parser) parseExpr(lhs bool) ast.Expr {
1344	if p.trace {
1345		defer un(trace(p, "Expression"))
1346	}
1347
1348	return p.parseBinaryExpr(lhs, token.LowestPrec+1)
1349}
1350
1351func (p *parser) parseRhs() ast.Expr {
1352	return p.parseExpr(false)
1353}
1354
1355// ----------------------------------------------------------------------------
1356// Statements
1357
1358func (p *parser) parseSimpleStmt(labelOk bool) ast.Stmt {
1359	if p.trace {
1360		defer un(trace(p, "SimpleStmt"))
1361	}
1362
1363	x := p.parseLhsList()
1364
1365	switch p.tok {
1366	case
1367		token.DEFINE, token.ASSIGN, token.ADD_ASSIGN,
1368		token.SUB_ASSIGN, token.MUL_ASSIGN, token.QUO_ASSIGN,
1369		token.REM_ASSIGN, token.AND_ASSIGN, token.OR_ASSIGN,
1370		token.XOR_ASSIGN, token.SHL_ASSIGN, token.SHR_ASSIGN, token.AND_NOT_ASSIGN:
1371		// assignment statement
1372		pos, tok := p.pos, p.tok
1373		p.next()
1374		y := p.parseRhsList()
1375		return &ast.AssignStmt{x, pos, tok, y}
1376	}
1377
1378	if len(x) > 1 {
1379		p.errorExpected(x[0].Pos(), "1 expression")
1380		// continue with first expression
1381	}
1382
1383	switch p.tok {
1384	case token.COLON:
1385		// labeled statement
1386		colon := p.pos
1387		p.next()
1388		if label, isIdent := x[0].(*ast.Ident); labelOk && isIdent {
1389			// Go spec: The scope of a label is the body of the function
1390			// in which it is declared and excludes the body of any nested
1391			// function.
1392			stmt := &ast.LabeledStmt{label, colon, p.parseStmt()}
1393			p.declare(stmt, p.labelScope, ast.Lbl, label)
1394			return stmt
1395		}
1396		p.error(x[0].Pos(), "illegal label declaration")
1397		return &ast.BadStmt{x[0].Pos(), colon + 1}
1398
1399	case token.ARROW:
1400		// send statement
1401		arrow := p.pos
1402		p.next() // consume "<-"
1403		y := p.parseRhs()
1404		return &ast.SendStmt{x[0], arrow, y}
1405
1406	case token.INC, token.DEC:
1407		// increment or decrement
1408		s := &ast.IncDecStmt{x[0], p.pos, p.tok}
1409		p.next() // consume "++" or "--"
1410		return s
1411	}
1412
1413	// expression
1414	return &ast.ExprStmt{x[0]}
1415}
1416
1417func (p *parser) parseCallExpr() *ast.CallExpr {
1418	x := p.parseRhs()
1419	if call, isCall := x.(*ast.CallExpr); isCall {
1420		return call
1421	}
1422	p.errorExpected(x.Pos(), "function/method call")
1423	return nil
1424}
1425
1426func (p *parser) parseGoStmt() ast.Stmt {
1427	if p.trace {
1428		defer un(trace(p, "GoStmt"))
1429	}
1430
1431	pos := p.expect(token.GO)
1432	call := p.parseCallExpr()
1433	p.expectSemi()
1434	if call == nil {
1435		return &ast.BadStmt{pos, pos + 2} // len("go")
1436	}
1437
1438	return &ast.GoStmt{pos, call}
1439}
1440
1441func (p *parser) parseDeferStmt() ast.Stmt {
1442	if p.trace {
1443		defer un(trace(p, "DeferStmt"))
1444	}
1445
1446	pos := p.expect(token.DEFER)
1447	call := p.parseCallExpr()
1448	p.expectSemi()
1449	if call == nil {
1450		return &ast.BadStmt{pos, pos + 5} // len("defer")
1451	}
1452
1453	return &ast.DeferStmt{pos, call}
1454}
1455
1456func (p *parser) parseReturnStmt() *ast.ReturnStmt {
1457	if p.trace {
1458		defer un(trace(p, "ReturnStmt"))
1459	}
1460
1461	pos := p.pos
1462	p.expect(token.RETURN)
1463	var x []ast.Expr
1464	if p.tok != token.SEMICOLON && p.tok != token.RBRACE {
1465		x = p.parseRhsList()
1466	}
1467	p.expectSemi()
1468
1469	return &ast.ReturnStmt{pos, x}
1470}
1471
1472func (p *parser) parseBranchStmt(tok token.Token) *ast.BranchStmt {
1473	if p.trace {
1474		defer un(trace(p, "BranchStmt"))
1475	}
1476
1477	pos := p.expect(tok)
1478	var label *ast.Ident
1479	if tok != token.FALLTHROUGH && p.tok == token.IDENT {
1480		label = p.parseIdent()
1481		// add to list of unresolved targets
1482		n := len(p.targetStack) - 1
1483		p.targetStack[n] = append(p.targetStack[n], label)
1484	}
1485	p.expectSemi()
1486
1487	return &ast.BranchStmt{pos, tok, label}
1488}
1489
1490func (p *parser) makeExpr(s ast.Stmt) ast.Expr {
1491	if s == nil {
1492		return nil
1493	}
1494	if es, isExpr := s.(*ast.ExprStmt); isExpr {
1495		return p.checkExpr(es.X)
1496	}
1497	p.error(s.Pos(), "expected condition, found simple statement")
1498	return &ast.BadExpr{s.Pos(), s.End()}
1499}
1500
1501func (p *parser) parseIfStmt() *ast.IfStmt {
1502	if p.trace {
1503		defer un(trace(p, "IfStmt"))
1504	}
1505
1506	pos := p.expect(token.IF)
1507	p.openScope()
1508	defer p.closeScope()
1509
1510	var s ast.Stmt
1511	var x ast.Expr
1512	{
1513		prevLev := p.exprLev
1514		p.exprLev = -1
1515		if p.tok == token.SEMICOLON {
1516			p.next()
1517			x = p.parseRhs()
1518		} else {
1519			s = p.parseSimpleStmt(false)
1520			if p.tok == token.SEMICOLON {
1521				p.next()
1522				x = p.parseRhs()
1523			} else {
1524				x = p.makeExpr(s)
1525				s = nil
1526			}
1527		}
1528		p.exprLev = prevLev
1529	}
1530
1531	body := p.parseBlockStmt()
1532	var else_ ast.Stmt
1533	if p.tok == token.ELSE {
1534		p.next()
1535		else_ = p.parseStmt()
1536	} else {
1537		p.expectSemi()
1538	}
1539
1540	return &ast.IfStmt{pos, s, x, body, else_}
1541}
1542
1543func (p *parser) parseTypeList() (list []ast.Expr) {
1544	if p.trace {
1545		defer un(trace(p, "TypeList"))
1546	}
1547
1548	list = append(list, p.parseType())
1549	for p.tok == token.COMMA {
1550		p.next()
1551		list = append(list, p.parseType())
1552	}
1553
1554	return
1555}
1556
1557func (p *parser) parseCaseClause(exprSwitch bool) *ast.CaseClause {
1558	if p.trace {
1559		defer un(trace(p, "CaseClause"))
1560	}
1561
1562	pos := p.pos
1563	var list []ast.Expr
1564	if p.tok == token.CASE {
1565		p.next()
1566		if exprSwitch {
1567			list = p.parseRhsList()
1568		} else {
1569			list = p.parseTypeList()
1570		}
1571	} else {
1572		p.expect(token.DEFAULT)
1573	}
1574
1575	colon := p.expect(token.COLON)
1576	p.openScope()
1577	body := p.parseStmtList()
1578	p.closeScope()
1579
1580	return &ast.CaseClause{pos, list, colon, body}
1581}
1582
1583func isExprSwitch(s ast.Stmt) bool {
1584	if s == nil {
1585		return true
1586	}
1587	if e, ok := s.(*ast.ExprStmt); ok {
1588		if a, ok := e.X.(*ast.TypeAssertExpr); ok {
1589			return a.Type != nil // regular type assertion
1590		}
1591		return true
1592	}
1593	return false
1594}
1595
1596func (p *parser) parseSwitchStmt() ast.Stmt {
1597	if p.trace {
1598		defer un(trace(p, "SwitchStmt"))
1599	}
1600
1601	pos := p.expect(token.SWITCH)
1602	p.openScope()
1603	defer p.closeScope()
1604
1605	var s1, s2 ast.Stmt
1606	if p.tok != token.LBRACE {
1607		prevLev := p.exprLev
1608		p.exprLev = -1
1609		if p.tok != token.SEMICOLON {
1610			s2 = p.parseSimpleStmt(false)
1611		}
1612		if p.tok == token.SEMICOLON {
1613			p.next()
1614			s1 = s2
1615			s2 = nil
1616			if p.tok != token.LBRACE {
1617				s2 = p.parseSimpleStmt(false)
1618			}
1619		}
1620		p.exprLev = prevLev
1621	}
1622
1623	exprSwitch := isExprSwitch(s2)
1624	lbrace := p.expect(token.LBRACE)
1625	var list []ast.Stmt
1626	for p.tok == token.CASE || p.tok == token.DEFAULT {
1627		list = append(list, p.parseCaseClause(exprSwitch))
1628	}
1629	rbrace := p.expect(token.RBRACE)
1630	p.expectSemi()
1631	body := &ast.BlockStmt{lbrace, list, rbrace}
1632
1633	if exprSwitch {
1634		return &ast.SwitchStmt{pos, s1, p.makeExpr(s2), body}
1635	}
1636	// type switch
1637	// TODO(gri): do all the checks!
1638	return &ast.TypeSwitchStmt{pos, s1, s2, body}
1639}
1640
1641func (p *parser) parseCommClause() *ast.CommClause {
1642	if p.trace {
1643		defer un(trace(p, "CommClause"))
1644	}
1645
1646	p.openScope()
1647	pos := p.pos
1648	var comm ast.Stmt
1649	if p.tok == token.CASE {
1650		p.next()
1651		lhs := p.parseLhsList()
1652		if p.tok == token.ARROW {
1653			// SendStmt
1654			if len(lhs) > 1 {
1655				p.errorExpected(lhs[0].Pos(), "1 expression")
1656				// continue with first expression
1657			}
1658			arrow := p.pos
1659			p.next()
1660			rhs := p.parseRhs()
1661			comm = &ast.SendStmt{lhs[0], arrow, rhs}
1662		} else {
1663			// RecvStmt
1664			pos := p.pos
1665			tok := p.tok
1666			var rhs ast.Expr
1667			if tok == token.ASSIGN || tok == token.DEFINE {
1668				// RecvStmt with assignment
1669				if len(lhs) > 2 {
1670					p.errorExpected(lhs[0].Pos(), "1 or 2 expressions")
1671					// continue with first two expressions
1672					lhs = lhs[0:2]
1673				}
1674				p.next()
1675				rhs = p.parseRhs()
1676			} else {
1677				// rhs must be single receive operation
1678				if len(lhs) > 1 {
1679					p.errorExpected(lhs[0].Pos(), "1 expression")
1680					// continue with first expression
1681				}
1682				rhs = lhs[0]
1683				lhs = nil // there is no lhs
1684			}
1685			if x, isUnary := rhs.(*ast.UnaryExpr); !isUnary || x.Op != token.ARROW {
1686				p.errorExpected(rhs.Pos(), "send or receive operation")
1687				rhs = &ast.BadExpr{rhs.Pos(), rhs.End()}
1688			}
1689			if lhs != nil {
1690				comm = &ast.AssignStmt{lhs, pos, tok, []ast.Expr{rhs}}
1691			} else {
1692				comm = &ast.ExprStmt{rhs}
1693			}
1694		}
1695	} else {
1696		p.expect(token.DEFAULT)
1697	}
1698
1699	colon := p.expect(token.COLON)
1700	body := p.parseStmtList()
1701	p.closeScope()
1702
1703	return &ast.CommClause{pos, comm, colon, body}
1704}
1705
1706func (p *parser) parseSelectStmt() *ast.SelectStmt {
1707	if p.trace {
1708		defer un(trace(p, "SelectStmt"))
1709	}
1710
1711	pos := p.expect(token.SELECT)
1712	lbrace := p.expect(token.LBRACE)
1713	var list []ast.Stmt
1714	for p.tok == token.CASE || p.tok == token.DEFAULT {
1715		list = append(list, p.parseCommClause())
1716	}
1717	rbrace := p.expect(token.RBRACE)
1718	p.expectSemi()
1719	body := &ast.BlockStmt{lbrace, list, rbrace}
1720
1721	return &ast.SelectStmt{pos, body}
1722}
1723
1724func (p *parser) parseForStmt() ast.Stmt {
1725	if p.trace {
1726		defer un(trace(p, "ForStmt"))
1727	}
1728
1729	pos := p.expect(token.FOR)
1730	p.openScope()
1731	defer p.closeScope()
1732
1733	var s1, s2, s3 ast.Stmt
1734	if p.tok != token.LBRACE {
1735		prevLev := p.exprLev
1736		p.exprLev = -1
1737		if p.tok != token.SEMICOLON {
1738			s2 = p.parseSimpleStmt(false)
1739		}
1740		if p.tok == token.SEMICOLON {
1741			p.next()
1742			s1 = s2
1743			s2 = nil
1744			if p.tok != token.SEMICOLON {
1745				s2 = p.parseSimpleStmt(false)
1746			}
1747			p.expectSemi()
1748			if p.tok != token.LBRACE {
1749				s3 = p.parseSimpleStmt(false)
1750			}
1751		}
1752		p.exprLev = prevLev
1753	}
1754
1755	body := p.parseBlockStmt()
1756	p.expectSemi()
1757
1758	if as, isAssign := s2.(*ast.AssignStmt); isAssign {
1759		// possibly a for statement with a range clause; check assignment operator
1760		if as.Tok != token.ASSIGN && as.Tok != token.DEFINE {
1761			p.errorExpected(as.TokPos, "'=' or ':='")
1762			return &ast.BadStmt{pos, body.End()}
1763		}
1764		// check lhs
1765		var key, value ast.Expr
1766		switch len(as.Lhs) {
1767		case 2:
1768			key, value = as.Lhs[0], as.Lhs[1]
1769		case 1:
1770			key = as.Lhs[0]
1771		default:
1772			p.errorExpected(as.Lhs[0].Pos(), "1 or 2 expressions")
1773			return &ast.BadStmt{pos, body.End()}
1774		}
1775		// check rhs
1776		if len(as.Rhs) != 1 {
1777			p.errorExpected(as.Rhs[0].Pos(), "1 expression")
1778			return &ast.BadStmt{pos, body.End()}
1779		}
1780		if rhs, isUnary := as.Rhs[0].(*ast.UnaryExpr); isUnary && rhs.Op == token.RANGE {
1781			// rhs is range expression
1782			// (any short variable declaration was handled by parseSimpleStat above)
1783			return &ast.RangeStmt{pos, key, value, as.TokPos, as.Tok, rhs.X, body}
1784		}
1785		p.errorExpected(s2.Pos(), "range clause")
1786		return &ast.BadStmt{pos, body.End()}
1787	}
1788
1789	// regular for statement
1790	return &ast.ForStmt{pos, s1, p.makeExpr(s2), s3, body}
1791}
1792
1793func (p *parser) parseStmt() (s ast.Stmt) {
1794	if p.trace {
1795		defer un(trace(p, "Statement"))
1796	}
1797
1798	switch p.tok {
1799	case token.CONST, token.TYPE, token.VAR:
1800		s = &ast.DeclStmt{p.parseDecl()}
1801	case
1802		// tokens that may start a top-level expression
1803		token.IDENT, token.INT, token.FLOAT, token.CHAR, token.STRING, token.FUNC, token.LPAREN, // operand
1804		token.LBRACK, token.STRUCT, // composite type
1805		token.MUL, token.AND, token.ARROW, token.ADD, token.SUB, token.XOR: // unary operators
1806		s = p.parseSimpleStmt(true)
1807		// because of the required look-ahead, labeled statements are
1808		// parsed by parseSimpleStmt - don't expect a semicolon after
1809		// them
1810		if _, isLabeledStmt := s.(*ast.LabeledStmt); !isLabeledStmt {
1811			p.expectSemi()
1812		}
1813	case token.GO:
1814		s = p.parseGoStmt()
1815	case token.DEFER:
1816		s = p.parseDeferStmt()
1817	case token.RETURN:
1818		s = p.parseReturnStmt()
1819	case token.BREAK, token.CONTINUE, token.GOTO, token.FALLTHROUGH:
1820		s = p.parseBranchStmt(p.tok)
1821	case token.LBRACE:
1822		s = p.parseBlockStmt()
1823		p.expectSemi()
1824	case token.IF:
1825		s = p.parseIfStmt()
1826	case token.SWITCH:
1827		s = p.parseSwitchStmt()
1828	case token.SELECT:
1829		s = p.parseSelectStmt()
1830	case token.FOR:
1831		s = p.parseForStmt()
1832	case token.SEMICOLON:
1833		s = &ast.EmptyStmt{p.pos}
1834		p.next()
1835	case token.RBRACE:
1836		// a semicolon may be omitted before a closing "}"
1837		s = &ast.EmptyStmt{p.pos}
1838	default:
1839		// no statement found
1840		pos := p.pos
1841		p.errorExpected(pos, "statement")
1842		p.next() // make progress
1843		s = &ast.BadStmt{pos, p.pos}
1844	}
1845
1846	return
1847}
1848
1849// ----------------------------------------------------------------------------
1850// Declarations
1851
1852type parseSpecFunction func(p *parser, doc *ast.CommentGroup, iota int) ast.Spec
1853
1854func parseImportSpec(p *parser, doc *ast.CommentGroup, _ int) ast.Spec {
1855	if p.trace {
1856		defer un(trace(p, "ImportSpec"))
1857	}
1858
1859	var ident *ast.Ident
1860	switch p.tok {
1861	case token.PERIOD:
1862		ident = &ast.Ident{p.pos, ".", nil}
1863		p.next()
1864	case token.IDENT:
1865		ident = p.parseIdent()
1866	}
1867
1868	var path *ast.BasicLit
1869	if p.tok == token.STRING {
1870		path = &ast.BasicLit{p.pos, p.tok, p.lit}
1871		p.next()
1872	} else {
1873		p.expect(token.STRING) // use expect() error handling
1874	}
1875	p.expectSemi() // call before accessing p.linecomment
1876
1877	// collect imports
1878	spec := &ast.ImportSpec{doc, ident, path, p.lineComment}
1879	p.imports = append(p.imports, spec)
1880
1881	return spec
1882}
1883
1884func parseConstSpec(p *parser, doc *ast.CommentGroup, iota int) ast.Spec {
1885	if p.trace {
1886		defer un(trace(p, "ConstSpec"))
1887	}
1888
1889	idents := p.parseIdentList()
1890	typ := p.tryType()
1891	var values []ast.Expr
1892	if typ != nil || p.tok == token.ASSIGN || iota == 0 {
1893		p.expect(token.ASSIGN)
1894		values = p.parseRhsList()
1895	}
1896	p.expectSemi() // call before accessing p.linecomment
1897
1898	// Go spec: The scope of a constant or variable identifier declared inside
1899	// a function begins at the end of the ConstSpec or VarSpec and ends at
1900	// the end of the innermost containing block.
1901	// (Global identifiers are resolved in a separate phase after parsing.)
1902	spec := &ast.ValueSpec{doc, idents, typ, values, p.lineComment}
1903	p.declare(spec, p.topScope, ast.Con, idents...)
1904
1905	return spec
1906}
1907
1908func parseTypeSpec(p *parser, doc *ast.CommentGroup, _ int) ast.Spec {
1909	if p.trace {
1910		defer un(trace(p, "TypeSpec"))
1911	}
1912
1913	ident := p.parseIdent()
1914
1915	// Go spec: The scope of a type identifier declared inside a function begins
1916	// at the identifier in the TypeSpec and ends at the end of the innermost
1917	// containing block.
1918	// (Global identifiers are resolved in a separate phase after parsing.)
1919	spec := &ast.TypeSpec{doc, ident, nil, nil}
1920	p.declare(spec, p.topScope, ast.Typ, ident)
1921
1922	spec.Type = p.parseType()
1923	p.expectSemi() // call before accessing p.linecomment
1924	spec.Comment = p.lineComment
1925
1926	return spec
1927}
1928
1929func parseVarSpec(p *parser, doc *ast.CommentGroup, _ int) ast.Spec {
1930	if p.trace {
1931		defer un(trace(p, "VarSpec"))
1932	}
1933
1934	idents := p.parseIdentList()
1935	typ := p.tryType()
1936	var values []ast.Expr
1937	if typ == nil || p.tok == token.ASSIGN {
1938		p.expect(token.ASSIGN)
1939		values = p.parseRhsList()
1940	}
1941	p.expectSemi() // call before accessing p.linecomment
1942
1943	// Go spec: The scope of a constant or variable identifier declared inside
1944	// a function begins at the end of the ConstSpec or VarSpec and ends at
1945	// the end of the innermost containing block.
1946	// (Global identifiers are resolved in a separate phase after parsing.)
1947	spec := &ast.ValueSpec{doc, idents, typ, values, p.lineComment}
1948	p.declare(spec, p.topScope, ast.Var, idents...)
1949
1950	return spec
1951}
1952
1953func (p *parser) parseGenDecl(keyword token.Token, f parseSpecFunction) *ast.GenDecl {
1954	if p.trace {
1955		defer un(trace(p, "GenDecl("+keyword.String()+")"))
1956	}
1957
1958	doc := p.leadComment
1959	pos := p.expect(keyword)
1960	var lparen, rparen token.Pos
1961	var list []ast.Spec
1962	if p.tok == token.LPAREN {
1963		lparen = p.pos
1964		p.next()
1965		for iota := 0; p.tok != token.RPAREN && p.tok != token.EOF; iota++ {
1966			list = append(list, f(p, p.leadComment, iota))
1967		}
1968		rparen = p.expect(token.RPAREN)
1969		p.expectSemi()
1970	} else {
1971		list = append(list, f(p, nil, 0))
1972	}
1973
1974	return &ast.GenDecl{doc, pos, keyword, lparen, list, rparen}
1975}
1976
1977func (p *parser) parseReceiver(scope *ast.Scope) *ast.FieldList {
1978	if p.trace {
1979		defer un(trace(p, "Receiver"))
1980	}
1981
1982	pos := p.pos
1983	par := p.parseParameters(scope, false)
1984
1985	// must have exactly one receiver
1986	if par.NumFields() != 1 {
1987		p.errorExpected(pos, "exactly one receiver")
1988		// TODO determine a better range for BadExpr below
1989		par.List = []*ast.Field{{Type: &ast.BadExpr{pos, pos}}}
1990		return par
1991	}
1992
1993	// recv type must be of the form ["*"] identifier
1994	recv := par.List[0]
1995	base := deref(recv.Type)
1996	if _, isIdent := base.(*ast.Ident); !isIdent {
1997		p.errorExpected(base.Pos(), "(unqualified) identifier")
1998		par.List = []*ast.Field{{Type: &ast.BadExpr{recv.Pos(), recv.End()}}}
1999	}
2000
2001	return par
2002}
2003
2004func (p *parser) parseFuncDecl() *ast.FuncDecl {
2005	if p.trace {
2006		defer un(trace(p, "FunctionDecl"))
2007	}
2008
2009	doc := p.leadComment
2010	pos := p.expect(token.FUNC)
2011	scope := ast.NewScope(p.topScope) // function scope
2012
2013	var recv *ast.FieldList
2014	if p.tok == token.LPAREN {
2015		recv = p.parseReceiver(scope)
2016	}
2017
2018	ident := p.parseIdent()
2019
2020	params, results := p.parseSignature(scope)
2021
2022	var body *ast.BlockStmt
2023	if p.tok == token.LBRACE {
2024		body = p.parseBody(scope)
2025	}
2026	p.expectSemi()
2027
2028	decl := &ast.FuncDecl{doc, recv, ident, &ast.FuncType{pos, params, results}, body}
2029	if recv == nil {
2030		// Go spec: The scope of an identifier denoting a constant, type,
2031		// variable, or function (but not method) declared at top level
2032		// (outside any function) is the package block.
2033		//
2034		// init() functions cannot be referred to and there may
2035		// be more than one - don't put them in the pkgScope
2036		if ident.Name != "init" {
2037			p.declare(decl, p.pkgScope, ast.Fun, ident)
2038		}
2039	}
2040
2041	return decl
2042}
2043
2044func (p *parser) parseDecl() ast.Decl {
2045	if p.trace {
2046		defer un(trace(p, "Declaration"))
2047	}
2048
2049	var f parseSpecFunction
2050	switch p.tok {
2051	case token.CONST:
2052		f = parseConstSpec
2053
2054	case token.TYPE:
2055		f = parseTypeSpec
2056
2057	case token.VAR:
2058		f = parseVarSpec
2059
2060	case token.FUNC:
2061		return p.parseFuncDecl()
2062
2063	default:
2064		pos := p.pos
2065		p.errorExpected(pos, "declaration")
2066		p.next() // make progress
2067		decl := &ast.BadDecl{pos, p.pos}
2068		return decl
2069	}
2070
2071	return p.parseGenDecl(p.tok, f)
2072}
2073
2074func (p *parser) parseDeclList() (list []ast.Decl) {
2075	if p.trace {
2076		defer un(trace(p, "DeclList"))
2077	}
2078
2079	for p.tok != token.EOF {
2080		list = append(list, p.parseDecl())
2081	}
2082
2083	return
2084}
2085
2086// ----------------------------------------------------------------------------
2087// Source files
2088
2089func (p *parser) parseFile() *ast.File {
2090	if p.trace {
2091		defer un(trace(p, "File"))
2092	}
2093
2094	// package clause
2095	doc := p.leadComment
2096	pos := p.expect(token.PACKAGE)
2097	// Go spec: The package clause is not a declaration;
2098	// the package name does not appear in any scope.
2099	ident := p.parseIdent()
2100	if ident.Name == "_" {
2101		p.error(p.pos, "invalid package name _")
2102	}
2103	p.expectSemi()
2104
2105	var decls []ast.Decl
2106
2107	// Don't bother parsing the rest if we had errors already.
2108	// Likely not a Go source file at all.
2109
2110	if p.ErrorCount() == 0 && p.mode&PackageClauseOnly == 0 {
2111		// import decls
2112		for p.tok == token.IMPORT {
2113			decls = append(decls, p.parseGenDecl(token.IMPORT, parseImportSpec))
2114		}
2115
2116		if p.mode&ImportsOnly == 0 {
2117			// rest of package body
2118			for p.tok != token.EOF {
2119				decls = append(decls, p.parseDecl())
2120			}
2121		}
2122	}
2123
2124	assert(p.topScope == p.pkgScope, "imbalanced scopes")
2125
2126	// resolve global identifiers within the same file
2127	i := 0
2128	for _, ident := range p.unresolved {
2129		// i <= index for current ident
2130		assert(ident.Obj == unresolved, "object already resolved")
2131		ident.Obj = p.pkgScope.Lookup(ident.Name) // also removes unresolved sentinel
2132		if ident.Obj == nil {
2133			p.unresolved[i] = ident
2134			i++
2135		}
2136	}
2137
2138	// TODO(gri): store p.imports in AST
2139	return &ast.File{doc, pos, ident, decls, p.pkgScope, p.imports, p.unresolved[0:i], p.comments}
2140}
2141