Skip to content

Commit bda615f

Browse files
author
Jesus Lapastora Núñez
committed
move baml_vis stuff to baml_vis; make scope traversal always source order
1 parent 5e71696 commit bda615f

File tree

8 files changed

+37
-51
lines changed

8 files changed

+37
-51
lines changed

engine/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

engine/baml-lib/ast/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ either.workspace = true
2626
test-log.workspace = true
2727
pretty = { workspace = true }
2828
regex.workspace = true
29+
indexmap.workspace = true
2930

3031
[dev-dependencies]
3132
pretty_assertions.workspace = true

engine/baml-lib/ast/src/ast.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,21 @@ mod type_expression_block;
2323
mod value_expression_block;
2424

2525
mod baml_vis;
26-
pub mod header_collector;
27-
pub mod mermaid_debug;
2826
pub use app::App;
2927
pub use argument::{Argument, ArgumentId, ArgumentsList};
3028
pub use assignment::Assignment;
3129
pub use attribute::{Attribute, AttributeContainer, AttributeId};
32-
pub use baml_vis::diagram_generator;
30+
pub use baml_vis::{diagram_generator, mermaid_debug::MermaidDiagramGenerator};
3331
pub use config::ConfigBlockProperty;
3432
pub use expr::{ExprFn, TopLevelAssignment};
3533
pub use expression::{
3634
BinaryOperator, ClassConstructor, ClassConstructorField, Expression, ExpressionBlock,
3735
RawString, UnaryOperator,
3836
};
3937
pub use field::{Field, FieldArity, FieldType};
40-
pub use header_collector::{HeaderCollector, HeaderIndex, RenderableHeader, ScopeId};
4138
pub use identifier::{Identifier, RefIdentifier};
4239
pub use indentation_type::IndentationType;
4340
pub use internal_baml_diagnostics::Span;
44-
pub use mermaid_debug::MermaidDiagramGenerator;
4541
pub use newline_type::NewlineType;
4642
pub use stmt::{
4743
AssertStmt, AssignOp, AssignOpStmt, AssignStmt, CForLoopStmt, ExprStmt, ForLoopStmt, Header,
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
pub mod diagram_generator;
12
mod graph;
3+
mod header_collector;
4+
pub(super) mod mermaid_debug;
25

36
use std::collections::{HashMap, HashSet};
47

@@ -9,10 +12,3 @@ use baml_types::BamlMap;
912
use graph::{Cluster, ClusterId, Direction, Graph, Node, NodeId, NodeKind};
1013
use internal_baml_diagnostics::SerializedSpan;
1114
use serde_json;
12-
13-
use super::{
14-
header_collector::{HeaderLabelKind, Hid},
15-
Ast, HeaderCollector, HeaderIndex, RenderableHeader, ScopeId,
16-
};
17-
18-
pub mod diagram_generator;

engine/baml-lib/ast/src/ast/baml_vis/diagram_generator.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ use internal_baml_diagnostics::SerializedSpan;
1212

1313
use crate::ast::{
1414
baml_vis::{
15-
graph::{self, BuilderConfig, Cluster, ClusterId, Direction, Graph, Node, NodeId, NodeKind},
15+
graph::{
16+
self, BuilderConfig, Cluster, ClusterId, Direction, Graph, Node, NodeId, NodeKind,
17+
},
1618
SHOW_CALL_NODES,
1719
},
18-
Ast, HeaderCollector,
20+
Ast,
1921
};
2022

23+
use super::header_collector::HeaderCollector;
24+
2125
/// Generate a Mermaid flowchart (LR) showing headers as linear steps and
2226
/// nested scopes as subgraphs.
2327
pub fn generate_headers_flowchart(ast: &Ast) -> String {
@@ -64,17 +68,11 @@ fn render_mermaid_graph(
6468

6569
let mut children_by_parent: BamlMap<_, Vec<_>> = BamlMap::new();
6670
for c in &graph.clusters {
67-
children_by_parent
68-
.entry(c.parent)
69-
.or_default()
70-
.push(c);
71+
children_by_parent.entry(c.parent).or_default().push(c);
7172
}
7273
let mut nodes_by_cluster: BamlMap<_, Vec<_>> = BamlMap::new();
7374
for n in &graph.nodes {
74-
nodes_by_cluster
75-
.entry(n.cluster)
76-
.or_default()
77-
.push(n);
75+
nodes_by_cluster.entry(n.cluster).or_default().push(n);
7876
}
7977

8078
fn emit<'index>(

engine/baml-lib/ast/src/ast/baml_vis/graph.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ use std::{
77
use baml_types::BamlMap;
88
use internal_baml_diagnostics::SerializedSpan;
99

10-
use crate::ast::{
11-
header_collector::{HeaderLabelKind, Hid},
12-
HeaderIndex, RenderableHeader, ScopeId,
13-
};
10+
use super::header_collector::{HeaderIndex, HeaderLabelKind, Hid, RenderableHeader, ScopeId};
1411

1512
/// Config: maximum number of direct children (markdown + nested) a non-branching
1613
/// container may have to be flattened into a linear sequence instead of a subgraph.

engine/baml-lib/ast/src/ast/header_collector.rs renamed to engine/baml-lib/ast/src/ast/baml_vis/header_collector.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
1515
use std::{collections::HashMap, sync::Arc};
1616

17-
use baml_types::BamlMap;
17+
use indexmap::IndexMap;
1818
use internal_baml_diagnostics::Span;
1919

20-
use super::{Ast, Expression, ExpressionBlock, Field, Header, Stmt, Top, WithName, WithSpan};
20+
use crate::ast::{
21+
Ast, ClassConstructorField, Expression, ExpressionBlock, Field, Header, Stmt, Top,
22+
};
2123

2224
/// Alias for external header identifiers for public consumption
2325
type HeaderId = String;
@@ -30,16 +32,6 @@ pub struct Hid(pub u32);
3032
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
3133
pub struct ScopeId(pub u32);
3234

33-
/// Classification of scope kinds for visualization semantics
34-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
35-
pub enum ScopeKind {
36-
TopLevel,
37-
ForBody,
38-
IfThen,
39-
IfElse,
40-
Generic,
41-
}
42-
4335
/// Classification of what kind of statement a header labels
4436
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4537
pub enum HeaderLabelKind {
@@ -73,8 +65,10 @@ pub struct HeaderIndex {
7365
pub headers: Vec<RenderableHeader>,
7466

7567
/// Header indexes in source order per scope.
76-
/// Since it uses `BamlMap` it will maintain source order in snapshot testing.
77-
by_scope: BamlMap<ScopeId, Vec<usize>>,
68+
/// Uses [`IndexMap`] instead of BamlMap to always guarantee iteration order = insertion order
69+
/// = source order.
70+
/// See [`graph`](crate::ast::baml_vis::graph) module, where we add
71+
by_scope: IndexMap<ScopeId, Vec<usize>>,
7872
/// Mapping of internal Hid -> names of functions called by the labeled expression
7973
pub header_calls: HashMap<Hid, Vec<String>>, // hid -> [callee_name]
8074
hid_to_idx: Vec<usize>,
@@ -94,7 +88,7 @@ impl HeaderIndex {
9488
.flat_map(|idxs| idxs.iter().map(|i| &self.headers[*i]))
9589
}
9690

97-
/// Iterates all scopes. Under snapshot testing, order is guaranteed to be
91+
/// Iterates all scopes. Order of iteration is guaranteed to be
9892
/// consistent with source order.
9993
pub fn scopes<'iter>(&'iter self) -> impl Iterator<Item = ScopeId> + 'iter {
10094
self.by_scope.keys().copied()
@@ -121,8 +115,8 @@ pub struct HeaderCollector {
121115
scope_counter: u32,
122116
scope_stack: Vec<ScopeId>,
123117
/// Raw headers by scope before normalization and in-scope parenting.
124-
/// Will maintain source order under snapshot testing.
125-
raw_by_scope: BamlMap<ScopeId, Vec<RawHeader>>, // source order
118+
/// [`IndexMap`] to maintain source order.
119+
raw_by_scope: IndexMap<ScopeId, Vec<RawHeader>>, // source order
126120
// Accumulated nested edges (Hid -> Hid)
127121
nested_edges_hid: Vec<(Hid, Hid)>,
128122
// Mapping during collection: header (by Hid) -> function names called by the labeled expression
@@ -353,8 +347,8 @@ impl HeaderCollector {
353347
Expression::ClassConstructor(cons, _) => {
354348
for f in &cons.fields {
355349
match f {
356-
super::ClassConstructorField::Named(_, e) => self.visit_expression(e),
357-
super::ClassConstructorField::Spread(e) => self.visit_expression(e),
350+
ClassConstructorField::Named(_, e) => self.visit_expression(e),
351+
ClassConstructorField::Spread(e) => self.visit_expression(e),
358352
}
359353
}
360354
}
@@ -466,7 +460,7 @@ impl HeaderCollector {
466460
fn build_index(self) -> HeaderIndex {
467461
let mut index = HeaderIndex {
468462
headers: Vec::new(),
469-
by_scope: BamlMap::new(),
463+
by_scope: IndexMap::new(),
470464
header_calls: HashMap::new(),
471465
hid_to_idx: Vec::new(),
472466
nested_edges_hid: Vec::new(),
@@ -545,6 +539,7 @@ impl HeaderCollector {
545539
/// Only captures the outermost call(s) that structurally represent the expression,
546540
/// ignoring nested calls within arguments or sub-expressions.
547541
fn collect_top_level_calls(expr: &Expression) -> Vec<String> {
542+
use crate::ast::WithName;
548543
match expr {
549544
// If the expression is a block, the top-level expression is inside it
550545
Expression::ExprBlock(block, _span) => {

engine/baml-lib/ast/src/ast/mermaid_debug.rs renamed to engine/baml-lib/ast/src/ast/baml_vis/mermaid_debug.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use std::collections::HashMap;
22

3-
use super::{
4-
App, Argument, ArgumentsList, Assignment, Ast, Attribute, BlockArgs, ClassConstructor,
5-
ClassConstructorField, ExprFn, Expression, ExpressionBlock, Field, FieldType, Header,
6-
HeaderCollector, HeaderIndex, Identifier, RawString, Stmt, TemplateString, Top,
7-
TopLevelAssignment, TypeExpressionBlock, ValueExprBlock, WithIdentifier, WithName, WithSpan,
3+
use crate::ast::{
4+
traits::{WithIdentifier, WithName},
5+
Argument, ArgumentsList, Assignment, Ast, Attribute, BlockArgs, ClassConstructor,
6+
ClassConstructorField, ExprFn, Expression, ExpressionBlock, Field, FieldType, Header, Stmt,
7+
TemplateString, Top, TopLevelAssignment, TypeExpressionBlock, ValueExprBlock,
88
};
99

10+
use super::header_collector::{HeaderCollector, HeaderIndex};
11+
1012
/// A debug utility for converting AST structures to Mermaid diagrams
1113
#[derive(Debug)]
1214
pub struct MermaidDiagramGenerator {

0 commit comments

Comments
 (0)