Skip to content

Instantly share code, notes, and snippets.

@tyru
Last active September 7, 2018 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyru/bcb4f4c1a92e094f6d6b5c731cfed60e to your computer and use it in GitHub Desktop.
Save tyru/bcb4f4c1a92e094f6d6b5c731cfed60e to your computer and use it in GitHub Desktop.
go-vimlparser walk
node = (*ast.File) &{Start:sample.vim:1:1 Body:[0xc420001200 0xc420001380]}
node = (*ast.Function) &{Func:sample.vim:1:1 ExArg:{Forceit:true AddrCount:0 Line1:0 Line2:0 Flags:0 DoEcmdCmd: DoEcmdLnum:0 Append:0 Usefilter:false Amount:0 Regname:0 ForceBin:0 ReadEdit:0 ForceFf: ForceEnc: BadChar: Linepos:sample.vim:1:1 Cmdpos:sample.vim:1:1 Argpos:sample.vim:1:11 Cmd:0xc42005e500 Modifiers:[] Range:[] Argopt:map[] Argcmd:map[]} Body:[0xc4200e3540] Name:0xc42005e640 Params:[] Attr:{Range:false Abort:false Dict:false Closure:false} EndFunction:0xc4200e3680}
node = (*ast.Ident) &{NamePos:sample.vim:1:11 Name:s:f}
node = (<nil>) <nil>
node = (*ast.ExCall) &{ExCall:sample.vim:2:3 ExArg:{Forceit:false AddrCount:0 Line1:0 Line2:0 Flags:0 DoEcmdCmd: DoEcmdLnum:0 Append:0 Usefilter:false Amount:0 Regname:0 ForceBin:0 ReadEdit:0 ForceFf: ForceEnc: BadChar: Linepos:sample.vim:2:3 Cmdpos:sample.vim:2:3 Argpos:sample.vim:2:8 Cmd:0xc42005e540 Modifiers:[] Range:[] Argopt:map[] Argcmd:map[]} FuncCall:0xc42008d630}
node = (*ast.CallExpr) &{Fun:0xc42005e580 Lparen:sample.vim:2:11 Args:[0xc42008d5e0]}
node = (*ast.Ident) &{NamePos:sample.vim:2:8 Name:s:g}
node = (<nil>) <nil>
node = (*ast.BinaryExpr) &{Left:0xc42005e5c0 OpPos:sample.vim:2:14 Op:+ Right:0xc42005e600}
node = (*ast.BasicLit) &{ValuePos:sample.vim:2:12 Kind:<NUMBER> Value:1}
node = (<nil>) <nil>
node = (*ast.BasicLit) &{ValuePos:sample.vim:2:16 Kind:<NUMBER> Value:1}
node = (<nil>) <nil>
node = (<nil>) <nil>
node = (<nil>) <nil>
node = (<nil>) <nil>
node = (*ast.EndFunction) &{EndFunc:sample.vim:3:1 ExArg:{Forceit:false AddrCount:0 Line1:0 Line2:0 Flags:0 DoEcmdCmd: DoEcmdLnum:0 Append:0 Usefilter:false Amount:0 Regname:0 ForceBin:0 ReadEdit:0 ForceFf: ForceEnc: BadChar: Linepos:sample.vim:3:1 Cmdpos:sample.vim:3:1 Argpos:sample.vim:3:12 Cmd:0xc42005e680 Modifiers:[] Range:[] Argopt:map[] Argcmd:map[]}}
node = (<nil>) <nil>
node = (<nil>) <nil>
node = (*ast.Function) &{Func:sample.vim:5:1 ExArg:{Forceit:true AddrCount:0 Line1:0 Line2:0 Flags:0 DoEcmdCmd: DoEcmdLnum:0 Append:0 Usefilter:false Amount:0 Regname:0 ForceBin:0 ReadEdit:0 ForceFf: ForceEnc: BadChar: Linepos:sample.vim:5:1 Cmdpos:sample.vim:5:1 Argpos:sample.vim:5:11 Cmd:0xc42005e6c0 Modifiers:[] Range:[] Argopt:map[] Argcmd:map[]} Body:[] Name:0xc42005e700 Params:[0xc42005e740] Attr:{Range:false Abort:false Dict:false Closure:false} EndFunction:0xc4200e37c0}
node = (*ast.Ident) &{NamePos:sample.vim:5:11 Name:s:g}
node = (<nil>) <nil>
node = (*ast.Ident) &{NamePos:sample.vim:5:15 Name:n}
node = (<nil>) <nil>
node = (*ast.EndFunction) &{EndFunc:sample.vim:6:1 ExArg:{Forceit:false AddrCount:0 Line1:0 Line2:0 Flags:0 DoEcmdCmd: DoEcmdLnum:0 Append:0 Usefilter:false Amount:0 Regname:0 ForceBin:0 ReadEdit:0 ForceFf: ForceEnc: BadChar: Linepos:sample.vim:6:1 Cmdpos:sample.vim:6:1 Argpos:sample.vim:6:12 Cmd:0xc42005e780 Modifiers:[] Range:[] Argopt:map[] Argcmd:map[]}}
node = (<nil>) <nil>
node = (<nil>) <nil>
node = (<nil>) <nil>
function! s:f()
call s:g(1 + 1)
endfunction
function! s:g(n)
endfunction
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
vimlparser "github.com/haya14busa/go-vimlparser"
"github.com/haya14busa/go-vimlparser/ast"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: <prog> <file>.vim")
return
}
src, err := ioutil.ReadFile(os.Args[1])
if err != nil {
panic(err)
}
node, err := vimlparser.ParseFile(bytes.NewReader(src), os.Args[1], nil)
if err != nil {
panic(err)
}
ast.Walk(&visitor{}, node)
}
type visitor struct {
indent int
}
func (v *visitor) Visit(node ast.Node) ast.Visitor {
indent := strings.Repeat(" ", v.indent)
fmt.Printf("%snode = (%T) %+v\n", indent, node, node)
if node == nil {
v.indent--
} else {
v.indent++
}
return v
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment