OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // A parser for Go source files. Input may be provided in a variety of | 5 // A parser for Go source files. Input may be provided in a variety of |
6 // forms (see the various Parse* functions); the output is an abstract | 6 // forms (see the various Parse* functions); the output is an abstract |
7 // syntax tree (AST) representing the Go source. The parser is invoked | 7 // syntax tree (AST) representing the Go source. The parser is invoked |
8 // through one of the Parse* functions. | 8 // through one of the Parse* functions. |
9 // | 9 // |
10 package parser | 10 package parser |
(...skipping 943 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
954 } | 954 } |
955 p.next() | 955 p.next() |
956 } | 956 } |
957 p.exprLev-- | 957 p.exprLev-- |
958 rparen := p.expect(token.RPAREN) | 958 rparen := p.expect(token.RPAREN) |
959 | 959 |
960 return &ast.CallExpr{fun, lparen, makeExprList(&list), ellipsis, rparen} | 960 return &ast.CallExpr{fun, lparen, makeExprList(&list), ellipsis, rparen} |
961 } | 961 } |
962 | 962 |
963 | 963 |
964 func (p *parser) parseElement() ast.Expr { | 964 func (p *parser) parseElement(keyOk bool) ast.Expr { |
965 if p.trace { | 965 if p.trace { |
966 defer un(trace(p, "Element")) | 966 defer un(trace(p, "Element")) |
967 } | 967 } |
968 | 968 |
| 969 if p.tok == token.LBRACE { |
| 970 return p.parseLiteralValue(nil) |
| 971 } |
| 972 |
969 x := p.parseExpr() | 973 x := p.parseExpr() |
970 » if p.tok == token.COLON { | 974 » if keyOk && p.tok == token.COLON { |
971 colon := p.pos | 975 colon := p.pos |
972 p.next() | 976 p.next() |
973 » » x = &ast.KeyValueExpr{x, colon, p.parseExpr()} | 977 » » x = &ast.KeyValueExpr{x, colon, p.parseElement(false)} |
974 } | 978 } |
975 | |
976 return x | 979 return x |
977 } | 980 } |
978 | 981 |
979 | 982 |
980 func (p *parser) parseElementList() []ast.Expr { | 983 func (p *parser) parseElementList() []ast.Expr { |
981 if p.trace { | 984 if p.trace { |
982 defer un(trace(p, "ElementList")) | 985 defer un(trace(p, "ElementList")) |
983 } | 986 } |
984 | 987 |
985 var list vector.Vector | 988 var list vector.Vector |
986 for p.tok != token.RBRACE && p.tok != token.EOF { | 989 for p.tok != token.RBRACE && p.tok != token.EOF { |
987 » » list.Push(p.parseElement()) | 990 » » list.Push(p.parseElement(true)) |
988 if p.tok != token.COMMA { | 991 if p.tok != token.COMMA { |
989 break | 992 break |
990 } | 993 } |
991 p.next() | 994 p.next() |
992 } | 995 } |
993 | 996 |
994 return makeExprList(&list) | 997 return makeExprList(&list) |
995 } | 998 } |
996 | 999 |
997 | 1000 |
998 func (p *parser) parseCompositeLit(typ ast.Expr) ast.Expr { | 1001 func (p *parser) parseLiteralValue(typ ast.Expr) ast.Expr { |
999 if p.trace { | 1002 if p.trace { |
1000 » » defer un(trace(p, "CompositeLit")) | 1003 » » defer un(trace(p, "LiteralValue")) |
1001 } | 1004 } |
1002 | 1005 |
1003 lbrace := p.expect(token.LBRACE) | 1006 lbrace := p.expect(token.LBRACE) |
1004 var elts []ast.Expr | 1007 var elts []ast.Expr |
1005 p.exprLev++ | 1008 p.exprLev++ |
1006 if p.tok != token.RBRACE { | 1009 if p.tok != token.RBRACE { |
1007 elts = p.parseElementList() | 1010 elts = p.parseElementList() |
1008 } | 1011 } |
1009 p.exprLev-- | 1012 p.exprLev-- |
1010 rbrace := p.expect(token.RBRACE) | 1013 rbrace := p.expect(token.RBRACE) |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1135 for { | 1138 for { |
1136 switch p.tok { | 1139 switch p.tok { |
1137 case token.PERIOD: | 1140 case token.PERIOD: |
1138 x = p.parseSelectorOrTypeAssertion(p.checkExpr(x)) | 1141 x = p.parseSelectorOrTypeAssertion(p.checkExpr(x)) |
1139 case token.LBRACK: | 1142 case token.LBRACK: |
1140 x = p.parseIndexOrSlice(p.checkExpr(x)) | 1143 x = p.parseIndexOrSlice(p.checkExpr(x)) |
1141 case token.LPAREN: | 1144 case token.LPAREN: |
1142 x = p.parseCallOrConversion(p.checkExprOrType(x)) | 1145 x = p.parseCallOrConversion(p.checkExprOrType(x)) |
1143 case token.LBRACE: | 1146 case token.LBRACE: |
1144 if isLiteralType(x) && (p.exprLev >= 0 || !isTypeName(x)
) { | 1147 if isLiteralType(x) && (p.exprLev >= 0 || !isTypeName(x)
) { |
1145 » » » » x = p.parseCompositeLit(x) | 1148 » » » » x = p.parseLiteralValue(x) |
1146 } else { | 1149 } else { |
1147 break L | 1150 break L |
1148 } | 1151 } |
1149 default: | 1152 default: |
1150 break L | 1153 break L |
1151 } | 1154 } |
1152 } | 1155 } |
1153 | 1156 |
1154 return x | 1157 return x |
1155 } | 1158 } |
(...skipping 784 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1940 } | 1943 } |
1941 | 1944 |
1942 // convert comments list | 1945 // convert comments list |
1943 comments := make([]*ast.CommentGroup, len(p.comments)) | 1946 comments := make([]*ast.CommentGroup, len(p.comments)) |
1944 for i, x := range p.comments { | 1947 for i, x := range p.comments { |
1945 comments[i] = x.(*ast.CommentGroup) | 1948 comments[i] = x.(*ast.CommentGroup) |
1946 } | 1949 } |
1947 | 1950 |
1948 return &ast.File{doc, pos, ident, decls, comments} | 1951 return &ast.File{doc, pos, ident, decls, comments} |
1949 } | 1952 } |
OLD | NEW |