8 releases
0.1.7 | Dec 9, 2023 |
---|---|
0.1.6 | Dec 2, 2023 |
0.1.3 | Nov 30, 2023 |
#1477 in Development tools
87 downloads per month
240KB
2K
SLoC
⚠️ Morgana is still in early stage. ⚠️
Morgana
Morgana is a declarative language that allows users to define the datatypes of the AST of a language together with their syntax rules.
Why?
Most compilers generator tend to overlook the aspect of the generation of the AST, which can be as tedious as writing a parser or a lexer. This formalism aims at providing a single declarative specification that cleanly and concisely allows to represent the nodes of the AST of a language together with their introduction rules, that is, their syntax rules.
Once a specification is in place, the user will be able to generate automatically code for the AST, lexer, parser, pretty printer and hopefully also a skeleton for the LSP. Morgana itself is not a CLI or a library, Morgana is the formalism; ideally, various adoption of the format will allow more generators for spurious objectives.
Consider the following Morg.
-- Simple arithmetic expressions
ast Expressions
node Expression :=
| Num : u32
| Mul : Expression '*' Expression
| Div : Expression '/' Expression
| Sum : Expression '+' Expression
| Min : Expression '-' Expression
Analysing the Morg, a Rust generator (for example) can generate an AST in a form such as
pub enum Expression {
Num(u32),
Mul(Box<Expression>, Box<Expression>),
...
}
together with a parser, automatically. It is clear that this would not be possible
- if we have the objective of automatically giving meaningful names to the nodes of the tree - by using EBNF rules only such as
Expression := Expression '*' Expression
| Expression '/' Expression
| Expression '+' Expression
| Expression '-' Expression
as, simply, no information regarding the structure of the AST is embedded in the rules: we would need another formalism or implement by hand the AST.
Usage
Remember that Morgana is in early stage and we are trying to figure out the best way to do what we plan. In general, the usage is as shown here:
flowchart TD
subgraph Z[" "]
direction LR
lang.morg --> parser
parser --> MorgAst
MorgAst --> AST-IR
MorgAst --> EBNF-IR
AST-IR --> ASTGen
EBNF-IR --> ParserGen
end
where MorgAst
is the internal representation of the structure of the language represented in the metalanguage and AST-IR
and EBNF-IR
are internal representation of the AST of the language as represented in the Morg and of the syntax of the language, respectively.
Dependencies
~2.6–4MB
~75K SLoC