123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- package build
- import (
- "strings"
- "unicode/utf8"
- )
- type Position struct {
- Line int
- LineRune int
- Byte int
- }
- func (p Position) add(s string) Position {
- p.Byte += len(s)
- if n := strings.Count(s, "\n"); n > 0 {
- p.Line += n
- s = s[strings.LastIndex(s, "\n")+1:]
- p.LineRune = 1
- }
- p.LineRune += utf8.RuneCountInString(s)
- return p
- }
- type Expr interface {
-
-
- Span() (start, end Position)
-
-
-
- Comment() *Comments
- }
- type Comment struct {
- Start Position
- Token string
- }
- type Comments struct {
- Before []Comment
- Suffix []Comment
-
-
- After []Comment
- }
- func (c *Comments) Comment() *Comments {
- return c
- }
- type File struct {
- Path string
- Comments
- Stmt []Expr
- }
- func (f *File) Span() (start, end Position) {
- if len(f.Stmt) == 0 {
- return
- }
- start, _ = f.Stmt[0].Span()
- _, end = f.Stmt[len(f.Stmt)-1].Span()
- return start, end
- }
- type CommentBlock struct {
- Comments
- Start Position
- }
- func (x *CommentBlock) Span() (start, end Position) {
- return x.Start, x.Start
- }
- type PythonBlock struct {
- Comments
- Start Position
- Token string
- }
- func (x *PythonBlock) Span() (start, end Position) {
- return x.Start, x.Start.add(x.Token)
- }
- type LiteralExpr struct {
- Comments
- Start Position
- Token string
- }
- func (x *LiteralExpr) Span() (start, end Position) {
- return x.Start, x.Start.add(x.Token)
- }
- type StringExpr struct {
- Comments
- Start Position
- Value string
- TripleQuote bool
- End Position
-
-
-
-
- Token string
- }
- func (x *StringExpr) Span() (start, end Position) {
- return x.Start, x.End
- }
- type End struct {
- Comments
- Pos Position
- }
- func (x *End) Span() (start, end Position) {
- return x.Pos, x.Pos.add(")")
- }
- type CallExpr struct {
- Comments
- X Expr
- ListStart Position
- List []Expr
- End
- ForceCompact bool
- ForceMultiLine bool
- }
- func (x *CallExpr) Span() (start, end Position) {
- start, _ = x.X.Span()
- return start, x.End.Pos.add(")")
- }
- type DotExpr struct {
- Comments
- X Expr
- Dot Position
- NamePos Position
- Name string
- }
- func (x *DotExpr) Span() (start, end Position) {
- start, _ = x.X.Span()
- return start, x.NamePos.add(x.Name)
- }
- type ListForExpr struct {
- Comments
- ForceMultiLine bool
- Brack string
- Start Position
- X Expr
- For []*ForClauseWithIfClausesOpt
- End
- }
- func (x *ListForExpr) Span() (start, end Position) {
- return x.Start, x.End.Pos.add("]")
- }
- type ForClause struct {
- Comments
- For Position
- Var []Expr
- In Position
- Expr Expr
- }
- func (x *ForClause) Span() (start, end Position) {
- _, end = x.Expr.Span()
- return x.For, end
- }
- type IfClause struct {
- Comments
- If Position
- Cond Expr
- }
- func (x *IfClause) Span() (start, end Position) {
- _, end = x.Cond.Span()
- return x.If, end
- }
- type ForClauseWithIfClausesOpt struct {
- Comments
- For *ForClause
- Ifs []*IfClause
- }
- func (x *ForClauseWithIfClausesOpt) Span() (start, end Position) {
- start, end = x.For.Span()
- if len(x.Ifs) > 0 {
- _, end = x.Ifs[len(x.Ifs)-1].Span()
- }
- return start, end
- }
- type KeyValueExpr struct {
- Comments
- Key Expr
- Colon Position
- Value Expr
- }
- func (x *KeyValueExpr) Span() (start, end Position) {
- start, _ = x.Key.Span()
- _, end = x.Value.Span()
- return start, end
- }
- type DictExpr struct {
- Comments
- Start Position
- List []Expr
- Comma Position
- End
- ForceMultiLine bool
- }
- func (x *DictExpr) Span() (start, end Position) {
- return x.Start, x.End.Pos.add("}")
- }
- type ListExpr struct {
- Comments
- Start Position
- List []Expr
- Comma Position
- End
- ForceMultiLine bool
- }
- func (x *ListExpr) Span() (start, end Position) {
- return x.Start, x.End.Pos.add("]")
- }
- type SetExpr struct {
- Comments
- Start Position
- List []Expr
- Comma Position
- End
- ForceMultiLine bool
- }
- func (x *SetExpr) Span() (start, end Position) {
- return x.Start, x.End.Pos.add("}")
- }
- type TupleExpr struct {
- Comments
- Start Position
- List []Expr
- Comma Position
- End
- ForceCompact bool
- ForceMultiLine bool
- }
- func (x *TupleExpr) Span() (start, end Position) {
- return x.Start, x.End.Pos.add(")")
- }
- type UnaryExpr struct {
- Comments
- OpStart Position
- Op string
- X Expr
- }
- func (x *UnaryExpr) Span() (start, end Position) {
- _, end = x.X.Span()
- return x.OpStart, end
- }
- type BinaryExpr struct {
- Comments
- X Expr
- OpStart Position
- Op string
- LineBreak bool
- Y Expr
- }
- func (x *BinaryExpr) Span() (start, end Position) {
- start, _ = x.X.Span()
- _, end = x.Y.Span()
- return start, end
- }
- type ParenExpr struct {
- Comments
- Start Position
- X Expr
- End
- ForceMultiLine bool
- }
- func (x *ParenExpr) Span() (start, end Position) {
- return x.Start, x.End.Pos.add(")")
- }
- type SliceExpr struct {
- Comments
- X Expr
- SliceStart Position
- From Expr
- FirstColon Position
- To Expr
- SecondColon Position
- Step Expr
- End Position
- }
- func (x *SliceExpr) Span() (start, end Position) {
- start, _ = x.X.Span()
- return start, x.End
- }
- type IndexExpr struct {
- Comments
- X Expr
- IndexStart Position
- Y Expr
- End Position
- }
- func (x *IndexExpr) Span() (start, end Position) {
- start, _ = x.X.Span()
- return start, x.End
- }
- type LambdaExpr struct {
- Comments
- Lambda Position
- Var []Expr
- Colon Position
- Expr Expr
- }
- func (x *LambdaExpr) Span() (start, end Position) {
- _, end = x.Expr.Span()
- return x.Lambda, end
- }
- type ConditionalExpr struct {
- Comments
- Then Expr
- IfStart Position
- Test Expr
- ElseStart Position
- Else Expr
- }
- func (x *ConditionalExpr) Span() (start, end Position) {
- start, _ = x.Then.Span()
- _, end = x.Else.Span()
- return start, end
- }
- type CodeBlock struct {
- Statements []Expr
- Start Position
- End
- }
- func (x *CodeBlock) Span() (start, end Position) {
- return x.Start, x.End.Pos
- }
- type FuncDef struct {
- Comments
- Start Position
- Name string
- ListStart Position
- Args []Expr
- Body CodeBlock
- End
- ForceCompact bool
- ForceMultiLine bool
- }
- func (x *FuncDef) Span() (start, end Position) {
- return x.Start, x.End.Pos
- }
- type ReturnExpr struct {
- Comments
- Start Position
- X Expr
- End Position
- }
- func (x *ReturnExpr) Span() (start, end Position) {
- return x.Start, x.End
- }
- type ForLoop struct {
- Comments
- Start Position
- LoopVars []Expr
- Iterable Expr
- Body CodeBlock
- End
- }
- func (x *ForLoop) Span() (start, end Position) {
- return x.Start, x.End.Pos
- }
- type IfElse struct {
- Comments
- Start Position
- Conditions []Condition
- End
- }
- type Condition struct {
- If Expr
- Then CodeBlock
- }
- func (x *IfElse) Span() (start, end Position) {
- return x.Start, x.End.Pos
- }
|