-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parse.hs
190 lines (158 loc) · 4.46 KB
/
Parse.hs
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
{-# OPTIONS_GHC -Wno-unused-do-bind #-}
module Parse where
import Text.ParserCombinators.ReadP hiding (many)
import Types
import Control.Applicative
import Control.Monad
import Data.List
import Data.Bits
import Debug.Trace
parseLine :: String -> Either Line String
parseLine w = let
(s,_) = break (== '#') w
parser = readP_to_S (lineParser)
parses = map head . group . sort $ parser s
finished = filter ((=="") . snd) parses
in if length finished == 1
then Left . fst . head $ finished
else Right (if s == "" then "" else "syntax error " ++ show (length finished) ++ " parses")
debugParse :: String -> IO ()
debugParse s = do
let ps = readP_to_S lineParser s
mapM_ (\(a,b) -> print a >> print b) ps
lineParser :: ReadP Line
lineParser = ignoreTrailingWhiteSpace $ parseExtract <++ parseLet <++ parsePlain
parseExtract :: ReadP Line
parseExtract = do
var <- parseWord
eatSpace
string "<-"
eatSpace
cmd <- parseCmd
return $ Extract var cmd
parseLet :: ReadP Line
parseLet = do
string "let"
left <- parseArgsLet
eatSpace
string "="
right <- parseArgsLet
maybeEatSpace
return $ Let left right
parsePlain :: ReadP Line
parsePlain = fmap Plain parseCmd
{-
- CMD grammer
-
- Cmd <- nonInfix `infix` CMD | nonInfix
- nonInfix <- if cmd then cmd else cmd | simple
- simple <- exec | exec &
- exec <- path [args] | ( CMD )
-
-}
infixes :: [String]
infixes = ["||","&&",">>",">>=","1>>=","2>>=","1&2>>="]
parseCmd :: ReadP Command
parseCmd = fmap liftInfix (do
ni <- parseNonInfix
eatSpace
infixWord <- foldl (<|>) pfail (map string infixes)
eatSpace
cmd <- parseCmd
return $ Infix ni infixWord cmd ) <++ parseNonInfix
liftInfix :: Command -> Command
liftInfix (Infix l word r) = case word of
"&&" -> And l r
"||" -> Or l r
">>" -> Seq l r
">>=" -> Pipe True False l r
"1>>=" -> Pipe True False l r
"2>>=" -> Pipe False True l r
"1&2>>=" -> Pipe True True l r
_ -> error "unsupoorted infix string givent to liftInfix in Parse.hs"
liftInfix _ = error "liftInfix called on non infix in Parse.hs"
parseNonInfix :: ReadP Command
parseNonInfix = (do
string "if "
i <- parseCmd
string " then "
t <- parseCmd
string " else "
e <- parseCmd
return $ ITE i t e ) <++ parseSimple
parseSimple :: ReadP Command
parseSimple = (do
cmd <- parseExec
eatSpace
char '&'
rest <- look
when ( (not $ null rest) && (head rest == '&') ) pfail
return $ Background cmd) <++ parseExec
parseExec :: ReadP Command
parseExec = parseParen <++ do
name <- parseHead
args <- parseArgs
return $ Exec name args
parseParen :: ReadP Command
parseParen = do
char '('
str <- look
when (singleWordInParen str) pfail
when (str == "" || head str == '\\') pfail
_ <- fmap (trace "this ran") look
maybeEatSpace
cmd <- parseCmd
maybeEatSpace
char ')'
return cmd
singleWordInParen :: String -> Bool
singleWordInParen w = let tilParen = fst . break (== ')') $ w in not $ ' ' `elem` tilParen
parseArgs :: ReadP [String]
parseArgs = many parseArg
parseArg :: ReadP String
parseArg = (eatSpace >> parseWord)
parseArgsLet :: ReadP [String]
parseArgsLet = many (eatSpace >> munch1 (/= ' '))
parseWord :: ReadP String
parseWord = tic <++ quote1 <++ quote2 <++ ((do
word <- munch (/= ' ')
when (unacceptable word) pfail
return word ) <|> (do
word <- munch (\x -> not $ x `elem` " )")
when (unacceptable word) pfail
return word))
wraped :: String -> Char -> ReadP String
wraped cl cr = do
string cl
fmap (cl ++) (continue cr)
wrapedSimple :: Char -> ReadP String
wrapedSimple c = wraped [c] c
parseHead :: ReadP String
parseHead = wraped "(\\" ')' <++ parseWord
continue :: Char -> ReadP String
continue c = do
n <- get
if n == c then return [c] else do
if n == '\\' then do
n2 <- get
rest <- continue c
return ([n,n2] ++ rest)
else do
fmap (n:) (continue c)
tic , quote1 , quote2 :: ReadP String
tic = wrapedSimple '`'
quote1 = wrapedSimple '\''
quote2 = wrapedSimple '"'
unacceptable :: String -> Bool
unacceptable s = (s `elem` unacceptableWords) || ((head s == '(') `xor` (last s == ')'))
unacceptableWords :: [String]
unacceptableWords = "" : infixes
eatSpace :: ReadP ()
eatSpace = many1 (char ' ') >> return ()
ignoreTrailingWhiteSpace :: ReadP a -> ReadP a
ignoreTrailingWhiteSpace p = do
x <- p
maybeEatSpace
return x
maybeEatSpace :: ReadP ()
maybeEatSpace = many (char ' ') >> return ()