Skip to content

Commit ef8b387

Browse files
author
Sasha Pourcelot
committed
Add inline_* macros and use it in parser tests
When writing parser test, it was super tedious to define the expected AST. The resulting code was ugly and not obvious to the user. We deserve better. This commit introduces a whole bunch of macro whose goal is to parse Rust tokens and produce the corresponding `dyl`-specific AST. It uses only declarative macros and does most of the work at compile time. The goal is to use it in order to be able to generate ASTs for the unit tests all across the codebase. The macro implementation is not trivial - almost unreadable for those who don't know what to expect - but it was fun to write. It deserves a blogpost for sure.
1 parent 0651408 commit ef8b387

9 files changed

Lines changed: 829 additions & 123 deletions

File tree

‎dyl-compiler/Cargo.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
name = "dyl-compiler"
33
version = "0.1.0"
44
authors = ["Sasha Pourcelot <sasha.pourcelot@protonmail.com>"]
5-
edition = "2018"
5+
edition = "2021"
66

77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
1010
dyl-bytecode = { path = "../dyl-bytecode" }
1111
anyhow = "1.0"
1212
nom = { version = "6.1", default-features = false, features = ["std"] }
13-
nom_locate = "3.0"
13+
nom_locate = "3.0"

‎dyl-compiler/src/ast.rs‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,6 @@ impl Program {
1111
pub(crate) fn functions(&self) -> &[Function] {
1212
self.functions.as_slice()
1313
}
14-
15-
#[cfg(test)]
16-
pub(crate) fn just_main(body: ExprKind) -> Program {
17-
let f = Function {
18-
name: "main".to_string(),
19-
body,
20-
};
21-
let functions = vec![f];
22-
23-
Program { functions }
24-
}
2514
}
2615

2716
#[derive(Clone, Debug, PartialEq)]
@@ -42,14 +31,6 @@ impl Function {
4231
pub(crate) fn body(&self) -> &ExprKind {
4332
&self.body
4433
}
45-
46-
#[cfg(test)]
47-
pub(crate) fn main_(body: ExprKind) -> Function {
48-
Function {
49-
name: "main".to_string(),
50-
body,
51-
}
52-
}
5334
}
5435

5536
#[derive(Clone, Debug, PartialEq)]

‎dyl-compiler/src/lib.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ use anyhow::{Context, Result};
44

55
use dyl_bytecode::Instruction;
66

7+
#[cfg(test)]
8+
mod macros;
9+
710
mod ast;
811
mod context;
912
mod instruction;

‎dyl-compiler/src/macros.rs‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//! Dear fellow macro enjoyer,
2+
//!
3+
//! You'll find, once you look at this design
4+
//! A huge piece of very complex code,
5+
//! It takes tokens - all written inline,
6+
//! Turns it into appropriate node.
7+
//!
8+
//! The next few sentences shall explain,
9+
//! What's complex, cursed, or damn simple,
10+
//! So you don't feel lost and get less pain,
11+
//! Once you start diving in this `impl`.
12+
//!
13+
//! You'll see some simple conventions there,
14+
//! So that everything stays consistent,
15+
//! And it's little less than a nightmare,
16+
//! To dive in this desolate content.
17+
//!
18+
//! # Ok Sasha, but I'm looking for the docs
19+
//!
20+
//! The complexity of the inline macros has been split across three different
21+
//! submodules: `expression`, `function` and `program`. The `node` submodule
22+
//! contains free functions aiming to make test AST creation as simple as
23+
//! possible. Let's forget this last module in the rest of the explanation.
24+
//!
25+
//! Each module (we will call it `$mod`) defines at least two macros:
26+
//! `inline_$mod` and `parse_$mod`. `inline_$mod` is the one that should be
27+
//! called directly by the outside user. It calls `parse_$mod` under the hood,
28+
//! and perform some additional conversion. These two macros should take all the
29+
//! tokens inline, with no specific syntax.
30+
//!
31+
//! If the grammar recognized by `parse_$mod` is simple enough, the whole
32+
//! parsing step may happen at `parse_$mod`. Otherwise, `parse_$mod` acts like
33+
//! a simple wrapper over `parse_$mod_inner`, calling it with the appropriate
34+
//! token trees.
35+
//!
36+
//! Declarative macros take tokens trees as argument. We're processing them one
37+
//! token at a time, taking the leftmost one, doing the appropriate
38+
//! transformations and putting it at the end of a buffer, then recursing, until
39+
//! we either meet an interesting condition or have no more tokens to consume.
40+
//! The tokens that are to be consumed are always passed first, enclosed
41+
//! brackets. Then are passed the tokens we have just processed. We sometimes
42+
//! have to specify which state we're in. This state name always starts with an
43+
//! `@` and is matched *even before* the unprocessed tokens. When there's a
44+
//! clear separation, that state can even be put in *another macro*.
45+
46+
pub(crate) mod expression;
47+
pub(crate) mod function;
48+
pub(crate) mod nodes;
49+
pub(crate) mod program;
50+
51+
#[macro_export]
52+
macro_rules! node {
53+
($node_name:ident( $( $args:tt )* )) => {
54+
$crate::macros::nodes::$node_name( $( $args )* )
55+
};
56+
}

0 commit comments

Comments
 (0)