From f5f79049c2735bc21d83356f8c5853fd0ae80389 Mon Sep 17 00:00:00 2001 From: Mohit Gaur Date: Tue, 25 Jul 2023 22:52:23 +0530 Subject: [PATCH] Incorporate code review improvements --- parser/parser.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/parser/parser.go b/parser/parser.go index f4a4e559..189a3da4 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -2,14 +2,12 @@ package parser import ( "bufio" + "bytes" "errors" "fmt" "io" - "strings" ) -const multilineString = `"""` - type Command struct { Name string Args string @@ -34,21 +32,23 @@ func Parse(reader io.Reader) ([]Command, error) { continue } - switch strings.ToUpper(string(fields[0])) { + switch string(bytes.ToUpper(fields[0])) { case "FROM": command.Name = "model" command.Args = string(fields[1]) // copy command for validation modelCommand = command case "LICENSE", "TEMPLATE", "SYSTEM", "PROMPT": - command.Name = strings.ToLower(string(fields[0])) + command.Name = string(bytes.ToLower(fields[0])) command.Args = string(fields[1]) case "PARAMETER": fields = bytes.SplitN(fields[1], []byte(" "), 2) command.Name = string(fields[0]) command.Args = string(fields[1]) default: - return nil, fmt.Errorf("unknown command: %s", fields[0]) + // log a warning for unknown commands + fmt.Printf("WARNING: Unknown command: %s\n", fields[0]) + continue } commands = append(commands, command) @@ -63,6 +63,8 @@ func Parse(reader io.Reader) ([]Command, error) { } func scanModelfile(data []byte, atEOF bool) (advance int, token []byte, err error) { + const multilineString = `"""` + newline := bytes.IndexByte(data, '\n') if start := bytes.Index(data, []byte(multilineString)); start >= 0 && start < newline {