Skip to content

Commit

Permalink
Add caveat expression consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
tstirrat15 committed Nov 23, 2024
1 parent ec3713c commit 87e8c0b
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions pkg/composableschemadsl/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (p *sourceParser) consumeCaveat() AstNode {
return defNode
}

exprNode, ok := p.consumeCaveatExpression()
exprNode, ok := p.consumeOpaqueBraceExpression()
if !ok {
return defNode
}
Expand All @@ -139,13 +139,16 @@ func (p *sourceParser) consumeCaveat() AstNode {
return defNode
}

func (p *sourceParser) consumeCaveatExpression() (AstNode, bool) {
exprNode := p.startNode(dslshape.NodeTypeCaveatExpression)
// Consume from an opening brace to a closing brace, returning all tokens as a single string.
// Used for caveat CEL expressions and test JSON expressions.
// Keeps track of brace balance.
// Special Logic Note: Since CEL is its own language, we consume here until we have a matching
// close brace, and then pass ALL the found tokens to CEL's own parser to attach the expression
// here.
func (p *sourceParser) consumeOpaqueBraceExpression() (AstNode, bool) {
exprNode := p.startNode(dslshape.NodeTypeOpaqueBraceExpression)
defer p.mustFinishNode()

// Special Logic Note: Since CEL is its own language, we consume here until we have a matching
// close brace, and then pass ALL the found tokens to CEL's own parser to attach the expression
// here.
braceDepth := 1 // Starting at 1 from the open brace above
var startToken *commentedLexeme
var endToken *commentedLexeme
Expand Down Expand Up @@ -185,7 +188,7 @@ consumer:
}

caveatExpression := p.input[startToken.Position : int(endToken.Position)+len(endToken.Value)]
exprNode.MustDecorate(dslshape.NodeCaveatExpressionPredicateExpression, caveatExpression)
exprNode.MustDecorate(dslshape.NodeOpaqueBraceExpressionPredicateExpression, caveatExpression)
return exprNode, true
}

Expand Down Expand Up @@ -770,7 +773,6 @@ func (p *sourceParser) consumeTestRelation() AstNode {
// A relation looks like:
// object:foo relation subject:bar
// object consumption

objectNode, ok := p.consumeTestObject()
if !ok {
return relationNode
Expand All @@ -791,6 +793,24 @@ func (p *sourceParser) consumeTestRelation() AstNode {
}
relationNode.Connect(dslshape.NodeTestRelationPredicateSubject, subjectNode)

// optional caveat consumption
if p.tryConsumeKeyword("with") {
caveatName, ok := p.consumeIdentifier()
if !ok {
return relationNode
}
relationNode.MustDecorate(dslshape.NodeTestRelationPredicateCaveatName, caveatName)

// optional caveat context
if _, ok := p.tryConsume(lexer.TokenTypeLeftBrace); ok {
caveatContextNode, ok := p.consumeOpaqueBraceExpression()
if !ok {
return relationNode
}
relationNode.Connect(dslshape.NodeTestRelationPredicateCaveatContext, caveatContextNode)
}
}

return relationNode
}

Expand Down Expand Up @@ -851,7 +871,7 @@ func (p *sourceParser) consumeTestAssertions() AstNode {
func (p *sourceParser) consumeTestAssertion() AstNode {
assertionNode := p.startNode(dslshape.NodeTypeImport)
defer p.mustFinishNode()

return assertionNode
}

Expand Down

0 comments on commit 87e8c0b

Please sign in to comment.