This repository was archived by the owner on Nov 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleparser2.hs
More file actions
65 lines (58 loc) · 2.5 KB
/
Copy pathsimpleparser2.hs
File metadata and controls
65 lines (58 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module Main where
import System.Environment
import Text.ParserCombinators.Parsec hiding (spaces)
-- main function needed to call readExpr and print out results
main :: IO()
main = do args <- getArgs
putStrLn (readExpr (args !! 0))
-- define a parser that recognizes one of the symbols allowed
symbol :: Parser Char --let ghc find this out
symbol = oneOf "!$%&|*+-/:<=?>#^_~#"
-- Defining a function to call our parser and handle errors:
readExpr :: String -> String
readExpr input = case parse (spaces >> symbol) "lisp" input of
Left err -> "No match: " ++ show err
Right val -> "Found value"
-- this
-- input = parameter name
-- symbol = action defined above
-- "lisp" = name of the parser
-- are passed to the plf parse.
-- parse can return either the parsed value or an error:
-- Defining a parser that recognizes any number of whitespace chars
spaces :: Parser ()
-- passing the Parser action space to the Parser action skipMany1
spaces = skipMany1 space
-- we need to define a data type to hold any lisp value
data LispVal = Atom String
| List [LispVal]]
| DottedList [LispVal] LispVal
| Number Integer
| String String
| Bool Bool
-- algebraic data type: it defines a set of possible
-- values that a variable of type List Val can hold.
parseString :: Parser LispVal
parseString = do
char ""
x <- many (noneOf "\"")
char ""
return $ String x
-- We're back to using the do-notation instead of the >> operator.
-- This is because we'll be retrieving the value of our parse
-- (returned by many(noneOf "\"")) and manipulating it,
-- interleaving some other parse operations in the meantime.
--
-- In general, use
-- >> if the actions don't return a value,
-- >>= if you'll be immediately passing
-- that value into the next action,
-- and do-notation otherwise.
-- Every constructor in an algebraic data type also acts like a function
-- that turns its arguments into a value of its type.
-- It also serves as a pattern that can be used in the left-hand side of a pattern-matching expression;
-- Every constructor in an algebraic data type also acts like a function
-- that turns its arguments into a value of its type.
-- It also serves as a pattern that can be used in the left-hand
-- side of a pattern-matching expression;
-- we then apply the built-in function return to lift our LispVal into the Parser monad