Skip to content

Commit

Permalink
Merge pull request #358 from koto-lang/various-tweaks
Browse files Browse the repository at this point in the history
Various tweaks
  • Loading branch information
irh authored Oct 1, 2024
2 parents 8cc8300 + 7b0fc95 commit 170c094
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 223 deletions.
291 changes: 97 additions & 194 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ glam = "0.22.0"
# Shared definitions of home directories.
home = "0.5.5"
# A Rust library for conveniently watching and handling file changes.
hotwatch = "0.4.5"
hotwatch = "0.5.0"
# A hash table with consistent order and fast iteration.
indexmap = "2.0.0"
# A partial replacement for std::time::Instant that works on WASM too.
Expand Down Expand Up @@ -67,8 +67,8 @@ saturating_cast = "0.1.0"
serde = "1.0.0"
# JSON support for serde
serde_json = { version = "1.0.0", features = ["preserve_order", "std"] }
# YAML support for serde
serde_yaml = "0.8.20"
# YAML data format for serde
serde_yaml_ng = "0.10.0"
# 'Small vector' optimization: store up to a small number of items on the stack
smallvec = { version = "1.11.1", features = ["const_generics", "union"] }
# Parser for Rust source code
Expand Down
12 changes: 6 additions & 6 deletions crates/cli/docs/libs/yaml.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ nested:
number: -1.2
entries:
- foo: bar
- foo: baz
- foo: bar
- foo: baz
'
result = yaml.from_string data
Expand Down Expand Up @@ -56,12 +56,12 @@ data =
)
print! yaml.to_string data
check! ---
check! string: ">_>"
check! string: '>_>'
check! nested:
check! number: 99
check! entries:
check! - foo: bar
check! - foo: baz
check! - foo: bar
check! - foo: baz
check!
```

23 changes: 14 additions & 9 deletions crates/koto/examples/poetry/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ mod poetry;
use anyhow::{bail, Context, Result};
use hotwatch::{
blocking::{Flow, Hotwatch},
Event,
notify::event::ModifyKind,
Event, EventKind,
};
use koto::{Koto, KotoSettings};
use poetry::*;
use std::{fs, path::Path, time::Duration};
use std::{
fs,
path::{Path, PathBuf},
time::Duration,
};

fn version_string() -> String {
format!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"))
Expand Down Expand Up @@ -87,21 +92,21 @@ fn main() -> Result<()> {
.insert("poetry", koto_bindings::make_module());
koto.prelude().insert("random", koto_random::make_module());

let script_path = Path::new(&args.script);
koto.set_script_path(Some(script_path))
let script_path = PathBuf::from(args.script);
koto.set_script_path(Some(&script_path))
.expect("Failed to set script path");

if args.watch {
if let Err(e) = compile_and_run(&mut koto, script_path) {
if let Err(e) = compile_and_run(&mut koto, &script_path) {
eprintln!("{e}");
}

let mut hotwatch = Hotwatch::new_with_custom_delay(Duration::from_secs_f64(0.25))
.context("Failed to initialize file watcher")?;
hotwatch
.watch(&args.script, move |event: Event| {
match event {
Event::Create(script_path) | Event::Write(script_path) => {
.watch(script_path.clone(), move |event: Event| {
match event.kind {
EventKind::Create(_) | EventKind::Modify(ModifyKind::Data(_)) => {
if let Err(error) = compile_and_run(&mut koto, &script_path) {
eprintln!("{error}");
}
Expand All @@ -114,7 +119,7 @@ fn main() -> Result<()> {
hotwatch.run();
Ok(())
} else {
compile_and_run(&mut koto, script_path)
compile_and_run(&mut koto, &script_path)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/parser/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub enum Node {
/// Keys will either be Id, String, or Meta nodes.
///
/// Values are optional for inline maps.
Map(Vec<(AstIndex, Option<AstIndex>)>),
Map(AstVec<(AstIndex, Option<AstIndex>)>),

/// The `self` keyword
Self_,
Expand Down
8 changes: 5 additions & 3 deletions crates/parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1861,7 +1861,7 @@ impl<'source> Parser<'source> {
return self.error(InternalError::ExpectedMapColon);
}

let mut entries = vec![(first_key, Some(self.consume_map_block_value()?))];
let mut entries = astvec![(first_key, Some(self.consume_map_block_value()?))];

let block_context = ExpressionContext::permissive()
.with_expected_indentation(Indentation::Equal(start_indent));
Expand Down Expand Up @@ -1919,8 +1919,10 @@ impl<'source> Parser<'source> {
)
}

fn parse_comma_separated_map_entries(&mut self) -> Result<Vec<(AstIndex, Option<AstIndex>)>> {
let mut entries = Vec::new();
fn parse_comma_separated_map_entries(
&mut self,
) -> Result<AstVec<(AstIndex, Option<AstIndex>)>> {
let mut entries = AstVec::new();
let mut entry_context = ExpressionContext::braced_items_start();

while self.peek_token_with_context(&entry_context).is_some() {
Expand Down
4 changes: 2 additions & 2 deletions koto/tests/libs/data/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ nested:
string: hello

entries:
- foo: bar
- foo: baz
- foo: bar
- foo: baz
2 changes: 1 addition & 1 deletion libs/yaml/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository = "https://github.com/koto-lang/koto"
keywords = ["scripting", "language", "koto", "yaml"]

[dependencies]
serde_yaml = { workspace = true }
serde_yaml_ng = { workspace = true }

[dependencies.koto_runtime]
path = "../../crates/runtime"
Expand Down
22 changes: 18 additions & 4 deletions libs/yaml/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

use koto_runtime::{prelude::*, Result};
use koto_serialize::SerializableValue;
use serde_yaml::Value as YamlValue;
use serde_yaml_ng::Value as YamlValue;

pub fn yaml_value_to_koto_value(value: &serde_yaml::Value) -> Result<KValue> {
pub fn yaml_value_to_koto_value(value: &YamlValue) -> Result<KValue> {
let result = match value {
YamlValue::Null => KValue::Null,
YamlValue::Bool(b) => KValue::Bool(*b),
Expand Down Expand Up @@ -37,6 +37,20 @@ pub fn yaml_value_to_koto_value(value: &serde_yaml::Value) -> Result<KValue> {
}
KValue::Map(map)
}
YamlValue::Tagged(tagged_value) => {
let map = KMap::with_type("TaggedValue");

let tag = tagged_value.tag.to_string();
let tag = match tag.strip_prefix("!") {
Some(stripped) => stripped.to_string(),
None => tag,
};

map.insert("tag", tag);
map.insert("value", yaml_value_to_koto_value(value)?);

KValue::Map(map)
}
};

Ok(result)
Expand All @@ -46,7 +60,7 @@ pub fn make_module() -> KMap {
let result = KMap::with_type("yaml");

result.add_fn("from_string", |ctx| match ctx.args() {
[KValue::Str(s)] => match serde_yaml::from_str(s) {
[KValue::Str(s)] => match serde_yaml_ng::from_str(s) {
Ok(value) => match yaml_value_to_koto_value(&value) {
Ok(result) => Ok(result),
Err(e) => runtime_error!("Error while parsing input: {}", e),
Expand All @@ -57,7 +71,7 @@ pub fn make_module() -> KMap {
});

result.add_fn("to_string", |ctx| match ctx.args() {
[value] => match serde_yaml::to_string(&SerializableValue(value)) {
[value] => match serde_yaml_ng::to_string(&SerializableValue(value)) {
Ok(result) => Ok(result.into()),
Err(e) => runtime_error!("yaml.to_string: {}", e),
},
Expand Down

0 comments on commit 170c094

Please sign in to comment.