-
Notifications
You must be signed in to change notification settings - Fork 5
/
Parse.hs
542 lines (455 loc) · 14.9 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
import Data.List
import Data.Maybe
import Control.Monad
import Parsec hiding (token,tokens)
-- A parameter can be denoted by a name, a number, or one of the special
-- characters listed in Special Parameters.
--
-- A variable is a parameter denoted by a name.
--
-- A positional parameter is a parameter denoted by the decimal value
-- represented by one or more digits, other than the single digit 0.
type Name = String
data Parameter = Var Name
| Positional Int
| Special Char
deriving Show
data SubstringType = Suffix | Prefix
deriving Show
data PatternType = Largest | Smallest
deriving Show
data CheckType = CheckUnset | CheckUnsetAndNull
deriving Show
data ParModifier = UseDefault Word CheckType
| AssignDefault Word CheckType
| Assert Word CheckType
| UseAlternative Word CheckType
| StringLength
| Remove Word PatternType SubstringType
| NoModifier
deriving Show
data ParSubstExpr = ParSubstExpr Parameter ParModifier
deriving Show
data WordPart = Bare String
| SQuoted String
| DQuoted [WordPart]
| Escaped Char
| CommandSubst CompoundList
| ParSubst ParSubstExpr
| Arith [WordPart]
deriving Show
type Word = [WordPart]
data Token = Word [WordPart]
| Op String
deriving Show
data Redirection = Redirection Int RedirectionOp Word
deriving Show
data StripHereDoc = Strip | NoStrip
deriving Show
data Clobber = Clobber | NoClobber
deriving Show
data RedirectionOp = RedirectInput
| RedirectOutput Clobber
| AppendOutput
| HereDoc StripHereDoc
| DupInput
| DupOutput
| ReadWrite
deriving Show
data Assignment = Assignment Name Word
deriving Show
data Command = ComSimple SimpleCommand
| ComCompound CompoundCommand
| ComFunction FunctionDefinition
deriving Show
data FunctionDefinition =
FunctionDefinition Name CompoundCommand [Redirection]
deriving Show
data SimpleCommand = SimpleCommand [Assignment] [Redirection] [Word]
deriving Show
data ForList = ForWords [Word] | ForPositional
deriving Show
data CompoundCommand = BraceGroup CompoundList
| SubShell CompoundList
| For Name ForList CompoundList
| Case Word [([Word],CompoundList)]
| If [(CompoundList,CompoundList)] -- 'if' and 'elif'
(Maybe CompoundList) -- optional 'else'
| While CompoundList CompoundList
| Until CompoundList CompoundList
deriving Show
data PipelineStatus = Straight | Inverted
deriving Show
data Pipeline = Pipeline PipelineStatus [Command]
deriving Show
data AndOrList = First Pipeline
| And Pipeline AndOrList
| Or Pipeline AndOrList
deriving Show
data ExecutionMode = Seq | Async
deriving Show
type CompoundList = [(AndOrList,ExecutionMode)]
singleQuoted :: Parser WordPart
singleQuoted = dontSkipLineConts $ do
squote
text <- many nonQuote
squote
return $ SQuoted text
where
squote = char '\''
nonQuote = satisfy (/= '\'')
bareWord :: String -> Parser WordPart
bareWord terminators = do
word <- many1 ordinarySymbol
return $ Bare word
where
ordinarySymbol = noneOf terminators
escaped :: Parser WordPart
escaped = do
char '\\'
c <- anyChar
return $ Escaped c
doubleQuoted :: Parser WordPart
doubleQuoted = do
dQuote
parts <- many1 $ escaped <|> bare_word <|> substitution
dQuote
return $ DQuoted parts
where
dQuote = char '"'
escapables = "$`\"\\\n"
escaped = try $ do
char '\\'
fmap Escaped $ oneOf escapables
bare_word = do
w <- many1 ordinary_symbol
return $ Bare w
where
ordinary_symbol = noneOf "$`\\\"" <|>
do char '\\'; lookAhead (noneOf escapables); return '\\'
word :: String -> Bool -> Parser Word
word terminators acceptEmpty = do
(if acceptEmpty then many else many1) $
escaped <|> singleQuoted <|> doubleQuoted <|> substitution <|> bareWord terminators
--- Operators ---
operator :: Parser String
operator = token $ do
choice $ map (try.string) operatorList
theOperator op = try $ do
op' <- operator
guard $ op' == op
return op
operatorList = ["(",")","&&","||",";;","<<",">>","<&",">&","<>","<<-",">|","<",">","&",";","|","\n"]
opFirstLetters = nub $ map head $ operatorList
--- Comments ---
comment :: Parser ()
comment = do
char '#'
many $ satisfy (/= '\n')
return ()
whiteSpace :: Parser ()
whiteSpace = do many1 $ comment <|> (oneOf " \t" >> return ());
return ()
--- Substitutions ---
-- Parameter expansion, command substitution or arithmetic expansion
substitution :: Parser WordPart
substitution = do
char '$'
fmap ParSubst parameterSubst <|> fmap CommandSubst commandSubst
-- A word consisting solely of underscores, digits, and alphabetics from the
-- portable character set. The first character of a name is not a digit.
name :: Parser Name
name = token $ do
first <- underscore <|> letter
rest <- many $ underscore <|> letter <|> digit
return $ first:rest
where
-- portable character set
letter = satisfy $ \x -> (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z')
underscore = char '_'
parameterSubst :: Parser ParSubstExpr
parameterSubst = do
braced <|> unbraced <?> "parameter substitution"
where
lbrace = char '{'
rbrace = char '}'
braced = between lbrace rbrace $
try string_length <|>
try parameter_check <|>
try parameter_substr <|>
try (flip ParSubstExpr NoModifier `fmap` parameter)
parameter = do special <|> positional <|> fmap Var name
word_arg = word "}'\"`$\\" True
parameter_check = do
par <- parameter
colon <- optionMaybe $ char ':'
op <- oneOf "-+=?"
w <- word_arg
let mod = case op of
'-' -> UseDefault
'=' -> AssignDefault
'+' -> UseAlternative
'?' -> Assert
let checkType = if isJust colon then CheckUnsetAndNull else CheckUnset
return $ ParSubstExpr par (mod w checkType)
parameter_substr = do
par <- parameter
op <- choice $ map (try.string) ["%%","%","##","#"]
w <- word_arg
let mod = case op of
"%" -> Remove w Smallest Suffix
"%%" -> Remove w Largest Suffix
"#" -> Remove w Smallest Prefix
"##" -> Remove w Largest Prefix
return $ ParSubstExpr par mod
string_length = do
char '#'
par <- parameter
return $ ParSubstExpr par StringLength
unbraced = do
par <- special -- should be the first to capture 0
<|> simple_positional
<|> fmap Var name
return $ ParSubstExpr par NoModifier
variable = fmap Var name <?> "variable"
simple_positional = do
d <- digit
return $ Positional $ read [d]
positional = fmap Positional number
special = fmap Special $ oneOf "@*#?-$!0"
commandSubst :: Parser CompoundList
commandSubst = between (char '(') (char ')') compoundList
--- Tokens ---
token :: Parser a -> Parser a
token p = do optional whiteSpace; x <- p; optional whiteSpace; return x
separated p = do
optional whiteSpace
sepEndBy p (optional whiteSpace)
separated1 p = do
optional whiteSpace
sepEndBy1 p (optional whiteSpace)
{-
tokens :: Parser [Token]
tokens = do
optional whiteSpace
sepEndBy (fmap Word token_word <|> operator) (optional whiteSpace)
-}
token_word = token $ word ("'\"`$\\\n# " ++ opFirstLetters) False
--- Syntax ---
redirOp :: Parser RedirectionOp
redirOp = do
op <- choice $ map (try.string) ["<<-",">>","<&",">&","<>","<<",">|","<",">"]
return $ case op of
"<" -> RedirectInput
">" -> RedirectOutput NoClobber
">|" -> RedirectOutput Clobber
"<<" -> HereDoc NoStrip
"<<-"-> HereDoc Strip
">>" -> AppendOutput
"<&" -> DupInput
">&" -> DupOutput
"<>" -> ReadWrite
redirection :: Parser Redirection
redirection = do
mbFd <- optionMaybe number
op <- redirOp
w <- token_word
let fd = case mbFd of
Just fd -> fd
Nothing -> case op of
RedirectOutput _ -> 1
AppendOutput -> 1
DupOutput -> 1
RedirectInput -> 0
HereDoc _ -> 0
DupInput -> 0
ReadWrite -> 0
return $ Redirection fd op w
assignment = do
var <- name
char '='
value <- token_word
return $ Assignment var value
ifNotReserved :: Parser a -> Parser a
ifNotReserved p = try $ do
r <- optionMaybe reservedWord
case r of
Just _ -> parserFail "unexpected reserved word"
Nothing -> p
simpleCommand = ifNotReserved $ do
cmd_prefix <- separated (fmap add_assignment (try assignment) <|> fmap add_redirection redirection)
cmd_word <- fmap maybeToList $ optionMaybe $ fmap add_word token_word
cmd_suffix <- separated (try (fmap add_redirection redirection) <|> fmap add_word token_word)
let (as,rs,ws) = foldr ($) ([],[],[]) (cmd_prefix ++ cmd_word ++ cmd_suffix)
case (as,rs,ws) of
([],[],[]) -> parserFail "Empty command"
_ -> return $ SimpleCommand as rs ws
where
add_assignment a (as,rs,ws) = (a:as,rs,ws)
add_redirection r (as,rs,ws) = (as,r:rs,ws)
add_word w (as,rs,ws) = (as,rs,w:ws)
reservedWords = ["!", "{", "}", "case", "do", "done", "elif", "else", "esac",
"fi", "for", "if", "in", "then", "until", "while"]
reservedWord = try $ do
[Bare x] <- token_word
guard $ x `elem` reservedWords
return $ x
theReservedWord w = (<?> "reserved word \"" ++ w ++ "\"") $ try $ do
w' <- reservedWord
guard $ w == w'
return w
linebreak = separated $ char '\n'
newline_list = separated1 $ char '\n'
sequential_sep = (theOperator ";" >> linebreak) <|> newline_list
pipeline = do
bang <- do optionMaybe (theReservedWord "!")
ps <- pipe_sequence
let status = if isJust bang then Inverted else Straight
return $ Pipeline status ps
where
pipe_sequence = do
sepBy1 command (do theOperator "|"; linebreak)
functionDefinition :: Parser FunctionDefinition
functionDefinition = ifNotReserved $ do
fname <- name
string "()"
linebreak
body <- compoundCommand
redirect <- many redirection
return $ FunctionDefinition fname body redirect
command = try $ fmap ComFunction functionDefinition
<|> fmap ComCompound compoundCommand
<|> fmap ComSimple simpleCommand
andOrList = do
p <- pipeline
op <- optionMaybe $ theOperator "&&" <|> theOperator "||"
case op of
Nothing -> return $ First p
Just op -> do
linebreak
rest <- andOrList
let opCon = case op of
"&&" -> And
"||" -> Or
return $ opCon p rest
list :: Parser CompoundList
list = do
linebreak
aol <- andOrList
mode <- optionMaybe $
(do theOperator ";"; linebreak; return Seq) <|>
(do theOperator "&"; linebreak; return Async) <|>
(do newline_list; return Seq)
-- if there is separator, try to parse list further
-- if there is no, finish
case mode of
Just mode -> do
rest <- optionMaybe list
return $ (aol,mode) : (concat.maybeToList) rest
Nothing -> return [(aol,Seq)]
compoundList = list
compoundCommand = choice $
[ braceGroup
, subShell
, forClause
, ifClause
, whileClause
, untilClause
, caseClause
]
braceGroup = fmap BraceGroup $ between (theReservedWord "{") (theReservedWord "}") compoundList
subShell = fmap SubShell $ between (theOperator "(") (theOperator ")") compoundList
doGroup = between (theReservedWord "do") (theReservedWord "done") compoundList
forClause = do
theReservedWord "for"
var <- name
linebreak
words <- optionMaybe wordlist
do_group <- doGroup
let list = case words of
Just ws -> ForWords ws
Nothing -> ForPositional
return $ For var list do_group
where
wordlist = do
theReservedWord "in"
ws <- many token_word
sequential_sep
return ws
whileClause = do
theReservedWord "while"
l <- compoundList
cmds <- doGroup
return $ While l cmds
untilClause = do
theReservedWord "until"
l <- compoundList
cmds <- doGroup
return $ Until l cmds
ifClause = do
theReservedWord "if"
cond <- compoundList
theReservedWord "then"
then_part <- compoundList
elif_parts <- many elif_part
mb_else_part <- optionMaybe else_part
theReservedWord "fi"
return $ If ((cond,then_part):elif_parts) mb_else_part
where
elif_part = do
theReservedWord "elif"
cond <- compoundList
theReservedWord "then"
then_part <- compoundList
return (cond, then_part)
else_part = do
theReservedWord "else"
compoundList
caseClause = do
theReservedWord "case"
w <- token_word
linebreak
theReservedWord "in"
linebreak
cl <- case_list
return $ Case w cl
where
-- case_item:
-- returns Left () if it parsed "esac",
-- returns Right (item, True) if it has parsed item with DSEMI
-- returns Right (item, False) if it has parsed item without DSEMI
pattern :: Parser [Word]
pattern = sepBy1 token_word (char '|')
case_item :: Parser (Either () (([Word],CompoundList), Bool))
case_item = do
openParen <- optionMaybe (theOperator "(")
let esac = case openParen of
-- if there was opening paren, don't recognise esac as reserved word
Nothing -> do theReservedWord "esac"; return $ Left ()
Just _ -> parserFail ""
item = do
pat <- pattern
theOperator ")"
linebreak
cl <- optionMaybe compoundList
dsemi <- optionMaybe (theOperator ";;")
linebreak
return $ Right ((pat,concat $ maybeToList cl),isJust dsemi)
esac <|> item
case_list = do
ci <- case_item
case ci of
Left _ -> return []
Right (pat,True) -> fmap (pat:) case_list
Right (pat,False) -> do theReservedWord "esac"; return [pat]
main = do
s <- getContents
--print $ parse tokens "" s
--print $ parse simpleCommand "" s
--print $ parse andOrList "" s
--print $ parse functionDefinition "" s
print $ parse (do l <- list; eof; return l) "" s
--- Misc ---
number = do
n <- many1 digit
return $ read n