Skip to content

Commit

Permalink
Add support for seperate output file locations (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
sasial-dev authored Dec 18, 2023
1 parent 0689d7f commit 63388a8
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 21 deletions.
9 changes: 2 additions & 7 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,19 @@ use zap::run;
struct Args {
#[arg(default_value = "net.zap")]
config: Option<PathBuf>,

#[arg(short, long, default_value = "network")]
output: Option<PathBuf>,
}

fn main() -> Result<()> {
let args = Args::parse();

let config_path = args.config.unwrap();
let output_dir_path = args.output.unwrap();

let config = std::fs::read_to_string(config_path)?;

let code = run(config.as_str())?;

std::fs::create_dir_all(&output_dir_path)?;
std::fs::write(output_dir_path.join("server.luau"), code.server)?;
std::fs::write(output_dir_path.join("client.luau"), code.client)?;
std::fs::write(code.server.path, code.server.contents)?;
std::fs::write(code.client.path, code.client.contents)?;

Ok(())
}
7 changes: 4 additions & 3 deletions docs/.vitepress/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const beforeMount = (monaco: Monaco) => {
const Calls = ["SingleSync", "SingleAsync", "ManySync", "ManyAsync"] as const;
const Options = ["typescript", "writechecks", "casing"] as const;
const Options = ["write_checks", "casing", "output_server", "output_client"] as const;
const Casing = ["PascalCase", "camelCase", "snake_case"] as const;
Expand Down Expand Up @@ -138,8 +138,9 @@ const beforeMount = (monaco: Monaco) => {
opt: Options,
casing: Casing,
typescript: Operators,
writechecks: Operators,
write_checks: Operators,
output_server: [],
output_client: [],
} as const;
monaco.languages.registerTokensProviderFactory("zapConfig", {
Expand Down
4 changes: 2 additions & 2 deletions docs/playground.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import MonacoEditor from "@guolao/vue-monaco-editor";
import type { Monaco } from "@monaco-editor/loader";
import { useData, useRouter } from "vitepress";
import { ref, watch, onMounted } from "vue";
import { run_wasm, Code } from "../zap/package"
import { run, Code } from "../zap/package"

const { isDark } = useData();
const { go } = useRouter();
Expand Down Expand Up @@ -78,7 +78,7 @@ const clamp = (number, min, max) => Math.max(min, Math.min(number, max));

watch(code, (newCode) => {
try {
compiledResult.value = run_wasm(newCode);
compiledResult.value = run(newCode);

if (!compiledResult.value.client && !compiledResult.value.server) compiledResult.value = {
client: "-- Add an event to see output here!\n",
Expand Down
46 changes: 41 additions & 5 deletions zap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ mod parser;
mod util;

use thiserror::Error;

#[cfg(not(target_arch = "wasm32"))]
use std::path::PathBuf;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

Expand All @@ -21,23 +25,55 @@ pub enum Error {
DuplicateEvent(String),
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter_with_clone))]
#[derive(Debug)]
#[cfg(not(target_arch = "wasm32"))]
pub struct Output {
pub path: PathBuf,
pub contents: String,
}

// wasm_bindgen doesn't support generics, so we must have two different Structs
#[derive(Debug)]
#[cfg(not(target_arch = "wasm32"))]
pub struct Code {
pub server: Output,
pub client: Output,
}

#[derive(Debug)]
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(getter_with_clone)]
pub struct Code {
pub server: String,
pub client: String,
}

#[cfg(not(target_arch = "wasm32"))]
pub fn run(config: &str) -> Result<Code, Error> {
let file = parser::parse(config)?;

let server_contents = output::server::code(&file);
let client_contents = output::client::code(&file);

Ok(Code {
server: output::server::code(&file),
client: output::client::code(&file),
server: Output {
path: file.server_output,
contents: server_contents,
},
client: Output {
path: file.client_output,
contents: client_contents,
},
})
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn run_wasm(config: &str) -> Result<Code, JsError> {
Ok(run(config)?)
pub fn run(config: &str) -> Result<Code, JsError> {
let file = parser::parse(config)?;

Ok(Code {
server: output::server::code(&file),
client: output::client::code(&file),
})
}
14 changes: 11 additions & 3 deletions zap/src/parser/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,31 @@ pub File: File = <opts:Opt*> <decls:Decl*> => {
let mut casing = Casing::Pascal;
let mut write_checks = false;

let mut server_output = PathBuf::from("network/server.luau");
let mut client_output = PathBuf::from("network/client.luau");

for opt in opts {
match opt {
Opt::Casing(v) => casing = v,
Opt::WriteChecks(v) => write_checks = v,
Opt::ServerOutput(v) => server_output = v,
Opt::ClientOutput(v) => client_output = v,
}
}

File { ty_decls, ev_decls, casing, write_checks }
File { ty_decls, ev_decls, casing, write_checks, server_output, client_output }
};

Opt: Opt = {
"opt" "casing" "=" "camelCase" => Opt::Casing(Casing::Camel),
"opt" "casing" "=" "snake_case" => Opt::Casing(Casing::Snake),
"opt" "casing" "=" "PascalCase" => Opt::Casing(Casing::Pascal),

"opt" "writechecks" "=" "true" => Opt::WriteChecks(true),
"opt" "writechecks" "=" "false" => Opt::WriteChecks(false),
"opt" "write_checks" "=" "true" => Opt::WriteChecks(true),
"opt" "write_checks" "=" "false" => Opt::WriteChecks(false),

"opt" "output_server" "=" <r"[^\s]+\.luau?"> => Opt::ServerOutput(PathBuf::from(<>)),
"opt" "output_client" "=" <r"[^\s]+\.luau?"> => Opt::ClientOutput(PathBuf::from(<>)),
}

Decl: Decl = {
Expand Down
5 changes: 4 additions & 1 deletion zap/src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashSet;
use std::{collections::HashSet, path::PathBuf};

use lalrpop_util::lalrpop_mod;

Expand All @@ -16,6 +16,9 @@ pub struct File {
pub ty_decls: Vec<TyDecl>,
pub ev_decls: Vec<EvDecl>,

pub server_output: PathBuf,
pub client_output: PathBuf,

pub casing: Casing,
pub write_checks: bool,
}
Expand Down
4 changes: 4 additions & 0 deletions zap/src/parser/working_ast.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use super::{Casing, EvCall, EvDecl, EvSource, EvType, Ty, TyDecl};

#[derive(Debug, Clone)]
Expand All @@ -16,6 +18,8 @@ pub enum Decl {

#[derive(Debug, Clone)]
pub enum Opt {
ServerOutput(PathBuf),
ClientOutput(PathBuf),
Casing(Casing),
WriteChecks(bool),
}

0 comments on commit 63388a8

Please sign in to comment.