Fastbelt 0.2: Picking up speed
Five months ago, we published the first version of Fastbelt, our language engineering framework for Go. Since then, we’ve been continuously adding new features to the framework. As a result, Fastbelt 0.2 ships with multi-language support, infix rules, token modes and semantic tokens. On top of that, we’ve added JSON serialization for ASTs, railroad diagrams and another round of performance work.
In the meantime, Fastbelt has also found a new home! The project now lives at the Eclipse Foundation, and the repository has moved to eclipse-fastbelt/fastbelt.
So let’s dig into what’s new!
Infix rules
Top-down parsers are infamously bad at parsing infix expressions. Every precedence level needs its own parser rule, and every expression, no matter how simple, has to walk through the whole parser rule chain. With Fastbelt 0.2, we’ve adopted the infix rule syntax that we introduced in Langium 4.0. You list the operators by precedence, highest first, and name the rule that parses each operand:
infix BinaryExpression on PrimaryExpression:
"%"
> right "^"
> "*" | "/"
> "+" | "-"
PrimaryExpression returns Expression:
"(" Expression ")" |
{NumberLiteral} Value=NUMBER
Operators are left-associative by default. Using the right keyword changes that for a precedence level. Not only is the grammar much shorter and easier to read, the parser also no longer builds a deep call stack for every single operand. In our benchmarks, this improves parsing performance by up to 20% on expression-heavy grammars.
Token modes
Some languages just can’t be tokenized with a single set of rules. Template strings are a classic example. Inside the string, almost everything is plain text, until an interpolation starts and the rules of the surrounding language apply again. Token modes (known to ANTLR users as lexer modes) solve this with a stack of modes that the lexer pushes and pops while it reads the input:
token mode default {
"`" -> push(IN_STRING)
ID
INT
"+"
hidden WS
}
token mode IN_STRING {
"`" -> pop
token STRING_CONTENT: /([^`#\\]|\\[`#tnr\\])+/
"#{" -> push(IN_INTERPOLATION)
}
token mode IN_INTERPOLATION {
"}" -> pop
"`" -> push(IN_STRING)
ID
INT
"+"
hidden WS
}
Each mode lists the tokens and keywords that are valid in it, in order of precedence. When a token matches, the commands push, pop and mode will change the active mode. If your grammar doesn’t use any token modes, nothing changes for you, since Fastbelt creates an implicit mode from all tokens and keywords of the grammar.
Multiple languages in one language server
Larger language engineering projects rarely consist of a single language. With Fastbelt 0.2, one build can produce multiple languages that share a workspace, a symbol index and a language server. Therefore, cross-references between them resolve just like any other reference.
As a consequence, a grammar can now be split across multiple .fb files in one directory, and entry rules have to be marked explicitly with the entry keyword:
entry Greeting: "hello" Name=ID
entry Farewell: "goodbye" To=[Greeting:ID]
While the fastbelt CLI still covers the single-language case, the new typefox.dev/fastbelt/cmd package exposes the build as an API for multiple languages. You write a very small generator program and call it from go generate:
ctx := &cmd.BuildContext{
Languages: []cmd.Language{
{
Entry: "Greeting",
LanguageID: "greeting",
Patterns: []string{"*.hello"},
},
{
Entry: "Farewell",
LanguageID: "farewell",
Patterns: []string{"*.bye"},
},
},
}
if err := ctx.Build(); err != nil {
log.Fatal(err)
}
Introducing multiple languages to Langium massively increased the complexity of its dependency injection mechanism. To keep dependency injection in Fastbelt as simple as possible, we opted for a solution that uses just a single dependency injection container for all languages. Instead, a LanguageSelector service decides which language a document belongs to, based on its language ID and the file patterns. Certain services, such as the lexer and parser, then dispatch based on the current language at runtime.
JSON serialization
Fastbelt now generates MarshalJSONTo and UnmarshalJSONFrom methods for all AST types, built on Go’s new encoding/json/v2 package. With that, an AST can be exported to JSON and imported again, including its cross-references, even those that point into other documents. Having a plain exchange format like this makes it possible to share models with tools that know absolutely nothing about your grammar. In particular, the implementation is completely compatible with Langium’s JSONSerializer.
Semantic tokens and more LSP services
Adopters of language engineering frameworks expect a large amount of LSP functionality out of the box. We try to deliver on that and added new provider interfaces for call hierarchy, type hierarchy, code actions, code lens, commands, declaration, type definition, implementation, document links, inlay hints and signature help.
In addition to that, new utility services have been introduced for more complicated features, such as semantic highlighting and the new LSP 3.18 text document content provider.
Closely related to that, improvements in the workspace lock and in build cancellation behavior should result in Fastbelt responding much faster to document updates and LSP client requests.
Railroad diagrams
Often, a picture explains a parser rule much faster than its definition. Running fastbelt generate --railroad writes an SVG railroad diagram for every rule of your grammar. When you hover over a rule in a .fb file, the Fastbelt VS Code extension shows you the same diagram:
Performance
As always, performance remains a first-class feature of Fastbelt, and this release improves on almost every stage of the build pipeline. In the lexer, we now consume runs of ASCII characters with a table lookup. The parser allocates less, and it defers creating AST nodes until it’s clear that they are needed. References take up less memory, linking got faster, and a build starts only as many goroutines as required, reducing the scheduling overhead.
A large share of the performance improvements was contributed by Christian Dietrich. Thank you!
What’s next
We were able to catch up to feature parity with other language engineering frameworks in a lot of areas. For the next release, we’re targeting the last missing features, such as formatting support, before moving on to production readiness.
Getting started
To get started, install the CLI in its new version:
go install typefox.dev/fastbelt/cmd/fastbelt@v0.2
Afterwards, you can scaffold a new project that’s good to go:
fastbelt scaffold --module example.com/you/mylang --language "MyLanguage" --vscode
The README in our GitHub repository has more details, and the arithmetics and statemachine examples show complete language implementations. Curious how we get Fastbelt to be as fast as it is? Our post on Fastbelt’s architecture explains it.
Conclusion
With version 0.2, Fastbelt can handle languages with template strings, languages split across multiple grammar files and projects with more than one language, which wasn’t possible with 0.1. We’d like you to try it, build something with it, and let us know what’s still missing. Head over to our GitHub repository to open issues, join the discussion, or simply follow along.
About the Author
Mark Sujew
Mark is the driving force behind a lot of TypeFox’s open-source engagement. He leads the development of the Eclipse Langium and Theia IDE projects. Away from his day job, he enjoys bartending and music, is an avid Dungeons & Dragons player, and works as a computer science lecturer at a Hamburg University.


