Debian Codegraph @

parser/error.go

raw symbols SCIP
1 package parser
2
3 import (
4 "fmt"
5 "sort"
6
7 "github.com/robertkrimen/otto/file"
8 "github.com/robertkrimen/otto/token"
9 )
10
11 const (
12 errUnexpectedToken = "Unexpected token %v"
13 errUnexpectedEndOfInput = "Unexpected end of input"
14 )
15
16 // UnexpectedNumber: 'Unexpected number',
17 // UnexpectedString: 'Unexpected string',
18 // UnexpectedIdentifier: 'Unexpected identifier',
19 // UnexpectedReserved: 'Unexpected reserved word',
20 // NewlineAfterThrow: 'Illegal newline after throw',
21 // InvalidRegExp: 'Invalid regular expression',
22 // UnterminatedRegExp: 'Invalid regular expression: missing /',
23 // InvalidLHSInAssignment: 'invalid left-hand side in assignment',
24 // InvalidLHSInForIn: 'Invalid left-hand side in for-in',
25 // MultipleDefaultsInSwitch: 'More than one default clause in switch statement',
26 // NoCatchOrFinally: 'Missing catch or finally after try',
27 // UnknownLabel: 'Undefined label \'%0\'',
28 // Redeclaration: '%0 \'%1\' has already been declared',
29 // IllegalContinue: 'Illegal continue statement',
30 // IllegalBreak: 'Illegal break statement',
31 // IllegalReturn: 'Illegal return statement',
32 // StrictModeWith: 'Strict mode code may not include a with statement',
33 // StrictCatchVariable: 'Catch variable may not be eval or arguments in strict mode',
34 // StrictVarName: 'Variable name may not be eval or arguments in strict mode',
35 // StrictParamName: 'Parameter name eval or arguments is not allowed in strict mode',
36 // StrictParamDupe: 'Strict mode function may not have duplicate parameter names',
37 // StrictFunctionName: 'Function name may not be eval or arguments in strict mode',
38 // StrictOctalLiteral: 'Octal literals are not allowed in strict mode.',
39 // StrictDelete: 'Delete of an unqualified identifier in strict mode.',
40 // StrictDuplicateProperty: 'Duplicate data property in object literal not allowed in strict mode',
41 // AccessorDataProperty: 'Object literal may not have data and accessor property with the same name',
42 // AccessorGetSet: 'Object literal may not have multiple get/set accessors with the same name',
43 // StrictLHSAssignment: 'Assignment to eval or arguments is not allowed in strict mode',
44 // StrictLHSPostfix: 'Postfix increment/decrement may not have eval or arguments operand in strict mode',
45 // StrictLHSPrefix: 'Prefix increment/decrement may not have eval or arguments operand in strict mode',
46 // StrictReservedWord: 'Use of future reserved word in strict mode'
47
48 // A SyntaxError is a description of an ECMAScript syntax error.
49
50 // An Error represents a parsing error. It includes the position where the error occurred and a message/description.
51 type Error struct {
52 Message string
53 Position file.Position
54 }
55
56 // FIXME Should this be "SyntaxError"?
57
58 func (e Error) Error() string {
59 filename := e.Position.Filename
60 if filename == "" {
61 filename = "(anonymous)"
62 }
63 return fmt.Sprintf("%s: Line %d:%d %s",
64 filename,
65 e.Position.Line,
66 e.Position.Column,
67 e.Message,
68 )
69 }
70
71 func (p *parser) error(place interface{}, msg string, msgValues ...interface{}) {
72 var idx file.Idx
73 switch place := place.(type) {
74 case int:
75 idx = p.idxOf(place)
76 case file.Idx:
77 if place == 0 {
78 idx = p.idxOf(p.chrOffset)
79 } else {
80 idx = place
81 }
82 default:
83 panic(fmt.Errorf("error(%T, ...)", place))
84 }
85
86 position := p.position(idx)
87 msg = fmt.Sprintf(msg, msgValues...)
88 p.errors.Add(position, msg)
89 }
90
91 func (p *parser) errorUnexpected(idx file.Idx, chr rune) {
92 if chr == -1 {
93 p.error(idx, errUnexpectedEndOfInput)
94 return
95 }
96 p.error(idx, errUnexpectedToken, token.ILLEGAL)
97 }
98
99 func (p *parser) errorUnexpectedToken(tkn token.Token) {
100 if tkn == token.EOF {
101 p.error(file.Idx(0), errUnexpectedEndOfInput)
102 return
103 }
104 value := tkn.String()
105 switch tkn {
106 case token.BOOLEAN, token.NULL:
107 p.error(p.idx, errUnexpectedToken, p.literal)
108 case token.IDENTIFIER:
109 p.error(p.idx, "Unexpected identifier")
110 case token.KEYWORD:
111 // TODO Might be a future reserved word
112 p.error(p.idx, "Unexpected reserved word")
113 case token.NUMBER:
114 p.error(p.idx, "Unexpected number")
115 case token.STRING:
116 p.error(p.idx, "Unexpected string")
117 default:
118 p.error(p.idx, errUnexpectedToken, value)
119 }
120 }
121
122 // ErrorList is a list of *Errors.
123 type ErrorList []*Error //nolint:errname
124
125 // Add adds an Error with given position and message to an ErrorList.
126 func (el *ErrorList) Add(position file.Position, msg string) {
127 *el = append(*el, &Error{Position: position, Message: msg})
128 }
129
130 // Reset resets an ErrorList to no errors.
131 func (el *ErrorList) Reset() {
132 *el = (*el)[0:0]
133 }
134
135 // Len implement sort.Interface.
136 func (el *ErrorList) Len() int {
137 return len(*el)
138 }
139
140 // Swap implement sort.Interface.
141 func (el *ErrorList) Swap(i, j int) {
142 (*el)[i], (*el)[j] = (*el)[j], (*el)[i]
143 }
144
145 // Less implement sort.Interface.
146 func (el *ErrorList) Less(i, j int) bool {
147 x := (*el)[i].Position
148 y := (*el)[j].Position
149 if x.Filename < y.Filename {
150 return true
151 }
152 if x.Filename == y.Filename {
153 if x.Line < y.Line {
154 return true
155 }
156 if x.Line == y.Line {
157 return x.Column < y.Column
158 }
159 }
160 return false
161 }
162
163 // Sort sorts el.
164 func (el *ErrorList) Sort() {
165 sort.Sort(el)
166 }
167
168 // Error implements the Error interface.
169 func (el *ErrorList) Error() string {
170 switch len(*el) {
171 case 0:
172 return "no errors"
173 case 1:
174 return (*el)[0].Error()
175 default:
176 return fmt.Sprintf("%s (and %d more errors)", (*el)[0].Error(), len(*el)-1)
177 }
178 }
179
180 // Err returns an error equivalent to this ErrorList.
181 // If the list is empty, Err returns nil.
182 func (el *ErrorList) Err() error {
183 if len(*el) == 0 {
184 return nil
185 }
186 return el
187 }