Skip to content

Commit

Permalink
Make it possible to generate code better in structures with snippets (#…
Browse files Browse the repository at this point in the history
…1146)

<!-- ELLIPSIS_HIDDEN -->



> [!IMPORTANT]
> Add support for backtick strings in JSON parser and include
comprehensive test cases for various string formats.
> 
>   - **Behavior**:
> - Add `BacktickString` variant to `JsonCollection` in
`json_collection.rs`.
> - Update `JsonParseState` in `json_parse_state.rs` to handle
`BacktickString` during parsing.
>   - **Tests**:
> - Add `test_code.rs` with test cases for `BacktickString`, including
handling of unescaped newlines and quotes.
> - Ensure existing string formats (single, double, triple quotes) are
tested for similar cases.
> 
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral)<sup>
for c98e049. It will automatically
update as commits are pushed.</sup>

<!-- ELLIPSIS_HIDDEN -->
  • Loading branch information
hellovai authored Nov 5, 2024
1 parent bf2b428 commit 3d8ef34
Show file tree
Hide file tree
Showing 4 changed files with 386 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum JsonCollection {
QuotedString(String),
TripleQuotedString(String),
SingleQuotedString(String),
BacktickString(String),
// Handles numbers, booleans, null, and unquoted strings
UnquotedString(String),
// Starting with // or #
Expand All @@ -25,6 +26,7 @@ impl JsonCollection {
JsonCollection::Array(_) => "Array",
JsonCollection::QuotedString(_) => "String",
JsonCollection::SingleQuotedString(_) => "String",
JsonCollection::BacktickString(_) => "String",
JsonCollection::TripleQuotedString(_) => "TripleQuotedString",
JsonCollection::UnquotedString(_) => "UnquotedString",
JsonCollection::TrailingComment(_) => "Comment",
Expand All @@ -49,6 +51,7 @@ impl From<JsonCollection> for Option<Value> {
JsonCollection::QuotedString(s) => Value::String(s),
JsonCollection::TripleQuotedString(s) => Value::String(s),
JsonCollection::SingleQuotedString(s) => Value::String(s),
JsonCollection::BacktickString(s) => Value::String(s),
JsonCollection::UnquotedString(s) => {
let s = s.trim();
if s == "true" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl JsonParseState {
| JsonCollection::TripleQuotedString(s)
| JsonCollection::BlockComment(s)
| JsonCollection::SingleQuotedString(s)
| JsonCollection::BacktickString(s)
| JsonCollection::UnquotedString(s)
| JsonCollection::TrailingComment(s) => {
// println!("Consuming: {s} + {:?}", token);
Expand Down Expand Up @@ -484,6 +485,22 @@ impl JsonParseState {
_ => self.consume(token),
}
}
JsonCollection::BacktickString(_) => {
// We could be expecting:
// - A closing backtick
// - A character
match token {
'`' => {
if self.should_close_string(next, '`') {
self.complete_collection();
Ok(0)
} else {
self.consume(token)
}
}
_ => self.consume(token),
}
}
JsonCollection::SingleQuotedString(_) => {
// We could be expecting:
// - A closing quote
Expand Down Expand Up @@ -599,6 +616,12 @@ impl JsonParseState {
Default::default(),
));
}
'`' => {
self.collection_stack.push((
JsonCollection::BacktickString(String::new()),
Default::default(),
));
}
'/' => {
// Could be a comment
match next.peek() {
Expand Down
1 change: 1 addition & 0 deletions engine/baml-lib/jsonish/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod macros;
mod test_basics;
mod test_class;
mod test_class_2;
mod test_code;
mod test_constraints;
mod test_enum;
mod test_lists;
Expand Down
Loading

0 comments on commit 3d8ef34

Please sign in to comment.