Skip to content

Commit

Permalink
Fix empty args not recognized in PresetInst (#147)
Browse files Browse the repository at this point in the history
* fix empty arg bug

* separate wasm build script to its own crate

* run fmt
  • Loading branch information
Pistonight committed Nov 3, 2023
1 parent 8cfb3d0 commit 3497ea7
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 20 deletions.
1 change: 0 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ jobs:
- uses: baptiste0928/cargo-install@v2
with:
crate: txtpp
features: cli
- run: task themes:ci client:ci --output group
- run: task build:client --output group
- uses: actions/upload-artifact@v3
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ jobs:
- uses: baptiste0928/cargo-install@v2
with:
crate: txtpp
features: cli
- run: task themes:ci client:ci docs:ci --output group
- run: task themes:check client:check docs:check --output group

Expand Down
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"compiler-core/",
"compiler-macros/",
"compiler-wasm/",
"compiler-wasm/build/",
"web-server/",
]

Expand Down
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ tasks:
- task: client:build

build:wasm:
dir: ./compiler-wasm
dir: ./compiler-wasm/build
deps:
- core:grammar
cmds:
Expand Down
46 changes: 41 additions & 5 deletions compiler-core/src/lang/preset/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ fn from_pt(pt: &pt::Preset) -> PresetInst {
}
let mut args = vec![];
if let Some(pt_args) = &pt.m_args {
args.push(parse_arg(&pt_args.m_first));
match &pt_args.m_first {
Some(arg) => args.push(parse_arg(arg)),
None => args.push("".to_string()),
}
for pt_arg in &pt_args.m_rest {
args.push(parse_arg(&pt_arg.m_arg));
match &pt_arg.m_arg {
Some(arg) => args.push(parse_arg(arg)),
None => args.push("".to_string()),
}
}
}

Expand Down Expand Up @@ -113,9 +119,7 @@ mod test {
}

#[test]
fn test_empty_args_not_allowed() {
assert_eq!(PresetInst::try_parse("hello<>"), None);
assert_eq!(PresetInst::try_parse("_hello::world<>"), None);
fn test_incomplete_args_not_allowed() {
assert_eq!(PresetInst::try_parse("_hello::world>"), None);
assert_eq!(PresetInst::try_parse("_hello::world<"), None);
}
Expand Down Expand Up @@ -172,4 +176,36 @@ mod test {
assert_eq!(PresetInst::try_parse("hello<world> "), None);
assert_eq!(PresetInst::try_parse("hello<world>a"), None);
}

#[test]
fn test_empty_arg() {
assert_eq!(
PresetInst::try_parse("hello::world<>").unwrap(),
PresetInst {
name: "hello::world".to_string(),
args: vec!["".to_string()],
}
);
assert_eq!(
PresetInst::try_parse("hello::world<,,>").unwrap(),
PresetInst {
name: "hello::world".to_string(),
args: vec!["".to_string(), "".to_string(), "".to_string()],
}
);
assert_eq!(
PresetInst::try_parse("hello::world<a,,a>").unwrap(),
PresetInst {
name: "hello::world".to_string(),
args: vec!["a".to_string(), "".to_string(), "a".to_string()],
}
);
assert_eq!(
PresetInst::try_parse("hello::world<,a,>").unwrap(),
PresetInst {
name: "hello::world".to_string(),
args: vec!["".to_string(), "a".to_string(), "".to_string()],
}
);
}
}
5 changes: 2 additions & 3 deletions compiler-core/src/lang/preset/preset.grammar
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ rule SubNamespace(

rule Args(
_: token Symbol"<",
first: ArgText,
first: optional ArgText,
rest: optional ArgListTail+,
trailing_comma: optional token Symbol",",
_: token Symbol">"
);

rule ArgListTail(
_: token Symbol",",
arg: ArgText
arg: optional ArgText
);

rule ArgText(blocks: ArgBlock+);
Expand Down
5 changes: 0 additions & 5 deletions compiler-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name = "compiler-wasm"
version = "0.0.0"
description = "WASM interface of the compiler"
edition = "2021"
default-run = "buildwasm"

[dependencies]
compiler-core = { path = "../compiler-core", features = ["wasm"], default-features = false }
Expand All @@ -21,7 +20,3 @@ tsify = { version = "0.4.5", default-features = false, features = ["js"] }
name = "celercwasm"
path = "src/lib.rs"
crate-type = ["cdylib", "lib"]

[[bin]]
name = "buildwasm"
path = "src/build.rs"
6 changes: 4 additions & 2 deletions compiler-wasm/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ version: '3'
tasks:
dev:
desc: Start wasm builds in watch mode
dir: build
cmds:
- cargo watch -x run
- cargo watch -x run {{.CLI_ARGS}}

build:
desc: Build debug wasm module
dir: build
cmds:
- cargo run
- cargo run {{.CLI_ARGS}}
12 changes: 12 additions & 0 deletions compiler-wasm/build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "compiler-wasm-build"
version = "0.0.0"
description = "Build script for the wasm package"
edition = "2021"
default-run = "buildwasm"

[dependencies]

[[bin]]
name = "buildwasm"
path = "src/build.rs"
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::io::{self, Write};
use std::path::Path;
use std::process::Command;

/// Output directory relative to the wasm package (not current dir)
const OUTPUT_DIR: &str = "../web-client/src/low/celerc";

pub fn main() {
Expand Down Expand Up @@ -49,19 +50,21 @@ fn wasm_pack_build() -> io::Result<()> {
fn build_wasm_pack_command() -> Command {
let mut command = Command::new("wasm-pack");
command.args(["build", "--out-dir", OUTPUT_DIR, "--dev"]);
command.current_dir("..");
command
}

#[cfg(not(debug_assertions))]
fn build_wasm_pack_command() -> Command {
let mut command = Command::new("wasm-pack");
command.args(&["build", "--out-dir", OUTPUT_DIR, "--release"]);
command.current_dir("..");
command
}

fn override_typescript_definitions() -> io::Result<()> {
println!("generating typescript definitions");
let path = Path::new(OUTPUT_DIR).join("celercwasm.d.ts");
let path = Path::new("..").join(OUTPUT_DIR).join("celercwasm.d.ts");
let d_ts = fs::read_to_string(&path)?;

// https://github.com/madonoharu/tsify/issues/37
Expand All @@ -76,7 +79,7 @@ fn add_console_log() -> io::Result<()> {
// open file for appending
let mut file = fs::OpenOptions::new()
.append(true)
.open(Path::new(OUTPUT_DIR).join("celercwasm.js"))?;
.open(Path::new("..").join(OUTPUT_DIR).join("celercwasm.js"))?;

// write to file
writeln!(
Expand Down

0 comments on commit 3497ea7

Please sign in to comment.