Skip to content

Commit

Permalink
Parse for loops
Browse files Browse the repository at this point in the history
  • Loading branch information
UnkindPartition committed Mar 13, 2010
1 parent e18fc93 commit 6c1c2b2
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions Parse.hs
Expand Up @@ -73,9 +73,11 @@ data Command = ComSimple SimpleCommand
deriving Show
data SimpleCommand = SimpleCommand [Assignment] [Redirection] [Word]
deriving Show
data ForList = ForWords [Word] | ForPositional
deriving Show
data CompoundCommand = BraceGroup CompoundList
| SubShell CompoundList
| For String [Word] CompoundList
| For String ForList CompoundList
-- | Case TODO
| If [(CompoundList,CompoundList)] -- 'if' and 'elif'
(Maybe CompoundList) -- optional 'else'
Expand Down Expand Up @@ -350,6 +352,7 @@ theReservedWord w = try $ do

linebreak = separated $ char '\n'
newline_list = separated1 $ char '\n'
sequential_sep = (theOperator ";" >> linebreak) <|> newline_list

pipeline = do
bang <- do optionMaybe (theReservedWord "!")
Expand Down Expand Up @@ -390,12 +393,31 @@ compoundList = do
linebreak
return aols

compoundCommand = braceGroup <|> subShell
compoundCommand = braceGroup <|> subShell <|> forClause

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

main = do
s <- getContents
--print $ parse tokens "" s
Expand Down

0 comments on commit 6c1c2b2

Please sign in to comment.