"( 100 ( 200 300 ) \"hello world\" x )" it :: String > run pSym "hello" Sym "hello" it :: () > run pSym "hello world" Sym "hello" it :: () > run pSym "3434hello world" Sym "3434hello" it :: () > run pQuotedStr "\"hello world haskell\"" Str "hello world haskell" it :: () > run (P.spaces > pQuotedStr) " \"hello world haskell\"" Str "hello world haskell" it :: () > run (P. - Im forced to use Parsec here because of Haste. "( 100 200 300 )" it :: String > sexpToStr $ List, Str "hello world", Sym "x"] Lets have a go in the interactive ghci console: Here, running Parsec.char 'H' returns a rule that will match a single character so long as it's 'H'. SexpToDouble :: Sexp -> Maybe Double sexpToInt :: Sexp -> Maybe Int > sexpToStr $ List This function returns a rule that matches the current character in the text that we are parsing to whatever character you provide it. Run :: Show a => Parser a -> String -> IO () sexpToStr :: Sexp -> String pStr :: Parser Sexp pSym :: Parser Sexp pList :: Parser Sexp pSexp :: Parser Sexp sexpOfStr :: String -> Maybe Sexp sexpOfList :: (a -> Sexp) -> -> Sexp sexpOfValue :: Show a => a -> Sexp sexpToList :: ( Sexp -> Maybe a) -> Sexp -> Maybe when p m if p then m else return () main do args <- getArgs when ( null args) ( putStrLn 'No args specified') and then use it with the same effect as the original expression. PARSEC HASKELL CODEImport Test.HUnit :set - XFlexibleContexts :ĭata Sexp = Sym String | Str String | List Īpply :: Parser a -> String -> Either P.ParseError aĪpplyOpt :: Parser a -> String -> Maybe a The purity of Haskell code makes it easy to fuse chains of functions together, allowing for performance benefits. This is equivalent to: parser pure ( ) <> many1 letter. It has been reimplemented in many other languages, including Erlang, Elixir, OCaml, and F, as well as imperative languages such as C, and Java. Try this: parser ( ) <> many1 letter <> option (fmap pure digit).It is based on higher-order parser combinators, so a complicated parser can be made out of many smaller ones. While it follows a similar combinator approach, the. JParsec is an existing Java port of Parsec. The current incarnation of the Haskell Parsec library has evolved considerably since the paper, however it still essentially follows the same monadic combinator approach. PARSEC HASKELL PCImport qualified Text.Parsec as P import qualified as PC import Control.Monad (void) Parsec is a library for writing parsers in Haskell. As mentioned at the outset, ParsecJ is based on the Parsec paper.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |