| 1 | package spdxexp |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strings" |
| 6 | ) |
| 7 | |
| 8 | // The ABNF grammar in the spec is totally ambiguous. |
| 9 | // |
| 10 | // This parser follows the operator precedence defined in the |
| 11 | // `Order of Precedence and Parentheses` section. |
| 12 | |
| 13 | type tokenStream struct { |
| 14 | tokens []token |
| 15 | index int |
| 16 | err error |
| 17 | } |
| 18 | |
| 19 | func parse(source string) (*node, error) { |
| 20 | if len(source) == 0 { |
| 21 | return nil, errors.New("parse error - cannot parse empty string") |
| 22 | } |
| 23 | tokens, err := scan(source) |
| 24 | if err != nil { |
| 25 | return nil, err |
| 26 | } |
| 27 | tokns := &tokenStream{tokens: tokens, index: 0, err: nil} |
| 28 | return tokns.parseTokens(), tokns.err |
| 29 | } |
| 30 | |
| 31 | func (t *tokenStream) parseTokens() *node { |
| 32 | if len(t.tokens) == 0 { |
| 33 | // malformed with no tokens |
| 34 | t.err = errors.New("no tokens to parse") |
| 35 | return nil |
| 36 | } |
| 37 | |
| 38 | node := t.parseExpression() |
| 39 | if t.err != nil { |
| 40 | return nil |
| 41 | } |
| 42 | |
| 43 | if node == nil { |
| 44 | // unable to parse expression for unknown reason |
| 45 | t.err = errors.New("syntax error") |
| 46 | return nil |
| 47 | } else if t.hasMore() { |
| 48 | // malformed with too many tokens - try to determine the cause |
| 49 | |
| 50 | // check for close parenthesis without matching open parenthesis |
| 51 | closeParen := t.parseOperator(")") |
| 52 | if closeParen != nil { |
| 53 | t.err = errors.New("close parenthesis does not have a matching open parenthesis") |
| 54 | return nil |
| 55 | } |
| 56 | |
| 57 | // check for licenses without operator |
| 58 | lic := t.parseLicense() |
| 59 | if lic != nil { |
| 60 | t.err = errors.New("licenses or expressions are not separated by an operator") |
| 61 | return nil |
| 62 | } |
| 63 | |
| 64 | // cannot determine what syntax error occurred |
| 65 | t.err = errors.New("syntax error") |
| 66 | return nil |
| 67 | } |
| 68 | |
| 69 | // all is well |
| 70 | return node |
| 71 | } |
| 72 | |
| 73 | // Return true if there is another token to process; otherwise, return false. |
| 74 | func (t *tokenStream) hasMore() bool { |
| 75 | return t.index < len(t.tokens) |
| 76 | } |
| 77 | |
| 78 | // Return the value of the next token without advancing the index. |
| 79 | func (t *tokenStream) peek() *token { |
| 80 | if t.hasMore() { |
| 81 | token := t.tokens[t.index] |
| 82 | return &token |
| 83 | } |
| 84 | return nil |
| 85 | } |
| 86 | |
| 87 | // Advance the index to the next token. |
| 88 | func (t *tokenStream) next() { |
| 89 | if !t.hasMore() { |
| 90 | t.err = errors.New("read past end of tokens") |
| 91 | return |
| 92 | } |
| 93 | t.index++ |
| 94 | } |
| 95 | |
| 96 | func (t *tokenStream) parseParenthesizedExpression() *node { |
| 97 | openParen := t.parseOperator("(") |
| 98 | if openParen == nil { |
| 99 | // paren not found |
| 100 | return nil |
| 101 | } |
| 102 | |
| 103 | expr := t.parseExpression() |
| 104 | if t.err != nil { |
| 105 | return nil |
| 106 | } |
| 107 | |
| 108 | if !t.hasMore() { |
| 109 | // no more tokens, so missing closing paren |
| 110 | t.err = errors.New("open parenthesis does not have a matching close parenthesis") |
| 111 | return nil |
| 112 | } |
| 113 | |
| 114 | closeParen := t.parseOperator(")") |
| 115 | if closeParen == nil { |
| 116 | t.err = errors.New("open parenthesis does not have a matching close parenthesis") |
| 117 | return nil |
| 118 | } |
| 119 | |
| 120 | return expr |
| 121 | } |
| 122 | |
| 123 | func (t *tokenStream) parseAtom() *node { |
| 124 | parenNode := t.parseParenthesizedExpression() |
| 125 | if t.err != nil { |
| 126 | return nil |
| 127 | } |
| 128 | if parenNode != nil { |
| 129 | return parenNode |
| 130 | } |
| 131 | |
| 132 | refNode := t.parseLicenseRef() |
| 133 | if t.err != nil { |
| 134 | return nil |
| 135 | } |
| 136 | if refNode != nil { |
| 137 | return refNode |
| 138 | } |
| 139 | |
| 140 | licenseNode := t.parseLicense() |
| 141 | if t.err != nil { |
| 142 | return nil |
| 143 | } |
| 144 | if licenseNode != nil { |
| 145 | return licenseNode |
| 146 | } |
| 147 | |
| 148 | // no atom found - try to determine the cause |
| 149 | if t.hasMore() { |
| 150 | // check for operators |
| 151 | operator := t.parseOperator(")") |
| 152 | if operator != nil { |
| 153 | if t.index == 1 { |
| 154 | t.err = errors.New("expression starts with close parenthesis") |
| 155 | } else { |
| 156 | t.err = errors.New("expected license or expression, but found close parenthesis") |
| 157 | } |
| 158 | return nil |
| 159 | } |
| 160 | |
| 161 | operator = t.parseOperator("OR") |
| 162 | if operator != nil { |
| 163 | if t.index == 1 { |
| 164 | t.err = errors.New("expression starts with OR") |
| 165 | } else { |
| 166 | t.err = errors.New("expected license or expression, but found OR") |
| 167 | } |
| 168 | return nil |
| 169 | } |
| 170 | |
| 171 | operator = t.parseOperator("AND") |
| 172 | if operator != nil { |
| 173 | if t.index == 1 { |
| 174 | t.err = errors.New("expression starts with AND") |
| 175 | } else { |
| 176 | t.err = errors.New("expected license or expression, but found AND") |
| 177 | } |
| 178 | return nil |
| 179 | } |
| 180 | |
| 181 | // cannot determine what syntax error occurred |
| 182 | t.err = errors.New("syntax error") |
| 183 | return nil |
| 184 | } |
| 185 | |
| 186 | t.err = errors.New("expected node, but found none") |
| 187 | return nil |
| 188 | } |
| 189 | |
| 190 | func (t *tokenStream) parseExpression() *node { |
| 191 | left := t.parseAnd() |
| 192 | if t.err != nil { |
| 193 | return nil |
| 194 | } |
| 195 | if left == nil { |
| 196 | return nil |
| 197 | } |
| 198 | if !t.hasMore() { |
| 199 | // expression found and no more tokens to process |
| 200 | return left |
| 201 | } |
| 202 | |
| 203 | operator := t.parseOperator("OR") |
| 204 | if operator == nil { |
| 205 | return left |
| 206 | } |
| 207 | op := strings.ToLower(*operator) |
| 208 | |
| 209 | if !t.hasMore() { |
| 210 | // expression found and no more tokens to process |
| 211 | t.err = errors.New("expected expression following OR, but found none") |
| 212 | return nil |
| 213 | } |
| 214 | |
| 215 | right := t.parseExpression() |
| 216 | if t.err != nil { |
| 217 | return nil |
| 218 | } |
| 219 | if right == nil { |
| 220 | t.err = errors.New("expected expression following OR, but found none") |
| 221 | return nil |
| 222 | } |
| 223 | |
| 224 | return &(node{ |
| 225 | role: expressionNode, |
| 226 | exp: &(expressionNodePartial{ |
| 227 | left: left, |
| 228 | conjunction: op, |
| 229 | right: right, |
| 230 | }), |
| 231 | }) |
| 232 | } |
| 233 | |
| 234 | // Return a node representation of an atomic value or an AND expression. If a malformed |
| 235 | // atomic value or expression is found, an error is returned. Advances the index if a |
| 236 | // valid atomic value or a valid expression is found. |
| 237 | func (t *tokenStream) parseAnd() *node { |
| 238 | left := t.parseAtom() |
| 239 | if t.err != nil { |
| 240 | return nil |
| 241 | } |
| 242 | if left == nil { |
| 243 | return nil |
| 244 | } |
| 245 | if !t.hasMore() { |
| 246 | // atomic token found and no more tokens to process |
| 247 | return left |
| 248 | } |
| 249 | |
| 250 | operator := t.parseOperator("AND") |
| 251 | if operator == nil { |
| 252 | return left |
| 253 | } |
| 254 | |
| 255 | if !t.hasMore() { |
| 256 | // expression found and no more tokens to process |
| 257 | t.err = errors.New("expected expression following AND, but found none") |
| 258 | return nil |
| 259 | } |
| 260 | |
| 261 | right := t.parseAnd() |
| 262 | if t.err != nil { |
| 263 | return nil |
| 264 | } |
| 265 | if right == nil { |
| 266 | t.err = errors.New("expected expression following AND, but found none") |
| 267 | return nil |
| 268 | } |
| 269 | |
| 270 | exp := expressionNodePartial{left: left, conjunction: "and", right: right} |
| 271 | |
| 272 | return &(node{ |
| 273 | role: expressionNode, |
| 274 | exp: &exp, |
| 275 | }) |
| 276 | } |
| 277 | |
| 278 | // Return a node representation of a License Reference. If a malformed license reference is |
| 279 | // found, an error is returned. Advances the index if a valid license reference is found. |
| 280 | func (t *tokenStream) parseLicenseRef() *node { |
| 281 | ref := referenceNodePartial{documentRef: "", hasDocumentRef: false, licenseRef: ""} |
| 282 | |
| 283 | token := t.peek() |
| 284 | if token.role == documentRefToken { |
| 285 | ref.documentRef = token.value |
| 286 | ref.hasDocumentRef = true |
| 287 | t.next() |
| 288 | |
| 289 | operator := t.parseOperator(":") |
| 290 | if operator == nil { |
| 291 | t.err = errors.New("expected ':' after 'DocumentRef-...'") |
| 292 | return nil |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | token = t.peek() |
| 297 | if token.role != licenseRefToken && ref.hasDocumentRef { |
| 298 | t.err = errors.New("expected 'LicenseRef-...' after 'DocumentRef-...'") |
| 299 | return nil |
| 300 | } else if token.role != licenseRefToken { |
| 301 | // not found is not an error as long as DocumentRef and : weren't the previous tokens |
| 302 | return nil |
| 303 | } |
| 304 | |
| 305 | ref.licenseRef = token.value |
| 306 | t.next() |
| 307 | |
| 308 | return &(node{ |
| 309 | role: licenseRefNode, |
| 310 | ref: &ref, |
| 311 | }) |
| 312 | } |
| 313 | |
| 314 | // Return a node representation of a License. If a malformed license is found, |
| 315 | // an error is returned. Advances the index if a valid license is found. |
| 316 | func (t *tokenStream) parseLicense() *node { |
| 317 | token := t.peek() |
| 318 | if token.role != licenseToken { |
| 319 | return nil |
| 320 | } |
| 321 | t.next() |
| 322 | |
| 323 | lic := licenseNodePartial{ |
| 324 | license: token.value, |
| 325 | hasPlus: false, |
| 326 | hasException: false, |
| 327 | exception: ""} |
| 328 | |
| 329 | // for licenses that specifically support -or-later, a `+` operator token isn't expected to be present |
| 330 | if strings.HasSuffix(token.value, "-or-later") { |
| 331 | lic.hasPlus = true |
| 332 | } |
| 333 | |
| 334 | if t.hasMore() { |
| 335 | // use new var idx to avoid creating a new var index |
| 336 | operator := t.parseOperator("+") |
| 337 | if operator != nil { |
| 338 | lic.hasPlus = true |
| 339 | } |
| 340 | |
| 341 | if t.hasMore() { |
| 342 | exception := t.parseWith() |
| 343 | if t.err != nil { |
| 344 | return nil |
| 345 | } |
| 346 | if exception != nil { |
| 347 | lic.hasException = true |
| 348 | lic.exception = *exception |
| 349 | t.next() |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | return &(node{ |
| 355 | role: licenseNode, |
| 356 | lic: &lic, |
| 357 | }) |
| 358 | } |
| 359 | |
| 360 | // Return the operator's value (e.g. AND, OR, WITH) if the current token is an OPERATOR. |
| 361 | // Advances the index if the operator is found. |
| 362 | func (t *tokenStream) parseOperator(operator string) *string { |
| 363 | token := t.peek() |
| 364 | if token.role == operatorToken && token.value == operator { |
| 365 | t.next() |
| 366 | return &(token.value) |
| 367 | } |
| 368 | // requested operator not found |
| 369 | return nil |
| 370 | } |
| 371 | |
| 372 | // Get the exception license when the WITH operator is found. |
| 373 | // Return without advancing the index if the current token is not the WITH operator. |
| 374 | // Raise an error if the WITH operator is not followed by and EXCEPTION license. |
| 375 | func (t *tokenStream) parseWith() *string { |
| 376 | operator := t.parseOperator("WITH") |
| 377 | if operator == nil { |
| 378 | // WITH not found is not an error |
| 379 | return nil |
| 380 | } |
| 381 | |
| 382 | token := t.peek() |
| 383 | if token == nil || token.role != exceptionToken { |
| 384 | t.err = errors.New("expected exception after 'WITH'") |
| 385 | return nil |
| 386 | } |
| 387 | |
| 388 | return &(token.value) |
| 389 | } |
| 390 | |
| 391 | // Returns a human readable representation of the node tree. |
| 392 | func (n *node) string() string { |
| 393 | switch n.role { |
| 394 | case expressionNode: |
| 395 | return expressionString(*n.exp) |
| 396 | case licenseNode: |
| 397 | return licenseString(*n.lic) |
| 398 | case licenseRefNode: |
| 399 | return referenceString(*n.ref) |
| 400 | } |
| 401 | return "" |
| 402 | } |
| 403 | |
| 404 | func expressionString(exp expressionNodePartial) string { |
| 405 | s := "{ LEFT: " + exp.left.string() + " " |
| 406 | s += exp.conjunction |
| 407 | s += " RIGHT: " + exp.right.string() + " }" |
| 408 | return s |
| 409 | } |
| 410 | |
| 411 | func licenseString(lic licenseNodePartial) string { |
| 412 | s := lic.license |
| 413 | if lic.hasPlus { |
| 414 | s += "+" |
| 415 | } |
| 416 | if lic.hasException { |
| 417 | s += " with " + lic.exception |
| 418 | } |
| 419 | return s |
| 420 | } |
| 421 | |
| 422 | func referenceString(ref referenceNodePartial) string { |
| 423 | s := "" |
| 424 | if ref.hasDocumentRef { |
| 425 | s = "DocumentRef-" + ref.documentRef + ":" |
| 426 | } |
| 427 | s += "LicenseRef-" + ref.licenseRef |
| 428 | return s |
| 429 | } |