<commit_before>--  Copyright (C) 2004 <NAME>
--
--  This program is free software; you can redistribute it and/or modify
--  it under the terms of the GNU General Public License as published by
--  the Free Software Foundation; either version 2, or (at your option)
--  any later version.
--
--  This program is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with this program; see the file COPYING.  If not, write to
--  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
--  Boston, MA 02110-1301, USA.

module System.Console.ShSh.Parse ( Command(..), parseLine, builtinHelp )
    where

import System.Console.ShSh.Builtins ( BuiltinCommand(..) )

import Text.ParserCombinators.Parsec
import Text.ParserCombinators.Parsec.Expr

data Command = Cmd [String]
             | Builtin BuiltinCommand [String]
             | Command :&&: Command
             | Command :||: Command
             | Command :>>: Command
             | Command :|: Command
             | EmptyCommand
               deriving ( Show )

parseLine :: String -> Either String Command
parseLine s = case parse command_parser "command" s of
              Left err -> Left $ show err
              Right x -> Right x

command_parser :: CharParser st Command
command_parser = do m <- subcommand
                    eof
                    return m

subcommand :: CharParser st Command
subcommand = buildExpressionParser table cmd <?> "command thingy"

table :: OperatorTable Char st Command
table   = [ [binary "&&" (:&&:),
             binary "||" (:||:),
             binary "|" (:|:),
             binary ";" (:>>:) ]
          ]
    where binary name fun =
              Infix (do trystring name
                        spaces
                        return fun) AssocLeft

trystring :: String -> CharParser st String
trystring s = try $ string s

cmd :: CharParser st Command
cmd = between spaces spaces
      (parens subcommand
       <|> choice parseCmds_
       <|> externalCmd
       <?> "simple cmd")
    where parseCmds_ = map createHelper builtinCommands

createHelper :: (String, String, [String] -> Command)
             -> CharParser st Command
createHelper (key,_,thiscmd) =
  do trystring key
     spaces
     q <- many quoted
     return $ thiscmd q

builtinHelp :: String
builtinHelp = unlines $ map blurb builtinCommands
  where blurb :: (String, String, [String] -> Command) -> String
        blurb (c, help, _) = "   "++c ++ ": " ++ help

builtinCommands :: [(String, String, [String] -> Command)]
builtinCommands = [ ("ls", "list directory contents", Builtin Ls ),
                    ("cd", "change directory", Builtin Cd ),
                    ("pwd", "print working directory", Builtin Pwd ),
                    ("exit", "quit", Builtin Exit ),
                    ("set", "set primitives", Builtin Set ) ]

externalCmd :: CharParser st Command
externalCmd = Cmd `fmap` many1 quoted

parens :: CharParser st Command -> CharParser st Command
parens p  = between (string "(") (string ")") p

quoted :: CharParser st String
quoted = between (char '"') (char '"')
                 (many $ do { char '\\' -- allow escapes
                            ; try (oneOf ['\\', '"']) <|> return '\\'
                            }
                         <|>  noneOf ['"'])
         <|> between spaces spaces (many1 $ noneOf " ;|&()\"")
         <?> "string"
<commit_msg>Extend parser AST to include redirection<commit_after>--  Copyright (C) 2004 <NAME>
--
--  This program is free software; you can redistribute it and/or modify
--  it under the terms of the GNU General Public License as published by
--  the Free Software Foundation; either version 2, or (at your option)
--  any later version.
--
--  This program is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with this program; see the file COPYING.  If not, write to
--  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
--  Boston, MA 02110-1301, USA.

module System.Console.ShSh.Parse ( Command(..), parseLine, builtinHelp )
    where

import System.Console.ShSh.Builtins ( BuiltinCommand(..) )

import Text.ParserCombinators.Parsec
import Text.ParserCombinators.Parsec.Expr

data Command = Cmd [String]
             | Builtin BuiltinCommand [String]
             | Command :&&: Command
             | Command :||: Command
             | Command :>>: Command
             | Command :|: Command
             | Command :> String
             | Command :>> String
             | Command :>| String
             | Command :< String
             | EmptyCommand
               deriving ( Show )

parseLine :: String -> Either String Command
parseLine s = case parse command_parser "command" s of
              Left err -> Left $ show err
              Right x -> Right x

command_parser :: CharParser st Command
command_parser = do m <- subcommand
                    eof
                    return m

subcommand :: CharParser st Command
subcommand = buildExpressionParser table cmd <?> "command thingy"

table :: OperatorTable Char st Command
table   = [ [binary "&&" (:&&:),
             binary "||" (:||:),
             binary "|" (:|:),
             binary ";" (:>>:) ]
          ]
    where binary name fun =
              Infix (do trystring name
                        spaces
                        return fun) AssocLeft

trystring :: String -> CharParser st String
trystring s = try $ string s

cmd :: CharParser st Command
cmd = between spaces spaces
      (parens subcommand
       <|> choice parseCmds_
       <|> externalCmd
       <?> "simple cmd")
    where parseCmds_ = map createHelper builtinCommands

createHelper :: (String, String, [String] -> Command)
             -> CharParser st Command
createHelper (key,_,thiscmd) =
  do trystring key
     spaces
     q <- many quoted
     return $ thiscmd q

builtinHelp :: String
builtinHelp = unlines $ map blurb builtinCommands
  where blurb :: (String, String, [String] -> Command) -> String
        blurb (c, help, _) = "   "++c ++ ": " ++ help

builtinCommands :: [(String, String, [String] -> Command)]
builtinCommands = [ ("ls", "list directory contents", Builtin Ls ),
                    ("cd", "change directory", Builtin Cd ),
                    ("pwd", "print working directory", Builtin Pwd ),
                    ("exit", "quit", Builtin Exit ),
                    ("set", "set primitives", Builtin Set ) ]

externalCmd :: CharParser st Command
externalCmd = Cmd `fmap` many1 quoted

parens :: CharParser st Command -> CharParser st Command
parens p  = between (string "(") (string ")") p

quoted :: CharParser st String
quoted = between (char '"') (char '"')
                 (many $ do { char '\\' -- allow escapes
                            ; try (oneOf ['\\', '"']) <|> return '\\'
                            }
                         <|>  noneOf ['"'])
         <|> between spaces spaces (many1 $ noneOf " ;|&()\"")
         <?> "string"

{'fraction_non_alphanumeric': 0.11381676308354635, 'fraction_numerical': 0.004843266514193462, 'mean_word_length': 2.1420118343195265, 'pattern_counts': {'":': 2, '<': 26, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6584605', 'n_tokens_mistral': 2028, 'n_tokens_neox': 1954, 'n_words': 735}