diff options
Diffstat (limited to 'core/vm/sqlvm/ast')
-rw-r--r-- | core/vm/sqlvm/ast/ast.go | 21 | ||||
-rw-r--r-- | core/vm/sqlvm/ast/printer.go | 16 |
2 files changed, 19 insertions, 18 deletions
diff --git a/core/vm/sqlvm/ast/ast.go b/core/vm/sqlvm/ast/ast.go index 8071fbe19..71e09f21f 100644 --- a/core/vm/sqlvm/ast/ast.go +++ b/core/vm/sqlvm/ast/ast.go @@ -19,6 +19,8 @@ type Node interface { SetPosition(uint32) GetLength() uint32 SetLength(uint32) + GetToken() []byte + SetToken([]byte) GetChildren() []Node } @@ -26,6 +28,7 @@ type Node interface { type NodeBase struct { Position uint32 `print:"-"` Length uint32 `print:"-"` + Token []byte `print:"-"` } // HasPosition returns whether the position is set. @@ -53,16 +56,14 @@ func (n *NodeBase) SetLength(length uint32) { n.Length = length } -// UpdatePosition sets the position of the destination node from two source -// nodes. It is assumed that the destination node consists of multiple tokens -// which can be mapped to child nodes of the destination node. srcLeft should -// be the node representing the left-most token of the destination node, and -// srcRight should represent the right-most token of the destination node. -func UpdatePosition(dest, srcLeft, srcRight Node) { - begin := srcLeft.GetPosition() - end := srcRight.GetPosition() + srcRight.GetLength() - dest.SetPosition(begin) - dest.SetLength(end - begin) +// GetToken returns the corresponding token of the node. +func (n *NodeBase) GetToken() []byte { + return n.Token +} + +// SetToken sets the corresponding token of the node. +func (n *NodeBase) SetToken(token []byte) { + n.Token = token } // --------------------------------------------------------------------------- diff --git a/core/vm/sqlvm/ast/printer.go b/core/vm/sqlvm/ast/printer.go index ac4c43fe0..92683bef1 100644 --- a/core/vm/sqlvm/ast/printer.go +++ b/core/vm/sqlvm/ast/printer.go @@ -35,8 +35,8 @@ func formatString(s string) string { return fmt.Sprintf("%v", []byte(s)) } -func printAST(w io.Writer, n interface{}, s []byte, prefix string, - detail bool, depth int) (int, error) { +func printAST(w io.Writer, n interface{}, prefix string, detail bool, + depth int) (int, error) { indent := strings.Repeat(prefix, depth) indentLong := strings.Repeat(prefix, depth+1) @@ -80,7 +80,7 @@ func printAST(w io.Writer, n interface{}, s []byte, prefix string, } for i := 0; i < l; i++ { v := valueOf.Index(i) - b, err = printAST(w, v.Interface(), s, prefix, detail, depth+1) + b, err = printAST(w, v.Interface(), prefix, detail, depth+1) bytesWritten += b if err != nil { return bytesWritten, err @@ -124,7 +124,7 @@ func printAST(w io.Writer, n interface{}, s []byte, prefix string, length := node.GetLength() if node.HasPosition() { end := begin + length - 1 - token := s[begin : begin+length] + token := node.GetToken() position = fmt.Sprintf("%d-%d %s", begin, end, strconv.Quote(string(token))) } else { @@ -154,7 +154,7 @@ func printAST(w io.Writer, n interface{}, s []byte, prefix string, if err != nil { return bytesWritten, err } - b, err = printAST(w, fields[i].value, s, prefix, detail, depth+2) + b, err = printAST(w, fields[i].value, prefix, detail, depth+2) bytesWritten += b if err != nil { return bytesWritten, err @@ -168,8 +168,8 @@ func printAST(w io.Writer, n interface{}, s []byte, prefix string, } // PrintAST prints AST for debugging. -func PrintAST(output io.Writer, node interface{}, source []byte, - indent string, detail bool) (int, error) { +func PrintAST(output io.Writer, node interface{}, indent string, detail bool) ( + int, error) { - return printAST(output, node, source, indent, detail, 0) + return printAST(output, node, indent, detail, 0) } |