diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cc3fe68 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/target +/.cache +**/*.rs.bk +*.profraw +.DS_Store diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..87f6940 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,58 @@ +[workspace] +resolver = "2" +members = [ + "crates/marqdo-span", + "crates/marqdo-value", + "crates/marqdo-expression", + "crates/marqdo-markdown", + "crates/marqdo-ir", + "crates/marqdo-frontend", + "crates/marqdo-math", + "crates/marqdo-foreign", + "crates/marqdo-plugin-abi", + "crates/marqdo-plugin-sdk", + "crates/marqdo-host", + "crates/marqdo-runtime", + "crates/marqdo-package", + "crates/marqdo-sandbox", + "crates/cli", +] + +[workspace.package] +version = "0.1.0" +edition = "2021" +rust-version = "1.82" +license = "MIT OR Apache-2.0" +repository = "https://github.com/cflmy/marqdo-cli" +authors = ["Marqdo contributors"] + +[workspace.dependencies] +marqdo-span = { path = "crates/marqdo-span" } +marqdo-value = { path = "crates/marqdo-value" } +marqdo-expression = { path = "crates/marqdo-expression" } +marqdo-markdown = { path = "crates/marqdo-markdown" } +marqdo-ir = { path = "crates/marqdo-ir" } +marqdo-frontend = { path = "crates/marqdo-frontend" } +marqdo-math = { path = "crates/marqdo-math" } +marqdo-foreign = { path = "crates/marqdo-foreign" } +marqdo-plugin-abi = { path = "crates/marqdo-plugin-abi" } +marqdo-plugin-sdk = { path = "crates/marqdo-plugin-sdk" } +marqdo-host = { path = "crates/marqdo-host" } +marqdo-runtime = { path = "crates/marqdo-runtime" } +marqdo-package = { path = "crates/marqdo-package" } +marqdo-sandbox = { path = "crates/marqdo-sandbox" } +comrak = { version = "=1.0.0", default-features = false } +serde = { version = "1", features = ["derive"] } +serde_json = "1" +serde_yaml = "0.9" +num-bigint = { version = "0.5", features = ["serde"] } +num-rational = { version = "0.4", features = ["serde"] } +num-traits = "0.2" +sha2 = "0.10" +hex = "0.4" +libloading = "0.8" +tempfile = "3" +thiserror = "2" +clap = { version = "4", features = ["derive"] } +walkdir = "2" +unicode-normalization = "0.1" diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml new file mode 100644 index 0000000..3b2b297 --- /dev/null +++ b/crates/cli/Cargo.toml @@ -0,0 +1,32 @@ +[package] +name = "marqdo-cli" +description = "Marqdo command line interface (composition root)" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +authors.workspace = true + +[dependencies] +marqdo-span = { workspace = true } +marqdo-value = { workspace = true } +marqdo-expression = { workspace = true } +marqdo-markdown = { workspace = true } +marqdo-ir = { workspace = true } +marqdo-frontend = { workspace = true } +marqdo-math = { workspace = true } +marqdo-foreign = { workspace = true } +marqdo-plugin-abi = { workspace = true } +marqdo-host = { workspace = true } +marqdo-runtime = { workspace = true } +marqdo-package = { workspace = true } +marqdo-sandbox = { workspace = true } +clap = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +thiserror = { workspace = true } + +[[bin]] +name = "marqdo" +path = "src/main.rs" diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs new file mode 100644 index 0000000..d1c7626 --- /dev/null +++ b/crates/cli/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("marqdo {}", env!("CARGO_PKG_VERSION")); +} diff --git a/crates/marqdo-expression/Cargo.toml b/crates/marqdo-expression/Cargo.toml new file mode 100644 index 0000000..a918d02 --- /dev/null +++ b/crates/marqdo-expression/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "marqdo-expression" +description = "Strict expression grammar for executable Markdown payloads" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +authors.workspace = true + +[dependencies] +marqdo-span = { workspace = true } +serde = { workspace = true } +thiserror = { workspace = true } + diff --git a/crates/marqdo-foreign/Cargo.toml b/crates/marqdo-foreign/Cargo.toml new file mode 100644 index 0000000..beb47fe --- /dev/null +++ b/crates/marqdo-foreign/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "marqdo-foreign" +description = "Explicit foreign code kernels (Python/Node) with structured protocol" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +authors.workspace = true + +[dependencies] +marqdo-span = { workspace = true } +marqdo-value = { workspace = true } +serde_json = { workspace = true } +thiserror = { workspace = true } + diff --git a/crates/marqdo-frontend/Cargo.toml b/crates/marqdo-frontend/Cargo.toml new file mode 100644 index 0000000..4675392 --- /dev/null +++ b/crates/marqdo-frontend/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "marqdo-frontend" +description = "Unit index, semantic mapping, static analysis and diagnostics" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +authors.workspace = true + +[dependencies] +unicode-normalization = { workspace = true } +marqdo-span = { workspace = true } +marqdo-markdown = { workspace = true } +marqdo-expression = { workspace = true } +marqdo-ir = { workspace = true } +marqdo-value = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +thiserror = { workspace = true } + diff --git a/crates/marqdo-host/Cargo.toml b/crates/marqdo-host/Cargo.toml new file mode 100644 index 0000000..7927cf3 --- /dev/null +++ b/crates/marqdo-host/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "marqdo-host" +description = "CapabilityHost dispatch, budgets, authorization and effect tracing" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +authors.workspace = true + +[dependencies] +marqdo-span = { workspace = true } +marqdo-value = { workspace = true } +marqdo-math = { workspace = true } +marqdo-foreign = { workspace = true } +marqdo-plugin-abi = { workspace = true } +marqdo-sandbox = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +thiserror = { workspace = true } + diff --git a/crates/marqdo-ir/Cargo.toml b/crates/marqdo-ir/Cargo.toml new file mode 100644 index 0000000..b73ef0c --- /dev/null +++ b/crates/marqdo-ir/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "marqdo-ir" +description = "Executable IR produced from Markdown AST structure" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +authors.workspace = true + +[dependencies] +marqdo-span = { workspace = true } +marqdo-value = { workspace = true } +marqdo-expression = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } + diff --git a/crates/marqdo-markdown/Cargo.toml b/crates/marqdo-markdown/Cargo.toml new file mode 100644 index 0000000..8db9cb2 --- /dev/null +++ b/crates/marqdo-markdown/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "marqdo-markdown" +description = "Canonical CommonMark/GFM/Math AST view, frontmatter extraction and source maps" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +authors.workspace = true + +[dependencies] +unicode-normalization = { workspace = true } +marqdo-span = { workspace = true } +comrak = { workspace = true } +serde = { workspace = true } +serde_yaml = { workspace = true } + diff --git a/crates/marqdo-math/Cargo.toml b/crates/marqdo-math/Cargo.toml new file mode 100644 index 0000000..9703a70 --- /dev/null +++ b/crates/marqdo-math/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "marqdo-math" +description = "Native Math/Formula kernel: parse, substitute and evaluate a defined subset" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +authors.workspace = true + +[dependencies] +marqdo-span = { workspace = true } +marqdo-value = { workspace = true } +num-bigint = { workspace = true } +num-rational = { workspace = true } +num-traits = { workspace = true } +serde = { workspace = true } +thiserror = { workspace = true } + diff --git a/crates/marqdo-package/Cargo.toml b/crates/marqdo-package/Cargo.toml new file mode 100644 index 0000000..65e82e4 --- /dev/null +++ b/crates/marqdo-package/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "marqdo-package" +description = "Project manifest, lockfile, dependency resolution and environment sync" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +authors.workspace = true + +[dependencies] +marqdo-span = { workspace = true } +serde = { workspace = true } +serde_yaml = { workspace = true } +serde_json = { workspace = true } +sha2 = { workspace = true } +hex = { workspace = true } +thiserror = { workspace = true } +walkdir = { workspace = true } + diff --git a/crates/marqdo-plugin-abi/Cargo.toml b/crates/marqdo-plugin-abi/Cargo.toml new file mode 100644 index 0000000..fd18f85 --- /dev/null +++ b/crates/marqdo-plugin-abi/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "marqdo-plugin-abi" +description = "Versioned C ABI v1 bindings, manifest validation and artifact loading" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +authors.workspace = true + +[dependencies] +marqdo-span = { workspace = true } +marqdo-value = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +serde_yaml = { workspace = true } +thiserror = { workspace = true } +libloading = { workspace = true } + diff --git a/crates/marqdo-plugin-sdk/Cargo.toml b/crates/marqdo-plugin-sdk/Cargo.toml new file mode 100644 index 0000000..2fb847b --- /dev/null +++ b/crates/marqdo-plugin-sdk/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "marqdo-plugin-sdk" +description = "Rust SDK for writing Marqdo plugins against ABI v1" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +authors.workspace = true + +[dependencies] +marqdo-plugin-abi = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } + diff --git a/crates/marqdo-runtime/Cargo.toml b/crates/marqdo-runtime/Cargo.toml new file mode 100644 index 0000000..2e1e462 --- /dev/null +++ b/crates/marqdo-runtime/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "marqdo-runtime" +description = "Rust tree-walk interpreter for Marqdo IR" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +authors.workspace = true + +[dependencies] +marqdo-span = { workspace = true } +marqdo-value = { workspace = true } +marqdo-ir = { workspace = true } +marqdo-expression = { workspace = true } +marqdo-host = { workspace = true } +thiserror = { workspace = true } + diff --git a/crates/marqdo-sandbox/Cargo.toml b/crates/marqdo-sandbox/Cargo.toml new file mode 100644 index 0000000..7abbc36 --- /dev/null +++ b/crates/marqdo-sandbox/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "marqdo-sandbox" +description = "Sandbox modes and platform isolation capability detection" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +authors.workspace = true + +[dependencies] +marqdo-span = { workspace = true } +serde = { workspace = true } +thiserror = { workspace = true } + diff --git a/crates/marqdo-span/Cargo.toml b/crates/marqdo-span/Cargo.toml new file mode 100644 index 0000000..e69501a --- /dev/null +++ b/crates/marqdo-span/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "marqdo-span" +description = "Byte source spans and line/column mapping shared by the Marqdo toolchain" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +authors.workspace = true + +[dependencies] +serde = { workspace = true } diff --git a/crates/marqdo-span/src/lib.rs b/crates/marqdo-span/src/lib.rs new file mode 100644 index 0000000..5434483 --- /dev/null +++ b/crates/marqdo-span/src/lib.rs @@ -0,0 +1,171 @@ +//! Byte source spans and line/column mapping shared by the Marqdo toolchain. +//! +//! Every syntax node and every diagnostic carries a [`Span`] expressed as byte +//! offsets into the original UTF-8 source file. Byte offsets are stable and +//! cheap to compare; human readable positions are derived on demand with a +//! [`LineIndex`]. This crate deliberately has no Marqdo dependencies so that +//! the Markdown layer can stay independent of the semantic layers. + +use serde::{Deserialize, Serialize}; + +/// Half-open byte range `[start, end)` into the original source text. +#[derive( + Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default, +)] +pub struct Span { + /// Byte offset of the first byte of the span. + pub start: usize, + /// Byte offset one past the last byte of the span. + pub end: usize, +} + +impl Span { + /// Create a span from half-open byte offsets. + pub const fn new(start: usize, end: usize) -> Self { + Self { start, end } + } + + /// The empty span at `offset`. + pub const fn empty(offset: usize) -> Self { + Self { + start: offset, + end: offset, + } + } + + /// Number of bytes covered by the span. + pub const fn len(&self) -> usize { + self.end.saturating_sub(self.start) + } + + /// Whether the span covers no bytes. + pub const fn is_empty(&self) -> bool { + self.start >= self.end + } + + /// Smallest span covering both `self` and `other`. + pub fn merge(self, other: Span) -> Span { + Span { + start: self.start.min(other.start), + end: self.end.max(other.end), + } + } + + /// Shift the span by `offset` bytes (used to re-base onto the original file + /// after a BOM has been stripped). + pub fn shift(self, offset: usize) -> Span { + Span { + start: self.start + offset, + end: self.end + offset, + } + } + + /// Whether `self` contains `offset`. + pub fn contains(&self, offset: usize) -> bool { + self.start <= offset && offset < self.end + } +} + +impl std::fmt::Display for Span { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}..{}", self.start, self.end) + } +} + +/// 1-based line and column (column counted in bytes). +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +pub struct LineCol { + /// 1-based line number. + pub line: usize, + /// 1-based byte column. + pub column: usize, +} + +impl std::fmt::Display for LineCol { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}:{}", self.line, self.column) + } +} + +/// Precomputed line-start offsets for a source text. +#[derive(Debug, Clone, Default)] +pub struct LineIndex { + line_starts: Vec, +} + +impl LineIndex { + /// Build a line index for `source`. + pub fn new(source: &str) -> Self { + let mut line_starts = vec![0usize]; + for (offset, byte) in source.bytes().enumerate() { + if byte == b'\n' { + line_starts.push(offset + 1); + } + } + Self { line_starts } + } + + /// Number of lines in the indexed source (a trailing newline does not add a + /// phantom line). + pub fn line_count(&self) -> usize { + if self.line_starts.is_empty() { + 0 + } else { + self.line_starts.len() + } + } + + /// Convert a byte offset into a 1-based line/column pair. Offsets past the + /// end of the source clamp to the last line. + pub fn line_col(&self, offset: usize) -> LineCol { + let idx = match self.line_starts.binary_search(&offset) { + Ok(i) => i, + Err(i) => i.saturating_sub(1), + }; + let line_start = self.line_starts.get(idx).copied().unwrap_or(0); + LineCol { + line: idx + 1, + column: offset.saturating_sub(line_start) + 1, + } + } + + /// Byte offset of the first byte of a 1-based line. + pub fn line_start(&self, line: usize) -> Option { + self.line_starts.get(line.checked_sub(1)?).copied() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn line_index_maps_multibyte_text() { + let src = "ab\n数学\nc"; + let idx = LineIndex::new(src); + assert_eq!(idx.line_count(), 3); + assert_eq!(idx.line_col(0), LineCol { line: 1, column: 1 }); + assert_eq!(idx.line_col(2), LineCol { line: 1, column: 3 }); + // '数' starts at byte 3. + assert_eq!(idx.line_col(3), LineCol { line: 2, column: 1 }); + // '学' is 3 bytes later. + assert_eq!(idx.line_col(6), LineCol { line: 2, column: 4 }); + // byte 7 is inside line 2 (the newline is byte 9). + assert_eq!(idx.line_col(7), LineCol { line: 2, column: 5 }); + assert_eq!(idx.line_col(9), LineCol { line: 2, column: 7 }); + assert_eq!(idx.line_col(10), LineCol { line: 3, column: 1 }); + assert_eq!(idx.line_start(2), Some(3)); + assert_eq!(idx.line_start(0), None); + } + + #[test] + fn span_merge_and_shift() { + let a = Span::new(4, 7); + let b = Span::new(2, 5); + assert_eq!(a.merge(b), Span::new(2, 7)); + assert_eq!(a.shift(3), Span::new(7, 10)); + assert!(a.contains(4)); + assert!(!a.contains(7)); + assert_eq!(Span::new(4, 7).len(), 3); + } +} diff --git a/crates/marqdo-value/Cargo.toml b/crates/marqdo-value/Cargo.toml new file mode 100644 index 0000000..e5b1672 --- /dev/null +++ b/crates/marqdo-value/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "marqdo-value" +description = "Marqdo value model, diagnostics and source spans" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +authors.workspace = true + +[dependencies] +marqdo-span = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +num-bigint = { workspace = true } +num-traits = { workspace = true } + diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..f42c8b3 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,2 @@ +edition = "2021" +max_width = 100