Skip to content

feat(obs-util): port util/lexer.c to Rust (#6) - #41

Closed
b7216309-jpg wants to merge 3 commits into
obs-rust:masterfrom
b7216309-jpg:rust/lexer-port
Closed

b7216309-jpg wants to merge 3 commits into
obs-rust:masterfrom
b7216309-jpg:rust/lexer-port

Conversation

@b7216309-jpg

@b7216309-jpg b7216309-jpg commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Refs #43 (Phase 3 tracking; #6 closed when #33 merged)

Ports libobs/util/lexer.c (all ten EXPORT functions) to Rust. This is step 3 of the Phase 3 port order, before cf-lexer/cf-parser. It is independent of #40 (utf8) and branches from grind/meta-4-9b41 like that PR.

Commits

  1. RED: the shim, #[repr(C)] mirrors, the oracle, the guard, rust-exports.txt and the tests, against a todo!() core. Tier 1 fails 0/16, Tier 3 aborts on the first call, and the layout tests pass.
  2. GREEN: the safe core. Tier 1 passes 16/16, layout 5/5, and Tier 3 9/9.

What's ported

C Safe core
strref_cmp, strref_cmpi, strref_cmp_strref, strref_cmpi_strref byte-slice compares
valid_int_str, valid_float_str valid_int_str(s, n) / valid_float_str(s, n) (n keeps C's meaning, including the sign not counting toward it)
lexer_getbasetoken get_base_token(text, ignore_ws) -> (advance, Option<BaseToken>)
lexer_getstroffset get_str_offset(text, pos) -> (row, col)
error_data_add, error_data_buildstring format_error_item. The darray push and the bmalloc/bstrdup allocations stay in the shim, since C frees them with bfree. error_data_add reuses ffi::darray::darray_push_back_array, so capacity growth matches.

The lexer.h static inline helpers stay in C under the header-inline exemption. The layout tests cover strref, base_token (including sizeof(enum base_token_type)), error_item, error_data and lexer.

Decisions

  • char signedness. C compares char, so bytes ≥ 0x80 sort below ASCII on x86 and above it on Arm Linux. The core compares via core::ffi::c_char, so it matches the platform. There is a Tier 1 test for this. Clippy is clean for aarch64-unknown-linux-gnu, where c_char is u8.
  • toupper is the C locale's, ASCII only. Parity on Windows/MSVC runs high bytes through strref_cmpi* and matches UCRT.
  • oracle/test_dstr.c: the lexer oracle needs dstr_catf, which lives in util/dstr.c, and that pulls in utf8.c. Rather than linking all of dstr, this test-only dstr_catf (dstr_vcatf with plain brealloc growth, since the capacity isn't observable here) follows the test_bmem.c precedent. The policy doc says to delete it when dstr.c is ported. bmemdup (for bstrdup) is added to test_bmem.c.
  • Intentional difference: lexer_getstroffset with a NULL lex->text leaves row/col untouched, where C dereferences NULL. This is listed in rust/README.md and in the parity file.

Tier 3 and mutation check

Parity compares the shim with the oracle: return values, the lexer offset, every base_token field (pre-filled with sentinels, so the untouched passed_whitespace is checked too), and the full error_data contents (num, capacity, each item, the message copies, and the built string). Inputs are random bytes weighted toward the lexer's special characters (signs, ., e, CR/LF, NUL, high bytes), plus two structured generators.

I ran fourteen mutations on the core. The first generators missed two of them, which led to the two structured generators:

  • Ignoring the C string's NUL in strref_cmp. This needs a strref holding a NUL exactly where the C string ends. The shared-prefix property now allows NULs in the strref's tail.
  • Accepting a sign right after e in valid_float_str. Random characters rarely form 1e-3. number_like() now builds [sign] digits [. digits] [e [sign] digits] [sign] with stray characters.

All fourteen are caught now:

Mutation Caught
no stop at the C string's NUL ✅
NUL-first strref not empty ✅
cmp_strref returns 1 when only the second is empty ✅
cmpi case-sensitive ✅
cmp_strref loop < instead of <= ✅
compare as unsigned / 7-bit ✅
sign counts toward n ✅
sign right after e accepted ✅
. allowed after the exponent ✅
e allowed without digits ✅
no CRLF/LFCR pairs in getbasetoken ✅
no push-back on a type change ✅
getstroffset does not skip pairs ✅
NULL prints as empty instead of (null) ✅

Verification

On Windows 11 (MSVC):

  • soldr cargo test --workspace --locked passes. Unlike utf8, the lexer shim is not platform-gated, so Tier 3 runs the shim here.
  • ./lint passes.
  • cargo clippy -p obs-util -p libobs-rust --lib -D warnings is clean for x86_64-unknown-linux-gnu, aarch64-unknown-linux-gnu and aarch64-apple-darwin.

Not verified locally

  • Tier 2 OFF/ON. test_lexer runs unchanged against the Rust symbols with ON, and cf-lexer.c/cf-parser.c/text-lookup.c link against them. The exported-symbol diff (linux-validate/run.sh, tier2-validate) also has to wait: this machine has no Docker and no WSL. Please watch rust-linux-tier2 and rust-tier2 in CI.
  • The local gate stamp.

🤖 Generated with Claude Code

b7216309-jpg and others added 2 commits October 9, 2026 13:26
Adds the C ABI shim for the ten EXPORT functions of
libobs/util/lexer.c, the #[repr(C)] mirrors of strref, base_token,
error_item, error_data and lexer, the oracle wrapper, the
ENABLE_RUST_LIBOBS guard, rust-exports.txt entries, and Tier 1, Tier 2
layout and Tier 3 tests. The safe core bodies are todo!(), so
soldr cargo test -p obs-util --test lexer fails 0/16 and
--test lexer_parity aborts on the first call; the layout test passes.

The lexer oracle needs dstr_catf and bmemdup. oracle/test_dstr.c is a
test-only dstr_catf (dstr_vcatf with plain brealloc growth), like
test_bmem.c for bmem, so the oracle does not pull in util/dstr.c and
its utf8.c dependency. bmemdup joins test_bmem.c.

Refs obs-rust#6

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Implements the safe core of libobs/util/lexer.c. GREEN for Tier 1
(16/16), the layout tests (5/5) and Tier 3 (9/9).

Characters compare as the platform's C char (c_char), so bytes from
0x80 order as in C on both signed (x86) and unsigned (Arm Linux) char
targets. toupper is the C locale's, ASCII only.

Intentional difference: lexer_getstroffset leaves the outputs untouched
when lex->text is NULL, where C dereferences it.

Refs obs-rust#6

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@b7216309-jpg
b7216309-jpg changed the base branch from grind/meta-4-9b41 to master October 9, 2026 11:38
# Conflicts:
#	libobs/cmake/rust-exports.txt
#	rust/obs-c-oracle/build.rs
#	rust/obs-c-oracle/src/lib.rs
@b7216309-jpg

Copy link
Copy Markdown
Contributor Author

Closing as superseded by #68, which ports this (along with the rest of Phase 3) on top of the merged C fixes #53, #54 and #60. Thanks for picking up the decisions from here. The cf-tokenizer work (#42, #49) isn't covered by #68; I'll rebuild #49 on #68's lexer once it lands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant