-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
64 lines (57 loc) · 1.57 KB
/
parser.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package ctxerr
import (
"errors"
"fmt"
"regexp"
"strconv"
)
var (
// ErrNoMatch is returned by Parse when no file path or position can be found.
ErrNoMatch = fmt.Errorf("line does not match position pattern: %s", matchRegionColon.String())
matchRegionColon = regexp.MustCompile(`^\s*(.+?):(\d+):(\d+)?:?\s*(.*)$`)
matchRegionNpm = regexp.MustCompile(`^(.+?)\[(\d+), (\d+)\]: (.*)$`)
)
// Parse a single line with at least a file path and position.
// Returns error ErrNoMatch when this does not appear to be an error line.
// Otherwise will return parsing errors or nil on success.
//
// foo.go:15:1: message
// foo.go[1, 2]: message
func Parse(line string) (*Ctx, error) {
ctx, err := parseRegionColon(line)
if err != ErrNoMatch {
return ctx, err
}
ctx, err = parseRegionNpm(line)
return ctx, err
}
func parseRegionColon(line string) (*Ctx, error) {
m := matchRegionColon.FindStringSubmatch(line)
if len(m) == 0 {
return nil, ErrNoMatch
}
// require a line number, column may be zero
lineNo, err := strconv.Atoi(m[2])
col, _ := strconv.Atoi(m[3])
if err != nil {
return nil, err
}
ctx, err := NewFromPath(m[1], Point(lineNo, col))
ctx.Err = errors.New(m[4])
return &ctx, err
}
func parseRegionNpm(line string) (*Ctx, error) {
m := matchRegionNpm.FindStringSubmatch(line)
if len(m) == 0 {
return nil, ErrNoMatch
}
// require a line number, column may be zero
lineNo, err := strconv.Atoi(m[2])
col, _ := strconv.Atoi(m[3])
if err != nil {
return nil, err
}
ctx, err := NewFromPath(m[1], Point(lineNo, col))
ctx.Err = errors.New(m[4])
return &ctx, err
}