| 1 | // Code generated by gocc; DO NOT EDIT. |
| 2 | |
| 3 | package errors |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "unicode" |
| 10 | |
| 11 | "github.com/goccmack/gocc/example/usercontext/token" |
| 12 | ) |
| 13 | |
| 14 | type ErrorSymbol interface { |
| 15 | } |
| 16 | |
| 17 | type Error struct { |
| 18 | Err error |
| 19 | ErrorToken *token.Token |
| 20 | ErrorSymbols []ErrorSymbol |
| 21 | ExpectedTokens []string |
| 22 | StackTop int |
| 23 | } |
| 24 | |
| 25 | func (e *Error) String() string { |
| 26 | w := new(strings.Builder) |
| 27 | if e.Err != nil { |
| 28 | fmt.Fprintln(w, "Error ", e.Err) |
| 29 | } else { |
| 30 | fmt.Fprintln(w, "Error") |
| 31 | } |
| 32 | fmt.Fprintf(w, "Token: type=%d, lit=%s\n", e.ErrorToken.Type, e.ErrorToken.Lit) |
| 33 | fmt.Fprintf(w, "Pos: offset=%d, line=%d, column=%d\n", e.ErrorToken.Pos.Offset, e.ErrorToken.Pos.Line, e.ErrorToken.Pos.Column) |
| 34 | fmt.Fprint(w, "Expected one of: ") |
| 35 | for _, sym := range e.ExpectedTokens { |
| 36 | fmt.Fprint(w, string(sym), " ") |
| 37 | } |
| 38 | fmt.Fprintln(w, "ErrorSymbol:") |
| 39 | for _, sym := range e.ErrorSymbols { |
| 40 | fmt.Fprintf(w, "%v\n", sym) |
| 41 | } |
| 42 | |
| 43 | return w.String() |
| 44 | } |
| 45 | |
| 46 | func DescribeExpected(tokens []string) string { |
| 47 | switch len(tokens) { |
| 48 | case 0: |
| 49 | return "unexpected additional tokens" |
| 50 | |
| 51 | case 1: |
| 52 | return "expected " + tokens[0] |
| 53 | |
| 54 | case 2: |
| 55 | return "expected either " + tokens[0] + " or " + tokens[1] |
| 56 | |
| 57 | case 3: |
| 58 | // Oxford-comma rules require more than 3 items in a list for the |
| 59 | // comma to appear before the 'or' |
| 60 | return fmt.Sprintf("expected one of %s, %s or %s", tokens[0], tokens[1], tokens[2]) |
| 61 | |
| 62 | default: |
| 63 | // Oxford-comma separated alternatives list. |
| 64 | tokens = append(tokens[:len(tokens)-1], "or "+tokens[len(tokens)-1]) |
| 65 | return "expected one of " + strings.Join(tokens, ", ") |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func DescribeToken(tok *token.Token) string { |
| 70 | switch tok.Type { |
| 71 | case token.INVALID: |
| 72 | return fmt.Sprintf("unknown/invalid token %q", tok.Lit) |
| 73 | case token.EOF: |
| 74 | return "end-of-file" |
| 75 | default: |
| 76 | return fmt.Sprintf("%q", tok.Lit) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func (e *Error) Error() string { |
| 81 | // identify the line and column of the error in 'gnu' style so it can be understood |
| 82 | // by editors and IDEs; user will need to prefix it with a filename. |
| 83 | text := fmt.Sprintf("%d:%d: error: ", e.ErrorToken.Pos.Line, e.ErrorToken.Pos.Column) |
| 84 | |
| 85 | // See if the error token can provide us with the filename. |
| 86 | switch src := e.ErrorToken.Pos.Context.(type) { |
| 87 | case token.Sourcer: |
| 88 | text = src.Source() + ":" + text |
| 89 | } |
| 90 | |
| 91 | if e.Err != nil { |
| 92 | // Custom error specified, e.g. by << nil, errors.New("missing newline") >> |
| 93 | text += e.Err.Error() |
| 94 | } else { |
| 95 | tokens := make([]string, len(e.ExpectedTokens)) |
| 96 | for idx, token := range e.ExpectedTokens { |
| 97 | if !unicode.IsLetter(rune(token[0])) { |
| 98 | token = strconv.Quote(token) |
| 99 | } |
| 100 | tokens[idx] = token |
| 101 | } |
| 102 | text += DescribeExpected(tokens) |
| 103 | actual := DescribeToken(e.ErrorToken) |
| 104 | text += fmt.Sprintf("; got: %s", actual) |
| 105 | } |
| 106 | |
| 107 | return text |
| 108 | } |