From 5c26f617583463382978429f4c3fe550de521d42 Mon Sep 17 00:00:00 2001 From: Ryo Nihei Date: Fri, 1 Apr 2022 01:39:28 +0900 Subject: Print a parse tree even if syntax error occur A parser can construct a parse tree even if syntax error occur. When there is a parse tree, print it. --- driver/semantic_action.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'driver/semantic_action.go') diff --git a/driver/semantic_action.go b/driver/semantic_action.go index d88d5fa..61b00f0 100644 --- a/driver/semantic_action.go +++ b/driver/semantic_action.go @@ -37,6 +37,7 @@ type Node struct { Row int Col int Children []*Node + Error bool } func PrintTree(w io.Writer, node *Node) { @@ -48,9 +49,12 @@ func printTree(w io.Writer, node *Node, ruledLine string, childRuledLinePrefix s return } - if node.Text != "" { + switch { + case node.Error: + fmt.Fprintf(w, "%v!%v\n", ruledLine, node.KindName) + case node.Text != "": fmt.Fprintf(w, "%v%v %#v\n", ruledLine, node.KindName, node.Text) - } else { + default: fmt.Fprintf(w, "%v%v\n", ruledLine, node.KindName) } @@ -212,11 +216,13 @@ func (a *SyntaxTreeActionSet) TrapAndShiftError(cause VToken, popped int) { if a.makeAST { ast = &Node{ KindName: a.gram.Terminal(a.gram.Error()), + Error: true, } } if a.makeCST { cst = &Node{ KindName: a.gram.Terminal(a.gram.Error()), + Error: true, } } -- cgit v1.2.3