ParseLine will parse the specified string as a line of G-Codes.
(s string)
| 38 | |
| 39 | // ParseLine will parse the specified string as a line of G-Codes. |
| 40 | func ParseLine(s string) (Line, error) { |
| 41 | // prepare line |
| 42 | l := Line{} |
| 43 | |
| 44 | // extract line comment |
| 45 | if i := strings.Index(s, ";"); i >= 0 { |
| 46 | // save comment |
| 47 | l.Comment = s[i+1:] |
| 48 | |
| 49 | // reset string |
| 50 | s = strings.TrimSpace(s[:i]) |
| 51 | } |
| 52 | |
| 53 | // check string |
| 54 | if s == "" { |
| 55 | return l, nil |
| 56 | } |
| 57 | |
| 58 | // parse line |
| 59 | for s != "" { |
| 60 | // prepare code |
| 61 | c := GCode{} |
| 62 | |
| 63 | // check for word comment |
| 64 | if strings.HasPrefix(s, "(") { |
| 65 | if i := strings.Index(s, ")"); i >= 0 { |
| 66 | // save comment |
| 67 | c.Comment = s[1:i] |
| 68 | |
| 69 | // reset string |
| 70 | s = strings.TrimSpace(s[i+1:]) |
| 71 | |
| 72 | // add code |
| 73 | l.Codes = append(l.Codes, c) |
| 74 | |
| 75 | // go on |
| 76 | continue |
| 77 | } else { |
| 78 | return l, errors.New("missing ) for word comment") |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // check letter |
| 83 | if !unicode.IsUpper(rune(s[0])) { |
| 84 | return l, errors.New("expected uppercase letter to begin word") |
| 85 | } |
| 86 | |
| 87 | // get word and reset string |
| 88 | var w string |
| 89 | if i := strings.Index(s, " "); i >= 0 { |
| 90 | w = s[:i] |
| 91 | s = strings.TrimSpace(s[i+1:]) |
| 92 | } else { |
| 93 | w = s |
| 94 | s = "" |
| 95 | } |
| 96 | |
| 97 | // check length |