Skip to content

Commit

Permalink
Update parser behavior to reflect new keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
tstirrat15 committed Oct 28, 2024
1 parent 0e50f16 commit 503d3eb
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions pkg/composableschemadsl/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (p *sourceParser) consumeCaveatTypeReference() AstNode {
typeRefNode := p.startNode(dslshape.NodeTypeCaveatTypeReference)
defer p.mustFinishNode()

name, ok := p.consumeIdentifier()
name, ok := p.consumeCaveatTypeIdentifier()
if !ok {
return typeRefNode
}
Expand All @@ -231,6 +231,21 @@ func (p *sourceParser) consumeCaveatTypeReference() AstNode {
return typeRefNode
}

// "any" is both a keyword and a valid caveat type, so a caveat type identifier
// can either be a keyword or an identifier. This wraps around that.
func (p *sourceParser) consumeCaveatTypeIdentifier() (string, bool) {
if ok := p.tryConsumeKeyword("any"); ok {
return "any", true
}

identifier, ok := p.tryConsume(lexer.TokenTypeIdentifier)
if !ok {
p.emitErrorf("Expected keyword \"any\" or a valid identifier, found token %v", p.currentToken.Kind)
return "", false
}
return identifier.Value, true
}

// consumeDefinition attempts to consume a single schema definition.
// ```definition somedef { ... }```
func (p *sourceParser) consumeDefinition() AstNode {
Expand Down Expand Up @@ -480,17 +495,11 @@ func (p *sourceParser) tryConsumeArrowExpression() (AstNode, bool) {
rightNodeBuilder := func(leftNode AstNode, operatorToken lexer.Lexeme) (AstNode, bool) {
// Check for an arrow function.
if operatorToken.Kind == lexer.TokenTypePeriod {
functionName, ok := p.consumeIdentifier()
functionName, ok := p.consumeKeywords("any", "all")
if !ok {
return nil, false
}

// TODO(jschorr): Change to keywords in schema v2.
if functionName != "any" && functionName != "all" {
p.emitErrorf("Expected 'any' or 'all' for arrow function, found: %s", functionName)
return nil, false
}

if _, ok := p.consume(lexer.TokenTypeLeftParen); !ok {
return nil, false
}
Expand Down

0 comments on commit 503d3eb

Please sign in to comment.