Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ jobs:
- name: Fmt
run: cargo fmt --all -- --files-with-diff --check

- name: Rue fmt
run: cargo run --quiet --package rue-cli -- fmt 'examples/**/*.rue' 'tests/**/*.rue' 'crates/rue-compiler/src/std/**/*.rue' --check

- name: Publish (dry run)
run: |
cargo binstall cargo-workspaces
Expand Down
78 changes: 63 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 12 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,19 @@ match_same_arms = "allow"
similar_names = "allow"
cast_possible_truncation = "allow"
case_sensitive_file_extension_comparisons = "allow"
wildcard_imports = "allow"

[workspace.dependencies]
rue-lexer = { path = "crates/rue-lexer", version = "0.9.0" }
rue-parser = { path = "crates/rue-parser", version = "0.9.0" }
rue-diagnostic = { path = "crates/rue-diagnostic", version = "0.9.0" }
rue-ast = { path = "crates/rue-ast", version = "0.9.0" }
rue-compiler = { path = "crates/rue-compiler", version = "0.9.0" }
rue-options = { path = "crates/rue-options", version = "0.9.0" }
rue-lir = { path = "crates/rue-lir", version = "0.9.0" }
rue-hir = { path = "crates/rue-hir", version = "0.9.0" }
rue-types = { path = "crates/rue-types", version = "0.9.0" }
rue-lexer = { path = "crates/rue-lexer", version = "0.10.0" }
rue-parser = { path = "crates/rue-parser", version = "0.10.0" }
rue-diagnostic = { path = "crates/rue-diagnostic", version = "0.10.0" }
rue-ast = { path = "crates/rue-ast", version = "0.10.0" }
rue-formatter = { path = "crates/rue-formatter", version = "0.10.0" }
rue-compiler = { path = "crates/rue-compiler", version = "0.10.0" }
rue-options = { path = "crates/rue-options", version = "0.10.0" }
rue-lir = { path = "crates/rue-lir", version = "0.10.0" }
rue-hir = { path = "crates/rue-hir", version = "0.10.0" }
rue-types = { path = "crates/rue-types", version = "0.10.0" }
anyhow = "1.0.98"
clvm-traits = "0.28.1"
clvm-utils = "0.28.1"
Expand All @@ -75,6 +77,7 @@ clap = "4.5.45"
rstest = "0.26.1"
log = "0.4.27"
env_logger = "0.11.8"
glob = "0.3.4"
wasm-bindgen = "0.2.102"
console_error_panic_hook = "0.1.7"
sha2 = "0.10.9"
Expand Down
5 changes: 4 additions & 1 deletion crates/rue-ast/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rue-ast"
version = "0.9.0"
version = "0.10.0"
edition = "2024"
license = "Apache-2.0"
description = "An implementation of the Abstract Syntax Tree for the Rue compiler."
Expand All @@ -17,3 +17,6 @@ workspace = true
[dependencies]
rue-parser = { workspace = true }
paste = { workspace = true }

[dev-dependencies]
num-traits = { workspace = true }
84 changes: 82 additions & 2 deletions crates/rue-ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,47 @@ pub trait AstNode {
}

macro_rules! ast_nodes {
($( $kind:ident),+ $(,)?) => { paste! { $(
($( $kind:ident),+ $(,)?) => { paste! {
/// The exhaustive set of syntax kinds with typed AST node wrappers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum AstNodeKind {
$( $kind, )+
}

impl AstNodeKind {
pub const ALL: &'static [Self] = &[
$( Self::$kind, )+
];

pub fn from_syntax(kind: SyntaxKind) -> Option<Self> {
match kind {
$( SyntaxKind::$kind => Some(Self::$kind), )+
_ => None,
}
}

pub fn of(node: &SyntaxNode) -> Option<Self> {
Self::from_syntax(node.kind())
}
}

impl TryFrom<SyntaxKind> for AstNodeKind {
type Error = SyntaxKind;

fn try_from(kind: SyntaxKind) -> Result<Self, Self::Error> {
Self::from_syntax(kind).ok_or(kind)
}
}

impl From<AstNodeKind> for SyntaxKind {
fn from(kind: AstNodeKind) -> Self {
match kind {
$( AstNodeKind::$kind => Self::$kind, )+
}
}
}

$(
#[derive(Debug, Clone)]
pub struct [< Ast $kind >](SyntaxNode);

Expand All @@ -26,7 +66,8 @@ macro_rules! ast_nodes {
&self.0
}
}
)+ } };
)+
} };
}

macro_rules! ast_enum {
Expand Down Expand Up @@ -900,3 +941,42 @@ impl AstStructFieldBinding {
self.syntax().children().find_map(AstBinding::cast)
}
}

#[cfg(test)]
mod tests {
use num_traits::FromPrimitive;

use super::*;

#[test]
fn every_parser_node_kind_has_an_ast_kind() {
let first = SyntaxKind::Document as u16;
let end = SyntaxKind::Error as u16;
assert_eq!(AstNodeKind::ALL.len(), usize::from(end - first));

for discriminant in first..end {
let syntax = SyntaxKind::from_u16(discriminant)
.expect("each contiguous parser node discriminant is valid");
let ast = AstNodeKind::try_from(syntax)
.unwrap_or_else(|kind| panic!("missing AST node kind for {kind:?}"));
assert_eq!(SyntaxKind::from(ast), syntax);
}
}

#[test]
fn tokens_trivia_and_sentinels_are_not_ast_node_kinds() {
for kind in [
SyntaxKind::Whitespace,
SyntaxKind::LineComment,
SyntaxKind::Ident,
SyntaxKind::Import,
SyntaxKind::OpenBrace,
SyntaxKind::Plus,
SyntaxKind::Error,
SyntaxKind::Eof,
] {
assert_eq!(AstNodeKind::from_syntax(kind), None);
assert_eq!(AstNodeKind::try_from(kind), Err(kind));
}
}
}
Loading
Loading