Upgrade to Pro — share decks privately, control downloads, hide ads and more …

fmt

ktr
January 25, 2018

 fmt

ktr

January 25, 2018
Tweet

More Decks by ktr

Other Decks in Technology

Transcript

  1. 3

  2. cmd/gofmt/gofmt.go cmd/gofmt/gofmt.go gofmtMain() gofmtMain() for i := 0; i <

    flag.NArg(); i++ { path := flag.Arg(i) switch dir, err := os.Stat(path); { case err != nil: report(err) case dir.IsDir(): walkDir(path) // NOTE; call processFil default: if err := processFile(path, nil, os.St report(err) } } } 9
  3. cmd/gofmt/internal.go cmd/gofmt/internal.go parse() parse() parser.ParseFile file, err = parser.ParseFile(fset, filename,

    psrc, parserMode) if err == nil { sourceAdj = func(src []byte, indent int) []byte { src = src[indent+len("package p\n"):] return bytes.TrimSpace(src) } 11
  4. cmd/gofmt/gofmt.go cmd/gofmt/gofmt.go processFile() processFile() ast.SortImports(fileSet, file) if *simplifyAST { simplify(file)

    } res, err := format(fileSet, file, sourceAdj, indentAdj, src, p if err != nil { return err } 13
  5. cmd/gofmt/internal.go cmd/gofmt/internal.go format format var buf bytes.Buffer err := cfg.Fprint(&buf,

    fset, file) if err != nil { return nil, err } return buf.Bytes(), nil 14
  6. go/printer/printer.go go/printer/printer.go Fprint() → Fprint() → fprint() fprint() AST ノードを整形・出力するパッケージ

    var p printer p.init(cfg, fset, nodeSizes) if err = p.printNode(node); err != nil { return } 15
  7. go/printer/printer.go go/printer/printer.go printNode() printNode() switch n := node.(type) { case

    ast.Expr: p.expr(n) case ast.Stmt: if _, ok := n.(*ast.LabeledStmt); ok { p.indent = 1 } p.stmt(n, false) case ast.Decl: p.decl(n) case ast.Spec: p.spec(n, 1, false) case []ast.Stmt: for _, s := range n { if _, ok := s.(*ast.LabeledStmt); ok { 16
  8. AST AST の構造 の構造 0 *ast.CallExpr { 1 . Fun:

    *ast.SelectorExpr { 2 . . X: *ast.Ident { 3 . . . NamePos: 1 4 . . . Name: "fmt" 5 . . . Obj: *ast.Object { 6 . . . . Kind: bad 7 . . . . Name: "" 8 . . . } 9 . . } 10 . . Sel: *ast.Ident { 11 . . . NamePos: 5 12 . . . Name: "Println" 13 . . } 14 . } 19
  9. 20

  10. go/printer/printer.go go/printer/printer.go printNode() printNode() switch n := node.(type) { case

    ast.Expr: p.expr(n) func (p *printer) expr(x ast.Expr) { const depth = 1 p.expr1(x, token.LowestPrec, depth) } 21
  11. go/printer/nodes.go go/printer/nodes.go expr1() expr1() switch x := expr.(type) { case

    *ast.BinaryExpr: if depth < 1 { p.internalError("depth < 1:", depth) depth = 1 } p.binaryExpr(x, prec1, cutoff(x, depth), depth) 22
  12. go/printer/nodes.go go/printer/nodes.go binaryExpr() binaryExpr() ( 一部略) ( 一部略) // X

    評価 p.expr1(x.X, prec, depth+diffPrec(x.X, prec)) if printBlank { // 追加 p.print(blank) } // "+" p.print(x.OpPos, x.Op) if printBlank { // 追加 p.print(blank) } // Y 評価 p.expr1(x.Y, prec+1, depth+1) 23
  13. 29

  14. 30

  15. 31

  16. これはデモ用のテキストです これはデモ用のテキストです this text is an example text for demonstration.

    current version of markdownfmt accepts 80 charactors as the max width. so, maybe this text will be splitted to some lines. 36
  17. 参考 参考 printer - The Go Programming Language parser -

    The Go Programming Language Go のための Go GitHub Flavored Markdown Spec 38