Skip to content

Commit 998ced7

Browse files
committed
improve error message
1 parent c8bcdd7 commit 998ced7

File tree

10 files changed

+30
-30
lines changed

10 files changed

+30
-30
lines changed

internal/decoder/array.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (d *arrayDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, rv refle
4646
case 'a':
4747
cursor++
4848
if buf[cursor] != ':' {
49-
return cursor, errors.ErrExpected("':' before array length", cursor)
49+
return cursor, errors.ErrUnexpected("':' before array length", cursor, buf[cursor])
5050
}
5151

5252
// set zero value first, php array may skip some index

internal/decoder/context.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func char(ptr unsafe.Pointer, offset int64) byte {
5656
// `:${length}:`
5757
func skipLengthWithBothColon(buf []byte, cursor int64) (int64, error) {
5858
if buf[cursor] != ':' {
59-
return cursor, errors.ErrExpected("':' before length", cursor)
59+
return cursor, errors.ErrUnexpected("':' before length", cursor, buf[cursor])
6060
}
6161
cursor++
6262

@@ -65,7 +65,7 @@ func skipLengthWithBothColon(buf []byte, cursor int64) (int64, error) {
6565
}
6666

6767
if buf[cursor] != ':' {
68-
return cursor, errors.ErrExpected("':' after length", cursor)
68+
return cursor, errors.ErrUnexpected("':' after length", cursor, buf[cursor])
6969
}
7070

7171
cursor++
@@ -185,13 +185,13 @@ func validateEmptyArray(buf []byte, cursor int64) error {
185185
}
186186

187187
if buf[cursor+1] != ':' {
188-
return errors.ErrExpected("':' before array length", cursor+1)
188+
return errors.ErrUnexpected("':' before array length", cursor+1, buf[cursor+1])
189189
}
190190
if buf[cursor+2] != '{' {
191191
return errors.ErrInvalidBeginningOfArray(buf[cursor+2], cursor+2)
192192
}
193193
if buf[cursor+3] != '}' {
194-
return errors.ErrExpected("empty array end with '}'", cursor+3)
194+
return errors.ErrUnexpected("empty array end with '}'", cursor+3, buf[cursor+3])
195195

196196
}
197197

@@ -239,11 +239,11 @@ func readString(buf []byte, cursor int64) ([]byte, int64, error) {
239239
end = end + sLen + 1
240240

241241
if buf[end] != '"' {
242-
return nil, end, errors.ErrExpected(`string quoted '"'`, end)
242+
return nil, end, errors.ErrUnexpected(`string quoted '"'`, end, buf[end])
243243
}
244244
cursor = end + 1
245245
if buf[cursor] != ';' {
246-
return nil, end, errors.ErrExpected(`string end ';'`, cursor)
246+
return nil, end, errors.ErrUnexpected(`string end ';'`, cursor, buf[cursor])
247247
}
248248

249249
cursor++

internal/decoder/float.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ func (d *floatDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, erro
2828
case 'd':
2929
break
3030
default:
31-
return nil, cursor, errors.ErrExpected("float start with 'd' or 'N'", cursor)
31+
return nil, cursor, errors.ErrUnexpected("float start with 'd' or 'N'", cursor, buf[cursor])
3232
}
3333

3434
cursor++
3535
if buf[cursor] != ':' {
36-
return nil, cursor, errors.ErrExpected("float start with 'd:'", cursor)
36+
return nil, cursor, errors.ErrUnexpected("float start with 'd:'", cursor, buf[cursor])
3737
}
3838
// cursor++
3939

@@ -57,7 +57,7 @@ func (d *floatDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, rv refle
5757
}
5858

5959
if buf[cursor] != ';' {
60-
return cursor, errors.ErrExpected("float end with ';'", cursor)
60+
return cursor, errors.ErrUnexpected("float end with ';'", cursor, buf[cursor])
6161
}
6262
cursor++
6363

internal/decoder/int.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,20 @@ var (
8686
func (d *intDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error) {
8787
b := (*sliceHeader)(unsafe.Pointer(&buf)).data
8888
if char(b, cursor) != 'i' {
89-
return nil, cursor, errors.ErrExpected("int", cursor)
89+
return nil, cursor, errors.ErrUnexpected("int", cursor, buf[cursor])
9090
}
9191

9292
cursor++
9393
if char(b, cursor) != ':' {
94-
return nil, cursor, errors.ErrExpected("int sep ':'", cursor)
94+
return nil, cursor, errors.ErrUnexpected("int sep ':'", cursor, buf[cursor])
9595
}
9696
cursor++
9797

9898
switch char(b, cursor) {
9999
case '0':
100100
cursor++
101101
if char(b, cursor) != ';' {
102-
return nil, cursor, errors.ErrExpected("';' end int", cursor)
102+
return nil, cursor, errors.ErrUnexpected("';' end int", cursor, buf[cursor])
103103
}
104104
return numZeroBuf, cursor + 1, nil
105105
case '-', '1', '2', '3', '4', '5', '6', '7', '8', '9':
@@ -109,7 +109,7 @@ func (d *intDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error)
109109
cursor++
110110
}
111111
if char(b, cursor) != ';' {
112-
return nil, cursor, errors.ErrExpected("';' end int", cursor)
112+
return nil, cursor, errors.ErrUnexpected("';' end int", cursor, buf[cursor])
113113
}
114114
num := buf[start:cursor]
115115
return num, cursor + 1, nil
@@ -155,20 +155,20 @@ func (d *intDecoder) processBytes(bytes []byte, cursor int64, rv reflect.Value)
155155
func readInt(buf []byte, cursor int64) (int, int64, error) {
156156
b := (*sliceHeader)(unsafe.Pointer(&buf)).data
157157
if char(b, cursor) != 'i' {
158-
return 0, cursor, errors.ErrExpected("'i' to start a int", cursor)
158+
return 0, cursor, errors.ErrUnexpected("'i' to start a int", cursor, buf[cursor])
159159
}
160160

161161
cursor++
162162
if char(b, cursor) != ':' {
163-
return 0, cursor, errors.ErrExpected("int sep ':'", cursor)
163+
return 0, cursor, errors.ErrUnexpected("int sep ':'", cursor, buf[cursor])
164164
}
165165
cursor++
166166

167167
switch char(b, cursor) {
168168
case '0':
169169
cursor++
170170
if char(b, cursor) != ';' {
171-
return 0, cursor, errors.ErrExpected("';' end int", cursor)
171+
return 0, cursor, errors.ErrUnexpected("';' end int", cursor, buf[cursor])
172172
}
173173
cursor++
174174
return 0, cursor, nil
@@ -180,12 +180,12 @@ func readInt(buf []byte, cursor int64) (int, int64, error) {
180180
}
181181

182182
if char(b, cursor) != ';' {
183-
return 0, cursor, errors.ErrExpected("';' end int", cursor)
183+
return 0, cursor, errors.ErrUnexpected("';' end int", cursor, buf[cursor])
184184
}
185185
value := parseByteStringInt(buf[start:cursor])
186186
cursor++
187187
return value, cursor, nil
188188
default:
189-
return 0, 0, errors.ErrExpected("int", cursor)
189+
return 0, 0, errors.ErrUnexpected("int", cursor, buf[cursor])
190190
}
191191
}

internal/decoder/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func (d *mapKeyDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, rv refl
268268
rv.Set(ptr)
269269
return cursor, nil
270270
default:
271-
return 0, errors.ErrExpected("array key", cursor)
271+
return 0, errors.ErrUnexpected("array key", cursor, buf[cursor])
272272
}
273273
}
274274

internal/decoder/map.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (d *mapDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, rv reflect
3737

3838
buflen := int64(len(buf))
3939
if buflen < 2 {
40-
return 0, errors.ErrExpected("{} for map", cursor)
40+
return 0, errors.ErrUnexpected("{} for map", cursor, buf[cursor])
4141
}
4242
switch buf[cursor] {
4343
case 'N':
@@ -69,7 +69,7 @@ func (d *mapDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, rv reflect
6969

7070
cursor = end
7171
if buf[cursor] != '{' {
72-
return 0, errors.ErrExpected("{ character for map value", cursor)
72+
return 0, errors.ErrUnexpected("{ character for map value", cursor, buf[cursor])
7373
}
7474

7575
if rv.IsNil() {

internal/decoder/slice.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (d *sliceDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, rv refle
6666
case 'a':
6767
cursor++
6868
if buf[cursor] != ':' {
69-
return cursor, errors.ErrExpected("':' before array length", cursor)
69+
return cursor, errors.ErrUnexpected("':' before array length", cursor, buf[cursor])
7070
}
7171

7272
cursor++

internal/decoder/struct.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (d *structDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, rv refl
105105

106106
// cursor++
107107
if cursor >= buflen {
108-
return 0, errors.ErrExpected("object value after colon", cursor)
108+
return 0, errors.ErrUnexpected("object value after colon", cursor, buf[cursor])
109109
}
110110
if field != nil {
111111
if field.err != nil {

internal/decoder/uint.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,20 @@ func (d *uintDecoder) parseUint(b []byte) (uint64, error) {
5757
func (d *uintDecoder) decodeBytes(buf []byte, cursor int64) ([]byte, int64, error) {
5858
b := (*sliceHeader)(unsafe.Pointer(&buf)).data
5959
if char(b, cursor) != 'i' {
60-
return nil, cursor, errors.ErrExpected("int", cursor)
60+
return nil, cursor, errors.ErrUnexpected("int", cursor, buf[cursor])
6161
}
6262

6363
cursor++
6464
if char(b, cursor) != ':' {
65-
return nil, cursor, errors.ErrExpected("int sep ':'", cursor)
65+
return nil, cursor, errors.ErrUnexpected("int sep ':'", cursor, buf[cursor])
6666
}
6767
cursor++
6868

6969
switch char(b, cursor) {
7070
case '0':
7171
cursor++
7272
if char(b, cursor) != ';' {
73-
return nil, cursor, errors.ErrExpected("';' end int", cursor)
73+
return nil, cursor, errors.ErrUnexpected("';' end int", cursor, buf[cursor])
7474
}
7575
return numZeroBuf, cursor + 1, nil
7676
case '-', '1', '2', '3', '4', '5', '6', '7', '8', '9':
@@ -80,7 +80,7 @@ func (d *uintDecoder) decodeBytes(buf []byte, cursor int64) ([]byte, int64, erro
8080
cursor++
8181
}
8282
if char(b, cursor) != ';' {
83-
return nil, cursor, errors.ErrExpected("';' end int", cursor)
83+
return nil, cursor, errors.ErrUnexpected("';' end int", cursor, buf[cursor])
8484
}
8585
num := buf[start:cursor]
8686
return num, cursor + 1, nil

internal/errors/error.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ func ErrUnexpectedEnd(msg string, cursor int64) *SyntaxError {
9191
}
9292
}
9393

94-
func ErrExpected(msg string, cursor int64) *SyntaxError {
95-
return &SyntaxError{msg: fmt.Sprintf("expected %s", msg), Offset: cursor}
94+
func ErrUnexpected(msg string, cursor int64, c byte) *SyntaxError {
95+
return &SyntaxError{msg: fmt.Sprintf("php: expecting %s, get char '%c' instead", msg, c), Offset: cursor}
9696
}
9797

9898
func ErrInvalidCharacter(c byte, context string, cursor int64) *SyntaxError {

0 commit comments

Comments
 (0)