|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/quasilyte/parsing-in-go/phpdoc" |
| 5 | +) |
| 6 | + |
| 7 | +type converter struct{} |
| 8 | + |
| 9 | +func (conv *converter) Convert(expr *UnionExpr) phpdoc.Type { |
| 10 | + return conv.convertUnion(expr) |
| 11 | +} |
| 12 | + |
| 13 | +func (conv *converter) convertPrefix(expr *PrefixExpr) phpdoc.Type { |
| 14 | + result := conv.convertPrimary(expr.Right) |
| 15 | + if len(expr.Ops) != 0 { |
| 16 | + for _, prefix := range expr.Ops { |
| 17 | + switch prefix.Tok { |
| 18 | + case "?": |
| 19 | + result = &phpdoc.NullableType{Elem: result} |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + // if expr.Postfix != nil { |
| 24 | + |
| 25 | + // } |
| 26 | + return result |
| 27 | +} |
| 28 | + |
| 29 | +func (conv *converter) convertPostfix(expr *PostfixExpr) phpdoc.Type { |
| 30 | + result := conv.convertPrefix(expr.Left) |
| 31 | + if len(expr.Ops) != 0 { |
| 32 | + for _, postfix := range expr.Ops { |
| 33 | + switch postfix.Tok { |
| 34 | + case "[]": |
| 35 | + result = &phpdoc.ArrayType{Elem: result} |
| 36 | + case "?": |
| 37 | + result = &phpdoc.OptionalKeyType{Elem: result} |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + return result |
| 42 | +} |
| 43 | + |
| 44 | +func (conv *converter) convertUnion(expr *UnionExpr) phpdoc.Type { |
| 45 | + left := conv.convertIntersection(expr.Left) |
| 46 | + if expr.Right != nil { |
| 47 | + right := conv.convertUnion(expr.Right) |
| 48 | + return &phpdoc.UnionType{X: left, Y: right} |
| 49 | + } |
| 50 | + return left |
| 51 | +} |
| 52 | + |
| 53 | +func (conv *converter) convertIntersection(expr *IntersectionExpr) phpdoc.Type { |
| 54 | + left := conv.convertPostfix(expr.Left) |
| 55 | + if expr.Right != nil { |
| 56 | + right := conv.convertIntersection(expr.Right) |
| 57 | + return &phpdoc.IntersectionType{X: left, Y: right} |
| 58 | + } |
| 59 | + return left |
| 60 | +} |
| 61 | + |
| 62 | +func (conv *converter) convertPrimary(expr *PrimaryExpr) phpdoc.Type { |
| 63 | + if expr.TypeName != nil { |
| 64 | + if expr.TypeName.Primitive != nil { |
| 65 | + return &phpdoc.PrimitiveTypeName{Name: *expr.TypeName.Primitive} |
| 66 | + } |
| 67 | + return conv.convertClassName(expr.TypeName.Class, nil) |
| 68 | + } |
| 69 | + if expr.Parens != nil { |
| 70 | + return conv.Convert(expr.Parens) |
| 71 | + } |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +func (conv *converter) convertClassName(name *ClassNameExpr, parts []string) phpdoc.Type { |
| 76 | + parts = append(parts, name.Part) |
| 77 | + if name.Next == nil { |
| 78 | + return &phpdoc.TypeName{Parts: parts} |
| 79 | + } |
| 80 | + return conv.convertClassName(name.Next, parts) |
| 81 | +} |
0 commit comments