aboutsummaryrefslogtreecommitdiff
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
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.
-rw-r--r--cmd/vartan/parse.go12
-rw-r--r--driver/semantic_action.go10
2 files changed, 18 insertions, 4 deletions
diff --git a/cmd/vartan/parse.go b/cmd/vartan/parse.go
index 9b15251..14412bf 100644
--- a/cmd/vartan/parse.go
+++ b/cmd/vartan/parse.go
@@ -141,14 +141,22 @@ func runParse(cmd *cobra.Command, args []string) (retErr error) {
fmt.Fprintf(os.Stderr, "\n")
}
- if len(synErrs) == 0 && !*parseFlags.onlyParse {
+ if !*parseFlags.onlyParse {
+ // A parser can construct a parse tree even if syntax errors occur.
+ // When therer is a parse tree, print it.
+
var tree *driver.Node
if *parseFlags.cst {
tree = treeAct.CST()
} else {
tree = treeAct.AST()
}
- driver.PrintTree(os.Stdout, tree)
+ if tree != nil {
+ if len(synErrs) > 0 {
+ fmt.Println("")
+ }
+ driver.PrintTree(os.Stdout, tree)
+ }
}
return nil
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,
}
}