Skip to content

Commit

Permalink
feat(compiler): add support for multiple array types
Browse files Browse the repository at this point in the history
  • Loading branch information
minenwerfer committed Feb 15, 2025
1 parent 55c2abf commit ae6b928
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-peas-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@aeriajs/compiler": patch
---

Add support for multiple array types
29 changes: 21 additions & 8 deletions packages/compiler/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,23 @@ export const parse = (tokens: (Token | undefined)[]) => {
}
}

const parseArray = <TTokenType extends TokenType>(type: TTokenType) => {
consume(TokenType.LeftSquareBracket)
const parseArray = <TTokenType extends TokenType>(types: TTokenType[]) => {
const { location } = consume(TokenType.LeftSquareBracket)

const array: unknown[] = []
let type: TokenType | undefined

for( const typeCandidate of types ) {
if( match(typeCandidate) ) {
type = typeCandidate
break
}
}

if( !type ) {
throw new Diagnostic(`array got an invalid type, accepted ones are: ${types.join(' | ')}`, location)
}

while( !match(TokenType.RightSquareBracket) ) {
const { value } = consume(type)
array.push(value)
Expand Down Expand Up @@ -197,7 +210,7 @@ export const parse = (tokens: (Token | undefined)[]) => {
}

if( 'enum' in property && attributeName === 'values' ) {
property.enum = parseArray(TokenType.QuotedString)
property.enum = parseArray([TokenType.QuotedString, TokenType.Number])
return
}

Expand Down Expand Up @@ -226,7 +239,7 @@ export const parse = (tokens: (Token | undefined)[]) => {
case 'form':
case 'populate':
case 'indexes': {
property[attributeName] = parseArray(TokenType.Identifier)
property[attributeName] = parseArray([TokenType.Identifier])
return
}
case 'populateDepth': {
Expand All @@ -240,7 +253,7 @@ export const parse = (tokens: (Token | undefined)[]) => {
switch( attributeName ) {
case 'extensions':
case 'accept': {
property[attributeName] = parseArray(TokenType.QuotedString)
property[attributeName] = parseArray([TokenType.QuotedString])
return
}
}
Expand All @@ -258,7 +271,7 @@ export const parse = (tokens: (Token | undefined)[]) => {
}
case 'mask': {
if( match(TokenType.LeftSquareBracket) ) {
property[attributeName] = parseArray(TokenType.QuotedString)
property[attributeName] = parseArray([TokenType.QuotedString])
return
} else {
const { value } = consume(TokenType.QuotedString)
Expand Down Expand Up @@ -778,7 +791,7 @@ export const parse = (tokens: (Token | undefined)[]) => {
accessCondition: value,
}
} else {
const value = parseArray(TokenType.QuotedString)
const value = parseArray([TokenType.QuotedString])
functions[functionName] = {
accessCondition: value as readonly UserRole[],
}
Expand Down Expand Up @@ -862,7 +875,7 @@ export const parse = (tokens: (Token | undefined)[]) => {
}
case 'roles':
case 'requires': {
const value = parseArray(TokenType.Identifier)
const value = parseArray([TokenType.Identifier])
baseSlots[keyword] = value
break
}
Expand Down

0 comments on commit ae6b928

Please sign in to comment.