TypeScript: Langium 4.0 brings infix operators for more compact grammar
The TypeScript framework for custom languages brings new grammar functions such as infix notation, faster parsing and features for complex DSL projects.
(Image: jakkaje879/Shutterstock.com)
With version 4.0, the open source project Langium has released a range of new functions, including support for infix operators. This extension is intended not only to simplify the definition of complex expressions, but also to improve parsing performance. Langium is an Eclipse framework for developing domain-specific languages (DSLs) in TypeScript and offers integrated support for the Language Server Protocol (LSP).
More compact grammar with infix notation
Until now, developers had to define binary expressions such as additions, comparisons or logical operations using so-called tree structure transformations. According to the development team, the new infix notation is a more compact way of expressing such operators and is also easier to read and understand.
Infix notation refers to the representation of operators between their operands, for example in the notation a + b. This form is familiar from programming languages such as JavaScript or Python and corresponds to standard mathematical notation. In contrast to this are prefix or postfix notations, in which operators are placed before or after the operands.
A simple example from the changelog illustrates the new syntax:
infix BinaryExpression on PrimaryExpression:
'%' // <-- Highest precedence
> '^'
> '*' | '/'
> '+' | '-'; // <-- Lowest precedence
PrimaryExpression infers Expression:
'(' Expression ')' |
{infer NumberLiteral} value=NUMBER;
The new notation is apparently not only syntactically more elegant, but is also touted as being more efficient: When parsing such expressions, it is said to be up to 50 percent faster than the previous approach. This information is based on internal measurements by the development team. The previous method will remain available, allowing a gradual changeover.
Videos by heise
Langium as a construction kit for custom languages
The Eclipse project Langium is aimed at developers who want to design their own textual languages and integrate them with development tools such as Visual Studio Code. It combines language definition, AST generation and language servers in one toolset. In contrast to classic parser generators such as ANTLR (ANother Tool for Language Recognition), Langium is written natively in TypeScript and therefore enables closer integration with modern web toolchains.
In addition to infix notation, Langium offers other new features such as multi-references and a strict mode for grammar definitions. According to the announcement article, it is increasingly being used for complex projects, for example for modeling requirements, configuration languages or domain-specific tools.
Further information can be found in the changelog on GitHub.
(mdo)