|
| 1 | +// Package printer implements printing of AST nodes. |
| 2 | +// |
| 3 | +// This is WIP package. DO NOT USE. |
| 4 | +package printer |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + |
| 11 | + "github.com/haya14busa/go-vimlparser/ast" |
| 12 | +) |
| 13 | + |
| 14 | +// A Config node controls the output of Fprint. |
| 15 | +type Config struct{} |
| 16 | + |
| 17 | +// Fprint "pretty-prints" an AST node to output for a given configuration cfg. |
| 18 | +func Fprint(output io.Writer, node ast.Node, cfg *Config) error { |
| 19 | + var p printer |
| 20 | + p.init(cfg) |
| 21 | + if err := p.printNode(node); err != nil { |
| 22 | + return err |
| 23 | + } |
| 24 | + if _, err := output.Write(p.output); err != nil { |
| 25 | + return err |
| 26 | + } |
| 27 | + return nil |
| 28 | +} |
| 29 | + |
| 30 | +type printer struct { |
| 31 | + Config |
| 32 | + |
| 33 | + // Current state |
| 34 | + output []byte // raw printer result |
| 35 | +} |
| 36 | + |
| 37 | +func (p *printer) init(cfg *Config) { |
| 38 | + if cfg != nil { |
| 39 | + p.Config = *cfg |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func (p *printer) writeString(s string) { |
| 44 | + p.output = append(p.output, s...) |
| 45 | +} |
| 46 | + |
| 47 | +func (p *printer) printNode(node ast.Node) error { |
| 48 | + switch n := node.(type) { |
| 49 | + case *ast.File: |
| 50 | + return p.file(n) |
| 51 | + case ast.Expr: |
| 52 | + return p.expr(n) |
| 53 | + case ast.Statement: |
| 54 | + return p.stmt(n) |
| 55 | + default: |
| 56 | + return fmt.Errorf("go-vimlparser/printer: unsupported node type %T", node) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func (p *printer) file(f *ast.File) error { |
| 61 | + return errors.New("Not implemented: printer.file") |
| 62 | +} |
| 63 | + |
| 64 | +func (p *printer) expr(expr ast.Expr) error { |
| 65 | + switch n := expr.(type) { |
| 66 | + // case *ast.TernaryExpr: |
| 67 | + // case *ast.BinaryExpr: |
| 68 | + // case *ast.UnaryExpr: |
| 69 | + // case *ast.SubscriptExpr: |
| 70 | + // case *ast.SliceExpr: |
| 71 | + // case *ast.CallExpr: |
| 72 | + // case *ast.DotExpr: |
| 73 | + // case *ast.List: |
| 74 | + // case *ast.Dict: |
| 75 | + // case *ast.CurlyName: |
| 76 | + // case *ast.CurlyNameLit: |
| 77 | + // case *ast.CurlyNameExpr: |
| 78 | + case *ast.BasicLit: |
| 79 | + p.writeString(n.Value) |
| 80 | + case *ast.Ident: |
| 81 | + p.writeString(n.Name) |
| 82 | + // case *ast.LambdaExpr: |
| 83 | + // case *ast.ParenExpr: |
| 84 | + default: |
| 85 | + return fmt.Errorf("unsupported expr type %T", n) |
| 86 | + } |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func (p *printer) stmt(node ast.Statement) error { |
| 91 | + return errors.New("Not implemented: printer.stmt") |
| 92 | +} |
0 commit comments