Adding a File Type
Supporting a specific file type at the language level, e.g. adding syntax to easily manipulate an object notation or for serialisation.
Library developers are encouraged to add support for specific file-types at the language level.
An example of this is through the Skript config .csk
file type.
Creating a Special Matcher
In order to support the typical path/to/file.ext
structure, we cannot use the normal pattern system.
In the constructor, we can specify a dummy pattern. This will not be used directly by the matcher.
Currently, this would match only the exact text path/to/file.ext
but we need it to match any (valid) file-path ending in our .ext
file extension.
By overriding the match
method we can handle this behaviour ourselves.
This can be broken down into three key sections.
We make sure the text is what we want (ends in our
.ext
, is a valid file path, etc.)If it's wrong, we make sure the matcher fails.
If it's right, we return a fake "success" matcher.
Handling the File
Since our pattern is non-standard we will need to handle the call manually.
This is done by overriding the compile
method.
The file-path will not be on the stack since it is not an input. Fortunately, it is available to add easily.
So far, we have put only a string onto the stack, but we will likely want either a File
object or some malleable handler object for the file that a user can edit.
Imagine, for example, this object is accessible via a MyFile.get(String)
static method.
Having put our path string onto the stack we can now call this method directly, which will return the file object from our expression.
Last updated