go-vimlparser: github.com/haya14busa/go-vimlparser/ast Index | Examples | Files

package ast

import "github.com/haya14busa/go-vimlparser/ast"

Package ast provides Vim script AST. ref: "go/ast"

Index

Examples

Package Files

exarg.go node.go pos.go walk.go

func Inspect Uses

func Inspect(node Node, f func(Node) bool)

Inspect traverses an AST in depth-first order: It starts by calling f(node); node must not be nil. If f returns true, Inspect invokes f recursively for each of the non-nil children of node, followed by a call of f(nil).

This example demonstrates how to inspect the AST of a Go program.

Code:

// src is the input for which we want to inspect the AST.
src := `
let s:c = 1.0
let X = F(3.14)*2 + s:c
`

opt := &vimlparser.ParseOption{}
f, err := vimlparser.ParseFile(strings.NewReader(src), "src.vim", opt)
if err != nil {
    log.Fatal(err)
}

// Inspect the AST and print all identifiers and literals.
ast.Inspect(f, func(n ast.Node) bool {
    var s string
    switch x := n.(type) {
    case *ast.BasicLit:
        s = x.Value
    case *ast.Ident:
        s = x.Name
    }
    if s != "" {
        fmt.Printf("%s:\t%s\n", n.Pos(), s)
    }
    return true
})

Output:

src.vim:2:5:	s:c
src.vim:2:11:	1.0
src.vim:3:5:	X
src.vim:3:9:	F
src.vim:3:11:	3.14
src.vim:3:17:	2
src.vim:3:21:	s:c

func Walk Uses

func Walk(v Visitor, node Node)

Walk traverses an AST in depth-first order: It starts by calling v.Visit(node); node must not be nil. If the visitor w returned by v.Visit(node) is not nil, Walk is invoked recursively with visitor w for each of the non-nil children of node, followed by a call of w.Visit(nil).

type BasicLit Uses

type BasicLit struct {
    ValuePos Pos         // literal position
    Kind     token.Token // token.INT, token.STRING, token.OPTION, token.ENV, token.REG
    Value    string
}

func (*BasicLit) Pos Uses

func (c *BasicLit) Pos() Pos

type BinaryExpr Uses

type BinaryExpr struct {
    Left  Expr        // left operand
    OpPos Pos         // position of Op
    Op    token.Token // operator
    Right Expr        // right operand
}

func (*BinaryExpr) Pos Uses

func (f *BinaryExpr) Pos() Pos

type Break Uses

type Break struct {
    Break Pos   // position of starting the :break
    ExArg ExArg // Ex command arg
}

vimlparser: BREAK .ea

func (*Break) Cmd Uses

func (f *Break) Cmd() Cmd

func (*Break) Pos Uses

func (f *Break) Pos() Pos

type CallExpr Uses

type CallExpr struct {
    Fun    Expr   // function expression
    Lparen Pos    // position of "("
    Args   []Expr // function arguments; or nil
}

vimlparser: CALL .left .rlist

func (*CallExpr) Pos Uses

func (c *CallExpr) Pos() Pos

type Catch Uses

type Catch struct {
    Catch   Pos         // position of starting the :catch
    ExArg   ExArg       // Ex command arg
    Body    []Statement // body of catch statement
    Pattern string      // pattern
}

vimlparser: CATCH .ea .body .pattern

func (*Catch) Cmd Uses

func (f *Catch) Cmd() Cmd

func (*Catch) Pos Uses

func (f *Catch) Pos() Pos

type Cmd Uses

type Cmd struct {
    Name   string
    Minlen int
    Flags  string
    Parser string
}

Cmd represents command.

type Comment Uses

type Comment struct {
    Statement
    Quote Pos    // position of `"` starting the comment
    Text  string // comment text (excluding '\n')
}

vimlparser: COMMENT .str

func (*Comment) Pos Uses

func (c *Comment) Pos() Pos

type Continue Uses

type Continue struct {
    Continue Pos   // position of starting the :continue
    ExArg    ExArg // Ex command arg
}

vimlparser: CONTINUE .ea

func (*Continue) Cmd Uses

func (f *Continue) Cmd() Cmd

func (*Continue) Pos Uses

func (f *Continue) Pos() Pos

type CurlyName Uses

type CurlyName struct {
    CurlyName Pos // position
    Parts     []CurlyNamePart
}

aaa{x{y{1+2}}}bbb ^^^^^^^^^^^^^^^^^ <- CurlyName

func (*CurlyName) Pos Uses

func (c *CurlyName) Pos() Pos

type CurlyNameExpr Uses

type CurlyNameExpr struct {
    CurlyNameExpr Pos // position
    Value         Expr
}

aaa{x{y{1+2}}}bbb

^^^^^^^^^^^    <- CurlyNameExpr

func (*CurlyNameExpr) IsCurlyExpr Uses

func (c *CurlyNameExpr) IsCurlyExpr() bool

func (*CurlyNameExpr) Pos Uses

func (c *CurlyNameExpr) Pos() Pos

type CurlyNameLit Uses

type CurlyNameLit struct {
    CurlyNameLit Pos // position
    Value        string
}

aaa{x{y{1+2}}}bbb ^^^ ^^^ <- CurlyNameLit

func (*CurlyNameLit) IsCurlyExpr Uses

func (c *CurlyNameLit) IsCurlyExpr() bool

func (*CurlyNameLit) Pos Uses

func (c *CurlyNameLit) Pos() Pos

type CurlyNamePart Uses

type CurlyNamePart interface {
    Expr
    IsCurlyExpr() bool
}

type DelFunction Uses

type DelFunction struct {
    DelFunc Pos   // position of starting the :delfunction
    ExArg   ExArg // Ex command arg
    Name    Expr  // function name to delete
}

vimlparser: DELFUNCTION .ea

func (*DelFunction) Cmd Uses

func (f *DelFunction) Cmd() Cmd

func (*DelFunction) Pos Uses

func (f *DelFunction) Pos() Pos

type Dict Uses

type Dict struct {
    Lcurlybrace Pos // position of "{"
    Entries     []KeyValue
}

func (*Dict) Pos Uses

func (c *Dict) Pos() Pos

type DotExpr Uses

type DotExpr struct {
    Left  Expr
    Dot   Pos // position of "."
    Right *Ident
}

Left.Right vimlparser: Dot .left .right

func (*DotExpr) Pos Uses

func (c *DotExpr) Pos() Pos

type EchoCmd Uses

type EchoCmd struct {
    Start   Pos    // position of starting the echo-command
    CmdName string // echo-command name
    ExArg   ExArg  // Ex command arg
    Exprs   []Expr
}

vimlparser: ECHO .ea .list vimlparser: ECHON .ea .list vimlparser: ECHOMSG .ea .list vimlparser: ECHOERR .ea .list :{echocmd} {Expr}.. {echocmd}: echo, echon, echomsg, echoerr

func (*EchoCmd) Cmd Uses

func (f *EchoCmd) Cmd() Cmd

func (*EchoCmd) Pos Uses

func (f *EchoCmd) Pos() Pos

type Echohl Uses

type Echohl struct {
    Echohl Pos   // position of starting the :echohl
    ExArg  ExArg // Ex command arg
    Name   string
}

vimlparser: ECHOHL .ea .str :echohl {name}

func (*Echohl) Cmd Uses

func (f *Echohl) Cmd() Cmd

func (*Echohl) Pos Uses

func (f *Echohl) Pos() Pos

type Else Uses

type Else struct {
    Else  Pos         // position of starting the :else
    ExArg ExArg       // Ex command arg
    Body  []Statement // body of else statement
}

vimlparser: ELSE .ea .body

func (*Else) Cmd Uses

func (f *Else) Cmd() Cmd

func (*Else) Pos Uses

func (f *Else) Pos() Pos

type ElseIf Uses

type ElseIf struct {
    ElseIf    Pos         // position of starting the :elseif
    ExArg     ExArg       // Ex command arg
    Body      []Statement // body of elseif statement
    Condition Expr        // condition
}

vimlparser: ELSEIF .ea .body .cond

func (*ElseIf) Cmd Uses

func (f *ElseIf) Cmd() Cmd

func (*ElseIf) Pos Uses

func (f *ElseIf) Pos() Pos

type EndFor Uses

type EndFor struct {
    EndFor Pos   // position of starting the :endfor
    ExArg  ExArg // Ex command arg
}

vimlparser: ENDFOR .ea

func (*EndFor) Cmd Uses

func (f *EndFor) Cmd() Cmd

func (*EndFor) Pos Uses

func (f *EndFor) Pos() Pos

type EndFunction Uses

type EndFunction struct {
    EndFunc Pos   // position of starting the :endfunction
    ExArg   ExArg // Ex command arg
}

vimlparser: ENDFUNCTION .ea

func (*EndFunction) Cmd Uses

func (f *EndFunction) Cmd() Cmd

func (*EndFunction) Pos Uses

func (f *EndFunction) Pos() Pos

type EndIf Uses

type EndIf struct {
    EndIf Pos   // position of starting the :endif
    ExArg ExArg // Ex command arg
}

vimlparser: ENDIF .ea

func (*EndIf) Cmd Uses

func (f *EndIf) Cmd() Cmd

func (*EndIf) Pos Uses

func (f *EndIf) Pos() Pos

type EndTry Uses

type EndTry struct {
    EndTry Pos   // position of starting the :endtry
    ExArg  ExArg // Ex command arg
}

vimlparser: ENDTRY .ea

func (*EndTry) Cmd Uses

func (f *EndTry) Cmd() Cmd

func (*EndTry) Pos Uses

func (f *EndTry) Pos() Pos

type EndWhile Uses

type EndWhile struct {
    EndWhile Pos   // position of starting the :endwhile
    ExArg    ExArg // Ex command arg
}

vimlparser: ENDWHILE .ea

func (*EndWhile) Cmd Uses

func (f *EndWhile) Cmd() Cmd

func (*EndWhile) Pos Uses

func (f *EndWhile) Pos() Pos

type ExArg Uses

type ExArg struct {
    Forceit    bool
    AddrCount  int
    Line1      int
    Line2      int
    Flags      int
    DoEcmdCmd  string
    DoEcmdLnum int
    Append     int
    Usefilter  bool
    Amount     int
    Regname    int
    ForceBin   int
    ReadEdit   int
    ForceFf    string // int
    ForceEnc   string // int
    BadChar    string // int
    Linepos    *Pos
    Cmdpos     *Pos
    Argpos     *Pos
    Cmd        *Cmd // Ex-command. It's not nil for most case?
    Modifiers  []interface{}
    Range      []interface{}
    Argopt     map[string]interface{}
    Argcmd     map[string]interface{}
}

ExArg represents extra arguments of command.

type ExCall Uses

type ExCall struct {
    ExCall   Pos       // position of starting the :call
    ExArg    ExArg     // Ex command arg
    FuncCall *CallExpr // a function call
}

vimlparser: EXCALL .ea .left

func (*ExCall) Cmd Uses

func (f *ExCall) Cmd() Cmd

func (*ExCall) Pos Uses

func (f *ExCall) Pos() Pos

type ExCommand Uses

type ExCommand interface {
    Statement
    Cmd() Cmd
}

ExCommand is the interface for Ex-command.

type Excmd Uses

type Excmd struct {
    Excmd   Pos    // position of starting the excmd
    Command string // Ex comamnd
    ExArg   ExArg  // Ex command arg
}

vimlparser: EXCMD .ea .str

func (*Excmd) Cmd Uses

func (e *Excmd) Cmd() Cmd

func (*Excmd) Pos Uses

func (e *Excmd) Pos() Pos

type Execute Uses

type Execute struct {
    Execute Pos   // position of starting the :execute
    ExArg   ExArg // Ex command arg
    Exprs   []Expr
}

vimlparser: EXECUTE .ea .list :execute {Expr}..

func (*Execute) Cmd Uses

func (f *Execute) Cmd() Cmd

func (*Execute) Pos Uses

func (f *Execute) Pos() Pos

type Expr Uses

type Expr interface {
    Node
    // contains filtered or unexported methods
}

Expr is the interface for expression.

type File Uses

type File struct {
    Start Pos         // position of start of node.
    Body  []Statement // top-level declarations; or nil
}

File node represents a Vim script source file. Equivalent to NODE_TOPLEVEL of vim-vimlparser. vimlparser: TOPLEVEL .body

func (*File) Pos Uses

func (f *File) Pos() Pos

type Finally Uses

type Finally struct {
    Finally Pos         // position of starting the :finally
    ExArg   ExArg       // Ex command arg
    Body    []Statement // body of else statement
}

vimlparser: FINALLY .ea .body

func (*Finally) Cmd Uses

func (f *Finally) Cmd() Cmd

func (*Finally) Pos Uses

func (f *Finally) Pos() Pos

type For Uses

type For struct {
    For   Pos         // position of starting the :for
    ExArg ExArg       // Ex command arg
    Body  []Statement // body of for statement

    // :for {'a'} in right
    //      ^^^^^ Left
    Left Expr // lhs; or nil

    // :for [{'a'}, b; {'c'}] in right
    //       ^^^^^^^^  ^^^^^
    //       List      Rest
    List []Expr // lhs list; or nil
    Rest Expr   // rest of lhs list; or nil

    Right  Expr // rhs expression.
    EndFor *EndFor
}

vimlparser: FOR .ea .body .left .list .rest .right .endfor

func (*For) Cmd Uses

func (f *For) Cmd() Cmd

func (*For) Pos Uses

func (f *For) Pos() Pos

type FuncAttr Uses

type FuncAttr struct {
    Range   bool
    Abort   bool
    Dict    bool
    Closure bool
}

type Function Uses

type Function struct {
    Func        Pos          // position of starting the :function
    ExArg       ExArg        // Ex command arg
    Body        []Statement  // function body
    Name        Expr         // function name
    Params      []*Ident     // parameters
    Attr        FuncAttr     // function attributes
    EndFunction *EndFunction // :endfunction
}

vimlparser: FUNCTION .ea .body .left .rlist .attr .endfunction

func (*Function) Cmd Uses

func (f *Function) Cmd() Cmd

func (*Function) Pos Uses

func (f *Function) Pos() Pos

type Ident Uses

type Ident struct {
    NamePos Pos    // identifier position
    Name    string // identifier name
}

An Ident node represents an identifier.

func (*Ident) Pos Uses

func (i *Ident) Pos() Pos

type If Uses

type If struct {
    If        Pos         // position of starting the :if
    ExArg     ExArg       // Ex command arg
    Body      []Statement // body of if statement
    Condition Expr        // condition
    ElseIf    []*ElseIf
    Else      *Else
    EndIf     *EndIf
}

vimlparser: IF .ea .body .cond .elseif .else .endif

func (*If) Cmd Uses

func (f *If) Cmd() Cmd

func (*If) Pos Uses

func (f *If) Pos() Pos

type KeyValue Uses

type KeyValue struct {
    Key   Expr
    Value Expr
}

type LambdaExpr Uses

type LambdaExpr struct {
    Lcurlybrace Pos      // position of "{"
    Params      []*Ident // parameters
    Expr        Expr
}

LambdaExpr node represents lambda. vimlparsr: LAMBDA .rlist .left { Params -> Expr }

func (*LambdaExpr) Pos Uses

func (i *LambdaExpr) Pos() Pos

type Let Uses

type Let struct {
    Let   Pos    // position of starting the :let
    ExArg ExArg  // Ex command arg
    Op    string // operator

    // :let {'a'} = 1
    //      ^^^^^ Left
    Left Expr // lhs; or nil

    // :let [{'a'}, b; {'c'}] = [1,2,3,4,5]
    //       ^^^^^^^^  ^^^^^
    //       List      Rest
    List []Expr // lhs list; or nil
    Rest Expr   // rest of lhs list; or nil

    Right Expr // rhs expression.
}

vimlparser: LET .ea .op .left .list .rest .right

func (*Let) Cmd Uses

func (f *Let) Cmd() Cmd

func (*Let) Pos Uses

func (f *Let) Pos() Pos

type List Uses

type List struct {
    Lsquare Pos // position of "["
    Values  []Expr
}

func (*List) Pos Uses

func (c *List) Pos() Pos

type LockVar Uses

type LockVar struct {
    LockVar Pos    // position of starting the :lockvar
    ExArg   ExArg  // Ex command arg
    Depth   int    // default: 0
    List    []Expr // list to lockvar
}

vimlparser: LOCKVAR .ea .depth .list

func (*LockVar) Cmd Uses

func (f *LockVar) Cmd() Cmd

func (*LockVar) Pos Uses

func (f *LockVar) Pos() Pos

type Node Uses

type Node interface {
    Pos() Pos // position of first character belonging to the node
}

Node is the interface for all node types to implement.

type ParenExpr Uses

type ParenExpr struct {
    Lparen Pos  // position of "("
    X      Expr // parenthesized expression
}

ParenExpr node represents a parenthesized expression. vimlparsr: PARENEXPR .value

func (*ParenExpr) Pos Uses

func (i *ParenExpr) Pos() Pos

type Pos Uses

type Pos struct {
    Offset int // offset, starting at 0
    Line   int // line number, starting at 1
    Column int // column number, starting at 1 (byte count)

    // Should I support Filename?
    Filename string // filename, if any
}

Pos represents node position.

func (Pos) String Uses

func (pos Pos) String() string

String returns a string in one of several forms:

file:line:column    valid position with file name
line:column         valid position without file name

type Return Uses

type Return struct {
    Return Pos   // position of starting the :return
    ExArg  ExArg // Ex command arg
    Result Expr  // expression to return
}

vimlparser: RETURN .ea .left

func (*Return) Cmd Uses

func (f *Return) Cmd() Cmd

func (*Return) Pos Uses

func (f *Return) Pos() Pos

type SliceExpr Uses

type SliceExpr struct {
    X      Expr // expression
    Lbrack Pos  // position of "["
    Low    Expr // begin of slice range; or nil
    High   Expr // end of slice range; or nil
}

X[Low:High]

func (*SliceExpr) Pos Uses

func (f *SliceExpr) Pos() Pos

type Statement Uses

type Statement interface {
    Node
    // contains filtered or unexported methods
}

Statement is the interface for statement (Ex command or Comment).

type SubscriptExpr Uses

type SubscriptExpr struct {
    Lbrack Pos // position of "["
    Left   Expr
    Right  Expr
}

Left[Right]

func (*SubscriptExpr) Pos Uses

func (f *SubscriptExpr) Pos() Pos

type TernaryExpr Uses

type TernaryExpr struct {
    Ternary   Pos // position of starting the :execute
    Condition Expr
    Left      Expr
    Right     Expr
}

vimlparser: TERNARY .cond .left .right Condition ? Left : Right

func (*TernaryExpr) Pos Uses

func (f *TernaryExpr) Pos() Pos

type Throw Uses

type Throw struct {
    Throw Pos   // position of starting the :throw
    ExArg ExArg // Ex command arg
    Expr  Expr
}

vimlparser: THROW .ea .left :throw {Expr}

func (*Throw) Cmd Uses

func (f *Throw) Cmd() Cmd

func (*Throw) Pos Uses

func (f *Throw) Pos() Pos

type Try Uses

type Try struct {
    Try     Pos         // position of starting the :try
    ExArg   ExArg       // Ex command arg
    Body    []Statement // body of try statement
    Catch   []*Catch
    Finally *Finally
    EndTry  *EndTry
}

vimlparser: TRY .ea .body .catch .finally .endtry

func (*Try) Cmd Uses

func (f *Try) Cmd() Cmd

func (*Try) Pos Uses

func (f *Try) Pos() Pos

type UnLet Uses

type UnLet struct {
    UnLet Pos    // position of starting the :unlet
    ExArg ExArg  // Ex command arg
    List  []Expr // list to unlet
}

vimlparser: UNLET .ea .list

func (*UnLet) Cmd Uses

func (f *UnLet) Cmd() Cmd

func (*UnLet) Pos Uses

func (f *UnLet) Pos() Pos

type UnLockVar Uses

type UnLockVar struct {
    UnLockVar Pos    // position of starting the :lockvar
    ExArg     ExArg  // Ex command arg
    Depth     int    // default: 0
    List      []Expr // list to lockvar
}

vimlparser: UNLOCKVAR .ea .depth .list

func (*UnLockVar) Cmd Uses

func (f *UnLockVar) Cmd() Cmd

func (*UnLockVar) Pos Uses

func (f *UnLockVar) Pos() Pos

type UnaryExpr Uses

type UnaryExpr struct {
    OpPos Pos         // position of Op
    Op    token.Token // operator
    X     Expr        // operand
}

func (*UnaryExpr) Pos Uses

func (f *UnaryExpr) Pos() Pos

type Visitor Uses

type Visitor interface {
    Visit(node Node) (w Visitor)
}

A Visitor's Visit method is invoked for each node encountered by Walk. If the result visitor w is not nil, Walk visits each of the children of node with the visitor w, followed by a call of w.Visit(nil). ref: https://golang.org/pkg/go/ast/#Visitor

type While Uses

type While struct {
    While     Pos         // position of starting the :while
    ExArg     ExArg       // Ex command arg
    Body      []Statement // body of while statement
    Condition Expr        // condition
    EndWhile  *EndWhile
}

vimlparser: WHILE .ea .body .cond .endwhile

func (*While) Cmd Uses

func (f *While) Cmd() Cmd

func (*While) Pos Uses

func (f *While) Pos() Pos

Package ast imports 2 packages (graph) and is imported by 4 packages. Updated 2017-05-05. Refresh now. Tools for package owners.