Repository navigation
Expand file tree
/
Copy pathparse.go
More file actions
151 lines (143 loc) · 3.39 KB
/
Copy pathparse.go
File metadata and controls
151 lines (143 loc) · 3.39 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package syntax
import (
"strings"
"unicode"
"unicode/utf8"
)
type parser struct {
source string
commit Commit
diagnostics []ParseError
recover bool
}
// Parse reads one Conventional Commit. Successful fields reference input without
// copying. Failure returns a zero Commit and a *ParseError, with no partial result.
func Parse(input string) (Commit, error) {
if err := checkInput(input); err != nil {
return Commit{}, err
}
end, next := lineEnd(input, 0)
p := parser{
source: input,
commit: Commit{source: input, header: Span{0, end}},
}
if err := p.parseHeader(0); err != nil {
return Commit{}, err
}
if err := p.parseSections(next); err != nil {
return Commit{}, err
}
return p.commit, nil
}
// checkInput validates bytes before structural parsing so error precedence does
// not depend on which fields the scanner needs to visit.
func checkInput(s string) *ParseError {
for i := 0; i < len(s); {
b := s[i]
if b > '\r' && b < utf8.RuneSelf {
i++
continue
}
switch {
case b == 0:
return &ParseError{InvalidCharacter, i}
case b == '\r':
if i+1 == len(s) || s[i+1] != '\n' {
return &ParseError{InvalidCharacter, i}
}
i += 2
case b < utf8.RuneSelf:
i++
default:
_, size := utf8.DecodeRuneInString(s[i:])
if size == 1 {
return &ParseError{InvalidEncoding, i}
}
i += size
}
}
return nil
}
// lineEnd excludes a validated line ending; next points beyond that ending.
func lineEnd(s string, start int) (end, next int) {
i := strings.IndexByte(s[start:], '\n')
if i < 0 {
return len(s), len(s)
}
end = start + i
next = end + 1
if end > start && s[end-1] == '\r' {
end--
}
return end, next
}
func (p *parser) parseHeader(start int) *ParseError {
s := p.source[:p.commit.header.End]
i, noun := start, false
for i < len(s) && s[i] != '(' && s[i] != '!' && s[i] != ':' {
r, size := utf8.DecodeRuneInString(s[i:])
if !nameRune(r) {
return &ParseError{InvalidType, i}
}
noun = noun || unicode.IsLetter(r) || unicode.IsNumber(r)
i += size
}
if !noun {
return &ParseError{InvalidType, i}
}
p.commit.kind = Span{start, i}
if i < len(s) && s[i] == '(' {
i++
scopeStart := i
noun = false
for i < len(s) && s[i] != ')' {
r, size := utf8.DecodeRuneInString(s[i:])
if !(nameRune(r) || (r == ' ' && i > scopeStart)) {
return &ParseError{InvalidScope, i}
}
noun = noun || unicode.IsLetter(r) || unicode.IsNumber(r)
i += size
}
if i == len(s) || !noun {
return &ParseError{InvalidScope, i}
}
if s[i-1] == ' ' {
return &ParseError{InvalidScope, i - 1}
}
p.commit.scope = Span{scopeStart, i}
i++
}
if i < len(s) && s[i] == '!' {
p.commit.bang = i
i++
}
if i == len(s) || s[i] != ':' {
return &ParseError{InvalidSeparator, i}
}
i++
if i == len(s) || s[i] != ' ' {
return &ParseError{InvalidSeparator, i}
}
i++
p.commit.sep = Span{i - 2, i}
p.commit.description = Span{i, len(s)}
if !nonblank(s[i:]) {
return &ParseError{InvalidDescription, i}
}
return nil
}
func nameRune(r rune) bool {
if r < utf8.RuneSelf {
return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') ||
(r >= '0' && r <= '9') || strings.ContainsRune("\"#%&'*,./;?@[\\]_{}", r) || r == '-'
}
return unicode.IsLetter(r) || unicode.IsMark(r) || unicode.IsNumber(r) || unicode.IsPunct(r)
}
func nonblank(s string) bool {
for _, r := range s {
if !unicode.IsSpace(r) {
return true
}
}
return false
}