ConstDecl = "const" ExportedName [ Type ] "=" Literal . Literal = bool_lit | int_lit | float_lit | complex_lit | string_lit . bool_lit = "true" | "false" . complex_lit = "(" float_lit "+" float_lit ")" . rune_lit = "(" int_lit "+" int_lit ")" . string_lit = `"` { unicode_char } `"` .
()
| 484 | // rune_lit = "(" int_lit "+" int_lit ")" . |
| 485 | // string_lit = `"` { unicode_char } `"` . |
| 486 | func (p *gc_parser) parse_const_decl() (string, *ast.GenDecl) { |
| 487 | // TODO: do we really need actual const value? gocode doesn't use this |
| 488 | p.expect_keyword("const") |
| 489 | name := p.parse_exported_name() |
| 490 | |
| 491 | var typ ast.Expr |
| 492 | if p.tok != '=' { |
| 493 | typ = p.parse_type() |
| 494 | } |
| 495 | |
| 496 | p.expect('=') |
| 497 | |
| 498 | // skip the value |
| 499 | switch p.tok { |
| 500 | case scanner.Ident: |
| 501 | // must be bool, true or false |
| 502 | p.next() |
| 503 | case '-', '+', scanner.Int: |
| 504 | // number |
| 505 | p.parse_number() |
| 506 | case '(': |
| 507 | // complex_lit or rune_lit |
| 508 | p.next() // skip '(' |
| 509 | if p.tok == scanner.Char { |
| 510 | p.next() |
| 511 | } else { |
| 512 | p.parse_number() |
| 513 | } |
| 514 | p.expect('+') |
| 515 | p.parse_number() |
| 516 | p.expect(')') |
| 517 | case scanner.Char: |
| 518 | p.next() |
| 519 | case scanner.String: |
| 520 | p.next() |
| 521 | default: |
| 522 | p.error("expected literal") |
| 523 | } |
| 524 | |
| 525 | return name.X.(*ast.Ident).Name, &ast.GenDecl{ |
| 526 | Tok: token.CONST, |
| 527 | Specs: []ast.Spec{ |
| 528 | &ast.ValueSpec{ |
| 529 | Names: []*ast.Ident{name.Sel}, |
| 530 | Type: typ, |
| 531 | Values: []ast.Expr{&ast.BasicLit{Kind: token.INT, Value: "0"}}, |
| 532 | }, |
| 533 | }, |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | // TypeDecl = "type" ExportedName Type . |
| 538 | func (p *gc_parser) parse_type_decl() (string, *ast.GenDecl) { |
no test coverage detected