on master help sign in
Merge pull request #1972 from david-christiansen/ahmadsalim-feature/betteroperatormessage

Update error message in reg050 from PR #1938

commit3233dd1c3f2dc27df1f73c6a8f29c00d06330e56 authorDavid Christiansen <david@davidchristiansen.dk> dateMon 2 Mar 2015, 18:10 (12 years ago) parent8ab9f72c70b2a3
5 files changed+27−20
src/Idris/ParseHelpers.hs+9−2 src/Idris/ParseOps.hs+9−1 src/Idris/Parser.hs+4 test/reg041/ott.idr+1−1 test/reg050/expected+4−16
src/Idris/ParseHelpers.hs+9−2
@@ -119,7 +119,7 @@ singleLineComment :: MonadicParsing m => m ()
119119singleLineComment = (string "--" *>
120120 many (satisfy (not . isEol)) *>
121121 eol *> pure ())
122− <?> "single-line comment"
122+ <?> ""
123123
124124{- | Consumes a multi-line comment
125125
@@ -321,10 +321,17 @@ opChars = ":!#$%&*+./<=>?@\\^|-~"
321321operatorLetter :: MonadicParsing m => m Char
322322operatorLetter = oneOf opChars
323323
324+
325+commentMarkers :: [String]
326+commentMarkers = [ "--", "|||" ]
327+
328+invalidOperators :: [String]
329+invalidOperators = [":", "=>", "->", "<-", "=", "?=", "|", "**", "==>", "\\"]
330+
324331-- | Parses an operator
325332operator :: MonadicParsing m => m String
326333operator = do op <- token . some $ operatorLetter
327− when (op `elem` [":", "=>", "->", "<-", "=", "?=", "|"]) $
334+ when (op `elem` (invalidOperators ++ commentMarkers)) $
328335 fail $ op ++ " is not a valid operator"
329336 return op
330337
src/Idris/ParseOps.hs+9−1
@@ -37,7 +37,8 @@ table fixes
3737 toTable (reverse fixes) ++
3838 [[backtick],
3939 [binary "$" (\fc x y -> flatten $ PApp fc x [pexp y]) AssocRight],
40− [binary "=" (\fc x y -> PEq fc Placeholder Placeholder x y) AssocLeft]]
40+ [binary "=" (\fc x y -> PEq fc Placeholder Placeholder x y) AssocLeft],
41+ [nofixityoperator]]
4142 where
4243 flatten :: PTerm -> PTerm -- flatten application
4344 flatten (PApp fc (PApp _ f as) bs) = flatten (PApp fc f (as ++ bs))
@@ -80,6 +81,13 @@ backtick = Infix (do indentPropHolds gtProp
8081 fc <- getFC
8182 return (\x y -> PApp fc (PRef fc n) [pexp x, pexp y])) AssocNone
8283
84+-- | Operator without fixity (throws an error)
85+nofixityoperator :: Operator IdrisParser PTerm
86+nofixityoperator = Infix (do indentPropHolds gtProp
87+ op <- try operator
88+ unexpected $ "Operator without known fixity: " ++ op) AssocNone
89+
90+
8391{- | Parses an operator in function position i.e. enclosed by `()', with an
8492 optional namespace
8593
src/Idris/Parser.hs+4
@@ -291,6 +291,10 @@ syntaxRule syn
291291
292292 mkSimple' (Expr e : Expr e1 : es) = SimpleExpr e : SimpleExpr e1 :
293293 mkSimple es
294+ -- Can't parse a full expression followed by operator like characters due to ambiguity
295+ mkSimple' (Expr e : Symbol s : es)
296+ | takeWhile (`elem` opChars) ts /= "" && ts `notElem` invalidOperators = SimpleExpr e : Symbol s : mkSimple' es
297+ where ts = dropWhile isSpace . dropWhileEnd isSpace $ s
294298 mkSimple' (e : es) = e : mkSimple' es
295299 mkSimple' [] = []
296300
test/reg041/ott.idr+1−1
@@ -37,6 +37,6 @@ EQ two False two False = one
3737EQ (pi s t) f (pi s' t') g = pi s $ \x => pi s' $ \y => EQ s x s' y ~> EQ (t x) (f x) (t' y) (g y)
3838EQ _ _ _ _ = zero
3939
40−example : <| id == id in (two ~> two) |>
40+example : <| (id == id in (two ~> two)) |>
4141example = ?prf
4242
test/reg050/expected+4−16
@@ -10,20 +10,8 @@ When elaborating an application of function Prelude.Monad.>>=:
1010 List
1111 with
1212 \{uv0} => argTy -> uv
13−./baddoublebang.idr:6:26: error: not
14− a terminator, expected: "$",
15− "&&", "*", "*>", "+", "++", "-",
16− "->", ".", "/", "/=", "::", ";",
17− "<", "<#>", "<$>", "<*", "<*>",
18− "<+>", "<->", "<.>", "<<", "<=",
19− "<|>", "=", "==", ">", ">=",
20− ">>", ">>=", "\\\\", "`", "in",
21− "||", "~=~",
22− ambiguous use of a left-associative operator,
23− ambiguous use of a non-associative operator,
24− ambiguous use of a right-associative operator,
25− end of input, function argument,
26− matching application expression,
27− single-line comment
13+./baddoublebang.idr:6:28: error: unexpected
14+ Operator without known fixity:
15+ !!, expected: space
2816doubleBang mmn = do pure !!mmn
29− ^
17+ ^