syntax-example (empty) → 0.1.0.0
raw patch · 4 files changed
+111/−0 lines, 4 filesdep +attoparsecdep +basedep +lenssetup-changed
Dependencies added: attoparsec, base, lens, pretty, semi-iso, syntax, syntax-attoparsec, syntax-pretty, text
Files
- LICENSE +20/−0
- Main.hs +67/−0
- Setup.hs +2/−0
- syntax-example.cabal +22/−0
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2014 Paweł Nowak++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Main.hs view
@@ -0,0 +1,67 @@+{-# LANGUAGE TemplateHaskell #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE OverloadedStrings #-}++import Control.Applicative+import Control.Lens.TH+import qualified Data.Attoparsec.Text as AP+import Data.Char+import Data.SemiIsoFunctor+import Data.Syntax (Syntax)+import qualified Data.Syntax as S+import qualified Data.Syntax.Attoparsec.Text as S+import qualified Data.Syntax.Char as S+import qualified Data.Syntax.Pretty as S+import Data.Text (Text)+import qualified Data.Text.IO as T+import qualified Text.PrettyPrint as P++-- | A simple untyped lambda calculus.+data AST = Var Text+ | App AST AST+ | Abs Text AST+ deriving (Show)++$(makePrisms ''AST)++-- | A variable name.+name :: Syntax syn Text => syn Text+name = S.takeWhile1 isAlphaNum++-- | Encloses a symbol in parentheses.+parens :: Syntax syn Text => syn a -> syn a+parens m = S.char '(' */ S.spaces_ */ m /* S.spaces_ /* S.char ')'++-- | An atom is a variable or an expression in parentheses.+atom :: Syntax syn Text => syn AST+atom = _Var /$/ name+ /|/ parens expr++-- | An expression of our lambda calculus.+expr :: Syntax syn Text => syn AST+expr = _App /$/ atom /* S.spaces1 /*/ atom+ /|/ _Abs /$/ S.char '\\' /* S.spaces_+ */ name /* S.spaces+ /* S.string "->" /* S.spaces+ /*/ expr + /|/ atom++main :: IO ()+main = do+ -- Load the standard input.+ t <- T.getContents++ -- Try to parse it.+ case AP.parseOnly (S.getParser expr <* AP.skipSpace <* AP.endOfInput) t of+ Left err -> putStrLn err+ Right ast -> do+ -- If parsing succeeded print the AST.+ print ast+ + -- Try to pretty print it.+ -- (Printing cannot really fail in this example)+ case S.runPrinter expr ast of+ Left err -> putStrLn err+ Right doc -> putStrLn (P.render doc)++ return ()
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ syntax-example.cabal view
@@ -0,0 +1,22 @@+name: syntax-example+version: 0.1.0.0+synopsis: Example application using syntax, a library for abstract syntax descriptions.+license: MIT+license-file: LICENSE+author: Paweł Nowak+maintainer: Paweł Nowak <pawel834@gmail.com>+copyright: Paweł Nowak 2014+category: Data+build-type: Simple+cabal-version: >=1.10++source-repository head+ type: git+ location: git@github.com:Pawel834/syntax-example.git++executable syntax-example+ main-is: Main.hs+ build-depends: base >= 4 && < 5, lens, semi-iso,+ syntax, syntax-attoparsec, syntax-pretty,+ attoparsec, pretty, text+ default-language: Haskell2010