r - format_print.go
1 package r
2
3 import (
4 "io"
5 )
6
7 func (a AdditionExpression) printSource(w io.Writer, v bool) {
8 a.MultiplicationExpression.printSource(w, v)
9
10 if a.AdditionType != AdditionNone && a.AdditionExpression != nil {
11 if v {
12 io.WriteString(w, " ")
13 a.Comments[0].printSource(w, v)
14 }
15
16 a.AdditionType.printSource(w, v)
17
18 if v {
19 io.WriteString(w, " ")
20 a.Comments[1].printSource(w, v)
21 }
22
23 a.AdditionExpression.printSource(w, v)
24 }
25 }
26
27 func (a AndExpression) printSource(w io.Writer, v bool) {
28 a.NotExpression.printSource(w, v)
29
30 if a.AndType != AndNone && a.AndExpression != nil {
31 if v {
32 io.WriteString(w, " ")
33 a.Comments[0].printSource(w, v)
34 }
35
36 a.AndType.printSource(w, v)
37
38 if v {
39 io.WriteString(w, " ")
40 a.Comments[1].printSource(w, v)
41 }
42
43 a.AndExpression.printSource(w, v)
44 }
45 }
46
47 func (a Arg) printSource(w io.Writer, v bool) {
48 if v {
49 a.Comments[0].printSource(w, v)
50 }
51
52 if a.Ellipsis != nil {
53 io.WriteString(w, a.Ellipsis.Data)
54 } else if a.QueryExpression != nil {
55 a.QueryExpression.printSource(w, v)
56 }
57
58 if v && len(a.Comments[1]) > 0 {
59 io.WriteString(w, " ")
60 a.Comments[1].printSource(w, v)
61 }
62 }
63
64 func (a ArgList) printSource(w io.Writer, v bool) {
65 if len(a.Args) > 0 {
66 ipp := indentPrinter{w}
67
68 for n, arg := range a.Args {
69 if n > 0 {
70 if v {
71 io.WriteString(w, ", ")
72 } else {
73 io.WriteString(w, ",")
74 }
75 }
76
77 arg.printSource(&ipp, v)
78
79 if v && (arg.Default == nil && len(arg.Comments[1]) > 0 || arg.Default != nil && len(arg.Default.Comments[1]) > 0) {
80 if n == len(a.Args)-1 {
81 io.WriteString(w, "\n")
82 } else {
83 ipp.WriteString("\n")
84 }
85 }
86 }
87 } else if v && len(a.Comments) > 0 {
88 ipp := indentPrinter{w}
89
90 io.WriteString(&ipp, "\n")
91 a.Comments.printSource(&ipp, false)
92 io.WriteString(w, "\n")
93 }
94 }
95
96 func (a Argument) printSource(w io.Writer, v bool) {
97 if a.Identifier != nil {
98 if v {
99 a.Comments[0].printSource(w, v)
100 }
101
102 io.WriteString(w, a.Identifier.Data)
103
104 if v && len(a.Comments[1]) > 0 {
105 io.WriteString(w, " ")
106 a.Comments[1].printSource(w, false)
107 }
108
109 if a.Identifier.Type == TokenIdentifier && a.Default != nil {
110 if v {
111 if len(a.Comments[1]) > 0 {
112 io.WriteString(w, "\n= ")
113 } else {
114 io.WriteString(w, " = ")
115 }
116 } else {
117 io.WriteString(w, "=")
118 }
119
120 a.Default.printSource(w, v)
121 }
122 }
123 }
124
125 func (a AssignmentExpression) printSource(w io.Writer, v bool) {
126 a.FormulaeExpression.printSource(w, v)
127
128 if a.AssignmentType != AssignmentNone && a.AssignmentExpression != nil {
129 if v {
130 io.WriteString(w, " ")
131 a.Comments[0].printSource(w, v)
132 }
133
134 a.AssignmentType.printSource(w, v)
135
136 if v {
137 io.WriteString(w, " ")
138 a.Comments[1].printSource(w, v)
139 }
140
141 a.AssignmentExpression.printSource(w, v)
142 }
143 }
144
145 func (c Call) printSource(w io.Writer, v bool) {
146 io.WriteString(w, "(")
147
148 if len(c.Args) > 0 {
149 c.Args[0].printSource(w, v)
150
151 for _, a := range c.Args[1:] {
152 if v {
153 io.WriteString(w, ", ")
154 } else {
155 io.WriteString(w, ",")
156 }
157
158 a.printSource(w, v)
159 }
160 } else if v && len(c.Comments) > 0 {
161 ipp := indentPrinter{w}
162
163 io.WriteString(&ipp, "\n")
164 c.Comments.printSource(&ipp, false)
165 io.WriteString(w, "\n")
166 }
167
168 io.WriteString(w, ")")
169 }
170
171 func (c CompoundExpression) printSource(w io.Writer, v bool) {
172 io.WriteString(w, "{")
173
174 if len(c.Expressions) > 0 {
175 ipp := indentPrinter{w}
176
177 for _, e := range c.Expressions {
178 io.WriteString(&ipp, "\n")
179 e.printSource(&ipp, v)
180 }
181
182 io.WriteString(w, "\n")
183 }
184
185 if v && len(c.Comments) > 0 {
186 ipp := indentPrinter{w}
187
188 io.WriteString(&ipp, "\n")
189 c.Comments.printSource(&ipp, false)
190 io.WriteString(w, "\n")
191 }
192
193 io.WriteString(w, "}")
194 }
195
196 func (e ExponentiationExpression) printSource(w io.Writer, v bool) {
197 e.SubsetExpression.printSource(w, v)
198
199 if e.ExponentiationExpression != nil {
200 if v && len(e.Comments[0]) > 0 {
201 io.WriteString(w, " ")
202 e.Comments[0].printSource(w, v)
203 }
204
205 io.WriteString(w, "^")
206
207 if v && len(e.Comments[1]) > 0 {
208 io.WriteString(w, " ")
209 e.Comments[1].printSource(w, v)
210 }
211
212 e.ExponentiationExpression.printSource(w, v)
213 }
214 }
215
216 func (e Expression) printSource(w io.Writer, v bool) {
217 if v {
218 e.Comments[0].printSource(w, v)
219 }
220
221 if e.FlowControl != nil {
222 e.FlowControl.printSource(w, v)
223 } else if e.FunctionDefinition != nil {
224 e.FunctionDefinition.printSource(w, v)
225 } else if e.QueryExpression != nil {
226 e.QueryExpression.printSource(w, v)
227 }
228
229 if v && e.Comments[1] != nil {
230 io.WriteString(w, " ")
231 e.Comments[1].printSource(w, false)
232 }
233 }
234
235 func (f File) printSource(w io.Writer, v bool) {
236 for _, e := range f.Statements {
237 e.printSource(w, v)
238 io.WriteString(w, "\n")
239 }
240
241 if v && len(f.Comments) > 0 {
242 io.WriteString(w, "\n")
243 f.Comments.printSource(w, v)
244 }
245 }
246
247 func (f FlowControl) printSource(w io.Writer, v bool) {
248 if f.IfControl != nil {
249 f.IfControl.printSource(w, v)
250 } else if f.WhileControl != nil {
251 f.WhileControl.printSource(w, v)
252 } else if f.RepeatControl != nil {
253 f.RepeatControl.printSource(w, v)
254 } else if f.ForControl != nil {
255 f.ForControl.printSource(w, v)
256 }
257 }
258
259 func (f ForControl) printSource(w io.Writer, v bool) {
260 if f.Var != nil && f.Var.Type == TokenIdentifier {
261 ipp := indentPrinter{w}
262
263 if v {
264 io.WriteString(w, "for ")
265
266 f.Comments[0].printSource(w, v)
267 io.WriteString(w, "(")
268 } else {
269 io.WriteString(w, "for(")
270 }
271
272 if v {
273 f.Comments[1].printSource(&ipp, v)
274 }
275
276 io.WriteString(w, f.Var.Data)
277 io.WriteString(w, " ")
278
279 if v {
280 f.Comments[2].printSource(&ipp, v)
281 }
282
283 io.WriteString(w, "in")
284 io.WriteString(w, " ")
285
286 if v {
287 f.Comments[3].printSource(&ipp, v)
288 }
289
290 f.List.printSource(&ipp, v)
291
292 if v && len(f.Comments[4]) > 0 {
293 io.WriteString(w, " ")
294 f.Comments[4].printSource(&ipp, false)
295 io.WriteString(w, "\n")
296 }
297
298 if v {
299 io.WriteString(w, ") ")
300 } else {
301 io.WriteString(w, ")")
302 }
303
304 f.Expr.printSource(w, v)
305 }
306 }
307
308 func (f FormulaeExpression) printSource(w io.Writer, v bool) {
309 if f.OrExpression != nil {
310 f.OrExpression.printSource(w, v)
311
312 if v && f.FormulaeExpression != nil {
313 io.WriteString(w, " ")
314 }
315 }
316
317 if f.FormulaeExpression != nil {
318 io.WriteString(w, "~")
319
320 if v {
321 io.WriteString(w, " ")
322 f.Comments.printSource(w, v)
323 }
324
325 f.FormulaeExpression.printSource(w, v)
326 }
327 }
328
329 func (f FunctionDefinition) printSource(w io.Writer, v bool) {
330 io.WriteString(w, "function")
331
332 if v && len(f.Comments) > 0 {
333 ipp := indentPrinter{w}
334
335 io.WriteString(w, " ")
336 f.Comments.printSource(&ipp, false)
337 io.WriteString(w, "\n")
338 }
339
340 io.WriteString(w, "(")
341
342 f.ArgList.printSource(w, v)
343
344 if v {
345 io.WriteString(w, ") ")
346 } else {
347 io.WriteString(w, ")")
348 }
349
350 f.Body.printSource(w, v)
351 }
352
353 func (i IfControl) printSource(w io.Writer, v bool) {
354 ipp := indentPrinter{w}
355
356 if v {
357 io.WriteString(w, "if ")
358
359 if len(i.Comments[0]) > 0 {
360 i.Comments[0].printSource(&ipp, false)
361 io.WriteString(w, "\n(")
362 } else {
363 io.WriteString(w, "(")
364 }
365
366 i.Comments[1].printSource(&ipp, v)
367 } else {
368 io.WriteString(w, "if(")
369 }
370
371 i.Cond.printSource(w, v)
372
373 if v {
374 if len(i.Comments[2]) > 0 {
375 i.Comments[2].printSource(&ipp, false)
376 io.WriteString(w, "\n")
377 }
378
379 io.WriteString(w, ") ")
380 } else {
381 io.WriteString(w, ")")
382 }
383
384 i.Expr.printSource(w, v)
385
386 if i.Else != nil {
387 if v && (len(i.Expr.Comments[1]) > 0 || len(i.Comments[3]) > 0) {
388 io.WriteString(w, "\n")
389 }
390
391 if v && len(i.Comments[3]) > 0 {
392 io.WriteString(w, "\n")
393 i.Comments[3].printSource(w, v)
394 io.WriteString(w, "else ")
395 } else {
396 io.WriteString(w, " else ")
397 }
398
399 i.Else.printSource(w, v)
400 }
401 }
402
403 func (i Index) printSource(w io.Writer, v bool) {
404 if !i.Double {
405 io.WriteString(w, "[")
406
407 if len(i.Args) > 0 {
408 i.Args[0].printSource(w, v)
409
410 for _, a := range i.Args[1:] {
411 if v {
412 io.WriteString(w, ", ")
413 } else {
414 io.WriteString(w, ",")
415 }
416
417 a.printSource(w, v)
418 }
419 }
420
421 io.WriteString(w, "]")
422 } else if len(i.Args) == 1 {
423 io.WriteString(w, "[[")
424 i.Args[0].printSource(w, v)
425 io.WriteString(w, "]]")
426 }
427 }
428
429 func (i IndexExpression) printSource(w io.Writer, v bool) {
430 if v {
431 i.Comments[0].printSource(w, v)
432 }
433
434 i.QueryExpression.printSource(w, v)
435
436 if v && len(i.Comments[1]) > 0 {
437 io.WriteString(w, " ")
438 i.Comments[1].printSource(w, v)
439 }
440 }
441
442 func (i IndexOrCallExpression) printSource(w io.Writer, v bool) {
443 if i.SimpleExpression != nil {
444 i.SimpleExpression.printSource(w, v)
445 } else if i.IndexOrCallExpression != nil {
446 i.IndexOrCallExpression.printSource(w, v)
447
448 if v && len(i.Comments) > 0 {
449 io.WriteString(w, " ")
450 i.Comments.printSource(w, v)
451 }
452
453 if i.Index != nil {
454 i.Index.printSource(w, v)
455 } else if i.Call != nil {
456 i.Call.printSource(w, v)
457 }
458 }
459 }
460
461 func (m MultiplicationExpression) printSource(w io.Writer, v bool) {
462 m.PipeOrSpecialExpression.printSource(w, v)
463
464 if m.MultiplicationType != MultiplicationNone && m.MultiplicationExpression != nil {
465 if v {
466 io.WriteString(w, " ")
467 m.Comments[0].printSource(w, v)
468 }
469
470 m.MultiplicationType.printSource(w, v)
471
472 if v {
473 io.WriteString(w, " ")
474 m.Comments[1].printSource(w, v)
475 }
476
477 m.MultiplicationExpression.printSource(w, v)
478 }
479 }
480
481 func (n NotExpression) printSource(w io.Writer, v bool) {
482 for m := range n.Nots {
483 io.WriteString(w, "!")
484
485 if v && uint(len(n.Comments)) > m && len(n.Comments[m]) > 0 {
486 io.WriteString(w, " ")
487 n.Comments[m].printSource(w, v)
488 }
489 }
490
491 n.RelationalExpression.printSource(w, v)
492 }
493
494 func (o OrExpression) printSource(w io.Writer, v bool) {
495 o.AndExpression.printSource(w, v)
496
497 if o.OrType != OrNone && o.OrExpression != nil {
498 if v {
499 io.WriteString(w, " ")
500 o.Comments[0].printSource(w, v)
501 }
502
503 o.OrType.printSource(w, v)
504
505 if v {
506 io.WriteString(w, " ")
507 o.Comments[1].printSource(w, v)
508 }
509
510 o.OrExpression.printSource(w, v)
511 }
512 }
513
514 func (p ParenthesizedExpression) printSource(w io.Writer, v bool) {
515 io.WriteString(w, "(")
516 p.Expression.printSource(w, v)
517
518 if v && len(p.Expression.Comments[1]) > 0 {
519 io.WriteString(w, "\n")
520 }
521
522 io.WriteString(w, ")")
523 }
524
525 func (p PipeOrSpecialExpression) printSource(w io.Writer, v bool) {
526 p.SequenceExpression.printSource(w, v)
527
528 if p.Operator != nil && p.PipeOrSpecialExpression != nil {
529 if v {
530 io.WriteString(w, " ")
531 p.Comments[0].printSource(w, v)
532 }
533
534 io.WriteString(w, p.Operator.Data)
535
536 if v {
537 io.WriteString(w, " ")
538 p.Comments[1].printSource(w, v)
539 }
540
541 p.PipeOrSpecialExpression.printSource(w, v)
542 }
543 }
544
545 func (q QueryExpression) printSource(w io.Writer, v bool) {
546 if q.AssignmentExpression != nil {
547 q.AssignmentExpression.printSource(w, v)
548 }
549
550 if q.QueryExpression != nil {
551 if v && q.AssignmentExpression != nil {
552 io.WriteString(w, " ")
553
554 q.Comments[0].printSource(w, v)
555 }
556
557 io.WriteString(w, "?")
558
559 if v {
560 if q.QueryExpression.AssignmentExpression != nil {
561 io.WriteString(w, " ")
562 }
563
564 if len(q.Comments) > 0 {
565 q.Comments[1].printSource(w, v)
566 }
567 }
568
569 q.QueryExpression.printSource(w, v)
570 }
571 }
572
573 func (r RelationalExpression) printSource(w io.Writer, v bool) {
574 r.AdditionExpression.printSource(w, v)
575
576 if r.RelationalOperator != RelationalNone && r.RelationalExpression != nil {
577 if v {
578 io.WriteString(w, " ")
579 r.Comments[0].printSource(w, v)
580 }
581
582 r.RelationalOperator.printSource(w, v)
583
584 if v {
585 io.WriteString(w, " ")
586 r.Comments[1].printSource(w, v)
587 }
588
589 r.RelationalExpression.printSource(w, v)
590 }
591 }
592
593 func (r RepeatControl) printSource(w io.Writer, v bool) {
594 io.WriteString(w, "repeat ")
595 r.Expr.printSource(w, v)
596 }
597
598 func (s ScopeExpression) printSource(w io.Writer, v bool) {
599 s.IndexOrCallExpression.printSource(w, v)
600
601 if s.ScopeExpression != nil {
602 if v && len(s.Comments[0]) > 0 {
603 io.WriteString(w, " ")
604 s.Comments[0].printSource(w, v)
605 }
606
607 io.WriteString(w, "::")
608
609 if v && len(s.Comments[1]) > 0 {
610 io.WriteString(w, " ")
611 s.Comments[1].printSource(w, v)
612 }
613
614 s.ScopeExpression.printSource(w, v)
615 }
616 }
617
618 func (s SequenceExpression) printSource(w io.Writer, v bool) {
619 s.UnaryExpression.printSource(w, v)
620
621 if s.SequenceExpression != nil {
622 if v && len(s.Comments[0]) > 0 {
623 io.WriteString(w, " ")
624 s.Comments[0].printSource(w, v)
625 }
626
627 io.WriteString(w, ":")
628
629 if v && len(s.Comments[1]) > 0 {
630 io.WriteString(w, " ")
631 s.Comments[1].printSource(w, v)
632 }
633
634 s.SequenceExpression.printSource(w, v)
635 }
636 }
637
638 func (s SimpleExpression) printSource(w io.Writer, v bool) {
639 if s.Identifier != nil {
640 io.WriteString(w, s.Identifier.Data)
641 } else if s.Constant != nil {
642 io.WriteString(w, s.Constant.Data)
643 } else if s.Ellipsis != nil {
644 io.WriteString(w, s.Ellipsis.Data)
645 } else if s.ParenthesizedExpression != nil {
646 s.ParenthesizedExpression.printSource(w, v)
647 } else if s.CompoundExpression != nil {
648 s.CompoundExpression.printSource(w, v)
649 }
650 }
651
652 func (s SubsetExpression) printSource(w io.Writer, v bool) {
653 s.ScopeExpression.printSource(w, v)
654
655 if s.SubsetExpression != nil && s.SubsetType != SubsetNone {
656 if v && len(s.Comments[0]) > 0 {
657 io.WriteString(w, " ")
658 s.Comments[0].printSource(w, v)
659 }
660
661 if s.SubsetType == SubsetList {
662 io.WriteString(w, "$")
663 } else {
664 io.WriteString(w, "@")
665 }
666
667 if v && len(s.Comments[1]) > 0 {
668 io.WriteString(w, " ")
669 s.Comments[1].printSource(w, v)
670 }
671
672 s.SubsetExpression.printSource(w, v)
673 }
674 }
675
676 func (u UnaryExpression) printSource(w io.Writer, v bool) {
677 for n, t := range u.UnaryType {
678 switch t {
679 case UnaryAdd:
680 io.WriteString(w, "+")
681 case UnaryMinus:
682 io.WriteString(w, "-")
683 }
684
685 if v && len(u.Comments) > n && len(u.Comments[n]) > 0 {
686 io.WriteString(w, " ")
687 u.Comments[n].printSource(w, v)
688 }
689 }
690
691 u.ExponentiationExpression.printSource(w, v)
692 }
693
694 func (wc WhileControl) printSource(w io.Writer, v bool) {
695 ipp := indentPrinter{w}
696
697 if v {
698 if len(wc.Comments[0]) > 0 {
699 io.WriteString(w, "while ")
700 wc.Comments[0].printSource(w, true)
701 io.WriteString(w, "(")
702 } else {
703 io.WriteString(w, "while (")
704 }
705
706 wc.Comments[1].printSource(&ipp, v)
707 } else {
708 io.WriteString(w, "while(")
709 }
710
711 wc.Cond.printSource(w, v)
712
713 if v && len(wc.Comments[2]) > 0 {
714 io.WriteString(w, " ")
715 wc.Comments[2].printSource(&ipp, false)
716 io.WriteString(w, "\n")
717 }
718
719 if v {
720 io.WriteString(w, ") ")
721 } else {
722 io.WriteString(w, ")")
723 }
724
725 wc.Expr.printSource(w, v)
726 }
727