Shinichiro Hamaji | 0493261 | 2015-03-31 23:46:56 +0900 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | ) |
| 6 | |
| 7 | type Rule struct { |
Shinichiro Hamaji | 5c53b57 | 2015-04-02 05:36:42 +0900 | [diff] [blame] | 8 | outputs []string |
| 9 | inputs []string |
| 10 | orderOnlyInputs []string |
| 11 | outputPatterns []string |
| 12 | isDoubleColon bool |
| 13 | isSuffixRule bool |
| 14 | cmds []string |
| 15 | filename string |
| 16 | lineno int |
| 17 | cmdLineno int |
Shinichiro Hamaji | 0493261 | 2015-03-31 23:46:56 +0900 | [diff] [blame] | 18 | } |
| 19 | |
Shinichiro Hamaji | 21a9b5f | 2015-04-01 02:42:59 +0900 | [diff] [blame] | 20 | func isPatternRule(s string) bool { |
| 21 | return strings.IndexByte(s, '%') >= 0 |
| 22 | } |
| 23 | |
Shinichiro Hamaji | 5c53b57 | 2015-04-02 05:36:42 +0900 | [diff] [blame] | 24 | func (r *Rule) parseInputs(s string) { |
| 25 | inputs := splitSpaces(s) |
| 26 | isOrderOnly := false |
| 27 | for _, input := range inputs { |
| 28 | if input == "|" { |
| 29 | isOrderOnly = true |
| 30 | continue |
| 31 | } |
| 32 | if isOrderOnly { |
| 33 | r.orderOnlyInputs = append(r.orderOnlyInputs, input) |
| 34 | } else { |
| 35 | r.inputs = append(r.inputs, input) |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
Shinichiro Hamaji | 21a9b5f | 2015-04-01 02:42:59 +0900 | [diff] [blame] | 40 | func (r *Rule) parse(line string) string { |
| 41 | index := strings.IndexByte(line, ':') |
| 42 | if index < 0 { |
| 43 | return "*** missing separator." |
Shinichiro Hamaji | 0493261 | 2015-03-31 23:46:56 +0900 | [diff] [blame] | 44 | } |
| 45 | |
Shinichiro Hamaji | 21a9b5f | 2015-04-01 02:42:59 +0900 | [diff] [blame] | 46 | first := line[:index] |
| 47 | outputs := splitSpaces(first) |
| 48 | isFirstPattern := isPatternRule(first) |
| 49 | if isFirstPattern { |
| 50 | if len(outputs) > 1 { |
| 51 | return "*** mixed implicit and normal rules: deprecated syntax" |
| 52 | } |
| 53 | r.outputPatterns = outputs |
| 54 | } else { |
| 55 | r.outputs = outputs |
| 56 | } |
| 57 | |
| 58 | index++ |
| 59 | if index < len(line) && line[index] == ':' { |
| 60 | r.isDoubleColon = true |
| 61 | index++ |
| 62 | } |
| 63 | |
| 64 | rest := line[index:] |
| 65 | index = strings.IndexByte(rest, ':') |
| 66 | if index < 0 { |
Shinichiro Hamaji | 5c53b57 | 2015-04-02 05:36:42 +0900 | [diff] [blame] | 67 | r.parseInputs(rest) |
Shinichiro Hamaji | 21a9b5f | 2015-04-01 02:42:59 +0900 | [diff] [blame] | 68 | return "" |
| 69 | } |
| 70 | |
| 71 | // %.x: %.y: %.z |
| 72 | if isFirstPattern { |
| 73 | return "*** mixed implicit and normal rules: deprecated syntax" |
| 74 | } |
| 75 | |
| 76 | second := rest[:index] |
| 77 | third := rest[index+1:] |
| 78 | |
| 79 | r.outputs = outputs |
| 80 | r.outputPatterns = splitSpaces(second) |
| 81 | if len(r.outputPatterns) == 0 { |
| 82 | return "*** missing target pattern." |
| 83 | } |
| 84 | if len(r.outputPatterns) > 1 { |
| 85 | return "*** multiple target patterns." |
| 86 | } |
| 87 | if !isPatternRule(r.outputPatterns[0]) { |
| 88 | return "*** target pattern contains no '%'." |
| 89 | } |
Shinichiro Hamaji | 5c53b57 | 2015-04-02 05:36:42 +0900 | [diff] [blame] | 90 | r.parseInputs(third) |
Shinichiro Hamaji | 21a9b5f | 2015-04-01 02:42:59 +0900 | [diff] [blame] | 91 | |
| 92 | return "" |
Shinichiro Hamaji | 0493261 | 2015-03-31 23:46:56 +0900 | [diff] [blame] | 93 | } |