pub mod ast;
pub mod diagnostic;
pub mod eval;
pub mod geom;
pub mod ir;
pub mod lexer;
mod math;
pub mod parser;
pub mod svg;
pub mod token;
pub use ast::{IncludeCtx, IncludePolicy};
pub use diagnostic::{CompileError, Diagnostic, Span};
pub use eval::{EvalError, eval};
pub use ir::Drawing;
pub use lexer::{LexError, lex};
pub use math::{MathSpan, set_math_renderer};
pub use parser::{ParseError, parse, parse_in_dir, parse_with_prelude};
pub use svg::to_svg;
pub use token::Token;
pub const CIRCUITS: &str = include_str!("std/circuits.pic");
pub fn compile(src: &str) -> Result<Drawing, String> {
compile_in_dir(src, None)
}
pub fn compile_in_dir(src: &str, base: Option<&std::path::Path>) -> Result<Drawing, String> {
let picture = parser::parse_in_dir(src, base).map_err(|e| e.to_string())?;
eval(&picture).map_err(|e| e.to_string())
}
pub fn render_svg(src: &str) -> Result<String, String> {
Ok(to_svg(&compile(src)?))
}
pub fn render_svg_in_dir(src: &str, base: Option<&std::path::Path>) -> Result<String, String> {
Ok(to_svg(&compile_in_dir(src, base)?))
}
pub fn animations_json(d: &Drawing) -> String {
let mut s = String::from("[");
for (i, a) in d.anims.iter().enumerate() {
if i > 0 {
s.push(',');
}
s.push_str(&format!(
"{{\"id\":\"s{}\",\"effect\":\"{}\",\"start\":{},\"duration\":{}}}",
a.shape,
json_str(&a.effect),
a.start,
a.duration
));
}
s.push(']');
s
}
pub fn diagnostics_json(d: &Drawing) -> String {
let mut s = String::from("[");
for (i, line) in d.diagnostics.iter().enumerate() {
if i > 0 {
s.push(',');
}
s.push('"');
s.push_str(&json_str(line));
s.push('"');
}
s.push(']');
s
}
#[derive(Debug, Clone, Default)]
pub struct CompileOptions {
pub circuits: bool,
pub texlabels: bool,
pub base: Option<std::path::PathBuf>,
pub includes: IncludePolicy,
}
pub fn compile_with_options(src: &str, opts: &CompileOptions) -> Result<Drawing, String> {
compile_with_diagnostics(src, opts).map_err(|e| e.message)
}
pub fn compile_with_diagnostics(src: &str, opts: &CompileOptions) -> Result<Drawing, CompileError> {
let picture = parse_options(src, opts).map_err(|e| CompileError {
message: e.to_string(),
info: Box::new(e.diagnostic()),
})?;
eval(&picture).map_err(|e| {
let info = e
.info
.clone()
.unwrap_or_else(|| Box::new(Diagnostic::new("eval", e.msg.clone())));
CompileError {
message: e.msg,
info,
}
})
}
pub fn render_svg_with_options(src: &str, opts: &CompileOptions) -> Result<String, String> {
Ok(to_svg(&compile_with_options(src, opts)?))
}
fn parse_options(src: &str, opts: &CompileOptions) -> Result<ast::Picture, ParseError> {
parser::parse_with_prelude(
src,
ast::IncludeCtx::with_policy(opts.base.clone(), opts.includes),
opts.circuits,
opts.texlabels,
)
}
pub fn compile_json(src: &str) -> String {
compile_json_with_options(src, &CompileOptions::default())
}
pub fn compile_json_in_dir(src: &str, base: Option<&std::path::Path>) -> String {
compile_json_with_options(
src,
&CompileOptions {
base: base.map(|p| p.to_path_buf()),
..Default::default()
},
)
}
pub fn compile_json_with_options(src: &str, opts: &CompileOptions) -> String {
match compile_with_diagnostics(src, opts) {
Ok(d) => drawing_json(&d),
Err(e) => error_json(&e.message, &e.info),
}
}
fn drawing_json(d: &Drawing) -> String {
format!(
"{{\"svg\":\"{}\",\"animations\":{},\"diagnostics\":{},\"warnings\":{}}}",
json_str(&to_svg(d)),
animations_json(d),
diagnostics_json(d),
diagnostics_json_structured(&d.warnings)
)
}
fn error_json(message: &str, diagnostic: &Diagnostic) -> String {
format!(
"{{\"error\":\"{}\",\"error_info\":{},\"warnings\":[]}}",
json_str(message),
diagnostic_json(diagnostic)
)
}
fn diagnostics_json_structured(items: &[Diagnostic]) -> String {
let mut s = String::from("[");
for (i, d) in items.iter().enumerate() {
if i > 0 {
s.push(',');
}
s.push_str(&diagnostic_json(d));
}
s.push(']');
s
}
fn diagnostic_json(d: &Diagnostic) -> String {
let mut s = String::from("{");
s.push_str(&format!("\"message\":\"{}\"", json_str(&d.message)));
s.push_str(&format!(",\"line\":{}", json_opt_u32(d.line)));
s.push_str(&format!(",\"col\":{}", json_opt_u32(d.col)));
s.push_str(&format!(",\"end_col\":{}", json_opt_u32(d.end_col)));
s.push_str(&format!(",\"file\":{}", json_opt_str(d.file.as_deref())));
s.push_str(&format!(",\"kind\":\"{}\"", json_str(&d.kind)));
s.push_str(&format!(",\"found\":{}", json_opt_str(d.found.as_deref())));
s.push_str(&format!(
",\"expected\":{}",
json_opt_str(d.expected.as_deref())
));
s.push_str(&format!(",\"hint\":{}", json_opt_str(d.hint.as_deref())));
s.push('}');
s
}
fn json_opt_u32(v: Option<u32>) -> String {
v.map(|n| n.to_string()).unwrap_or_else(|| "null".into())
}
fn json_opt_str(v: Option<&str>) -> String {
v.map(|s| format!("\"{}\"", json_str(s)))
.unwrap_or_else(|| "null".into())
}
fn json_str(s: &str) -> String {
let mut o = String::with_capacity(s.len() + 8);
for c in s.chars() {
match c {
'"' => o.push_str("\\\""),
'\\' => o.push_str("\\\\"),
'\n' => o.push_str("\\n"),
'\r' => o.push_str("\\r"),
'\t' => o.push_str("\\t"),
c if (c as u32) < 0x20 => o.push_str(&format!("\\u{:04x}", c as u32)),
c => o.push(c),
}
}
o
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn json_bundles_svg_and_animations() {
let j = compile_json("box\nanimate last box with \"fade\"");
assert!(j.starts_with("{\"svg\":\"<svg"));
assert!(j.contains("<g id=\\\"s0\\\">")); assert!(j.contains("\"animations\":[{\"id\":\"s0\",\"effect\":\"fade\""));
assert!(j.contains("\"diagnostics\":[]"));
}
#[test]
fn json_reports_errors() {
let j = compile_json("copy \"oops\"");
assert!(j.contains("\"error\""));
assert!(j.contains("\"error_info\""));
}
#[test]
fn json_reports_print_diagnostics() {
let j = compile_json("print \"hi\"\nprint 2+3");
assert!(j.contains("\"diagnostics\":[\"hi\",\"5\"]"));
}
#[test]
fn copy_circuits_loads_the_embedded_library() {
let body = "A:(0,0); B:(2,0)\nresistor(A,B)";
let via_copy = compile(&format!("copy \"circuits\"\n{body}")).unwrap();
let via_flag = compile(&format!("{CIRCUITS}\n{body}")).unwrap();
assert_eq!(to_svg(&via_copy), to_svg(&via_flag));
}
#[test]
fn copy_circuits_is_idempotent_with_the_flag() {
let body = "A:(0,0); B:(2,0)\nresistor(A,B)";
let both = compile(&format!("{CIRCUITS}\ncopy \"circuits\"\n{body}")).unwrap();
let flag = compile(&format!("{CIRCUITS}\n{body}")).unwrap();
assert_eq!(to_svg(&both), to_svg(&flag));
}
#[test]
fn options_preludes_do_not_shift_user_positions() {
let opts = CompileOptions {
circuits: true,
texlabels: true,
..Default::default()
};
let j = compile_json_with_options("bxo\n", &opts);
assert!(j.contains("\"line\":1"), "{j}");
assert!(j.contains("\"col\":1"), "{j}");
assert!(j.contains("\"file\":null"), "{j}");
assert!(j.contains("\"error\":\"1:1: expected an object"), "{j}");
}
#[test]
fn options_output_matches_the_prepending_it_replaces() {
let body = "A:(0,0); B:(2,0)\nresistor(A,B)";
let opts = CompileOptions {
circuits: true,
..Default::default()
};
let via_opts = compile_with_options(body, &opts).unwrap();
let via_prepend = compile(&format!("{CIRCUITS}\n{body}")).unwrap();
assert_eq!(to_svg(&via_opts), to_svg(&via_prepend));
}
#[test]
fn options_texlabels_is_an_initializer_only() {
let opts = CompileOptions {
texlabels: true,
..Default::default()
};
let j = compile_json_with_options("texlabels = 0\nbox \"$x$\"\n", &opts);
assert!(!j.contains("no math renderer"), "{j}");
}
#[test]
fn diagnostics_inside_an_include_name_the_file() {
let dir = std::env::temp_dir().join(format!("rpic_incl_diag_{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("warn.pic"), "# comment\nbox \"a\" dashd\n").unwrap();
let j = compile_json_in_dir("circle\ncopy \"warn.pic\"", Some(dir.as_path()));
assert!(j.contains("\"kind\":\"ignored_attribute\""), "{j}");
assert!(j.contains("\"file\":\"warn.pic\""), "{j}");
assert!(j.contains("\"line\":2"), "{j}");
std::fs::write(dir.join("bad.pic"), "bxo\n").unwrap();
let e = compile_json_in_dir("circle\ncopy \"bad.pic\"", Some(dir.as_path()));
let _ = std::fs::remove_dir_all(&dir);
assert!(
e.contains("\"error\":\"bad.pic:1:1: expected an object"),
"{e}"
);
assert!(e.contains("\"file\":\"bad.pic\""), "{e}");
assert!(e.contains("\"line\":1"), "{e}");
}
#[test]
fn include_policy_sandboxes_and_denies() {
let root = std::env::temp_dir().join(format!("rpic_inc_policy_{}", std::process::id()));
let base = root.join("base");
std::fs::create_dir_all(&base).unwrap();
std::fs::write(root.join("outside.pic"), "circle\n").unwrap();
std::fs::write(base.join("inc.pic"), "box wid 0.5 ht 0.5\n").unwrap();
let sandboxed = CompileOptions {
base: Some(base.clone()),
includes: IncludePolicy::SandboxedToBase,
..Default::default()
};
assert!(compile_with_options("copy \"inc.pic\"\nbox", &sandboxed).is_ok());
let esc = compile_json_with_options("copy \"../outside.pic\"\nbox", &sandboxed);
assert!(esc.contains("\"kind\":\"include_denied\""), "{esc}");
assert!(esc.contains("outside the include base directory"), "{esc}");
assert!(esc.contains("\"line\":1"), "{esc}");
let abs_src = format!("copy \"{}\"\nbox", root.join("outside.pic").display());
let abs = compile_json_with_options(&abs_src, &sandboxed);
assert!(abs.contains("\"kind\":\"include_denied\""), "{abs}");
assert!(abs.contains("absolute paths are not allowed"), "{abs}");
#[cfg(unix)]
{
let link = base.join("link.pic");
let _ = std::fs::remove_file(&link);
std::os::unix::fs::symlink(root.join("outside.pic"), &link).unwrap();
let sym = compile_json_with_options("copy \"link.pic\"\nbox", &sandboxed);
assert!(sym.contains("\"kind\":\"include_denied\""), "{sym}");
}
assert!(
compile_with_options(
"copy \"circuits\"\nA:(0,0); B:(1,0)\nresistor(A,B)",
&sandboxed
)
.is_ok()
);
let deny = CompileOptions {
base: Some(base.clone()),
includes: IncludePolicy::Deny,
..Default::default()
};
let d = compile_json_with_options("copy \"inc.pic\"\nbox", &deny);
assert!(d.contains("\"kind\":\"include_denied\""), "{d}");
assert!(d.contains("disabled by the include policy"), "{d}");
assert!(compile_with_options("copy \"circuits\"\nbox", &deny).is_ok());
let open = CompileOptions {
base: Some(base.clone()),
..Default::default()
};
assert!(compile_with_options("copy \"../outside.pic\"\nbox", &open).is_ok());
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn json_in_dir_resolves_copy_includes() {
let dir = std::env::temp_dir().join(format!("rpic_json_copy_{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("inc.pic"), "box wid 0.5 ht 0.5\n").unwrap();
let j = compile_json_in_dir("copy \"inc.pic\"\ncircle", Some(dir.as_path()));
let _ = std::fs::remove_dir_all(&dir);
assert!(j.starts_with("{\"svg\":\"<svg"), "{j}");
assert!(j.contains("<rect"), "{j}");
assert!(j.contains("<circle"), "{j}");
assert!(j.contains("\"diagnostics\":[]"), "{j}");
assert!(!j.contains("\"error\""), "{j}");
}
#[test]
fn json_error_info_uses_user_facing_tokens_and_spans() {
let j = compile_json("bxo\n");
assert!(
j.contains("\"error\":\"1:1: expected an object, found `bxo`\""),
"{j}"
);
assert!(j.contains("\"kind\":\"expected_token\""), "{j}");
assert!(j.contains("\"line\":1"), "{j}");
assert!(j.contains("\"col\":1"), "{j}");
assert!(j.contains("\"end_col\":4"), "{j}");
assert!(j.contains("\"found\":\"`bxo`\""), "{j}");
assert!(j.contains("\"expected\":\"an object\""), "{j}");
assert!(j.contains("\"hint\":\"did you mean `box`?\""), "{j}");
}
#[test]
fn json_unterminated_string_points_at_opening_quote() {
let j = compile_json("box wid 1\n \"oops\n");
assert!(j.contains("\"kind\":\"unterminated_string\""), "{j}");
assert!(j.contains("\"line\":2"), "{j}");
assert!(j.contains("\"col\":3"), "{j}");
}
#[test]
fn json_eval_errors_get_structured_locations_when_possible() {
let label = compile_json("box at A\n");
assert!(label.contains("\"kind\":\"unknown_label\""), "{label}");
assert!(label.contains("\"line\":1"), "{label}");
assert!(label.contains("\"col\":8"), "{label}");
assert!(label.contains("\"found\":\"A\""), "{label}");
let ordinal = compile_json("box\nbox at 3rd box\n");
assert!(
ordinal.contains("\"kind\":\"ordinal_out_of_range\""),
"{ordinal}"
);
assert!(ordinal.contains("\"line\":2"), "{ordinal}");
assert!(ordinal.contains("\"col\":8"), "{ordinal}");
assert!(ordinal.contains("\"found\":\"3\""), "{ordinal}");
assert!(ordinal.contains("\"expected\":\"1..1\""), "{ordinal}");
}
#[test]
fn json_eval_error_inside_include_names_the_file() {
let dir = std::env::temp_dir().join(format!("rpic_eval_incl_{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("inc-eval.pic"), "# comment\nbox at Missing\n").unwrap();
let j = compile_json_in_dir("circle\ncopy \"inc-eval.pic\"", Some(dir.as_path()));
let _ = std::fs::remove_dir_all(&dir);
assert!(j.contains("\"error\":\"unknown label `Missing`\""), "{j}");
assert!(j.contains("\"kind\":\"unknown_label\""), "{j}");
assert!(j.contains("\"file\":\"inc-eval.pic\""), "{j}");
assert!(j.contains("\"line\":2"), "{j}"); assert!(j.contains("\"col\":8"), "{j}");
}
#[test]
fn json_deferred_parse_errors_keep_their_structure() {
let j = compile_json("x = 1\nif x then { bxo }\n");
assert!(j.contains("\"kind\":\"expected_token\""), "{j}");
assert!(j.contains("\"line\":2"), "{j}");
assert!(j.contains("\"col\":13"), "{j}");
assert!(j.contains("\"found\":\"`bxo`\""), "{j}");
assert!(j.contains("\"hint\":\"did you mean `box`?\""), "{j}");
let f = compile_json("for i = 1 to 2 do { bxo }\n");
assert!(f.contains("\"kind\":\"expected_token\""), "{f}");
assert!(f.contains("\"hint\":\"did you mean `box`?\""), "{f}");
}
#[test]
fn json_success_bundle_reports_warnings() {
let attr = compile_json("box \"a\" dashd\n");
assert!(attr.contains("\"warnings\":[{"), "{attr}");
assert!(attr.contains("\"kind\":\"ignored_attribute\""), "{attr}");
assert!(attr.contains("\"found\":\"dashd\""), "{attr}");
assert!(
attr.contains("\"hint\":\"did you mean `dashed`?\""),
"{attr}"
);
let anim = compile_json("box\nanimate 1st box with \"zoom\"\n");
assert!(
anim.contains("\"kind\":\"unknown_animation_effect\""),
"{anim}"
);
assert!(anim.contains("\"found\":\"zoom\""), "{anim}");
assert!(anim.contains("\"line\":2"), "{anim}");
}
#[test]
fn circuits_library_compiles_and_draws() {
let src = format!(
"{}\nA:(0,0); B:(1.6,0); C:(3,0)\nresistor(A,B)\ncapacitor(A,B)\ninductor(A,B)\n\
diode(A,B)\nbattery(A,B)\nground(A)\ndot(A)\nwire(A,B)\n\
and_gate(C)\nor_gate(C)\nnand_gate(C)\nnor_gate(C)\nxor_gate(C)\n\
xnor_gate(C)\nbuffer(C)\nnot_gate(C)\n\
opamp(C)\nnpn(C)\npnp(C)\nac_source(A,B)\nswitch(A,B)\n\
potentiometer(A,B)\ntransformer(C)\n\
nmos(C)\npmos(C)\ncurrent_source(A,B)\nvsource_ctrl(A,B)\n\
isource_ctrl(A,B)\nled(A,B)\nphotodiode(A,B)\nzener(A,B)\n\
clabel(A,\"x\")\nterminal(A)\nvdd(A)\nantenna(A)\ncurrent(A,B)\n\
voltage(A,B)\nlamp(A,B)\nfuse(A,B)\n\
voltmeter(A,B)\nammeter(A,B)\nohmmeter(A,B)\nmeter(A,B,\"X\")\n\
crystal(A,B)\nspeaker(A,B)\nhop(A,B)\nspdt(A,B,C)\n\
iec_resistor(A,B)\nvoltage_source(A,B)\npushbutton(A,B)\nrelay(A,B)\n\
thermistor(A,B)\nmotor(A,B)\ngenerator(A,B)\nbell(A,B)\n\
ieee_and(C)\nieee_or(C)\nieee_xor(C)\nieee_buf(C)\niecgate(C,\"x\")\n\
varistor(A,B)\ntline(A,B)\nbus(A,B)\n\
polcap(A,B)\nvarcap(A,B)\nvarind(A,B)\nschottky(A,B)\nldr(A,B)\n\
spark_gap(A,B)\nchassis_ground(A)\nsignal_ground(A)\n\
njfet(C)\npjfet(C)\nphototransistor(C)\nic_block(C,\"x\")\nmux(C)\n\
solar_cell(A,B)\nmicrophone(A,B)\nthermocouple(A,B)",
CIRCUITS
);
let d = compile(&src).expect("circuit library should compile");
assert!(!d.shapes.is_empty());
}
}