Skip to content

Commit

Permalink
fix: more robust parser
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl committed Nov 14, 2022
1 parent 05c9a01 commit d38e006
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion internal/schema/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,12 @@ func (p *parser) parsePermissionExpressions(finalToken itemType, depth int) *ast

case item.Typ == itemOperatorAnd, item.Typ == itemOperatorOr:
p.next() // consume operator

// A nil root means that we saw a binary expression before the first
// expression.
if root == nil {
return nil
}
newRoot := &ast.SubjectSetRewrite{
Operation: setOperation(item.Typ),
Children: []ast.Child{root},
Expand Down Expand Up @@ -491,7 +497,7 @@ func simplifyExpression(root *ast.SubjectSetRewrite) *ast.SubjectSetRewrite {
}
var newChildren []ast.Child
for _, child := range root.Children {
if ch, ok := child.(*ast.SubjectSetRewrite); ok && ch.Operation == root.Operation {
if ch, ok := child.(*ast.SubjectSetRewrite); ok && ch != nil && ch.Operation == root.Operation {
// merge child and root
simplifyExpression(ch)
newChildren = append(newChildren, ch.Children...)
Expand Down
7 changes: 7 additions & 0 deletions internal/schema/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ var parserErrorTestCases = []struct{ name, input string }{
this.related.siblings.traverse(s => s.permits.edit(ctx)),
}
}
`},
{"parser error", `
class Resource implements Namespace {
permits = {
update: (ctx: Context) => ||
this.related.annotators.traverse((role) => role.related.member.includes(ctx.subject)) ||
this.related.supervisors.traverse((role) => role.related.member.includes(ctx.subject)),
`},
}

Expand Down

0 comments on commit d38e006

Please sign in to comment.