Repository navigation
feat(obs-util): port util/cf-tokenizer.c to Rust (#6) - #49
Conversation
|
Maintainer update: rebased onto master as a linear history. Dropped the cf-tokenizer.c extraction commit (merged via #42) and the two merge commits; merged export-list/oracle build.rs/oracle lib.rs entries with #61 (base) and vec2. Added one fix commit: master's #60 (strref compare returns 1 when only the second strref is empty) and #54 (cf_literal_to_str stops at the closing quote) changed the C the ports characterized, so the Rust ports and their Tier 1 expectations now match; parity tests were failing before. Local gate green (lint, rust-test, Tier 2 OFF/ON, exports IDENTICAL 1752) and stamped. Note: #68 also ports lexer.c and will overlap with this. Still a draft for you to mark ready. |
4b1e887 to
c879c12
Compare
c879c12 to
f40241a
Compare
|
Rebased onto current master (#66 and #74 landed), still linear. Pushed with Changes besides the rebase:
Locally on Windows: |
f40241a to
9f6b63a
Compare
163d247 to
b7383ba
Compare
|
Rebased onto master 56f6067 (resolved an rust-exports.txt conflict with obs_nal_find_startcode); kept the #54/#60 adaptations. RED commits verified: cf_tokenizer 0/14 and lexer 0/16 pass at the todo!() commits; at head all obs-util tests pass. Local gate (lint, rust-test, tier2-linux) passed and stamped this commit. The earlier Windows failure was an MSBuild file-lock flake. Note: #68 merged into grind/meta-4-c7d3, not master, so the lexer port here still overlaps it. |
55518bd to
cd8a67e
Compare
Adds the C ABI shim for cf_literal_to_str, cf_lexer_init, cf_lexer_free and cf_lexer_lex (libobs/util/cf-tokenizer.c), the ENABLE_RUST_LIBOBS guard, rust-exports.txt entries, and Tier 1, layout and Tier 3 tests. The safe core bodies are todo!(), so soldr cargo test -p obs-util --test cf_tokenizer fails 0/14 and --test cf_tokenizer_parity aborts on the first call; the layout test passes. The oracle renames the lexer.c functions it calls to their oracle_ names, so it runs on the C lexer oracle rather than on the Rust lexer port. Refs obs-rust#6 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Implements the safe core of the C-family lexer and cf_literal_to_str. GREEN for Tier 1 (14/14), the layout tests (2/2) and Tier 3 (5/5). cf_lex returns the reformatted text and tokens as offsets; the shim allocates file, the base lexer text, reformatted and the token darray through bmalloc exactly as C does and turns the offsets into the pointers the C preprocessor and parsers walk. The text is read as if NUL bytes followed it. That matches C wherever C stays in bounds; where C reads past the terminator (a comment or string ending in a line splice at the end of the text, or \x/octal escapes skipping past a literal's NUL) the port reads NUL. A NULL cf_literal_to_str literal returns NULL, where C dereferences it. \x and octal escapes keep C's strtoul semantics, including the overflow to 0xff at the width of unsigned long (32 bits on Windows, 64 on Linux and macOS). Refs obs-rust#6 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Master fixed two C quirks the lexer/cf-tokenizer ports had characterized: strref_cmp_strref/strref_cmpi_strref now return 1 when only the second strref is empty (obs-rust#60), and cf_literal_to_str stops at the closing quote instead of leaking it after an escape (obs-rust#54). Port both and update the Tier 1 expectations; the parity tests against the C oracle now pass. Co-Authored-By: Claude Opus 5.5 <[email protected]> Local-Gate: v1 tree=b9700f40fdf2c959859b23729ac355aac18f8611 secs=94 lanes=lint:run,rust-test:run,tier2-linux:run Ci-Attestation: {"at":1791659811,"gate":"rust/all/fmt","host":"linux-x86_64","key":"80b41cabc2b99754af82351b24464077a0f2c3b5e8e0435931a367feff4ba543","lane":"lint","parents":["24c28c018467ba0a96fa769c9dad315b93364ee6"],"secs":21,"stamp":"e78152623289d105c70f3f12be5fbaa3","tree":"b9700f40fdf2c959859b23729ac355aac18f8611","v":1,"via":"run"} Ci-Attestation: {"at":1791659811,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"80b41cabc2b99754af82351b24464077a0f2c3b5e8e0435931a367feff4ba543","lane":"lint","parents":["24c28c018467ba0a96fa769c9dad315b93364ee6"],"secs":21,"stamp":"b968c0775d5f1beddcf661bf919941da","tree":"b9700f40fdf2c959859b23729ac355aac18f8611","v":1,"via":"run"} Ci-Attestation: {"at":1791659811,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"caf8e4acd1f26e9c504c4828ce9275052454638ab4735c03c11aba9d7e9b72f2","lane":"rust-test","parents":["24c28c018467ba0a96fa769c9dad315b93364ee6"],"secs":21,"stamp":"709942d4b526e2d335967e90ca1a2d42","tree":"b9700f40fdf2c959859b23729ac355aac18f8611","v":1,"via":"run"} Ci-Attestation: {"at":1791659811,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"127862c3824d34ae1b11b28ac0aa24185302d5538923458e438655efe47de3ef","lane":"tier2-linux","parents":["24c28c018467ba0a96fa769c9dad315b93364ee6"],"secs":52,"stamp":"cb6ddd2624a4bf11e4728568e684301c","tree":"b9700f40fdf2c959859b23729ac355aac18f8611","v":1,"via":"run"}
|
Rebased onto master after #59 landed #68's lexer port. I dropped this PR's own lexer port (its RED and GREEN commits and the lexer-notes doc commit) and adapted cf_tokenizer to master's lexer API ( |
cd8a67e to
fbec99f
Compare
Refs #43 (Phase 3 tracking; #6 closed when #33 merged)
Ports
libobs/util/cf-tokenizer.c(the C-family lexer extracted fromcf-lexer.cin #42) to Rust:cf_literal_to_str,cf_lexer_init,cf_lexer_free,cf_lexer_lex. The preprocessor incf-lexer.cstays in C and keeps consuming the same token arrays.Commits
lexer.cRED, then GREEN (from feat(obs-util): port util/lexer.c to Rust (#6) #41).cf_token/cf_lexermirrors, the oracle, the guard,rust-exports.txtand the tests, against atodo!()core. Tier 1 fails 0/14, Tier 3 aborts on the first call, and the layout tests pass.Design
cf_lex(text) -> CfLexed { reformatted, tokens, offset, unexpected_eof }returns tokens as offsets (strintoreformatted,unmergedinto the input). It reuses the safelexer::get_base_tokenandstrref_cmpfrom feat(obs-util): port util/lexer.c to Rust (#6) #41.bmalloc:file, the base lexer'sbstrduptext,reformatted(strlen + 1), and the token darray, one push at a time so capacity growth matches. It then turns the offsets into the pointers thatcf_preprocessand the shader/effect parsers walk.oracle/cf_tokenizer.calso renames thelexer.cfunctions it calls, so it runs on the C lexer oracle, not on the Rust lexer port.Quirks kept (characterized, not endorsed)
unmerged_stris one byte too long: the opening quote is counted twice.\xtakes as many hex digits asstrtoulaccepts, then skips exactly 2. Octal escapes start parsing at their second digit and skip 3. Unknown escapes write nothing.strtoulsemantics are emulated: leading whitespace, sign,0xprefix, and overflow →ULONG_MAX→0xffat the platform'sunsigned longwidth. That is 32-bit on Windows and 64-bit elsewhere, so\x123456789gives0xffon Windows and0x89on Linux, as in C. The core takes the width as a parameter, and both widths are unit-tested.Intentional differences (C over-reads)
\+ newline) inside a comment or string makes C read one byte past the NUL of thebstrdup'd text, a heap over-read in today's code. The port reads NUL there. Parity inputs never end in a splice.cf_literal_to_str,\x/octal escapes can skip past the literal's NUL and C keeps reading. The port reads NUL. Parity pads literals with NULs so the oracle is deterministic.cf_literal_to_str(NULL, …)returns NULL; C dereferences it.These are listed in
rust/README.mdand in the parity file header.Tier 3 and mutation check
Parity compares, field by field:
file,unexpected_eof, and the base lexer offset;reformattedup towrite_offset;lexback-pointer, andstr/unmerged_stras offsets;numandcapacity.It does this for fresh lexes and for re-lexing a used lexer, on generated C-like sources (splices, comments, strings,
#include <…>, numbers like.5/1.5e3, underscores, CR/LF pairs, high bytes) plus arbitrary bytes.cf_literal_to_stris compared over the wholebzallocbuffer for literals full of escapes, with anycount. Seventeen mutations, all caught, were run before the rebase. The 16 still meaningful are listed below; the 17th "fixed" the closing-quote leak, which #54 has since made the real C behavior:_→ alpha reclassificationunmerged_stroff-by-one "fixed"#include)<…>never a string#includenot required at line startunexpected_eofnever cleared*/star tracking broken.5not a number.\xskips 3strtouloverflow wraps instead of saturatingstrtoulskips no whitespaceVerification
On Windows 11 (MSVC):
soldr cargo test --workspace --lockedpasses (107 tests). Tier 3 runs the shim on this platform../lintpasses.-D warningsis clean for x86_64/aarch64 Linux and aarch64 macOS.Since the rebase, @zackees ran the local gate on Linux (lint, rust-test, Tier 2 OFF/ON, exports identical) and stamped it.
Not verified locally (by me)
test_cf_lexerruns unchanged on the Rust tokenizer with ON, and its preprocessor and parser cases exercise the C preprocessor on Rust-built token arrays.There is no Docker or WSL on this machine.
🤖 Generated with Claude Code