Skip to content

Commit

Permalink
implement error for double braces
Browse files Browse the repository at this point in the history
  • Loading branch information
janmasrovira committed Jul 20, 2022
1 parent 841b4b4 commit 15023f7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/Juvix/Syntax/Concrete/Scoped/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ data ScoperError
| ErrMultipleCompileRuleSameBackend MultipleCompileRuleSameBackend
| ErrWrongKindExpressionCompileBlock WrongKindExpressionCompileBlock
| ErrDuplicateInductiveParameterName DuplicateInductiveParameterName
| ErrDoubleBracesPattern DoubleBracesPattern
deriving stock (Show)

instance ToGenericError ScoperError where
Expand Down Expand Up @@ -60,3 +61,4 @@ instance ToGenericError ScoperError where
ErrMultipleCompileRuleSameBackend e -> genericError e
ErrWrongKindExpressionCompileBlock e -> genericError e
ErrDuplicateInductiveParameterName e -> genericError e
ErrDoubleBracesPattern e -> genericError e
23 changes: 23 additions & 0 deletions src/Juvix/Syntax/Concrete/Scoped/Error/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -546,3 +546,26 @@ instance ToGenericError DuplicateInductiveParameterName where
<> "."
<> line
<> "Inductive parameter names can not be repeated."

newtype DoubleBracesPattern = DoubleBracesPattern
{ _doubleBracesPatternArg :: PatternArg
}
deriving stock (Show)

makeLenses ''DoubleBracesPattern

instance ToGenericError DoubleBracesPattern where
genericError e =
GenericError
{ _genericErrorLoc = i,
_genericErrorMessage = prettyError msg,
_genericErrorIntervals = [i]
}
where
pat :: PatternArg
pat = e ^. doubleBracesPatternArg
i = getLoc (e ^. doubleBracesPatternArg)
msg =
"Double braces are not valid:"
-- TODO add bold to braces
<+> braces (ppCode pat)
24 changes: 16 additions & 8 deletions src/Juvix/Syntax/Concrete/Scoped/Scoper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,7 @@ parseTerm =
-- Infix Patterns
-------------------------------------------------------------------------------

type ParsePat = P.Parsec () [PatternAtom 'Scoped]
type ParsePat = P.ParsecT () [PatternAtom 'Scoped] (Sem '[Error ScoperError])

makePatternTable ::
PatternAtoms 'Scoped -> [[P.Operator ParsePat PatternArg]]
Expand Down Expand Up @@ -1543,11 +1543,18 @@ parsePatternTerm = do
_ -> Nothing

parseBraces :: ParsePat PatternArg
parseBraces = P.token bracesPat mempty
parseBraces = do
res <- P.token bracesPat mempty
case res of
Left er -> P.lift (throw er)
Right a -> return a
where
bracesPat :: PatternAtom 'Scoped -> Maybe PatternArg
bracesPat :: PatternAtom 'Scoped -> Maybe (Either ScoperError PatternArg)
bracesPat s = case s of
PatternAtomBraces r -> Just (set patternArgIsImplicit Implicit r)
PatternAtomBraces r
| Implicit <- r ^. patternArgIsImplicit ->
Just (Left (ErrDoubleBracesPattern (DoubleBracesPattern r)))
| otherwise -> Just (Right (set patternArgIsImplicit Implicit r))
_ -> Nothing

parseParens :: ParsePat PatternArg
Expand Down Expand Up @@ -1587,15 +1594,16 @@ parsePatternAtoms ::
PatternAtoms 'Scoped ->
Sem r PatternArg
parsePatternAtoms atoms@(PatternAtoms sec' _) = do
case res of
Left {} -> throw (ErrInfixPattern (InfixErrorP atoms))
Right r -> return r
case run (runError res) of
Left e -> throw e -- Scoper effect error
Right Left {} -> throw (ErrInfixPattern (InfixErrorP atoms)) -- Megaparsec error
Right (Right r) -> return r
where
sec = toList sec'
tbl = makePatternTable atoms
parser :: ParsePat PatternArg
parser = runM (mkPatternParser tbl) <* P.eof
res = P.parse parser filePath sec
res = P.runParserT parser filePath sec

filePath :: FilePath
filePath = "tmp"
7 changes: 7 additions & 0 deletions test/Scope/Negative.hs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ scoperErrorTests =
$ \case
ErrQualSymNotInScope {} -> Nothing
_ -> wrongError,
NegTest
"Double braces in pattern"
"."
"NestedPatternBraces.juvix"
$ \case
ErrDoubleBracesPattern {} -> Nothing
_ -> wrongError,
NegTest
"Compile block for a unsupported kind of expression"
"CompileBlocks"
Expand Down
6 changes: 6 additions & 0 deletions tests/negative/NestedPatternBraces.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module NestedPatternBraces;

a : {A : Type} → Type;
a {{A}} ≔ a;

end;

0 comments on commit 15023f7

Please sign in to comment.