PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/go/types/errors.go

https://code.google.com/p/gordon-go/
Go | 81 lines | 63 code | 13 blank | 5 comment | 7 complexity | a53fa1bc9e78bf00764264cb9a057a40 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause
  1. // Copyright 2012 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. // This file implements various error reporters.
  5. package types
  6. import (
  7. "fmt"
  8. "go/ast"
  9. "go/token"
  10. "strings"
  11. )
  12. func assert(p bool) {
  13. if !p {
  14. panic("assertion failed")
  15. }
  16. }
  17. func unreachable() {
  18. panic("unreachable")
  19. }
  20. func (check *checker) sprintf(format string, args ...interface{}) string {
  21. for i, arg := range args {
  22. switch a := arg.(type) {
  23. case nil:
  24. args[i] = "<nil>"
  25. case operand:
  26. panic("internal error: should always pass *operand")
  27. case token.Pos:
  28. args[i] = check.fset.Position(a).String()
  29. case ast.Expr:
  30. args[i] = ExprString(a)
  31. }
  32. }
  33. return fmt.Sprintf(format, args...)
  34. }
  35. func (check *checker) trace(pos token.Pos, format string, args ...interface{}) {
  36. fmt.Printf("%s:\t%s%s\n",
  37. check.fset.Position(pos),
  38. strings.Repeat(". ", check.indent),
  39. check.sprintf(format, args...),
  40. )
  41. }
  42. // dump is only needed for debugging
  43. func (check *checker) dump(format string, args ...interface{}) {
  44. fmt.Println(check.sprintf(format, args...))
  45. }
  46. func (check *checker) err(pos token.Pos, msg string) {
  47. err := Error{check.fset, pos, msg}
  48. if check.firstErr == nil {
  49. check.firstErr = err
  50. }
  51. f := check.conf.Error
  52. if f == nil {
  53. panic(bailout{}) // report only first error
  54. }
  55. f(err)
  56. }
  57. func (check *checker) errorf(pos token.Pos, format string, args ...interface{}) {
  58. check.err(pos, check.sprintf(format, args...))
  59. }
  60. func (check *checker) invalidAST(pos token.Pos, format string, args ...interface{}) {
  61. check.errorf(pos, "invalid AST: "+format, args...)
  62. }
  63. func (check *checker) invalidArg(pos token.Pos, format string, args ...interface{}) {
  64. check.errorf(pos, "invalid argument: "+format, args...)
  65. }
  66. func (check *checker) invalidOp(pos token.Pos, format string, args ...interface{}) {
  67. check.errorf(pos, "invalid operation: "+format, args...)
  68. }