aboutsummaryrefslogtreecommitdiff
path: root/driver/semantic_action.go
diff options
context:
space:
mode:
authorRyo Nihei <nihei.dev@gmail.com>2022-04-01 01:39:28 +0900
committerRyo Nihei <nihei.dev@gmail.com>2022-04-01 01:41:41 +0900
commit5c26f617583463382978429f4c3fe550de521d42 (patch)
treec656d783d9f5073c5ee794812d35ae059dd5822a /driver/semantic_action.go
parentFix error messages (diff)
downloadurubu-5c26f617583463382978429f4c3fe550de521d42.tar.gz
urubu-5c26f617583463382978429f4c3fe550de521d42.tar.xz
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.
Diffstat (limited to 'driver/semantic_action.go')
-rw-r--r--driver/semantic_action.go10
1 files changed, 8 insertions, 2 deletions
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,
}
}