-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreturn_error.go
33 lines (26 loc) · 941 Bytes
/
return_error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Copyright (c) 2017 Opsidian Ltd.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package parser
import (
"github.com/conflowio/parsley/data"
"github.com/conflowio/parsley/parsley"
)
// ReturnError will override the returned error by the parser if its position is the same as the reader's position
func ReturnError(p parsley.Parser, customErr error) Func {
return Func(func(ctx *parsley.Context, leftRecCtx data.IntMap, pos parsley.Pos) (parsley.Node, data.IntSet, parsley.Error) {
res, cp, err := p.Parse(ctx, leftRecCtx, pos)
if err != nil {
if err.Pos() == pos && parsley.IsNotFoundError(err) {
err = parsley.NewError(pos, customErr)
}
return nil, cp, err
}
if res == nil {
err = parsley.NewError(pos, customErr)
}
return res, cp, err
})
}