|
parser 1.0.0
UTF-8 CEL expression compilation and checked library extensions
|
This tutorial compiles decoded text into cached programs, extracts results, and reuses caller-owned stack storage. Start with Build and link the parser with CPM.
parse_str consumes the complete UTF-8 string and returns an owned cel::compiled_segment. This program remains valid after both its compiler and its source string disappear:
execute takes the stack and a span of owning argument values; this program needs none. The returned cel::value owns its result independently of the execution frame. Arrays and tuples can be extracted using the runtime's std::vector and std::tuple adapters. Use at() and field() when you need to inspect a result whose concrete shape is determined at runtime.
For a known result type, prepare the program once and call its typed boundary:
Preparation checks the complete result signature. Calls return independent results and retire their temporary stack suffix. The stack remains reusable after successful calls and exceptions; callback side effects are not rolled back. Use one exclusive evaluator per stack, and synchronize any shared mutable callback state separately.
The cache example compiles three expressions, destroys the compiler, reserves storage around a caller-owned bool prefix, and evaluates each prepared program 1,000 times:
The example-local helpers use only public interfaces. compile_decoded prepares every result; reserve reserves each program's requirements without shrinking existing capacity:
The strings are already decoded UTF-8. Decode JSON or another transport format before passing its string values to parse_str; the compiler does not decode transport escapes.
Use the application from the CPM build tutorial to run this example:
Replace its add_executable line with the example source fetched by CPM:
Build it:
Run the cache test:
Confirm that the test passes and prints cached sum=30000. The test also checks that the bool prefix survives every call.
Measure the stack cursor and depth before reserving, without intervening prefix mutation. Reserve before entering evaluation. Known requirements cover the program's canonical stack storage, not composite payload allocations, arbitrary callback work, or unknown external callable bodies.
Catch cel::parse_error to obtain a diagnostic category and a half-open source interval from span().begin through span().end. Offsets count UTF-8 bytes, not characters. Retain the source yourself if you want to display the diagnostic against it.
Malformed input, unsupported literals, incompatible types, missing operations, delimiter errors, and nesting limits have distinct categories. A failed parse normally leaves the compiler reusable. Client emitters run during compilation; their exceptions propagate without being relabeled as syntax errors. Emitted runtime callbacks execute only when you call the program.
Continue with Add an extension library to compile named client operations.