Files
makeCAD/crates/doc/src/persistent_name/lexer.rs
T
JIJINBEI cdde7d62b2 Persistent names read by one parser; four misreadings fixed (#5814) (#5966)
* doc: persistent names read as their structure; joint.inspect splits an edge with a blend face first

`persistent_name::{FaceName, EdgeName}` parse a face or edge name once (blends, inner walls,
copies, pieces, numbered pieces, `~k`) and write it back as the same text, bounded against
hostile input. A suffix after a blend's edge naming a later feature belongs to the blend face.

joint.inspect split an edge at its first `|`, so an edge whose first face is a blend
(`F4:blend:F3:+x|F3:+z#[…]|F5:-y`) came back as a cut-off blend name and a string that is no
face. It now uses `EdgeName::parse`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* doc: persistent names as typed values: features, roles, suffixes, a lexer and errors that say where

`FaceName` is a base (made by a feature in a role, a blend and the edge it rounds, an inner
wall, a body's face by index, other text) and its suffixes (copy, piece, numbered piece).
Roles are typed where the vocabulary is known (top, side:<curve>, +x, face:<k>, …) and kept as
written otherwise. The text form is lexed into tokens and read by the grammar in the module's
doc; a name that doesn't read says where and what was expected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* names: read what made a face, its kind and its feature ids through persistent_name

The places that took a name apart by hand to find the feature that made a face, whether it is
a blend, a copy, a numbered piece or a wall, or to remap feature ids on copy, now ask the typed
name (`made_by`, `split_feature`, `is_blend`, `is_copy`, `is_numbered_piece`, `is_wall`,
`features`, `map`). A name that doesn't read falls back to the old text check.

- An offset of a face that only names a blend inside it (`F2:top#[F5:blend:…]`) is no longer
  taken for a fillet's.
- Copying a component remaps feature ids inside a piece's `#[…]` too.
- A shell's inner wall split by a later cut keeps the qualifier on the wall.
- joint.inspect finds the sketch curve of a copied side (`side:l1@F7.2`).

Finding a face by name (strip_piece, parent_piece) is unchanged: it reads a trailing
qualifier as the whole name's and moves once the text form says where it belongs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* engine: regression tests for press-pull on a wall that only names a fillet and joint.inspect on a copied side

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* names: an edge holds one face or more; made_by reads only the leading token

An open boundary's edge has one face and an edge several faces meet at has more, so an edge name
is a list of faces (a blend's own edge still takes two at most, as before). `made_by` reads just
the leading `F<n>:`, which every name a feature made starts with, so asking it of every face
each frame stays cheap. Names are read up to 1 MiB: a face engraved with text has piece names
past 4 KiB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* persistent_name: split into lexer, parser, write, text and tests

No code changed; callers' paths stay the same (`pub use text::*`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 01:48:26 +00:00

94 lines
2.8 KiB
Rust

//! The lexer: a name's text as tokens.
/// A plain decimal number: digits only, no leading zero (so it writes back the same).
pub(super) fn number<T: std::str::FromStr>(d: &str) -> Option<T> {
let ok = !d.is_empty() && d.bytes().all(|b| b.is_ascii_digit()) && (d.len() == 1 || !d.starts_with('0'));
if ok { d.parse().ok() } else { None }
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Kind {
/// `F<n>` before a `:`.
Feature(u64),
/// Text between separators.
Word,
Colon,
Pipe,
Comma,
/// `#[`
Open,
/// `]`
Close,
/// `#<n>`
Hash(u32),
/// `~<n>`
Tie(u32),
/// `@F<n>.<k>`
Copy(u64, u32),
}
#[derive(Clone, Copy, Debug)]
pub(super) struct Tok {
pub(super) kind: Kind,
pub(super) start: usize,
pub(super) end: usize,
}
/// The digits of `s` from byte `i` as a number, and where they end.
fn digits_at<T: std::str::FromStr>(s: &str, i: usize) -> Option<(T, usize)> {
let rest = s.get(i..)?;
let n = rest.bytes().take_while(u8::is_ascii_digit).count();
Some((number(rest.get(..n)?)?, i + n))
}
/// The separator token starting at byte `i`, if one does.
fn separator_at(s: &str, i: usize) -> Option<Tok> {
let b = s.as_bytes();
let tok = |kind, end| Some(Tok { kind, start: i, end });
match b.get(i)? {
b':' => tok(Kind::Colon, i + 1),
b'|' => tok(Kind::Pipe, i + 1),
b',' => tok(Kind::Comma, i + 1),
b']' => tok(Kind::Close, i + 1),
b'#' if b.get(i + 1) == Some(&b'[') => tok(Kind::Open, i + 2),
b'#' => digits_at(s, i + 1).and_then(|(k, end)| tok(Kind::Hash(k), end)),
b'~' => digits_at(s, i + 1).and_then(|(k, end)| tok(Kind::Tie(k), end)),
b'@' if b.get(i + 1) == Some(&b'F') => {
let (feature, dot) = digits_at(s, i + 2)?;
if b.get(dot) != Some(&b'.') {
return None;
}
let (instance, end) = digits_at(s, dot + 1)?;
tok(Kind::Copy(feature, instance), end)
}
_ => None,
}
}
pub(super) fn lex(s: &str) -> Vec<Tok> {
let mut toks = Vec::new();
let mut i = 0;
while i < s.len() {
if let Some(t) = separator_at(s, i) {
toks.push(t);
i = t.end;
continue;
}
// `F<n>` right before a `:` names a feature.
if s.as_bytes().get(i) == Some(&b'F')
&& let Some((n, end)) = digits_at::<u64>(s, i + 1)
&& s.as_bytes().get(end) == Some(&b':')
{
toks.push(Tok { kind: Kind::Feature(n), start: i, end });
i = end;
continue;
}
let start = i;
while i < s.len() && separator_at(s, i).is_none() {
i += s.get(i..).and_then(|r| r.chars().next()).map_or(1, char::len_utf8);
}
toks.push(Tok { kind: Kind::Word, start, end: i });
}
toks
}