Skip to content

Commit

Permalink
fix #57: Fix annotation for property access
Browse files Browse the repository at this point in the history
  • Loading branch information
supki committed Oct 8, 2024
1 parent c10baab commit 0992246
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/T/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ prettyError = \case
excerpt ann
TypeError (ann :< _) expected actual value ->
header ann <>
"mismatched types:" <> PP.line <>
PP.indent 2 "expected: " <> PP.pretty (show expected) <> PP.line <>
PP.indent 2 " but got: " <> PP.pretty value <> " : " <> PP.pretty (show actual) <> PP.line <>
excerpt ann
Expand Down
18 changes: 8 additions & 10 deletions src/T/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,11 @@ accessOp =
Postfix (chainl1 (idxP <|> dotP) (pure (flip (.))))
where
idxP = do
ann :+ expIdx <- anned $ do
_ <- symbol "["
expIdx <- expP
_ <- symbol "]"
pure expIdx
ann :+ expIdx <- anned (between (symbol "[") (symbol "]") expP)
pure (\exp -> ann :< Idx exp expIdx)
dotP = do
ann :+ key <- anned $ do
_ <- symbol "."
nameP
pure (\exp -> ann :< Key exp key)
ann0 :+ (ann1 :+ key) <- anned (symbol "." *> nameP_) <* spaces
pure (\exp -> ann0 :< Key exp (ann1 :+ key))

infixOp :: DeltaParsing m => String -> Operator m Exp
infixOp name =
Expand Down Expand Up @@ -365,7 +359,11 @@ varP =

nameP :: DeltaParsing m => m (Ann :+ Name)
nameP =
anned (map fromString (liftA2 (:) firstL (many restL))) <* spaces
nameP_ <* spaces

nameP_ :: DeltaParsing m => m (Ann :+ Name)
nameP_ =
anned (map fromString (liftA2 (:) firstL (many restL)))
where
firstL =
letter <|> char '_'
Expand Down
11 changes: 10 additions & 1 deletion test/T/Parse/AnnSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import T.Prelude

spec :: Spec
spec =
describe "annotations" $
describe "annotations" $ do
context "literals" $ do
it "null" $
errorOf "{{ null + 1 }}" `shouldBe`
Expand Down Expand Up @@ -74,6 +74,15 @@ spec =
\1 | {{ {foo:4} + 1 }}<EOF> \n\
\ | ~~~~~~~ "

context "property access" $ do
it "record" $ do
errorOf "{% set foo = {} foo.bar = [] %}{{ foo.bar }}" `shouldBe`
"(interactive):1:38: error: mismatched types:\n\
\ expected: Renderable\n\
\ but got: [] : Array\n\
\1 | {% set foo = {} foo.bar = [] %}{{ foo.bar }}<EOF> \n\
\ | ~~~~ "

errorOf :: Text -> String
errorOf str =
let
Expand Down

0 comments on commit 0992246

Please sign in to comment.