From 7007a875e36768eff690bad997c919ab7edccb56 Mon Sep 17 00:00:00 2001 From: bjorn Date: Mon, 18 Aug 2008 09:26:24 +0000 Subject: [PATCH] Added canParse function to the PGF API. Make parseAllLang and parseAll skip languages which don't have parsers, instead of always throwing exceptions when some language is missing a parser. --- src/PGF.hs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/PGF.hs b/src/PGF.hs index 8ae924c42..f943a0b4c 100644 --- a/src/PGF.hs +++ b/src/PGF.hs @@ -41,7 +41,7 @@ module PGF( linearize, linearizeAllLang, linearizeAll, -- ** Parsing - parse, parseAllLang, parseAll, + parse, canParse, parseAllLang, parseAll, -- ** Evaluation tree2expr, expr2tree, @@ -71,6 +71,7 @@ import GF.Data.ErrM import GF.Data.Utilities import qualified Data.Map as Map +import Data.Maybe import System.Random (newStdGen) --------------------------------------------------- @@ -104,8 +105,13 @@ linearize :: PGF -> Language -> Tree -> String -- and to produce abstract syntax expression. An empty -- list is returned if the parsing is not successful. The list may also -- contain more than one element if the grammar is ambiguous. +-- Throws an exception if the given language cannot be used +-- for parsing, see 'canParse'. parse :: PGF -> Language -> Category -> String -> [Tree] +-- | Checks whether the given language can be used for parsing. +canParse :: PGF -> Language -> Bool + -- | The same as 'linearizeAllLang' but does not return -- the language. linearizeAll :: PGF -> Tree -> [String] @@ -118,13 +124,14 @@ linearizeAllLang :: PGF -> Tree -> [(Language,String)] -- the language. parseAll :: PGF -> Category -> String -> [[Tree]] --- | Tries to parse the given string with every language --- available in the grammar and to produce abstract syntax --- expression. The returned list contains pairs of language --- and list of possible expressions. Only those languages +-- | Tries to parse the given string with all available languages. +-- Languages which cannot be used for parsing (see 'canParse') +-- are ignored. +-- The returned list contains pairs of language +-- and list of abstract syntax expressions +-- (this is a list, since grammars can be ambiguous). +-- Only those languages -- for which at least one parsing is possible are listed. --- More than one abstract syntax expressions are possible --- if the grammar is ambiguous. parseAllLang :: PGF -> Category -> String -> [(Language,[Tree])] -- | Creates an initial parsing state for a given language and @@ -194,9 +201,11 @@ parse pgf lang cat s = else case parseFCFG "bottomup" pinfo (mkCId cat) (words s) of Ok x -> x Bad s -> error s - Nothing -> error ("No parser built fo language: " ++ lang) + Nothing -> error ("No parser built for language: " ++ lang) Nothing -> error ("Unknown language: " ++ lang) +canParse pgf cnc = isJust (lookParser pgf (mkCId cnc)) + linearizeAll mgr = map snd . linearizeAllLang mgr linearizeAllLang mgr t = [(lang,PGF.linearize mgr lang t) | lang <- languages mgr] @@ -204,7 +213,7 @@ linearizeAllLang mgr t = parseAll mgr cat = map snd . parseAllLang mgr cat parseAllLang mgr cat s = - [(lang,ts) | lang <- languages mgr, let ts = parse mgr lang cat s, not (null ts)] + [(lang,ts) | lang <- languages mgr, canParse mgr lang, let ts = parse mgr lang cat s, not (null ts)] initState pgf lang cat = case lookParser pgf langCId of