From 0d4659fe8c6665d825e4eb685105e58894545eab Mon Sep 17 00:00:00 2001 From: "John J. Camilleri" Date: Fri, 5 Mar 2021 13:23:00 +0100 Subject: [PATCH] Add workaround for missing param defs. Add links to gf-core issues in workaround comments. --- src/compiler/GF/Compile/GrammarToLPGF.hs | 21 +++++++++++++++------ testsuite/lpgf/README.md | 1 + 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/compiler/GF/Compile/GrammarToLPGF.hs b/src/compiler/GF/Compile/GrammarToLPGF.hs index f90f43fd8..caf5e94f8 100644 --- a/src/compiler/GF/Compile/GrammarToLPGF.hs +++ b/src/compiler/GF/Compile/GrammarToLPGF.hs @@ -70,6 +70,15 @@ mkCanon2lpgf opts gr am = do funMap :: Map.Map C.FunId C.FunDef funMap = Map.fromList [ (fid,d) | d@(C.FunDef fid _) <- funs ] + -- | Lookup paramdef, providing dummy fallback when not found + -- Workaround for https://github.com/GrammaticalFramework/gf-core/issues/100 + lookupParamDef :: C.ParamId -> Either String C.ParamDef + lookupParamDef pid = case Map.lookup pid paramValueMap of + Just d -> Right d + Nothing -> + -- Left $ printf "Cannot find param definition: %s" (show pid) + Right $ C.ParamDef (C.ParamId (C.Unqual "DUMMY")) [C.Param pid []] + -- | Lookup lintype for a function lookupLinType :: C.FunId -> Either String C.LinType lookupLinType funId = do @@ -89,8 +98,8 @@ mkCanon2lpgf opts gr am = do let (C.LincatDef _ lt) = lincat return lt - -- filter out record fields from defn which don't appear in lincat - -- this seems to be an inconsistency in the canonical representation + -- Filter out record fields from definitions which don't appear in lincat. + -- Workaround for https://github.com/GrammaticalFramework/gf-core/issues/101 lindefs' = [ C.LinDef funId varIds linValue' | (C.LinDef funId varIds linValue) <- lindefs @@ -129,7 +138,7 @@ mkCanon2lpgf opts gr am = do let collectProjections :: C.LinValue -> Either String [L.LinFun] collectProjections (C.ParamConstant (C.Param pid lvs)) = do - def <- m2e (printf "Cannot find param definition: %s" (show pid)) (Map.lookup pid paramValueMap) + def <- lookupParamDef pid let (C.ParamDef tpid defpids) = def pidIx <- eitherElemIndex pid [ p | C.Param p _ <- defpids ] rest <- mapM collectProjections lvs @@ -139,7 +148,7 @@ mkCanon2lpgf opts gr am = do return [lf] lfs <- collectProjections lv let term = L.Tuple lfs - def <- m2e (printf "Cannot find param definition: %s" (show pid)) (Map.lookup pid paramValueMap) + def <- lookupParamDef pid let (C.ParamDef tpid _) = def return (term, Just $ C.ParamType (C.ParamTypeId tpid)) @@ -341,8 +350,8 @@ inlineParamAliases defs = if null aliases then defs else map rp' pdefs Just (C.ParamAliasDef _ (C.ParamType (C.ParamTypeId p))) -> p _ -> pid --- | Always put 's' reocord field first, then sort alphabetically --- This seems to be done inconsistently in the canonical format +-- | Always put 's' reocord field first, then sort alphabetically. +-- Workaround for https://github.com/GrammaticalFramework/gf-core/issues/102 -- Based on GF.Granmar.Macros.sortRec sortRecordRows :: [C.RecordRowValue] -> [C.RecordRowValue] sortRecordRows = L.sortBy ordLabel diff --git a/testsuite/lpgf/README.md b/testsuite/lpgf/README.md index f549be9ae..5df8a7d17 100644 --- a/testsuite/lpgf/README.md +++ b/testsuite/lpgf/README.md @@ -19,6 +19,7 @@ stack test gf:test:lpgf --test-arguments="foods/Foods Fre Ger" # specific gramma ``` stack build --test --bench --no-run-tests --no-run-benchmarks && DEBUG=1 stack test gf:test:lpgf --test-arguments="foods/Foods Fre Ger" +stack build --test --bench --no-run-tests --no-run-benchmarks && DEBUG=1 stack test gf:test:lpgf --test-arguments="phrasebook/Phrasebook Bul" ``` Set environment variable `DEBUG=1` to enable dumping of intermediate formats.