{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module BNFC.Backend.Common
( unicodeAndSymbols
, asciiKeywords
, flexEps
, switchByPrecedence
)
where
import Prelude hiding ((<>))
import Data.Char
import BNFC.CF
import BNFC.Utils ( (>.>) )
import BNFC.PrettyPrint
unicodeAndSymbols :: CF -> [String]
unicodeAndSymbols :: CF -> [String]
unicodeAndSymbols CF
cf = forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isAscii) (forall function. CFG function -> [String]
cfgKeywords CF
cf) forall a. [a] -> [a] -> [a]
++ forall function. CFG function -> [String]
cfgSymbols CF
cf
asciiKeywords :: CF -> [String]
asciiKeywords :: CF -> [String]
asciiKeywords = forall a. (a -> Bool) -> [a] -> [a]
filter (forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isAscii) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall function. CFG function -> [String]
cfgKeywords
flexEps :: String
flexEps :: String
flexEps = String
"[^.\\n]?"
switchByPrecedence
:: Doc
-> [(Integer, Doc)]
-> [Doc]
switchByPrecedence :: Doc -> [(Integer, Doc)] -> [Doc]
switchByPrecedence Doc
var = forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> Bool
isEmpty forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) forall a b c. (a -> b) -> (b -> c) -> a -> c
>.> \case
[] -> []
[(Integer
_,Doc
doc)] -> [ Doc
doc ]
[(Integer, Doc)]
ds ->
[ Doc
"switch(" Doc -> Doc -> Doc
<> Doc
var Doc -> Doc -> Doc
<> Doc
")"
, Int -> [Doc] -> Doc
codeblock Int
2
[ Doc
"case" Doc -> Doc -> Doc
<+> Integer -> Doc
integer Integer
i Doc -> Doc -> Doc
<:> Doc
doc Doc -> Doc -> Doc
<+> Doc
"break;" | (Integer
i, Doc
doc) <- [(Integer, Doc)]
ds ]
]
where
Doc
a <:> :: Doc -> Doc -> Doc
<:> Doc
b = Doc
a Doc -> Doc -> Doc
<> Doc
":" Doc -> Doc -> Doc
<+> Doc
b