Merge pull request #189 from Mohit-Gaur/main
Improve command parsing and multiline string handling
This commit is contained in:
commit
5614984f06
1 changed files with 10 additions and 7 deletions
|
@ -20,7 +20,6 @@ func (c *Command) Reset() {
|
||||||
|
|
||||||
func Parse(reader io.Reader) ([]Command, error) {
|
func Parse(reader io.Reader) ([]Command, error) {
|
||||||
var commands []Command
|
var commands []Command
|
||||||
|
|
||||||
var command, modelCommand Command
|
var command, modelCommand Command
|
||||||
|
|
||||||
scanner := bufio.NewScanner(reader)
|
scanner := bufio.NewScanner(reader)
|
||||||
|
@ -47,6 +46,8 @@ func Parse(reader io.Reader) ([]Command, error) {
|
||||||
command.Name = string(fields[0])
|
command.Name = string(fields[0])
|
||||||
command.Args = string(fields[1])
|
command.Args = string(fields[1])
|
||||||
default:
|
default:
|
||||||
|
// log a warning for unknown commands
|
||||||
|
fmt.Printf("WARNING: Unknown command: %s\n", fields[0])
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,27 +56,29 @@ func Parse(reader io.Reader) ([]Command, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if modelCommand.Args == "" {
|
if modelCommand.Args == "" {
|
||||||
return nil, fmt.Errorf("no FROM line for the model was specified")
|
return nil, errors.New("no FROM line for the model was specified")
|
||||||
}
|
}
|
||||||
|
|
||||||
return commands, scanner.Err()
|
return commands, scanner.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func scanModelfile(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
func scanModelfile(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
||||||
|
const multilineString = `"""`
|
||||||
|
|
||||||
newline := bytes.IndexByte(data, '\n')
|
newline := bytes.IndexByte(data, '\n')
|
||||||
|
|
||||||
if start := bytes.Index(data, []byte(`"""`)); start >= 0 && start < newline {
|
if start := bytes.Index(data, []byte(multilineString)); start >= 0 && start < newline {
|
||||||
end := bytes.Index(data[start+3:], []byte(`"""`))
|
end := bytes.Index(data[start+len(multilineString):], []byte(multilineString))
|
||||||
if end < 0 {
|
if end < 0 {
|
||||||
if atEOF {
|
if atEOF {
|
||||||
return 0, nil, errors.New(`unterminated multiline string: """`)
|
return 0, nil, errors.New("unterminated multiline string: " + multilineString)
|
||||||
} else {
|
} else {
|
||||||
return 0, nil, nil
|
return 0, nil, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
n := start + 3 + end + 3
|
n := start + len(multilineString) + end + len(multilineString)
|
||||||
return n, bytes.Replace(data[:n], []byte(`"""`), []byte(""), 2), nil
|
return n, data[:n], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return bufio.ScanLines(data, atEOF)
|
return bufio.ScanLines(data, atEOF)
|
||||||
|
|
Loading…
Reference in a new issue