module BNFC.Backend.Java.CFtoCup15 ( cf2Cup ) where
import Data.List (intercalate)
import BNFC.CF
import BNFC.Options (RecordPositions(..))
import BNFC.Utils ( (+++) )
import BNFC.Backend.Common.NamedVariables
import BNFC.Backend.Java.Utils ( getRuleName )
type Rules = [(NonTerminal,[(Pattern,Action)])]
type Pattern = String
type Action = String
type MetaVar = String
cf2Cup :: String -> String -> CF -> RecordPositions -> KeywordEnv -> String
cf2Cup :: String -> String -> CF -> RecordPositions -> KeywordEnv -> String
cf2Cup String
packageBase String
packageAbsyn CF
cf RecordPositions
rp KeywordEnv
env = [String] -> String
unlines
[ String
header
, String -> [Cat] -> String
declarations String
packageAbsyn (CF -> [Cat]
forall f. CFG f -> [Cat]
allParserCats CF
cf)
, KeywordEnv -> String
tokens KeywordEnv
env
, CF -> String
specialToks CF
cf
, CF -> String
specialRules CF
cf
, CF -> String
prEntryPoint CF
cf
, Rules -> String
prRules (String -> CF -> RecordPositions -> KeywordEnv -> Rules
rulesForCup String
packageAbsyn CF
cf RecordPositions
rp KeywordEnv
env)
]
where
header :: String
header :: String
header = [String] -> String
unlines
[ String
"// Parser definition for use with Java Cup"
, String
"package" String -> String -> String
+++ String
packageBase String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
";"
, String
""
, String
"action code {:"
, String
"public java_cup.runtime.ComplexSymbolFactory.Location getLeftLocation("
, String
" java_cup.runtime.ComplexSymbolFactory.Location ... locations) {"
, String
" for (java_cup.runtime.ComplexSymbolFactory.Location l : locations) {"
, String
" if (l != null) {"
, String
" return l;"
, String
" }"
, String
" }"
, String
" return null;"
, String
"}"
, String
":}"
, String
"parser code {:"
, String -> Cat -> String
parseMethod String
packageAbsyn (CF -> Cat
firstEntry CF
cf)
, String
"public void syntax_error(java_cup.runtime.Symbol cur_token)"
, String
"{"
, String
" report_error(\"Syntax Error, trying to recover and continue"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" parse...\", cur_token);"
, String
"}"
, String
""
, String
"public void unrecovered_syntax_error(java_cup.runtime.Symbol "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"cur_token) throws java.lang.Exception"
, String
"{"
, String
" throw new Exception(\"Unrecoverable Syntax Error\");"
, String
"}"
, String
""
, String
":}"
]
prEntryPoint :: CF -> String
prEntryPoint :: CF -> String
prEntryPoint CF
cf = [String] -> String
unlines [String
"", String
"start with " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Cat -> String
identCat (CF -> Cat
firstEntry CF
cf) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
";", String
""]
parseMethod :: String -> Cat -> String
parseMethod :: String -> Cat -> String
parseMethod String
packageAbsyn Cat
cat = [String] -> String
unlines
[ String
" public" String -> String -> String
+++ String
packageAbsyn String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"." String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
dat String -> String -> String
+++ String
"p" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
cat' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"()"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" throws Exception"
, String
" {"
, String
" java_cup.runtime.Symbol res = parse();"
, String
" return (" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
packageAbsyn String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"." String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
dat String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
") res.value;"
, String
" }"
]
where
dat :: String
dat = Cat -> String
identCat (Cat -> Cat
normCat Cat
cat)
cat' :: String
cat' = Cat -> String
identCat Cat
cat
declarations :: String -> [Cat] -> String
declarations :: String -> [Cat] -> String
declarations String
packageAbsyn [Cat]
ns = [String] -> String
unlines ((Cat -> String) -> [Cat] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (String -> Cat -> String
forall p. p -> Cat -> String
typeNT String
packageAbsyn) [Cat]
ns)
where
typeNT :: p -> Cat -> String
typeNT p
_nm Cat
nt = String
"nonterminal" String -> String -> String
+++ String
packageAbsyn String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"."
String -> String -> String
forall a. [a] -> [a] -> [a]
++ Cat -> String
identCat (Cat -> Cat
normCat Cat
nt) String -> String -> String
+++ Cat -> String
identCat Cat
nt String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
";"
tokens :: KeywordEnv -> String
tokens :: KeywordEnv -> String
tokens KeywordEnv
ts = [String] -> String
unlines (((String, String) -> String) -> KeywordEnv -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (String, String) -> String
declTok KeywordEnv
ts)
where
declTok :: (String, String) -> String
declTok (String
s,String
r) = String
"terminal" String -> String -> String
+++ String
r String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"; // " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s
specialToks :: CF -> String
specialToks :: CF -> String
specialToks CF
cf = [String] -> String
unlines
[ String -> String -> String
forall p. IsString p => String -> p -> p
ifC String
catString String
"terminal String _STRING_;"
, String -> String -> String
forall p. IsString p => String -> p -> p
ifC String
catChar String
"terminal Character _CHAR_;"
, String -> String -> String
forall p. IsString p => String -> p -> p
ifC String
catInteger String
"terminal Integer _INTEGER_;"
, String -> String -> String
forall p. IsString p => String -> p -> p
ifC String
catDouble String
"terminal Double _DOUBLE_;"
, String -> String -> String
forall p. IsString p => String -> p -> p
ifC String
catIdent String
"terminal String _IDENT_;"
]
where
ifC :: String -> p -> p
ifC String
cat p
s = if CF -> Cat -> Bool
forall f. CFG f -> Cat -> Bool
isUsedCat CF
cf (String -> Cat
TokenCat String
cat) then p
s else p
""
specialRules:: CF -> String
specialRules :: CF -> String
specialRules CF
cf =
[String] -> String
unlines [String
"terminal String " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
name String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
";" | String
name <- CF -> [String]
forall f. CFG f -> [String]
tokenNames CF
cf]
rulesForCup :: String -> CF -> RecordPositions -> KeywordEnv -> Rules
rulesForCup :: String -> CF -> RecordPositions -> KeywordEnv -> Rules
rulesForCup String
packageAbsyn CF
cf RecordPositions
rp KeywordEnv
env = ((Cat, [Rule]) -> (Cat, KeywordEnv)) -> [(Cat, [Rule])] -> Rules
forall a b. (a -> b) -> [a] -> [b]
map (Cat, [Rule]) -> (Cat, KeywordEnv)
mkOne ([(Cat, [Rule])] -> Rules) -> [(Cat, [Rule])] -> Rules
forall a b. (a -> b) -> a -> b
$ CF -> [(Cat, [Rule])]
ruleGroups CF
cf where
mkOne :: (Cat, [Rule]) -> (Cat, KeywordEnv)
mkOne (Cat
cat,[Rule]
rules) = String
-> CF
-> RecordPositions
-> KeywordEnv
-> [Rule]
-> Cat
-> (Cat, KeywordEnv)
constructRule String
packageAbsyn CF
cf RecordPositions
rp KeywordEnv
env [Rule]
rules Cat
cat
constructRule :: String -> CF -> RecordPositions -> KeywordEnv -> [Rule] -> NonTerminal
-> (NonTerminal,[(Pattern,Action)])
constructRule :: String
-> CF
-> RecordPositions
-> KeywordEnv
-> [Rule]
-> Cat
-> (Cat, KeywordEnv)
constructRule String
packageAbsyn CF
cf RecordPositions
rp KeywordEnv
env [Rule]
rules Cat
nt =
(Cat
nt, [ (String
p, String
-> Cat -> String -> [String] -> Bool -> RecordPositions -> String
generateAction String
packageAbsyn Cat
nt (RFun -> String
forall a. IsFun a => a -> String
funName (RFun -> String) -> RFun -> String
forall a b. (a -> b) -> a -> b
$ Rule -> RFun
forall function. Rul function -> function
funRule Rule
r) (Bool -> [String] -> [String]
forall a. Bool -> [a] -> [a]
revM Bool
b [String]
m) Bool
b RecordPositions
rp)
| Rule
r0 <- [Rule]
rules,
let (Bool
b,Rule
r) = if RFun -> Bool
forall a. IsFun a => a -> Bool
isConsFun (Rule -> RFun
forall function. Rul function -> function
funRule Rule
r0) Bool -> Bool -> Bool
&& Cat -> [Cat] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem (Rule -> Cat
forall fun. Rul fun -> Cat
valCat Rule
r0) [Cat]
revs
then (Bool
True, Rule -> Rule
forall f. Rul f -> Rul f
revSepListRule Rule
r0)
else (Bool
False, Rule
r0)
(String
p,[String]
m) = KeywordEnv -> Rule -> (String, [String])
generatePatterns KeywordEnv
env Rule
r])
where
revM :: Bool -> [a] -> [a]
revM Bool
False = [a] -> [a]
forall a. a -> a
id
revM Bool
True = [a] -> [a]
forall a. [a] -> [a]
reverse
revs :: [Cat]
revs = CF -> [Cat]
forall f. CFG f -> [Cat]
cfgReversibleCats CF
cf
generateAction :: String -> NonTerminal -> Fun -> [MetaVar]
-> Bool
-> RecordPositions
-> Action
generateAction :: String
-> Cat -> String -> [String] -> Bool -> RecordPositions -> String
generateAction String
packageAbsyn Cat
nt String
fun [String]
ms Bool
rev RecordPositions
rp
| String -> Bool
forall a. IsFun a => a -> Bool
isNilFun String
f = String
"RESULT = new " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
c String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"();"
| String -> Bool
forall a. IsFun a => a -> Bool
isOneFun String
f = String
"RESULT = new " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
c String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"(); RESULT.addLast("
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
p_1 String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
");"
| String -> Bool
forall a. IsFun a => a -> Bool
isConsFun String
f = String
"RESULT = " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
p_2 String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"; "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
p_2 String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"." String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
add String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
p_1 String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
");"
| String -> Bool
forall a. IsFun a => a -> Bool
isCoercion String
f = String
"RESULT = " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
p_1 String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
";"
| String -> Bool
forall a. IsFun a => a -> Bool
isDefinedRule String
f = String
"RESULT = " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
packageAbsyn String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"Def." String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
sanitize String
f
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"," [String]
ms String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
");"
| Bool
otherwise = String
"RESULT = new " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
c
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"," [String]
ms String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
");" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
lineInfo
where
sanitize :: String -> String
sanitize = String -> String
getRuleName
f :: String
f = String -> String
forall a. IsFun a => a -> String
funName String
fun
c :: String
c = String
packageAbsyn String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"." String -> String -> String
forall a. [a] -> [a] -> [a]
++
if String -> Bool
forall a. IsFun a => a -> Bool
isNilFun String
f Bool -> Bool -> Bool
|| String -> Bool
forall a. IsFun a => a -> Bool
isOneFun String
f Bool -> Bool -> Bool
|| String -> Bool
forall a. IsFun a => a -> Bool
isConsFun String
f
then Cat -> String
identCat (Cat -> Cat
normCat Cat
nt) else String
f
p_1 :: String
p_1 = [String]
ms [String] -> Int -> String
forall a. [a] -> Int -> a
!! Int
0
p_2 :: String
p_2 = [String]
ms [String] -> Int -> String
forall a. [a] -> Int -> a
!! Int
1
add :: String
add = if Bool
rev then String
"addLast" else String
"addFirst"
lineInfo :: String
lineInfo =
if RecordPositions
rp RecordPositions -> RecordPositions -> Bool
forall a. Eq a => a -> a -> Bool
== RecordPositions
RecordPositions
then case [String]
ms of
[] -> String
"\n((" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
c String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")RESULT).line_num = -1;" String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
"\n((" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
c String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")RESULT).col_num = -1;" String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
"\n((" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
c String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")RESULT).offset = -1;"
[String]
_ -> String
"\njava_cup.runtime.ComplexSymbolFactory.Location leftLoc = getLeftLocation(" String -> String -> String
forall a. [a] -> [a] -> [a]
++
String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"," ((String -> String) -> [String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (String -> String -> String
forall a. [a] -> [a] -> [a]
++String
"xleft") [String]
ms) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
");" String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
"\nif (leftLoc != null) {" String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
"\n ((" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
c String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")RESULT).line_num = leftLoc.getLine();" String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
"\n ((" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
c String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")RESULT).col_num = leftLoc.getColumn();" String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
"\n ((" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
c String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")RESULT).offset = leftLoc.getOffset();" String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
"\n} else {" String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
"\n ((" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
c String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")RESULT).line_num = -1;" String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
"\n ((" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
c String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")RESULT).col_num = -1;" String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
"\n ((" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
c String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")RESULT).offset = -1;" String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
"\n}"
else String
""
generatePatterns :: KeywordEnv -> Rule -> (Pattern,[MetaVar])
generatePatterns :: KeywordEnv -> Rule -> (String, [String])
generatePatterns KeywordEnv
env Rule
r = case Rule -> SentForm
forall function. Rul function -> SentForm
rhsRule Rule
r of
[] -> (String
" /* empty */ ", [])
SentForm
its -> (Int -> SentForm -> String
mkIt Int
1 SentForm
its, SentForm -> [String]
forall a b. [Either a b] -> [String]
metas SentForm
its)
where
mkIt :: Int -> SentForm -> String
mkIt Int
_ [] = []
mkIt Int
n (Either Cat String
i:SentForm
is) =
case Either Cat String
i of
Left Cat
c -> String
c' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
":p_" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show (Int
n :: Int) String -> String -> String
+++ Int -> SentForm -> String
mkIt (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) SentForm
is
where
c' :: String
c' = case Cat
c of
TokenCat "Ident" -> String
"_IDENT_"
TokenCat "Integer" -> String
"_INTEGER_"
TokenCat "Char" -> String
"_CHAR_"
TokenCat "Double" -> String
"_DOUBLE_"
TokenCat "String" -> String
"_STRING_"
Cat
_ -> Cat -> String
identCat Cat
c
Right String
s -> case String -> KeywordEnv -> Maybe String
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
s KeywordEnv
env of
Just String
x -> (String
x String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
":p_" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show (Int
n :: Int)) String -> String -> String
+++ Int -> SentForm -> String
mkIt (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) SentForm
is
Maybe String
Nothing -> Int -> SentForm -> String
mkIt Int
n SentForm
is
metas :: [Either a b] -> [String]
metas [Either a b]
its = [String
"p_" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
i | (Int
i,Left a
_) <- [Int] -> [Either a b] -> [(Int, Either a b)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
1 :: Int ..] [Either a b]
its]
prRules :: Rules -> String
prRules :: Rules -> String
prRules [] = []
prRules ((Cat
_ , [] ) : Rules
rs) = Rules -> String
prRules Rules
rs
prRules ((Cat
nt, (String
p,String
a):KeywordEnv
ls) : Rules
rs) =
[String] -> String
unwords [ String
nt', String
"::=", String
p, String
"{:", String
a, String
":}", Char
'\n' Char -> String -> String
forall a. a -> [a] -> [a]
: KeywordEnv -> String
pr KeywordEnv
ls ] String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
";\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Rules -> String
prRules Rules
rs
where
nt' :: String
nt' = Cat -> String
identCat Cat
nt
pr :: KeywordEnv -> String
pr [] = []
pr ((String
p,String
a):KeywordEnv
ls) = [String] -> String
unlines [ [String] -> String
unwords [ String
" |", String
p, String
"{:", String
a , String
":}" ] ] String -> String -> String
forall a. [a] -> [a] -> [a]
++ KeywordEnv -> String
pr KeywordEnv
ls