2020-06-25 15:06:08 +03:00
|
|
|
---
|
|
|
|
layout: developer-doc
|
2020-06-26 16:54:20 +03:00
|
|
|
title: AST
|
2020-06-25 15:06:08 +03:00
|
|
|
category: parser
|
|
|
|
tags: [parser, ast]
|
2020-10-30 17:06:24 +03:00
|
|
|
order: 8
|
2020-06-25 15:06:08 +03:00
|
|
|
---
|
|
|
|
|
2020-06-26 16:54:20 +03:00
|
|
|
# AST
|
2020-07-21 15:59:40 +03:00
|
|
|
|
2020-06-26 16:54:20 +03:00
|
|
|
The parser AST describes the high-level syntactic structure of Enso, as well as
|
|
|
|
containing robust and descriptive parser errors directly in the AST.
|
2020-06-25 15:06:08 +03:00
|
|
|
|
|
|
|
<!-- MarkdownTOC levels="2,3" autolink="true" -->
|
|
|
|
|
2020-06-26 16:54:20 +03:00
|
|
|
- [Functionality](#functionality)
|
2020-07-31 08:31:12 +03:00
|
|
|
- [Generation](#generation)
|
2020-06-26 16:54:20 +03:00
|
|
|
|
2020-06-25 15:06:08 +03:00
|
|
|
<!-- /MarkdownTOC -->
|
2020-06-26 16:54:20 +03:00
|
|
|
|
|
|
|
## Functionality
|
2020-07-21 15:59:40 +03:00
|
|
|
|
2020-06-26 16:54:20 +03:00
|
|
|
The parser AST needs to account for the following:
|
|
|
|
|
|
|
|
- A single `Name` type, removing the distinction between different names found
|
|
|
|
in the [lexer](./lexer.md). This should provide functions `is_var`, `is_opr`,
|
|
|
|
and `is_ref`.
|
|
|
|
- It should contain all of the language constructs that may appear in Enso's
|
|
|
|
source.
|
|
|
|
- It should contain `Invalid` nodes, but these should be given a descriptive
|
|
|
|
error as to _why_ the construct is invalid.
|
|
|
|
- It should also contain `Ambiguous` nodes, where a macro cannot be resolved in
|
|
|
|
an unambiguous fashion.
|
|
|
|
|
|
|
|
Each node should contain:
|
|
|
|
|
|
|
|
- An identifier, attributed to it from the ID map.
|
|
|
|
- The start source position of the node, and the length (span) of the node.
|
|
|
|
|
|
|
|
> The actionables for this section are:
|
|
|
|
>
|
|
|
|
> - Flesh out the design for the AST based on the requirements of the various
|
|
|
|
> parser phases.
|
2020-07-31 08:31:12 +03:00
|
|
|
|
|
|
|
## Generation
|
|
|
|
|
|
|
|
The single source of truth for the AST is its Rust implementation. Therefore, in
|
|
|
|
order to not get out of sync, the Scala AST implementation is generated during
|
|
|
|
compilation directly from the Rust ast source file.
|
|
|
|
|
|
|
|
The command for generating the Scala ast and storing it in the file
|
|
|
|
`foo/ast.scala` is following:
|
|
|
|
`cargo run -p ast -- --generate-scala-ast foo/ast.scala`.
|
|
|
|
|
|
|
|
Since there isn't 1:1 mapping between Rust and Scala, only a subset of Rust's
|
|
|
|
structures is supported. These are follows.
|
|
|
|
|
|
|
|
##### Primitives
|
|
|
|
|
|
|
|
```
|
|
|
|
u32 | i32 | u16 | i16 | i8 => Int,
|
|
|
|
usize | isize | u64 | i64 => Long,
|
|
|
|
u8 => Byte,
|
|
|
|
char => Char,
|
|
|
|
Vec => Vector,
|
2020-08-03 17:00:12 +03:00
|
|
|
Uuid => UUID,
|
2020-07-31 08:31:12 +03:00
|
|
|
```
|
|
|
|
|
2020-08-10 13:14:39 +03:00
|
|
|
> Note: It is assumed, that Enso runs on 64bit platforms. Therefore, `usize` and
|
|
|
|
> `isize` are converted to `Long`.
|
2020-07-31 08:31:12 +03:00
|
|
|
|
|
|
|
##### Structures With Named Fields
|
|
|
|
|
|
|
|
```
|
|
|
|
struct Foo<X> { x: X<z::Y>, .. }
|
|
|
|
```
|
|
|
|
|
|
|
|
Is converted into:
|
|
|
|
|
|
|
|
```
|
|
|
|
case class Foo[X](x: X[Z.Y], ..)
|
|
|
|
```
|
|
|
|
|
|
|
|
##### Enums With Named Fields
|
|
|
|
|
|
|
|
```
|
|
|
|
enum Foo{ Bar{x:X}, Baz{y:Y, z:Z} }
|
|
|
|
```
|
|
|
|
|
|
|
|
Is converted into:
|
|
|
|
|
|
|
|
```
|
|
|
|
sealed trait Foo
|
|
|
|
case class Bar(x:X) extends Foo
|
|
|
|
case class Baz(y:Y, z:Z) extends Foo
|
|
|
|
```
|
|
|
|
|
|
|
|
##### Enums With One Unnamed Qualified Field
|
|
|
|
|
|
|
|
```
|
|
|
|
enum Enum { Foo(x::Foo), Bar(x::Bar), Baz(y::Baz) }
|
|
|
|
|
|
|
|
mod x {
|
|
|
|
pub struct Foo { .. }
|
|
|
|
pub struct Bar { .. }
|
|
|
|
}
|
|
|
|
|
|
|
|
mod y {
|
|
|
|
pub struct Baz { .. }
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Is converted into:
|
|
|
|
|
|
|
|
```
|
|
|
|
sealed trait Enum
|
|
|
|
|
|
|
|
object X {
|
|
|
|
sealed trait X extends Enum
|
|
|
|
case class Foo(..) extends X
|
|
|
|
case class Bar(..) extends X
|
|
|
|
}
|
|
|
|
|
|
|
|
object Y {
|
|
|
|
sealed trait Y extends Enum
|
|
|
|
case class Baz(..) extends Y
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
##### Modules
|
|
|
|
|
|
|
|
```
|
|
|
|
mod foo { .. }
|
|
|
|
```
|
|
|
|
|
|
|
|
Is converted into:
|
|
|
|
|
|
|
|
```
|
2020-08-03 17:00:12 +03:00
|
|
|
object Foo { .. }
|
2020-07-31 08:31:12 +03:00
|
|
|
```
|
|
|
|
|
2020-08-03 17:00:12 +03:00
|
|
|
Furthermore, the content of `ast.rs` is wrapped inside additional `object Ast`,
|
|
|
|
in order to support top level type aliases.
|
|
|
|
|
2020-07-31 08:31:12 +03:00
|
|
|
##### Type Aliases
|
|
|
|
|
|
|
|
```
|
|
|
|
type A<X> = B<X,Y>;
|
|
|
|
```
|
|
|
|
|
|
|
|
Is converted into:
|
|
|
|
|
|
|
|
```
|
|
|
|
type A[X] = B[X, Y]
|
|
|
|
```
|
2020-08-03 17:00:12 +03:00
|
|
|
|
|
|
|
Note that in contrast to Rust, Scala doesn't support types outside objects.
|