summaryrefslogtreecommitdiff
path: root/src/compiler/GF/Grammar/BNFC.hs
diff options
context:
space:
mode:
authorkrasimir <krasimir@chalmers.se>2016-03-22 13:13:35 +0000
committerkrasimir <krasimir@chalmers.se>2016-03-22 13:13:35 +0000
commita393c1a246bb946e53f26b7b91a173c2ba1a0fa7 (patch)
tree54dbc9eff02cda48568821d53061a01d3f4944dd /src/compiler/GF/Grammar/BNFC.hs
parentce7072085947f4981c8d6d49b571e3cf5683fbb6 (diff)
fix the handling of separators in BNFC which are not nonempty
Diffstat (limited to 'src/compiler/GF/Grammar/BNFC.hs')
-rw-r--r--src/compiler/GF/Grammar/BNFC.hs26
1 files changed, 19 insertions, 7 deletions
diff --git a/src/compiler/GF/Grammar/BNFC.hs b/src/compiler/GF/Grammar/BNFC.hs
index dbc3d8edf..9d0915072 100644
--- a/src/compiler/GF/Grammar/BNFC.hs
+++ b/src/compiler/GF/Grammar/BNFC.hs
@@ -55,7 +55,7 @@ isSepTerm _ = False
transformRules :: SepMap -> BNFCRule -> [ParamCFRule]
transformRules sepMap (BNFCRule c smbs@(s:ss) r) = Rule (c,[0]) cfSmbs r : rls
- where smbs' = map transformSymb smbs
+ where smbs' = map (transformSymb sepMap) smbs
cfSmbs = [snd s | s <- smbs']
ids = filter (/= "") [fst s | s <- smbs']
rls = concatMap (createListRules sepMap) ids
@@ -71,10 +71,14 @@ fRules c n = Rule (c',[0]) ss rn
ss = [NonTerminal (c ++ show (n+1),[0])]
rn = CFObj (mkCId $ "coercion_" ++ c') []
-transformSymb :: BNFCSymbol -> (String, ParamCFSymbol)
-transformSymb s = case s of
+transformSymb :: SepMap -> BNFCSymbol -> (String, ParamCFSymbol)
+transformSymb sepMap s = case s of
NonTerminal (c,False) -> ("", NonTerminal (c,[0]))
- NonTerminal (c,True ) -> (c , NonTerminal $ ("List" ++ c,[0]))
+ NonTerminal (c,True ) -> let needsCoercion =
+ case lookup c sepMap of
+ Just (ne, isSep, symb) -> isSep && symb /= "" && not ne
+ Nothing -> False
+ in (c , NonTerminal ("List" ++ c,if needsCoercion then [0,1] else [0]))
Terminal t -> ("", Terminal t)
createListRules :: SepMap -> String -> [ParamCFRule]
@@ -84,15 +88,23 @@ createListRules sepMap c =
Nothing -> createListRules' False True "" c
createListRules':: IsNonempty -> IsSeparator -> SepTermSymb -> String -> [ParamCFRule]
-createListRules' ne isSep symb c = ruleCons : [ruleBase]
+createListRules' ne isSep symb c = ruleBase : ruleCons
where ruleBase = Rule ("List" ++ c,[0]) smbs rn
where smbs = if isSep
then [NonTerminal (c,[0]) | ne]
else [NonTerminal (c,[0]) | ne] ++
[Terminal symb | symb /= "" && ne]
rn = CFObj (mkCId $ "Base" ++ c) []
- ruleCons = Rule ("List" ++ c,[0]) smbs rn
- where smbs = [NonTerminal (c,[0])] ++
+ ruleCons
+ | isSep && symb /= "" && not ne = [Rule ("List" ++ c,[1]) smbs0 rn
+ ,Rule ("List" ++ c,[1]) smbs1 rn]
+ | otherwise = [Rule ("List" ++ c,[0]) smbs rn]
+ where smbs0 =[NonTerminal (c,[0])] ++
+ [NonTerminal ("List" ++ c,[0])]
+ smbs1 =[NonTerminal (c,[0])] ++
+ [Terminal symb] ++
+ [NonTerminal ("List" ++ c,[1])]
+ smbs = [NonTerminal (c,[0])] ++
[Terminal symb | symb /= ""] ++
[NonTerminal ("List" ++ c,[0])]
rn = CFObj (mkCId $ "Cons" ++ c) []