parser.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package parser
  2. import (
  3. "bufio"
  4. "bytes"
  5. "errors"
  6. "fmt"
  7. "io"
  8. )
  9. type Command struct {
  10. Name string
  11. Args string
  12. }
  13. func (c *Command) Reset() {
  14. c.Name = ""
  15. c.Args = ""
  16. }
  17. func Parse(reader io.Reader) ([]Command, error) {
  18. var commands []Command
  19. var command, modelCommand Command
  20. scanner := bufio.NewScanner(reader)
  21. scanner.Split(scanModelfile)
  22. for scanner.Scan() {
  23. line := scanner.Bytes()
  24. fields := bytes.SplitN(line, []byte(" "), 2)
  25. if len(fields) == 0 {
  26. continue
  27. }
  28. switch string(bytes.ToUpper(fields[0])) {
  29. case "FROM":
  30. command.Name = "model"
  31. command.Args = string(fields[1])
  32. // copy command for validation
  33. modelCommand = command
  34. case "LICENSE", "TEMPLATE", "SYSTEM":
  35. command.Name = string(bytes.ToLower(fields[0]))
  36. command.Args = string(fields[1])
  37. case "PARAMETER":
  38. fields = bytes.SplitN(fields[1], []byte(" "), 2)
  39. command.Name = string(fields[0])
  40. command.Args = string(fields[1])
  41. default:
  42. continue
  43. }
  44. commands = append(commands, command)
  45. command.Reset()
  46. }
  47. if modelCommand.Args == "" {
  48. return nil, fmt.Errorf("no FROM line for the model was specified")
  49. }
  50. return commands, scanner.Err()
  51. }
  52. func scanModelfile(data []byte, atEOF bool) (advance int, token []byte, err error) {
  53. if atEOF || len(data) == 0 {
  54. return 0, nil, nil
  55. }
  56. newline := bytes.IndexByte(data, '\n')
  57. if start := bytes.Index(data, []byte(`"""`)); start >= 0 && start < newline {
  58. end := bytes.Index(data[start+3:], []byte(`"""`))
  59. if end < 0 {
  60. return 0, nil, errors.New(`unterminated multiline string: """`)
  61. }
  62. n := start + 3 + end + 3
  63. return n, bytes.Replace(data[:n], []byte(`"""`), []byte(""), 2), nil
  64. }
  65. if start := bytes.Index(data, []byte(`'''`)); start >= 0 && start < newline {
  66. end := bytes.Index(data[start+3:], []byte(`'''`))
  67. if end < 0 {
  68. return 0, nil, errors.New("unterminated multiline string: '''")
  69. }
  70. n := start + 3 + end + 3
  71. return n, bytes.Replace(data[:n], []byte("'''"), []byte(""), 2), nil
  72. }
  73. return bufio.ScanLines(data, atEOF)
  74. }