forked from GitHub/gf-core
Added --parser=ondemand flag.
This commit is contained in:
@@ -76,7 +76,10 @@ optimize opts = cse . suf
|
|||||||
|
|
||||||
buildParser :: Options -> PGF -> PGF
|
buildParser :: Options -> PGF -> PGF
|
||||||
buildParser opts =
|
buildParser opts =
|
||||||
if flag optBuildParser opts then addParsers opts else id
|
case flag optBuildParser opts of
|
||||||
|
BuildParser -> addParsers opts
|
||||||
|
DontBuildParser -> id
|
||||||
|
BuildParserOnDemand -> mapConcretes (\cnc -> cnc { cflags = Map.insert (mkCId "parser") "ondemand" (cflags cnc) })
|
||||||
|
|
||||||
batchCompile :: Options -> [FilePath] -> IOE SourceGrammar
|
batchCompile :: Options -> [FilePath] -> IOE SourceGrammar
|
||||||
batchCompile opts files = do
|
batchCompile opts files = do
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ canon2gfcc opts pars cgr@(M.MGrammar ((a,M.ModMod abm):cms)) =
|
|||||||
cns = map (i2i . fst) cms
|
cns = map (i2i . fst) cms
|
||||||
abs = D.Abstr aflags funs cats catfuns
|
abs = D.Abstr aflags funs cats catfuns
|
||||||
gflags = Map.empty
|
gflags = Map.empty
|
||||||
aflags = Map.fromList [(mkCId f,x) | (f,x) <- moduleOptionsGFO (M.flags abm)]
|
aflags = Map.fromList [(mkCId f,x) | (f,x) <- optionsPGF (M.flags abm)]
|
||||||
mkDef pty = case pty of
|
mkDef pty = case pty of
|
||||||
Yes t -> mkExp t
|
Yes t -> mkExp t
|
||||||
_ -> CM.primNotion
|
_ -> CM.primNotion
|
||||||
@@ -93,7 +93,7 @@ canon2gfcc opts pars cgr@(M.MGrammar ((a,M.ModMod abm):cms)) =
|
|||||||
(lang,D.Concr flags lins opers lincats lindefs printnames params fcfg)
|
(lang,D.Concr flags lins opers lincats lindefs printnames params fcfg)
|
||||||
where
|
where
|
||||||
js = tree2list (M.jments mo)
|
js = tree2list (M.jments mo)
|
||||||
flags = Map.fromList [(mkCId f,x) | (f,x) <- moduleOptionsGFO (M.flags mo)]
|
flags = Map.fromList [(mkCId f,x) | (f,x) <- optionsPGF (M.flags mo)]
|
||||||
opers = Map.fromAscList [] -- opers will be created as optimization
|
opers = Map.fromAscList [] -- opers will be created as optimization
|
||||||
utf = id -- trace (show lang0 +++ show flags) $
|
utf = id -- trace (show lang0 +++ show flags) $
|
||||||
-- if moduleFlag optEncoding (moduleOptions (M.flags mo)) == UTF_8
|
-- if moduleFlag optEncoding (moduleOptions (M.flags mo)) == UTF_8
|
||||||
|
|||||||
@@ -5,11 +5,12 @@ module GF.Infra.Option
|
|||||||
Flags(..),
|
Flags(..),
|
||||||
Mode(..), Phase(..), Verbosity(..), Encoding(..), OutputFormat(..),
|
Mode(..), Phase(..), Verbosity(..), Encoding(..), OutputFormat(..),
|
||||||
SISRFormat(..), Optimization(..), CFGTransform(..), HaskellOption(..),
|
SISRFormat(..), Optimization(..), CFGTransform(..), HaskellOption(..),
|
||||||
Dump(..), Printer(..), Recomp(..),
|
Dump(..), Printer(..), Recomp(..), BuildParser(..),
|
||||||
-- * Option parsing
|
-- * Option parsing
|
||||||
parseOptions, parseModuleOptions,
|
parseOptions, parseModuleOptions,
|
||||||
-- * Option pretty-printing
|
-- * Option pretty-printing
|
||||||
moduleOptionsGFO,
|
optionsGFO,
|
||||||
|
optionsPGF,
|
||||||
-- * Option manipulation
|
-- * Option manipulation
|
||||||
addOptions, concatOptions, noOptions,
|
addOptions, concatOptions, noOptions,
|
||||||
modifyFlags,
|
modifyFlags,
|
||||||
@@ -136,6 +137,9 @@ data Printer = PrinterStrip -- ^ Remove name qualifiers.
|
|||||||
data Recomp = AlwaysRecomp | RecompIfNewer | NeverRecomp
|
data Recomp = AlwaysRecomp | RecompIfNewer | NeverRecomp
|
||||||
deriving (Show,Eq,Ord)
|
deriving (Show,Eq,Ord)
|
||||||
|
|
||||||
|
data BuildParser = BuildParser | DontBuildParser | BuildParserOnDemand
|
||||||
|
deriving (Show,Eq,Ord)
|
||||||
|
|
||||||
data Flags = Flags {
|
data Flags = Flags {
|
||||||
optMode :: Mode,
|
optMode :: Mode,
|
||||||
optStopAfterPhase :: Phase,
|
optStopAfterPhase :: Phase,
|
||||||
@@ -167,7 +171,7 @@ data Flags = Flags {
|
|||||||
optLexer :: Maybe String,
|
optLexer :: Maybe String,
|
||||||
optUnlexer :: Maybe String,
|
optUnlexer :: Maybe String,
|
||||||
optErasing :: Bool,
|
optErasing :: Bool,
|
||||||
optBuildParser :: Bool,
|
optBuildParser :: BuildParser,
|
||||||
optWarnings :: [Warning],
|
optWarnings :: [Warning],
|
||||||
optDump :: [Dump]
|
optDump :: [Dump]
|
||||||
}
|
}
|
||||||
@@ -195,13 +199,18 @@ parseModuleOptions args = do (opts,nonopts) <- parseOptions args
|
|||||||
|
|
||||||
-- Showing options
|
-- Showing options
|
||||||
|
|
||||||
-- | Pretty-print the module options that are preserved in .gfo files.
|
-- | Pretty-print the options that are preserved in .gfo files.
|
||||||
moduleOptionsGFO :: Options -> [(String,String)]
|
optionsGFO :: Options -> [(String,String)]
|
||||||
moduleOptionsGFO opts =
|
optionsGFO opts = optionsPGF opts
|
||||||
|
++ [("coding", show (flag optEncoding opts))]
|
||||||
|
|
||||||
|
-- | Pretty-print the options that are preserved in .pgf files.
|
||||||
|
optionsPGF :: Options -> [(String,String)]
|
||||||
|
optionsPGF opts =
|
||||||
maybe [] (\x -> [("language",x)]) (flag optSpeechLanguage opts)
|
maybe [] (\x -> [("language",x)]) (flag optSpeechLanguage opts)
|
||||||
++ maybe [] (\x -> [("startcat",x)]) (flag optStartCat opts)
|
++ maybe [] (\x -> [("startcat",x)]) (flag optStartCat opts)
|
||||||
++ [("coding", show (flag optEncoding opts))]
|
|
||||||
++ (if flag optErasing opts then [("erasing","on")] else [])
|
++ (if flag optErasing opts then [("erasing","on")] else [])
|
||||||
|
++ (if flag optBuildParser opts == BuildParserOnDemand then [("parser","ondemand")] else [])
|
||||||
|
|
||||||
-- Option manipulation
|
-- Option manipulation
|
||||||
|
|
||||||
@@ -256,7 +265,7 @@ defaultFlags = Flags {
|
|||||||
optLexer = Nothing,
|
optLexer = Nothing,
|
||||||
optUnlexer = Nothing,
|
optUnlexer = Nothing,
|
||||||
optErasing = False,
|
optErasing = False,
|
||||||
optBuildParser = True,
|
optBuildParser = BuildParser,
|
||||||
optWarnings = [],
|
optWarnings = [],
|
||||||
optDump = []
|
optDump = []
|
||||||
}
|
}
|
||||||
@@ -331,7 +340,7 @@ optDescr =
|
|||||||
("Character encoding of the source grammar, ENCODING = "
|
("Character encoding of the source grammar, ENCODING = "
|
||||||
++ concat (intersperse " | " (map fst encodings)) ++ "."),
|
++ concat (intersperse " | " (map fst encodings)) ++ "."),
|
||||||
Option [] ["erasing"] (onOff erasing False) "Generate erasing grammar (default off).",
|
Option [] ["erasing"] (onOff erasing False) "Generate erasing grammar (default off).",
|
||||||
Option [] ["parser"] (onOff parser True) "Build parser (default on).",
|
Option [] ["parser"] (ReqArg buildParser "VALUE") "Build parser (default on). VALUE = on | off | ondemand",
|
||||||
Option [] ["startcat"] (ReqArg startcat "CAT") "Grammar start category.",
|
Option [] ["startcat"] (ReqArg startcat "CAT") "Grammar start category.",
|
||||||
Option [] ["language"] (ReqArg language "LANG") "Set the speech language flag to LANG in the generated grammar.",
|
Option [] ["language"] (ReqArg language "LANG") "Set the speech language flag to LANG in the generated grammar.",
|
||||||
Option [] ["lexer"] (ReqArg lexer "LEXER") "Use lexer LEXER.",
|
Option [] ["lexer"] (ReqArg lexer "LEXER") "Use lexer LEXER.",
|
||||||
@@ -388,7 +397,11 @@ optDescr =
|
|||||||
Just c -> set $ \o -> o { optEncoding = c }
|
Just c -> set $ \o -> o { optEncoding = c }
|
||||||
Nothing -> fail $ "Unknown character encoding: " ++ x
|
Nothing -> fail $ "Unknown character encoding: " ++ x
|
||||||
erasing x = set $ \o -> o { optErasing = x }
|
erasing x = set $ \o -> o { optErasing = x }
|
||||||
parser x = set $ \o -> o { optBuildParser = x }
|
buildParser x = do v <- case x of
|
||||||
|
"on" -> return BuildParser
|
||||||
|
"off" -> return DontBuildParser
|
||||||
|
"ondemand" -> return BuildParserOnDemand
|
||||||
|
set $ \o -> o { optBuildParser = v }
|
||||||
startcat x = set $ \o -> o { optStartCat = Just x }
|
startcat x = set $ \o -> o { optStartCat = Just x }
|
||||||
language x = set $ \o -> o { optSpeechLanguage = Just x }
|
language x = set $ \o -> o { optSpeechLanguage = Just x }
|
||||||
lexer x = set $ \o -> o { optLexer = Just x }
|
lexer x = set $ \o -> o { optLexer = Just x }
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ trPerh p = case p of
|
|||||||
_ -> P.EMeta ---
|
_ -> P.EMeta ---
|
||||||
|
|
||||||
trFlags :: Options -> [P.TopDef]
|
trFlags :: Options -> [P.TopDef]
|
||||||
trFlags = map trFlag . moduleOptionsGFO
|
trFlags = map trFlag . optionsGFO
|
||||||
|
|
||||||
trFlag :: (String,String) -> P.TopDef
|
trFlag :: (String,String) -> P.TopDef
|
||||||
trFlag (f,x) = P.DefFlag [P.FlagDef (tri $ identC (BS.pack f)) (tri $ identC (BS.pack x))]
|
trFlag (f,x) = P.DefFlag [P.FlagDef (tri $ identC (BS.pack f)) (tri $ identC (BS.pack x))]
|
||||||
|
|||||||
10
src/PGF.hs
10
src/PGF.hs
@@ -214,12 +214,12 @@ readPGF f = do
|
|||||||
g <- parseGrammar s
|
g <- parseGrammar s
|
||||||
return $! addParsers $ toPGF g
|
return $! addParsers $ toPGF g
|
||||||
|
|
||||||
-- Adds parsers for all concretes that don't have a parser.
|
-- Adds parsers for all concretes that don't have a parser and that have parser=ondemand.
|
||||||
addParsers :: PGF -> PGF
|
addParsers :: PGF -> PGF
|
||||||
addParsers pgf = pgf { concretes = Map.map conv (concretes pgf) }
|
addParsers pgf = mapConcretes (\cnc -> if wantsParser cnc then addParser cnc else cnc) pgf
|
||||||
where
|
where
|
||||||
conv cnc | isJust (parser cnc) = cnc
|
wantsParser cnc = isNothing (parser cnc) && Map.lookup (mkCId "parser") (cflags cnc) == Just "ondemand"
|
||||||
| otherwise = cnc { parser = Just (PMCFG.convertConcrete (abstract pgf) cnc) }
|
addParser cnc = cnc { parser = Just (PMCFG.convertConcrete (abstract pgf) cnc) }
|
||||||
|
|
||||||
linearize pgf lang = concat . take 1 . PGF.Linearize.linearizes pgf lang
|
linearize pgf lang = concat . take 1 . PGF.Linearize.linearizes pgf lang
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user