parseRuleOrVariable gets the parsing scanner in a state where it resides on a line that could be a variable or a rule. The function parses the line and subsequent lines if there is a rule body to parse and returns an interface that is either a Variable or a Rule struct and leaves the scanner in a st
(scanner *MakefileScanner)
| 145 | // |
| 146 | //nolint:unparam // parseRuleOrVariable never returns an error yet, placeholder for future error handling |
| 147 | func parseRuleOrVariable(scanner *MakefileScanner) (ret interface{}, err error) { |
| 148 | line := scanner.Text() |
| 149 | |
| 150 | if matches := reFindSimpleVariable.FindStringSubmatch(line); matches != nil { |
| 151 | ret = Variable{ |
| 152 | Name: strings.TrimSpace(matches[1]), |
| 153 | Assignment: strings.TrimSpace(matches[2]), |
| 154 | SimplyExpanded: true, |
| 155 | FileName: scanner.FileHandle.Name(), |
| 156 | LineNumber: scanner.LineNumber, |
| 157 | } |
| 158 | scanner.Scan() |
| 159 | return |
| 160 | } |
| 161 | |
| 162 | if matches := reFindExpandedVariable.FindStringSubmatch(line); matches != nil { |
| 163 | ret = Variable{ |
| 164 | Name: strings.TrimSpace(matches[1]), |
| 165 | Assignment: strings.TrimSpace(matches[2]), |
| 166 | SimplyExpanded: false, |
| 167 | FileName: scanner.FileHandle.Name(), |
| 168 | LineNumber: scanner.LineNumber, |
| 169 | } |
| 170 | scanner.Scan() |
| 171 | return |
| 172 | } |
| 173 | if matches := reFindOtherVariable.FindStringSubmatch(line); matches != nil { |
| 174 | op := strings.TrimSpace(matches[2]) |
| 175 | isSimple := false // Default to recursive/false |
| 176 | |
| 177 | switch op { |
| 178 | case "!=": |
| 179 | // Shell assignment is immediate, like simple expansion. |
| 180 | isSimple = true |
| 181 | case "?=": |
| 182 | // Conditional assignment is recursive, just like '='. |
| 183 | isSimple = false |
| 184 | case "+=": |
| 185 | // Append ('+=') inherits its expansion behavior. If the variable was |
| 186 | // undefined, '+=' acts like '=' (recursive). Since this parser doesn't |
| 187 | // track variable history, we default to recursive (false) as the |
| 188 | // safest and most common-case behavior. |
| 189 | isSimple = false |
| 190 | } |
| 191 | |
| 192 | ret = Variable{ |
| 193 | Name: strings.TrimSpace(matches[1]), |
| 194 | Assignment: strings.TrimSpace(matches[3]), // Use index 3 for value |
| 195 | SimplyExpanded: isSimple, |
| 196 | FileName: scanner.FileHandle.Name(), |
| 197 | LineNumber: scanner.LineNumber, |
| 198 | } |
| 199 | scanner.Scan() |
| 200 | return |
| 201 | } |
| 202 | |
| 203 | if matches := reFindRule.FindStringSubmatch(line); matches != nil { |
| 204 | beginLineNumber := scanner.LineNumber - 1 |