Skip to content

Commit

Permalink
Prohibit error node having children
Browse files Browse the repository at this point in the history
  • Loading branch information
nihei9 committed Aug 4, 2022
1 parent 017b1b2 commit 2fcab70
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
8 changes: 8 additions & 0 deletions spec/test/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,14 @@ func formatSyntaxError(synErr *SyntaxError, gram Grammar, lineOffset int) []byte
}

func (tp *treeParser) genTree(node *Node) (*Tree, error) {
// A node labeled 'error' cannot have children. It always must be (error).
if sym := node.Children[0]; sym.Text == "error" {
if len(node.Children) > 1 {
return nil, fmt.Errorf("%v:%v: error node cannot take children", tp.lineOffset+sym.Row+1, sym.Col+1)
}
return NewTerminalNode(sym.Text, ""), nil
}

if len(node.Children) == 2 && node.Children[1].KindName == "string" {
var text string
str := node.Children[1].Children[0]
Expand Down
77 changes: 77 additions & 0 deletions spec/test/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,83 @@ foo
foo
--
(foo)
`,
parseErr: true,
},
// A node may have just one string node.
{
src: `test
----
foo bar
----
(foo (bar 'bar'))
`,
tc: &TestCase{
Description: "test",
Source: []byte("foo bar"),
Output: NewNonTerminalTree("foo",
NewTerminalNode("bar", "bar"),
).Fill(),
},
},
// A node may have just one pattern node.
{
src: `test
----
foo bar
----
(foo (bar "bar"))
`,
tc: &TestCase{
Description: "test",
Source: []byte("foo bar"),
Output: NewNonTerminalTree("foo",
NewTerminalNode("bar", "bar"),
).Fill(),
},
},
// A node may be the error node.
{
src: `test
----
foo x
----
(foo (error))
`,
tc: &TestCase{
Description: "test",
Source: []byte("foo x"),
Output: NewNonTerminalTree("foo",
NewTerminalNode("error", ""),
).Fill(),
},
},
// The error node cannot have a string node.
{
src: `test
----
foo x
----
(foo (error 'x'))
`,
parseErr: true,
},
// The error node cannot have a pattern node.
{
src: `test
----
foo x
----
(foo (error "x"))
`,
parseErr: true,
},
{
src: `test
----
foo x
----
(foo (error (_ (x 'x'))))
`,
parseErr: true,
},
Expand Down

0 comments on commit 2fcab70

Please sign in to comment.