Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unicode input feature #275

Merged
merged 1 commit into from
Dec 5, 2023
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
107 changes: 80 additions & 27 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion numbat-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ rust-version = "1.70"

[dependencies]
anyhow = "1"
rustyline = { version = "12", features = ["derive"] }
rustyline = { version = "13", features = ["derive"] }
dirs = "5"
numbat = { version = "1.8.0", path = "../numbat" }
colored = "2"
Expand Down
17 changes: 16 additions & 1 deletion numbat-cli/src/completer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::{Arc, Mutex};

use numbat::Context;
use numbat::{unicode_input::UNICODE_INPUT, Context};
use rustyline::{
self,
completion::{extract_word, Completer, Pair},
Expand All @@ -20,6 +20,21 @@ impl Completer for NumbatCompleter {
pos: usize,
_: &rustyline::Context<'_>,
) -> rustyline::Result<(usize, Vec<Self::Candidate>)> {
for (patterns, replacement) in UNICODE_INPUT {
for pattern in *patterns {
let backslash_pattern = format!("\\{}", pattern);
if line[..pos].ends_with(&backslash_pattern) {
return Ok((
pos - (1 + pattern.len()),
vec![Pair {
display: backslash_pattern.to_string(),
replacement: replacement.to_string(),
}],
));
}
}
}

if line.starts_with("use ") {
return Ok((
0,
Expand Down
2 changes: 1 addition & 1 deletion numbat/modules/units/misc.nbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ unit bar: Pressure = 100 kPa

@name("Ångström")
@url("https://en.wikipedia.org/wiki/Angstrom")
@aliases(angstroms, Å: short)
@aliases(angstroms, Å: short, Å: short)
unit angstrom: Length = 1e-10 meter

@name("Barn")
Expand Down
8 changes: 8 additions & 0 deletions numbat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod suggestion;
mod tokenizer;
mod typechecker;
mod typed_ast;
pub mod unicode_input;
mod unit;
mod unit_registry;
pub mod value;
Expand Down Expand Up @@ -68,6 +69,7 @@ use unit::BaseUnitAndFactor;
use unit_registry::UnitMetadata;

use crate::prefix_parser::PrefixParserResult;
use crate::unicode_input::UNICODE_INPUT;

#[derive(Debug, Error)]
pub enum NumbatError {
Expand Down Expand Up @@ -215,6 +217,12 @@ impl Context {

let mut words: Vec<_> = KEYWORDS.iter().map(|k| k.to_string()).collect();

for (patterns, _) in UNICODE_INPUT {
for pattern in *patterns {
words.push(pattern.to_string());
}
}

{
for variable in self.variable_names() {
words.push(variable.clone());
Expand Down
77 changes: 77 additions & 0 deletions numbat/src/unicode_input.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// We are following Julia here [1], but we only support
// a small (useful) subset of sequences for now.
// [1] https://docs.julialang.org/en/v1/manual/unicode-input/
pub const UNICODE_INPUT: &[(&[&str], &str)] = &[
// Superscript symbols
(&["^-"], "⁻"),
(&["pm"], "±"),
(&["^1"], "¹"),
(&["^2"], "²"),
(&["^3"], "³"),
(&["^4"], "⁴"),
(&["^5"], "⁵"),
(&["^6"], "⁶"),
(&["^7"], "⁷"),
(&["^8"], "⁸"),
(&["^9"], "⁹"),
// Numbers
(&["1/2"], "½"),
// Operators
(&["cdot"], "⋅"),
(&["cdotp"], "·"),
(&["times"], "×"),
(&["div"], "÷"),
(&["to", "rightarrow"], "→"),
(&["ge"], "≥"),
(&["le"], "≤"),
(&["dots", "ldots"], "…"),
// Greek alphabet
(&["Gamma"], "Γ"),
(&["Delta"], "Δ"),
(&["Theta"], "Θ"),
(&["Lambda"], "Λ"),
(&["Pi"], "Π"),
(&["Sigma"], "Σ"),
(&["Phi"], "Φ"),
(&["Psi"], "Ψ"),
(&["Omega"], "Ω"),
(&["alpha"], "α"),
(&["beta"], "β"),
(&["gamma"], "γ"),
(&["delta"], "δ"),
(&["epsilon"], "ε"),
(&["varepsilon"], "ϵ"),
(&["zeta"], "ζ"),
(&["eta"], "η"),
(&["theta"], "θ"),
(&["vartheta"], "ϑ"),
(&["iota"], "ι"),
(&["kappa"], "κ"),
(&["lambda"], "λ"),
(&["mu"], "μ"),
(&["nu"], "ν"),
(&["xi"], "ξ"),
(&["pi"], "π"),
(&["rho"], "ρ"),
(&["sigma"], "σ"),
(&["tau"], "τ"),
(&["upsilon"], "υ"),
(&["phi"], "ϕ"),
(&["varphi"], "φ"),
(&["chi"], "χ"),
(&["psi"], "ψ"),
(&["omega"], "ω"),
// Units
(&["sterling"], "£"),
(&["yen"], "¥"),
(&["euro"], "€"),
(&["degree"], "°"),
(&["ohm"], "Ω"),
(&["Angstrom"], "Å"),
(&["percent"], "%"),
(&["perthousand"], "‰"),
(&["pertenthousand"], "‱"),
// Constants
(&["hbar"], "ℏ"),
(&["planck"], "ℎ"),
];