2023-07-17 00:02:22 +00:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2023-07-17 21:21:27 +00:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2023-07-17 00:02:22 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Command struct {
|
|
|
|
Name string
|
2023-07-17 21:21:27 +00:00
|
|
|
Args string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) Reset() {
|
|
|
|
c.Name = ""
|
|
|
|
c.Args = ""
|
2023-07-17 00:02:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Parse(reader io.Reader) ([]Command, error) {
|
|
|
|
var commands []Command
|
2023-07-17 21:21:27 +00:00
|
|
|
|
|
|
|
var command, modelCommand Command
|
2023-07-17 00:02:22 +00:00
|
|
|
|
|
|
|
scanner := bufio.NewScanner(reader)
|
2023-07-17 21:21:27 +00:00
|
|
|
scanner.Split(scanModelfile)
|
2023-07-17 00:02:22 +00:00
|
|
|
for scanner.Scan() {
|
2023-07-17 21:21:27 +00:00
|
|
|
line := scanner.Bytes()
|
|
|
|
|
|
|
|
fields := bytes.SplitN(line, []byte(" "), 2)
|
2023-07-17 00:02:22 +00:00
|
|
|
if len(fields) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-07-17 21:21:27 +00:00
|
|
|
switch string(bytes.ToUpper(fields[0])) {
|
2023-07-17 00:02:22 +00:00
|
|
|
case "FROM":
|
|
|
|
command.Name = "model"
|
2023-07-17 21:21:27 +00:00
|
|
|
command.Args = string(fields[1])
|
|
|
|
// copy command for validation
|
|
|
|
modelCommand = command
|
|
|
|
case "LICENSE", "TEMPLATE", "SYSTEM":
|
|
|
|
command.Name = string(bytes.ToLower(fields[0]))
|
|
|
|
command.Args = string(fields[1])
|
2023-07-17 00:02:22 +00:00
|
|
|
case "PARAMETER":
|
2023-07-17 21:21:27 +00:00
|
|
|
fields = bytes.SplitN(fields[1], []byte(" "), 2)
|
|
|
|
command.Name = string(fields[0])
|
|
|
|
command.Args = string(fields[1])
|
2023-07-17 00:02:22 +00:00
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
2023-07-17 21:21:27 +00:00
|
|
|
|
|
|
|
commands = append(commands, command)
|
|
|
|
command.Reset()
|
2023-07-17 00:02:22 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 21:21:27 +00:00
|
|
|
if modelCommand.Args == "" {
|
2023-07-17 00:02:22 +00:00
|
|
|
return nil, fmt.Errorf("no FROM line for the model was specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
return commands, scanner.Err()
|
|
|
|
}
|
2023-07-17 21:21:27 +00:00
|
|
|
|
|
|
|
func scanModelfile(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
|
|
|
if atEOF || len(data) == 0 {
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
newline := bytes.IndexByte(data, '\n')
|
|
|
|
|
|
|
|
if start := bytes.Index(data, []byte(`"""`)); start >= 0 && start < newline {
|
|
|
|
end := bytes.Index(data[start+3:], []byte(`"""`))
|
|
|
|
if end < 0 {
|
|
|
|
return 0, nil, errors.New(`unterminated multiline string: """`)
|
|
|
|
}
|
|
|
|
|
|
|
|
n := start + 3 + end + 3
|
|
|
|
return n, bytes.Replace(data[:n], []byte(`"""`), []byte(""), 2), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if start := bytes.Index(data, []byte(`'''`)); start >= 0 && start < newline {
|
|
|
|
end := bytes.Index(data[start+3:], []byte(`'''`))
|
|
|
|
if end < 0 {
|
|
|
|
return 0, nil, errors.New("unterminated multiline string: '''")
|
|
|
|
}
|
|
|
|
|
|
|
|
n := start + 3 + end + 3
|
|
|
|
return n, bytes.Replace(data[:n], []byte("'''"), []byte(""), 2), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return bufio.ScanLines(data, atEOF)
|
|
|
|
}
|