#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Action {
None,
Draw,
Fill,
FillDraw,
Clip,
Shade,
ShadeDraw,
Node,
}
impl Action {
fn named(name: &str) -> Option<Action> {
match name {
"path" => Some(Action::None),
"draw" => Some(Action::Draw),
"fill" => Some(Action::Fill),
"filldraw" => Some(Action::FillDraw),
"clip" => Some(Action::Clip),
"shade" => Some(Action::Shade),
"shadedraw" => Some(Action::ShadeDraw),
"node" | "coordinate" => Some(Action::Node),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Chunk {
Path {
action: Action,
options: String,
body: String,
},
Scope { options: String, body: String },
}
pub fn commands(body: &str) -> Vec<Chunk> {
let mut out = Vec::new();
let mut rest = body;
while let Some(at) = rest.find('\\') {
let (before, after) = rest.split_at(at);
if let Some(comment) = last_comment(before) {
rest = match rest[comment..].find('\n') {
Some(newline) => &rest[comment + newline + 1..],
None => "",
};
continue;
}
let name: String = after[1..]
.chars()
.take_while(|c| c.is_ascii_alphabetic())
.collect();
let tail = &after[1 + name.len()..];
match name.as_str() {
"begin" => match environment(tail) {
Some(("scope", options, inner, after)) => {
out.push(Chunk::Scope {
options,
body: inner,
});
rest = after;
}
_ => rest = tail,
},
"foreach" => {
let (expanded, after) = foreach(tail);
out.extend(commands(&expanded));
rest = after;
}
_ => match Action::named(&name) {
Some(action) => {
let (options, text, after) = statement(tail);
let (options, body) = match action {
Action::Node => (String::new(), format!("{name}[{options}] {text}")),
_ => (options, text),
};
out.push(Chunk::Path {
action,
options,
body,
});
rest = after;
}
None => rest = tail,
},
}
}
out
}
fn last_comment(text: &str) -> Option<usize> {
let line = text.rfind('\n').map(|at| at + 1).unwrap_or(0);
text[line..].find('%').map(|at| line + at)
}
fn statement(text: &str) -> (String, String, &str) {
let (options, rest) = bracketed(text);
let mut depth = 0i32;
for (at, ch) in rest.char_indices() {
match ch {
'{' | '[' => depth += 1,
'}' | ']' => depth -= 1,
';' if depth == 0 => {
return (options, rest[..at].to_string(), &rest[at + 1..]);
}
_ => {}
}
}
(options, rest.to_string(), "")
}
fn environment(text: &str) -> Option<(&str, String, String, &str)> {
let open = text.trim_start().strip_prefix('{')?;
let close = open.find('}')?;
let name = &open[..close];
let (options, body) = bracketed(&open[close + 1..]);
let opening = format!("\\begin{{{name}}}");
let closing = format!("\\end{{{name}}}");
let mut depth = 1usize;
let mut at = 0usize;
while at < body.len() {
let next_open = body[at..].find(&opening).map(|i| at + i);
let next_close = body[at..].find(&closing).map(|i| at + i);
match (next_open, next_close) {
(Some(o), Some(c)) if o < c => {
depth += 1;
at = o + opening.len();
}
(_, Some(c)) => {
depth -= 1;
if depth == 0 {
return Some((
name,
options,
body[..c].to_string(),
&body[c + closing.len()..],
));
}
at = c + closing.len();
}
_ => break,
}
}
None
}
fn foreach(text: &str) -> (String, &str) {
let mut rest = text.trim_start();
let mut variables = Vec::new();
while let Some(after) = rest.strip_prefix('\\') {
let name: String = after
.chars()
.take_while(|c| c.is_ascii_alphabetic())
.collect();
if name.is_empty() {
break;
}
rest = after[name.len()..].trim_start();
variables.push(name);
match rest.strip_prefix('/') {
Some(after) => rest = after.trim_start(),
None => break,
}
}
let (_, rest) = bracketed(rest);
let rest = rest.trim_start();
let Some(rest) = rest.strip_prefix("in") else {
return (String::new(), rest);
};
let (list, rest) = braced(rest.trim_start());
let rest = rest.trim_start();
let (body, after) = match rest.starts_with('{') {
true => braced(rest),
false => match rest.find(';') {
Some(at) => (rest[..=at].to_string(), &rest[at + 1..]),
None => (rest.to_string(), ""),
},
};
let mut out = String::new();
for entry in values(&list) {
let mut once = body.clone();
for (variable, value) in variables.iter().zip(entry.split('/')) {
once = substitute(&once, variable, value.trim());
}
out.push_str(&once);
out.push('\n');
}
(out, after)
}
fn values(list: &str) -> Vec<String> {
let entries: Vec<&str> = split_commas(list);
let mut out: Vec<String> = Vec::new();
for (at, entry) in entries.iter().enumerate() {
let entry = entry.trim();
if entry != "..." {
out.push(entry.to_string());
continue;
}
let (Some(last), Some(next)) = (out.last(), entries.get(at + 1)) else {
continue;
};
let (Ok(from), Ok(to)) = (last.parse::<f64>(), next.trim().parse::<f64>()) else {
continue;
};
let step = match out.len() >= 2 {
true => match out[out.len() - 2].parse::<f64>() {
Ok(before) => from - before,
Err(_) => 1.0,
},
false => 1.0,
};
let step = match step == 0.0 {
true => 1.0,
false => step,
};
let mut value = from + step;
while (step > 0.0 && value < to) || (step < 0.0 && value > to) {
out.push(format(value));
value += step;
}
}
out
}
fn format(value: f64) -> String {
match value.fract() == 0.0 {
true => format!("{}", value as i64),
false => format!("{value}"),
}
}
fn substitute(text: &str, variable: &str, value: &str) -> String {
let needle = format!("\\{variable}");
let mut out = String::with_capacity(text.len());
let mut rest = text;
while let Some(at) = rest.find(&needle) {
let after = &rest[at + needle.len()..];
out.push_str(&rest[..at]);
match after.chars().next() {
Some(c) if c.is_ascii_alphabetic() => out.push_str(&needle),
_ => out.push_str(value),
}
rest = after;
}
out.push_str(rest);
out
}
fn split_commas(list: &str) -> Vec<&str> {
let mut out = Vec::new();
let mut depth = 0i32;
let mut start = 0;
for (at, ch) in list.char_indices() {
match ch {
'{' | '(' | '[' => depth += 1,
'}' | ')' | ']' => depth -= 1,
',' if depth == 0 => {
out.push(&list[start..at]);
start = at + 1;
}
_ => {}
}
}
out.push(&list[start..]);
out.into_iter().filter(|e| !e.trim().is_empty()).collect()
}
fn bracketed(text: &str) -> (String, &str) {
let text = text.trim_start();
let Some(open) = text.strip_prefix('[') else {
return (String::new(), text);
};
let mut depth = 0i32;
for (at, ch) in open.char_indices() {
match ch {
'[' | '{' => depth += 1,
'}' => depth -= 1,
']' if depth == 0 => return (open[..at].to_string(), &open[at + 1..]),
']' => depth -= 1,
_ => {}
}
}
(String::new(), text)
}
fn braced(text: &str) -> (String, &str) {
let Some(open) = text.strip_prefix('{') else {
return (String::new(), text);
};
let mut depth = 0i32;
for (at, ch) in open.char_indices() {
match ch {
'{' => depth += 1,
'}' if depth == 0 => return (open[..at].to_string(), &open[at + 1..]),
'}' => depth -= 1,
_ => {}
}
}
(String::new(), text)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn each_command_is_read_with_the_action_it_names() {
let chunks = commands(r"\draw (0,0) -- (1,1); \fill (0,0) circle[radius=1]; \path (2,2);");
assert_eq!(chunks.len(), 3);
match &chunks[0] {
Chunk::Path { action, body, .. } => {
assert_eq!(*action, Action::Draw);
assert_eq!(body.trim(), "(0,0) -- (1,1)");
}
other => panic!("a path, not {other:?}"),
}
assert!(matches!(
chunks[1],
Chunk::Path {
action: Action::Fill,
..
}
));
}
#[test]
fn a_scope_keeps_its_body_and_its_options() {
let chunks = commands(
r"\begin{scope}[xshift=1cm]\draw (0,0) -- (1,0);\end{scope}\draw (2,2) -- (3,3);",
);
assert_eq!(chunks.len(), 2);
match &chunks[0] {
Chunk::Scope { options, body } => {
assert_eq!(options, "xshift=1cm");
assert!(body.contains("(1,0)"));
}
other => panic!("a scope, not {other:?}"),
}
}
#[test]
fn a_loop_writes_its_body_once_per_value() {
let chunks = commands(r"\foreach \x in {0,1,2} { \draw (\x,0) -- (\x,1); }");
assert_eq!(chunks.len(), 3);
let bodies: Vec<String> = chunks
.iter()
.map(|c| match c {
Chunk::Path { body, .. } => body.trim().to_string(),
other => panic!("a path, not {other:?}"),
})
.collect();
assert_eq!(bodies[0], "(0,0) -- (0,1)");
assert_eq!(bodies[2], "(2,0) -- (2,1)");
}
#[test]
fn a_range_is_written_out() {
assert_eq!(values("1,...,4"), vec!["1", "2", "3", "4"]);
assert_eq!(values("0,2,...,6"), vec!["0", "2", "4", "6"]);
}
#[test]
fn a_loop_variable_is_not_matched_by_prefix() {
let out = substitute(r"\draw[xshift=\x cm] (\x,0);", "x", "1");
assert_eq!(out, r"\draw[xshift=1 cm] (1,0);");
let out = substitute(r"\draw[\xshift] (\x,0);", "x", "1");
assert_eq!(out, r"\draw[\xshift] (1,0);");
}
}