Skip to main content

Module lowering

Module lowering 

Source
Expand description

Lowering of the call-shaped r!() subset to RCall-based Rf_lang* construction.

§Strategy

After grammar validation, the macro attempts to classify the tail token stream as a lowerable call: a single top-level call of the form ident(args…) or pkg::fn(args…) whose every argument belongs to the lowerable atom set (string literals, integer/float literals, symbolic constants, bare identifiers, nested lowerable calls, or name = value named args).

When lowerable, the macro emits code that builds the call tree with RCall + literal→SEXP constructors and evaluates it. This avoids the parse-then-eval round-trip through r_eval_str.

When NOT lowerable (operators, assignments, control flow, [/$/@, any ambiguity), it falls back to the original r_eval_str string path. The fallback is the safety guarantee — no accepted input can start failing.

§Lowerable subset (exhaustive)

§Top-level shape

  • ident(args…) — simple function call
  • pkg::fn(args…) — namespaced call

§Arg atoms

  • String literal ("hello")
  • Integer literal (42L — one proc_macro2 token with the L suffix). An unsuffixed 42 lowers as a double, matching R’s parser (R only produces INTSXP for L-suffixed literals; typeof(42) is "double")
  • Float literal (1.5, 1e3)
  • Symbolic constants: TRUE, FALSE, NULL, NA, NA_integer_, NA_real_, NA_character_, NA_complex_, Inf, NaN
  • Bare identifier (symbol lookup at eval time)
  • Nested lowerable call
  • Named arg: name = <atom>

Everything else → fallback.

Structs§

LowerArg 🔒
A single argument (positional or named).
LowerCall 🔒
A lowerable R call expression.

Enums§

LowerAtom 🔒
An atom that maps directly to an R SEXP constructor.
LowerFun 🔒
Function designator.

Functions§

classify_args 🔒
Parse the argument group of a call into LowerArg list. Returns None if any argument is not lowerable.
classify_atom 🔒
Classify a token slice as a lowerable atom.
classify_call 🔒
Classify a flat token slice as a lowerable top-level call.
classify_one_arg 🔒
Classify one argument slot (possibly name = atom).
emit_atom 🔒
Emit the SEXP-producing expression for an atom.
emit_call 🔒
Emit a TokenStream that builds + evaluates the lowered call.
emit_nested_call 🔒
Emit a nested call expression (returns the SEXP, adds protect_raw lines to the parent scope via a temporary).
split_by_comma_owned 🔒
Split a flat Vec<TokenTree> by top-level commas.
try_lower 🔒
Attempt to lower a validated R tail token stream to a RCall-based TokenStream.
try_parse_r_dotname 🔒
Try to parse a token sequence as an R dot-separated function name.