This repositort contains a TypeScript implementation of the GF runtime.
It is ported from an older JavaScript implementation gflib.js
, with some improvements.
Importantly, all future updates will only be made to this TypeScript version. The JavaScript version mentioned above is now deprecated.
This runtime allows you use GF in pure JavaScript, and thus have GF-powered apps without the need for a server backend. However, it has not been actively maintained as the other runtimes have been. So its features are limited and it is not efficient, making it really only useful for smaller grammars.
Your GF grammar should be compiled into JSON with: gf --make --output-format=json
.
This requires a version of GF later than the 3.10 release.
import { fromJSON, GFGrammar } from 'gf-typescript'
import { readFileSync } from 'fs'
let json = JSON.parse(readFileSync('./test/grammars/Zero.json').toString())
let grammar: GFGrammar | null = fromJSON(json)
Using ES6 modules:
<script type="module">
import { fromJSON } from 'dist/index.js'
let xhr = new XMLHttpRequest()
xhr.open('GET', 'test/grammars/Zero.json')
xhr.onload = function () {
if (xhr.status === 200) {
let json = JSON.parse(xhr.responseText)
let grammar = fromJSON(json)
}
}
xhr.send()
</script>
Compilation of GF to JavaScript will soon be deprecated, but these instructions are here in case you can't/won't use the latest GF.
Your GF grammar should be compiled into JavaScript with: gf --make --output-format=js
The resulting JavaScript grammar file needs to be modified from:
var Zero = new GFGrammar(...)
to:
import {
GFGrammar,
GFAbstract,
GFConcrete,
Fun,
Type,
Apply,
Coerce,
PArg,
Const,
CncFun,
SymCat,
SymKS,
SymKP,
SymLit,
Alt,
} from '../../dist/index' // assuming it's in test/grammars
export default new GFGrammar(...)
You can then use the grammar like so:
import grammar from './test/grammars/Zero.js'
To avoid using modules, you need to comment out the export
statements from the runtime (dist/index.js
):
/**
* Module exports
*/
// export { GFGrammar, GFAbstract, GFConcrete, Fun, Type, Apply, Coerce, PArg, Const, CncFun, SymCat, SymKS, SymKP, SymLit, Alt, };
Then you can import both runtime and grammar into the global namespace just like in the good old days:
<script src="dist/index.js"></script>
<script src="test/grammars/Zero.js"></script>
<script>
Zero.abstract.parseTree(...)
</script>
dist
: compiled for use in browserlib
: compiled for use as Node module (without TypeScript)src
: TypeScript sourcestest
: test grammars and scripts