module BNFC.Backend.Java.Utils where

import BNFC.CF
import BNFC.Utils ( mkName, NameStyle(..))
import BNFC.Backend.Common.NamedVariables

javaReserved :: [[Char]]
javaReserved =
  [ [Char]
"abstract"
  , [Char]
"assert"
  , [Char]
"boolean"
  , [Char]
"break"
  , [Char]
"byte"
  , [Char]
"case"
  , [Char]
"catch"
  , [Char]
"char"
  , [Char]
"class"
  , [Char]
"const"
  , [Char]
"continue"
  , [Char]
"default"
  , [Char]
"do"
  , [Char]
"double"
  , [Char]
"else"
  , [Char]
"enum"
  , [Char]
"extends"
  , [Char]
"false"         -- there for Java/ANTLR backend
  , [Char]
"final"
  , [Char]
"finally"
  , [Char]
"float"
  , [Char]
"for"
  , [Char]
"goto"
  , [Char]
"if"
  , [Char]
"implements"
  , [Char]
"import"
  , [Char]
"instanceof"
  , [Char]
"int"
  , [Char]
"interface"
  , [Char]
"long"
  , [Char]
"native"
  , [Char]
"new"
  , [Char]
"null"          -- there for Java/ANTLR backend
  -- , "Object"
  , [Char]
"package"
  , [Char]
"private"
  , [Char]
"protected"
  , [Char]
"public"
  , [Char]
"return"
  , [Char]
"short"
  , [Char]
"static"
  , [Char]
"strictfp"
  , [Char]
"super"
  , [Char]
"switch"
  , [Char]
"synchronized"
  , [Char]
"true"          -- there for Java/ANTLR backend
  , [Char]
"this"
  , [Char]
"throw"
  , [Char]
"throws"
  , [Char]
"transient"
  , [Char]
"try"
  , [Char]
"void"
  , [Char]
"volatile"
  , [Char]
"while"
  ]

-- | Append an underscore if there is a clash with a java or ANTLR keyword.
--   E.g. "Grammar" clashes with ANTLR keyword "grammar" since
--   we sometimes need the upper and sometimes the lower case version
--   of "Grammar" in the generated parser.
getRuleName :: String -> String
getRuleName :: [Char] -> [Char]
getRuleName [Char]
z
  | [Char] -> [Char]
firstLowerCase [Char]
z [Char] -> [[Char]] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ([Char]
"grammar" [Char] -> [[Char]] -> [[Char]]
forall a. a -> [a] -> [a]
: [[Char]]
javaReserved) = [Char]
z [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"_"
  | Bool
otherwise = [Char]
z

getLabelName :: Fun -> String
getLabelName :: [Char] -> [Char]
getLabelName = [[Char]] -> NameStyle -> [Char] -> [Char]
mkName [[Char]
"Rule"] NameStyle
CamelCase

getLastInPackage :: String -> String
getLastInPackage :: [Char] -> [Char]
getLastInPackage =
    [[Char]] -> [Char]
forall a. [a] -> a
last ([[Char]] -> [Char]) -> ([Char] -> [[Char]]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
words ([Char] -> [[Char]]) -> ([Char] -> [Char]) -> [Char] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Char) -> [Char] -> [Char]
forall a b. (a -> b) -> [a] -> [b]
map (\Char
c -> if Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'.' then Char
' ' else Char
c)

-- | Make a new entrypoint NT for an existing NT.

startSymbol :: String -> String
startSymbol :: [Char] -> [Char]
startSymbol = ([Char]
"Start_" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++)