Codebase list golang-github-tdewolff-parse / 8020d83
Change NewError to accept message as a fmt with values following Taco de Wolff 4 years ago
6 changed file(s) with 30 addition(s) and 24 deletion(s). Raw diff Collapse all Expand all
11
22 import (
33 "bytes"
4 "fmt"
54 "io"
65 "strconv"
76
213212 // parse error
214213 p.initBuf()
215214 p.l.r.Move(-len(p.data))
216 p.err = parse.NewErrorLexer(fmt.Sprintf("CSS parse error: unexpected token '%s' in declaration", string(p.data)), p.l.r)
215 p.err = parse.NewErrorLexer(p.l.r, "CSS parse error: unexpected token '%s' in declaration", string(p.data))
217216 p.l.r.Move(len(p.data))
218217
219218 if p.tt == RightBraceToken {
336335 p.state = append(p.state, (*Parser).parseQualifiedRuleDeclarationList)
337336 return BeginRulesetGrammar
338337 } else if tt == ErrorToken {
339 p.err = parse.NewErrorLexer("CSS parse error: unexpected ending in qualified rule", p.l.r)
338 p.err = parse.NewErrorLexer(p.l.r, "CSS parse error: unexpected ending in qualified rule")
340339 return ErrorGrammar
341340 } else if tt == LeftParenthesisToken || tt == LeftBraceToken || tt == LeftBracketToken || tt == FunctionToken {
342341 p.level++
381380 tt, data := p.popToken(false)
382381 if tt != ColonToken {
383382 p.l.r.Move(-len(data))
384 p.err = parse.NewErrorLexer("CSS parse error: expected colon in declaration", p.l.r)
383 p.err = parse.NewErrorLexer(p.l.r, "CSS parse error: expected colon in declaration")
385384 p.l.r.Move(len(data))
386385 p.pushBuf(ttName, dataName)
387386 return p.parseDeclarationError(tt, data)
438437 p.initBuf()
439438 if tt, data := p.popToken(false); tt != ColonToken {
440439 p.l.r.Move(-len(data))
441 p.err = parse.NewErrorLexer("CSS parse error: expected colon in custom property", p.l.r)
440 p.err = parse.NewErrorLexer(p.l.r, "CSS parse error: expected colon in custom property")
442441 p.l.r.Move(len(data))
443442 return ErrorGrammar
444443 }
1515 }
1616
1717 // NewError creates a new error
18 func NewError(msg string, r io.Reader, offset int) *Error {
18 func NewError(r io.Reader, offset int, message string, a ...interface{}) *Error {
1919 line, column, context := Position(r, offset)
20 if 0 < len(a) {
21 message = fmt.Sprintf(message, a...)
22 }
2023 return &Error{
21 Message: msg,
24 Message: message,
2225 Line: line,
2326 Column: column,
2427 Context: context,
2629 }
2730
2831 // NewErrorLexer creates a new error from an active Lexer.
29 func NewErrorLexer(msg string, l *buffer.Lexer) *Error {
32 func NewErrorLexer(l *buffer.Lexer, message string, a ...interface{}) *Error {
3033 r := buffer.NewReader(l.Bytes())
3134 offset := l.Offset()
32 return NewError(msg, r, offset)
35 return NewError(r, offset, message, a...)
3336 }
3437
3538 // Positions returns the line, column, and context of the error.
88 )
99
1010 func TestError(t *testing.T) {
11 err := NewError("message", bytes.NewBufferString("buffer"), 3)
11 err := NewError(bytes.NewBufferString("buffer"), 3, "message")
1212
1313 line, column, context := err.Position()
1414 test.T(t, line, 1, "line")
2121 func TestErrorLexer(t *testing.T) {
2222 l := buffer.NewLexer(bytes.NewBufferString("buffer"))
2323 l.Move(3)
24 err := NewErrorLexer("message", l)
24 err := NewErrorLexer(l, "message")
2525
2626 line, column, context := err.Position()
2727 test.T(t, line, 1, "line")
3030
3131 test.T(t, err.Error(), "message on line 1 and column 4\n 1: buffer\n ^", "error")
3232 }
33
34 func TestErrorMessages(t *testing.T) {
35 err := NewError(bytes.NewBufferString("buffer"), 3, "message %d", 5)
36 test.T(t, err.Error(), "message 5 on line 1 and column 4\n 1: buffer\n ^", "error")
37 }
460460 }
461461 } else if c == 0 {
462462 if l.r.Err() == nil {
463 l.err = parse.NewErrorLexer("HTML parse error: unexpected null character", l.r)
463 l.err = parse.NewErrorLexer(l.r, "HTML parse error: unexpected NULL character")
464464 }
465465 return l.r.Shift()
466466 } else {
475475 break
476476 } else if c == 0 {
477477 if l.r.Err() == nil {
478 l.err = parse.NewErrorLexer("HTML parse error: unexpected null character", l.r)
478 l.err = parse.NewErrorLexer(l.r, "HTML parse error: unexpected NULL character")
479479 }
480480 return l.r.Shift()
481481 }
11 package json
22
33 import (
4 "fmt"
54 "io"
65 "strconv"
76
127126 state := p.state[len(p.state)-1]
128127 if c == ',' {
129128 if state != ArrayState && state != ObjectKeyState {
130 p.err = parse.NewErrorLexer("JSON parse error: unexpected comma character", p.r)
129 p.err = parse.NewErrorLexer(p.r, "JSON parse error: unexpected comma character")
131130 return ErrorGrammar, nil
132131 }
133132 p.r.Move(1)
138137 p.r.Skip()
139138
140139 if p.needComma && c != '}' && c != ']' && c != 0 {
141 p.err = parse.NewErrorLexer("JSON parse error: expected comma character or an array or object ending", p.r)
140 p.err = parse.NewErrorLexer(p.r, "JSON parse error: expected comma character or an array or object ending")
142141 return ErrorGrammar, nil
143142 } else if c == '{' {
144143 p.state = append(p.state, ObjectKeyState)
146145 return StartObjectGrammar, p.r.Shift()
147146 } else if c == '}' {
148147 if state != ObjectKeyState {
149 p.err = parse.NewErrorLexer("JSON parse error: unexpected right brace character", p.r)
148 p.err = parse.NewErrorLexer(p.r, "JSON parse error: unexpected right brace character")
150149 return ErrorGrammar, nil
151150 }
152151 p.needComma = true
163162 } else if c == ']' {
164163 p.needComma = true
165164 if state != ArrayState {
166 p.err = parse.NewErrorLexer("JSON parse error: unexpected right bracket character", p.r)
165 p.err = parse.NewErrorLexer(p.r, "JSON parse error: unexpected right bracket character")
167166 return ErrorGrammar, nil
168167 }
169168 p.state = p.state[:len(p.state)-1]
174173 return EndArrayGrammar, p.r.Shift()
175174 } else if state == ObjectKeyState {
176175 if c != '"' || !p.consumeStringToken() {
177 p.err = parse.NewErrorLexer("JSON parse error: expected object key to be a quoted string", p.r)
176 p.err = parse.NewErrorLexer(p.r, "JSON parse error: expected object key to be a quoted string")
178177 return ErrorGrammar, nil
179178 }
180179 n := p.r.Pos()
181180 p.moveWhitespace()
182181 if c := p.r.Peek(0); c != ':' {
183 p.err = parse.NewErrorLexer("JSON parse error: expected colon character after object key", p.r)
182 p.err = parse.NewErrorLexer(p.r, "JSON parse error: expected colon character after object key")
184183 return ErrorGrammar, nil
185184 }
186185 p.r.Move(1)
200199 }
201200 c := p.r.Peek(0) // pick up movement from consumeStringToken to detect NULL or EOF
202201 if c == 0 && p.r.Err() == nil {
203 p.err = parse.NewErrorLexer("JSON parse error: unexpected NULL character", p.r)
202 p.err = parse.NewErrorLexer(p.r, "JSON parse error: unexpected NULL character")
204203 return ErrorGrammar, nil
205204 } else if c == 0 { // EOF
206205 return ErrorGrammar, nil
207206 }
208207 }
209 p.err = parse.NewErrorLexer(fmt.Sprintf("JSON parse error: unexpected character '%c'", c), p.r)
208 p.err = parse.NewErrorLexer(p.r, "JSON parse error: unexpected character '%c'", c)
210209 return ErrorGrammar, nil
211210 }
212211
121121 }
122122 if c == 0 {
123123 if l.r.Err() == nil {
124 l.err = parse.NewErrorLexer("XML parse error: unexpected null character", l.r)
124 l.err = parse.NewErrorLexer(l.r, "XML parse error: unexpected NULL character")
125125 }
126126 return ErrorToken, nil
127127 } else if c != '>' && (c != '/' && c != '?' || l.r.Peek(1) != '>') {
179179 return TextToken, l.text
180180 }
181181 if l.r.Err() == nil {
182 l.err = parse.NewErrorLexer("XML parse error: unexpected null character", l.r)
182 l.err = parse.NewErrorLexer(l.r, "XML parse error: unexpected NULL character")
183183 }
184184 return ErrorToken, nil
185185 }