Skip to content

Commit

Permalink
Fix numeric input widget. (#11561)
Browse files Browse the repository at this point in the history
  • Loading branch information
Frizi authored Nov 14, 2024
1 parent 159a7a3 commit 008aa19
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions app/rust-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,39 @@ pub fn is_numeric_literal(code: &str) -> bool {
let parsed = PARSER.with(|parser| parser.parse_block(code));
let enso_parser::syntax::tree::Variant::BodyBlock(body) = parsed.variant else { return false };
let [stmt] = &body.statements[..] else { return false };
stmt.expression.as_ref().map_or(false, |expr| match &expr.variant {
let Some(stmt) = &stmt.expression else { return false };
let enso_parser::syntax::tree::Variant::ExpressionStatement(stmt) = &stmt.variant else {
return false;
};
match &stmt.expression.variant {
enso_parser::syntax::tree::Variant::Number(_) => true,
enso_parser::syntax::tree::Variant::UnaryOprApp(app) =>
app.opr.code == "-"
&& app.rhs.as_ref().map_or(false, |rhs| {
matches!(rhs.variant, enso_parser::syntax::tree::Variant::Number(_))
}),
_ => false,
})
}
}

#[wasm_bindgen(start)]
fn main() {
console_error_panic_hook::set_once();
}


#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_is_ident_or_operator() {
assert!(is_numeric_literal("1234"));
assert!(is_numeric_literal("-1234"));
assert!(!is_numeric_literal(""));
assert!(!is_numeric_literal("-"));
assert!(!is_numeric_literal("1-234"));
assert!(!is_numeric_literal("1234!"));
assert!(!is_numeric_literal("1234e5"));
}
}

0 comments on commit 008aa19

Please sign in to comment.