Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 14 commits
  • 4 files changed
  • 0 commit comments
  • 3 contributors
Commits on Jan 13, 2012
Reiner Pope Add Language.Haskell.Meta.Parse.Careful for reporting unresolved infi…
…x as parse errors in old versions of GHC.
847897e
Commits on Feb 08, 2012
Reiner Pope Add documentation 2231524
Commits on Feb 09, 2012
Ben Millwood bmillwood Merge pull request #11 from reinerp/careful-parse
Careful parse
2a561c7
Commits on Feb 13, 2012
Ben Millwood bmillwood haskell-src-exts >= 1.8.0 for InlineConlikeSig e918767
Commits on Feb 14, 2012
Ben Millwood bmillwood Use MIN_VERSION instead of cpp-options c50eabe
Ben Millwood bmillwood 0.5.1 a2577fc
Commits on Mar 14, 2012
Nicolas Frisby Added the naive translation of view patterns --- is it safe? 1875100
Commits on Mar 15, 2012
Nicolas Frisby need to guard the ViewP case against the Template Haskell version 9fea7f1
Commits on Mar 16, 2012
Ben Millwood bmillwood Merge pull request #14 from nfrisby/master
View pattern support, by Nicolas Frisby
5d63fbd
Commits on Mar 17, 2012
Ben Millwood bmillwood Default new-th flag to False
I hope this will lead to better error messages when people don't have
uniplate.
db40208
Ben Millwood bmillwood 0.5.1.1 c7a8c5a
Commits on Mar 27, 2012
Ben Millwood bmillwood Update HSE dependency 442f9e5
Ben Millwood bmillwood Use impl instead of flags to determine TH version. 9a35141
Ben Millwood bmillwood Update tested-with d3295b9
7 README
View
@@ -12,6 +12,13 @@ maintainer, just ask.
Major changes:
+0.5.1 -> 0.5.1.1:
+- View pattern support, thanks to Nicolas Frisby.
+
+0.5.0.3 -> 0.5.1:
+- New module Language.Haskell.Meta.Parse.Careful, written by Reiner Pope
+ so that ambiguous parses can be rejected instead of quietly done wrong.
+
0.5.0.2 -> 0.5.0.3:
- Support for GHC 7.4, thanks to Reiner Pope
- Support for unresolved infix expressions, again thanks to Reiner Pope
15 haskell-src-meta.cabal
View
@@ -1,5 +1,5 @@
name: haskell-src-meta
-version: 0.5.0.3
+version: 0.5.1.1
cabal-version: >= 1.6
build-type: Simple
license: BSD3
@@ -10,7 +10,7 @@ copyright: (c) Matt Morrow
maintainer: Ben Millwood <haskell@benmachine.co.uk>
bug-reports: https://github.com/benmachine/haskell-src-meta/issues
stability: experimental
-tested-with: GHC == 6.12.3, GHC == 7.0.4, GHC == 7.2.1
+tested-with: GHC == 6.12.3, GHC == 7.0.4, GHC == 7.2.2, GHC == 7.4.1
synopsis: Parse source to template-haskell abstract syntax.
description: The translation from haskell-src-exts abstract syntax
to template-haskell abstract syntax isn't 100% complete yet.
@@ -19,11 +19,17 @@ extra-source-files: examples/*.hs README
library
build-depends: base >= 4.2 && < 4.6,
- haskell-src-exts >= 1.6 && < 1.12,
- template-haskell >= 2.4 && < 2.8,
+ haskell-src-exts >= 1.8 && < 1.13,
pretty >= 1.0 && < 1.2,
syb >= 0.1 && < 0.4,
th-lift == 0.5.*
+
+ if impl(ghc >= 7.4)
+ Build-depends: template-haskell >= 2.7 && < 2.9
+ else
+ Build-depends: template-haskell >= 2.4 && < 2.7,
+ uniplate >= 1.3 && < 1.7
+
extensions: CPP,
RankNTypes,
StandaloneDeriving,
@@ -36,6 +42,7 @@ library
hs-source-dirs: src
exposed-modules: Language.Haskell.Meta
Language.Haskell.Meta.Parse
+ Language.Haskell.Meta.Parse.Careful
Language.Haskell.Meta.Syntax.Translate
Language.Haskell.TH.Instances.Lift
Language.Haskell.Meta.Utils
73 src/Language/Haskell/Meta/Parse/Careful.hs
View
@@ -0,0 +1,73 @@
+{- |
+This module provides the tools to handle operator fixities in infix expressions correctly.
+
+The problem we solve is the following. Consider making a quasiquoter which antiquotes to Haskell - for instance, the quasiquoter in <http://hackage.haskell.org/package/hmatrix-static> allows me to write
+
+> myVec :: Vector Double
+> myVec = [vec| 2+3*4, 5-4-3 |]
+
+To correctly parse such expressions, we need to know the fixities and precedences of the operators, so that the above is parsed the same way as
+
+> myVec = [vec| 2+(3*4), (5-4)-3 |]
+
+There is a danger, if we are not careful in parsing, that the above expression instead parses as
+
+> myVec = [vec| (2+3)*4, 5-(4-3) |]
+
+which is a surprising bug, and would only be detected through testing at runtime, rather than at compile time.
+
+When this danger arises, we use this \"careful\" module. It handles \"unresolved infix\" expressions such as @2+3*4@ in two ways, depending on the version of GHC:
+
+ * in GHC 7.4 and above (where support for \"unresolved infix\" was added in Template Haskell), resolution of the infix expression is deferred to the compiler, which has all fixities available to it.
+
+ * prior to GHC 7.4, any ambiguous infix expression is flagged as a parse error at compile time, and the user is advised to resolve the ambiguity by adding parentheses.
+
+-}
+module Language.Haskell.Meta.Parse.Careful(
+ parsePat,
+ parseExp,
+ parseType,
+ parseDecs
+ ) where
+
+import qualified Language.Haskell.Meta.Parse as Sloppy
+import qualified Language.Haskell.Meta.Syntax.Translate as Translate
+import qualified Language.Haskell.TH as TH
+import qualified Language.Haskell.Exts.Syntax as Hs
+#if !(MIN_VERSION_template_haskell(2,7,0))
+import Data.Generics.Uniplate.Data
+#endif
+
+doChecked parser translater p =
+ case parser p of
+ Left s -> Left s
+ Right p' | amb p' -> Left "Infix expression could not be resolved as operator fixities are not known. Resolve ambiguity by adding parentheses"
+ | otherwise -> Right (translater p')
+
+parsePat :: String -> Either String TH.Pat
+parsePat = doChecked Sloppy.parseHsPat Translate.toPat
+
+parseExp :: String -> Either String TH.Exp
+parseExp = doChecked Sloppy.parseHsExp Translate.toExp
+
+parseType :: String -> Either String TH.Type
+parseType = doChecked Sloppy.parseHsType Translate.toType
+
+parseDecs :: String -> Either String [TH.Dec]
+parseDecs = doChecked Sloppy.parseHsDecls Translate.toDecs
+
+#if MIN_VERSION_template_haskell(2,7,0)
+amb = const False
+#else
+amb syn = any isAmbExp (universeBi syn) || any isAmbPat (universeBi syn)
+ where
+ isAmbExp (Hs.InfixApp Hs.InfixApp{} _ _) = True
+ isAmbExp (Hs.InfixApp _ _ Hs.InfixApp{}) = True
+ isAmbExp (Hs.InfixApp Hs.RightSection{} _ _) = True
+ isAmbExp (Hs.InfixApp _ _ Hs.LeftSection{}) = True
+ isAmbExp _ = False
+
+ isAmbPat (Hs.PInfixApp Hs.PInfixApp{} _ _) = True
+ isAmbPat (Hs.PInfixApp _ _ Hs.PInfixApp{}) = True
+ isAmbPat _ = False
+#endif
4 src/Language/Haskell/Meta/Syntax/Translate.hs
View
@@ -194,6 +194,9 @@ Right (HsPParen (HsPNeg (HsPLit (HsInt 2))))
toPat (Hs.PWildCard) = WildP
toPat (Hs.PIrrPat p) = TildeP (toPat p)
toPat (Hs.PatTypeSig _ p t) = SigP (toPat p) (toType t)
+#if MIN_VERSION_template_haskell(2,5,0)
+ toPat (Hs.PViewPat e p) = ViewP (toExp e) (toPat p)
+#endif
-- regular pattern
toPat p@Hs.PRPat{} = noTH "toPat" p
-- XML stuff
@@ -204,6 +207,7 @@ Right (HsPParen (HsPNeg (HsPLit (HsInt 2))))
#if MIN_VERSION_template_haskell(2,4,0)
toPat (Hs.PBangPat p) = BangP (toPat p)
#endif /* MIN_VERSION_template_haskell(2,4,0) */
+ toPat p = todo "toPat" p
-----------------------------------------------------------------------------

No commit comments for this range

Something went wrong with that request. Please try again.