Skip to content

Commit

Permalink
Make updates to consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
tstirrat15 committed Oct 28, 2024
1 parent 5ae8471 commit 4edf35c
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions pkg/composableschemadsl/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,20 @@ func (p *sourceParser) tryConsumeIdentifierLiteral() (AstNode, bool) {
return identNode, true
}

// consumeIdentifierLiteral is similar to the above, but attempts and errors
// rather than checking the token type beforehand
func (p *sourceParser) consumeIdentifierLiteral() AstNode {
identNode := p.startNode(dslshape.NodeTypeIdentifier)
defer p.mustFinishNode()

identifier, ok := p.consumeIdentifier()
if !ok {
return identNode
}
identNode.MustDecorate(dslshape.NodeIdentiferPredicateValue, identifier)
return identNode
}

func (p *sourceParser) tryConsumeNilExpression() (AstNode, bool) {
if !p.isKeyword("nil") {
return nil, false
Expand All @@ -594,6 +608,8 @@ func (p *sourceParser) consumeImport() AstNode {
defer p.mustFinishNode()

// from ...
// NOTE: error handling isn't necessary here because this function is only
// invoked if the `from` keyword is found in the function above.
p.consumeKeyword("from")

// Consume alternating periods and identifiers
Expand All @@ -602,10 +618,7 @@ func (p *sourceParser) consumeImport() AstNode {
return importNode
}

segmentNode, ok := p.tryConsumeIdentifierLiteral()
if !ok {
return importNode
}
segmentNode := p.consumeIdentifierLiteral()
importNode.Connect(dslshape.NodeImportPredicatePathSegment, segmentNode)

if !p.isToken(lexer.TokenTypePeriod) {
Expand All @@ -615,14 +628,13 @@ func (p *sourceParser) consumeImport() AstNode {
}
}

p.consumeKeyword("import")
if ok := p.consumeKeyword("import"); !ok {
return importNode
}

// Consume alternating identifiers and commas until we reach the end of the import statement
for {
definitionNode, ok := p.tryConsumeIdentifierLiteral()
if !ok {
return importNode
}
definitionNode := p.consumeIdentifierLiteral()
importNode.Connect(dslshape.NodeImportPredicateDefinitionName, definitionNode)

if _, ok := p.tryConsumeStatementTerminator(); ok {
Expand Down

0 comments on commit 4edf35c

Please sign in to comment.