diff options
author | Ryo Nihei <nihei.dev@gmail.com> | 2022-06-10 16:41:41 +0900 |
---|---|---|
committer | Ryo Nihei <nihei.dev@gmail.com> | 2022-06-11 19:09:19 +0900 |
commit | 7403c18fbc04f3045df1e69b63d7ffd5f04d77db (patch) | |
tree | 0933a8e875bac01b1b5b385b5069eb069aaad219 /driver/semantic_action.go | |
parent | Support testable tree output in vartan-parse command (diff) | |
download | urubu-7403c18fbc04f3045df1e69b63d7ffd5f04d77db.tar.gz urubu-7403c18fbc04f3045df1e69b63d7ffd5f04d77db.tar.xz |
Remove the kind field from a node corresponding to an anonymous terminal symbol
Diffstat (limited to 'driver/semantic_action.go')
-rw-r--r-- | driver/semantic_action.go | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/driver/semantic_action.go b/driver/semantic_action.go index 73f3bb0..54d3291 100644 --- a/driver/semantic_action.go +++ b/driver/semantic_action.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "io" + "strconv" ) // SemanticActionSet is a set of semantic actions a parser calls. @@ -270,6 +271,19 @@ func (n *Node) MarshalJSON() ([]byte, error) { KindName: n.KindName, }) case NodeTypeTerminal: + if n.KindName == "" { + return json.Marshal(struct { + Type NodeType `json:"type"` + Text string `json:"text"` + Row int `json:"row"` + Col int `json:"col"` + }{ + Type: n.Type, + Text: n.Text, + Row: n.Row, + Col: n.Col, + }) + } return json.Marshal(struct { Type NodeType `json:"type"` KindName string `json:"kind_name"` @@ -324,9 +338,13 @@ func printTree(w io.Writer, node *Node, ruledLine string, childRuledLinePrefix s switch node.Type { case NodeTypeError: - fmt.Fprintf(w, "%v!%v\n", ruledLine, node.KindName) + fmt.Fprintf(w, "%v%v\n", ruledLine, node.KindName) case NodeTypeTerminal: - fmt.Fprintf(w, "%v%v %#v\n", ruledLine, node.KindName, node.Text) + if node.KindName == "" { + fmt.Fprintf(w, "%v<anonymous> %v\n", ruledLine, strconv.Quote(node.Text)) + } else { + fmt.Fprintf(w, "%v%v %v\n", ruledLine, node.KindName, strconv.Quote(node.Text)) + } case NodeTypeNonTerminal: fmt.Fprintf(w, "%v%v\n", ruledLine, node.KindName) |