Skip to content

Commit

Permalink
[WIP] Implement AST dump as JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Zollerboy1 committed Jun 20, 2024
1 parent 023ccc8 commit 4fb76b7
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 37 deletions.
85 changes: 85 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ chumsky = { workspace = true }
derive_more = { workspace = true }
in_definite = "1.0.0"
itertools = { workspace = true }
serde = { version = "1.0.203", features = ["derive", "rc"] }
serde-tuple-vec-map = "1.0.1"
thousands = "0.2.0"
53 changes: 39 additions & 14 deletions core/src/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{io, sync::Arc};
use ariadne::{Color, Fmt as _};
use derive_more::From;
use itertools::Itertools as _;
use serde::Serialize;
use thousands::{digits::ASCII_HEXADECIMAL, Separable as _, SeparatorPolicy};

use crate::{
Expand All @@ -16,16 +17,45 @@ const UNDERSCORE_HEX_SEPARATOR: SeparatorPolicy = SeparatorPolicy {
digits: ASCII_HEXADECIMAL,
};

#[derive(Debug, Clone, From)]
fn format_bigint(v: &[u64]) -> String {
let (first, rest) = v.split_last().unwrap();

let first = format!("{first:x}").separate_by_policy(UNDERSCORE_HEX_SEPARATOR);

let rest = rest.iter().rev().format_with("_", |part, f| {
f(&format!("{part:016x}").separate_by_policy(UNDERSCORE_HEX_SEPARATOR))
});

format!("0x{first}_{rest}")
}

fn serialize_bigint<S>(v: &[u64], serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&format_bigint(v))
}

#[derive(Debug, Clone, From, Serialize)]
#[serde(tag = "type", content = "value")]
pub enum DumpField<'src> {
#[serde(rename = "bigint", serialize_with = "serialize_bigint")]
BigInt(Arc<[u64]>),
#[serde(untagged)]
Bool(bool),
#[serde(untagged)]
Int(u64),
BigInt(Arc<[u64]>),
#[serde(untagged)]
Float(f64),
#[serde(untagged)]
Char(char),
#[serde(untagged)]
String(&'src str),
#[serde(untagged)]
ArcStr(Arc<str>),
#[serde(untagged)]
List(Vec<Dump<'src>>),
#[serde(untagged)]
Dump(Dump<'src>),
}

Expand All @@ -38,21 +68,13 @@ impl DumpField<'_> {
let indent_str = " ".repeat(indentation);

match self {
Self::Bool(v) => write!(stream, "{v}"),
Self::Int(v) => write!(stream, "{v}"),
Self::BigInt(v) => {
let (first, rest) = v.split_last().unwrap();

let first = format!("{first:x}").separate_by_policy(UNDERSCORE_HEX_SEPARATOR);

let rest = rest.iter().rev().format_with("_", |part, f| {
f(&format!("{part:016x}").separate_by_policy(UNDERSCORE_HEX_SEPARATOR))
});

write!(stream, "0x{first}_{rest}")?;
write!(stream, "{}", format_bigint(v))?;

Ok(())
}
Self::Bool(v) => write!(stream, "{v}"),
Self::Int(v) => write!(stream, "{v}"),
Self::Float(v) => write!(stream, "{v}"),
Self::Char(c) => write!(stream, "{c:?}"),
Self::String(s) => write!(stream, "{s:?}"),
Expand All @@ -75,10 +97,13 @@ impl DumpField<'_> {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize)]
pub struct Dump<'src> {
#[serde(rename = "node")]
pub name: &'static str,
#[serde(rename = "error", skip_serializing_if = "std::ops::Not::not")]
pub is_error: bool,
#[serde(flatten, with = "tuple_vec_map")]
pub fields: Vec<(&'static str, DumpField<'src>)>,
}

Expand Down
1 change: 1 addition & 0 deletions driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ async-once-cell = "0.5.3"
async-process = "2.1.0"
cfg-if = "1.0.0"
clap = { version = "4.5.1", features = ["derive"] }
color-print = "0.3.6"
derive_more = { workspace = true }
juice-core = { workspace = true }
juice-frontend = { workspace = true }
Expand Down
Loading

0 comments on commit 4fb76b7

Please sign in to comment.