| 1 | package spdxexp |
| 2 | |
| 3 | /* Translation to Go from javascript code: https://github.com/clearlydefined/spdx-expression-parse.js/blob/master/scan.js */ |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "regexp" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | type expressionStream struct { |
| 13 | expression string |
| 14 | index int |
| 15 | err error |
| 16 | } |
| 17 | |
| 18 | type token struct { |
| 19 | role tokenrole |
| 20 | value string |
| 21 | } |
| 22 | |
| 23 | type tokenrole uint8 |
| 24 | |
| 25 | const ( |
| 26 | operatorToken tokenrole = iota |
| 27 | documentRefToken |
| 28 | licenseRefToken |
| 29 | licenseToken |
| 30 | exceptionToken |
| 31 | ) |
| 32 | |
| 33 | // Scan scans a string expression gathering valid SPDX expression tokens. Returns error if any tokens are invalid. |
| 34 | func scan(expression string) ([]token, error) { |
| 35 | var tokens []token |
| 36 | var token *token |
| 37 | |
| 38 | exp := &expressionStream{expression: expression, index: 0, err: nil} |
| 39 | |
| 40 | for exp.hasMore() { |
| 41 | exp.skipWhitespace() |
| 42 | if !exp.hasMore() { |
| 43 | break |
| 44 | } |
| 45 | |
| 46 | token = exp.parseToken() |
| 47 | if exp.err != nil { |
| 48 | // stop processing at first error and return |
| 49 | return nil, exp.err |
| 50 | } |
| 51 | |
| 52 | if token == nil { |
| 53 | // TODO: shouldn't happen ??? |
| 54 | return nil, errors.New("got nil token when expecting more") |
| 55 | } |
| 56 | |
| 57 | tokens = append(tokens, *token) |
| 58 | } |
| 59 | return tokens, nil |
| 60 | } |
| 61 | |
| 62 | // Determine if expression has more to process. |
| 63 | func (exp *expressionStream) hasMore() bool { |
| 64 | return exp.index < len(exp.expression) |
| 65 | } |
| 66 | |
| 67 | // Try to read the next token starting at index. Returns error if no token is recognized. |
| 68 | func (exp *expressionStream) parseToken() *token { |
| 69 | // Ordering matters |
| 70 | op := exp.readOperator() |
| 71 | if exp.err != nil { |
| 72 | return nil |
| 73 | } |
| 74 | if op != nil { |
| 75 | return op |
| 76 | } |
| 77 | |
| 78 | dref := exp.readDocumentRef() |
| 79 | if exp.err != nil { |
| 80 | return nil |
| 81 | } |
| 82 | if dref != nil { |
| 83 | return dref |
| 84 | } |
| 85 | |
| 86 | lref := exp.readLicenseRef() |
| 87 | if exp.err != nil { |
| 88 | return nil |
| 89 | } |
| 90 | if lref != nil { |
| 91 | return lref |
| 92 | } |
| 93 | |
| 94 | identifier := exp.readLicense() |
| 95 | if exp.err != nil { |
| 96 | return nil |
| 97 | } |
| 98 | if identifier != nil { |
| 99 | return identifier |
| 100 | } |
| 101 | |
| 102 | errmsg := fmt.Sprintf("unexpected '%c' at offset %d", exp.expression[exp.index], exp.index) |
| 103 | exp.err = errors.New(errmsg) |
| 104 | return nil |
| 105 | } |
| 106 | |
| 107 | // Read more from expression if the next substring starting at index matches the regex pattern. |
| 108 | func (exp *expressionStream) readRegex(pattern string) string { |
| 109 | expressionSlice := exp.expression[exp.index:] |
| 110 | |
| 111 | r, _ := regexp.Compile(pattern) |
| 112 | i := r.FindStringIndex(expressionSlice) |
| 113 | if i != nil && i[1] > 0 && i[0] == 0 { |
| 114 | // match found in expression at index |
| 115 | exp.index += i[1] |
| 116 | return expressionSlice[0:i[1]] |
| 117 | } |
| 118 | return "" |
| 119 | } |
| 120 | |
| 121 | // Read more from expression if the substring starting at index is the next expected string. |
| 122 | func (exp *expressionStream) read(next string) string { |
| 123 | expressionSlice := exp.expression[exp.index:] |
| 124 | |
| 125 | if strings.HasPrefix(expressionSlice, next) { |
| 126 | // next found in expression at index |
| 127 | exp.index += len(next) |
| 128 | return next |
| 129 | } |
| 130 | return "" |
| 131 | } |
| 132 | |
| 133 | // Skip whitespace in expression starting at index |
| 134 | func (exp *expressionStream) skipWhitespace() { |
| 135 | exp.readRegex("[ ]*") |
| 136 | } |
| 137 | |
| 138 | // Read operator in expression starting at index if it exists |
| 139 | func (exp *expressionStream) readOperator() *token { |
| 140 | possibilities := []string{"WITH", "AND", "OR", "(", ")", ":", "+"} |
| 141 | |
| 142 | var op string |
| 143 | for _, p := range possibilities { |
| 144 | op = exp.read(p) |
| 145 | if len(op) > 0 { |
| 146 | break |
| 147 | } |
| 148 | } |
| 149 | if len(op) == 0 { |
| 150 | // not an error if an operator isn't found |
| 151 | return nil |
| 152 | } |
| 153 | |
| 154 | if op == "+" && exp.index > 1 && exp.expression[exp.index-2:exp.index-1] == " " { |
| 155 | exp.err = errors.New("unexpected space before +") |
| 156 | exp.index-- |
| 157 | return nil |
| 158 | } |
| 159 | |
| 160 | return &token{role: operatorToken, value: op} |
| 161 | } |
| 162 | |
| 163 | // Get id from expression starting at index. Raise error if id not found. |
| 164 | func (exp *expressionStream) readID() string { |
| 165 | id := exp.readRegex("[A-Za-z0-9-.]+") |
| 166 | if len(id) == 0 { |
| 167 | errmsg := fmt.Sprintf("expected id at offset %d", exp.index) |
| 168 | exp.err = errors.New(errmsg) |
| 169 | return "" |
| 170 | } |
| 171 | return id |
| 172 | } |
| 173 | |
| 174 | // Read DocumentRef in expression starting at index if it exists. Raise error if found and id doesn't follow. |
| 175 | func (exp *expressionStream) readDocumentRef() *token { |
| 176 | ref := exp.read("DocumentRef-") |
| 177 | if len(ref) == 0 { |
| 178 | // not an error if a DocumentRef isn't found |
| 179 | return nil |
| 180 | } |
| 181 | |
| 182 | id := exp.readID() |
| 183 | if exp.err != nil { |
| 184 | return nil |
| 185 | } |
| 186 | return &token{role: documentRefToken, value: id} |
| 187 | } |
| 188 | |
| 189 | // Read LicenseRef in expression starting at index if it exists. Raise error if found and id doesn't follow. |
| 190 | func (exp *expressionStream) readLicenseRef() *token { |
| 191 | ref := exp.read("LicenseRef-") |
| 192 | if len(ref) == 0 { |
| 193 | // not an error if a LicenseRef isn't found |
| 194 | return nil |
| 195 | } |
| 196 | |
| 197 | id := exp.readID() |
| 198 | if exp.err != nil { |
| 199 | return nil |
| 200 | } |
| 201 | return &token{role: licenseRefToken, value: id} |
| 202 | } |
| 203 | |
| 204 | // Read a LICENSE/EXCEPTION in expression starting at index if it exists. Raise error if found and id doesn't follow. |
| 205 | func (exp *expressionStream) readLicense() *token { |
| 206 | // because readID matches broadly, save the index so it can be reset if an actual license is not found |
| 207 | index := exp.index |
| 208 | |
| 209 | license := exp.readID() |
| 210 | if exp.err != nil { |
| 211 | return nil |
| 212 | } |
| 213 | |
| 214 | if token := exp.normalizeLicense(license); token != nil { |
| 215 | return token |
| 216 | } |
| 217 | |
| 218 | // license not found in indices, need to reset index since readID advanced it |
| 219 | exp.index = index |
| 220 | errmsg := fmt.Sprintf("unknown license '%s' at offset %d", license, exp.index) |
| 221 | exp.err = errors.New(errmsg) |
| 222 | return nil |
| 223 | } |
| 224 | |
| 225 | // Generate a token using the normalized form of the license name. |
| 226 | // |
| 227 | // License name can be in the form: |
| 228 | // - a_license-2.0, a_license, a_license-ab - there is variability in the form of the base license. a_license-2.0 is used for these |
| 229 | // examples, but any base license form can have the suffixes described. |
| 230 | // - a_license-2.0-only - normalizes to a_license-2.0 if the -only form is not specifically in the set of licenses |
| 231 | // - a_license-2.0-or-later - normalizes to a_license-2.0+ if the -or-later form is not specifically in the set of licenses |
| 232 | // - a_license-2.0+ - normalizes to a_license-2.0-or-later if the -or-later form is specifically in the set of licenses |
| 233 | func (exp *expressionStream) normalizeLicense(license string) *token { |
| 234 | if token := licenseLookup(license); token != nil { |
| 235 | // checks active and exception license lists |
| 236 | // deprecated list is checked at the end to avoid a deprecated license being used for + |
| 237 | // (example: GPL-1.0 is on the deprecated list, but GPL-1.0+ should become GPL-1.0-or-later) |
| 238 | return token |
| 239 | } |
| 240 | |
| 241 | lenLicense := len(license) |
| 242 | if strings.HasSuffix(license, "-only") { |
| 243 | adjustedLicense := license[0 : lenLicense-5] |
| 244 | if token := licenseLookup(adjustedLicense); token != nil { |
| 245 | // no need to remove the -only from the expression stream; it is ignored |
| 246 | return token |
| 247 | } |
| 248 | } |
| 249 | if exp.hasMore() && exp.expression[exp.index:exp.index+1] == "+" { |
| 250 | adjustedLicense := license[0:lenLicense] + "-or-later" |
| 251 | if token := licenseLookup(adjustedLicense); token != nil { |
| 252 | // need to consume the + to avoid a + operator token being added |
| 253 | exp.index++ |
| 254 | return token |
| 255 | } |
| 256 | } |
| 257 | if strings.HasSuffix(license, "-or-later") { |
| 258 | adjustedLicense := license[0 : lenLicense-9] |
| 259 | if token := licenseLookup(adjustedLicense); token != nil { |
| 260 | // replace `-or-later` with `+` |
| 261 | newExpression := exp.expression[0:exp.index-len("-or-later")] + "+" |
| 262 | if exp.hasMore() { |
| 263 | newExpression += exp.expression[exp.index+1:] |
| 264 | } |
| 265 | exp.expression = newExpression |
| 266 | // update index to remove `-or-later`; now pointing at the `+` operator |
| 267 | exp.index -= len("-or-later") |
| 268 | |
| 269 | return token |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | return deprecatedLicenseLookup(license) |
| 274 | } |
| 275 | |
| 276 | // Lookup license identifier in active and exception lists to determine if it is a supported SPDX id |
| 277 | func licenseLookup(license string) *token { |
| 278 | active, preferredLicense := activeLicense(license) |
| 279 | if active { |
| 280 | return &token{role: licenseToken, value: preferredLicense} |
| 281 | } |
| 282 | exception, preferredLicense := exceptionLicense(license) |
| 283 | if exception { |
| 284 | return &token{role: exceptionToken, value: preferredLicense} |
| 285 | } |
| 286 | return nil |
| 287 | } |
| 288 | |
| 289 | // Lookup license identifier in deprecated list to determine if it is a supported SPDX id |
| 290 | func deprecatedLicenseLookup(license string) *token { |
| 291 | deprecated, preferredLicense := deprecatedLicense(license) |
| 292 | if deprecated { |
| 293 | return &token{role: licenseToken, value: preferredLicense} |
| 294 | } |
| 295 | return nil |
| 296 | } |