summaryrefslogtreecommitdiff
path: root/src/runtime
diff options
context:
space:
mode:
Diffstat (limited to 'src/runtime')
-rw-r--r--src/runtime/c/pgf/linearizer.c174
-rw-r--r--src/runtime/c/pgf/linearizer.h14
-rw-r--r--src/runtime/haskell-bind/PGF2.hsc106
-rw-r--r--src/runtime/haskell-bind/PGF2/FFI.hsc4
-rw-r--r--src/runtime/typescript/gflib.d.ts337
5 files changed, 541 insertions, 94 deletions
diff --git a/src/runtime/c/pgf/linearizer.c b/src/runtime/c/pgf/linearizer.c
index ced2a8cf2..2284b10dd 100644
--- a/src/runtime/c/pgf/linearizer.c
+++ b/src/runtime/c/pgf/linearizer.c
@@ -130,6 +130,13 @@ pgf_print_cnc_tree(PgfCncTree ctree, GuOut* out, GuExn* err)
if (clit->n_vars > 0) gu_putc(')', out, err);
break;
}
+ case PGF_CNC_TREE_LINDEF: {
+ PgfCncTreeLinDef* clindef = ti.data;
+ gu_putc('(', out, err);
+ pgf_print_cnc_tree_vars(clindef->n_vars, clindef->context, out, err);
+ gu_printf(out, err, "F%d[%s] %s)", clindef->fun->funid, clindef->str);
+ break;
+ }
case GU_VARIANT_NULL:
gu_puts("null", out, err);
break;
@@ -222,44 +229,32 @@ redo:;
static PgfCncTree
pgf_cnc_resolve_def(PgfCnc* cnc,
size_t n_vars, PgfPrintContext* context,
- PgfCCat* ccat, GuString s, GuPool* pool)
+ PgfCId abs_id, PgfCCat* ccat, GuString s, GuPool* pool)
{
- PgfCncTree lit = gu_null_variant;
PgfCncTree ret = gu_null_variant;
+ PgfCncFun* fun = NULL;
- PgfCncTreeLit* clit =
- gu_new_variant(PGF_CNC_TREE_LIT,
- PgfCncTreeLit,
- &lit, pool);
- clit->n_vars = 0;
- clit->context = context;
- clit->fid = cnc->fid++;
- PgfLiteralStr* lit_str =
- gu_new_flex_variant(PGF_LITERAL_STR,
- PgfLiteralStr,
- val, strlen(s)+1,
- &clit->lit, pool);
- strcpy((char*) lit_str->val, (char*) s);
-
- if (ccat->lindefs == NULL)
- return lit;
-
- int index =
- gu_choice_next(cnc->ch, gu_seq_length(ccat->lindefs));
- if (index < 0) {
- return ret;
+ if (ccat->lindefs != NULL) {
+ int index =
+ gu_choice_next(cnc->ch, gu_seq_length(ccat->lindefs));
+ if (index < 0) {
+ return ret;
+ }
+
+ fun = gu_seq_get(ccat->lindefs, PgfCncFun*, index);
}
- PgfCncTreeApp* capp =
- gu_new_flex_variant(PGF_CNC_TREE_APP,
- PgfCncTreeApp,
- args, 1, &ret, pool);
- capp->ccat = ccat;
- capp->fun = gu_seq_get(ccat->lindefs, PgfCncFun*, index);
- capp->fid = cnc->fid++;
- capp->n_vars = n_vars;
- capp->context = context;
- capp->n_args = 1;
- capp->args[0] = lit;
+
+ PgfCncTreeLinDef* clindef =
+ gu_new_variant(PGF_CNC_TREE_LINDEF,
+ PgfCncTreeLinDef,
+ &ret, pool);
+ clindef->abs_id = abs_id;
+ clindef->ccat = ccat;
+ clindef->fun = fun;
+ clindef->fid = cnc->fid++;
+ clindef->n_vars = n_vars;
+ clindef->context = context;
+ clindef->str = s;
return ret;
}
@@ -433,10 +428,10 @@ pgf_cnc_resolve(PgfCnc* cnc,
gu_putc('[', out, err);
gu_string_write(efun->fun, out, err);
gu_putc(']', out, err);
- GuString s = gu_string_buf_freeze(sbuf, tmp_pool);
+ GuString s = gu_string_buf_freeze(sbuf, pool);
if (ccat != NULL) {
- ret = pgf_cnc_resolve_def(cnc, n_vars, context, ccat, s, pool);
+ ret = pgf_cnc_resolve_def(cnc, n_vars, context, efun->fun, ccat, s, pool);
} else {
PgfCncTreeLit* clit =
gu_new_variant(PGF_CNC_TREE_LIT,
@@ -504,7 +499,7 @@ redo:;
}
if (ccat != NULL) {
- ret = pgf_cnc_resolve_def(cnc, n_vars, context, ccat, ctxt->name, pool);
+ ret = pgf_cnc_resolve_def(cnc, n_vars, context, ctxt->name, ccat, ctxt->name, pool);
} else {
PgfCncTreeLit* clit =
gu_new_variant(PGF_CNC_TREE_LIT,
@@ -634,7 +629,7 @@ typedef struct {
typedef struct {
PgfLinFuncs* funcs;
- PgfCncTreeApp* app;
+ PgfCncTree ctree;
PgfSymbolKP* kp;
GuBuf* events;
PgfLzr* lzr;
@@ -642,7 +637,7 @@ typedef struct {
} PgfLzrCache;
static void
-pgf_lzr_linearize_symbols(PgfLzr* lzr, PgfCncTreeApp* fapp,
+pgf_lzr_linearize_symbols(PgfLzr* lzr, PgfCncTree ctree,
PgfSymbols* syms, uint16_t sym_idx);
static void
@@ -652,7 +647,7 @@ static void
pgf_lzr_cache_flush(PgfLzrCache* cache, PgfSymbols* form)
{
cache->lzr->funcs = cache->prev;
- pgf_lzr_linearize_symbols(cache->lzr, cache->app, form, 0);
+ pgf_lzr_linearize_symbols(cache->lzr, cache->ctree, form, 0);
size_t n_cached = gu_buf_length(cache->events);
for (size_t i = 0; i < n_cached; i++) {
@@ -826,6 +821,12 @@ pgf_lzr_linearize_var(PgfLzr* lzr, PgfCncTree ctree, size_t var_idx)
context = flit->context;
break;
}
+ case PGF_CNC_TREE_LINDEF: {
+ PgfCncTreeLinDef* flindef = cti.data;
+ n_vars = flindef->n_vars;
+ context = flindef->context;
+ break;
+ }
default:
gu_impossible();
}
@@ -842,7 +843,7 @@ pgf_lzr_linearize_var(PgfLzr* lzr, PgfCncTree ctree, size_t var_idx)
}
static void
-pgf_lzr_linearize_symbols(PgfLzr* lzr, PgfCncTreeApp* fapp,
+pgf_lzr_linearize_symbols(PgfLzr* lzr, PgfCncTree ctree,
PgfSymbols* syms, uint16_t sym_idx)
{
size_t nsyms = gu_seq_length(syms);
@@ -851,26 +852,40 @@ pgf_lzr_linearize_symbols(PgfLzr* lzr, PgfCncTreeApp* fapp,
GuVariantInfo sym_i = gu_variant_open(sym);
switch (sym_i.tag) {
case PGF_SYMBOL_CAT:
- case PGF_SYMBOL_LIT: {
- if (fapp == NULL)
+ case PGF_SYMBOL_LIT:
+ case PGF_SYMBOL_VAR: {
+ if (gu_variant_is_null(ctree))
return;
PgfSymbolIdx* sidx = sym_i.data;
- gu_assert((unsigned) sidx->d < fapp->n_args);
- PgfCncTree argf = fapp->args[sidx->d];
- pgf_lzr_linearize_tree(lzr, argf, sidx->r);
- break;
- }
- case PGF_SYMBOL_VAR: {
- if (fapp == NULL)
- return;
+ GuVariantInfo tree_i = gu_variant_open(ctree);
+ switch (tree_i.tag) {
+ case PGF_CNC_TREE_APP: {
+ PgfCncTreeApp* papp = tree_i.data;
- PgfSymbolIdx* sidx = sym_i.data;
- gu_assert((unsigned) sidx->d < fapp->n_args);
+ gu_assert((unsigned) sidx->d < papp->n_args);
+
+ PgfCncTree argf = papp->args[sidx->d];
+ if (sym_i.tag == PGF_SYMBOL_VAR)
+ pgf_lzr_linearize_var(lzr, argf, sidx->r);
+ else
+ pgf_lzr_linearize_tree(lzr, argf, sidx->r);
+ break;
+ }
+ case PGF_CNC_TREE_LINDEF: {
+ PgfCncTreeLinDef* plindef = tree_i.data;
+
+ gu_assert(sidx->d == 0);
- PgfCncTree argf = fapp->args[sidx->d];
- pgf_lzr_linearize_var(lzr, argf, sidx->r);
+ if ((*lzr->funcs)->symbol_token) {
+ (*lzr->funcs)->symbol_token(lzr->funcs, plindef->str);
+ }
+ break;
+ }
+ default:
+ gu_impossible();
+ }
break;
}
case PGF_SYMBOL_KS: {
@@ -885,12 +900,12 @@ pgf_lzr_linearize_symbols(PgfLzr* lzr, PgfCncTreeApp* fapp,
PgfSymbolKP* kp = sym_i.data;
//gu_buf_push(pres, PgfSymbolKP*, kp);
PgfLzrCache* cache = gu_new(PgfLzrCache, lzr->tmp_pool);
- cache->funcs = &pgf_lzr_cache_funcs;
- cache->app = fapp;
- cache->kp = kp;
- cache->events= gu_new_buf(PgfLzrCached, lzr->tmp_pool);
- cache->lzr = lzr;
- cache->prev = lzr->funcs;
+ cache->funcs = &pgf_lzr_cache_funcs;
+ cache->ctree = ctree;
+ cache->kp = kp;
+ cache->events = gu_new_buf(PgfLzrCached, lzr->tmp_pool);
+ cache->lzr = lzr;
+ cache->prev = lzr->funcs;
lzr->funcs = &cache->funcs;
break;
}
@@ -945,8 +960,8 @@ pgf_lzr_linearize_tree(PgfLzr* lzr, PgfCncTree ctree, size_t lin_idx)
}
gu_require(lin_idx < fun->n_lins);
- pgf_lzr_linearize_symbols(lzr, fapp, fun->lins[lin_idx]->syms, 0);
-
+ pgf_lzr_linearize_symbols(lzr, ctree, fun->lins[lin_idx]->syms, 0);
+
if ((*lzr->funcs)->end_phrase && fapp->ccat != NULL) {
(*lzr->funcs)->end_phrase(lzr->funcs,
fun->absfun->type->cid,
@@ -1017,6 +1032,29 @@ pgf_lzr_linearize_tree(PgfLzr* lzr, PgfCncTree ctree, size_t lin_idx)
break;
}
+ case PGF_CNC_TREE_LINDEF: {
+ PgfCncTreeLinDef* flindef = cti.data;
+ PgfCncFun* fun = flindef->fun;
+
+ if ((*lzr->funcs)->begin_phrase) {
+ (*lzr->funcs)->begin_phrase(lzr->funcs,
+ "",
+ flindef->fid, lin_idx,
+ flindef->abs_id);
+ }
+
+ gu_require(lin_idx < fun->n_lins);
+ pgf_lzr_linearize_symbols(lzr, ctree, fun->lins[lin_idx]->syms, 0);
+
+ if ((*lzr->funcs)->end_phrase) {
+ (*lzr->funcs)->end_phrase(lzr->funcs,
+ "",
+ flindef->fid, lin_idx,
+ flindef->abs_id);
+ }
+
+ break;
+ }
default:
gu_impossible();
}
@@ -1186,10 +1224,16 @@ pgf_lzr_get_table(PgfConcr* concr, PgfCncTree ctree,
*labels = &s_label;
break;
}
+ case PGF_CNC_TREE_LINDEF: {
+ static GuString s = "s";
+
+ *n_lins = 1;
+ *labels = &s;
+ break;
+ }
default:
gu_impossible();
}
-
}
PGF_API void
@@ -1230,7 +1274,7 @@ pgf_get_tokens(PgfSymbols* syms, uint16_t sym_idx, GuPool* pool)
};
PgfLzr* lzr = pgf_new_lzr(NULL, &flin.funcs, tmp_pool);
- pgf_lzr_linearize_symbols(lzr, NULL, syms, sym_idx);
+ pgf_lzr_linearize_symbols(lzr, gu_null_variant, syms, sym_idx);
while (lzr->funcs != &flin.funcs) {
PgfLzrCache* cache = gu_container(lzr->funcs, PgfLzrCache, funcs);
diff --git a/src/runtime/c/pgf/linearizer.h b/src/runtime/c/pgf/linearizer.h
index 57fad962f..78ac092b6 100644
--- a/src/runtime/c/pgf/linearizer.h
+++ b/src/runtime/c/pgf/linearizer.h
@@ -18,6 +18,7 @@ typedef enum {
PGF_CNC_TREE_APP,
PGF_CNC_TREE_CHUNKS,
PGF_CNC_TREE_LIT,
+ PGF_CNC_TREE_LINDEF
} PgfCncTreeTag;
typedef struct {
@@ -50,6 +51,19 @@ typedef struct {
PgfLiteral lit;
} PgfCncTreeLit;
+typedef struct {
+ PgfCId abs_id;
+
+ PgfCCat* ccat;
+ PgfCncFun* fun;
+ int fid;
+
+ size_t n_vars;
+ PgfPrintContext* context;
+
+ GuString str;
+} PgfCncTreeLinDef;
+
#endif
/// An enumeration of #PgfCncTree trees.
diff --git a/src/runtime/haskell-bind/PGF2.hsc b/src/runtime/haskell-bind/PGF2.hsc
index 895d13ca4..186aa2b31 100644
--- a/src/runtime/haskell-bind/PGF2.hsc
+++ b/src/runtime/haskell-bind/PGF2.hsc
@@ -58,7 +58,7 @@ module PGF2 (-- * PGF
ConcName,Concr,languages,concreteName,languageCode,
-- ** Linearization
- linearize,linearizeAll,tabularLinearize,tabularLinearizeAll,bracketedLinearize,
+ linearize,linearizeAll,tabularLinearize,tabularLinearizeAll,bracketedLinearize,bracketedLinearizeAll,
FId, LIndex, BracketedString(..), showBracketedString, flattenBracketedString,
printName,
@@ -862,6 +862,7 @@ type LIndex = Int
-- mark the beginning and the end of each constituent.
data BracketedString
= Leaf String -- ^ this is the leaf i.e. a single token
+ | BIND -- ^ the surrounding tokens must be bound together
| Bracket CId {-# UNPACK #-} !FId {-# UNPACK #-} !LIndex CId [BracketedString]
-- ^ this is a bracket. The 'CId' is the category of
-- the phrase. The 'FId' is an unique identifier for
@@ -884,11 +885,13 @@ showBracketedString :: BracketedString -> String
showBracketedString = render . ppBracketedString
ppBracketedString (Leaf t) = text t
+ppBracketedString BIND = text "&+"
ppBracketedString (Bracket cat fid index _ bss) = parens (text cat <> colon <> int fid <+> hsep (map ppBracketedString bss))
-- | Extracts the sequence of tokens from the bracketed string
flattenBracketedString :: BracketedString -> [String]
flattenBracketedString (Leaf w) = [w]
+flattenBracketedString BIND = []
flattenBracketedString (Bracket _ _ _ _ bss) = concatMap flattenBracketedString bss
bracketedLinearize :: Concr -> Expr -> [BracketedString]
@@ -906,27 +909,8 @@ bracketedLinearize lang e = unsafePerformIO $
return []
else do ctree <- pgf_lzr_wrap_linref ctree pl
ref <- newIORef ([],[])
- allocaBytes (#size PgfLinFuncs) $ \pLinFuncs ->
- alloca $ \ppLinFuncs -> do
- fptr_symbol_token <- wrapSymbolTokenCallback (symbol_token ref)
- fptr_begin_phrase <- wrapPhraseCallback (begin_phrase ref)
- fptr_end_phrase <- wrapPhraseCallback (end_phrase ref)
- fptr_symbol_ne <- wrapSymbolNonExistCallback (symbol_ne exn)
- fptr_symbol_meta <- wrapSymbolMetaCallback (symbol_meta ref)
- (#poke PgfLinFuncs, symbol_token) pLinFuncs fptr_symbol_token
- (#poke PgfLinFuncs, begin_phrase) pLinFuncs fptr_begin_phrase
- (#poke PgfLinFuncs, end_phrase) pLinFuncs fptr_end_phrase
- (#poke PgfLinFuncs, symbol_ne) pLinFuncs fptr_symbol_ne
- (#poke PgfLinFuncs, symbol_bind) pLinFuncs nullPtr
- (#poke PgfLinFuncs, symbol_capit) pLinFuncs nullPtr
- (#poke PgfLinFuncs, symbol_meta) pLinFuncs fptr_symbol_meta
- poke ppLinFuncs pLinFuncs
- pgf_lzr_linearize (concr lang) ctree 0 ppLinFuncs pl
- freeHaskellFunPtr fptr_symbol_token
- freeHaskellFunPtr fptr_begin_phrase
- freeHaskellFunPtr fptr_end_phrase
- freeHaskellFunPtr fptr_symbol_ne
- freeHaskellFunPtr fptr_symbol_meta
+ withBracketLinFuncs ref exn $ \ppLinFuncs ->
+ pgf_lzr_linearize (concr lang) ctree 0 ppLinFuncs pl
failed <- gu_exn_is_raised exn
if failed
then do is_nonexist <- gu_exn_caught exn gu_exn_type_PgfLinNonExist
@@ -935,6 +919,65 @@ bracketedLinearize lang e = unsafePerformIO $
else throwExn exn
else do (_,bs) <- readIORef ref
return (reverse bs)
+
+bracketedLinearizeAll :: Concr -> Expr -> [[BracketedString]]
+bracketedLinearizeAll lang e = unsafePerformIO $
+ withGuPool $ \pl ->
+ do exn <- gu_new_exn pl
+ cts <- pgf_lzr_concretize (concr lang) (expr e) exn pl
+ failed <- gu_exn_is_raised exn
+ if failed
+ then do touchExpr e
+ throwExn exn
+ else do ref <- newIORef ([],[])
+ bss <- withBracketLinFuncs ref exn $ \ppLinFuncs ->
+ collect ref cts ppLinFuncs exn pl
+ touchExpr e
+ return bss
+ where
+ collect ref cts ppLinFuncs exn pl = withGuPool $ \tmpPl -> do
+ ctree <- alloca $ \ptr -> do gu_enum_next cts ptr tmpPl
+ peek ptr
+ if ctree == nullPtr
+ then return []
+ else do ctree <- pgf_lzr_wrap_linref ctree pl
+ pgf_lzr_linearize (concr lang) ctree 0 ppLinFuncs pl
+ failed <- gu_exn_is_raised exn
+ if failed
+ then do is_nonexist <- gu_exn_caught exn gu_exn_type_PgfLinNonExist
+ if is_nonexist
+ then collect ref cts ppLinFuncs exn pl
+ else throwExn exn
+ else do (_,bs) <- readIORef ref
+ writeIORef ref ([],[])
+ bss <- collect ref cts ppLinFuncs exn pl
+ return (reverse bs : bss)
+
+withBracketLinFuncs ref exn f =
+ allocaBytes (#size PgfLinFuncs) $ \pLinFuncs ->
+ alloca $ \ppLinFuncs -> do
+ fptr_symbol_token <- wrapSymbolTokenCallback (symbol_token ref)
+ fptr_begin_phrase <- wrapPhraseCallback (begin_phrase ref)
+ fptr_end_phrase <- wrapPhraseCallback (end_phrase ref)
+ fptr_symbol_ne <- wrapSymbolNonExistCallback (symbol_ne exn)
+ fptr_symbol_bind <- wrapSymbolBindCallback (symbol_bind ref)
+ fptr_symbol_meta <- wrapSymbolMetaCallback (symbol_meta ref)
+ (#poke PgfLinFuncs, symbol_token) pLinFuncs fptr_symbol_token
+ (#poke PgfLinFuncs, begin_phrase) pLinFuncs fptr_begin_phrase
+ (#poke PgfLinFuncs, end_phrase) pLinFuncs fptr_end_phrase
+ (#poke PgfLinFuncs, symbol_ne) pLinFuncs fptr_symbol_ne
+ (#poke PgfLinFuncs, symbol_bind) pLinFuncs fptr_symbol_bind
+ (#poke PgfLinFuncs, symbol_capit) pLinFuncs nullPtr
+ (#poke PgfLinFuncs, symbol_meta) pLinFuncs fptr_symbol_meta
+ poke ppLinFuncs pLinFuncs
+ res <- f ppLinFuncs
+ freeHaskellFunPtr fptr_symbol_token
+ freeHaskellFunPtr fptr_begin_phrase
+ freeHaskellFunPtr fptr_end_phrase
+ freeHaskellFunPtr fptr_symbol_ne
+ freeHaskellFunPtr fptr_symbol_bind
+ freeHaskellFunPtr fptr_symbol_meta
+ return res
where
symbol_token ref _ c_token = do
(stack,bs) <- readIORef ref
@@ -957,17 +1000,22 @@ bracketedLinearize lang e = unsafePerformIO $
gu_exn_raise exn gu_exn_type_PgfLinNonExist
return ()
+ symbol_bind ref _ = do
+ (stack,bs) <- readIORef ref
+ writeIORef ref (stack,BIND : bs)
+ return ()
+
symbol_meta ref _ meta_id = do
(stack,bs) <- readIORef ref
writeIORef ref (stack,Leaf "?" : bs)
- throwExn exn = do
- is_exn <- gu_exn_caught exn gu_exn_type_PgfExn
- if is_exn
- then do c_msg <- (#peek GuExn, data.data) exn
- msg <- peekUtf8CString c_msg
- throwIO (PGFError msg)
- else do throwIO (PGFError "The abstract tree cannot be linearized")
+throwExn exn = do
+ is_exn <- gu_exn_caught exn gu_exn_type_PgfExn
+ if is_exn
+ then do c_msg <- (#peek GuExn, data.data) exn
+ msg <- peekUtf8CString c_msg
+ throwIO (PGFError msg)
+ else do throwIO (PGFError "The abstract tree cannot be linearized")
alignWords :: Concr -> Expr -> [(String, [Int])]
alignWords lang e = unsafePerformIO $
diff --git a/src/runtime/haskell-bind/PGF2/FFI.hsc b/src/runtime/haskell-bind/PGF2/FFI.hsc
index c33f1da50..39b18fcf3 100644
--- a/src/runtime/haskell-bind/PGF2/FFI.hsc
+++ b/src/runtime/haskell-bind/PGF2/FFI.hsc
@@ -340,6 +340,7 @@ foreign import ccall "pgf/pgf.h pgf_lzr_get_table"
type SymbolTokenCallback = Ptr (Ptr PgfLinFuncs) -> CString -> IO ()
type PhraseCallback = Ptr (Ptr PgfLinFuncs) -> CString -> CInt -> CSizeT -> CString -> IO ()
type NonExistCallback = Ptr (Ptr PgfLinFuncs) -> IO ()
+type BindCallback = Ptr (Ptr PgfLinFuncs) -> IO ()
type MetaCallback = Ptr (Ptr PgfLinFuncs) -> CInt -> IO ()
foreign import ccall "wrapper"
@@ -352,6 +353,9 @@ foreign import ccall "wrapper"
wrapSymbolNonExistCallback :: NonExistCallback -> IO (FunPtr NonExistCallback)
foreign import ccall "wrapper"
+ wrapSymbolBindCallback :: BindCallback -> IO (FunPtr BindCallback)
+
+foreign import ccall "wrapper"
wrapSymbolMetaCallback :: MetaCallback -> IO (FunPtr MetaCallback)
foreign import ccall "pgf/pgf.h pgf_align_words"
diff --git a/src/runtime/typescript/gflib.d.ts b/src/runtime/typescript/gflib.d.ts
new file mode 100644
index 000000000..4249e66d2
--- /dev/null
+++ b/src/runtime/typescript/gflib.d.ts
@@ -0,0 +1,337 @@
+/**
+ * gflib.dt.s
+ *
+ * by John J. Camilleri
+ *
+ * TypeScript type definitions for the "original" JS GF runtime (GF:src/runtime/javascript/gflib.js)
+ */
+
+// Note: the String prototype is extended with:
+// String.prototype.tag = "";
+// String.prototype.setTag = function (tag) { this.tag = tag; };
+
+/**
+ * A GF grammar is one abstract and multiple concretes
+ */
+declare class GFGrammar {
+ abstract: GFAbstract
+ concretes: {[key: string]: GFConcrete}
+
+ constructor(abstract: GFAbstract, concretes: {[key: string]: GFConcrete})
+
+ translate(
+ input: string,
+ fromLang: string,
+ toLang: string
+ ): {[key: string]: {[key: string]: string}}
+}
+
+/**
+ * Abstract Syntax Tree
+ */
+declare class Fun {
+ name: string
+ args: Fun[]
+
+ constructor(name: string, ...args: Fun[])
+
+ print(): string
+ show(): string
+ getArg(i: number): Fun
+ setArg(i: number, c: Fun): void
+ isMeta(): boolean
+ isComplete(): boolean
+ isLiteral(): boolean
+ isString(): boolean
+ isInt(): boolean
+ isFloat(): boolean
+ isEqual(obj: any): boolean
+}
+
+/**
+ * Abstract syntax
+ */
+declare class GFAbstract {
+ startcat: string
+ types: {[key: string]: Type} // key is function name
+
+ constructor(startcat: string, types: {[key: string]: Type})
+
+ addType(fun: string, args: string[], cat: string): void
+ getArgs(fun: string): string[]
+ getCat(fun: string): string
+ annotate(tree: Fun, type: string): Fun
+ handleLiterals(tree: Fun, type: Type): Fun
+ copyTree(x: Fun): Fun
+ parseTree(str: string, type: string): Fun
+ parseTree_(tokens: string[], prec: number): Fun
+}
+
+/**
+ * Type
+ */
+declare class Type {
+ args: string[]
+ cat: string
+
+ constructor(args: string[], cat: string)
+}
+
+type ApplyOrCoerce = Apply | Coerce
+
+/**
+ * Concrete syntax
+ */
+declare class GFConcrete {
+ flags: {[key: string]: string}
+ productions: {[key: number]: ApplyOrCoerce[]}
+ functions: CncFun[]
+ sequences: Array<Array<Sym>>
+ startCats: {[key: string]: {s: number, e: number}}
+ totalFIds: number
+ pproductions: {[key: number]: ApplyOrCoerce[]}
+ lproductions: {[key: string]: {fid: FId, fun: CncFun}}
+
+ constructor(
+ flags: {[key: string]: string},
+ productions: {[key: number]: ApplyOrCoerce[]},
+ functions: CncFun[],
+ sequences: Array<Array<Sym>>,
+ startCats: {[key: string]: {s: number, e: number}},
+ totalFIds: number
+ )
+
+ linearizeSyms(tree: Fun, tag: string): Array<{fid: FId, table: any}>
+ syms2toks(syms: Sym[]): string[]
+ linearizeAll(tree: Fun): string[]
+ linearize(tree: Fun): string
+ tagAndLinearize(tree: Fun): string[]
+ unlex(ts: string): string
+ tagIt(obj: any, tag: string): any
+ // showRules(): string // Uncaught TypeError: Cannot read property 'length' of undefined at gflib.js:451
+ tokenize(string: string): string[]
+ parseString(string: string, cat: string): Fun[]
+ complete(
+ input: string,
+ cat: string
+ ): {consumed: string[], suggestions: string[]}
+}
+
+/**
+ * Function ID
+ */
+type FId = number
+
+/**
+ * Apply
+ */
+declare class Apply {
+ id: string
+ fun: FId
+ args: PArg[]
+
+ constructor(fun: FId, args: PArg[])
+
+ show(cat: string): string
+ isEqual(obj: any): boolean
+}
+
+/**
+ * PArg
+ */
+declare class PArg {
+ fid: FId
+ hypos: any[]
+
+ constructor(fid: FId, ...hypos: any[])
+}
+
+/**
+ * Coerce
+ */
+declare class Coerce {
+ id: string
+ arg: FId
+
+ constructor(arg: FId)
+
+ show(cat: string): string
+}
+
+/**
+ * Const
+ */
+declare class Const {
+ id: string
+ lit: Fun
+ toks: any[]
+
+ constructor(lit: Fun, toks: any[])
+
+ show(cat: string): string
+ isEqual(obj: any): boolean
+}
+
+/**
+ * CncFun
+ */
+declare class CncFun {
+ name: string
+ lins: FId[]
+
+ constructor(name: string, lins: FId[])
+}
+
+type Sym = SymCat | SymKS | SymKP | SymLit
+
+/**
+ * SymCat
+ */
+declare class SymCat {
+ id: string
+ i: number
+ label: number
+
+ constructor(i: number, label: number)
+
+ getId(): string
+ getArgNum(): number
+ show(): string
+}
+
+/**
+ * SymKS
+ */
+declare class SymKS {
+ id: string
+ tokens: string[]
+
+ constructor(...tokens: string[])
+
+ getId(): string
+ show(): string
+}
+
+/**
+ * SymKP
+ */
+declare class SymKP {
+ id: string
+ tokens: string[]
+ alts: Alt[]
+
+ constructor(tokens: string[], alts: Alt[])
+
+ getId(): string
+ show(): string
+}
+
+/**
+ * Alt
+ */
+declare class Alt {
+ tokens: string[]
+ prefixes: string[]
+
+ constructor(tokens: string[], prefixes: string[])
+}
+
+/**
+ * SymLit
+ */
+declare class SymLit {
+ id: string
+ i: number
+ label: number
+
+ constructor(i: number, label: number)
+
+ getId(): string
+ show(): string
+}
+
+/**
+ * Trie
+ */
+declare class Trie {
+ value: any
+ items: Trie[]
+
+ insertChain(keys, obj): void
+ insertChain1(keys, obj): void
+ lookup(key, obj): any
+ isEmpty(): boolean
+}
+
+/**
+ * ParseState
+ */
+declare class ParseState {
+ concrete: GFConcrete
+ startCat: string
+ items: Trie
+ chart: Chart
+
+ constructor(concrete: GFConcrete, startCat: string)
+
+ next(token: string): boolean
+ complete(correntToken: string): Trie
+ extractTrees(): any[]
+ process(
+ agenda,
+ literalCallback: (fid: FId) => any,
+ tokenCallback: (tokens: string[], item: any) => any
+ ): void
+}
+
+/**
+ * Chart
+ */
+declare class Chart {
+ active: any
+ actives: {[key: number]: any}
+ passive: any
+ forest: {[key: number]: ApplyOrCoerce[]}
+ nextId: number
+ offset: number
+
+ constructor(concrete: GFConcrete)
+
+ lookupAC(fid: FId,label)
+ lookupACo(offset, fid: FId, label)
+
+ labelsAC(fid: FId)
+ insertAC(fid: FId, label, items): void
+
+ lookupPC(fid: FId, label, offset)
+ insertPC(fid1: FId, label, offset, fid2: FId): void
+ shift(): void
+ expandForest(fid: FId): any[]
+}
+
+/**
+ * ActiveItem
+ */
+declare class ActiveItem {
+ offset: number
+ dot: number
+ fun: CncFun
+ seq: Array<Sym>
+ args: PArg[]
+ fid: FId
+ lbl: number
+
+ constructor(
+ offset: number,
+ dot: number,
+ fun: CncFun,
+ seq: Array<Sym>,
+ args: PArg[],
+ fid: FId,
+ lbl: number
+ )
+
+ isEqual(obj: any): boolean
+ shiftOverArg(i: number, fid: FId): ActiveItem
+ shiftOverTokn(): ActiveItem
+}