-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.go
71 lines (61 loc) · 1.5 KB
/
source.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
65
66
67
68
69
70
71
package oututil
import (
"regexp"
"strconv"
)
// Direction impies the text direction
type Direction byte
const (
// LTR is left to right
LTR Direction = iota
// RTL is right to left
RTL
)
// SourceError represents an errror in the srouce code
type SourceError struct {
File string
Line, Column int
Message string
}
// Point is a coordinate in text with a length and direction
type Point struct {
Origin int64
Length int64
Dir Direction
}
// ScanSourceError takes the log of a process and
// returns it's sourcecode errors
func ScanSourceError(message string) []SourceError {
/*
Parse a message that is almost the standard in error messages that are outputed by
most modern compilers and tools that work with source code
format is either
xxx.yyy:01:01: some message
{filename}.{fileext}:{line}:{column}: {message}
or
xxx.yyy:01: some message
{filename}.{fileext}:{line}: {message}
*/
var validMessage = regexp.MustCompile(`([[:alnum:]]+.[[:alnum:]]+):([0-9]+):([0-9]+)?:? (.*)`)
var errors []SourceError
messages := validMessage.FindAllStringSubmatch(message, -1)
for _, message := range messages {
e := SourceError{
File: message[1],
}
if line, err := strconv.Atoi(message[2]); err == nil {
e.Line = line
}
if column, err := strconv.Atoi(message[3]); err == nil {
e.Column = column
} else {
e.Column = -1
e.Message = message[3]
}
if len(message) > 3 {
e.Message = message[4]
}
errors = append(errors, e)
}
return errors
}