| 1 | package ssh_config |
| 2 | |
| 3 | import "fmt" |
| 4 | |
| 5 | type token struct { |
| 6 | Position |
| 7 | typ tokenType |
| 8 | val string |
| 9 | } |
| 10 | |
| 11 | func (t token) String() string { |
| 12 | switch t.typ { |
| 13 | case tokenEOF: |
| 14 | return "EOF" |
| 15 | } |
| 16 | return fmt.Sprintf("%q", t.val) |
| 17 | } |
| 18 | |
| 19 | type tokenType int |
| 20 | |
| 21 | const ( |
| 22 | eof = -(iota + 1) |
| 23 | ) |
| 24 | |
| 25 | const ( |
| 26 | tokenError tokenType = iota |
| 27 | tokenEOF |
| 28 | tokenEmptyLine |
| 29 | tokenComment |
| 30 | tokenKey |
| 31 | tokenEquals |
| 32 | tokenString |
| 33 | ) |
| 34 | |
| 35 | func isSpace(r rune) bool { |
| 36 | return r == ' ' || r == '\t' |
| 37 | } |
| 38 | |
| 39 | func isKeyStartChar(r rune) bool { |
| 40 | return !(isSpace(r) || r == '\r' || r == '\n' || r == eof) |
| 41 | } |
| 42 | |
| 43 | // I'm not sure that this is correct |
| 44 | func isKeyChar(r rune) bool { |
| 45 | // Keys start with the first character that isn't whitespace or [ and end |
| 46 | // with the last non-whitespace character before the equals sign. Keys |
| 47 | // cannot contain a # character." |
| 48 | return !(r == '\r' || r == '\n' || r == eof || r == '=') |
| 49 | } |