Skip to content

Commit 3905e36

Browse files
nickpdemarcoHKalbasi
authored andcommitted
Add documentation to book
1 parent ba2ac49 commit 3905e36

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [Writing `impl` blocks for Rust types in C++](./call_cpp_from_rust/rust_impl.md)
1515
- [`Box<dyn Fn>`]()
1616
- [`Opaque C++ types`](./call_cpp_from_rust/opaque.md)
17+
- [Import](./import.md)
1718
- [Safety](./safety.md)
1819
- [How it compares to other tools](./how_it_compares.md)
1920
- [Design decisions](./philosophy.md)

book/src/import.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Import
2+
3+
The `import` directive allows you to include type definitions and other declarations from other `.zng` files into your main specification. Types, traits, and modules can appear multiple times across the transitive set of imported files, and their content is merged together.
4+
5+
## Syntax
6+
7+
```zng
8+
import "./path/to/file.zng";
9+
```
10+
11+
## Path Resolution
12+
13+
Import paths are resolved relative to the directory containing the current `.zng` file:
14+
15+
- `import "./types.zng";`
16+
17+
At this time, absolute paths are not supported.
18+
Importing paths without a leading specified (e.g. `import "foo/bar.zng";`) is reserved for a possible future extension.
19+
20+
Above, "the current zng file" refers to the `.zng` file being parsed, which is not necessarily the top-level `.zng` file passed to `zngur` on the command line.
21+
22+
## Behavior
23+
24+
When an import statement is processed:
25+
26+
1. The parser reads and parses the imported file
27+
2. All declarations from the imported file are _merged_ into the current specification
28+
3. Imported content becomes available as if it were defined in the importing file
29+
4. Import processing happens recursively - imported files can themselves contain import statements
30+
31+
## Merging
32+
33+
Zngur's merge algorithm attempts to compute the union of each set of declarations which share an identity (e.g. every `type crate::Inventory { ... }` across all imported files). Duplicates are ignored, but contradictions will raise a compiler error. For example, if two different `type crate::Inventory { ... }` declarations both specify `wellknown_traits(Debug);`, parsing will succeed. However, if they specify different layouts, an error will be reported.
34+
35+
## `#convert_panic_to_exception` constraints
36+
37+
`#convert_panic_to_exception` may only appear in a top-level `.zng` file. This is an application-level decision that should not be determined by dependent libraries.
38+
39+
## Example
40+
41+
**main.zng:**
42+
```zng
43+
import "./core_types.zng";
44+
import "./iterators.zng";
45+
46+
// May only appear in the top-level file.
47+
#convert_panic_to_exception
48+
49+
type MyApp {
50+
#layout(size = 8, align = 8);
51+
52+
fn run(&self) -> i32;
53+
}
54+
```
55+
56+
**core_types.zng:**
57+
```zng
58+
mod ::std {
59+
type option::Option<i32> {
60+
#layout(size = 8, align = 4);
61+
wellknown_traits(Copy);
62+
63+
constructor None;
64+
constructor Some(i32);
65+
66+
fn unwrap(self) -> i32;
67+
}
68+
69+
mod vec {
70+
type Vec<i32> {
71+
#layout(size = 24, align = 8);
72+
fn new() -> Vec<i32>;
73+
fn push(&mut self, i32);
74+
}
75+
}
76+
}
77+
```
78+
79+
**iterators.zng:**
80+
```zng
81+
mod ::std {
82+
mod vec {
83+
type Vec<i32> {
84+
fn into_iter(self) -> ::std::vec::IntoIter<i32>;
85+
}
86+
}
87+
}
88+
```
89+
90+
In this example, `main.zng` imports type definitions from two separate files, allowing for better organization of the zngur specification.
91+
92+
Notice that `iterators.zng` is able to "reopen" the `::std::vec::Vec<i32>` specification and extend it with a single function, `into_iter`. It does not need to respecify the `#layout` because that is already declared in `core_types.zng`.

0 commit comments

Comments
 (0)