Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on duplicate keys in YAML #2290

Merged
merged 2 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ dependencies:
- gitrev == 1.3.*
- hashable == 1.4.*
- language-c == 0.9.*
- libyaml == 0.1.*
- megaparsec == 9.2.*
- microlens-platform == 0.4.*
- parser-combinators == 1.3.*
Expand Down
4 changes: 2 additions & 2 deletions src/Juvix/Compiler/Concrete/Translation/FromSource.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Data.ByteString.UTF8 qualified as BS
import Data.List.NonEmpty.Extra qualified as NonEmpty
import Data.Singletons
import Data.Text qualified as Text
import Data.Yaml
import Juvix.Compiler.Concrete.Data.Highlight.Input (HighlightBuilder, ignoreHighlightBuilder)
import Juvix.Compiler.Concrete.Data.ParsedInfoTable
import Juvix.Compiler.Concrete.Data.ParsedInfoTableBuilder
Expand All @@ -23,6 +22,7 @@ import Juvix.Compiler.Concrete.Translation.FromSource.Lexer hiding
( symbol,
)
import Juvix.Compiler.Pipeline.EntryPoint
import Juvix.Data.Yaml
import Juvix.Extra.Paths
import Juvix.Extra.Strings qualified as Str
import Juvix.Parser.Error
Expand Down Expand Up @@ -341,7 +341,7 @@ parseYaml l r = do
| otherwise -> '{' : str ++ "}"
space
let bs = BS.fromString str'
case decodeEither' bs of
case decodeEither bs of
Left err -> parseFailure off (prettyPrintParseException err)
Right yaml -> return $ WithSource (fromString str) yaml

Expand Down
26 changes: 23 additions & 3 deletions src/Juvix/Data/Yaml.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,38 @@ module Juvix.Data.Yaml
)
where

import Data.Aeson.BetterErrors hiding ((<|>))
import Data.Yaml (FromJSON (..))
import Data.Aeson.BetterErrors hiding (mapError, (<|>))
import Data.Aeson.Internal (formatError)
import Data.Yaml (FromJSON (..), ParseException (..), prettyPrintParseException)
import Data.Yaml.Internal (Warning (..), decodeHelper)
import GHC.IO (unsafePerformIO)
import Juvix.Prelude.Base
import Text.Libyaml qualified as Y

type YamlError = Text

-- | Check that all keys are in the given list.
checkYamlKeys :: [Text] -> Parse Text ()
checkYamlKeys :: [Text] -> Parse YamlError ()
checkYamlKeys keys = do
forEachInObject
( \k ->
unless (k `elem` keys) $
throwCustomError ("unknown key: " <> k)
)
return ()

decodeEither :: FromJSON a => ByteString -> Either ParseException a
decodeEither =
either Left splitEither
. unsafePerformIO
. decodeHelper
. Y.decode
where
splitEither :: ([Warning], Either String a) -> Either ParseException a
splitEither (_, Left s) = Left (AesonException s)
splitEither (w : _, Right _) = Left (AesonException (prettyPrintWarning w))
splitEither ([], Right x) = Right x

prettyPrintWarning :: Warning -> String
prettyPrintWarning = \case
DuplicateKey a -> formatError a "duplicate key"
7 changes: 7 additions & 0 deletions test/Parsing/Negative.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ parserErrorTests =
"Pragmas format error"
$(mkRelDir ".")
$(mkRelFile "PragmasFormat.juvix")
$ \case
ErrMegaparsec {} -> Nothing
_ -> wrongError,
negTest
"Pragmas duplicate keys error"
$(mkRelDir ".")
$(mkRelFile "PragmasDuplicateKeys.juvix")
$ \case
ErrMegaparsec {} -> Nothing
_ -> wrongError
Expand Down
6 changes: 6 additions & 0 deletions tests/negative/PragmasDuplicateKeys.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module PragmasDuplicateKeys;

type Bool := true | false;

{-# inline: true, inline: false #-}
main : Bool := true;
Loading