Debian Codegraph @

spdxexp/parse.go

raw symbols SCIP
1 package spdxexpscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/
2
3 import (
4 "errorsscip-go gomod github.com/golang/go/src go1.24 errors/"
5 "stringsscip-go gomod github.com/golang/go/src go1.24 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 tokenStreamscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream# struct {
14 tokensscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#tokens. []tokenscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/token#
15 indexscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#index. int
16 errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. error
17 }
18
19 func parsescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/parse().(source string) (*nodescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node#, error) {
20 if len(source) == 0 {
21 return nil, errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 errors/New().("parse error - cannot parse empty string")
22 }
23 tokens, err := scanscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/scan().(source)
24 if err != nil {
25 return nil, err
26 }
27 tokns := &tokenStreamscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#{tokensscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#tokens.: tokens, indexscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#index.: 0, errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err.: nil}
28 return tokns.parseTokensscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseTokens().(), tokns.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err.
29 }
30
31 func (t *tokenStreamscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#) parseTokensscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseTokens().() *nodescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node# {
32 if len(t.tokensscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#tokens.) == 0 {
33 // malformed with no tokens
34 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 errors/New().("no tokens to parse")
35 return nil
36 }
37
38 node := t.parseExpressionscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseExpression().()
39 if t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. != nil {
40 return nil
41 }
42
43 if node == nil {
44 // unable to parse expression for unknown reason
45 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 errors/New().("syntax error")
46 return nil
47 } else if t.hasMorescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#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.parseOperatorscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseOperator().(")")
52 if closeParen != nil {
53 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 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.parseLicensescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseLicense().()
59 if lic != nil {
60 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 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.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 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 *tokenStreamscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#) hasMorescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#hasMore().() bool {
75 return t.indexscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#index. < len(t.tokensscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#tokens.)
76 }
77
78 // Return the value of the next token without advancing the index.
79 func (t *tokenStreamscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#) peekscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#peek().() *tokenscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/token# {
80 if t.hasMorescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#hasMore().() {
81 token := t.tokensscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#tokens.[t.indexscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#index.]
82 return &token
83 }
84 return nil
85 }
86
87 // Advance the index to the next token.
88 func (t *tokenStreamscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#) nextscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#next().() {
89 if !t.hasMorescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#hasMore().() {
90 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 errors/New().("read past end of tokens")
91 return
92 }
93 t.indexscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#index.++
94 }
95
96 func (t *tokenStreamscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#) parseParenthesizedExpressionscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseParenthesizedExpression().() *nodescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node# {
97 openParen := t.parseOperatorscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseOperator().("(")
98 if openParen == nil {
99 // paren not found
100 return nil
101 }
102
103 expr := t.parseExpressionscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseExpression().()
104 if t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. != nil {
105 return nil
106 }
107
108 if !t.hasMorescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#hasMore().() {
109 // no more tokens, so missing closing paren
110 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 errors/New().("open parenthesis does not have a matching close parenthesis")
111 return nil
112 }
113
114 closeParen := t.parseOperatorscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseOperator().(")")
115 if closeParen == nil {
116 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 errors/New().("open parenthesis does not have a matching close parenthesis")
117 return nil
118 }
119
120 return expr
121 }
122
123 func (t *tokenStreamscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#) parseAtomscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseAtom().() *nodescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node# {
124 parenNode := t.parseParenthesizedExpressionscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseParenthesizedExpression().()
125 if t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. != nil {
126 return nil
127 }
128 if parenNode != nil {
129 return parenNode
130 }
131
132 refNode := t.parseLicenseRefscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseLicenseRef().()
133 if t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. != nil {
134 return nil
135 }
136 if refNode != nil {
137 return refNode
138 }
139
140 licenseNode := t.parseLicensescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseLicense().()
141 if t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#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.hasMorescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#hasMore().() {
150 // check for operators
151 operator := t.parseOperatorscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseOperator().(")")
152 if operator != nil {
153 if t.indexscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#index. == 1 {
154 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 errors/New().("expression starts with close parenthesis")
155 } else {
156 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 errors/New().("expected license or expression, but found close parenthesis")
157 }
158 return nil
159 }
160
161 operator = t.parseOperatorscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseOperator().("OR")
162 if operator != nil {
163 if t.indexscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#index. == 1 {
164 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 errors/New().("expression starts with OR")
165 } else {
166 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 errors/New().("expected license or expression, but found OR")
167 }
168 return nil
169 }
170
171 operator = t.parseOperatorscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseOperator().("AND")
172 if operator != nil {
173 if t.indexscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#index. == 1 {
174 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 errors/New().("expression starts with AND")
175 } else {
176 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 errors/New().("expected license or expression, but found AND")
177 }
178 return nil
179 }
180
181 // cannot determine what syntax error occurred
182 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 errors/New().("syntax error")
183 return nil
184 }
185
186 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 errors/New().("expected node, but found none")
187 return nil
188 }
189
190 func (t *tokenStreamscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#) parseExpressionscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseExpression().() *nodescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node# {
191 left := t.parseAndscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseAnd().()
192 if t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. != nil {
193 return nil
194 }
195 if left == nil {
196 return nil
197 }
198 if !t.hasMorescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#hasMore().() {
199 // expression found and no more tokens to process
200 return left
201 }
202
203 operator := t.parseOperatorscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseOperator().("OR")
204 if operator == nil {
205 return left
206 }
207 op := stringsscip-go gomod github.com/golang/go/src go1.24 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.24 strings/ToLower().(*operator)
208
209 if !t.hasMorescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#hasMore().() {
210 // expression found and no more tokens to process
211 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 errors/New().("expected expression following OR, but found none")
212 return nil
213 }
214
215 right := t.parseExpressionscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseExpression().()
216 if t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. != nil {
217 return nil
218 }
219 if right == nil {
220 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 errors/New().("expected expression following OR, but found none")
221 return nil
222 }
223
224 return &(nodescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node#{
225 rolescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node#role.: expressionNodescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/expressionNode.,
226 expscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node#exp.: &(expressionNodePartialscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/expressionNodePartial#{
227 leftscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/expressionNodePartial#left.: left,
228 conjunctionscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/expressionNodePartial#conjunction.: op,
229 rightscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/expressionNodePartial#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 *tokenStreamscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#) parseAndscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseAnd().() *nodescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node# {
238 left := t.parseAtomscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseAtom().()
239 if t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. != nil {
240 return nil
241 }
242 if left == nil {
243 return nil
244 }
245 if !t.hasMorescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#hasMore().() {
246 // atomic token found and no more tokens to process
247 return left
248 }
249
250 operator := t.parseOperatorscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseOperator().("AND")
251 if operator == nil {
252 return left
253 }
254
255 if !t.hasMorescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#hasMore().() {
256 // expression found and no more tokens to process
257 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 errors/New().("expected expression following AND, but found none")
258 return nil
259 }
260
261 right := t.parseAndscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseAnd().()
262 if t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. != nil {
263 return nil
264 }
265 if right == nil {
266 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 errors/New().("expected expression following AND, but found none")
267 return nil
268 }
269
270 exp := expressionNodePartialscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/expressionNodePartial#{leftscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/expressionNodePartial#left.: left, conjunctionscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/expressionNodePartial#conjunction.: "and", rightscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/expressionNodePartial#right.: right}
271
272 return &(nodescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node#{
273 rolescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node#role.: expressionNodescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/expressionNode.,
274 expscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node#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 *tokenStreamscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#) parseLicenseRefscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseLicenseRef().() *nodescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node# {
281 ref := referenceNodePartialscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/referenceNodePartial#{documentRefscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/referenceNodePartial#documentRef.: "", hasDocumentRefscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/referenceNodePartial#hasDocumentRef.: false, licenseRefscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/referenceNodePartial#licenseRef.: ""}
282
283 token := t.peekscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#peek().()
284 if token.rolescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/token#role. == documentRefTokenscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/documentRefToken. {
285 ref.documentRefscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/referenceNodePartial#documentRef. = token.valuescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/token#value.
286 ref.hasDocumentRefscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/referenceNodePartial#hasDocumentRef. = true
287 t.nextscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#next().()
288
289 operator := t.parseOperatorscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseOperator().(":")
290 if operator == nil {
291 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 errors/New().("expected ':' after 'DocumentRef-...'")
292 return nil
293 }
294 }
295
296 token = t.peekscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#peek().()
297 if token.rolescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/token#role. != licenseRefTokenscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseRefToken. && ref.hasDocumentRefscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/referenceNodePartial#hasDocumentRef. {
298 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 errors/New().("expected 'LicenseRef-...' after 'DocumentRef-...'")
299 return nil
300 } else if token.rolescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/token#role. != licenseRefTokenscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/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.licenseRefscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/referenceNodePartial#licenseRef. = token.valuescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/token#value.
306 t.nextscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#next().()
307
308 return &(nodescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node#{
309 rolescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node#role.: licenseRefNodescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseRefNode.,
310 refscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node#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 *tokenStreamscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#) parseLicensescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseLicense().() *nodescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node# {
317 token := t.peekscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#peek().()
318 if token.rolescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/token#role. != licenseTokenscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseToken. {
319 return nil
320 }
321 t.nextscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#next().()
322
323 lic := licenseNodePartialscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseNodePartial#{
324 licensescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseNodePartial#license.: token.valuescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/token#value.,
325 hasPlusscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseNodePartial#hasPlus.: false,
326 hasExceptionscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseNodePartial#hasException.: false,
327 exceptionscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseNodePartial#exception.: ""}
328
329 // for licenses that specifically support -or-later, a `+` operator token isn't expected to be present
330 if stringsscip-go gomod github.com/golang/go/src go1.24 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.24 strings/HasSuffix().(token.valuescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/token#value., "-or-later") {
331 lic.hasPlusscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseNodePartial#hasPlus. = true
332 }
333
334 if t.hasMorescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#hasMore().() {
335 // use new var idx to avoid creating a new var index
336 operator := t.parseOperatorscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseOperator().("+")
337 if operator != nil {
338 lic.hasPlusscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseNodePartial#hasPlus. = true
339 }
340
341 if t.hasMorescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#hasMore().() {
342 exception := t.parseWithscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseWith().()
343 if t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. != nil {
344 return nil
345 }
346 if exception != nil {
347 lic.hasExceptionscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseNodePartial#hasException. = true
348 lic.exceptionscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseNodePartial#exception. = *exception
349 t.nextscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#next().()
350 }
351 }
352 }
353
354 return &(nodescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node#{
355 rolescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node#role.: licenseNodescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseNode.,
356 licscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node#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 *tokenStreamscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#) parseOperatorscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseOperator().(operator string) *string {
363 token := t.peekscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#peek().()
364 if token.rolescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/token#role. == operatorTokenscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/operatorToken. && token.valuescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/token#value. == operator {
365 t.nextscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#next().()
366 return &(token.valuescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/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 *tokenStreamscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#) parseWithscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseWith().() *string {
376 operator := t.parseOperatorscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#parseOperator().("WITH")
377 if operator == nil {
378 // WITH not found is not an error
379 return nil
380 }
381
382 token := t.peekscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#peek().()
383 if token == nil || token.rolescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/token#role. != exceptionTokenscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/exceptionToken. {
384 t.errscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/tokenStream#err. = errorsscip-go gomod github.com/golang/go/src go1.24 errors/.Newscip-go gomod github.com/golang/go/src go1.24 errors/New().("expected exception after 'WITH'")
385 return nil
386 }
387
388 return &(token.valuescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/token#value.)
389 }
390
391 // Returns a human readable representation of the node tree.
392 func (n *nodescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node#) stringscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node#string().() string {
393 switch n.rolescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node#role. {
394 case expressionNodescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/expressionNode.:
395 return expressionStringscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/expressionString().(*n.expscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node#exp.)
396 case licenseNodescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseNode.:
397 return licenseStringscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseString().(*n.licscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node#lic.)
398 case licenseRefNodescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseRefNode.:
399 return referenceStringscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/referenceString().(*n.refscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node#ref.)
400 }
401 return ""
402 }
403
404 func expressionStringscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/expressionString().(exp expressionNodePartialscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/expressionNodePartial#) string {
405 s := "{ LEFT: " + exp.leftscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/expressionNodePartial#left..stringscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node#string().() + " "
406 s += exp.conjunctionscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/expressionNodePartial#conjunction.
407 s += " RIGHT: " + exp.rightscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/expressionNodePartial#right..stringscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/node#string().() + " }"
408 return s
409 }
410
411 func licenseStringscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseString().(lic licenseNodePartialscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseNodePartial#) string {
412 s := lic.licensescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseNodePartial#license.
413 if lic.hasPlusscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseNodePartial#hasPlus. {
414 s += "+"
415 }
416 if lic.hasExceptionscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseNodePartial#hasException. {
417 s += " with " + lic.exceptionscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/licenseNodePartial#exception.
418 }
419 return s
420 }
421
422 func referenceStringscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/referenceString().(ref referenceNodePartialscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/referenceNodePartial#) string {
423 s := ""
424 if ref.hasDocumentRefscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/referenceNodePartial#hasDocumentRef. {
425 s = "DocumentRef-" + ref.documentRefscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/referenceNodePartial#documentRef. + ":"
426 }
427 s += "LicenseRef-" + ref.licenseRefscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/referenceNodePartial#licenseRef.
428 return s
429 }