Skip to content

Commit 9901b50

Browse files
committed
add invalid unicode syntax and rust-as-baml tests
1 parent 29702a2 commit 9901b50

File tree

15 files changed

+6515
-133
lines changed

15 files changed

+6515
-133
lines changed

baml_language/crates/baml_hir/src/pretty_print.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
//! Pretty printing for HIR items.
22
3-
use crate::{ClassId, EnumId, FunctionId, ItemId};
43
use std::fmt;
54

5+
use crate::{ClassId, EnumId, FunctionId, ItemId};
6+
67
impl fmt::Display for ItemId {
78
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89
match self {
9-
ItemId::Function(func) => write!(f, "{}", func),
10-
ItemId::Class(class) => write!(f, "{}", class),
11-
ItemId::Enum(enum_) => write!(f, "{}", enum_),
10+
ItemId::Function(func) => write!(f, "{func}"),
11+
ItemId::Class(class) => write!(f, "{class}"),
12+
ItemId::Enum(enum_) => write!(f, "{enum_}"),
1213
}
1314
}
1415
}
@@ -36,7 +37,7 @@ pub fn format_items(items: &[ItemId]) -> String {
3637
let mut output = String::new();
3738

3839
for item in items {
39-
output.push_str(&format!(" {}\n", item));
40+
output.push_str(&format!(" {item}\n"));
4041
}
4142

4243
if output.is_empty() {
@@ -65,23 +66,23 @@ pub fn format_items_grouped(items: &[ItemId]) -> String {
6566
if !functions.is_empty() {
6667
output.push_str("Functions:\n");
6768
for func in functions {
68-
output.push_str(&format!(" {}\n", func));
69+
output.push_str(&format!(" {func}\n"));
6970
}
7071
output.push('\n');
7172
}
7273

7374
if !classes.is_empty() {
7475
output.push_str("Classes:\n");
7576
for class in classes {
76-
output.push_str(&format!(" {}\n", class));
77+
output.push_str(&format!(" {class}\n"));
7778
}
7879
output.push('\n');
7980
}
8081

8182
if !enums.is_empty() {
8283
output.push_str("Enums:\n");
8384
for enum_ in enums {
84-
output.push_str(&format!(" {}\n", enum_));
85+
output.push_str(&format!(" {enum_}\n"));
8586
}
8687
output.push('\n');
8788
}
Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,58 @@
1-
function FooDemo() -> int {
2-
let x = 100;
3-
let z = 100;
4-
20
1+
class Receipt {
2+
establishment_name string
3+
date string @description("ISO8601 formatted date")
4+
total int @description("The total amount of the receipt")
5+
currency string
6+
items Item[] @description("The items on the receipt")
7+
}
58

9+
class Item {
10+
name string
11+
price float
12+
quantity int @description("If not specified, assume 1")
13+
}
14+
15+
// This is our LLM function we can call in Python or Typescript
16+
// the receipt can be an image OR text here!
17+
function ExtractReceipt(receipt: image | string) -> Receipt {
18+
// see clients.baml
19+
client GPT4o
20+
prompt #"
21+
{# start a user message #}
22+
{{ _.role("user") }}
23+
24+
Extract info from this receipt:
25+
{{ receipt }}
26+
27+
{# special macro to print the output schema instructions. #}
28+
{{ ctx.output_format }}
29+
"#
30+
}
31+
32+
// Test when the input is an image
33+
test ImageReceiptTest {
34+
functions [ExtractReceipt]
35+
args {
36+
receipt { url "https://i.redd.it/adzt4bz4llfc1.jpeg"}
37+
}
38+
}
39+
40+
// Test when the input is a string
41+
test StarbucksTextReceiptTest {
42+
functions [ExtractReceipt]
43+
args {
44+
// use #""# for multi-line strings
45+
receipt #"
46+
Starbucks
47+
Date: 2022-01-01
48+
Total: $5.00 USD
49+
Items:
50+
- Coffee
51+
- $2.50
52+
- 1
53+
- Croissant
54+
- $2.50
55+
- 1
56+
"#
57+
}
658
}

baml_language/crates/baml_onionskin/src/app.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
use crate::compiler::{CompilerPhase, CompilerRunner, VisualizationMode, read_files_from_disk};
2-
use crate::ui;
3-
use crate::watcher::FileWatcher;
1+
use std::{collections::HashMap, path::PathBuf, time::Duration};
2+
43
use anyhow::Result;
54
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers, MouseEventKind};
6-
use ratatui::Terminal;
7-
use ratatui::backend::CrosstermBackend;
8-
use std::collections::HashMap;
9-
use std::path::PathBuf;
10-
use std::time::Duration;
5+
use ratatui::{Terminal, backend::CrosstermBackend};
6+
7+
use crate::{
8+
compiler::{CompilerPhase, CompilerRunner, VisualizationMode, read_files_from_disk},
9+
ui,
10+
watcher::FileWatcher,
11+
};
1112

12-
pub struct App {
13+
pub(crate) struct App {
1314
file_path: PathBuf,
1415
/// Which tab is shown in the TUI.
1516
current_phase: CompilerPhase,
@@ -28,7 +29,7 @@ pub struct App {
2829
}
2930

3031
impl App {
31-
pub fn new(path: PathBuf) -> Result<Self> {
32+
pub(crate) fn new(path: PathBuf) -> Result<Self> {
3233
let watcher = FileWatcher::new(&path)?;
3334
let mut compiler = CompilerRunner::new(&path);
3435

@@ -52,7 +53,7 @@ impl App {
5253
})
5354
}
5455

55-
pub fn run(
56+
pub(crate) fn run(
5657
&mut self,
5758
terminal: &mut Terminal<CrosstermBackend<std::io::Stdout>>,
5859
) -> Result<()> {
@@ -193,42 +194,42 @@ impl App {
193194
Ok(())
194195
}
195196

196-
pub fn current_phase(&self) -> CompilerPhase {
197+
pub(crate) fn current_phase(&self) -> CompilerPhase {
197198
self.current_phase
198199
}
199200

200-
pub fn current_output(&self) -> &str {
201+
pub(crate) fn current_output(&self) -> &str {
201202
self.compiler
202203
.get_phase_output(self.current_phase)
203204
.unwrap_or("No output available")
204205
}
205206

206-
pub fn snapshot_output(&self) -> Option<&str> {
207+
pub(crate) fn snapshot_output(&self) -> Option<&str> {
207208
self.snapshot_compiler
208209
.as_ref()
209210
.and_then(|c| c.get_phase_output(self.current_phase))
210211
}
211212

212-
pub fn file_path(&self) -> &PathBuf {
213+
pub(crate) fn file_path(&self) -> &PathBuf {
213214
&self.file_path
214215
}
215216

216-
pub fn has_snapshot(&self) -> bool {
217+
pub(crate) fn has_snapshot(&self) -> bool {
217218
self.snapshot_compiler.is_some()
218219
}
219220

220-
pub fn scroll_offset(&self) -> u16 {
221+
pub(crate) fn scroll_offset(&self) -> u16 {
221222
self.scroll_offset
222223
}
223224

224-
pub fn get_recomputation_status(
225+
pub(crate) fn get_recomputation_status(
225226
&self,
226227
phase: CompilerPhase,
227228
) -> crate::compiler::RecomputationStatus {
228229
self.compiler.get_recomputation_status(phase)
229230
}
230231

231-
pub fn get_snapshot_recomputation_status(
232+
pub(crate) fn get_snapshot_recomputation_status(
232233
&self,
233234
phase: CompilerPhase,
234235
) -> Option<crate::compiler::RecomputationStatus> {
@@ -237,15 +238,15 @@ impl App {
237238
.map(|c| c.get_recomputation_status(phase))
238239
}
239240

240-
pub fn get_output_annotated(
241+
pub(crate) fn get_output_annotated(
241242
&self,
242243
phase: CompilerPhase,
243244
) -> Vec<(String, crate::compiler::LineStatus)> {
244245
self.compiler
245246
.get_annotated_output_with_mode(phase, self.visualization_mode)
246247
}
247248

248-
pub fn get_snapshot_output_annotated(
249+
pub(crate) fn get_snapshot_output_annotated(
249250
&self,
250251
phase: CompilerPhase,
251252
) -> Option<Vec<(String, crate::compiler::LineStatus)>> {
@@ -255,11 +256,11 @@ impl App {
255256
})
256257
}
257258

258-
pub fn visualization_mode(&self) -> VisualizationMode {
259+
pub(crate) fn visualization_mode(&self) -> VisualizationMode {
259260
self.visualization_mode
260261
}
261262

262-
pub fn visualization_mode_name(&self) -> &'static str {
263+
pub(crate) fn visualization_mode_name(&self) -> &'static str {
263264
match self.visualization_mode {
264265
VisualizationMode::Diff => "Diff",
265266
VisualizationMode::Salsa => "Salsa",

0 commit comments

Comments
 (0)