package parser import ( "bufio" "fmt" "io" "strings" ) type Command struct { Name string Arg string } func Parse(reader io.Reader) ([]Command, error) { var commands []Command scanner := bufio.NewScanner(reader) multiline := false var multilineCommand *Command for scanner.Scan() { line := scanner.Text() if multiline { // If we're in a multiline string and the line is """, end the multiline string. if strings.TrimSpace(line) == `"""` { multiline = false commands = append(commands, *multilineCommand) } else { // Otherwise, append the line to the multiline string. multilineCommand.Arg += "\n" + line } continue } fields := strings.Fields(line) if len(fields) == 0 { continue } command := Command{} switch fields[0] { case "FROM": // TODO - support only one of FROM or MODELFILE command.Name = "image" command.Arg = fields[1] case "MODELFILE": command.Name = "model" command.Arg = fields[1] case "PROMPT": command.Name = "prompt" if fields[1] == `"""` { multiline = true multilineCommand = &command multilineCommand.Arg = "" } else { command.Arg = strings.Join(fields[1:], " ") } case "PARAMETER": command.Name = fields[1] command.Arg = strings.Join(fields[2:], " ") default: continue } if !multiline { commands = append(commands, command) } } if multiline { return nil, fmt.Errorf("unclosed multiline string") } return commands, scanner.Err() }