Debian Codegraph @

syntax/parser_arithm.go

raw
1 package syntax
2
3 // compact specifies whether we allow spaces between expressions.
4 // This is true for let
5 func (p *Parser) arithmExpr(compact bool) ArithmExpr {
6 return p.arithmExprComma(compact)
7 }
8
9 // These function names are inspired by Bash's expr.c
10
11 func (p *Parser) arithmExprComma(compact bool) ArithmExpr {
12 return p.arithmExprBinary(compact, p.arithmExprAssign, Comma)
13 }
14
15 func (p *Parser) arithmExprAssign(compact bool) ArithmExpr {
16 // Assign is different from the other binary operators because it's
17 // right-associative and needs to check that it's placed after a name
18 value := p.arithmExprTernary(compact)
19 switch BinAritOperator(p.tok) {
20 case AddAssgn, SubAssgn, MulAssgn, QuoAssgn, RemAssgn, AndAssgn,
21 OrAssgn, XorAssgn, ShlAssgn, ShrAssgn, Assgn,
22 AndBoolAssgn, OrBoolAssgn, XorBoolAssgn, PowAssgn:
23 if compact && p.spaced {
24 return value
25 }
26 if !isArithName(value) {
27 p.posErr(p.pos, "%#q must follow a name", p.tok)
28 }
29 pos := p.pos
30 tok := p.tok
31 p.nextArithOp(compact)
32 y := p.arithmExprAssign(compact)
33 if y == nil {
34 p.followErrExp(pos, tok)
35 }
36 return &BinaryArithm{
37 OpPos: pos,
38 Op: BinAritOperator(tok),
39 X: value,
40 Y: y,
41 }
42 }
43 return value
44 }
45
46 func (p *Parser) arithmExprTernary(compact bool) ArithmExpr {
47 value := p.arithmExprLor(compact)
48 if BinAritOperator(p.tok) != TernQuest || (compact && p.spaced) {
49 return value
50 }
51
52 if value == nil {
53 p.curErr("%#q must follow an expression", p.tok)
54 }
55 questPos := p.pos
56 p.nextArithOp(compact)
57 if BinAritOperator(p.tok) == TernColon {
58 p.followErrExp(questPos, TernQuest)
59 }
60 trueExpr := p.arithmExpr(compact)
61 if trueExpr == nil {
62 p.followErrExp(questPos, TernQuest)
63 }
64 if BinAritOperator(p.tok) != TernColon {
65 p.posErr(questPos, "ternary operator missing %#q after %#q", colon, quest)
66 }
67 colonPos := p.pos
68 p.nextArithOp(compact)
69 falseExpr := p.arithmExprTernary(compact)
70 if falseExpr == nil {
71 p.followErrExp(colonPos, TernColon)
72 }
73 return &BinaryArithm{
74 OpPos: questPos,
75 Op: TernQuest,
76 X: value,
77 Y: &BinaryArithm{
78 OpPos: colonPos,
79 Op: TernColon,
80 X: trueExpr,
81 Y: falseExpr,
82 },
83 }
84 }
85
86 func (p *Parser) arithmExprLor(compact bool) ArithmExpr {
87 return p.arithmExprBinary(compact, p.arithmExprLand, OrArit, XorBool)
88 }
89
90 func (p *Parser) arithmExprLand(compact bool) ArithmExpr {
91 return p.arithmExprBinary(compact, p.arithmExprBor, AndArit)
92 }
93
94 func (p *Parser) arithmExprBor(compact bool) ArithmExpr {
95 return p.arithmExprBinary(compact, p.arithmExprBxor, Or)
96 }
97
98 func (p *Parser) arithmExprBxor(compact bool) ArithmExpr {
99 return p.arithmExprBinary(compact, p.arithmExprBand, Xor)
100 }
101
102 func (p *Parser) arithmExprBand(compact bool) ArithmExpr {
103 return p.arithmExprBinary(compact, p.arithmExprEquality, And)
104 }
105
106 func (p *Parser) arithmExprEquality(compact bool) ArithmExpr {
107 return p.arithmExprBinary(compact, p.arithmExprComparison, Eql, Neq)
108 }
109
110 func (p *Parser) arithmExprComparison(compact bool) ArithmExpr {
111 return p.arithmExprBinary(compact, p.arithmExprShift, Lss, Gtr, Leq, Geq)
112 }
113
114 func (p *Parser) arithmExprShift(compact bool) ArithmExpr {
115 return p.arithmExprBinary(compact, p.arithmExprAddition, Shl, Shr)
116 }
117
118 func (p *Parser) arithmExprAddition(compact bool) ArithmExpr {
119 return p.arithmExprBinary(compact, p.arithmExprMultiplication, Add, Sub)
120 }
121
122 func (p *Parser) arithmExprMultiplication(compact bool) ArithmExpr {
123 return p.arithmExprBinary(compact, p.arithmExprPower, Mul, Quo, Rem)
124 }
125
126 func (p *Parser) arithmExprPower(compact bool) ArithmExpr {
127 // Power is different from the other binary operators because it's right-associative
128 value := p.arithmExprUnary(compact)
129 if BinAritOperator(p.tok) != Pow || (compact && p.spaced) {
130 return value
131 }
132
133 if value == nil {
134 p.curErr("%#q must follow an expression", p.tok)
135 }
136
137 op := p.tok
138 pos := p.pos
139 p.nextArithOp(compact)
140 y := p.arithmExprPower(compact)
141 if y == nil {
142 p.followErrExp(pos, op)
143 }
144 return &BinaryArithm{
145 OpPos: pos,
146 Op: BinAritOperator(op),
147 X: value,
148 Y: y,
149 }
150 }
151
152 func (p *Parser) arithmExprUnary(compact bool) ArithmExpr {
153 if !compact {
154 p.got(_Newl)
155 }
156
157 switch UnAritOperator(p.tok) {
158 case Not, BitNegation, Plus, Minus:
159 ue := &UnaryArithm{OpPos: p.pos, Op: UnAritOperator(p.tok)}
160 p.nextArithOp(compact)
161 if ue.X = p.arithmExprUnary(compact); ue.X == nil {
162 p.followErrExp(ue.OpPos, ue.Op)
163 }
164 return ue
165 }
166 return p.arithmExprValue(compact)
167 }
168
169 func (p *Parser) arithmExprValue(compact bool) ArithmExpr {
170 var x ArithmExpr
171 switch p.tok {
172 case addAdd, subSub:
173 ue := &UnaryArithm{OpPos: p.pos, Op: UnAritOperator(p.tok)}
174 p.nextArith(compact)
175 if p.tok != _LitWord {
176 p.followErr(ue.OpPos, ue.Op, noQuote("a literal"))
177 }
178 ue.X = p.arithmExprValue(compact)
179 return ue
180 case leftParen:
181 if p.quote == paramExpArithm && p.lang.in(LangZsh) {
182 x = p.zshSubFlags()
183 break
184 }
185 pe := &ParenArithm{Lparen: p.pos}
186 p.nextArithOp(compact)
187 pe.X = p.followArithm(leftParen, pe.Lparen)
188 pe.Rparen = p.matched(pe.Lparen, leftParen, rightParen)
189 if p.quote == paramExpArithm && p.tok == _LitWord {
190 p.checkLang(pe.Lparen, LangZsh, "subscript flags")
191 }
192 x = pe
193 case leftBrack:
194 p.curErr("%#q must follow a name like a[i]", p.tok)
195 case colon:
196 p.curErr("ternary operator missing %#q before %#q", quest, colon)
197 case _LitWord:
198 l := p.getLit()
199 if p.tok != leftBrack {
200 x = p.wordOne(l)
201 break
202 }
203 pe := &ParamExp{Short: true, Param: l}
204 pe.Index = p.eitherIndex()
205 x = p.wordOne(pe)
206 case bckQuote:
207 if p.quote == arithmExprLet && p.openBquotes > 0 {
208 return nil
209 }
210 fallthrough
211 default:
212 if w := p.getWord(); w != nil {
213 x = w
214 } else {
215 return nil
216 }
217 }
218
219 if compact && p.spaced {
220 return x
221 }
222 if !compact {
223 p.got(_Newl)
224 }
225
226 // we want real nil, not (*Word)(nil) as that
227 // sets the type to non-nil and then x != nil
228 if p.tok == addAdd || p.tok == subSub {
229 if !isArithName(x) {
230 p.curErr("%#q must follow a name", p.tok)
231 }
232 u := &UnaryArithm{
233 Post: true,
234 OpPos: p.pos,
235 Op: UnAritOperator(p.tok),
236 X: x,
237 }
238 p.nextArith(compact)
239 return u
240 }
241 return x
242 }
243
244 // nextArith consumes a token.
245 // It returns true if compact and the token was followed by spaces
246 func (p *Parser) nextArith(compact bool) bool {
247 p.next()
248 if compact && p.spaced {
249 return true
250 }
251 if !compact {
252 p.got(_Newl)
253 }
254 return false
255 }
256
257 func (p *Parser) nextArithOp(compact bool) {
258 pos := p.pos
259 tok := p.tok
260 if p.nextArith(compact) {
261 p.followErrExp(pos, tok)
262 }
263 }
264
265 // arithmExprBinary is used for all left-associative binary operators
266 func (p *Parser) arithmExprBinary(compact bool, nextOp func(bool) ArithmExpr, operators ...BinAritOperator) ArithmExpr {
267 value := nextOp(compact)
268 for {
269 var foundOp BinAritOperator
270 for _, op := range operators {
271 if p.tok == token(op) {
272 foundOp = op
273 break
274 }
275 }
276
277 if token(foundOp) == illegalTok || (compact && p.spaced) {
278 return value
279 }
280
281 if value == nil {
282 p.curErr("%#q must follow an expression", p.tok)
283 }
284
285 pos := p.pos
286 p.nextArithOp(compact)
287 y := nextOp(compact)
288 if y == nil {
289 p.followErrExp(pos, foundOp)
290 }
291
292 value = &BinaryArithm{
293 OpPos: pos,
294 Op: foundOp,
295 X: value,
296 Y: y,
297 }
298 }
299 }
300
301 func isArithName(left ArithmExpr) bool {
302 w, ok := left.(*Word)
303 if !ok || len(w.Parts) != 1 {
304 return false
305 }
306 switch wp := w.Parts[0].(type) {
307 case *Lit:
308 return ValidName(wp.Value)
309 case *ParamExp:
310 return wp.nakedIndex()
311 default:
312 return false
313 }
314 }
315
316 func (p *Parser) followArithm(ftok token, fpos Pos) ArithmExpr {
317 x := p.arithmExpr(false)
318 if x == nil {
319 p.followErrExp(fpos, ftok)
320 }
321 return x
322 }
323
324 func (p *Parser) peekArithmEnd() bool {
325 return p.tok == rightParen && p.r == ')'
326 }
327
328 func (p *Parser) arithmMatchingErr(pos Pos, left, right token) {
329 switch p.tok {
330 case _Lit, _LitWord:
331 p.curErr("not a valid arithmetic operator: %#q", p.val)
332 case leftBrack:
333 p.curErr("%#q must follow a name like a[i]", leftBrack)
334 case colon:
335 p.curErr("ternary operator missing %#q before %#q", quest, colon)
336 case rightParen, _EOF:
337 p.matchingErr(pos, left, right)
338 case period:
339 p.checkLang(p.pos, LangZsh, `floating point arithmetic`)
340 default:
341 if p.quote&allArithmExpr != 0 {
342 p.curErr("not a valid arithmetic operator: %#q", p.tok)
343 }
344 p.matchingErr(pos, left, right)
345 }
346 }
347
348 func (p *Parser) matchedArithm(lpos Pos, left, right token) {
349 if !p.got(right) {
350 p.arithmMatchingErr(lpos, left, right)
351 }
352 }
353
354 func (p *Parser) arithmEnd(ltok token, lpos Pos, old saveState) Pos {
355 if !p.peekArithmEnd() {
356 if p.recoverError() {
357 return recoveredPos
358 }
359 p.arithmMatchingErr(lpos, ltok, dblRightParen)
360 }
361 p.rune()
362 p.postNested(old)
363 pos := p.pos
364 p.next()
365 return pos
366 }