{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances#-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TupleSections #-}

{-# OPTIONS_GHC -fno-warn-orphans #-}

module BNFC.Options
  ( Mode(..), Target(..), Backend
  , parseMode, usage, help, versionString
  , SharedOptions(..)
  , defaultOptions, isDefault, printOptions
  , AlexVersion(..), HappyMode(..), OCamlParser(..), JavaLexerParser(..)
  , RecordPositions(..), TokenText(..)
  , Ansi(..)
  , InPackage
  , removedIn290
  , translateOldOptions
  )
  where

import qualified Control.Monad as Ctrl
import Control.Monad.Writer (WriterT, runWriterT, tell)
import Control.Monad.Except (MonadError(..))

import Data.Bifunctor
import Data.Either     (partitionEithers)
import qualified Data.Map  as Map
-- import qualified Data.List as List
import Data.Maybe      (fromMaybe, maybeToList)
#if !MIN_VERSION_base(4,11,0)
import Data.Semigroup  (Semigroup(..))  -- for ghc 7.10 - 8.2
#endif
import Data.Version    (showVersion )

import System.Console.GetOpt
import System.FilePath (takeBaseName)

import Text.Printf     (printf)

import Paths_BNFC      (version)
import BNFC.CF         (CF)
import BNFC.Utils      (unless)

-- ~~~ Option data structures ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- | To decouple the option parsing from the execution of the program,
-- we introduce a data structure that holds the result of the
-- parsing of the arguments.
data Mode
    -- An error has been made by the user
    -- e.g. invalid argument/combination of arguments
    = UsageError String
    -- Basic modes: print some info and exits
    | Help | License | Version
    -- Normal mode, specifying the back end to use,
    -- the option record to be passed to the backend
    -- and the path of the input grammar file
    | Target SharedOptions FilePath
  deriving (Mode -> Mode -> Bool
(Mode -> Mode -> Bool) -> (Mode -> Mode -> Bool) -> Eq Mode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Mode -> Mode -> Bool
== :: Mode -> Mode -> Bool
$c/= :: Mode -> Mode -> Bool
/= :: Mode -> Mode -> Bool
Eq,Int -> Mode -> ShowS
[Mode] -> ShowS
Mode -> String
(Int -> Mode -> ShowS)
-> (Mode -> String) -> ([Mode] -> ShowS) -> Show Mode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Mode -> ShowS
showsPrec :: Int -> Mode -> ShowS
$cshow :: Mode -> String
show :: Mode -> String
$cshowList :: [Mode] -> ShowS
showList :: [Mode] -> ShowS
Show,Eq Mode
Eq Mode =>
(Mode -> Mode -> Ordering)
-> (Mode -> Mode -> Bool)
-> (Mode -> Mode -> Bool)
-> (Mode -> Mode -> Bool)
-> (Mode -> Mode -> Bool)
-> (Mode -> Mode -> Mode)
-> (Mode -> Mode -> Mode)
-> Ord Mode
Mode -> Mode -> Bool
Mode -> Mode -> Ordering
Mode -> Mode -> Mode
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Mode -> Mode -> Ordering
compare :: Mode -> Mode -> Ordering
$c< :: Mode -> Mode -> Bool
< :: Mode -> Mode -> Bool
$c<= :: Mode -> Mode -> Bool
<= :: Mode -> Mode -> Bool
$c> :: Mode -> Mode -> Bool
> :: Mode -> Mode -> Bool
$c>= :: Mode -> Mode -> Bool
>= :: Mode -> Mode -> Bool
$cmax :: Mode -> Mode -> Mode
max :: Mode -> Mode -> Mode
$cmin :: Mode -> Mode -> Mode
min :: Mode -> Mode -> Mode
Ord)

-- | Target languages
data Target = TargetC | TargetCpp | TargetCppNoStl
            | TargetHaskell | TargetHaskellGadt | TargetLatex
            | TargetJava | TargetOCaml | TargetPygments
            | TargetCheck
  deriving (Target -> Target -> Bool
(Target -> Target -> Bool)
-> (Target -> Target -> Bool) -> Eq Target
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Target -> Target -> Bool
== :: Target -> Target -> Bool
$c/= :: Target -> Target -> Bool
/= :: Target -> Target -> Bool
Eq, Target
Target -> Target -> Bounded Target
forall a. a -> a -> Bounded a
$cminBound :: Target
minBound :: Target
$cmaxBound :: Target
maxBound :: Target
Bounded, Int -> Target
Target -> Int
Target -> [Target]
Target -> Target
Target -> Target -> [Target]
Target -> Target -> Target -> [Target]
(Target -> Target)
-> (Target -> Target)
-> (Int -> Target)
-> (Target -> Int)
-> (Target -> [Target])
-> (Target -> Target -> [Target])
-> (Target -> Target -> [Target])
-> (Target -> Target -> Target -> [Target])
-> Enum Target
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: Target -> Target
succ :: Target -> Target
$cpred :: Target -> Target
pred :: Target -> Target
$ctoEnum :: Int -> Target
toEnum :: Int -> Target
$cfromEnum :: Target -> Int
fromEnum :: Target -> Int
$cenumFrom :: Target -> [Target]
enumFrom :: Target -> [Target]
$cenumFromThen :: Target -> Target -> [Target]
enumFromThen :: Target -> Target -> [Target]
$cenumFromTo :: Target -> Target -> [Target]
enumFromTo :: Target -> Target -> [Target]
$cenumFromThenTo :: Target -> Target -> Target -> [Target]
enumFromThenTo :: Target -> Target -> Target -> [Target]
Enum, Eq Target
Eq Target =>
(Target -> Target -> Ordering)
-> (Target -> Target -> Bool)
-> (Target -> Target -> Bool)
-> (Target -> Target -> Bool)
-> (Target -> Target -> Bool)
-> (Target -> Target -> Target)
-> (Target -> Target -> Target)
-> Ord Target
Target -> Target -> Bool
Target -> Target -> Ordering
Target -> Target -> Target
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Target -> Target -> Ordering
compare :: Target -> Target -> Ordering
$c< :: Target -> Target -> Bool
< :: Target -> Target -> Bool
$c<= :: Target -> Target -> Bool
<= :: Target -> Target -> Bool
$c> :: Target -> Target -> Bool
> :: Target -> Target -> Bool
$c>= :: Target -> Target -> Bool
>= :: Target -> Target -> Bool
$cmax :: Target -> Target -> Target
max :: Target -> Target -> Target
$cmin :: Target -> Target -> Target
min :: Target -> Target -> Target
Ord)

-- | List of Haskell target.
haskellTargets :: [Target]
haskellTargets :: [Target]
haskellTargets = [ Target
TargetHaskell, Target
TargetHaskellGadt ]

instance Show Target where
  show :: Target -> String
show Target
TargetC            = String
"C"
  show Target
TargetCpp          = String
"C++"
  show Target
TargetCppNoStl     = String
"C++ (without STL)"
  show Target
TargetHaskell      = String
"Haskell"
  show Target
TargetHaskellGadt  = String
"Haskell (with GADT)"
  show Target
TargetLatex        = String
"Latex"
  show Target
TargetJava         = String
"Java"
  show Target
TargetOCaml        = String
"OCaml"
  show Target
TargetPygments     = String
"Pygments"
  show Target
TargetCheck        = String
"Check LBNF file"

-- | Which version of Alex is targeted?
data AlexVersion = Alex3
  deriving (Int -> AlexVersion -> ShowS
[AlexVersion] -> ShowS
AlexVersion -> String
(Int -> AlexVersion -> ShowS)
-> (AlexVersion -> String)
-> ([AlexVersion] -> ShowS)
-> Show AlexVersion
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AlexVersion -> ShowS
showsPrec :: Int -> AlexVersion -> ShowS
$cshow :: AlexVersion -> String
show :: AlexVersion -> String
$cshowList :: [AlexVersion] -> ShowS
showList :: [AlexVersion] -> ShowS
Show,AlexVersion -> AlexVersion -> Bool
(AlexVersion -> AlexVersion -> Bool)
-> (AlexVersion -> AlexVersion -> Bool) -> Eq AlexVersion
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AlexVersion -> AlexVersion -> Bool
== :: AlexVersion -> AlexVersion -> Bool
$c/= :: AlexVersion -> AlexVersion -> Bool
/= :: AlexVersion -> AlexVersion -> Bool
Eq,Eq AlexVersion
Eq AlexVersion =>
(AlexVersion -> AlexVersion -> Ordering)
-> (AlexVersion -> AlexVersion -> Bool)
-> (AlexVersion -> AlexVersion -> Bool)
-> (AlexVersion -> AlexVersion -> Bool)
-> (AlexVersion -> AlexVersion -> Bool)
-> (AlexVersion -> AlexVersion -> AlexVersion)
-> (AlexVersion -> AlexVersion -> AlexVersion)
-> Ord AlexVersion
AlexVersion -> AlexVersion -> Bool
AlexVersion -> AlexVersion -> Ordering
AlexVersion -> AlexVersion -> AlexVersion
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: AlexVersion -> AlexVersion -> Ordering
compare :: AlexVersion -> AlexVersion -> Ordering
$c< :: AlexVersion -> AlexVersion -> Bool
< :: AlexVersion -> AlexVersion -> Bool
$c<= :: AlexVersion -> AlexVersion -> Bool
<= :: AlexVersion -> AlexVersion -> Bool
$c> :: AlexVersion -> AlexVersion -> Bool
> :: AlexVersion -> AlexVersion -> Bool
$c>= :: AlexVersion -> AlexVersion -> Bool
>= :: AlexVersion -> AlexVersion -> Bool
$cmax :: AlexVersion -> AlexVersion -> AlexVersion
max :: AlexVersion -> AlexVersion -> AlexVersion
$cmin :: AlexVersion -> AlexVersion -> AlexVersion
min :: AlexVersion -> AlexVersion -> AlexVersion
Ord,AlexVersion
AlexVersion -> AlexVersion -> Bounded AlexVersion
forall a. a -> a -> Bounded a
$cminBound :: AlexVersion
minBound :: AlexVersion
$cmaxBound :: AlexVersion
maxBound :: AlexVersion
Bounded,Int -> AlexVersion
AlexVersion -> Int
AlexVersion -> [AlexVersion]
AlexVersion -> AlexVersion
AlexVersion -> AlexVersion -> [AlexVersion]
AlexVersion -> AlexVersion -> AlexVersion -> [AlexVersion]
(AlexVersion -> AlexVersion)
-> (AlexVersion -> AlexVersion)
-> (Int -> AlexVersion)
-> (AlexVersion -> Int)
-> (AlexVersion -> [AlexVersion])
-> (AlexVersion -> AlexVersion -> [AlexVersion])
-> (AlexVersion -> AlexVersion -> [AlexVersion])
-> (AlexVersion -> AlexVersion -> AlexVersion -> [AlexVersion])
-> Enum AlexVersion
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: AlexVersion -> AlexVersion
succ :: AlexVersion -> AlexVersion
$cpred :: AlexVersion -> AlexVersion
pred :: AlexVersion -> AlexVersion
$ctoEnum :: Int -> AlexVersion
toEnum :: Int -> AlexVersion
$cfromEnum :: AlexVersion -> Int
fromEnum :: AlexVersion -> Int
$cenumFrom :: AlexVersion -> [AlexVersion]
enumFrom :: AlexVersion -> [AlexVersion]
$cenumFromThen :: AlexVersion -> AlexVersion -> [AlexVersion]
enumFromThen :: AlexVersion -> AlexVersion -> [AlexVersion]
$cenumFromTo :: AlexVersion -> AlexVersion -> [AlexVersion]
enumFromTo :: AlexVersion -> AlexVersion -> [AlexVersion]
$cenumFromThenTo :: AlexVersion -> AlexVersion -> AlexVersion -> [AlexVersion]
enumFromThenTo :: AlexVersion -> AlexVersion -> AlexVersion -> [AlexVersion]
Enum)

-- | Happy modes
data HappyMode = Standard | GLR
  deriving (HappyMode -> HappyMode -> Bool
(HappyMode -> HappyMode -> Bool)
-> (HappyMode -> HappyMode -> Bool) -> Eq HappyMode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HappyMode -> HappyMode -> Bool
== :: HappyMode -> HappyMode -> Bool
$c/= :: HappyMode -> HappyMode -> Bool
/= :: HappyMode -> HappyMode -> Bool
Eq,Int -> HappyMode -> ShowS
[HappyMode] -> ShowS
HappyMode -> String
(Int -> HappyMode -> ShowS)
-> (HappyMode -> String)
-> ([HappyMode] -> ShowS)
-> Show HappyMode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HappyMode -> ShowS
showsPrec :: Int -> HappyMode -> ShowS
$cshow :: HappyMode -> String
show :: HappyMode -> String
$cshowList :: [HappyMode] -> ShowS
showList :: [HappyMode] -> ShowS
Show,HappyMode
HappyMode -> HappyMode -> Bounded HappyMode
forall a. a -> a -> Bounded a
$cminBound :: HappyMode
minBound :: HappyMode
$cmaxBound :: HappyMode
maxBound :: HappyMode
Bounded,Int -> HappyMode
HappyMode -> Int
HappyMode -> [HappyMode]
HappyMode -> HappyMode
HappyMode -> HappyMode -> [HappyMode]
HappyMode -> HappyMode -> HappyMode -> [HappyMode]
(HappyMode -> HappyMode)
-> (HappyMode -> HappyMode)
-> (Int -> HappyMode)
-> (HappyMode -> Int)
-> (HappyMode -> [HappyMode])
-> (HappyMode -> HappyMode -> [HappyMode])
-> (HappyMode -> HappyMode -> [HappyMode])
-> (HappyMode -> HappyMode -> HappyMode -> [HappyMode])
-> Enum HappyMode
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: HappyMode -> HappyMode
succ :: HappyMode -> HappyMode
$cpred :: HappyMode -> HappyMode
pred :: HappyMode -> HappyMode
$ctoEnum :: Int -> HappyMode
toEnum :: Int -> HappyMode
$cfromEnum :: HappyMode -> Int
fromEnum :: HappyMode -> Int
$cenumFrom :: HappyMode -> [HappyMode]
enumFrom :: HappyMode -> [HappyMode]
$cenumFromThen :: HappyMode -> HappyMode -> [HappyMode]
enumFromThen :: HappyMode -> HappyMode -> [HappyMode]
$cenumFromTo :: HappyMode -> HappyMode -> [HappyMode]
enumFromTo :: HappyMode -> HappyMode -> [HappyMode]
$cenumFromThenTo :: HappyMode -> HappyMode -> HappyMode -> [HappyMode]
enumFromThenTo :: HappyMode -> HappyMode -> HappyMode -> [HappyMode]
Enum,Eq HappyMode
Eq HappyMode =>
(HappyMode -> HappyMode -> Ordering)
-> (HappyMode -> HappyMode -> Bool)
-> (HappyMode -> HappyMode -> Bool)
-> (HappyMode -> HappyMode -> Bool)
-> (HappyMode -> HappyMode -> Bool)
-> (HappyMode -> HappyMode -> HappyMode)
-> (HappyMode -> HappyMode -> HappyMode)
-> Ord HappyMode
HappyMode -> HappyMode -> Bool
HappyMode -> HappyMode -> Ordering
HappyMode -> HappyMode -> HappyMode
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: HappyMode -> HappyMode -> Ordering
compare :: HappyMode -> HappyMode -> Ordering
$c< :: HappyMode -> HappyMode -> Bool
< :: HappyMode -> HappyMode -> Bool
$c<= :: HappyMode -> HappyMode -> Bool
<= :: HappyMode -> HappyMode -> Bool
$c> :: HappyMode -> HappyMode -> Bool
> :: HappyMode -> HappyMode -> Bool
$c>= :: HappyMode -> HappyMode -> Bool
>= :: HappyMode -> HappyMode -> Bool
$cmax :: HappyMode -> HappyMode -> HappyMode
max :: HappyMode -> HappyMode -> HappyMode
$cmin :: HappyMode -> HappyMode -> HappyMode
min :: HappyMode -> HappyMode -> HappyMode
Ord)

-- | Which parser generator for ocaml?
data OCamlParser = OCamlYacc | Menhir
    deriving (OCamlParser -> OCamlParser -> Bool
(OCamlParser -> OCamlParser -> Bool)
-> (OCamlParser -> OCamlParser -> Bool) -> Eq OCamlParser
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OCamlParser -> OCamlParser -> Bool
== :: OCamlParser -> OCamlParser -> Bool
$c/= :: OCamlParser -> OCamlParser -> Bool
/= :: OCamlParser -> OCamlParser -> Bool
Eq,Int -> OCamlParser -> ShowS
[OCamlParser] -> ShowS
OCamlParser -> String
(Int -> OCamlParser -> ShowS)
-> (OCamlParser -> String)
-> ([OCamlParser] -> ShowS)
-> Show OCamlParser
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OCamlParser -> ShowS
showsPrec :: Int -> OCamlParser -> ShowS
$cshow :: OCamlParser -> String
show :: OCamlParser -> String
$cshowList :: [OCamlParser] -> ShowS
showList :: [OCamlParser] -> ShowS
Show,Eq OCamlParser
Eq OCamlParser =>
(OCamlParser -> OCamlParser -> Ordering)
-> (OCamlParser -> OCamlParser -> Bool)
-> (OCamlParser -> OCamlParser -> Bool)
-> (OCamlParser -> OCamlParser -> Bool)
-> (OCamlParser -> OCamlParser -> Bool)
-> (OCamlParser -> OCamlParser -> OCamlParser)
-> (OCamlParser -> OCamlParser -> OCamlParser)
-> Ord OCamlParser
OCamlParser -> OCamlParser -> Bool
OCamlParser -> OCamlParser -> Ordering
OCamlParser -> OCamlParser -> OCamlParser
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: OCamlParser -> OCamlParser -> Ordering
compare :: OCamlParser -> OCamlParser -> Ordering
$c< :: OCamlParser -> OCamlParser -> Bool
< :: OCamlParser -> OCamlParser -> Bool
$c<= :: OCamlParser -> OCamlParser -> Bool
<= :: OCamlParser -> OCamlParser -> Bool
$c> :: OCamlParser -> OCamlParser -> Bool
> :: OCamlParser -> OCamlParser -> Bool
$c>= :: OCamlParser -> OCamlParser -> Bool
>= :: OCamlParser -> OCamlParser -> Bool
$cmax :: OCamlParser -> OCamlParser -> OCamlParser
max :: OCamlParser -> OCamlParser -> OCamlParser
$cmin :: OCamlParser -> OCamlParser -> OCamlParser
min :: OCamlParser -> OCamlParser -> OCamlParser
Ord)

-- | Which Java backend?
data JavaLexerParser = JLexCup | JFlexCup | Antlr4
    deriving (JavaLexerParser -> JavaLexerParser -> Bool
(JavaLexerParser -> JavaLexerParser -> Bool)
-> (JavaLexerParser -> JavaLexerParser -> Bool)
-> Eq JavaLexerParser
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: JavaLexerParser -> JavaLexerParser -> Bool
== :: JavaLexerParser -> JavaLexerParser -> Bool
$c/= :: JavaLexerParser -> JavaLexerParser -> Bool
/= :: JavaLexerParser -> JavaLexerParser -> Bool
Eq,Int -> JavaLexerParser -> ShowS
[JavaLexerParser] -> ShowS
JavaLexerParser -> String
(Int -> JavaLexerParser -> ShowS)
-> (JavaLexerParser -> String)
-> ([JavaLexerParser] -> ShowS)
-> Show JavaLexerParser
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> JavaLexerParser -> ShowS
showsPrec :: Int -> JavaLexerParser -> ShowS
$cshow :: JavaLexerParser -> String
show :: JavaLexerParser -> String
$cshowList :: [JavaLexerParser] -> ShowS
showList :: [JavaLexerParser] -> ShowS
Show,Eq JavaLexerParser
Eq JavaLexerParser =>
(JavaLexerParser -> JavaLexerParser -> Ordering)
-> (JavaLexerParser -> JavaLexerParser -> Bool)
-> (JavaLexerParser -> JavaLexerParser -> Bool)
-> (JavaLexerParser -> JavaLexerParser -> Bool)
-> (JavaLexerParser -> JavaLexerParser -> Bool)
-> (JavaLexerParser -> JavaLexerParser -> JavaLexerParser)
-> (JavaLexerParser -> JavaLexerParser -> JavaLexerParser)
-> Ord JavaLexerParser
JavaLexerParser -> JavaLexerParser -> Bool
JavaLexerParser -> JavaLexerParser -> Ordering
JavaLexerParser -> JavaLexerParser -> JavaLexerParser
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: JavaLexerParser -> JavaLexerParser -> Ordering
compare :: JavaLexerParser -> JavaLexerParser -> Ordering
$c< :: JavaLexerParser -> JavaLexerParser -> Bool
< :: JavaLexerParser -> JavaLexerParser -> Bool
$c<= :: JavaLexerParser -> JavaLexerParser -> Bool
<= :: JavaLexerParser -> JavaLexerParser -> Bool
$c> :: JavaLexerParser -> JavaLexerParser -> Bool
> :: JavaLexerParser -> JavaLexerParser -> Bool
$c>= :: JavaLexerParser -> JavaLexerParser -> Bool
>= :: JavaLexerParser -> JavaLexerParser -> Bool
$cmax :: JavaLexerParser -> JavaLexerParser -> JavaLexerParser
max :: JavaLexerParser -> JavaLexerParser -> JavaLexerParser
$cmin :: JavaLexerParser -> JavaLexerParser -> JavaLexerParser
min :: JavaLexerParser -> JavaLexerParser -> JavaLexerParser
Ord)

-- | Line numbers or not?
data RecordPositions = RecordPositions | NoRecordPositions
    deriving (RecordPositions -> RecordPositions -> Bool
(RecordPositions -> RecordPositions -> Bool)
-> (RecordPositions -> RecordPositions -> Bool)
-> Eq RecordPositions
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RecordPositions -> RecordPositions -> Bool
== :: RecordPositions -> RecordPositions -> Bool
$c/= :: RecordPositions -> RecordPositions -> Bool
/= :: RecordPositions -> RecordPositions -> Bool
Eq,Int -> RecordPositions -> ShowS
[RecordPositions] -> ShowS
RecordPositions -> String
(Int -> RecordPositions -> ShowS)
-> (RecordPositions -> String)
-> ([RecordPositions] -> ShowS)
-> Show RecordPositions
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> RecordPositions -> ShowS
showsPrec :: Int -> RecordPositions -> ShowS
$cshow :: RecordPositions -> String
show :: RecordPositions -> String
$cshowList :: [RecordPositions] -> ShowS
showList :: [RecordPositions] -> ShowS
Show,Eq RecordPositions
Eq RecordPositions =>
(RecordPositions -> RecordPositions -> Ordering)
-> (RecordPositions -> RecordPositions -> Bool)
-> (RecordPositions -> RecordPositions -> Bool)
-> (RecordPositions -> RecordPositions -> Bool)
-> (RecordPositions -> RecordPositions -> Bool)
-> (RecordPositions -> RecordPositions -> RecordPositions)
-> (RecordPositions -> RecordPositions -> RecordPositions)
-> Ord RecordPositions
RecordPositions -> RecordPositions -> Bool
RecordPositions -> RecordPositions -> Ordering
RecordPositions -> RecordPositions -> RecordPositions
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: RecordPositions -> RecordPositions -> Ordering
compare :: RecordPositions -> RecordPositions -> Ordering
$c< :: RecordPositions -> RecordPositions -> Bool
< :: RecordPositions -> RecordPositions -> Bool
$c<= :: RecordPositions -> RecordPositions -> Bool
<= :: RecordPositions -> RecordPositions -> Bool
$c> :: RecordPositions -> RecordPositions -> Bool
> :: RecordPositions -> RecordPositions -> Bool
$c>= :: RecordPositions -> RecordPositions -> Bool
>= :: RecordPositions -> RecordPositions -> Bool
$cmax :: RecordPositions -> RecordPositions -> RecordPositions
max :: RecordPositions -> RecordPositions -> RecordPositions
$cmin :: RecordPositions -> RecordPositions -> RecordPositions
min :: RecordPositions -> RecordPositions -> RecordPositions
Ord)

-- | Restrict to ANSI standard (C/C++)?
data Ansi = Ansi | BeyondAnsi
    deriving (Ansi -> Ansi -> Bool
(Ansi -> Ansi -> Bool) -> (Ansi -> Ansi -> Bool) -> Eq Ansi
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Ansi -> Ansi -> Bool
== :: Ansi -> Ansi -> Bool
$c/= :: Ansi -> Ansi -> Bool
/= :: Ansi -> Ansi -> Bool
Eq, Eq Ansi
Eq Ansi =>
(Ansi -> Ansi -> Ordering)
-> (Ansi -> Ansi -> Bool)
-> (Ansi -> Ansi -> Bool)
-> (Ansi -> Ansi -> Bool)
-> (Ansi -> Ansi -> Bool)
-> (Ansi -> Ansi -> Ansi)
-> (Ansi -> Ansi -> Ansi)
-> Ord Ansi
Ansi -> Ansi -> Bool
Ansi -> Ansi -> Ordering
Ansi -> Ansi -> Ansi
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Ansi -> Ansi -> Ordering
compare :: Ansi -> Ansi -> Ordering
$c< :: Ansi -> Ansi -> Bool
< :: Ansi -> Ansi -> Bool
$c<= :: Ansi -> Ansi -> Bool
<= :: Ansi -> Ansi -> Bool
$c> :: Ansi -> Ansi -> Bool
> :: Ansi -> Ansi -> Bool
$c>= :: Ansi -> Ansi -> Bool
>= :: Ansi -> Ansi -> Bool
$cmax :: Ansi -> Ansi -> Ansi
max :: Ansi -> Ansi -> Ansi
$cmin :: Ansi -> Ansi -> Ansi
min :: Ansi -> Ansi -> Ansi
Ord, Int -> Ansi -> ShowS
[Ansi] -> ShowS
Ansi -> String
(Int -> Ansi -> ShowS)
-> (Ansi -> String) -> ([Ansi] -> ShowS) -> Show Ansi
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Ansi -> ShowS
showsPrec :: Int -> Ansi -> ShowS
$cshow :: Ansi -> String
show :: Ansi -> String
$cshowList :: [Ansi] -> ShowS
showList :: [Ansi] -> ShowS
Show)

-- | Package name (C++ and Java backends).
type InPackage = Maybe String

-- | How to represent token content in the Haskell backend?

data TokenText
  = StringToken      -- ^ Represent strings as @String@.
  | ByteStringToken  -- ^ Represent strings as @ByteString@.
  | TextToken        -- ^ Represent strings as @Data.Text@.
  deriving (TokenText -> TokenText -> Bool
(TokenText -> TokenText -> Bool)
-> (TokenText -> TokenText -> Bool) -> Eq TokenText
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TokenText -> TokenText -> Bool
== :: TokenText -> TokenText -> Bool
$c/= :: TokenText -> TokenText -> Bool
/= :: TokenText -> TokenText -> Bool
Eq, Eq TokenText
Eq TokenText =>
(TokenText -> TokenText -> Ordering)
-> (TokenText -> TokenText -> Bool)
-> (TokenText -> TokenText -> Bool)
-> (TokenText -> TokenText -> Bool)
-> (TokenText -> TokenText -> Bool)
-> (TokenText -> TokenText -> TokenText)
-> (TokenText -> TokenText -> TokenText)
-> Ord TokenText
TokenText -> TokenText -> Bool
TokenText -> TokenText -> Ordering
TokenText -> TokenText -> TokenText
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: TokenText -> TokenText -> Ordering
compare :: TokenText -> TokenText -> Ordering
$c< :: TokenText -> TokenText -> Bool
< :: TokenText -> TokenText -> Bool
$c<= :: TokenText -> TokenText -> Bool
<= :: TokenText -> TokenText -> Bool
$c> :: TokenText -> TokenText -> Bool
> :: TokenText -> TokenText -> Bool
$c>= :: TokenText -> TokenText -> Bool
>= :: TokenText -> TokenText -> Bool
$cmax :: TokenText -> TokenText -> TokenText
max :: TokenText -> TokenText -> TokenText
$cmin :: TokenText -> TokenText -> TokenText
min :: TokenText -> TokenText -> TokenText
Ord, Int -> TokenText -> ShowS
[TokenText] -> ShowS
TokenText -> String
(Int -> TokenText -> ShowS)
-> (TokenText -> String)
-> ([TokenText] -> ShowS)
-> Show TokenText
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TokenText -> ShowS
showsPrec :: Int -> TokenText -> ShowS
$cshow :: TokenText -> String
show :: TokenText -> String
$cshowList :: [TokenText] -> ShowS
showList :: [TokenText] -> ShowS
Show)

-- | This is the option record that is passed to the different backends.
data SharedOptions = Options
  --- Option shared by at least 2 backends
  { SharedOptions -> String
lbnfFile    :: FilePath        -- ^ The input file BNFC processes.
  , SharedOptions -> String
lang        :: String          -- ^ The language we generate: the basename of 'lbnfFile'.
  , SharedOptions -> String
outDir      :: FilePath        -- ^ Target directory for generated files.
  , SharedOptions -> Bool
force       :: Bool            -- ^ Ignore errors as much as possible?
  , SharedOptions -> Target
target      :: Target          -- ^ E.g. @--haskell@.
  , SharedOptions -> Maybe String
optMake     :: Maybe String    -- ^ The name of the Makefile to generate or Nothing for no Makefile.
  , SharedOptions -> Maybe String
inPackage   :: InPackage       -- ^ The hierarchical package to put the modules in, or Nothing.
  , SharedOptions -> RecordPositions
linenumbers :: RecordPositions -- ^ Add and set line_number field for syntax classes
  , SharedOptions -> Ansi
ansi        :: Ansi            -- ^ Restrict to the ANSI language standard (C/C++)?
  --- Haskell specific:
  , SharedOptions -> Bool
inDir         :: Bool        -- ^ Option @-d@.
  , SharedOptions -> Bool
functor       :: Bool        -- ^ Option @--functor@.  Make AST functorial?
  , SharedOptions -> Bool
generic       :: Bool        -- ^ Option @--generic@.  Derive Data, Generic, Typeable?
  , SharedOptions -> AlexVersion
alexMode      :: AlexVersion -- ^ Options @--alex@.
  , SharedOptions -> TokenText
tokenText     :: TokenText   -- ^ Options @--bytestrings@, @--string-token@, and @--text-token@.
  , SharedOptions -> HappyMode
glr           :: HappyMode   -- ^ Happy option @--glr@.
  , SharedOptions -> Int
xml           :: Int         -- ^ Options @--xml@, generate DTD and XML printers.
  , SharedOptions -> Bool
agda          :: Bool        -- ^ Option @--agda@. Create bindings for Agda?
  --- OCaml specific
  , SharedOptions -> OCamlParser
ocamlParser   :: OCamlParser -- ^ Option @--menhir@ to switch to @Menhir@.
  --- Java specific
  , SharedOptions -> JavaLexerParser
javaLexerParser :: JavaLexerParser
  --- C# specific
  , SharedOptions -> Bool
visualStudio  :: Bool        -- ^ Generate Visual Studio solution/project files.
  , SharedOptions -> Bool
wcf           :: Bool        -- ^ Windows Communication Foundation.
  } deriving (SharedOptions -> SharedOptions -> Bool
(SharedOptions -> SharedOptions -> Bool)
-> (SharedOptions -> SharedOptions -> Bool) -> Eq SharedOptions
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SharedOptions -> SharedOptions -> Bool
== :: SharedOptions -> SharedOptions -> Bool
$c/= :: SharedOptions -> SharedOptions -> Bool
/= :: SharedOptions -> SharedOptions -> Bool
Eq, Eq SharedOptions
Eq SharedOptions =>
(SharedOptions -> SharedOptions -> Ordering)
-> (SharedOptions -> SharedOptions -> Bool)
-> (SharedOptions -> SharedOptions -> Bool)
-> (SharedOptions -> SharedOptions -> Bool)
-> (SharedOptions -> SharedOptions -> Bool)
-> (SharedOptions -> SharedOptions -> SharedOptions)
-> (SharedOptions -> SharedOptions -> SharedOptions)
-> Ord SharedOptions
SharedOptions -> SharedOptions -> Bool
SharedOptions -> SharedOptions -> Ordering
SharedOptions -> SharedOptions -> SharedOptions
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: SharedOptions -> SharedOptions -> Ordering
compare :: SharedOptions -> SharedOptions -> Ordering
$c< :: SharedOptions -> SharedOptions -> Bool
< :: SharedOptions -> SharedOptions -> Bool
$c<= :: SharedOptions -> SharedOptions -> Bool
<= :: SharedOptions -> SharedOptions -> Bool
$c> :: SharedOptions -> SharedOptions -> Bool
> :: SharedOptions -> SharedOptions -> Bool
$c>= :: SharedOptions -> SharedOptions -> Bool
>= :: SharedOptions -> SharedOptions -> Bool
$cmax :: SharedOptions -> SharedOptions -> SharedOptions
max :: SharedOptions -> SharedOptions -> SharedOptions
$cmin :: SharedOptions -> SharedOptions -> SharedOptions
min :: SharedOptions -> SharedOptions -> SharedOptions
Ord, Int -> SharedOptions -> ShowS
[SharedOptions] -> ShowS
SharedOptions -> String
(Int -> SharedOptions -> ShowS)
-> (SharedOptions -> String)
-> ([SharedOptions] -> ShowS)
-> Show SharedOptions
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SharedOptions -> ShowS
showsPrec :: Int -> SharedOptions -> ShowS
$cshow :: SharedOptions -> String
show :: SharedOptions -> String
$cshowList :: [SharedOptions] -> ShowS
showList :: [SharedOptions] -> ShowS
Show)

-- We take this opportunity to define the type of the backend functions.
type Backend = SharedOptions  -- ^ Options
            -> CF             -- ^ Grammar
            -> IO ()

defaultOptions :: SharedOptions
defaultOptions :: SharedOptions
defaultOptions = Options
  { lbnfFile :: String
lbnfFile        = ShowS
forall a. HasCallStack => String -> a
error String
"lbnfFile not set"
  , lang :: String
lang            = ShowS
forall a. HasCallStack => String -> a
error String
"lang not set"
  , outDir :: String
outDir          = String
"."
  , force :: Bool
force           = Bool
False
  , target :: Target
target          = Target
TargetHaskell
  , optMake :: Maybe String
optMake         = Maybe String
forall a. Maybe a
Nothing
  , inPackage :: Maybe String
inPackage       = Maybe String
forall a. Maybe a
Nothing
  , linenumbers :: RecordPositions
linenumbers     = RecordPositions
NoRecordPositions
  , ansi :: Ansi
ansi            = Ansi
BeyondAnsi
  -- Haskell specific
  , inDir :: Bool
inDir           = Bool
False
  , functor :: Bool
functor         = Bool
False
  , generic :: Bool
generic         = Bool
False
  , alexMode :: AlexVersion
alexMode        = AlexVersion
Alex3
  , tokenText :: TokenText
tokenText       = TokenText
StringToken
  , glr :: HappyMode
glr             = HappyMode
Standard
  , xml :: Int
xml             = Int
0
  , agda :: Bool
agda            = Bool
False
  -- OCaml specific
  , ocamlParser :: OCamlParser
ocamlParser     = OCamlParser
OCamlYacc
  -- Java specific
  , javaLexerParser :: JavaLexerParser
javaLexerParser = JavaLexerParser
JLexCup
  -- C# specific
  , visualStudio :: Bool
visualStudio    = Bool
False
  , wcf :: Bool
wcf             = Bool
False
  }

-- | Check whether an option is unchanged from the default.
isDefault :: (Eq a)
  => (SharedOptions -> a)  -- ^ Option field name.
  -> SharedOptions         -- ^ Options.
  -> Bool
isDefault :: forall a. Eq a => (SharedOptions -> a) -> SharedOptions -> Bool
isDefault SharedOptions -> a
flag SharedOptions
opts = SharedOptions -> a
flag SharedOptions
opts a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== SharedOptions -> a
flag SharedOptions
defaultOptions

-- | Return something in case option differs from default.
unlessDefault :: (Monoid m, Eq a)
  => (SharedOptions -> a)  -- ^ Option field name.
  -> SharedOptions         -- ^ Options.
  -> (a -> m)              -- ^ Action in case option differs from standard.
  -> m
unlessDefault :: forall m a.
(Monoid m, Eq a) =>
(SharedOptions -> a) -> SharedOptions -> (a -> m) -> m
unlessDefault SharedOptions -> a
flag SharedOptions
opts a -> m
f = Bool -> m -> m
forall m. Monoid m => Bool -> m -> m
unless (a
o a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== SharedOptions -> a
flag SharedOptions
defaultOptions) (m -> m) -> m -> m
forall a b. (a -> b) -> a -> b
$ a -> m
f a
o
  where o :: a
o = SharedOptions -> a
flag SharedOptions
opts

-- -- | Return something in case option is unchanged from default.
-- whenDefault :: (Monoid m, Eq a)
--   => (SharedOptions -> a)  -- ^ Option field name.
--   -> SharedOptions         -- ^ Options.
--   -> m                     -- ^ Action in case option is unchanged from standard.
--   -> m
-- whenDefault flag opts m = when (o == flag defaultOptions) m
--   where o = flag opts

-- | Print options as input to BNFC.
--
-- @unwords [ "bnfc", printOptions opts ]@ should call bnfc with the same options
-- as the current instance.
--
printOptions :: SharedOptions -> String
printOptions :: SharedOptions -> String
printOptions SharedOptions
opts = UsageWarnings -> String
unwords (UsageWarnings -> String)
-> ([UsageWarnings] -> UsageWarnings) -> [UsageWarnings] -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [UsageWarnings] -> UsageWarnings
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([UsageWarnings] -> String) -> [UsageWarnings] -> String
forall a b. (a -> b) -> a -> b
$
  [ [ Target -> String
printTargetOption Target
tgt ]
  -- General and shared options:
  , (SharedOptions -> String)
-> SharedOptions -> (String -> UsageWarnings) -> UsageWarnings
forall m a.
(Monoid m, Eq a) =>
(SharedOptions -> a) -> SharedOptions -> (a -> m) -> m
unlessDefault SharedOptions -> String
outDir SharedOptions
opts ((String -> UsageWarnings) -> UsageWarnings)
-> (String -> UsageWarnings) -> UsageWarnings
forall a b. (a -> b) -> a -> b
$ \ String
o -> [ String
"--outputdir=" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
o ]
  , [ String
"--makefile=" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
m  | String
m <- Maybe String -> UsageWarnings
forall a. Maybe a -> [a]
maybeToList (Maybe String -> UsageWarnings) -> Maybe String -> UsageWarnings
forall a b. (a -> b) -> a -> b
$ SharedOptions -> Maybe String
optMake SharedOptions
opts        ]
  , [ String
"-p " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
p          | String
p <- Maybe String -> UsageWarnings
forall a. Maybe a -> [a]
maybeToList (Maybe String -> UsageWarnings) -> Maybe String -> UsageWarnings
forall a b. (a -> b) -> a -> b
$ SharedOptions -> Maybe String
inPackage SharedOptions
opts   ]
  , (SharedOptions -> RecordPositions)
-> SharedOptions
-> (RecordPositions -> UsageWarnings)
-> UsageWarnings
forall m a.
(Monoid m, Eq a) =>
(SharedOptions -> a) -> SharedOptions -> (a -> m) -> m
unlessDefault SharedOptions -> RecordPositions
linenumbers SharedOptions
opts ((RecordPositions -> UsageWarnings) -> UsageWarnings)
-> (RecordPositions -> UsageWarnings) -> UsageWarnings
forall a b. (a -> b) -> a -> b
$ UsageWarnings -> RecordPositions -> UsageWarnings
forall a b. a -> b -> a
const [ String
"-l" ]
  , (SharedOptions -> Ansi)
-> SharedOptions -> (Ansi -> UsageWarnings) -> UsageWarnings
forall m a.
(Monoid m, Eq a) =>
(SharedOptions -> a) -> SharedOptions -> (a -> m) -> m
unlessDefault SharedOptions -> Ansi
ansi SharedOptions
opts ((Ansi -> UsageWarnings) -> UsageWarnings)
-> (Ansi -> UsageWarnings) -> UsageWarnings
forall a b. (a -> b) -> a -> b
$ UsageWarnings -> Ansi -> UsageWarnings
forall a b. a -> b -> a
const [ String
"--ansi" ]
  -- Haskell options:
  , [ String
"-d"                | SharedOptions -> Bool
inDir SharedOptions
opts                          ]
  , [ String
"--functor"         | SharedOptions -> Bool
functor SharedOptions
opts                        ]
  , [ String
"--generic"         | SharedOptions -> Bool
generic SharedOptions
opts                        ]
  , (SharedOptions -> AlexVersion)
-> SharedOptions -> (AlexVersion -> UsageWarnings) -> UsageWarnings
forall m a.
(Monoid m, Eq a) =>
(SharedOptions -> a) -> SharedOptions -> (a -> m) -> m
unlessDefault SharedOptions -> AlexVersion
alexMode SharedOptions
opts ((AlexVersion -> UsageWarnings) -> UsageWarnings)
-> (AlexVersion -> UsageWarnings) -> UsageWarnings
forall a b. (a -> b) -> a -> b
$ \ AlexVersion
o -> [ AlexVersion -> String
printAlexOption AlexVersion
o ]
  , [ String
"--bytestrings"     | SharedOptions -> TokenText
tokenText SharedOptions
opts TokenText -> TokenText -> Bool
forall a. Eq a => a -> a -> Bool
== TokenText
ByteStringToken   ]
  , [ String
"--text-token"      | SharedOptions -> TokenText
tokenText SharedOptions
opts TokenText -> TokenText -> Bool
forall a. Eq a => a -> a -> Bool
== TokenText
TextToken, Bool -> Bool
not (SharedOptions -> Bool
agda SharedOptions
opts) ]  -- default for --agda
  , [ String
"--string-token"    | SharedOptions -> TokenText
tokenText SharedOptions
opts TokenText -> TokenText -> Bool
forall a. Eq a => a -> a -> Bool
== TokenText
StringToken, SharedOptions -> Bool
agda SharedOptions
opts ]      -- default unless --agda
  , [ String
"--glr"             | SharedOptions -> HappyMode
glr SharedOptions
opts HappyMode -> HappyMode -> Bool
forall a. Eq a => a -> a -> Bool
== HappyMode
GLR                     ]
  , [ String
"--xml"             | SharedOptions -> Int
xml SharedOptions
opts Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1                       ]
  , [ String
"--xmlt"            | SharedOptions -> Int
xml SharedOptions
opts Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
2                       ]
  , [ String
"--agda"            | SharedOptions -> Bool
agda SharedOptions
opts                           ]
  -- C# options:
  , [ String
"--vs"              | SharedOptions -> Bool
visualStudio SharedOptions
opts                   ]
  , [ String
"--wfc"             | SharedOptions -> Bool
wcf SharedOptions
opts                            ]
  -- Java options:
  , (SharedOptions -> JavaLexerParser)
-> SharedOptions
-> (JavaLexerParser -> UsageWarnings)
-> UsageWarnings
forall m a.
(Monoid m, Eq a) =>
(SharedOptions -> a) -> SharedOptions -> (a -> m) -> m
unlessDefault SharedOptions -> JavaLexerParser
javaLexerParser SharedOptions
opts ((JavaLexerParser -> UsageWarnings) -> UsageWarnings)
-> (JavaLexerParser -> UsageWarnings) -> UsageWarnings
forall a b. (a -> b) -> a -> b
$ \ JavaLexerParser
o -> [ JavaLexerParser -> String
printJavaLexerParserOption JavaLexerParser
o ]
  -- Java options:
  , (SharedOptions -> OCamlParser)
-> SharedOptions -> (OCamlParser -> UsageWarnings) -> UsageWarnings
forall m a.
(Monoid m, Eq a) =>
(SharedOptions -> a) -> SharedOptions -> (a -> m) -> m
unlessDefault SharedOptions -> OCamlParser
ocamlParser SharedOptions
opts ((OCamlParser -> UsageWarnings) -> UsageWarnings)
-> (OCamlParser -> UsageWarnings) -> UsageWarnings
forall a b. (a -> b) -> a -> b
$ \ OCamlParser
o -> [ OCamlParser -> String
printOCamlParserOption OCamlParser
o ]
  -- Grammar file:
  , [ SharedOptions -> String
lbnfFile SharedOptions
opts ]
  ]
  where
  tgt :: Target
tgt = SharedOptions -> Target
target SharedOptions
opts
  -- haskell = tgt `elem` haskellTargets

-- | Print target as an option to BNFC.
printTargetOption :: Target -> String
printTargetOption :: Target -> String
printTargetOption = (String
"--" String -> ShowS
forall a. [a] -> [a] -> [a]
++) ShowS -> (Target -> String) -> Target -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. \case
  Target
TargetC           -> String
"c"
  Target
TargetCpp         -> String
"cpp"
  Target
TargetCppNoStl    -> String
"cpp-nostl"
  Target
TargetHaskell     -> String
"haskell"
  Target
TargetHaskellGadt -> String
"haskell-gadt"
  Target
TargetLatex       -> String
"latex"
  Target
TargetJava        -> String
"java"
  Target
TargetOCaml       -> String
"ocaml"
  Target
TargetPygments    -> String
"pygments"
  Target
TargetCheck       -> String
"check"

printAlexOption :: AlexVersion -> String
printAlexOption :: AlexVersion -> String
printAlexOption = (String
"--" String -> ShowS
forall a. [a] -> [a] -> [a]
++) ShowS -> (AlexVersion -> String) -> AlexVersion -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. \case
  AlexVersion
Alex3 -> String
"alex3"

printJavaLexerParserOption :: JavaLexerParser -> String
printJavaLexerParserOption :: JavaLexerParser -> String
printJavaLexerParserOption = (String
"--" String -> ShowS
forall a. [a] -> [a] -> [a]
++) ShowS -> (JavaLexerParser -> String) -> JavaLexerParser -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. \case
  JavaLexerParser
JLexCup  -> String
"jlex"
  JavaLexerParser
JFlexCup -> String
"jflex"
  JavaLexerParser
Antlr4   -> String
"antlr4"

printOCamlParserOption :: OCamlParser -> String
printOCamlParserOption :: OCamlParser -> String
printOCamlParserOption = (String
"--" String -> ShowS
forall a. [a] -> [a] -> [a]
++) ShowS -> (OCamlParser -> String) -> OCamlParser -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. \case
  OCamlParser
OCamlYacc -> String
"yacc"
  OCamlParser
Menhir    -> String
"menhir"

-- ~~~ Option definition ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- This defines bnfc's "global" options, like --help
globalOptions :: [ OptDescr Mode ]
globalOptions :: [OptDescr Mode]
globalOptions = [
  String -> UsageWarnings -> ArgDescr Mode -> String -> OptDescr Mode
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option [] [String
"help"]                      (Mode -> ArgDescr Mode
forall a. a -> ArgDescr a
NoArg Mode
Help)         String
"show help",
  String -> UsageWarnings -> ArgDescr Mode -> String -> OptDescr Mode
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option [] [String
"license"]                   (Mode -> ArgDescr Mode
forall a. a -> ArgDescr a
NoArg Mode
License)      String
"show license",
  String -> UsageWarnings -> ArgDescr Mode -> String -> OptDescr Mode
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option [] [String
"version",String
"numeric-version"] (Mode -> ArgDescr Mode
forall a. a -> ArgDescr a
NoArg Mode
Version)      String
"show version number"]

-- | Options for the target languages
-- targetOptions :: [ OptDescr Target ]
targetOptions :: [ OptDescr (SharedOptions -> SharedOptions)]
targetOptions :: [OptDescr (SharedOptions -> SharedOptions)]
targetOptions =
  [ String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option String
"" [String
"java"]          ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o {target = TargetJava}))
    String
"Output Java code [default: for use with JLex and CUP]"
  , String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option String
"" [String
"java-antlr"]    ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\ SharedOptions
o -> SharedOptions
o{ target = TargetJava, javaLexerParser = Antlr4 }))
    String
"Output Java code for use with ANTLR (short for --java --antlr)"
  , String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option String
"" [String
"haskell"]       ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o {target = TargetHaskell}))
    String
"Output Haskell code for use with Alex and Happy (default)"
  , String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option String
"" [String
"haskell-gadt"]  ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o {target = TargetHaskellGadt}))
    String
"Output Haskell code which uses GADTs"
  , String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option String
"" [String
"latex"]         ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o {target = TargetLatex}))
    String
"Output LaTeX code to generate a PDF description of the language"
  , String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option String
"" [String
"c"]             ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o {target = TargetC}))
    String
"Output C code for use with FLex and Bison"
  , String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option String
"" [String
"cpp"]           ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o {target = TargetCpp}))
    String
"Output C++ code for use with FLex and Bison"
  , String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option String
"" [String
"cpp-nostl"]     ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o {target = TargetCppNoStl}))
    String
"Output C++ code (without STL) for use with FLex and Bison"
  , String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option String
"" [String
"ocaml"]         ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o {target = TargetOCaml}))
    String
"Output OCaml code for use with ocamllex and ocamlyacc"
  , String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option String
"" [String
"ocaml-menhir"]  ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\ SharedOptions
o -> SharedOptions
o{ target = TargetOCaml, ocamlParser = Menhir }))
    String
"Output OCaml code for use with ocamllex and menhir (short for --ocaml --menhir)"
  , String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option String
"" [String
"pygments"]      ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o {target = TargetPygments}))
    String
"Output a Python lexer for Pygments"
  , String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option String
"" [String
"check"]         ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\ SharedOptions
o -> SharedOptions
o{target = TargetCheck }))
    String
"No output. Just check input LBNF file"
  ]

-- | A list of the options and for each of them, the target language
-- they apply to.
specificOptions :: [(OptDescr (SharedOptions -> SharedOptions), [Target])]
specificOptions :: [(OptDescr (SharedOptions -> SharedOptions), [Target])]
specificOptions =
  [ ( String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option [Char
'l'] [String
"line-numbers"] ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o {linenumbers = RecordPositions})) (String -> OptDescr (SharedOptions -> SharedOptions))
-> String -> OptDescr (SharedOptions -> SharedOptions)
forall a b. (a -> b) -> a -> b
$ UsageWarnings -> String
unlines
        [ String
"Add and set line_number field for all syntax classes"
        , String
"(Note: Java requires cup version 0.11b-2014-06-11 or greater.)"
        ]
    , [Target
TargetC, Target
TargetCpp, Target
TargetJava] )
  , ( String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option [] [String
"ansi"] ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o{ ansi = Ansi })) (String -> OptDescr (SharedOptions -> SharedOptions))
-> String -> OptDescr (SharedOptions -> SharedOptions)
forall a b. (a -> b) -> a -> b
$ UsageWarnings -> String
unlines
        [ String
"Restrict to ANSI language standard"
        ]
    , [Target
TargetCpp] )  -- In the future maybe also: TargetC
  , ( String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option [Char
'p'] [String
"name-space"]
      ((String -> SharedOptions -> SharedOptions)
-> String -> ArgDescr (SharedOptions -> SharedOptions)
forall a. (String -> a) -> String -> ArgDescr a
ReqArg (\String
n SharedOptions
o -> SharedOptions
o {inPackage = Just n}) String
"NAMESPACE")
          String
"Prepend NAMESPACE to the package/module name"
    , [Target
TargetCpp, Target
TargetJava] [Target] -> [Target] -> [Target]
forall a. [a] -> [a] -> [a]
++ [Target]
haskellTargets)
  -- Java backend:
  , ( String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option [] [String
"jlex"  ] ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o {javaLexerParser = JLexCup}))
          String
"Lex with JLex, parse with CUP (default)"
    , [Target
TargetJava] )
  , ( String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option [] [String
"jflex" ] ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o {javaLexerParser = JFlexCup}))
          String
"Lex with JFlex, parse with CUP"
    , [Target
TargetJava] )
  , ( String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option [] [String
"antlr4"] ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o {javaLexerParser = Antlr4}))
          String
"Lex and parse with antlr4"
    , [Target
TargetJava] )
  -- OCaml backend:
  , ( String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option [] [String
"yacc"  ] ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\ SharedOptions
o -> SharedOptions
o { ocamlParser = OCamlYacc }))
          String
"Generate parser with ocamlyacc (default)"
    , [Target
TargetOCaml] )
  , ( String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option [] [String
"menhir"] ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\ SharedOptions
o -> SharedOptions
o { ocamlParser = Menhir }))
          String
"Generate parser with menhir"
    , [Target
TargetOCaml] )
  -- Haskell backends:
  , ( String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option [Char
'd'] [] ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o {inDir = True}))
          String
"Put Haskell code in modules LANG.* instead of LANG* (recommended)"
    , [Target]
haskellTargets )
  -- -- Option --alex3 is obsolete since Alex 3 is the only choice now.
  -- -- Keep this in case there will be a new lexer backend for Haskell.
  -- , ( Option []    ["alex3"] (NoArg (\o -> o {alexMode = Alex3}))
  --         "Use Alex 3 as Haskell lexer tool (default)"
  --   , haskellTargets )
  , ( String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option []    [String
"bytestrings"] ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o { tokenText = ByteStringToken }))
          String
"Use ByteString in Alex lexer [deprecated, use --text-token]"
    , [Target]
haskellTargets )
  , ( String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option []    [String
"text-token"] ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o { tokenText = TextToken }))
          String
"Use Text in Alex lexer"
          -- "Use Text in Alex lexer (default for --agda)"
    , [Target]
haskellTargets )
  , ( String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option []    [String
"string-token"] ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o { tokenText = StringToken }))
          String
"Use String in Alex lexer (default)"
    , [Target]
haskellTargets )
  , ( String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option []    [String
"glr"] ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o {glr = GLR}))
          String
"Output Happy GLR parser [deprecated]"
    , [Target]
haskellTargets )
  , ( String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option []    [String
"functor"] ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o {functor = True}))
          String
"Make the AST a functor and use it to store the position of the nodes"
    , [Target]
haskellTargets )
  , ( String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option []    [String
"generic"] ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o {generic = True}))
          String
"Derive Data, Generic, and Typeable instances for AST types"
    , [Target]
haskellTargets )
  , ( String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option []    [String
"xml"] ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o {xml = 1}))
          String
"Also generate a DTD and an XML printer"
    , [Target]
haskellTargets )
  , ( String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option []    [String
"xmlt"] ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o {xml = 2}))
          String
"DTD and an XML printer, another encoding"
    , [Target]
haskellTargets )
  -- Agda does not support the GADT syntax
  , ( String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option []    [String
"agda"] ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\SharedOptions
o -> SharedOptions
o { agda = True, tokenText = TextToken }))
          String
"Also generate Agda bindings for the abstract syntax"
    , [Target
TargetHaskell] )
  ]

-- | The list of specific options for a target.
specificOptions' :: Target -> [OptDescr (SharedOptions -> SharedOptions)]
specificOptions' :: Target -> [OptDescr (SharedOptions -> SharedOptions)]
specificOptions' Target
t = ((OptDescr (SharedOptions -> SharedOptions), [Target])
 -> OptDescr (SharedOptions -> SharedOptions))
-> [(OptDescr (SharedOptions -> SharedOptions), [Target])]
-> [OptDescr (SharedOptions -> SharedOptions)]
forall a b. (a -> b) -> [a] -> [b]
map (OptDescr (SharedOptions -> SharedOptions), [Target])
-> OptDescr (SharedOptions -> SharedOptions)
forall a b. (a, b) -> a
fst ([(OptDescr (SharedOptions -> SharedOptions), [Target])]
 -> [OptDescr (SharedOptions -> SharedOptions)])
-> [(OptDescr (SharedOptions -> SharedOptions), [Target])]
-> [OptDescr (SharedOptions -> SharedOptions)]
forall a b. (a -> b) -> a -> b
$ ((OptDescr (SharedOptions -> SharedOptions), [Target]) -> Bool)
-> [(OptDescr (SharedOptions -> SharedOptions), [Target])]
-> [(OptDescr (SharedOptions -> SharedOptions), [Target])]
forall a. (a -> Bool) -> [a] -> [a]
filter (Target -> [Target] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem Target
t ([Target] -> Bool)
-> ((OptDescr (SharedOptions -> SharedOptions), [Target])
    -> [Target])
-> (OptDescr (SharedOptions -> SharedOptions), [Target])
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (OptDescr (SharedOptions -> SharedOptions), [Target]) -> [Target]
forall a b. (a, b) -> b
snd) [(OptDescr (SharedOptions -> SharedOptions), [Target])]
specificOptions

commonOptions :: [OptDescr (SharedOptions -> SharedOptions)]
commonOptions :: [OptDescr (SharedOptions -> SharedOptions)]
commonOptions =
  [ String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option String
"m" [String
"makefile"] ((Maybe String -> SharedOptions -> SharedOptions)
-> String -> ArgDescr (SharedOptions -> SharedOptions)
forall a. (Maybe String -> a) -> String -> ArgDescr a
OptArg (String -> SharedOptions -> SharedOptions
setMakefile (String -> SharedOptions -> SharedOptions)
-> (Maybe String -> String)
-> Maybe String
-> SharedOptions
-> SharedOptions
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe String
"Makefile") String
"MAKEFILE")
      String
"generate Makefile"
  , String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option String
"o" [String
"outputdir"] ((String -> SharedOptions -> SharedOptions)
-> String -> ArgDescr (SharedOptions -> SharedOptions)
forall a. (String -> a) -> String -> ArgDescr a
ReqArg (\String
n SharedOptions
o -> SharedOptions
o {outDir = n}) String
"DIR")
      String
"Redirects all generated files into DIR"
  , String
-> UsageWarnings
-> ArgDescr (SharedOptions -> SharedOptions)
-> String
-> OptDescr (SharedOptions -> SharedOptions)
forall a.
String -> UsageWarnings -> ArgDescr a -> String -> OptDescr a
Option String
""  [String
"force"]     ((SharedOptions -> SharedOptions)
-> ArgDescr (SharedOptions -> SharedOptions)
forall a. a -> ArgDescr a
NoArg (\ SharedOptions
o -> SharedOptions
o { force = True }))
      String
"Ignore errors in the grammar (may produce ill-formed output or crash)"
  ]
  where setMakefile :: String -> SharedOptions -> SharedOptions
setMakefile String
mf SharedOptions
o = SharedOptions
o { optMake = Just mf }

allOptions :: [OptDescr (SharedOptions -> SharedOptions)]
allOptions :: [OptDescr (SharedOptions -> SharedOptions)]
allOptions = [OptDescr (SharedOptions -> SharedOptions)]
targetOptions [OptDescr (SharedOptions -> SharedOptions)]
-> [OptDescr (SharedOptions -> SharedOptions)]
-> [OptDescr (SharedOptions -> SharedOptions)]
forall a. [a] -> [a] -> [a]
++ [OptDescr (SharedOptions -> SharedOptions)]
commonOptions [OptDescr (SharedOptions -> SharedOptions)]
-> [OptDescr (SharedOptions -> SharedOptions)]
-> [OptDescr (SharedOptions -> SharedOptions)]
forall a. [a] -> [a] -> [a]
++ ((OptDescr (SharedOptions -> SharedOptions), [Target])
 -> OptDescr (SharedOptions -> SharedOptions))
-> [(OptDescr (SharedOptions -> SharedOptions), [Target])]
-> [OptDescr (SharedOptions -> SharedOptions)]
forall a b. (a -> b) -> [a] -> [b]
map (OptDescr (SharedOptions -> SharedOptions), [Target])
-> OptDescr (SharedOptions -> SharedOptions)
forall a b. (a, b) -> a
fst [(OptDescr (SharedOptions -> SharedOptions), [Target])]
specificOptions

-- | All target options and all specific options for a given target.
allOptions' :: Target -> [OptDescr (SharedOptions -> SharedOptions)]
allOptions' :: Target -> [OptDescr (SharedOptions -> SharedOptions)]
allOptions' Target
t = [OptDescr (SharedOptions -> SharedOptions)]
targetOptions [OptDescr (SharedOptions -> SharedOptions)]
-> [OptDescr (SharedOptions -> SharedOptions)]
-> [OptDescr (SharedOptions -> SharedOptions)]
forall a. [a] -> [a] -> [a]
++ [OptDescr (SharedOptions -> SharedOptions)]
commonOptions [OptDescr (SharedOptions -> SharedOptions)]
-> [OptDescr (SharedOptions -> SharedOptions)]
-> [OptDescr (SharedOptions -> SharedOptions)]
forall a. [a] -> [a] -> [a]
++ Target -> [OptDescr (SharedOptions -> SharedOptions)]
specificOptions' Target
t

-- ~~~ Help strings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

versionString :: String
versionString :: String
versionString = Version -> String
showVersion Version
version

title :: [String]
title :: UsageWarnings
title =
  [ String
"The BNF Converter, " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
versionString String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" (c) 2002-today BNFC development team."
  , String
"Free software under the BSD 3-clause license."
  , String
"List of recent contributors at https://github.com/BNFC/bnfc/graphs/contributors."
  , String
"Report bugs at https://github.com/BNFC/bnfc/issues."
  , String
""
  ]

-- oldContributors :: [String]
-- oldContributors =
--   [ "(c) Jonas Almström Duregård, Krasimir Angelov, Jean-Philippe Bernardy, Björn Bringert, Johan Broberg, Paul Callaghan, "
--   , "    Grégoire Détrez, Markus Forsberg, Ola Frid, Peter Gammie, Thomas Hallgren, Patrik Jansson, "
--   , "    Kristofer Johannisson, Antti-Juhani Kaijanaho, Ulf Norell, "
--   , "    Michael Pellauer and Aarne Ranta 2002 - 2013."
--   ]

usage :: String
usage :: String
usage = UsageWarnings -> String
unlines
  [ String
"usage: bnfc [--TARGET] [OPTIONS] LANG.cf"
  , String
"   or: bnfc --[numeric-]version"
  , String
"   or: bnfc [--license]"
  , String
"   or: bnfc [--help]"
  ]

help :: String
help :: String
help = UsageWarnings -> String
unlines (UsageWarnings -> String) -> UsageWarnings -> String
forall a b. (a -> b) -> a -> b
$ UsageWarnings
title UsageWarnings -> UsageWarnings -> UsageWarnings
forall a. [a] -> [a] -> [a]
++
    [ String
usage
    , String -> [OptDescr Mode] -> String
forall a. String -> [OptDescr a] -> String
usageInfo String
"Global options"   [OptDescr Mode]
globalOptions
    , String -> [OptDescr (SharedOptions -> SharedOptions)] -> String
forall a. String -> [OptDescr a] -> String
usageInfo String
"Common options"   [OptDescr (SharedOptions -> SharedOptions)]
commonOptions
    , String -> [OptDescr (SharedOptions -> SharedOptions)] -> String
forall a. String -> [OptDescr a] -> String
usageInfo String
"TARGET languages" [OptDescr (SharedOptions -> SharedOptions)]
targetOptions
    ] UsageWarnings -> UsageWarnings -> UsageWarnings
forall a. [a] -> [a] -> [a]
++ (Target -> String) -> [Target] -> UsageWarnings
forall a b. (a -> b) -> [a] -> [b]
map Target -> String
targetUsage [Target]
helpTargets
  where
  helpTargets :: [Target]
helpTargets = [ Target
TargetHaskell, Target
TargetJava, Target
TargetC, Target
TargetCpp ]
  targetUsage :: Target -> String
targetUsage Target
t = String -> [OptDescr (SharedOptions -> SharedOptions)] -> String
forall a. String -> [OptDescr a] -> String
usageInfo
    (String -> ShowS
forall r. PrintfType r => String -> r
printf String
"Special options for the %s backend" (Target -> String
forall a. Show a => a -> String
show Target
t))
    (Target -> [OptDescr (SharedOptions -> SharedOptions)]
specificOptions' Target
t)

-- ~~~ Parsing machinery ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-- | Main parsing function
parseMode :: [String] -> (Mode, UsageWarnings)
parseMode :: UsageWarnings -> (Mode, UsageWarnings)
parseMode UsageWarnings
args =
  case WriterT UsageWarnings (Either String) Mode
-> Either String (Mode, UsageWarnings)
forall w (m :: * -> *) a. WriterT w m a -> m (a, w)
runWriterT (WriterT UsageWarnings (Either String) Mode
 -> Either String (Mode, UsageWarnings))
-> WriterT UsageWarnings (Either String) Mode
-> Either String (Mode, UsageWarnings)
forall a b. (a -> b) -> a -> b
$ UsageWarnings -> WriterT UsageWarnings (Either String) Mode
parseMode' (UsageWarnings -> WriterT UsageWarnings (Either String) Mode)
-> WriterT UsageWarnings (Either String) UsageWarnings
-> WriterT UsageWarnings (Either String) Mode
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< UsageWarnings
-> WriterT UsageWarnings (Either String) UsageWarnings
translateOldOptions UsageWarnings
args of
    Left String
err  -> (String -> Mode
UsageError String
err, [])
    Right (Mode, UsageWarnings)
res -> (Mode, UsageWarnings)
res

type ParseOpt = WriterT UsageWarnings (Either String)
type UsageWarnings = [String]

instance {-# OVERLAPPING #-} Semigroup (ParseOpt ()) where <> :: WriterT UsageWarnings (Either String) ()
-> WriterT UsageWarnings (Either String) ()
-> WriterT UsageWarnings (Either String) ()
(<>)   = WriterT UsageWarnings (Either String) ()
-> WriterT UsageWarnings (Either String) ()
-> WriterT UsageWarnings (Either String) ()
forall a b.
WriterT UsageWarnings (Either String) a
-> WriterT UsageWarnings (Either String) b
-> WriterT UsageWarnings (Either String) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
(>>)
instance {-# OVERLAPPING #-} Monoid    (ParseOpt ()) where mempty :: WriterT UsageWarnings (Either String) ()
mempty = () -> WriterT UsageWarnings (Either String) ()
forall a. a -> WriterT UsageWarnings (Either String) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (); mappend :: WriterT UsageWarnings (Either String) ()
-> WriterT UsageWarnings (Either String) ()
-> WriterT UsageWarnings (Either String) ()
mappend = WriterT UsageWarnings (Either String) ()
-> WriterT UsageWarnings (Either String) ()
-> WriterT UsageWarnings (Either String) ()
forall a. Semigroup a => a -> a -> a
(<>)

parseMode' :: [String] -> ParseOpt Mode
parseMode' :: UsageWarnings -> WriterT UsageWarnings (Either String) Mode
parseMode' []   = Mode -> WriterT UsageWarnings (Either String) Mode
forall a. a -> WriterT UsageWarnings (Either String) a
forall (m :: * -> *) a. Monad m => a -> m a
return Mode
Help
parseMode' UsageWarnings
args =
  -- First, check for global options like --help or --version
  case ArgOrder Mode
-> [OptDescr Mode]
-> UsageWarnings
-> ([Mode], UsageWarnings, UsageWarnings, UsageWarnings)
forall a.
ArgOrder a
-> [OptDescr a]
-> UsageWarnings
-> ([a], UsageWarnings, UsageWarnings, UsageWarnings)
getOpt' ArgOrder Mode
forall a. ArgOrder a
Permute [OptDescr Mode]
globalOptions UsageWarnings
args of
   (Mode
mode:[Mode]
_,UsageWarnings
_,UsageWarnings
_,UsageWarnings
_) -> Mode -> WriterT UsageWarnings (Either String) Mode
forall a. a -> WriterT UsageWarnings (Either String) a
forall (m :: * -> *) a. Monad m => a -> m a
return Mode
mode

   -- Then, check for unrecognized options.
   ([Mode], UsageWarnings, UsageWarnings, UsageWarnings)
_ -> do
    let ([SharedOptions -> SharedOptions]
_, UsageWarnings
_, UsageWarnings
unknown, UsageWarnings
_) = ArgOrder (SharedOptions -> SharedOptions)
-> [OptDescr (SharedOptions -> SharedOptions)]
-> UsageWarnings
-> ([SharedOptions -> SharedOptions], UsageWarnings, UsageWarnings,
    UsageWarnings)
forall a.
ArgOrder a
-> [OptDescr a]
-> UsageWarnings
-> ([a], UsageWarnings, UsageWarnings, UsageWarnings)
getOpt' ArgOrder (SharedOptions -> SharedOptions)
forall a. ArgOrder a
Permute [OptDescr (SharedOptions -> SharedOptions)]
allOptions UsageWarnings
args
    UsageWarnings -> WriterT UsageWarnings (Either String) ()
processUnknownOptions UsageWarnings
unknown

    -- Then, determine target language.
    case ArgOrder (SharedOptions -> SharedOptions)
-> [OptDescr (SharedOptions -> SharedOptions)]
-> UsageWarnings
-> ([SharedOptions -> SharedOptions], UsageWarnings, UsageWarnings,
    UsageWarnings)
forall a.
ArgOrder a
-> [OptDescr a]
-> UsageWarnings
-> ([a], UsageWarnings, UsageWarnings, UsageWarnings)
getOpt' ArgOrder (SharedOptions -> SharedOptions)
forall a. ArgOrder a
Permute [OptDescr (SharedOptions -> SharedOptions)]
targetOptions UsageWarnings
args of
      -- ([]     ,_,_,_) -> usageError "No target selected"  -- --haskell is default target
      (SharedOptions -> SharedOptions
_:SharedOptions -> SharedOptions
_:[SharedOptions -> SharedOptions]
_,UsageWarnings
_,UsageWarnings
_,UsageWarnings
_) -> String -> WriterT UsageWarnings (Either String) Mode
usageError String
"At most one target is allowed"

      -- Finally, parse options with known target.
      ([SharedOptions -> SharedOptions]
optionUpdates,UsageWarnings
_,UsageWarnings
_,UsageWarnings
_) -> do
        let tgt :: Target
tgt = SharedOptions -> Target
target ([SharedOptions -> SharedOptions] -> SharedOptions
forall {t :: * -> *}.
Foldable t =>
t (SharedOptions -> SharedOptions) -> SharedOptions
options [SharedOptions -> SharedOptions]
optionUpdates)
        case ArgOrder (SharedOptions -> SharedOptions)
-> [OptDescr (SharedOptions -> SharedOptions)]
-> UsageWarnings
-> ([SharedOptions -> SharedOptions], UsageWarnings, UsageWarnings,
    UsageWarnings)
forall a.
ArgOrder a
-> [OptDescr a]
-> UsageWarnings
-> ([a], UsageWarnings, UsageWarnings, UsageWarnings)
getOpt' ArgOrder (SharedOptions -> SharedOptions)
forall a. ArgOrder a
Permute (Target -> [OptDescr (SharedOptions -> SharedOptions)]
allOptions' Target
tgt) UsageWarnings
args of
          ([SharedOptions -> SharedOptions]
_,  UsageWarnings
_, UsageWarnings
_,      String
e:UsageWarnings
_) -> String -> WriterT UsageWarnings (Either String) Mode
usageError String
e
          ([SharedOptions -> SharedOptions]
_,  UsageWarnings
_, [String
u],      UsageWarnings
_) -> String -> WriterT UsageWarnings (Either String) Mode
usageError (String -> WriterT UsageWarnings (Either String) Mode)
-> String -> WriterT UsageWarnings (Either String) Mode
forall a b. (a -> b) -> a -> b
$ UsageWarnings -> String
unwords (UsageWarnings -> String) -> UsageWarnings -> String
forall a b. (a -> b) -> a -> b
$ [ String
"Backend", Target -> String
forall a. Show a => a -> String
show Target
tgt, String
"does not support option", String
u ]
          ([SharedOptions -> SharedOptions]
_,  UsageWarnings
_, us :: UsageWarnings
us@(String
_:UsageWarnings
_), UsageWarnings
_) -> String -> WriterT UsageWarnings (Either String) Mode
usageError (String -> WriterT UsageWarnings (Either String) Mode)
-> String -> WriterT UsageWarnings (Either String) Mode
forall a b. (a -> b) -> a -> b
$ UsageWarnings -> String
unwords (UsageWarnings -> String) -> UsageWarnings -> String
forall a b. (a -> b) -> a -> b
$ [ String
"Backend", Target -> String
forall a. Show a => a -> String
show Target
tgt, String
"does not support options" ] UsageWarnings -> UsageWarnings -> UsageWarnings
forall a. [a] -> [a] -> [a]
++ UsageWarnings
us
          ([SharedOptions -> SharedOptions]
_, [], UsageWarnings
_,        UsageWarnings
_) -> String -> WriterT UsageWarnings (Either String) Mode
usageError String
"Missing grammar file"
          ([SharedOptions -> SharedOptions]
optionsUpdates, [String
grammarFile], [], []) -> do
            let opts :: SharedOptions
opts = ([SharedOptions -> SharedOptions] -> SharedOptions
forall {t :: * -> *}.
Foldable t =>
t (SharedOptions -> SharedOptions) -> SharedOptions
options [SharedOptions -> SharedOptions]
optionsUpdates)
                       { lbnfFile = grammarFile
                       , lang = takeBaseName grammarFile
                       }
            Target -> WriterT UsageWarnings (Either String) ()
forall a.
Maintained a =>
a -> WriterT UsageWarnings (Either String) ()
warnDeprecatedBackend Target
tgt
            SharedOptions -> WriterT UsageWarnings (Either String) ()
warnDeprecatedOptions SharedOptions
opts
            Mode -> WriterT UsageWarnings (Either String) Mode
forall a. a -> WriterT UsageWarnings (Either String) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Mode -> WriterT UsageWarnings (Either String) Mode)
-> Mode -> WriterT UsageWarnings (Either String) Mode
forall a b. (a -> b) -> a -> b
$ SharedOptions -> String -> Mode
Target SharedOptions
opts String
grammarFile
          ([SharedOptions -> SharedOptions]
_,  UsageWarnings
_, UsageWarnings
_,        UsageWarnings
_) -> String -> WriterT UsageWarnings (Either String) Mode
usageError String
"Too many arguments"
  where
  options :: t (SharedOptions -> SharedOptions) -> SharedOptions
options t (SharedOptions -> SharedOptions)
optionsUpdates = ((SharedOptions -> SharedOptions)
 -> (SharedOptions -> SharedOptions)
 -> SharedOptions
 -> SharedOptions)
-> (SharedOptions -> SharedOptions)
-> t (SharedOptions -> SharedOptions)
-> SharedOptions
-> SharedOptions
forall b a. (b -> a -> b) -> b -> t a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (SharedOptions -> SharedOptions)
-> (SharedOptions -> SharedOptions)
-> SharedOptions
-> SharedOptions
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) SharedOptions -> SharedOptions
forall a. a -> a
id t (SharedOptions -> SharedOptions)
optionsUpdates SharedOptions
defaultOptions
  usageError :: String -> WriterT UsageWarnings (Either String) Mode
usageError = Mode -> WriterT UsageWarnings (Either String) Mode
forall a. a -> WriterT UsageWarnings (Either String) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Mode -> WriterT UsageWarnings (Either String) Mode)
-> (String -> Mode)
-> String
-> WriterT UsageWarnings (Either String) Mode
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Mode
UsageError


-- * Deprecation

class Maintained a where
  maintained   :: a -> Bool
  printFeature :: a -> String

instance Maintained Target where
  printFeature :: Target -> String
printFeature = Target -> String
printTargetOption
  maintained :: Target -> Bool
maintained = \case
    Target
TargetC           -> Bool
True
    Target
TargetCpp         -> Bool
True
    Target
TargetCppNoStl    -> Bool
True
    Target
TargetHaskell     -> Bool
True
    Target
TargetHaskellGadt -> Bool
True
    Target
TargetLatex       -> Bool
True
    Target
TargetJava        -> Bool
True
    Target
TargetOCaml       -> Bool
True
    Target
TargetPygments    -> Bool
True
    Target
TargetCheck       -> Bool
True

instance Maintained AlexVersion where
  printFeature :: AlexVersion -> String
printFeature = AlexVersion -> String
printAlexOption
  maintained :: AlexVersion -> Bool
maintained = \case
    AlexVersion
Alex3 -> Bool
True

instance Maintained HappyMode where
  printFeature :: HappyMode -> String
printFeature = \case
    HappyMode
Standard -> String
forall a. HasCallStack => a
undefined
    HappyMode
GLR      -> String
"--glr"
  maintained :: HappyMode -> Bool
maintained = \case
    HappyMode
Standard -> Bool
True
    HappyMode
GLR      -> Bool
False

warnDeprecatedBackend :: Maintained a => a -> ParseOpt ()
warnDeprecatedBackend :: forall a.
Maintained a =>
a -> WriterT UsageWarnings (Either String) ()
warnDeprecatedBackend a
backend =
  Bool
-> WriterT UsageWarnings (Either String) ()
-> WriterT UsageWarnings (Either String) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
Ctrl.unless (a -> Bool
forall a. Maintained a => a -> Bool
maintained a
backend) (WriterT UsageWarnings (Either String) ()
 -> WriterT UsageWarnings (Either String) ())
-> WriterT UsageWarnings (Either String) ()
-> WriterT UsageWarnings (Either String) ()
forall a b. (a -> b) -> a -> b
$ String -> WriterT UsageWarnings (Either String) ()
warnDeprecated (String -> WriterT UsageWarnings (Either String) ())
-> String -> WriterT UsageWarnings (Either String) ()
forall a b. (a -> b) -> a -> b
$ UsageWarnings -> String
unwords [ String
"backend", a -> String
forall a. Maintained a => a -> String
printFeature a
backend ]

warnDeprecated :: String -> ParseOpt ()
warnDeprecated :: String -> WriterT UsageWarnings (Either String) ()
warnDeprecated String
feature =
  UsageWarnings -> WriterT UsageWarnings (Either String) ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell
    [ UsageWarnings -> String
unwords [ String
"Warning:", String
feature, String
"is deprecated and no longer maintained." ]
    -- , "Should it be broken, try an older version of BNFC."
    ]

warnDeprecatedOptions :: SharedOptions -> ParseOpt ()
warnDeprecatedOptions :: SharedOptions -> WriterT UsageWarnings (Either String) ()
warnDeprecatedOptions Options{Bool
Int
String
Maybe String
TokenText
Ansi
RecordPositions
JavaLexerParser
OCamlParser
HappyMode
AlexVersion
Target
lbnfFile :: SharedOptions -> String
lang :: SharedOptions -> String
outDir :: SharedOptions -> String
force :: SharedOptions -> Bool
target :: SharedOptions -> Target
optMake :: SharedOptions -> Maybe String
inPackage :: SharedOptions -> Maybe String
linenumbers :: SharedOptions -> RecordPositions
ansi :: SharedOptions -> Ansi
inDir :: SharedOptions -> Bool
functor :: SharedOptions -> Bool
generic :: SharedOptions -> Bool
alexMode :: SharedOptions -> AlexVersion
tokenText :: SharedOptions -> TokenText
glr :: SharedOptions -> HappyMode
xml :: SharedOptions -> Int
agda :: SharedOptions -> Bool
ocamlParser :: SharedOptions -> OCamlParser
javaLexerParser :: SharedOptions -> JavaLexerParser
visualStudio :: SharedOptions -> Bool
wcf :: SharedOptions -> Bool
lbnfFile :: String
lang :: String
outDir :: String
force :: Bool
target :: Target
optMake :: Maybe String
inPackage :: Maybe String
linenumbers :: RecordPositions
ansi :: Ansi
inDir :: Bool
functor :: Bool
generic :: Bool
alexMode :: AlexVersion
tokenText :: TokenText
glr :: HappyMode
xml :: Int
agda :: Bool
ocamlParser :: OCamlParser
javaLexerParser :: JavaLexerParser
visualStudio :: Bool
wcf :: Bool
..} = do
  AlexVersion -> WriterT UsageWarnings (Either String) ()
forall a.
Maintained a =>
a -> WriterT UsageWarnings (Either String) ()
warnDeprecatedBackend AlexVersion
alexMode
  HappyMode -> WriterT UsageWarnings (Either String) ()
forall a.
Maintained a =>
a -> WriterT UsageWarnings (Either String) ()
warnDeprecatedBackend HappyMode
glr

-- * Backward compatibility

-- | Produce a warning for former options that are now obsolete.
--   Throw an error for properly unknown options.
--
--   Note: this only works properly for former options that had no arguments.
processUnknownOptions :: [String] -> ParseOpt ()
processUnknownOptions :: UsageWarnings -> WriterT UsageWarnings (Either String) ()
processUnknownOptions UsageWarnings
os = do

  -- Classify unknown options.
  let cl :: [Either
   (Either (String, UnknownOption) (String, RemovedOption))
   (String, ObsoleteOption)]
cl = (String
 -> Either
      (Either (String, UnknownOption) (String, RemovedOption))
      (String, ObsoleteOption))
-> UsageWarnings
-> [Either
      (Either (String, UnknownOption) (String, RemovedOption))
      (String, ObsoleteOption)]
forall a b. (a -> b) -> [a] -> [b]
map (\ String
o -> (Either UnknownOption RemovedOption
 -> Either (String, UnknownOption) (String, RemovedOption))
-> (ObsoleteOption -> (String, ObsoleteOption))
-> Either (Either UnknownOption RemovedOption) ObsoleteOption
-> Either
     (Either (String, UnknownOption) (String, RemovedOption))
     (String, ObsoleteOption)
forall a b c d. (a -> b) -> (c -> d) -> Either a c -> Either b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap ((UnknownOption -> (String, UnknownOption))
-> (RemovedOption -> (String, RemovedOption))
-> Either UnknownOption RemovedOption
-> Either (String, UnknownOption) (String, RemovedOption)
forall a b c d. (a -> b) -> (c -> d) -> Either a c -> Either b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (String
o,) (String
o,)) (String
o,) (Either (Either UnknownOption RemovedOption) ObsoleteOption
 -> Either
      (Either (String, UnknownOption) (String, RemovedOption))
      (String, ObsoleteOption))
-> Either (Either UnknownOption RemovedOption) ObsoleteOption
-> Either
     (Either (String, UnknownOption) (String, RemovedOption))
     (String, ObsoleteOption)
forall a b. (a -> b) -> a -> b
$ String
-> Either (Either UnknownOption RemovedOption) ObsoleteOption
classifyUnknownOption String
o) UsageWarnings
os
  let ([Either (String, UnknownOption) (String, RemovedOption)]
errs, [(String, ObsoleteOption)]
obsolete) = [Either
   (Either (String, UnknownOption) (String, RemovedOption))
   (String, ObsoleteOption)]
-> ([Either (String, UnknownOption) (String, RemovedOption)],
    [(String, ObsoleteOption)])
forall a b. [Either a b] -> ([a], [b])
partitionEithers [Either
   (Either (String, UnknownOption) (String, RemovedOption))
   (String, ObsoleteOption)]
cl

  -- Print warnings about obsolete options.
  case ((String, ObsoleteOption) -> String)
-> [(String, ObsoleteOption)] -> UsageWarnings
forall a b. (a -> b) -> [a] -> [b]
map (\ (String
o, ObsoleteOption
ObsoleteOption) -> String
o) [(String, ObsoleteOption)]
obsolete of
    []       -> () -> WriterT UsageWarnings (Either String) ()
forall a. a -> WriterT UsageWarnings (Either String) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    os :: UsageWarnings
os@[String
_]   -> UsageWarnings -> WriterT UsageWarnings (Either String) ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [ UsageWarnings -> String
unwords (UsageWarnings -> String) -> UsageWarnings -> String
forall a b. (a -> b) -> a -> b
$ String
"Warning: ignoring obsolete option:"  String -> UsageWarnings -> UsageWarnings
forall a. a -> [a] -> [a]
: UsageWarnings
os ]
    os :: UsageWarnings
os@(String
_:UsageWarnings
_) -> UsageWarnings -> WriterT UsageWarnings (Either String) ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [ UsageWarnings -> String
unwords (UsageWarnings -> String) -> UsageWarnings -> String
forall a b. (a -> b) -> a -> b
$ String
"Warning: ignoring obsolete options:" String -> UsageWarnings -> UsageWarnings
forall a. a -> [a] -> [a]
: UsageWarnings
os ]

  -- Throw errors.
  Bool
-> WriterT UsageWarnings (Either String) ()
-> WriterT UsageWarnings (Either String) ()
forall m. Monoid m => Bool -> m -> m
unless ([Either (String, UnknownOption) (String, RemovedOption)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Either (String, UnknownOption) (String, RemovedOption)]
errs) (WriterT UsageWarnings (Either String) ()
 -> WriterT UsageWarnings (Either String) ())
-> WriterT UsageWarnings (Either String) ()
-> WriterT UsageWarnings (Either String) ()
forall a b. (a -> b) -> a -> b
$ do
    let ([(String, UnknownOption)]
unknown, [(String, RemovedOption)]
removed) = [Either (String, UnknownOption) (String, RemovedOption)]
-> ([(String, UnknownOption)], [(String, RemovedOption)])
forall a b. [Either a b] -> ([a], [b])
partitionEithers [Either (String, UnknownOption) (String, RemovedOption)]
errs
    String -> WriterT UsageWarnings (Either String) ()
forall a. String -> WriterT UsageWarnings (Either String) a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (String -> WriterT UsageWarnings (Either String) ())
-> String -> WriterT UsageWarnings (Either String) ()
forall a b. (a -> b) -> a -> b
$ UsageWarnings -> String
unlines (UsageWarnings -> String) -> UsageWarnings -> String
forall a b. (a -> b) -> a -> b
$ [UsageWarnings] -> UsageWarnings
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
      [ [ String
"Option error(s):" ]
      , case ((String, UnknownOption) -> String)
-> [(String, UnknownOption)] -> UsageWarnings
forall a b. (a -> b) -> [a] -> [b]
map (\ (String
o, UnknownOption
UnknownOption) -> String
o) [(String, UnknownOption)]
unknown of
          []       -> []
          us :: UsageWarnings
us@[String
_]   -> [ UsageWarnings -> String
unwords (UsageWarnings -> String) -> UsageWarnings -> String
forall a b. (a -> b) -> a -> b
$ String
"Unrecognized option:"  String -> UsageWarnings -> UsageWarnings
forall a. a -> [a] -> [a]
: UsageWarnings
us ]
          us :: UsageWarnings
us@(String
_:UsageWarnings
_) -> [ UsageWarnings -> String
unwords (UsageWarnings -> String) -> UsageWarnings -> String
forall a b. (a -> b) -> a -> b
$ String
"Unrecognized options:" String -> UsageWarnings -> UsageWarnings
forall a. a -> [a] -> [a]
: UsageWarnings
us ]
      , ((String, RemovedOption) -> String)
-> [(String, RemovedOption)] -> UsageWarnings
forall a b. (a -> b) -> [a] -> [b]
map (\ (String
o, RemovedOption String
msg) -> UsageWarnings -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ String
o, String
": ", String
msg ]) [(String, RemovedOption)]
removed
      ]

-- | Option has never been known.
data UnknownOption  = UnknownOption

-- | Option is obsolete, print warning and continue.
data ObsoleteOption = ObsoleteOption

-- | Error: Option has been removed, throw error with given message.
newtype RemovedOption = RemovedOption String

classifyUnknownOption :: String -> Either (Either UnknownOption RemovedOption) ObsoleteOption
classifyUnknownOption :: String
-> Either (Either UnknownOption RemovedOption) ObsoleteOption
classifyUnknownOption = \case
  String
"--alex1" -> String
-> Either (Either UnknownOption RemovedOption) ObsoleteOption
forall {a} {b}. String -> Either (Either a RemovedOption) b
supportRemovedIn290 (String
 -> Either (Either UnknownOption RemovedOption) ObsoleteOption)
-> String
-> Either (Either UnknownOption RemovedOption) ObsoleteOption
forall a b. (a -> b) -> a -> b
$ String
"Alex version 1"
  String
"--alex2" -> String
-> Either (Either UnknownOption RemovedOption) ObsoleteOption
forall {a} {b}. String -> Either (Either a RemovedOption) b
supportRemovedIn290 (String
 -> Either (Either UnknownOption RemovedOption) ObsoleteOption)
-> String
-> Either (Either UnknownOption RemovedOption) ObsoleteOption
forall a b. (a -> b) -> a -> b
$ String
"Alex version 2"
  String
"--alex3" -> Either (Either UnknownOption RemovedOption) ObsoleteOption
forall {a}. Either a ObsoleteOption
obsolete
  s :: String
s@String
"--sharestrings" -> String
-> Either (Either UnknownOption RemovedOption) ObsoleteOption
forall {a} {b}. String -> Either (Either a RemovedOption) b
optionRemovedIn290 String
s
  s :: String
s@String
"--cnf" -> String
-> Either (Either UnknownOption RemovedOption) ObsoleteOption
forall {a} {b}. String -> Either (Either a RemovedOption) b
optionRemovedIn290 String
s
  String
"--csharp" -> String
-> Either (Either UnknownOption RemovedOption) ObsoleteOption
forall {a} {b}. String -> Either (Either a RemovedOption) b
supportRemovedIn290 String
"C#"
  String
"--profile" -> String
-> Either (Either UnknownOption RemovedOption) ObsoleteOption
forall {a} {b}. String -> Either (Either a RemovedOption) b
supportRemovedIn290 String
"permutation profiles"
  String
_ -> Either (Either UnknownOption RemovedOption) ObsoleteOption
forall {b} {b}. Either (Either UnknownOption b) b
unknown
  where
  unknown :: Either (Either UnknownOption b) b
unknown  = Either UnknownOption b -> Either (Either UnknownOption b) b
forall a b. a -> Either a b
Left (Either UnknownOption b -> Either (Either UnknownOption b) b)
-> Either UnknownOption b -> Either (Either UnknownOption b) b
forall a b. (a -> b) -> a -> b
$ UnknownOption -> Either UnknownOption b
forall a b. a -> Either a b
Left UnknownOption
UnknownOption
  obsolete :: Either a ObsoleteOption
obsolete = ObsoleteOption -> Either a ObsoleteOption
forall a b. b -> Either a b
Right ObsoleteOption
ObsoleteOption
  removed :: String -> Either (Either a RemovedOption) b
removed  = Either a RemovedOption -> Either (Either a RemovedOption) b
forall a b. a -> Either a b
Left (Either a RemovedOption -> Either (Either a RemovedOption) b)
-> (String -> Either a RemovedOption)
-> String
-> Either (Either a RemovedOption) b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RemovedOption -> Either a RemovedOption
forall a b. b -> Either a b
Right (RemovedOption -> Either a RemovedOption)
-> (String -> RemovedOption) -> String -> Either a RemovedOption
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> RemovedOption
RemovedOption
  supportRemovedIn290 :: String -> Either (Either a RemovedOption) b
supportRemovedIn290 String
feature = String -> Either (Either a RemovedOption) b
forall {a} {b}. String -> Either (Either a RemovedOption) b
removed (String -> Either (Either a RemovedOption) b)
-> String -> Either (Either a RemovedOption) b
forall a b. (a -> b) -> a -> b
$
    UsageWarnings -> String
unwords [ String
"Support for", String
feature, String
removedIn290 ]
  optionRemovedIn290 :: String -> Either (Either a RemovedOption) b
optionRemovedIn290 String
o = String -> Either (Either a RemovedOption) b
forall {a} {b}. String -> Either (Either a RemovedOption) b
removed (String -> Either (Either a RemovedOption) b)
-> String -> Either (Either a RemovedOption) b
forall a b. (a -> b) -> a -> b
$
    UsageWarnings -> String
unwords [ String
"Option", String
o, String
removedIn290 ]

removedIn290 :: String
removedIn290 :: String
removedIn290 = String
"has been removed in version 2.9.0."

-- | A translation function to maintain backward compatibility
--   with the old option syntax.

translateOldOptions :: [String] -> ParseOpt [String]
translateOldOptions :: UsageWarnings
-> WriterT UsageWarnings (Either String) UsageWarnings
translateOldOptions = (String -> WriterT UsageWarnings (Either String) String)
-> UsageWarnings
-> WriterT UsageWarnings (Either String) UsageWarnings
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ((String -> WriterT UsageWarnings (Either String) String)
 -> UsageWarnings
 -> WriterT UsageWarnings (Either String) UsageWarnings)
-> (String -> WriterT UsageWarnings (Either String) String)
-> UsageWarnings
-> WriterT UsageWarnings (Either String) UsageWarnings
forall a b. (a -> b) -> a -> b
$ \ String
o -> do
   case String -> Map String String -> Maybe String
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup String
o Map String String
translation of
     Maybe String
Nothing -> String -> WriterT UsageWarnings (Either String) String
forall a. a -> WriterT UsageWarnings (Either String) a
forall (m :: * -> *) a. Monad m => a -> m a
return String
o
     Just String
o' -> do
       UsageWarnings -> WriterT UsageWarnings (Either String) ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [ UsageWarnings -> String
unwords [ String
"Warning: unrecognized option", String
o, String
"treated as if", String
o', String
"was provided." ] ]
       String -> WriterT UsageWarnings (Either String) String
forall a. a -> WriterT UsageWarnings (Either String) a
forall (m :: * -> *) a. Monad m => a -> m a
return String
o'
  where
  translation :: Map String String
translation = [(String, String)] -> Map String String
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(String, String)] -> Map String String)
-> [(String, String)] -> Map String String
forall a b. (a -> b) -> a -> b
$
    [ (String
"-agda"         , String
"--agda")
    , (String
"-java"         , String
"--java")
    , (String
"-java1.5"      , String
"--java")
    , (String
"-c"            , String
"--c")
    , (String
"-cpp"          , String
"--cpp")
    , (String
"-cpp_stl"      , String
"--cpp")
    , (String
"-cpp_no_stl"   , String
"--cpp-nostl")
    , (String
"-csharp"       , String
"--csharp")
    , (String
"-ocaml"        , String
"--ocaml")
    , (String
"-haskell"      , String
"--haskell")
    , (String
"-prof"         , String
"--profile")
    , (String
"-gadt"         , String
"--haskell-gadt")
    , (String
"-alex1"        , String
"--alex1")
    , (String
"-alex2"        , String
"--alex2")
    , (String
"-alex3"        , String
"--alex3")
    , (String
"-sharestrings" , String
"--sharestrings")
    , (String
"-bytestrings"  , String
"--bytestrings")
    , (String
"-glr"          , String
"--glr")
    , (String
"-xml"          , String
"--xml")
    , (String
"-xmlt"         , String
"--xmlt")
    , (String
"-vs"           , String
"--vs")
    , (String
"-wcf"          , String
"--wcf")
    , (String
"-generic"             , String
"--generic")
    , (String
"--ghc"                , String
"--generic")
    , (String
"--deriveGeneric"      , String
"--generic")
    , (String
"--deriveDataTypeable" , String
"--generic")
    ]