1
16
17
18 package build
19
20
21
22 import (
23 "fmt"
24 "strings"
25 "unicode/utf8"
26 )
27
28
29 type Position struct {
30 Line int
31 LineRune int
32 Byte int
33 }
34
35
36 func (p Position) add(s string) Position {
37 p.Byte += len(s)
38 if n := strings.Count(s, "\n"); n > 0 {
39 p.Line += n
40 s = s[strings.LastIndex(s, "\n")+1:]
41 p.LineRune = 1
42 }
43 p.LineRune += utf8.RuneCountInString(s)
44 return p
45 }
46
47
48 type Expr interface {
49
50
51 Span() (start, end Position)
52
53
54
55
56 Comment() *Comments
57
58
59
60
61 Copy() Expr
62 }
63
64
65 type Comment struct {
66 Start Position
67 Token string
68 }
69
70
71 func (c Comment) Span() (start, end Position) {
72 return c.Start, c.Start.add(c.Token)
73 }
74
75
76 type Comments struct {
77 Before []Comment
78 Suffix []Comment
79
80
81
82 After []Comment
83 }
84
85
86
87
88
89 func (c *Comments) Comment() *Comments {
90 return c
91 }
92
93
94 func stmtsEnd(stmts []Expr) Position {
95 for i := len(stmts) - 1; i >= 0; i-- {
96 if stmts[i] != nil {
97 _, end := stmts[i].Span()
98 return end
99 }
100 }
101 return Position{}
102 }
103
104
105 type File struct {
106 Path string
107 Pkg string
108 Label string
109 WorkspaceRoot string
110 Type FileType
111 Comments
112 Stmt []Expr
113 }
114
115
116 func (f *File) DisplayPath() string {
117 if f.Path == "" {
118 return "<stdin>"
119 }
120 return f.Path
121 }
122
123
124 func (f *File) CanonicalPath() string {
125 if f.Pkg == "" {
126 return "//" + f.Label
127 }
128 return fmt.Sprintf("//%s/%s", f.Pkg, f.Label)
129 }
130
131
132 func (f *File) Span() (start, end Position) {
133 if len(f.Stmt) == 0 {
134 p := Position{Line: 1, LineRune: 1}
135 return p, p
136 }
137 start = Position{}
138 end = stmtsEnd(f.Stmt)
139 return start, end
140 }
141
142
143 func (f *File) Copy() Expr {
144 n := *f
145 return &n
146 }
147
148
149
150 type CommentBlock struct {
151 Comments
152 Start Position
153 }
154
155
156 func (x *CommentBlock) Span() (start, end Position) {
157 return x.Start, x.Start
158 }
159
160
161 func (x *CommentBlock) Copy() Expr {
162 n := *x
163 return &n
164 }
165
166
167 type Ident struct {
168 Comments
169 NamePos Position
170 Name string
171 }
172
173
174 func (x *Ident) Span() (start, end Position) {
175 return x.NamePos, x.NamePos.add(x.Name)
176 }
177
178
179 func (x *Ident) Copy() Expr {
180 n := *x
181 return &n
182 }
183
184 func (x *Ident) asString() *StringExpr {
185 _, end := x.Span()
186 return &StringExpr{
187 Comments: x.Comments,
188 Start: x.NamePos,
189 Value: x.Name,
190 End: end,
191 }
192 }
193
194
195 type TypedIdent struct {
196 Comments
197 Ident *Ident
198 Type Expr
199 }
200
201
202 func (x *TypedIdent) Span() (start, end Position) {
203 start, _ = x.Ident.Span()
204 _, end = x.Type.Span()
205 return start, end
206 }
207
208
209 func (x *TypedIdent) Copy() Expr {
210 n := *x
211 return &n
212 }
213
214
215 type BranchStmt struct {
216 Comments
217 Token string
218 TokenPos Position
219 }
220
221
222 func (x *BranchStmt) Span() (start, end Position) {
223 return x.TokenPos, x.TokenPos.add(x.Token)
224 }
225
226
227 func (x *BranchStmt) Copy() Expr {
228 n := *x
229 return &n
230 }
231
232
233 type LiteralExpr struct {
234 Comments
235 Start Position
236 Token string
237 }
238
239
240 func (x *LiteralExpr) Span() (start, end Position) {
241 return x.Start, x.Start.add(x.Token)
242 }
243
244
245 func (x *LiteralExpr) Copy() Expr {
246 n := *x
247 return &n
248 }
249
250
251 type StringExpr struct {
252 Comments
253 Start Position
254 Value string
255 TripleQuote bool
256 End Position
257
258
259
260
261
262 Token string
263 }
264
265
266 func (x *StringExpr) Span() (start, end Position) {
267 return x.Start, x.End
268 }
269
270
271 func (x *StringExpr) Copy() Expr {
272 n := *x
273 return &n
274 }
275
276
277
278 type End struct {
279 Comments
280 Pos Position
281 }
282
283
284 func (x *End) Span() (start, end Position) {
285 return x.Pos, x.Pos.add(")")
286 }
287
288
289 func (x *End) Copy() Expr {
290 n := *x
291 return &n
292 }
293
294
295 type CallExpr struct {
296 Comments
297 X Expr
298 ListStart Position
299 List []Expr
300 End
301 ForceCompact bool
302 ForceMultiLine bool
303 }
304
305
306 func (x *CallExpr) Span() (start, end Position) {
307 start, _ = x.X.Span()
308 return start, x.End.Pos.add(")")
309 }
310
311
312 func (x *CallExpr) Copy() Expr {
313 n := *x
314 return &n
315 }
316
317
318 type DotExpr struct {
319 Comments
320 X Expr
321 Dot Position
322 NamePos Position
323 Name string
324 }
325
326
327 func (x *DotExpr) Span() (start, end Position) {
328 start, _ = x.X.Span()
329 return start, x.NamePos.add(x.Name)
330 }
331
332
333 func (x *DotExpr) Copy() Expr {
334 n := *x
335 return &n
336 }
337
338
339 type Comprehension struct {
340 Comments
341 Curly bool
342 Lbrack Position
343 Body Expr
344 Clauses []Expr
345 ForceMultiLine bool
346 End
347 }
348
349
350 func (x *Comprehension) Span() (start, end Position) {
351 return x.Lbrack, x.End.Pos.add("]")
352 }
353
354
355 func (x *Comprehension) Copy() Expr {
356 n := *x
357 return &n
358 }
359
360
361 type ForClause struct {
362 Comments
363 For Position
364 Vars Expr
365 In Position
366 X Expr
367 }
368
369
370 func (x *ForClause) Span() (start, end Position) {
371 _, end = x.X.Span()
372 return x.For, end
373 }
374
375
376 func (x *ForClause) Copy() Expr {
377 n := *x
378 return &n
379 }
380
381
382 type IfClause struct {
383 Comments
384 If Position
385 Cond Expr
386 }
387
388
389 func (x *IfClause) Span() (start, end Position) {
390 _, end = x.Cond.Span()
391 return x.If, end
392 }
393
394
395 func (x *IfClause) Copy() Expr {
396 n := *x
397 return &n
398 }
399
400
401 type KeyValueExpr struct {
402 Comments
403 Key Expr
404 Colon Position
405 Value Expr
406 }
407
408
409 func (x *KeyValueExpr) Span() (start, end Position) {
410 start, _ = x.Key.Span()
411 _, end = x.Value.Span()
412 return start, end
413 }
414
415
416 func (x *KeyValueExpr) Copy() Expr {
417 n := *x
418 return &n
419 }
420
421
422 type DictExpr struct {
423 Comments
424 Start Position
425 List []*KeyValueExpr
426 End
427 ForceMultiLine bool
428 }
429
430
431 func (x *DictExpr) Span() (start, end Position) {
432 return x.Start, x.End.Pos.add("}")
433 }
434
435
436 func (x *DictExpr) Copy() Expr {
437 n := *x
438 return &n
439 }
440
441
442 type ListExpr struct {
443 Comments
444 Start Position
445 List []Expr
446 End
447 ForceMultiLine bool
448 }
449
450
451 func (x *ListExpr) Span() (start, end Position) {
452 return x.Start, x.End.Pos.add("]")
453 }
454
455
456 func (x *ListExpr) Copy() Expr {
457 n := *x
458 return &n
459 }
460
461
462 type SetExpr struct {
463 Comments
464 Start Position
465 List []Expr
466 End
467 ForceMultiLine bool
468 }
469
470
471 func (x *SetExpr) Span() (start, end Position) {
472 return x.Start, x.End.Pos.add("}")
473 }
474
475
476 func (x *SetExpr) Copy() Expr {
477 n := *x
478 return &n
479 }
480
481
482 type TupleExpr struct {
483 Comments
484 NoBrackets bool
485 Start Position
486 List []Expr
487 End
488 ForceCompact bool
489 ForceMultiLine bool
490 }
491
492
493 func (x *TupleExpr) Span() (start, end Position) {
494 if !x.NoBrackets {
495 return x.Start, x.End.Pos.add(")")
496 }
497 start, _ = x.List[0].Span()
498 _, end = x.List[len(x.List)-1].Span()
499 return start, end
500 }
501
502
503 func (x *TupleExpr) Copy() Expr {
504 n := *x
505 return &n
506 }
507
508
509 type UnaryExpr struct {
510 Comments
511 OpStart Position
512 Op string
513 X Expr
514 }
515
516
517 func (x *UnaryExpr) Span() (start, end Position) {
518 if x.X == nil {
519 return x.OpStart, x.OpStart
520 }
521 _, end = x.X.Span()
522 return x.OpStart, end
523 }
524
525
526 func (x *UnaryExpr) Copy() Expr {
527 n := *x
528 return &n
529 }
530
531
532 type BinaryExpr struct {
533 Comments
534 X Expr
535 OpStart Position
536 Op string
537 LineBreak bool
538 Y Expr
539 }
540
541
542 func (x *BinaryExpr) Span() (start, end Position) {
543 start, _ = x.X.Span()
544 _, end = x.Y.Span()
545 return start, end
546 }
547
548
549 func (x *BinaryExpr) Copy() Expr {
550 n := *x
551 return &n
552 }
553
554
555 type AssignExpr struct {
556 Comments
557 LHS Expr
558 OpPos Position
559 Op string
560 LineBreak bool
561 RHS Expr
562 }
563
564
565 func (x *AssignExpr) Span() (start, end Position) {
566 start, _ = x.LHS.Span()
567 _, end = x.RHS.Span()
568 return start, end
569 }
570
571
572 func (x *AssignExpr) Copy() Expr {
573 n := *x
574 return &n
575 }
576
577
578 type ParenExpr struct {
579 Comments
580 Start Position
581 X Expr
582 End
583 ForceMultiLine bool
584 }
585
586
587 func (x *ParenExpr) Span() (start, end Position) {
588 return x.Start, x.End.Pos.add(")")
589 }
590
591
592 func (x *ParenExpr) Copy() Expr {
593 n := *x
594 return &n
595 }
596
597
598 type SliceExpr struct {
599 Comments
600 X Expr
601 SliceStart Position
602 From Expr
603 FirstColon Position
604 To Expr
605 SecondColon Position
606 Step Expr
607 End Position
608 }
609
610
611 func (x *SliceExpr) Span() (start, end Position) {
612 start, _ = x.X.Span()
613 return start, x.End.add("]")
614 }
615
616
617 func (x *SliceExpr) Copy() Expr {
618 n := *x
619 return &n
620 }
621
622
623 type IndexExpr struct {
624 Comments
625 X Expr
626 IndexStart Position
627 Y Expr
628 End Position
629 }
630
631
632 func (x *IndexExpr) Span() (start, end Position) {
633 start, _ = x.X.Span()
634 return start, x.End.add("]")
635 }
636
637
638 func (x *IndexExpr) Copy() Expr {
639 n := *x
640 return &n
641 }
642
643
644 type Function struct {
645 Comments
646 StartPos Position
647 Params []Expr
648 Body []Expr
649 }
650
651
652 func (x *Function) Span() (start, end Position) {
653 _, end = x.Body[len(x.Body)-1].Span()
654 return x.StartPos, end
655 }
656
657
658 func (x *Function) Copy() Expr {
659 n := *x
660 return &n
661 }
662
663
664 type LambdaExpr struct {
665 Comments
666 Function
667 }
668
669
670 func (x *LambdaExpr) Span() (start, end Position) {
671 return x.Function.Span()
672 }
673
674
675 func (x *LambdaExpr) Copy() Expr {
676 n := *x
677 return &n
678 }
679
680
681 type ConditionalExpr struct {
682 Comments
683 Then Expr
684 IfStart Position
685 Test Expr
686 ElseStart Position
687 Else Expr
688 }
689
690
691
692
693 func (x *ConditionalExpr) Span() (start, end Position) {
694 start, _ = x.Then.Span()
695 _, end = x.Else.Span()
696 return start, end
697 }
698
699
700 func (x *ConditionalExpr) Copy() Expr {
701 n := *x
702 return &n
703 }
704
705
706
707
708
709
710
711
712
713 type LoadStmt struct {
714 Comments
715 Load Position
716 Module *StringExpr
717 From []*Ident
718 To []*Ident
719 Rparen End
720 ForceCompact bool
721 }
722
723
724 func (x *LoadStmt) Span() (start, end Position) {
725 return x.Load, x.Rparen.Pos.add(")")
726 }
727
728
729 func (x *LoadStmt) Copy() Expr {
730 n := *x
731 return &n
732 }
733
734
735 type DefStmt struct {
736 Comments
737 Function
738 Name string
739 ColonPos Position
740 ForceCompact bool
741 ForceMultiLine bool
742 Type Expr
743 }
744
745
746 func (x *DefStmt) Span() (start, end Position) {
747 return x.Function.Span()
748 }
749
750
751 func (x *DefStmt) Copy() Expr {
752 n := *x
753 return &n
754 }
755
756
757 func (x *DefStmt) HeaderSpan() (start, end Position) {
758 return x.Function.StartPos, x.ColonPos
759 }
760
761
762 type ReturnStmt struct {
763 Comments
764 Return Position
765 Result Expr
766 }
767
768
769 func (x *ReturnStmt) Span() (start, end Position) {
770 if x.Result == nil {
771 return x.Return, x.Return.add("return")
772 }
773 _, end = x.Result.Span()
774 return x.Return, end
775 }
776
777
778 func (x *ReturnStmt) Copy() Expr {
779 n := *x
780 return &n
781 }
782
783
784 type ForStmt struct {
785 Comments
786 Function
787 For Position
788 Vars Expr
789 X Expr
790 Body []Expr
791 }
792
793
794 func (x *ForStmt) Span() (start, end Position) {
795 end = stmtsEnd(x.Body)
796 return x.For, end
797 }
798
799
800 func (x *ForStmt) Copy() Expr {
801 n := *x
802 return &n
803 }
804
805
806
807 type IfStmt struct {
808 Comments
809 If Position
810 Cond Expr
811 True []Expr
812 ElsePos End
813 False []Expr
814 }
815
816
817 func (x *IfStmt) Span() (start, end Position) {
818 body := x.False
819 if body == nil {
820 body = x.True
821 }
822 end = stmtsEnd(body)
823 return x.If, end
824 }
825
826
827 func (x *IfStmt) Copy() Expr {
828 n := *x
829 return &n
830 }
831
View as plain text