Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions zap/src/irgen/des.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Gen for Des<'_> {
impl Des<'_> {
fn push_struct(&mut self, struct_ty: &Struct, into: Var) {
for (name, ty) in struct_ty.fields.iter() {
self.push_ty(ty, into.clone().nindex(*name))
self.push_ty(ty, into.clone().eindex(Expr::Str((*name).into())))
}
}

Expand Down Expand Up @@ -72,7 +72,10 @@ impl Des<'_> {
self.push_stmt(Stmt::ElseIf(enum_value_expr.clone().eq((i as f64).into())));
}

self.push_assign(into.clone().nindex(*tag), Expr::StrOrBool(name.to_string()));
self.push_assign(
into.clone().eindex(Expr::Str((*tag).into())),
Expr::StrOrBool(name.to_string()),
);
self.push_struct(struct_ty, into.clone());
}

Expand Down
4 changes: 2 additions & 2 deletions zap/src/irgen/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Gen for Ser<'_> {
impl Ser<'_> {
fn push_struct(&mut self, struct_ty: &Struct, from: Var) {
for (name, ty) in struct_ty.fields.iter() {
self.push_ty(ty, from.clone().nindex(*name));
self.push_ty(ty, from.clone().eindex(Expr::Str((*name).into())));
}
}

Expand Down Expand Up @@ -61,7 +61,7 @@ impl Ser<'_> {
}

Enum::Tagged { tag, variants } => {
let tag_expr = Expr::from(from.clone().nindex(*tag));
let tag_expr = Expr::from(from.clone().eindex(Expr::Str((*tag).into())));
let numty = NumTy::from_f64(0.0, variants.len() as f64 - 1.0);

for (i, variant) in variants.iter().enumerate() {
Expand Down
8 changes: 4 additions & 4 deletions zap/src/output/luau/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ pub trait Output {
self.push_indent();

if *name == "true" || *name == "false" {
self.push(&format!("{tag}: {name},\n"));
self.push(&format!("[\"{tag}\"]: {name},\n"));
} else {
self.push(&format!("{tag}: \"{name}\",\n"));
self.push(&format!("[\"{tag}\"]: \"{name}\",\n"));
}

for (name, ty) in struct_ty.fields.iter() {
self.push_indent();
self.push(&format!("{name}: "));
self.push(&format!("[\"{name}\"]: "));
self.push_ty(ty);
self.push(",\n");
}
Expand All @@ -173,7 +173,7 @@ pub trait Output {

for (name, ty) in struct_ty.fields.iter() {
self.push_indent();
self.push(&format!("{name}: "));
self.push(&format!("[\"{name}\"]: "));
self.push_ty(ty);
self.push(",\n");
}
Expand Down
8 changes: 6 additions & 2 deletions zap/src/output/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,16 @@ pub trait Output: ConfigProvider {
self.push_indent();

if *name == "true" || *name == "false" {
self.push(&format!("{tag}: {name},\n"));
self.push(&format!("[\"{tag}\"]: {name},\n"));
} else {
self.push(&format!("{tag}: \"{name}\",\n"));
self.push(&format!("[\"{tag}\"]: \"{name}\",\n"));
}

for (name, ty) in struct_ty.fields.iter() {
self.push_indent();
self.push("[\"");
self.push(name);
self.push("\"]");
self.push_arg_ty(ty);
self.push(",\n");
}
Expand All @@ -184,7 +186,9 @@ pub trait Output: ConfigProvider {

for (name, ty) in struct_ty.fields.iter() {
self.push_indent();
self.push("[\"");
self.push(name);
self.push("\"]");
self.push_arg_ty(ty);
self.push(",\n");
}
Expand Down
11 changes: 8 additions & 3 deletions zap/src/parser/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ Enum: SyntaxEnum<'input> = {
}

EnumKind: SyntaxEnumKind<'input> = {
"{" <enumerators:Comma<Identifier>> "}" => SyntaxEnumKind::Unit(enumerators),
"{" <enumerators:Comma<KeyIdentifier>> "}" => SyntaxEnumKind::Unit(enumerators),

<tag:StrLit> "{" <variants:Comma<(<Identifier> <Struct>)>> "}" => SyntaxEnumKind::Tagged { tag, variants },
<tag:StrLit> "{" <variants:Comma<(<KeyIdentifier> <Struct>)>> "}" => SyntaxEnumKind::Tagged { tag, variants },
}

Struct: SyntaxStruct<'input> = {
<start:@L> "{" <fields:Comma<(<Identifier> ":" <Ty>)>> "}" <end:@R> => SyntaxStruct { start, fields, end },
<start:@L> "{" <fields:Comma<(<KeyIdentifier> ":" <Ty>)>> "}" <end:@R> => SyntaxStruct { start, fields, end },
}

IntRange: SyntaxRange<'input> = {
Expand Down Expand Up @@ -230,6 +230,11 @@ Identifier: SyntaxIdentifier<'input> = {
<start:@L> "false" <end:@R> => SyntaxIdentifier { start, name: "false", end },
}

KeyIdentifier: SyntaxIdentifier<'input> = {
Identifier,
<start:@L> <s:StrLit> <end:@R> => SyntaxIdentifier { start, name: &s.value[1..s.value.len() - 1], end },
}

Comma<T>: Vec<T> = {
<mut v:(<T> ",")*> <e:T?> => match e {
Some(e) => { v.push(e); v },
Expand Down
9 changes: 9 additions & 0 deletions zap/tests/files/struct_quoted_fields.zap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
event MyEvent = {
from: Server,
type: Reliable,
call: ManyAsync,
data: struct {
"foo bar": string,
buzz: u8
}
}
9 changes: 9 additions & 0 deletions zap/tests/files/tagged_enum_quoted.zap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
event MyEvent = {
from: Server,
type: Reliable,
call: ManyAsync,
data: enum "tag with spaces" {
foo { "value with spaces": string, bar: u8 },
bar { buzz: u8 },
}
}
6 changes: 6 additions & 0 deletions zap/tests/files/unit_enum_quoted.zap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
event MyEvent = {
from: Server,
type: Reliable,
call: ManyAsync,
data: enum { "Foo Bar", "Bar Foo", Buzz }
}
55 changes: 55 additions & 0 deletions zap/tests/irgen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,58 @@ async fn test_no_data() {

runtime.run("Zap", output).await.unwrap();
}

#[tokio::test]
async fn test_unit_enum_quoted() {
let (config, reports) = parse(include_str!("../files/unit_enum_quoted.zap"));

assert!(config.is_some());
assert!(reports.is_empty());

let default_value = r#""Foo Bar""#;

let default_values: HashMap<&str, Vec<&str>> = HashMap::from([("MyEvent", vec![default_value])]);
let output = TestOutput::new(&config.unwrap(), default_values).output();
let mut runtime = Runtime::new();

runtime.run("Zap", output).await.unwrap();
}

#[tokio::test]
async fn test_struct_quoted_fields() {
let (config, reports) = parse(include_str!("../files/struct_quoted_fields.zap"));

assert!(config.is_some());
assert!(reports.is_empty());

let default_value = r#"{
["foo bar"] = "baz",
buzz = 21
}"#;

let default_values: HashMap<&str, Vec<&str>> = HashMap::from([("MyEvent", vec![default_value])]);
let output = TestOutput::new(&config.unwrap(), default_values).output();
let mut runtime = Runtime::new();

runtime.run("Zap", output).await.unwrap();
}

#[tokio::test]
async fn test_tagged_enum_quoted() {
let (config, reports) = parse(include_str!("../files/tagged_enum_quoted.zap"));

assert!(config.is_some());
assert!(reports.is_empty());

let default_value = r#"{
["tag with spaces"] = "foo",
["value with spaces"] = "buzz",
bar = 21
}"#;

let default_values: HashMap<&str, Vec<&str>> = HashMap::from([("MyEvent", vec![default_value])]);
let output = TestOutput::new(&config.unwrap(), default_values).output();
let mut runtime = Runtime::new();

runtime.run("Zap", output).await.unwrap();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: zap/tests/lune/mod.rs
expression: result
input_file: zap/tests/files/struct_quoted_fields.zap
---
Ok(
0,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: zap/tests/lune/mod.rs
expression: result
input_file: zap/tests/files/tagged_enum_quoted.zap
---
Ok(
0,
)
8 changes: 8 additions & 0 deletions zap/tests/lune/snapshots/run_lune_test@unit_enum_quoted.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: zap/tests/lune/mod.rs
expression: result
input_file: zap/tests/files/unit_enum_quoted.zap
---
Ok(
0,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: zap/tests/selene/mod.rs
expression: client_diagnostics
input_file: zap/tests/files/struct_quoted_fields.zap
---
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: zap/tests/selene/mod.rs
expression: server_diagnostics
input_file: zap/tests/files/struct_quoted_fields.zap
---
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: zap/tests/selene/mod.rs
expression: tooling_diagnostics
input_file: zap/tests/files/struct_quoted_fields.zap
---
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: zap/tests/selene/mod.rs
expression: types_diagnostics
input_file: zap/tests/files/struct_quoted_fields.zap
---
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: zap/tests/selene/mod.rs
expression: client_diagnostics
input_file: zap/tests/files/tagged_enum_quoted.zap
---
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: zap/tests/selene/mod.rs
expression: server_diagnostics
input_file: zap/tests/files/tagged_enum_quoted.zap
---
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: zap/tests/selene/mod.rs
expression: tooling_diagnostics
input_file: zap/tests/files/tagged_enum_quoted.zap
---
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: zap/tests/selene/mod.rs
expression: types_diagnostics
input_file: zap/tests/files/tagged_enum_quoted.zap
---
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: zap/tests/selene/mod.rs
expression: client_diagnostics
input_file: zap/tests/files/unit_enum_quoted.zap
---
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: zap/tests/selene/mod.rs
expression: server_diagnostics
input_file: zap/tests/files/unit_enum_quoted.zap
---
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: zap/tests/selene/mod.rs
expression: tooling_diagnostics
input_file: zap/tests/files/unit_enum_quoted.zap
---
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: zap/tests/selene/mod.rs
expression: types_diagnostics
input_file: zap/tests/files/unit_enum_quoted.zap
---
[]
Loading