Skip to content

Commit

Permalink
feat(aeria-compiler): allow 'always' and 'on-write' values in owned c…
Browse files Browse the repository at this point in the history
…ollection property #36
  • Loading branch information
gabrielolivrp committed Aug 7, 2024
1 parent 7256cec commit aeb25a2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
3 changes: 2 additions & 1 deletion aeria-compiler/src/Aeria/Codegen/Collection.purs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ cIcon :: CollectionIcon -> Js.Tree
cIcon (CollectionIcon icon) = Js.string icon

cOwned :: CollectionOwned -> Js.Tree
cOwned (CollectionOwned owned) = Js.boolean owned
cOwned (CollectionOwnedBoolean _ owned) = Js.boolean owned
cOwned (CollectionOwnedCustom _ owned) = Js.string owned

cTimestamps :: CollectionTimestamps -> Js.Tree
cTimestamps (CollectionTimestamps timestamps) = Js.boolean timestamps
Expand Down
10 changes: 10 additions & 0 deletions aeria-compiler/src/Aeria/Semantic.purs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ sDescription :: Collection -> SemanticM Context
sDescription (Collection
{ name
, properties
, owned
, getters
, table
, tableMeta
Expand All @@ -51,6 +52,7 @@ sDescription (Collection
where
go = do
sProperties name properties
sOwned owned
sRequired name required
sTableLayout name tableLayout
sLayout name layout
Expand All @@ -69,6 +71,14 @@ sDescription (Collection
sSearch name search
ask

sOwned :: Maybe CollectionOwned -> SemanticM Unit
sOwned (Just (CollectionOwnedCustom span owned)) =
case owned of
"always" -> pure unit
"on-write" -> pure unit
_ -> throwDiagnostic span "Expected value \"always\", \"on-write\" or boolean"
sOwned _ = pure unit

sFiltersPresets :: CollectionFiltersPresets -> SemanticM Unit
sFiltersPresets = traverse_ go
where
Expand Down
35 changes: 23 additions & 12 deletions aeria-compiler/src/Aeria/Syntax/Parser.purs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ sourcePos = do
pName :: forall a. (Span -> String -> a) -> ParserM a
pName constructor = do
begin <- sourcePos
ident <- lang.identifier <?> "Expected an identifier"
ident <- lang.identifier <?> "an identifier"
end <- sourcePos
pure $ constructor (Span begin end) ident

Expand Down Expand Up @@ -79,7 +79,7 @@ pPropertyType p = fix \self -> choice
, try tPrimitives
, try tCollection
, try (tObject self)
] <?> "Expected a property type"
] <?> "a property type"
where
tPrimitives = choice [tConst, tStr, tBool, tInt, tNum, tEnum]
tArray self = pArrayType self
Expand All @@ -88,7 +88,7 @@ pPropertyType p = fix \self -> choice

pArrayType self = do
begin <- sourcePos
_ <- string "[]" <?> "Expected \"[]\" for array type"
_ <- string "[]" <?> "\"[]\" for array type"
arrType <- self
end <- sourcePos
pure $ PArray (Span begin end) arrType
Expand Down Expand Up @@ -124,12 +124,12 @@ pPropertyType p = fix \self -> choice

pSimpleType constructor keyword = do
begin <- sourcePos
_ <- lang.reservedOp keyword <?> ("Expected \"" <> keyword <> "\"")
_ <- lang.reservedOp keyword <?> ("\"" <> keyword <> "\"")
end <- sourcePos
pure $ constructor (Span begin end)

pBoolean :: ParserM Boolean
pBoolean = pTrue <|> pFalse <?> "Expected a boolean (\"true\" or \"false\")"
pBoolean = pTrue <|> pFalse <?> "a boolean (\"true\" or \"false\")"
where
pTrue :: ParserM Boolean
pTrue = lang.reserved "true" $> true
Expand All @@ -147,7 +147,7 @@ pLiteral = fix \self -> choice
, try pBoolean'
, try pProp
, pArray self
] <?> "Expected a literal value"
] <?> "a literal value"
where
pInteger = pLiteralValue LInteger lang.integer
pNum = pLiteralValue LNum lang.float
Expand Down Expand Up @@ -201,13 +201,13 @@ pExpr = fix \self -> buildExprParser table (expr self)
binary name fun assoc = Infix go assoc
where
go = do
lang.reservedOp name <?> ("Expected binary operator \"" <> name <> "\"")
lang.reservedOp name <?> ("binary operator \"" <> name <> "\"")
pure fun

unary name fun = Prefix go
where
go = do
lang.reservedOp name <?> ("Expected unary operator \"" <> name <> "\"")
lang.reservedOp name <?> ("unary operator \"" <> name <> "\"")
pure fun

expr self = lang.parens self <|> value
Expand All @@ -217,7 +217,7 @@ pExpr = fix \self -> buildExprParser table (expr self)
pAttribute :: ParserM Attribute
pAttribute = do
begin <- sourcePos
attributeName <- lang.reservedOp "@" *> pAttributeName <?> "Expected attribute name starting with \"@\""
attributeName <- lang.reservedOp "@" *> pAttributeName <?> "attribute name starting with \"@\""
attributeValue <- case attributeName of
AttributeName _ "constraints" -> do
beginAttributeValue <- sourcePos
Expand Down Expand Up @@ -250,7 +250,7 @@ pCond :: ParserM Cond
pCond = do
lang.reserved "@cond"
begin <- sourcePos
expr <- lang.parens pExpr <?> "Expected condition expression"
expr <- lang.parens pExpr <?> "condition expression"
end <- sourcePos
pure (Cond (Span begin end) expr)

Expand Down Expand Up @@ -337,7 +337,18 @@ pCollectionIcon :: ParserM CollectionIcon
pCollectionIcon = CollectionIcon <$> lang.stringLiteral

pCollectionOwned :: ParserM CollectionOwned
pCollectionOwned = CollectionOwned <$> pBoolean
pCollectionOwned = pCollectionOwnedBoolean <|> pCollectionOwnedCustom
where
pCollectionOwnedBoolean = do
start <- sourcePos
value <- pBoolean
end <- sourcePos
pure $ CollectionOwnedBoolean (Span start end) value
pCollectionOwnedCustom = do
start <- sourcePos
value <- lang.stringLiteral
end <- sourcePos
pure $ CollectionOwnedCustom (Span start end) value

pCollectionTimestamps :: ParserM CollectionTimestamps
pCollectionTimestamps = CollectionTimestamps <$> pBoolean
Expand Down Expand Up @@ -680,7 +691,7 @@ pCollectionLayout = lang.braces $ do
, name: name'
, options
}
Nothing -> fail "Expected 'name' property in layout"
Nothing -> fail "'name' property in layout"

where
allParsers' =
Expand Down
13 changes: 10 additions & 3 deletions aeria-compiler/src/Aeria/Syntax/Tree.purs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,9 @@ instance WriteForeign CollectionIcon where
, icon: writeImpl icon
}

data CollectionOwned = CollectionOwned Boolean
data CollectionOwned
= CollectionOwnedBoolean Span Boolean
| CollectionOwnedCustom Span String

derive instance genericCollectionOwned :: Generic CollectionOwned _

Expand All @@ -576,9 +578,14 @@ instance eqCollectionOwned :: Eq CollectionOwned where
eq = genericEq

instance WriteForeign CollectionOwned where
writeImpl (CollectionOwned owned) =
writeImpl (CollectionOwnedBoolean _ owned) =
writeImpl
{ kind: "CollectionOwned"
{ kind: "CollectionOwnedBoolean"
, owned: writeImpl owned
}
writeImpl (CollectionOwnedCustom _ owned) =
writeImpl
{ kind: "CollectionOwnedCustom"
, owned: writeImpl owned
}

Expand Down
2 changes: 1 addition & 1 deletion aeria-compiler/test/Suite/Syntax/collection_owned.golden
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"kind": "Collection",
"owned": {
"owned": true,
"kind": "CollectionOwned"
"kind": "CollectionOwnedBoolean"
}
}
]
Expand Down

0 comments on commit aeb25a2

Please sign in to comment.