Skip to content

update vim-vimlparser #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Dec 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
implement HeredocExpr
  • Loading branch information
itchyny committed Dec 6, 2020
commit a57c52d40be416c02ff125fdf995e1ab33e76c93
16 changes: 14 additions & 2 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ type Ident struct {
func (i *Ident) Pos() Pos { return i.NamePos }

// LambdaExpr node represents lambda.
// vimlparsr: LAMBDA .rlist .left
// vimlparser: LAMBDA .rlist .left
// { Params -> Expr }
type LambdaExpr struct {
Lcurlybrace Pos // position of "{"
Expand All @@ -528,14 +528,25 @@ type LambdaExpr struct {
func (i *LambdaExpr) Pos() Pos { return i.Lcurlybrace }

// ParenExpr node represents a parenthesized expression.
// vimlparsr: PARENEXPR .value
// vimlparser: PARENEXPR .value
type ParenExpr struct {
Lparen Pos // position of "("
X Expr // parenthesized expression
}

func (i *ParenExpr) Pos() Pos { return i.Lparen }

// HeredocExpr node represents a heredoc expression.
// vimlparser: HEREDOC .rlist .op .body
type HeredocExpr struct {
OpPos Pos // position of "=<<"
Flags []Expr // modifiers [trim]; or nil
EndMarker string // {endmarker}
Body []Expr // body
}

func (i *HeredocExpr) Pos() Pos { return i.OpPos }

// stmtNode() ensures that only ExComamnd and Comment nodes can be assigned to
// an Statement.
//
Expand Down Expand Up @@ -588,3 +599,4 @@ func (*CurlyNameExpr) exprNode() {}
func (*Ident) exprNode() {}
func (*LambdaExpr) exprNode() {}
func (*ParenExpr) exprNode() {}
func (*HeredocExpr) exprNode() {}
4 changes: 4 additions & 0 deletions ast/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ func Walk(v Visitor, node Node) {
case *ParenExpr:
Walk(v, n.X)

case *HeredocExpr:
walkExprList(v, n.Flags)
walkExprList(v, n.Body)

default:
panic(fmt.Sprintf("ast.Walk: unexpected node type %T", n))
}
Expand Down
33 changes: 32 additions & 1 deletion compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,28 @@ func (c *Compiler) compileExpr(node ast.Expr) string {
params = append(params, p.Name)
}
return fmt.Sprintf("(lambda (%s) %s)", strings.Join(params, " "), c.compileExpr(n.Expr))
case *ast.HeredocExpr:
var flags string
if len(n.Flags) == 0 {
flags = "(list)"
} else {
xs := make([]string, len(n.Flags))
for i, f := range n.Flags {
xs[i] = `"` + c.compileExpr(f) + `"`
}
flags = fmt.Sprintf("(list %s)", strings.Join(xs, " "))
}
var body string
if len(n.Body) == 0 {
body = "(list)"
} else {
xs := make([]string, len(n.Body))
for i, p := range n.Body {
xs[i] = `"` + escape(c.compileExpr(p), "\n\t\r") + `"`
}
body = fmt.Sprintf("(list %s)", strings.Join(xs, " "))
}
return fmt.Sprintf(`(heredoc %s "%s" %s)`, flags, n.EndMarker, body)
case *ast.ParenExpr:
return c.compileExpr(n.X)
}
Expand All @@ -450,7 +472,16 @@ func escape(s string, chars string) string {
r := ""
for _, c := range s {
if strings.IndexRune(chars, c) != -1 {
r += `\` + string(c)
switch c {
case '\n':
r += `\n`
case '\t':
r += `\t`
case '\r':
r += `\r`
default:
r += `\` + string(c)
}
} else {
r += string(c)
}
Expand Down
16 changes: 16 additions & 0 deletions go/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,14 @@ func newAstNode(n *VimNode, filename string) ast.Node {
Value: n.value.(string),
}

case NODE_HEREDOC:
return &ast.HeredocExpr{
OpPos: pos,
Flags: newRlist(*n, filename),
EndMarker: n.op,
Body: newBodyExprs(*n, filename),
}

case NODE_PARENEXPR:
n := n.value.(*VimNode)
return &ast.ParenExpr{
Expand Down Expand Up @@ -535,6 +543,14 @@ func newBody(n VimNode, filename string) []ast.Statement {
return body
}

func newBodyExprs(n VimNode, filename string) []ast.Expr {
body := make([]ast.Expr, len(n.body))
for i, node := range n.body {
body[i] = newExprNode(node, filename)
}
return body
}

func newIdents(n VimNode, filename string) []*ast.Ident {
var idents []*ast.Ident
if n.rlist != nil {
Expand Down