MultiKit Parser is a code preprocessor library for making static preprocessor substitutions, made with Kotlin/JS for Node.js.
Many cross-platform toolkits inject large runtime libraries to run application code, but cross-platform development shouldn't come at the cost of runtime overhead. MultiKit statically preprocesses application code and outputs code that directly uses native platform features - no runtime libraries.
This is the MultiKit parser library for use in preprocessor modules. By using this library, preprocessor tools can easily be made to substitute certain function calls with other code that directly uses native features. The same code can be passed through different preprocessor modules to produce code for different environments.
For example, with an HTTP preprocessor module for JavaScript:
MultiKit.HTTP.request('GET', myURL).then(response =>
console.log(MultiKit.HTTP.Response.getStatusCode(response))
)
might translate into the following for Node.js:
if(!globalThis.__MultiKit)
globalThis.__MultiKit = {}
if(!__MultiKit.HTTP)
__MultiKit.HTTP = {}
if(!__MultiKit.HTTP.https)
__MultiKit.HTTP.https = require('https')
new Promise((resolve, reject) => {
try {
__MultiKit.HTTP.https.request(myURL, {method: 'GET'}).on('response', resolve).end()
} catch(e) {
reject(e)
}
}).then(response =>
console.log(response.statusCode)
)
or, for browser:
fetch(myURL, {method: 'GET'}).then(response =>
console.log(response.status)
)
Follow the official guide to make a Kotlin/JS project and target Node.js.
Add the following line to gradle.properties
if it is not already set:
kotlin.js.compiler=ir
Run git clone --depth=1 --single-branch https://github.com/universe-software/multikit-parser.git
to put the multikit-parser
directory in the root of your project directory. (Or use Git submodules if your project is a Git repository.)
In settings.gradle(.kts), include the multikit-parser
project:
includeBuild("multikit-parser")
includeBuild 'multikit-parser'
Add an implementation dependency on universe.multikit:parser:1.0.0
:
dependencies {
implementation("universe.multikit:parser:1.0.0")
}
dependencies {
implementation 'universe.multikit:parser:1.0.0'
}
import universe.multikit.parser.parse
fun main() {
parse("YourNamespace", mapOf(
// ...
))
}
-
prefix
: The namespace prefix of calls the parser should try to substitute. For example, if the prefix isMultiKit
, the parser will try to substitute calls in the form ofMultiKit....()
. -
substitutions
: Map of preprocessor function names to functions that compute the replacement output. A substitution function will be called when the parser encounters that function name (which must start with the prefix and a dot) passing the parameters of the call as an array of strings. The returned string will replace the call in the output.
When called, parse
will read all input from the standard input stream and then begin preprocessing. When done, it will print the output to the standard output stream.