1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package spdx
import (
"fmt"
"io"
)
const (
opAND = "AND"
opOR = "OR"
opWITH = "WITH"
)
func isOperator(tok string) bool {
return tok == opAND || tok == opOR || tok == opWITH
}
type licenseID string
func newLicenseID(s string) (licenseID, error) {
needle := s
for _, known := range allLicenses {
if needle == known {
return licenseID(s), nil
}
}
return "", fmt.Errorf("unknown license: %s", s)
}
type licenseExceptionID string
func newLicenseExceptionID(s string) (licenseExceptionID, error) {
for _, known := range licenseExceptions {
if s == known {
return licenseExceptionID(s), nil
}
}
return "", fmt.Errorf("unknown license exception: %s", s)
}
type parser struct {
s *Scanner
}
func newParser(r io.Reader) *parser {
return &parser{s: NewScanner(r)}
}
func (p *parser) Validate() error {
return p.validate(0)
}
func (p *parser) validate(depth int) error {
last := ""
for p.s.Scan() {
tok := p.s.Text()
switch {
case tok == "(":
if last == opWITH {
return fmt.Errorf("%q not allowed after WITH", tok)
}
if err := p.validate(depth + 1); err != nil {
return err
}
if p.s.Text() != ")" {
return fmt.Errorf(`expected ")" got %q`, p.s.Text())
}
case tok == ")":
if depth == 0 {
return fmt.Errorf(`unexpected ")"`)
}
if last == "" {
return fmt.Errorf("empty expression")
}
return nil
case isOperator(tok):
if last == "" {
return fmt.Errorf("missing license before %s", tok)
}
if last == opAND || last == opOR {
return fmt.Errorf("expected license name, got %q", tok)
}
if tok == opWITH && last == "(" {
return fmt.Errorf("expected license name before %s", tok)
}
if last == opWITH {
return fmt.Errorf("expected exception name, got %q", tok)
}
default:
switch {
case last == opWITH:
if _, err := newLicenseExceptionID(tok); err != nil {
return err
}
case last == "", last == opAND, last == opOR:
if _, err := newLicenseID(tok); err != nil {
return err
}
default:
if _, err := newLicenseID(last); err == nil {
if _, err := newLicenseID(tok); err == nil {
return fmt.Errorf("missing AND or OR between %q and %q", last, tok)
}
}
return fmt.Errorf("unexpected string: %q", tok)
}
}
last = tok
}
if err := p.s.Err(); err != nil {
return err
}
if isOperator(last) {
return fmt.Errorf("missing license after %s", last)
}
if last == "" {
return fmt.Errorf("empty expression")
}
return nil
}
|