Skip to content

Instantly share code, notes, and snippets.

@leaysgur
Created September 7, 2024 07:17
Show Gist options
  • Save leaysgur/1d4cef680979aeb3422e6139f515e413 to your computer and use it in GitHub Desktop.
Save leaysgur/1d4cef680979aeb3422e6139f515e413 to your computer and use it in GitHub Desktop.
oxc_regular_expression_wasm
[package]
name = "oxc_regular_expression_wasm"
version = "0.0.1"
publish = false
authors.workspace = true
description.workspace = true
edition.workspace = true
homepage.workspace = true
keywords.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true
categories.workspace = true
[lints]
workspace = true
[lib]
crate-type = ["cdylib", "rlib"]
test = false
doctest = false
[dependencies]
oxc = { workspace = true, features = ["serialize"] }
oxc_regular_expression = { workspace = true }
serde = { workspace = true, features = ["derive"] }
wasm-bindgen = { workspace = true }
serde-wasm-bindgen = { workspace = true }
tsify = { workspace = true }
// Silence erroneous warnings from Rust Analyser for `#[derive(Tsify)]`
#![allow(non_snake_case)]
use oxc::allocator::Allocator;
use oxc_regular_expression::{ParserOptions, PatternParser};
use serde::{Deserialize, Serialize};
use tsify::Tsify;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(getter_with_clone)]
#[derive(Default, Tsify)]
pub struct Return {
#[tsify(type = "Pattern")]
pub ast: JsValue,
}
#[derive(Debug, Default, Clone, Deserialize, Tsify)]
#[tsify(from_wasm_abi)]
#[serde(rename_all = "camelCase")]
pub struct ParseOptions {
#[tsify(optional)]
pub unicode_mode: Option<bool>,
#[tsify(optional)]
pub unicode_sets_mode: Option<bool>,
}
/// # Errors
/// Serde serialization error
#[wasm_bindgen(js_name = parsePattern)]
pub fn parse_pattern(
source_text: &str,
options: Option<ParseOptions>,
) -> Result<Return, serde_wasm_bindgen::Error> {
let allocator = Allocator::default();
let serializer = serde_wasm_bindgen::Serializer::json_compatible();
let (unicode_mode, unicode_sets_mode) = match options {
Some(options) => {
(options.unicode_mode.unwrap_or(false), options.unicode_sets_mode.unwrap_or(false))
}
None => (false, false),
};
let result = PatternParser::new(
&allocator,
source_text,
ParserOptions { span_offset: 0, unicode_mode, unicode_sets_mode },
)
.parse();
match result {
Ok(parsed) => match parsed.serialize(&serializer) {
Ok(ast) => Ok(Return { ast }),
Err(err) => Err(serde_wasm_bindgen::Error::new(err.to_string())),
},
Err(err) => Err(serde_wasm_bindgen::Error::new(err.to_string())),
}
}
{
"name": "@oxc-regular-expression/wasm",
"version": "0.27.0",
"description": "Wasm target for the oxc parser.",
"packageManager": "[email protected]",
"keywords": [
"JavaScript",
"TypeScript",
"parser"
],
"author": "Boshen and oxc contributors",
"license": "MIT",
"homepage": "https://oxc.rs",
"repository": {
"type": "git",
"url": "https://github.com/oxc-project/oxc",
"directory": "wasm/parser"
},
"funding": {
"url": "https://github.com/sponsors/Boshen"
},
"main": "./node/oxc_regular_expression_wasm.js",
"browser": "./web/oxc_regular_expression_wasm.js",
"types": "./node/oxc_regular_expression_wasm.d.ts",
"files": [
"node",
"web"
],
"scripts": {
"build": "pnpm run build-node && pnpm run build-web && pnpm run copy-files && pnpm run clean-files",
"build-node": "pnpm run build-base --target nodejs --out-dir ../../npm/parser-wasm/node .",
"build-web": "pnpm run build-base --target web --out-dir ../../npm/parser-wasm/web .",
"build-base": "wasm-pack build --release --no-pack",
"copy-files": "cp ./package.json ../../npm/parser-wasm/package.json && cp ./README.md ../../npm/parser-wasm/README.md",
"clean-files": "rm ../../npm/parser-wasm/*/.gitignore",
"test": "node ./test-node.mjs"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment