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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
// Copyright (C) 2017-2018 Luke Shumaker <lukeshu@lukeshu.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package libfastimport
import (
"strings"
"git.lukeshu.com/go/libfastimport/textproto"
"github.com/pkg/errors"
)
var parser_regularCmds = make(map[string]Cmd)
var parser_commentCmds = make(map[string]Cmd)
func parser_registerCmd(prefix string, cmd Cmd) {
switch cmd.fiCmdClass() {
case cmdClassCommand, cmdClassCommit:
parser_regularCmds[prefix] = cmd
case cmdClassComment:
parser_commentCmds[prefix] = cmd
default:
panic(errors.Errorf("invalid cmdClass: %d", cmd.fiCmdClass()))
}
}
var parser_regular func(line string) func(fiReader) (Cmd, error)
var parser_comment func(line string) func(fiReader) (Cmd, error)
func parser_compile(cmds map[string]Cmd) func(line string) func(fiReader) (Cmd, error) {
// This assumes that 2 characters is enough to uniquely
// identify a command, and that "#" is the only one-character
// command.
ch2map := make(map[string]string, len(cmds))
for prefix := range cmds {
var ch2 string
if len(prefix) < 2 {
if prefix != "#" {
panic("the assumptions of parser_compile are invalid, it must be rewritten")
}
ch2 = prefix
} else {
ch2 = prefix[:2]
}
if _, dup := ch2map[ch2]; dup {
panic("the assumptions of parser_compile are invalid, it must be rewritten")
}
ch2map[ch2] = prefix
}
return func(line string) func(fiReader) (Cmd, error) {
n := 2
if strings.HasPrefix(line, "#") {
n = 1
}
if len(line) < n {
return nil
}
prototype := cmds[ch2map[line[:n]]]
if prototype == nil {
return nil
}
return prototype.fiCmdRead
}
}
type parser struct {
fir *textproto.FIReader
inCommit bool
buf_line *string
buf_err error
ret_cmd chan Cmd
ret_err error
}
func newParser(fir *textproto.FIReader) *parser {
if parser_regular == nil {
parser_regular = parser_compile(parser_regularCmds)
}
if parser_comment == nil {
parser_comment = parser_compile(parser_commentCmds)
}
ret := &parser{
fir: fir,
ret_cmd: make(chan Cmd),
}
go func() {
ret.ret_err = ret.parse()
close(ret.ret_cmd)
}()
return ret
}
func (p *parser) ReadCmd() (Cmd, error) {
cmd, ok := <-p.ret_cmd
if !ok {
return nil, p.ret_err
}
return cmd, nil
}
func (p *parser) parse() error {
for {
line, err := p.PeekLine()
if err != nil {
return err
}
subparser := parser_regular(line)
if subparser == nil {
return UnsupportedCommand(line)
}
cmd, err := subparser(p)
if err != nil {
return err
}
switch cmd.fiCmdClass() {
case cmdClassCommand:
if p.inCommit {
p.ret_cmd <- CmdCommitEnd{}
}
_, p.inCommit = cmd.(CmdCommit)
case cmdClassCommit:
if !p.inCommit {
return errors.Errorf("Got in-commit-only command outside of a commit: %[1]T(%#[1]v)", cmd)
}
case cmdClassComment:
/* do nothing */
default:
panic(errors.Errorf("invalid cmdClass: %d", cmd.fiCmdClass()))
}
p.ret_cmd <- cmd
}
}
func (p *parser) PeekLine() (string, error) {
for p.buf_line == nil && p.buf_err == nil {
var line string
line, p.buf_err = p.fir.ReadLine()
p.buf_line = &line
if p.buf_err != nil {
return *p.buf_line, p.buf_err
}
subparser := parser_comment(line)
if subparser != nil {
var cmd Cmd
cmd, p.buf_err = subparser(p)
if p.buf_err != nil {
return "", p.buf_err
}
p.ret_cmd <- cmd
}
}
return *p.buf_line, p.buf_err
}
func (p *parser) ReadLine() (string, error) {
line, err := p.PeekLine()
p.buf_line = nil
p.buf_err = nil
return line, err
}
|