Parsing Pipeline¶
Detailed architecture of the IFClite parsing system.
Overview¶
The parsing pipeline transforms STEP/IFC files into structured data. Two independent components cooperate:
- A fast entity scanner (
rust/core/src/parser/scanner.rs): a hand-rolled, memchr-accelerated byte scanner that walks the raw file once and records every entity's id, byte span, and type name. It is quote- and comment-aware and skips the STEP HEADER, but it never builds tokens. - A nom tokenizer (
rust/core/src/parser/tokenizer.rs): a zero-copy combinator lexer used only when an entity's attributes are actually decoded. String-like tokens borrow their original bytes.
On native targets the entity index is additionally built in parallel (rust/processing/src/parallel_scan.rs): the file is split into byte ranges scanned concurrently, and a handoff-stitch pass makes the merged index byte-identical to the serial single-walk scanner. On wasm32 the serial scanner is used directly.
Tokenization¶
Token Types¶
The tokenizer produces structured tokens directly (there are no separate punctuation tokens; lists and typed values are parsed as nested structures):
UnrepresentableReal carries a complete STEP REAL that f64 cannot hold
(1.E400) as raw text; Binary is the undecoded content of a "0FF" literal.
String and Enum tokens borrow their original bytes; STEP escape decoding (\X2\, \S\, ...) happens only when a value crosses a user-facing boundary.
Lexer Architecture¶
Key Lexer Functions¶
// Entity reference: #123 (rust/core/src/parser/tokenizer.rs)
fn entity_ref(input: &[u8]) -> IResult<&[u8], Token<'_>> {
map(preceded(char('#'), express_id), Token::EntityRef)(input)
}
String literals use memchr to find the closing quote (with '' escape handling), "..." binary literals are their own token, and numbers go through the one shared reader read_step_number (fast-float2), the same one every raw-byte reader uses.
Performance Optimizations¶
| Optimization | Technique | Benefit |
|---|---|---|
| Zero-copy | Store byte slices | No allocation |
| SIMD search | memchr crate | Vectorized byte search |
| Fast numbers | fast-float2 behind one literal grammar | Single pass |
| Branch prediction | Ordered alternatives | Better CPU prediction |
Entity Scanning¶
Scanner State Machine¶
Entity Index Structure¶
The entity index is deliberately minimal: a hash map from express ID to the entity's byte span in the file buffer.
// rust/core/src/decoder.rs
pub type EntityIndex = FxHashMap<u32, (usize, usize)>; // id -> (start, end)
The scanner also yields the entity's type name during the same walk, so callers that need type-filtered job lists (the geometry pre-pass, the RTC sampler) get it without a second pass.
Scanning Algorithm¶
The real scanner (EntityScanner in rust/core/src/parser/scanner.rs) is more careful than a naive # hunt:
- Skips the STEP HEADER section first, so a stray
#inside a header string cannot corrupt quote parity for the rest of the file. The marker is found byparser::header::find_keyword_then, the same keyword finder the header reader uses, over one shared 256 KiB window (MAX_HEADER_BYTES):DATA, case-insensitive, on identifier boundaries, trivia allowed before the;, and the ed.3DATA('name',(...));form. A/*that never closes hides everything after it. - Hunts for
#with memchr (SIMD-accelerated), then parses#id = TYPE(...)in place. - Tracks quoted strings and
/* ... */comments so;inside strings never terminates a record. - Returns
(id, type_name, start, end)per entity;build_entity_indexinserts spans with last-wins semantics on duplicate IDs.
On native, parallel_scan shards this walk across all cores and stitches the shard results back into the exact serial output (same key set, same spans, same last-wins order).
Entity Decoding¶
Decoder Architecture¶
Lazy vs Eager Decoding¶
Attribute Value Types¶
Booleans arrive as Enum values (.T. / .F.). A typed value such as IFCBOOLEAN(.T.) is kept as Typed { name, value } (rust/core/src/schema_gen.rs); as_float, as_int, as_string and as_enum see through it, and as_typed / unwrap_typed / typed_value read it explicitly. Tessellation-heavy payloads (coordinate and index lists) can bypass the token pipeline entirely via the zero-allocation fast path in rust/core/src/fast_parse.rs.
Data Building¶
Builder Pipeline¶
Columnar Table Building¶
In the browser, the columnar tables live in @ifc-lite/data and are filled by the single-pass columnar parser in @ifc-lite/parser (columnar-parser.ts):
// packages/data/src/entity-table.ts (abridged)
export class EntityTableBuilder {
expressId: Uint32Array;
typeEnum: Uint16Array;
globalId: Uint32Array; // StringTable index
name: Uint32Array; // StringTable index
description: Uint32Array;
objectType: Uint32Array;
flags: Uint8Array;
containedInStorey: Int32Array;
definedByType: Int32Array;
geometryIndex: Int32Array;
rawTypeName: Uint32Array; // fallback display for unknown types
// add(...) interns strings; build() shrinks arrays to count
}
String-valued columns store indices into a shared deduplicating StringTable. Properties and quantities are NOT bulk-parsed here; the builder records entityId -> psetIds maps and values are decoded on access (see the on-demand extractors).
Relationship Graph Building¶
Browser Scan Paths¶
The TypeScript parser (@ifc-lite/parser) picks the fastest available scan path at runtime (entity-scanner.ts):
| Path | When | How |
|---|---|---|
pre-scanned |
Geometry pre-pass already indexed the file | Reuses the WASM scan's (ids, starts, lengths) arrays, zero rescan |
worker |
Default for large buffers | Inline Web Worker runs the scan off the main thread |
wasm |
WASM API available | scanEntitiesFastBytes (memchr scanner in Rust) |
tokenizer |
Fallback | Pure-TS StepTokenizer scan |
After the scan, the columnar parser (columnar-parser.ts) does a single pass over the entity refs to build tables, relationships, and the spatial hierarchy, yielding to the event loop periodically so the UI stays responsive.
Error Handling¶
Error Types¶
ifc_lite_core::Error (rust/core/src/error.rs) is one enum, not a class
hierarchy:
| Variant | Meaning |
|---|---|
ParseError { position, message } |
Generic parse failure at a byte position |
InvalidEntityRef(u32) |
A reference that does not resolve |
InvalidIfcType(String) |
An unknown type name |
UnexpectedToken { position, expected, got } |
Tokenizer mismatch |
Io, Utf8 |
Wrapped I/O and UTF-8 errors |
UnrepresentableReal(u32) |
A REAL outside the double range, refused as geometry input |
MalformedRecord(u32) |
A point/direction or reference slot that is not valid geometry input |
UnusableUnitFactor { factor, overflowed } |
A length unit factor that is not a positive finite number |
Error Recovery¶
The scanner drops a record it cannot terminate and carries on (rust/core/src/parser/scanner.rs):
- a record with no
;resumes at the)that balances its parameter list; - a record with an unescaped
'resumes at the next anchored#id=declaration; - a record with no balancing
)but readable bytes after it resumes the search just past its own#, so the next declaration is still found.
It stops the whole scan only when nothing after the record is readable, such as
a /* ... */ comment that never closes. A dropped
record is reported through the host-installed report sink
(rust/core/src/parser/malformed_records.rs), so a short model is not silent.
Records refused as geometry input (UnrepresentableReal, MalformedRecord)
are counted in EntityDecoder::refused_records.
Performance Characteristics¶
- Entity scanning is memchr-bound; on native it also parallelizes across cores.
- Decoding dominates when many entities are actually materialized, which is why decoding is lazy and properties are parsed on access.
- Full parse (scan + columnar build) can be a large share of total load time on entity-heavy files, which is what motivated the fast scanner and the on-demand property model.
Measured numbers live in Performance.
Next Steps¶
- Geometry Pipeline - Geometry processing
- Rendering Pipeline - WebGPU rendering
- API Reference - Parser API