Skip to content

Commit

Permalink
Update AST classes as simple, use list instead of vector. Fix bison a…
Browse files Browse the repository at this point in the history
…ction of list.
  • Loading branch information
hangingman authored and andreasabel committed Mar 3, 2022
1 parent 9e0eece commit afccbee
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 240 deletions.
9 changes: 3 additions & 6 deletions source/src/BNFC/Backend/C/CFtoBisonC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ header mode cf = unlines $ concat [
, "%defines \"" ++ ("Bison" <.> hExt) ++ "\""
]
, when (beyondAnsi mode)
[ "%define parse.trace"
, "%define api.namespace {" ++ ns ++ "}"
[ "%define api.namespace {" ++ ns ++ "}"
, "/* Specify the namespace for the C++ parser class. */"]
, whenJust (parserPackage mode) $ \ ns ->
[ "%name-prefix = \"" ++ ns ++ "\""
Expand Down Expand Up @@ -596,7 +595,7 @@ generateActionSTLBeyondAnsi rp inPackage nt f b mbs = reverses ++
if | isCoercion f -> concat ["$$ = ", unwords ms, ";", loc]
| isNilFun f -> concat ["$$ = ", "std::make_shared<", scope, nt, ">();"]
| isOneFun f -> concat ["$$ = ", "std::make_shared<", scope, nt, ">(); $$->cons(", head ms, ");"]
| isConsFun f -> concat [lst, "->cons(", el, ");"]
| isConsFun f -> concat [lst, "->cons(", el, "); $$ = ", lst, ";"]
| isDefinedRule f -> concat ["$$ = ", scope, sanitizeCpp (funName f), "(", intercalate ", " ms, ");" ]
| otherwise -> concat ["$$ = ", "std::make_shared<", scope, funName f, ">(", (intercalate ", " ms), ");", loc]
where
Expand All @@ -609,9 +608,7 @@ generateActionSTLBeyondAnsi rp inPackage nt f b mbs = reverses ++
= " $$->line_number = @$.first_line; $$->char_number = @$.first_column;"
| otherwise
= ""
-- TODO: temporary commented reverse()
-- reverses = unwords [m ++"->reverse();" | (m, True) <- mbs]
reverses = unwords ["/*" ++m++ "->reverse(); */" | (m, True) <- mbs]
reverses = unwords [m ++"->reverse();" | (m, True) <- mbs]
scope = nsScope inPackage


Expand Down
9 changes: 0 additions & 9 deletions source/src/BNFC/Backend/CPP/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,8 @@ data CppStdMode
wrapPointerIf :: Bool -> String -> String
wrapPointerIf b v = if b then "*" ++ v else v

wrapUniquePtrIf :: Bool -> String -> String
wrapUniquePtrIf b v = if b then "std::unique_ptr<" ++v++">" else v

wrapUniquePtr :: String -> String
wrapUniquePtr v = "std::unique_ptr<" ++v++">"

wrapSharedPtrIf :: Bool -> String -> String
wrapSharedPtrIf b v = if b then "std::shared_ptr<" ++v++">" else v

wrapSharedPtr :: String -> String
wrapSharedPtr v = "std::shared_ptr<" ++v++">"

wrapMoveIf :: Bool -> String -> String
wrapMoveIf b v = if b then "std::move(" ++v++")" else v
40 changes: 11 additions & 29 deletions source/src/BNFC/Backend/CPP/PrettyPrinter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import BNFC.Utils
import BNFC.Backend.Common
import BNFC.Backend.Common.NamedVariables
import BNFC.Backend.Common.StrUtils (renderCharOrString)
import BNFC.Backend.CPP.Common ( CppStdMode(..), wrapUniquePtr, wrapSharedPtrIf, wrapPointerIf )
import BNFC.Backend.CPP.Common (CppStdMode(..))
import BNFC.Backend.CPP.STL.STLUtils
import BNFC.PrettyPrint

Expand Down Expand Up @@ -199,7 +199,6 @@ prDataH mode useSTL (cat, rules)
CppStdBeyondAnsi _ -> True
CppStdAnsi _ -> False
cl = identCat (normCat cat)
vararg = if beyondAnsi then "p" else "*p"
prRuleH = if beyondAnsi then prRuleHBeyondAnsi else prRuleHAnsi
itty = concat [ cl, "::", "const_iterator" ]
abstract = case lookupRule (noPosition $ catToStr cat) rules of
Expand Down Expand Up @@ -393,7 +392,7 @@ prPrintData True mode _ _ (cat@(ListCat _), rules) =
render $ genPrintVisitorList (mode, cat, rules)
prPrintData False mode _ _ (cat@(ListCat _), rules) =
genPrintVisitorListNoStl (mode, cat, rules)
prPrintData _ mode _inPackage cf (TokenCat cat, _rules) |
prPrintData _ _ _inPackage cf (TokenCat cat, _rules) |
isPositionCat cf cat = genPositionToken cat
prPrintData _ mode inPackage _cf (cat, rules) =
abstract ++ concatMap (prPrintRule beyondAnsi inPackage) rules
Expand All @@ -402,15 +401,14 @@ prPrintData _ mode inPackage _cf (cat, rules) =
CppStdBeyondAnsi _ -> True
CppStdAnsi _ -> False
cl = identCat (normCat cat)
vararg = "*p"
abstract = case lookupRule (noPosition $ catToStr cat) rules of
Just _ -> ""
Nothing -> "void PrintAbsyn::visit" ++ cl ++ "(" ++ cl +++ "*p) {} //abstract class\n\n"

-- | Generate pretty printer visitor for a list category (STL version).
--
genPrintVisitorList :: (CppStdMode, Cat, [Rule]) -> Doc
genPrintVisitorList (mode, cat@(ListCat _), rules) = vcat
genPrintVisitorList (_, cat@(ListCat _), rules) = vcat
[ "void PrintAbsyn::visit" <> lty <> parens (ltyarg <> "*" <+> varg)
, codeblock 2
[ "iter" <> lty <> parens (vname <> "->begin()" <> comma <+> vname <> "->end()") <> semi ]
Expand All @@ -427,7 +425,7 @@ genPrintVisitorList (mode, cat@(ListCat _), rules) = vcat
, "else"
]
, unless (null docs1)
[ "if (i == j-1)"
[ "if (i == std::prev(j, 1))"
, "{ /* last */"
, nest 2 $ vcat docs1
, "}"
Expand All @@ -442,9 +440,6 @@ genPrintVisitorList (mode, cat@(ListCat _), rules) = vcat
, ""
]
where
beyondAnsi = case mode of
CppStdBeyondAnsi _ -> True
CppStdAnsi _ -> False
cl = identCat (normCat cat)
lty = text cl -- List type
ltyarg = text cl -- List type arg
Expand Down Expand Up @@ -480,7 +475,7 @@ prListRule_ (Rule _ _ items _) = for items $ \case
Left c
| Just{} <- maybeTokenCat c
-> "visit" <> dat <> "(*i);"
| isList c -> "iter" <> dat <> "(i+1, j);"
| isList c -> "iter" <> dat <> "(std::next(i,1), j);"
| otherwise -> "(*i)->accept(this);"
where
dat = text $ identCat $ normCat c
Expand All @@ -489,7 +484,7 @@ prListRule_ (Rule _ _ items _) = for items $ \case
-- between the versions with and without STL.
-- The present version has been adapted from CFtoCPrinter.
genPrintVisitorListNoStl :: (CppStdMode, Cat, [Rule]) -> String
genPrintVisitorListNoStl (mode, cat@(ListCat _), rules) = unlines $ concat
genPrintVisitorListNoStl (_, cat@(ListCat _), rules) = unlines $ concat
[ [ "void PrintAbsyn::visit" ++ cl ++ "("++ cl ++ " *" ++ vname ++ ")"
, "{"
, " if (" ++ vname +++ "== 0)"
Expand All @@ -515,12 +510,8 @@ genPrintVisitorListNoStl (mode, cat@(ListCat _), rules) = unlines $ concat
]
]
where
beyondAnsi = case mode of
CppStdBeyondAnsi _ -> True
CppStdAnsi _ -> False
cl = identCat (normCat cat)
vname = map toLower cl
varg = map toLower cl
pre = vname ++ "->"
prules = sortRulesByPrecedence rules
swRules f = switchByPrecedence "_i_" $
Expand All @@ -531,7 +522,7 @@ genPrintVisitorListNoStl _ = error "genPrintVisitorListNoStl expects a ListCat"

--Pretty Printer methods for a rule.
prPrintRule :: Bool -> Maybe String -> Rule -> String
prPrintRule beyondAnsi inPackage r@(Rule fun _ _ _) | isProperLabel fun = unlines $ concat
prPrintRule _ inPackage r@(Rule fun _ _ _) | isProperLabel fun = unlines $ concat
[ [ "void PrintAbsyn::visit" ++ visitFunName ++ "(" ++ vararg +++ fnm ++ ")"
, "{"
, " int oldi = _i_;"
Expand Down Expand Up @@ -573,7 +564,7 @@ prPrintItem pre (Left (c, nt))

--This prints the functions for Abstract Syntax tree printing.
prShowData :: Bool -> CppStdMode -> (Cat, [Rule]) -> String
prShowData True mode (cat@(ListCat c), _) = unlines
prShowData True _ (cat@(ListCat c), _) = unlines
[
"void ShowAbsyn::visit" ++ cl ++ "("++ cl ++ " *" ++ vname ++ ")",
"{",
Expand All @@ -583,20 +574,16 @@ prShowData True mode (cat@(ListCat c), _) = unlines
if isTokenCat c
then " visit" ++ baseName cl ++ "(*i) ;"
else " (*i)->accept(this);",
" if (i != " ++ vname ++ "->end() - 1) bufAppend(\", \");",
" if (i != std::prev(" ++ vname ++ "->end(), 1)) bufAppend(\", \");",
" }",
"}",
""
]
where
beyondAnsi = case mode of
CppStdBeyondAnsi _ -> True
CppStdAnsi _ -> False
cl = identCat (normCat cat)
vname = map toLower cl
varg = vname

prShowData False mode (cat@(ListCat c), _) =
prShowData False _ (cat@(ListCat c), _) =
unlines
[
"void ShowAbsyn::visit" ++ cl ++ "("++ cl ++ " *" ++ vname ++ ")",
Expand All @@ -619,13 +606,9 @@ prShowData False mode (cat@(ListCat c), _) =
""
]
where
beyondAnsi = case mode of
CppStdBeyondAnsi _ -> True
CppStdAnsi _ -> False
cl = identCat (normCat cat)
ecl = identCat (normCatOfList cat)
vname = map toLower cl
varg = vname
member = map toLower ecl ++ "_"
visitMember
| Just t <- maybeTokenCat c =
Expand All @@ -640,14 +623,13 @@ prShowData _ mode (cat, rules) = --Not a list:
CppStdBeyondAnsi _ -> True
CppStdAnsi _ -> False
cl = identCat (normCat cat)
varg = "p"
abstract = case lookupRule (noPosition $ catToStr cat) rules of
Just _ -> ""
Nothing -> "void ShowAbsyn::visit" ++ cl ++ "(" ++ cl ++ " *p) {} //abstract class\n\n"

--This prints all the methods for Abstract Syntax tree rules.
prShowRule :: IsFun f => Rul f -> Bool -> String
prShowRule (Rule f _ cats _) beyondAnsi | isProperLabel f = concat
prShowRule (Rule f _ cats _) _ | isProperLabel f = concat
[
"void ShowAbsyn::visit" ++ fun ++ "(" ++ vararg +++ fnm ++ ")\n",
"{\n",
Expand Down
Loading

0 comments on commit afccbee

Please sign in to comment.