mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 11:42:49 -06:00
A new function called "completions" is added in the Haskell runtime and used in PGFService. This makes the extraction of completions more platform independent
This commit is contained in:
@@ -66,7 +66,7 @@ module PGF(
|
|||||||
Forest.showBracketedString,flattenBracketedString,
|
Forest.showBracketedString,flattenBracketedString,
|
||||||
|
|
||||||
-- ** Parsing
|
-- ** Parsing
|
||||||
parse, parseAllLang, parseAll, parse_, parseWithRecovery,
|
parse, parseAllLang, parseAll, parse_, parseWithRecovery, complete,
|
||||||
|
|
||||||
-- ** Evaluation
|
-- ** Evaluation
|
||||||
PGF.compute, paraphrase,
|
PGF.compute, paraphrase,
|
||||||
@@ -273,6 +273,25 @@ parse_ pgf lang typ dp s =
|
|||||||
|
|
||||||
parseWithRecovery pgf lang typ open_typs dp s = Parse.parseWithRecovery pgf lang typ open_typs dp (words s)
|
parseWithRecovery pgf lang typ open_typs dp s = Parse.parseWithRecovery pgf lang typ open_typs dp (words s)
|
||||||
|
|
||||||
|
complete :: PGF -> Language -> Type -> String -> String -> (BracketedString,String,Map.Map Token [CId])
|
||||||
|
complete pgf from typ input prefix =
|
||||||
|
let ws = words input
|
||||||
|
ps0 = Parse.initState pgf from typ
|
||||||
|
(ps,ws') = loop ps0 ws
|
||||||
|
bs = snd (Parse.getParseOutput ps typ Nothing)
|
||||||
|
in if not (null ws')
|
||||||
|
then (bs, unwords (if null prefix then ws' else ws'++[prefix]), Map.empty)
|
||||||
|
else (bs, prefix, fmap getFuns (Parse.getCompletions ps prefix))
|
||||||
|
where
|
||||||
|
loop ps [] = (ps,[])
|
||||||
|
loop ps (w:ws) = case Parse.nextState ps (Parse.simpleParseInput w) of
|
||||||
|
Left es -> (ps,w:ws)
|
||||||
|
Right ps -> loop ps ws
|
||||||
|
|
||||||
|
getFuns ps = [cid | (funid,cid,seq) <- snd . head $ Map.toList contInfo]
|
||||||
|
where
|
||||||
|
contInfo = Parse.getContinuationInfo ps
|
||||||
|
|
||||||
groupResults :: [[(Language,String)]] -> [(Language,[String])]
|
groupResults :: [[(Language,String)]] -> [(Language,[String])]
|
||||||
groupResults = Map.toList . foldr more Map.empty . start . concat
|
groupResults = Map.toList . foldr more Map.empty . start . concat
|
||||||
where
|
where
|
||||||
|
|||||||
@@ -663,19 +663,16 @@ doComplete pgf (mfrom,input) mcat mlimit full = showJSON
|
|||||||
froms = maybe (PGF.languages pgf) (:[]) mfrom
|
froms = maybe (PGF.languages pgf) (:[]) mfrom
|
||||||
cat = fromMaybe (PGF.startCat pgf) mcat
|
cat = fromMaybe (PGF.startCat pgf) mcat
|
||||||
|
|
||||||
completionInfo :: PGF -> PGF.Token -> PGF.ParseState -> JSValue
|
completionInfo :: PGF -> PGF.Token -> [PGF.CId] -> JSValue
|
||||||
completionInfo pgf token pstate =
|
completionInfo pgf token funs =
|
||||||
makeObj
|
makeObj
|
||||||
["token".= token
|
["token".= token
|
||||||
,"funs" .= (map mkFun (nubBy ignoreFunIds funs))
|
,"funs" .= map mkFun (nub funs)
|
||||||
]
|
]
|
||||||
where
|
where
|
||||||
contInfo = PGF.getContinuationInfo pstate
|
mkFun cid = case PGF.functionType pgf cid of
|
||||||
funs = snd . head $ Map.toList contInfo -- always get [([],_)] ; funs :: [(fid,cid,seq)]
|
|
||||||
ignoreFunIds (_,cid1,seq1) (_,cid2,seq2) = (cid1,seq1) == (cid2,seq2)
|
|
||||||
mkFun (funid,cid,seq) = case PGF.functionType pgf cid of
|
|
||||||
Just typ ->
|
Just typ ->
|
||||||
makeObj [ {-"fid".=funid,-} "fun".=cid, "hyps".=hyps', "cat".=cat, "seq".=seq ]
|
makeObj [ {-"fid".=funid,-} "fun".=cid, "hyps".=hyps', "cat".=cat ]
|
||||||
where
|
where
|
||||||
(hyps,cat,_es) = PGF.unType typ
|
(hyps,cat,_es) = PGF.unType typ
|
||||||
hyps' = [ PGF.showType [] typ | (_,_,typ) <- hyps ]
|
hyps' = [ PGF.showType [] typ | (_,_,typ) <- hyps ]
|
||||||
@@ -993,28 +990,17 @@ parse' pgf input mcat mfrom =
|
|||||||
cat = fromMaybe (PGF.startCat pgf) mcat
|
cat = fromMaybe (PGF.startCat pgf) mcat
|
||||||
|
|
||||||
complete' :: PGF -> PGF.Language -> PGF.Type -> Maybe Int -> String
|
complete' :: PGF -> PGF.Language -> PGF.Type -> Maybe Int -> String
|
||||||
-> (PGF.BracketedString, String, Map.Map PGF.Token PGF.ParseState)
|
-> (PGF.BracketedString, String, Map.Map PGF.Token [PGF.CId])
|
||||||
complete' pgf from typ mlimit input =
|
complete' pgf from typ mlimit input =
|
||||||
let (ws,prefix) = tokensAndPrefix input
|
let (ws,prefix) = tokensAndPrefix input
|
||||||
ps0 = PGF.initState pgf from typ
|
in PGF.complete pgf from typ (unwords ws) prefix
|
||||||
(ps,ws') = loop ps0 ws
|
|
||||||
bs = snd (PGF.getParseOutput ps typ Nothing)
|
|
||||||
in if not (null ws')
|
|
||||||
then (bs, unwords (if null prefix then ws' else ws'++[prefix]), Map.empty)
|
|
||||||
else (bs, prefix, PGF.getCompletions ps prefix)
|
|
||||||
where
|
where
|
||||||
--order = sortBy (compare `on` map toLower)
|
|
||||||
|
|
||||||
tokensAndPrefix :: String -> ([String],String)
|
tokensAndPrefix :: String -> ([String],String)
|
||||||
tokensAndPrefix s | not (null s) && isSpace (last s) = (ws, "")
|
tokensAndPrefix s | not (null s) && isSpace (last s) = (ws, "")
|
||||||
| null ws = ([],"")
|
| null ws = ([],"")
|
||||||
| otherwise = (init ws, last ws)
|
| otherwise = (init ws, last ws)
|
||||||
where ws = words s
|
where ws = words s
|
||||||
|
|
||||||
loop ps [] = (ps,[])
|
|
||||||
loop ps (w:ws) = case PGF.nextState ps (PGF.simpleParseInput w) of
|
|
||||||
Left es -> (ps,w:ws)
|
|
||||||
Right ps -> loop ps ws
|
|
||||||
|
|
||||||
transfer lang = if "LaTeX" `isSuffixOf` show lang
|
transfer lang = if "LaTeX" `isSuffixOf` show lang
|
||||||
then fold -- OpenMath LaTeX transfer
|
then fold -- OpenMath LaTeX transfer
|
||||||
|
|||||||
Reference in New Issue
Block a user