Debian Codegraph @

internal/parser/gen/golang/errors.go

raw symbols SCIP
1 // Copyright 2012 Vastech SA (PTY) LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 package golang
15
16 import (
17 "bytes"
18 "path"
19 "text/template"
20
21 "github.com/goccmack/gocc/internal/io"
22 )
23
24 func GenErrors(pkg, outDir string) {
25 tmpl, err := template.New("parser errors").Parse(errorsSrc[1:])
26 if err != nil {
27 panic(err)
28 }
29 wr := new(bytes.Buffer)
30 if err := tmpl.Execute(wr, path.Join(pkg, "token")); err != nil {
31 panic(err)
32 }
33 io.WriteFile(path.Join(outDir, "errors", "errors.go"), wr.Bytes())
34 }
35
36 const errorsSrc = `
37 // Code generated by gocc; DO NOT EDIT.
38
39 package errors
40
41 import (
42 "fmt"
43 "strconv"
44 "strings"
45 "unicode"
46
47 "{{.}}"
48 )
49
50 type ErrorSymbol interface {
51 }
52
53 type Error struct {
54 Err error
55 ErrorToken *token.Token
56 ErrorSymbols []ErrorSymbol
57 ExpectedTokens []string
58 StackTop int
59 }
60
61 func (e *Error) String() string {
62 w := new(strings.Builder)
63 if e.Err != nil {
64 fmt.Fprintln(w, "Error ", e.Err)
65 } else {
66 fmt.Fprintln(w, "Error")
67 }
68 fmt.Fprintf(w, "Token: type=%d, lit=%s\n", e.ErrorToken.Type, e.ErrorToken.Lit)
69 fmt.Fprintf(w, "Pos: offset=%d, line=%d, column=%d\n", e.ErrorToken.Pos.Offset, e.ErrorToken.Pos.Line, e.ErrorToken.Pos.Column)
70 fmt.Fprint(w, "Expected one of: ")
71 for _, sym := range e.ExpectedTokens {
72 fmt.Fprint(w, string(sym), " ")
73 }
74 fmt.Fprintln(w, "ErrorSymbol:")
75 for _, sym := range e.ErrorSymbols {
76 fmt.Fprintf(w, "%v\n", sym)
77 }
78
79 return w.String()
80 }
81
82 func DescribeExpected(tokens []string) string {
83 switch len(tokens) {
84 case 0:
85 return "unexpected additional tokens"
86
87 case 1:
88 return "expected " + tokens[0]
89
90 case 2:
91 return "expected either " + tokens[0] + " or " + tokens[1]
92
93 case 3:
94 // Oxford-comma rules require more than 3 items in a list for the
95 // comma to appear before the 'or'
96 return fmt.Sprintf("expected one of %s, %s or %s", tokens[0], tokens[1], tokens[2])
97
98 default:
99 // Oxford-comma separated alternatives list.
100 tokens = append(tokens[:len(tokens)-1], "or "+tokens[len(tokens)-1])
101 return "expected one of " + strings.Join(tokens, ", ")
102 }
103 }
104
105 func DescribeToken(tok *token.Token) string {
106 switch tok.Type {
107 case token.INVALID:
108 return fmt.Sprintf("unknown/invalid token %q", tok.Lit)
109 case token.EOF:
110 return "end-of-file"
111 default:
112 return fmt.Sprintf("%q", tok.Lit)
113 }
114 }
115
116 func (e *Error) Error() string {
117 // identify the line and column of the error in 'gnu' style so it can be understood
118 // by editors and IDEs; user will need to prefix it with a filename.
119 text := fmt.Sprintf("%d:%d: error: ", e.ErrorToken.Pos.Line, e.ErrorToken.Pos.Column)
120
121 // See if the error token can provide us with the filename.
122 switch src := e.ErrorToken.Pos.Context.(type) {
123 case token.Sourcer:
124 text = src.Source() + ":" + text
125 }
126
127 if e.Err != nil {
128 // Custom error specified, e.g. by << nil, errors.New("missing newline") >>
129 text += e.Err.Error()
130 } else {
131 tokens := make([]string, len(e.ExpectedTokens))
132 for idx, token := range e.ExpectedTokens {
133 if !unicode.IsLetter(rune(token[0])) {
134 token = strconv.Quote(token)
135 }
136 tokens[idx] = token
137 }
138 text += DescribeExpected(tokens)
139 actual := DescribeToken(e.ErrorToken)
140 text += fmt.Sprintf("; got: %s", actual)
141 }
142
143 return text
144 }
145 `